Rearrange struct client_state to reduce memory on amd64
[privoxy.git] / errlog.c
index c54ec4c..969c0bd 100644 (file)
--- a/errlog.c
+++ b/errlog.c
@@ -1,4 +1,3 @@
-const char errlog_rcs[] = "$Id: errlog.c,v 1.122 2014/10/18 11:30:40 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/errlog.c,v $
@@ -7,7 +6,7 @@ const char errlog_rcs[] = "$Id: errlog.c,v 1.122 2014/10/18 11:30:40 fabiankeil
  *                printf-like fashion.
  *
  * Copyright   :  Written by and Copyright (C) 2001-2014 the
- *                Privoxy team. http://www.privoxy.org/
+ *                Privoxy team. https://www.privoxy.org/
  *
  *                Based on the Internet Junkbuster originally written
  *                by and Copyright (C) 1997 Anonymous Coders and
@@ -46,9 +45,9 @@ const char errlog_rcs[] = "$Id: errlog.c,v 1.122 2014/10/18 11:30:40 fabiankeil
 /* For gettimeofday() */
 #include <sys/time.h>
 
-#if !defined(_WIN32) && !defined(__OS2__)
+#if !defined(_WIN32)
 #include <unistd.h>
-#endif /* !defined(_WIN32) && !defined(__OS2__) */
+#endif /* !defined(_WIN32) */
 
 #include <errno.h>
 #include <assert.h>
@@ -66,12 +65,6 @@ const char errlog_rcs[] = "$Id: errlog.c,v 1.122 2014/10/18 11:30:40 fabiankeil
 #define inline __inline
 #endif /* def _MSC_VER */
 
-#ifdef __OS2__
-#include <sys/socket.h> /* For sock_errno */
-#define INCL_DOS
-#include <os2.h>
-#endif
-
 #include "errlog.h"
 #include "project.h"
 #include "jcc.h"
@@ -79,9 +72,6 @@ const char errlog_rcs[] = "$Id: errlog.c,v 1.122 2014/10/18 11:30:40 fabiankeil
 #include "jbsockets.h"
 #endif
 
-const char errlog_h_rcs[] = ERRLOG_H_VERSION;
-
-
 /*
  * LOG_LEVEL_FATAL cannot be turned off.  (There are
  * some exceptional situations where we need to get a
@@ -100,24 +90,21 @@ static void fatal_error(const char * error_message);
 #ifdef _WIN32
 static char *w32_socket_strerr(int errcode, char *tmp_buf);
 #endif
-#ifdef __OS2__
-static char *os2_socket_strerr(int errcode, char *tmp_buf);
-#endif
 
 #ifdef MUTEX_LOCKS_AVAILABLE
-static inline void lock_logfile(void)
+static void lock_logfile(void)
 {
    privoxy_mutex_lock(&log_mutex);
 }
-static inline void unlock_logfile(void)
+static void unlock_logfile(void)
 {
    privoxy_mutex_unlock(&log_mutex);
 }
-static inline void lock_loginit(void)
+static void lock_loginit(void)
 {
    privoxy_mutex_lock(&log_init_mutex);
 }
-static inline void unlock_loginit(void)
+static void unlock_loginit(void)
 {
    privoxy_mutex_unlock(&log_init_mutex);
 }
@@ -127,10 +114,10 @@ static inline void unlock_loginit(void)
  * The locking/unlocking functions below should be
  * fleshed out for non-pthread implementations.
  */
-static inline void lock_logfile() {}
-static inline void unlock_logfile() {}
-static inline void lock_loginit() {}
-static inline void unlock_loginit() {}
+static void lock_logfile() {}
+static void unlock_logfile() {}
+static void lock_loginit() {}
+static void unlock_loginit() {}
 #endif
 
 /*********************************************************************
@@ -245,6 +232,17 @@ void init_log_module(void)
  *********************************************************************/
 void set_debug_level(int debug_level)
 {
+#ifdef FUZZ
+   if (LOG_LEVEL_STFU == debug_level)
+   {
+      debug = LOG_LEVEL_STFU;
+   }
+   if (LOG_LEVEL_STFU == debug)
+   {
+      return;
+   }
+#endif
+
    debug = debug_level | LOG_LEVEL_MINIMUM;
 }
 
@@ -324,9 +322,9 @@ void init_error_log(const char *prog_name, const char *logfname)
    if ((NULL == fp) && (logfp != NULL))
    {
       /*
-       * Some platforms (like OS/2) don't allow us to open
-       * the same file twice, therefore we give it another
-       * shot after closing the old file descriptor first.
+       * Some platforms (like OS/2 (XXX: no longer supported)) don't
+       * allow us to open the same file twice, therefore we give it
+       * another shot after closing the old file descriptor first.
        *
        * We don't do it right away because it prevents us
        * from logging the "can't open logfile" message to
@@ -411,29 +409,19 @@ static long get_thread_id(void)
 {
    long this_thread;
 
-#ifdef __OS2__
-   PTIB     ptib;
-   APIRET   ulrc; /* XXX: I have no clue what this does */
-#endif /* __OS2__ */
-
    /* FIXME get current thread id */
 #ifdef FEATURE_PTHREAD
    this_thread = (long)pthread_self();
 #ifdef __MACH__
    /*
-    * Mac OSX (and perhaps other Mach instances) doesn't have a debuggable
-    * value at the first 4 bytes of pthread_self()'s return value, a pthread_t.
-    * pthread_t is supposed to be opaque... but it's fairly random, though, so
-    * we make it mostly presentable.
+    * Mac OSX (and perhaps other Mach instances) doesn't have a unique
+    * value at the lowest order 4 bytes of pthread_self()'s return value, a pthread_t,
+    * so trim the three lowest-order bytes from the value (16^3).
     */
-   this_thread = labs(this_thread % 1000);
+   this_thread = this_thread / 4096;
 #endif /* def __MACH__ */
 #elif defined(_WIN32)
    this_thread = GetCurrentThreadId();
-#elif defined(__OS2__)
-   ulrc = DosGetInfoBlocks(&ptib, NULL);
-   if (ulrc == 0)
-     this_thread = ptib -> tib_ptib2 -> tib2_ultid;
 #else
    /* Forking instead of threading. */
    this_thread = 1;
@@ -456,7 +444,7 @@ static long get_thread_id(void)
  * Returns     :  Number of written characters or 0 for error.
  *
  *********************************************************************/
-static inline size_t get_log_timestamp(char *buffer, size_t buffer_size)
+static size_t get_log_timestamp(char *buffer, size_t buffer_size)
 {
    size_t length;
    time_t now;
@@ -510,7 +498,7 @@ static inline size_t get_log_timestamp(char *buffer, size_t buffer_size)
  * Returns     :  Number of written characters or 0 for error.
  *
  *********************************************************************/
-static inline size_t get_clf_timestamp(char *buffer, size_t buffer_size)
+static size_t get_clf_timestamp(char *buffer, size_t buffer_size)
 {
    /*
     * Complex because not all OSs have tm_gmtoff or
@@ -527,21 +515,12 @@ static inline size_t get_clf_timestamp(char *buffer, size_t buffer_size)
    int tz_length = 0;
 
    time (&now);
-#ifdef HAVE_GMTIME_R
-   gmt = *gmtime_r(&now, &gmt);
-#elif defined(MUTEX_LOCKS_AVAILABLE)
-   privoxy_mutex_lock(&gmtime_mutex);
-   gmt = *gmtime(&now);
-   privoxy_mutex_unlock(&gmtime_mutex);
-#else
-   gmt = *gmtime(&now);
-#endif
+   gmt = *privoxy_gmtime_r(&now, &gmt);
 #ifdef HAVE_LOCALTIME_R
    tm_now = localtime_r(&now, &dummy);
 #elif defined(MUTEX_LOCKS_AVAILABLE)
    privoxy_mutex_lock(&localtime_mutex);
    tm_now = localtime(&now);
-   privoxy_mutex_unlock(&localtime_mutex);
 #else
    tm_now = localtime(&now);
 #endif
@@ -550,6 +529,9 @@ static inline size_t get_clf_timestamp(char *buffer, size_t buffer_size)
    mins = hrs * 60 + tm_now->tm_min - gmt.tm_min;
 
    length = strftime(buffer, buffer_size, "%d/%b/%Y:%H:%M:%S ", tm_now);
+#if !defined(HAVE_LOCALTIME_R) && defined(MUTEX_LOCKS_AVAILABLE)
+   privoxy_mutex_unlock(&localtime_mutex);
+#endif
 
    if (length > (size_t)0)
    {
@@ -581,7 +563,7 @@ static inline size_t get_clf_timestamp(char *buffer, size_t buffer_size)
  * Returns     :  Log level string.
  *
  *********************************************************************/
-static inline const char *get_log_level_string(int loglevel)
+static const char *get_log_level_string(int loglevel)
 {
    char *log_level_string = NULL;
 
@@ -595,12 +577,15 @@ static inline const char *get_log_level_string(int loglevel)
       case LOG_LEVEL_FATAL:
          log_level_string = "Fatal error";
          break;
-      case LOG_LEVEL_GPC:
+      case LOG_LEVEL_REQUEST:
          log_level_string = "Request";
          break;
       case LOG_LEVEL_CONNECT:
          log_level_string = "Connect";
          break;
+     case LOG_LEVEL_TAGGING:
+         log_level_string = "Tagging";
+         break;
       case LOG_LEVEL_WRITING:
          log_level_string = "Writing";
          break;
@@ -664,19 +649,13 @@ static inline const char *get_log_level_string(int loglevel)
 void log_error(int loglevel, const char *fmt, ...)
 {
    va_list ap;
-   char *outbuf = NULL;
-   static char *outbuf_save = NULL;
+   char outbuf[LOG_BUFFER_SIZE+1];
    char tempbuf[LOG_BUFFER_SIZE];
    size_t length = 0;
    const char * src = fmt;
    long thread_id;
    char timestamp[30];
-   /*
-    * XXX: Make this a config option,
-    * why else do we allocate instead of using
-    * an array?
-    */
-   size_t log_buffer_size = LOG_BUFFER_SIZE;
+   const size_t log_buffer_size = LOG_BUFFER_SIZE;
 
 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
    /*
@@ -684,7 +663,7 @@ void log_error(int loglevel, const char *fmt, ...)
     * the taskbar icon animate.  (There is an option to disable
     * this but checking that is handled inside LogShowActivity()).
     */
-   if ((loglevel == LOG_LEVEL_GPC) || (loglevel == LOG_LEVEL_CRUNCH))
+   if ((loglevel == LOG_LEVEL_REQUEST) || (loglevel == LOG_LEVEL_CRUNCH))
    {
       LogShowActivity();
    }
@@ -701,6 +680,12 @@ void log_error(int loglevel, const char *fmt, ...)
 #endif
       )
    {
+#ifdef FUZZ
+      if (debug == LOG_LEVEL_STFU)
+      {
+         return;
+      }
+#endif
       if (loglevel == LOG_LEVEL_FATAL)
       {
          fatal_error("Fatal error. You're not supposed to"
@@ -712,28 +697,11 @@ void log_error(int loglevel, const char *fmt, ...)
    thread_id = get_thread_id();
    get_log_timestamp(timestamp, sizeof(timestamp));
 
-   /* protect the whole function because of the static buffer (outbuf) */
-   lock_logfile();
-
-   if (NULL == outbuf_save)
-   {
-      outbuf_save = (char*)zalloc(log_buffer_size + 1); /* +1 for paranoia */
-      if (NULL == outbuf_save)
-      {
-         snprintf(tempbuf, sizeof(tempbuf),
-            "%s %08lx Fatal error: Out of memory in log_error().",
-            timestamp, thread_id);
-         fatal_error(tempbuf); /* Exit */
-         return;
-      }
-   }
-   outbuf = outbuf_save;
-
    /*
     * Memsetting the whole buffer to zero (in theory)
     * makes things easier later on.
     */
-   memset(outbuf, 0, log_buffer_size);
+   memset(outbuf, 0, sizeof(outbuf));
 
    /* Add prefix for everything but Common Log Format messages */
    if (loglevel != LOG_LEVEL_CLF)
@@ -763,7 +731,8 @@ void log_error(int loglevel, const char *fmt, ...)
          /*
           * XXX: Only necessary on platforms where multiple threads
           * can write to the buffer at the same time because we
-          * don't support mutexes (OS/2 for example).
+          * don't support mutexes.
+          * XXX: Are there any such platforms left now that OS/2 is gone?
           */
          outbuf[length] = '\0';
          continue;
@@ -810,7 +779,7 @@ void log_error(int loglevel, const char *fmt, ...)
             break;
          case 'c':
             /*
-             * Note that char paramaters are converted to int, so we need to
+             * Note that char parameters are converted to int, so we need to
              * pass "int" to va_arg.  (See K&R, 2nd ed, section A7.3.2, page 202)
              */
             tempbuf[0] = (char) va_arg(ap, int);
@@ -861,17 +830,6 @@ void log_error(int loglevel, const char *fmt, ...)
 #ifdef _WIN32
             ival = WSAGetLastError();
             format_string = w32_socket_strerr(ival, tempbuf);
-#elif __OS2__
-            ival = sock_errno();
-            if (ival != 0)
-            {
-               format_string = os2_socket_strerr(ival, tempbuf);
-            }
-            else
-            {
-               ival = errno;
-               format_string = strerror(ival);
-            }
 #else /* ifndef _WIN32 */
             ival = errno;
 #ifdef HAVE_STRERROR
@@ -947,19 +905,21 @@ void log_error(int loglevel, const char *fmt, ...)
    assert(NULL != logfp);
 #endif
 
+   lock_logfile();
+
    if (loglevel == LOG_LEVEL_FATAL)
    {
-      fatal_error(outbuf_save);
+      fatal_error(outbuf);
       /* Never get here */
    }
    if (logfp != NULL)
    {
-      fputs(outbuf_save, logfp);
+      fputs(outbuf, logfp);
    }
 
 #if defined(_WIN32) && !defined(_WIN_CONSOLE)
    /* Write to display */
-   LogPutString(outbuf_save);
+   LogPutString(outbuf);
 #endif /* defined(_WIN32) && !defined(_WIN_CONSOLE) */
 
    unlock_logfile();
@@ -973,17 +933,13 @@ void log_error(int loglevel, const char *fmt, ...)
  *
  * Description :  Translates JB_ERR_FOO codes into strings.
  *
- *                XXX: the type of error codes is jb_err
- *                but the typedef'inition is currently not
- *                visible to all files that include errlog.h.
- *
  * Parameters  :
  *          1  :  jb_error = a valid jb_err code
  *
  * Returns     :  A string with the jb_err translation
  *
  *********************************************************************/
-const char *jb_err_to_string(int jb_error)
+const char *jb_err_to_string(jb_err jb_error)
 {
    switch (jb_error)
    {
@@ -1096,82 +1052,6 @@ static char *w32_socket_strerr(int errcode, char *tmp_buf)
 #endif /* def _WIN32 */
 
 
-#ifdef __OS2__
-/*********************************************************************
- *
- * Function    :  os2_socket_strerr
- *
- * Description :  Translate the return value from sock_errno()
- *                into a string.
- *
- * Parameters  :
- *          1  :  errcode = The return value from sock_errno().
- *          2  :  tmp_buf = A temporary buffer that might be used to
- *                          store the string.
- *
- * Returns     :  String representing the error code.  This may be
- *                a global string constant or a string stored in
- *                tmp_buf.
- *
- *********************************************************************/
-static char *os2_socket_strerr(int errcode, char *tmp_buf)
-{
-#define TEXT_FOR_ERROR(code,text) \
-   if (errcode == code)           \
-   {                              \
-      return #code " - " text;    \
-   }
-
-   TEXT_FOR_ERROR(SOCEPERM          , "Not owner.")
-   TEXT_FOR_ERROR(SOCESRCH          , "No such process.")
-   TEXT_FOR_ERROR(SOCEINTR          , "Interrupted system call.")
-   TEXT_FOR_ERROR(SOCENXIO          , "No such device or address.")
-   TEXT_FOR_ERROR(SOCEBADF          , "Bad file number.")
-   TEXT_FOR_ERROR(SOCEACCES         , "Permission denied.")
-   TEXT_FOR_ERROR(SOCEFAULT         , "Bad address.")
-   TEXT_FOR_ERROR(SOCEINVAL         , "Invalid argument.")
-   TEXT_FOR_ERROR(SOCEMFILE         , "Too many open files.")
-   TEXT_FOR_ERROR(SOCEPIPE          , "Broken pipe.")
-   TEXT_FOR_ERROR(SOCEWOULDBLOCK    , "Operation would block.")
-   TEXT_FOR_ERROR(SOCEINPROGRESS    , "Operation now in progress.")
-   TEXT_FOR_ERROR(SOCEALREADY       , "Operation already in progress.")
-   TEXT_FOR_ERROR(SOCENOTSOCK       , "Socket operation on non-socket.")
-   TEXT_FOR_ERROR(SOCEDESTADDRREQ   , "Destination address required.")
-   TEXT_FOR_ERROR(SOCEMSGSIZE       , "Message too long.")
-   TEXT_FOR_ERROR(SOCEPROTOTYPE     , "Protocol wrong type for socket.")
-   TEXT_FOR_ERROR(SOCENOPROTOOPT    , "Protocol not available.")
-   TEXT_FOR_ERROR(SOCEPROTONOSUPPORT, "Protocol not supported.")
-   TEXT_FOR_ERROR(SOCESOCKTNOSUPPORT, "Socket type not supported.")
-   TEXT_FOR_ERROR(SOCEOPNOTSUPP     , "Operation not supported.")
-   TEXT_FOR_ERROR(SOCEPFNOSUPPORT   , "Protocol family not supported.")
-   TEXT_FOR_ERROR(SOCEAFNOSUPPORT   , "Address family not supported by protocol family.")
-   TEXT_FOR_ERROR(SOCEADDRINUSE     , "Address already in use.")
-   TEXT_FOR_ERROR(SOCEADDRNOTAVAIL  , "Can't assign requested address.")
-   TEXT_FOR_ERROR(SOCENETDOWN       , "Network is down.")
-   TEXT_FOR_ERROR(SOCENETUNREACH    , "Network is unreachable.")
-   TEXT_FOR_ERROR(SOCENETRESET      , "Network dropped connection on reset.")
-   TEXT_FOR_ERROR(SOCECONNABORTED   , "Software caused connection abort.")
-   TEXT_FOR_ERROR(SOCECONNRESET     , "Connection reset by peer.")
-   TEXT_FOR_ERROR(SOCENOBUFS        , "No buffer space available.")
-   TEXT_FOR_ERROR(SOCEISCONN        , "Socket is already connected.")
-   TEXT_FOR_ERROR(SOCENOTCONN       , "Socket is not connected.")
-   TEXT_FOR_ERROR(SOCESHUTDOWN      , "Can't send after socket shutdown.")
-   TEXT_FOR_ERROR(SOCETOOMANYREFS   , "Too many references: can't splice.")
-   TEXT_FOR_ERROR(SOCETIMEDOUT      , "Operation timed out.")
-   TEXT_FOR_ERROR(SOCECONNREFUSED   , "Connection refused.")
-   TEXT_FOR_ERROR(SOCELOOP          , "Too many levels of symbolic links.")
-   TEXT_FOR_ERROR(SOCENAMETOOLONG   , "File name too long.")
-   TEXT_FOR_ERROR(SOCEHOSTDOWN      , "Host is down.")
-   TEXT_FOR_ERROR(SOCEHOSTUNREACH   , "No route to host.")
-   TEXT_FOR_ERROR(SOCENOTEMPTY      , "Directory not empty.")
-   TEXT_FOR_ERROR(SOCEOS2ERR        , "OS/2 Error.")
-
-   sprintf(tmp_buf, "(error number %d)", errcode);
-   return tmp_buf;
-}
-#endif /* def __OS2__ */
-
-
 /*
   Local Variables:
   tab-width: 3