When in daemon mode, close stderr after the configuration file has been
authorFabian Keil <fk@fabiankeil.de>
Sun, 14 Oct 2007 14:12:41 +0000 (14:12 +0000)
committerFabian Keil <fk@fabiankeil.de>
Sun, 14 Oct 2007 14:12:41 +0000 (14:12 +0000)
parsed the first time. If logfile isn't set, stop logging. Fixes BR#897436.

errlog.c
errlog.h
jcc.c
loadcfg.c

index c887baf..1e65865 100644 (file)
--- 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 $
 /*********************************************************************
  *
  * 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 $
  *
  * 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.
  *    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) */
    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)
 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
 
 #if defined(unix)
-   if(pidfile)
+   if (pidfile)
    {
       unlink(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
  *
 /*********************************************************************
  *
  * 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.
  *
  * Parameters  :
  *          1  :  prog_name  = The program name.
@@ -437,52 +533,44 @@ static void fatal_error(const char * error_message)
  * Returns     :  N/A
  *
  *********************************************************************/
  * 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;
 
 {
    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))
    {
 
    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 */
 
    /* 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 */
 
 
    unlock_loginit();
 
 } /* init_error_log */
 
+
 #if defined(USE_NEW_LOG_ERROR)
 /*
  * Use an alternative log_error version and its helper functions.
 #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) */
 
    }
 #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;
    }
    {
       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);
          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 */
          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;
    }
 
       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 */
    }
 
    if (loglevel == LOG_LEVEL_FATAL)
    {
       fatal_error(outbuf_save);
       /* Never get here */
    }
+   fputs(outbuf_save, logfp);
 
    unlock_logfile();
 
 
    unlock_logfile();
 
index 47c6353..def55f5 100644 (file)
--- a/errlog.h
+++ b/errlog.h
@@ -1,6 +1,6 @@
 #ifndef ERRLOG_H_INCLUDED
 #define ERRLOG_H_INCLUDED
 #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 $
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/errlog.h,v $
@@ -35,6 +35,9 @@
  *
  * Revisions   :
  *    $Log: errlog.h,v $
  *
  * 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
  *    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 */
 
 #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);
 
 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 e5eb7f5..f979b31 100644 (file)
--- 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 $
 /*********************************************************************
  *
  * 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 $
  *
  * 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.
  *    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
       ;
 
 #endif
       ;
 
+   init_log_module(Argv[0]);
+
    /*
     * Parse the command line arguments
     *
    /*
     * Parse the command line arguments
     *
@@ -3203,10 +3213,9 @@ int main(int argc, const char *argv[])
          close ( fd );
       }
 #endif /* 1 */
          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 );
        */
 
       close( 0 );
@@ -3445,7 +3454,7 @@ static void listen_loop(void)
        */
       if (received_hup_signal)
       {
        */
       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
          received_hup_signal = 0;
       }
 #endif
index 39c5566..085f470 100644 (file)
--- 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 $
 /*********************************************************************
  *
  * 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 $
  *
  * 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,
  *    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.
  *
  *
  * 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.
  * 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;
    struct file_list *fs;
    unsigned long linenum = 0;
    int i;
+   char *logfile = NULL;
 
    if ( !check_file_changed(current_configfile, configfile, &fs))
    {
 
    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 :
  * 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;
 
 /* *************************************************************************
             continue;
 
 /* *************************************************************************
@@ -1593,13 +1603,31 @@ struct configuration_spec * load_config(void)
 
    fclose(configfp);
 
 
    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");
    }
 
    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);
    if (config->actions_file[0])
    {
       add_loader(load_actions_file, config);