2001-12-24 06:00:36 +08:00
|
|
|
/* $Id: tinyproxy.c,v 1.21 2001-12-23 22:00:36 rjkaes Exp $
|
2000-02-17 01:32:49 +08:00
|
|
|
*
|
2000-09-12 08:03:53 +08:00
|
|
|
* The initialise routine. Basically sets up all the initial stuff (logfile,
|
2000-02-17 01:32:49 +08:00
|
|
|
* listening socket, config options, etc.) and then sits there and loops
|
|
|
|
* over the new connections until the daemon is closed. Also has additional
|
|
|
|
* functions to handle the "user friendly" aspects of a program (usage,
|
|
|
|
* stats, etc.) Like any good program, most of the work is actually done
|
|
|
|
* elsewhere.
|
|
|
|
*
|
|
|
|
* Copyright (C) 1998 Steven Young
|
|
|
|
* Copyright (C) 1999 Robert James Kaes (rjkaes@flarenet.com)
|
|
|
|
* Copyright (C) 2000 Chris Lightfoot (chris@ex-parrot.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2, or (at your option) any
|
|
|
|
* later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2000-09-12 08:03:53 +08:00
|
|
|
#include "tinyproxy.h"
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2000-09-12 08:03:53 +08:00
|
|
|
#include "anonymous.h"
|
2000-02-17 01:32:49 +08:00
|
|
|
#include "buffer.h"
|
|
|
|
#include "filter.h"
|
2000-09-12 08:03:53 +08:00
|
|
|
#include "log.h"
|
|
|
|
#include "reqs.h"
|
|
|
|
#include "sock.h"
|
|
|
|
#include "stats.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "utils.h"
|
2000-02-17 01:32:49 +08:00
|
|
|
|
|
|
|
void takesig(int sig);
|
|
|
|
|
2000-09-12 08:03:53 +08:00
|
|
|
extern int yyparse(void);
|
|
|
|
extern FILE *yyin;
|
|
|
|
|
2000-02-17 01:32:49 +08:00
|
|
|
/*
|
|
|
|
* Global Structures
|
|
|
|
*/
|
2000-09-12 08:03:53 +08:00
|
|
|
struct config_s config;
|
2000-02-17 01:32:49 +08:00
|
|
|
float load = 0.00;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle a signal
|
|
|
|
*/
|
2001-11-22 08:31:10 +08:00
|
|
|
void
|
|
|
|
takesig(int sig)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
|
|
|
switch (sig) {
|
|
|
|
case SIGHUP:
|
2001-09-07 12:21:07 +08:00
|
|
|
log_message(LOG_NOTICE, "SIGHUP received, cleaning up.");
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-09-16 05:29:22 +08:00
|
|
|
if (config.logf) {
|
|
|
|
char *rename_file;
|
2001-09-16 13:38:27 +08:00
|
|
|
int log_file_des;
|
|
|
|
FILE *old_fd;
|
2001-09-16 05:29:22 +08:00
|
|
|
|
|
|
|
rename_file = safemalloc(strlen(config.logf_name) + 5);
|
|
|
|
if (!rename_file) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Could not allocate memory in signal handler!\n");
|
2001-09-16 05:29:22 +08:00
|
|
|
exit(EX_OSERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
strcpy(rename_file, config.logf_name);
|
|
|
|
strcat(rename_file, ".rot");
|
|
|
|
|
|
|
|
rename(config.logf_name, rename_file);
|
|
|
|
|
2001-09-16 13:38:27 +08:00
|
|
|
log_file_des = create_file_safely(config.logf_name);
|
|
|
|
if (log_file_des < 0) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Could not safely create new log file.\n");
|
2001-09-16 05:29:22 +08:00
|
|
|
exit(EX_OSERR);
|
|
|
|
}
|
2001-09-16 13:38:27 +08:00
|
|
|
|
|
|
|
old_fd = config.logf;
|
2001-11-22 08:31:10 +08:00
|
|
|
|
2001-09-16 13:38:27 +08:00
|
|
|
if (!(config.logf = fdopen(log_file_des, "w"))) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Could not create new log file.\n");
|
2001-09-16 05:29:22 +08:00
|
|
|
exit(EX_CANTCREAT);
|
|
|
|
}
|
|
|
|
|
2001-09-16 13:38:27 +08:00
|
|
|
fclose(old_fd);
|
|
|
|
|
2001-09-16 05:29:22 +08:00
|
|
|
log_message(LOG_NOTICE, "Log file rotated.");
|
|
|
|
|
|
|
|
safefree(rename_file);
|
|
|
|
}
|
2000-02-17 01:32:49 +08:00
|
|
|
#ifdef FILTER_ENABLE
|
|
|
|
if (config.filter) {
|
|
|
|
filter_destroy();
|
|
|
|
filter_init();
|
|
|
|
}
|
2001-05-27 10:36:22 +08:00
|
|
|
log_message(LOG_NOTICE, "Re-reading filter file.");
|
2000-02-17 01:32:49 +08:00
|
|
|
#endif /* FILTER_ENABLE */
|
2001-11-22 08:31:10 +08:00
|
|
|
log_message(LOG_NOTICE,
|
|
|
|
"Finished cleaning memory/connections.");
|
2000-02-17 01:32:49 +08:00
|
|
|
break;
|
|
|
|
case SIGTERM:
|
|
|
|
#ifdef FILTER_ENABLE
|
|
|
|
if (config.filter)
|
|
|
|
filter_destroy();
|
|
|
|
#endif /* FILTER_ENABLE */
|
|
|
|
config.quit = TRUE;
|
2001-05-27 10:36:22 +08:00
|
|
|
log_message(LOG_INFO, "SIGTERM received.");
|
2000-02-17 01:32:49 +08:00
|
|
|
break;
|
|
|
|
}
|
2000-12-08 11:35:07 +08:00
|
|
|
|
2000-02-17 01:32:49 +08:00
|
|
|
if (sig != SIGTERM)
|
|
|
|
signal(sig, takesig);
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-09-12 08:03:53 +08:00
|
|
|
* Display the version information for the user.
|
|
|
|
*/
|
2001-11-22 08:31:10 +08:00
|
|
|
static void
|
|
|
|
display_version(void)
|
2000-09-12 08:03:53 +08:00
|
|
|
{
|
2001-08-27 05:17:30 +08:00
|
|
|
printf("%s %s (%s)\n", PACKAGE, VERSION, TARGET_SYSTEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display the copyright and license for this program.
|
|
|
|
*/
|
2001-11-22 08:31:10 +08:00
|
|
|
static void
|
|
|
|
display_license(void)
|
2001-08-27 05:17:30 +08:00
|
|
|
{
|
|
|
|
display_version();
|
|
|
|
|
2000-09-12 08:03:53 +08:00
|
|
|
printf("\
|
2001-08-27 05:17:30 +08:00
|
|
|
Copyright 1998 Steven Young (sdyoung@well.com)\n\
|
|
|
|
Copyright 1998-2001 Robert James Kaes (rjkaes@users.sourceforge.net)\n\
|
|
|
|
Copyright 1999 George Talusan (gstalusan@uwaterloo.ca)\n\
|
|
|
|
Copyright 2000 Chris Lightfoot (chris@ex-parrot.com)\n\
|
|
|
|
\n\
|
|
|
|
This program is free software; you can redistribute it and/or modify\n\
|
|
|
|
it under the terms of the GNU General Public License as published by\n\
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)\n\
|
|
|
|
any later version.\n\
|
2000-09-12 08:03:53 +08:00
|
|
|
\n\
|
2001-08-27 05:17:30 +08:00
|
|
|
This program is distributed in the hope that it will be useful,\n\
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
|
|
|
|
GNU General Public License for more details.\n\
|
2000-09-12 08:03:53 +08:00
|
|
|
\n\
|
2001-08-27 05:17:30 +08:00
|
|
|
You should have received a copy of the GNU General Public License\n\
|
|
|
|
along with this program; if not, write to the Free Software\n\
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.\n");
|
2000-09-12 08:03:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Display usage to the user.
|
2000-02-17 01:32:49 +08:00
|
|
|
*/
|
2001-11-22 08:31:10 +08:00
|
|
|
static void
|
|
|
|
display_usage(void)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
2001-08-27 05:17:30 +08:00
|
|
|
printf("Usage: %s [options]\n", PACKAGE);
|
2000-09-12 08:03:53 +08:00
|
|
|
printf("\
|
|
|
|
Options:\n\
|
|
|
|
-d Operate in DEBUG mode.\n\
|
|
|
|
-c FILE Use an alternate configuration file.\n\
|
|
|
|
-h Display this usage information.\n\
|
2001-08-27 05:17:30 +08:00
|
|
|
-l Display the license.\n\
|
2000-09-12 08:03:53 +08:00
|
|
|
-v Display the version number.\n");
|
|
|
|
|
2000-02-17 01:32:49 +08:00
|
|
|
/* Display the modes compiled into tinyproxy */
|
|
|
|
printf("\nFeatures Compiled In:\n");
|
2000-09-12 08:03:53 +08:00
|
|
|
#ifdef XTINYPROXY_ENABLE
|
2000-02-17 01:32:49 +08:00
|
|
|
printf(" XTinyproxy Header\n");
|
|
|
|
#endif /* XTINYPROXY */
|
|
|
|
#ifdef FILTER_ENABLE
|
|
|
|
printf(" Filtering\n");
|
|
|
|
printf(" * with Regular Expression support\n");
|
|
|
|
#endif /* FILTER_ENABLE */
|
|
|
|
#ifndef NDEBUG
|
2000-11-23 12:46:48 +08:00
|
|
|
printf(" Debugging code\n");
|
2000-02-17 01:32:49 +08:00
|
|
|
#endif /* NDEBUG */
|
2000-09-12 08:03:53 +08:00
|
|
|
#ifdef TUNNEL_SUPPORT
|
|
|
|
printf(" TCP Tunneling\n");
|
|
|
|
#endif /* TUNNEL_SUPPORT */
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
2001-11-22 08:31:10 +08:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
2000-02-17 01:32:49 +08:00
|
|
|
{
|
|
|
|
int optch;
|
2000-09-12 08:03:53 +08:00
|
|
|
bool_t godaemon = TRUE;
|
2000-02-17 01:32:49 +08:00
|
|
|
struct passwd *thisuser = NULL;
|
2000-09-12 08:03:53 +08:00
|
|
|
struct group *thisgroup = NULL;
|
|
|
|
char *conf_file = DEFAULT_CONF_FILE;
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-05-27 10:36:22 +08:00
|
|
|
/*
|
|
|
|
* Disable the creation of CORE files right up front.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_SETRLIMIT
|
2001-11-22 08:31:10 +08:00
|
|
|
struct rlimit core_limit = { 0, 0 };
|
2001-05-27 10:36:22 +08:00
|
|
|
if (setrlimit(RLIMIT_CORE, &core_limit) < 0) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr, "%s: Could not set the core limit to zero.\n",
|
|
|
|
argv[0]);
|
2001-05-27 10:36:22 +08:00
|
|
|
exit(EX_SOFTWARE);
|
|
|
|
}
|
2001-11-22 08:31:10 +08:00
|
|
|
#endif /* HAVE_SETRLIMIT */
|
2001-05-27 10:36:22 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Process the various options
|
|
|
|
*/
|
2001-11-22 08:31:10 +08:00
|
|
|
while ((optch = getopt(argc, argv, "c:vldh")) != EOF) {
|
2000-02-17 01:32:49 +08:00
|
|
|
switch (optch) {
|
|
|
|
case 'v':
|
2001-08-27 05:17:30 +08:00
|
|
|
display_version();
|
|
|
|
exit(EX_OK);
|
|
|
|
case 'l':
|
|
|
|
display_license();
|
2000-02-17 01:32:49 +08:00
|
|
|
exit(EX_OK);
|
|
|
|
case 'd':
|
|
|
|
godaemon = FALSE;
|
|
|
|
break;
|
2000-09-12 08:03:53 +08:00
|
|
|
case 'c':
|
|
|
|
conf_file = strdup(optarg);
|
|
|
|
if (!conf_file) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: Could not allocate memory.\n",
|
|
|
|
argv[0]);
|
2000-03-12 04:37:44 +08:00
|
|
|
exit(EX_SOFTWARE);
|
|
|
|
}
|
|
|
|
break;
|
2000-02-17 01:32:49 +08:00
|
|
|
case 'h':
|
|
|
|
default:
|
2001-11-22 08:31:10 +08:00
|
|
|
display_usage();
|
2000-09-12 08:03:53 +08:00
|
|
|
exit(EX_OK);
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-01 04:08:19 +08:00
|
|
|
/*
|
2000-09-12 08:03:53 +08:00
|
|
|
* Read in the settings from the config file.
|
2000-04-01 04:08:19 +08:00
|
|
|
*/
|
2000-09-12 08:03:53 +08:00
|
|
|
yyin = fopen(conf_file, "r");
|
|
|
|
if (!yyin) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: Could not open configuration file \"%s\".\n",
|
|
|
|
argv[0], conf_file);
|
2000-02-17 01:32:49 +08:00
|
|
|
exit(EX_SOFTWARE);
|
|
|
|
}
|
2000-09-12 08:03:53 +08:00
|
|
|
yyparse();
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-09-17 04:11:54 +08:00
|
|
|
#if defined(TUNNEL_SUPPORT) && defined(UPSTREAM_SUPPORT)
|
|
|
|
if (config.tunnel_name && config.upstream_name) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: \"Tunnel\" and \"Upstream\" directives can not be both set.\n",
|
|
|
|
argv[0]);
|
2001-09-17 04:11:54 +08:00
|
|
|
exit(EX_SOFTWARE);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-02-17 01:32:49 +08:00
|
|
|
/* Open the log file if not using syslog */
|
|
|
|
if (config.syslog == FALSE) {
|
2001-09-16 05:29:22 +08:00
|
|
|
int log_file_fd;
|
|
|
|
|
2000-09-12 08:03:53 +08:00
|
|
|
if (!config.logf_name) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: You MUST set a LogFile in the configuration file.\n",
|
|
|
|
argv[0]);
|
2001-06-05 07:30:34 +08:00
|
|
|
exit(EX_SOFTWARE);
|
2000-09-12 08:03:53 +08:00
|
|
|
}
|
|
|
|
|
2001-09-16 05:29:22 +08:00
|
|
|
log_file_fd = create_file_safely(config.logf_name);
|
|
|
|
if (log_file_fd < 0) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"Could not safely create logfile \"%s\".\n",
|
|
|
|
config.logf_name);
|
2001-09-16 05:29:22 +08:00
|
|
|
exit(EX_CANTCREAT);
|
|
|
|
}
|
|
|
|
|
|
|
|
config.logf = fdopen(log_file_fd, "w");
|
|
|
|
if (!config.logf) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr, "Could not write to log file \"%s\".\n",
|
|
|
|
config.logf_name);
|
2000-02-17 01:32:49 +08:00
|
|
|
exit(EX_CANTCREAT);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (godaemon == TRUE)
|
|
|
|
openlog("tinyproxy", LOG_PID, LOG_DAEMON);
|
|
|
|
else
|
|
|
|
openlog("tinyproxy", LOG_PID, LOG_USER);
|
|
|
|
}
|
|
|
|
|
2001-05-27 10:36:22 +08:00
|
|
|
log_message(LOG_INFO, PACKAGE " " VERSION " starting...");
|
2000-09-12 08:03:53 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the default values if they were not set in the config file.
|
|
|
|
*/
|
|
|
|
if (config.port == 0) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: You MUST set a Port in the configuration file.\n",
|
|
|
|
argv[0]);
|
2001-06-05 07:30:34 +08:00
|
|
|
exit(EX_SOFTWARE);
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
2000-09-12 08:03:53 +08:00
|
|
|
if (!config.stathost) {
|
2001-11-22 08:31:10 +08:00
|
|
|
log_message(LOG_INFO, "Setting stathost to \"%s\".",
|
|
|
|
DEFAULT_STATHOST);
|
2000-09-12 08:03:53 +08:00
|
|
|
config.stathost = DEFAULT_STATHOST;
|
|
|
|
}
|
|
|
|
if (!config.username) {
|
2001-11-22 08:31:10 +08:00
|
|
|
log_message(LOG_WARNING,
|
|
|
|
"You SHOULD set a UserName in the configuration file. Using current user instead.");
|
2000-09-12 08:03:53 +08:00
|
|
|
}
|
|
|
|
if (config.idletimeout == 0) {
|
2001-12-24 06:00:36 +08:00
|
|
|
log_message(LOG_WARNING, "Invalid idle time setting. Only values greater than zero allowed; therefore setting idle timeout to %u seconds.",
|
2001-11-22 08:31:10 +08:00
|
|
|
MAX_IDLE_TIME);
|
2000-09-12 08:03:53 +08:00
|
|
|
config.idletimeout = MAX_IDLE_TIME;
|
|
|
|
}
|
|
|
|
|
|
|
|
init_stats();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If ANONYMOUS is turned on, make sure that Content-Length is
|
|
|
|
* in the list of allowed headers, since it is required in a
|
|
|
|
* HTTP/1.0 request. Also add the Content-Type header since it goes
|
|
|
|
* hand in hand with Content-Length.
|
|
|
|
* - rjkaes
|
|
|
|
*/
|
2001-08-27 05:17:30 +08:00
|
|
|
if (is_anonymous_enabled()) {
|
|
|
|
anonymous_insert("Content-Length");
|
|
|
|
anonymous_insert("Content-Type");
|
2000-09-12 08:03:53 +08:00
|
|
|
}
|
|
|
|
|
2000-02-17 01:32:49 +08:00
|
|
|
if (godaemon == TRUE)
|
|
|
|
makedaemon();
|
2000-09-12 08:03:53 +08:00
|
|
|
|
|
|
|
if (config.pidpath) {
|
|
|
|
pidfile_create(config.pidpath);
|
|
|
|
}
|
2000-02-17 01:32:49 +08:00
|
|
|
|
|
|
|
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr, "%s: Could not set the \"SIGPIPE\" signal.\n",
|
|
|
|
argv[0]);
|
2000-02-17 01:32:49 +08:00
|
|
|
exit(EX_OSERR);
|
|
|
|
}
|
2000-09-12 08:03:53 +08:00
|
|
|
#ifdef FILTER_ENABLE
|
|
|
|
if (config.filter)
|
|
|
|
filter_init();
|
|
|
|
#endif /* FILTER_ENABLE */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Start listening on the selected port.
|
|
|
|
*/
|
|
|
|
if (thread_listening_sock(config.port) < 0) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr, "%s: Could not create listening socket.\n",
|
|
|
|
argv[0]);
|
2000-02-17 01:32:49 +08:00
|
|
|
exit(EX_OSERR);
|
|
|
|
}
|
2000-09-12 08:03:53 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Switch to a different user.
|
|
|
|
*/
|
|
|
|
if (geteuid() == 0) {
|
|
|
|
if (config.group && strlen(config.group) > 0) {
|
|
|
|
thisgroup = getgrnam(config.group);
|
|
|
|
if (!thisgroup) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: Unable to find group \"%s\".\n",
|
|
|
|
argv[0], config.group);
|
2000-09-12 08:03:53 +08:00
|
|
|
exit(EX_NOUSER);
|
|
|
|
}
|
|
|
|
if (setgid(thisgroup->gr_gid) < 0) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: Unable to change to group \"%s\".\n",
|
|
|
|
argv[0], config.group);
|
2000-09-12 08:03:53 +08:00
|
|
|
exit(EX_CANTCREAT);
|
|
|
|
}
|
2001-11-22 08:31:10 +08:00
|
|
|
log_message(LOG_INFO, "Now running as group \"%s\".",
|
|
|
|
config.group);
|
2000-09-12 08:03:53 +08:00
|
|
|
}
|
|
|
|
if (config.username && strlen(config.username) > 0) {
|
|
|
|
thisuser = getpwnam(config.username);
|
|
|
|
if (!thisuser) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: Unable to find user \"%s\".",
|
|
|
|
argv[0], config.username);
|
2000-09-12 08:03:53 +08:00
|
|
|
exit(EX_NOUSER);
|
|
|
|
}
|
|
|
|
if (setuid(thisuser->pw_uid) < 0) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr,
|
|
|
|
"%s: Unable to change to user \"%s\".",
|
|
|
|
argv[0], config.username);
|
2000-09-12 08:03:53 +08:00
|
|
|
exit(EX_CANTCREAT);
|
|
|
|
}
|
2001-11-22 08:31:10 +08:00
|
|
|
log_message(LOG_INFO, "Now running as user \"%s\".",
|
|
|
|
config.username);
|
2000-09-12 08:03:53 +08:00
|
|
|
}
|
|
|
|
} else {
|
2001-11-22 08:31:10 +08:00
|
|
|
log_message(LOG_WARNING,
|
|
|
|
"Not running as root, so not changing UID/GID.");
|
2000-09-12 08:03:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (thread_pool_create() < 0) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr, "%s: Could not create the pool of threads.",
|
|
|
|
argv[0]);
|
2000-09-12 08:03:53 +08:00
|
|
|
exit(EX_SOFTWARE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These signals are only for the main thread.
|
|
|
|
*/
|
2001-05-27 10:36:22 +08:00
|
|
|
log_message(LOG_INFO, "Setting the various signals.");
|
2000-02-17 01:32:49 +08:00
|
|
|
if (signal(SIGTERM, takesig) == SIG_ERR) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr, "%s: Could not set the \"SIGTERM\" signal.\n",
|
|
|
|
argv[0]);
|
2000-02-17 01:32:49 +08:00
|
|
|
exit(EX_OSERR);
|
|
|
|
}
|
|
|
|
if (signal(SIGHUP, takesig) == SIG_ERR) {
|
2001-11-22 08:31:10 +08:00
|
|
|
fprintf(stderr, "%s: Could not set the \"SIGHUP\" signal.\n",
|
|
|
|
argv[0]);
|
2000-02-17 01:32:49 +08:00
|
|
|
exit(EX_OSERR);
|
|
|
|
}
|
|
|
|
|
2000-10-24 05:44:43 +08:00
|
|
|
/*
|
|
|
|
* Start the main loop.
|
|
|
|
*/
|
2001-05-27 10:36:22 +08:00
|
|
|
log_message(LOG_INFO, "Starting main loop. Accepting connections.");
|
2000-12-08 11:35:07 +08:00
|
|
|
do {
|
|
|
|
thread_main_loop();
|
|
|
|
sleep(1);
|
|
|
|
} while (!config.quit);
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2001-05-27 10:36:22 +08:00
|
|
|
log_message(LOG_INFO, "Shutting down.");
|
2000-09-12 08:03:53 +08:00
|
|
|
thread_close_sock();
|
2000-02-17 01:32:49 +08:00
|
|
|
|
2000-09-12 08:03:53 +08:00
|
|
|
/*
|
|
|
|
* Remove the PID file.
|
|
|
|
*/
|
|
|
|
if (unlink(config.pidpath) < 0) {
|
2001-11-22 08:31:10 +08:00
|
|
|
log_message(LOG_WARNING,
|
|
|
|
"Could not remove PID file \"%s\": %s.",
|
2001-08-27 05:17:30 +08:00
|
|
|
config.pidpath, strerror(errno));
|
2000-02-17 01:32:49 +08:00
|
|
|
}
|
|
|
|
#ifdef FILTER_ENABLE
|
|
|
|
if (config.filter)
|
|
|
|
filter_destroy();
|
|
|
|
#endif /* FILTER_ENABLE */
|
|
|
|
|
|
|
|
if (config.syslog == FALSE)
|
|
|
|
fclose(config.logf);
|
|
|
|
else
|
|
|
|
closelog();
|
|
|
|
|
|
|
|
exit(EX_OK);
|
|
|
|
}
|