From: Fabian Keil Date: Sun, 14 Oct 2007 14:12:41 +0000 (+0000) Subject: When in daemon mode, close stderr after the configuration file has been X-Git-Tag: v_3_0_7~131 X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=commitdiff_plain;h=b8a3f42d3acb904a23af1fd82fc87d1f8e0fd0a5 When in daemon mode, close stderr after the configuration file has been parsed the first time. If logfile isn't set, stop logging. Fixes BR#897436. --- diff --git a/errlog.c b/errlog.c index c887baf2..1e658658 100644 --- a/errlog.c +++ b/errlog.c @@ -1,4 +1,4 @@ -const char errlog_rcs[] = "$Id: errlog.c,v 1.53 2007/08/05 13:53:14 fabiankeil Exp $"; +const char errlog_rcs[] = "$Id: errlog.c,v 1.54 2007/09/22 16:15:34 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/errlog.c,v $ @@ -33,6 +33,10 @@ const char errlog_rcs[] = "$Id: errlog.c,v 1.53 2007/08/05 13:53:14 fabiankeil E * * Revisions : * $Log: errlog.c,v $ + * Revision 1.54 2007/09/22 16:15:34 fabiankeil + * - Let it compile with pcc. + * - Move our includes below system includes to prevent macro conflicts. + * * Revision 1.53 2007/08/05 13:53:14 fabiankeil * #1763173 from Stefan Huehner: declare some more functions * static and use void instead of empty parameter lists. @@ -408,11 +412,11 @@ static void fatal_error(const char * error_message) TermLogWindow(); #else /* if !defined(_WIN32) || defined(_WIN_CONSOLE) */ - fputs(error_message, stderr); + fputs(error_message, logfp); #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */ #if defined(unix) - if(pidfile) + if (pidfile) { unlink(pidfile); } @@ -422,12 +426,104 @@ static void fatal_error(const char * error_message) } +/********************************************************************* + * + * Function : show_version + * + * Description : Logs the Privoxy version and the program name. + * + * Parameters : + * 1 : prog_name = The program name. + * + * Returns : Nothing. + * + *********************************************************************/ +static void show_version(const char *prog_name) +{ + log_error(LOG_LEVEL_INFO, "Privoxy version " VERSION); + if (prog_name != NULL) + { + log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name); + } +} + + +/********************************************************************* + * + * Function : init_log_module + * + * Description : Initializes the logging module to log to stderr. + * Can only be called while stderr hasn't been closed + * yet and is only supposed to be called once. + * + * Parameters : + * 1 : prog_name = The program name. + * + * Returns : Nothing. + * + *********************************************************************/ +void init_log_module(const char *prog_name) +{ + lock_logfile(); + logfp = stderr; + unlock_logfile(); + set_debug_level(debug); + show_version(prog_name); +} + + +/********************************************************************* + * + * Function : set_debug_level + * + * Description : Sets the debug level to the provided value + * plus LOG_LEVEL_MINIMUM. + * + * XXX: we should only use the LOG_LEVEL_MINIMUM + * until the frist time the configuration file has + * been parsed. + * + * Parameters : 1: debug_level = The debug level to set. + * + * Returns : Nothing. + * + *********************************************************************/ +void set_debug_level(int debug_level) +{ + debug = debug_level | LOG_LEVEL_MINIMUM; +} + + +/********************************************************************* + * + * Function : disable_logging + * + * Description : Disables logging. + * + * Parameters : None. + * + * Returns : Nothing. + * + *********************************************************************/ +void disable_logging(void) +{ + lock_logfile(); + if (logfp != NULL) + { + fclose(logfp); + } + logfp = NULL; + unlock_logfile(); +} + + /********************************************************************* * * Function : init_error_log * - * Description : Initializes the logging module. Must call before - * calling log_error. + * Description : Initializes the logging module to log to a file. + * + * XXX: should be renamed. * * Parameters : * 1 : prog_name = The program name. @@ -437,52 +533,44 @@ static void fatal_error(const char * error_message) * Returns : N/A * *********************************************************************/ -void init_error_log(const char *prog_name, const char *logfname, int debuglevel) +void init_error_log(const char *prog_name, const char *logfname) { FILE *fp; - lock_loginit(); + assert(NULL != logfname); - /* set the logging detail level */ - debug = debuglevel | LOG_LEVEL_MINIMUM; + lock_loginit(); + lock_logfile(); if ((logfp != NULL) && (logfp != stderr)) { - log_error(LOG_LEVEL_INFO, "(Re-)Open logfile %s", logfname ? logfname : "none"); - lock_logfile(); - fclose(logfp); - } else { - lock_logfile(); + log_error(LOG_LEVEL_INFO, "(Re-)Open logfile \'%s\'", logfname ? logfname : "none"); } - logfp = stderr; - unlock_logfile(); /* set the designated log file */ - if( logfname ) + fp = fopen(logfname, "a"); + if (NULL == fp) { - if( NULL == (fp = fopen(logfname, "a")) ) - { - log_error(LOG_LEVEL_FATAL, "init_error_log(): can't open logfile: %s", logfname); - } - - /* set logging to be completely unbuffered */ - setbuf(fp, NULL); - - lock_logfile(); - logfp = fp; - unlock_logfile(); + log_error(LOG_LEVEL_FATAL, "init_error_log(): can't open logfile: \'%s\'", logfname); } - log_error(LOG_LEVEL_INFO, "Privoxy version " VERSION); - if (prog_name != NULL) + /* set logging to be completely unbuffered */ + setbuf(fp, NULL); + + if (logfp != NULL) { - log_error(LOG_LEVEL_INFO, "Program name: %s", prog_name); + fclose(logfp); } + logfp = fp; + unlock_logfile(); + + show_version(prog_name); unlock_loginit(); } /* init_error_log */ + #if defined(USE_NEW_LOG_ERROR) /* * Use an alternative log_error version and its helper functions. @@ -791,8 +879,12 @@ void log_error(int loglevel, const char *fmt, ...) } #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */ - /* verify if loglevel applies to current settings and bail out if negative */ - if ((loglevel & debug) == 0) + /* + * verify that the loglevel applies to current + * settings and that logging is enabled. + * Bail out otherwise. + */ + if ((0 == (loglevel & debug)) || (logfp == NULL)) { return; } @@ -811,10 +903,7 @@ void log_error(int loglevel, const char *fmt, ...) snprintf(tempbuf, sizeof(tempbuf), "%s Privoxy(%08lx) Fatal error: log_error() failed to allocate buffer memory.\n" "\nExiting.", timestamp, thread_id); - if( !logfp ) - { - logfp = stderr; - } + assert(NULL != logfp); fputs(tempbuf, logfp); unlock_logfile(); fatal_error(tempbuf); /* Exit */ @@ -1034,20 +1123,14 @@ void log_error(int loglevel, const char *fmt, ...) loglevel = LOG_LEVEL_FATAL; } - /* deal with glibc stupidity - it won't let you initialize logfp */ - /* XXX: Still necessary? */ - if(NULL == logfp) - { - logfp = stderr; - } - - fputs(outbuf_save, logfp); + assert(NULL != logfp); if (loglevel == LOG_LEVEL_FATAL) { fatal_error(outbuf_save); /* Never get here */ } + fputs(outbuf_save, logfp); unlock_logfile(); diff --git a/errlog.h b/errlog.h index 47c63531..def55f51 100644 --- a/errlog.h +++ b/errlog.h @@ -1,6 +1,6 @@ #ifndef ERRLOG_H_INCLUDED #define ERRLOG_H_INCLUDED -#define ERRLOG_H_VERSION "$Id: errlog.h,v 1.17 2007/03/31 13:33:28 fabiankeil Exp $" +#define ERRLOG_H_VERSION "$Id: errlog.h,v 1.18 2007/07/14 07:28:47 fabiankeil Exp $" /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/errlog.h,v $ @@ -35,6 +35,9 @@ * * Revisions : * $Log: errlog.h,v $ + * Revision 1.18 2007/07/14 07:28:47 fabiankeil + * Add translation function for JB_ERR_FOO codes. + * * Revision 1.17 2007/03/31 13:33:28 fabiankeil * Add alternative log_error() with timestamps * that contain milliseconds and without using @@ -174,7 +177,10 @@ extern "C" { #define LOG_LEVEL_ERROR 0x2000 #define LOG_LEVEL_FATAL 0x4000 /* Exits after writing log */ -extern void init_error_log(const char *prog_name, const char *logfname, int debuglevel); +extern void init_error_log(const char *prog_name, const char *logfname); +extern void set_debug_level(int debuglevel); +void disable_logging(void); +void init_log_module(const char *prog_name); extern void log_error(int loglevel, const char *fmt, ...); extern const char *jb_err_to_string(int error); diff --git a/jcc.c b/jcc.c index e5eb7f55..f979b31d 100644 --- a/jcc.c +++ b/jcc.c @@ -1,4 +1,4 @@ -const char jcc_rcs[] = "$Id: jcc.c,v 1.151 2007/09/29 10:21:16 fabiankeil Exp $"; +const char jcc_rcs[] = "$Id: jcc.c,v 1.152 2007/10/04 18:03:34 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/jcc.c,v $ @@ -33,6 +33,14 @@ const char jcc_rcs[] = "$Id: jcc.c,v 1.151 2007/09/29 10:21:16 fabiankeil Exp $" * * Revisions : * $Log: jcc.c,v $ + * Revision 1.152 2007/10/04 18:03:34 fabiankeil + * - Fix a crash when parsing invalid requests whose first header + * is rejected by get_header(). Regression (re?)introduced + * in r1.143 by yours truly. + * - Move ACTION_VANILLA_WAFER handling into parsers.c's + * client_cookie_adder() to make sure send-vanilla-wafer can be + * controlled through tags (and thus regression-tested). + * * Revision 1.151 2007/09/29 10:21:16 fabiankeil * - Move get_filter_function() from jcc.c to filters.c * so the filter functions can be static. @@ -2938,6 +2946,8 @@ int main(int argc, const char *argv[]) #endif ; + init_log_module(Argv[0]); + /* * Parse the command line arguments * @@ -3203,10 +3213,9 @@ int main(int argc, const char *argv[]) close ( fd ); } #endif /* 1 */ - /* FIXME: should close stderr (fd 2) here too, but the test - * for existence - * and load config file is done in listen_loop() and puts - * some messages on stderr there. + /* + * stderr (fd 2) will be closed later on, when the + * log file has been parsed. */ close( 0 ); @@ -3445,7 +3454,7 @@ static void listen_loop(void) */ if (received_hup_signal) { - init_error_log(Argv[0], config->logfile, config->debug); + init_error_log(Argv[0], config->logfile); received_hup_signal = 0; } #endif diff --git a/loadcfg.c b/loadcfg.c index 39c55666..085f4708 100644 --- a/loadcfg.c +++ b/loadcfg.c @@ -1,4 +1,4 @@ -const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.65 2007/07/21 11:51:36 fabiankeil Exp $"; +const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.66 2007/08/05 14:02:09 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/loadcfg.c,v $ @@ -35,6 +35,9 @@ const char loadcfg_rcs[] = "$Id: loadcfg.c,v 1.65 2007/07/21 11:51:36 fabiankeil * * Revisions : * $Log: loadcfg.c,v $ + * Revision 1.66 2007/08/05 14:02:09 fabiankeil + * #1763173 from Stefan Huehner: declare unload_configfile() static. + * * Revision 1.65 2007/07/21 11:51:36 fabiankeil * As Hal noticed, checking dispatch_cgi() as the last cruncher * looks like a bug if CGI requests are blocked unintentionally, @@ -661,6 +664,9 @@ void unload_current_config_file(void) * * Description : Load the config file and all parameters. * + * XXX: more than thousand lines long + * and thus in serious need of refactoring. + * * Parameters : None * * Returns : The configuration_spec, or NULL on error. @@ -676,6 +682,7 @@ struct configuration_spec * load_config(void) struct file_list *fs; unsigned long linenum = 0; int i; + char *logfile = NULL; if ( !check_file_changed(current_configfile, configfile, &fs)) { @@ -1291,8 +1298,11 @@ struct configuration_spec * load_config(void) * In logdir by default * *************************************************************************/ case hash_logfile : - freez(config->logfile); - config->logfile = no_daemon ? NULL : make_path(config->logdir, arg); + logfile = make_path(config->logdir, arg); + if (NULL == logfile) + { + log_error(LOG_LEVEL_FATAL, "Out of memore while creating logfile path"); + } continue; /* ************************************************************************* @@ -1593,13 +1603,31 @@ struct configuration_spec * load_config(void) fclose(configfp); + set_debug_level(config->debug); + + freez(config->logfile); + + if (!no_daemon) + { + if (NULL != logfile) + { + config->logfile = logfile; + log_error(LOG_LEVEL_INFO, + "Switching to daemon mode. Log messages will be written to: %s", config->logfile); + init_error_log(Argv[0], config->logfile); + } + else + { + log_error(LOG_LEVEL_INFO, "No logfile configured while in daemon mode. Logging disabled."); + disable_logging(); + } + } + if (NULL == config->proxy_args) { log_error(LOG_LEVEL_FATAL, "Out of memory loading config - insufficient memory for config->proxy_args"); } - init_error_log(Argv[0], config->logfile, config->debug); - if (config->actions_file[0]) { add_loader(load_actions_file, config);