[PATCH]: thread-independent LogTag, LogFacility, and LogStat in syslog()

Kentaro Makita k-makita@soft.fujitsu.com
Wed Dec 5 06:27:00 GMT 2007


Hi,

I found following:

   syslog() uses global variable LogTag, LogFacility, and LogStat
   between threads. It may cause interraction between threads
   such as overriding global variables.

I think it is not a bug, but it is better to use different LogTag,
LogFacility, and LogStat values, so I'll send a patch.
With this patch, it is able to have diffrent LogTag, LogFacility,
  and LogStat between threads.

I also posted this solutions to bugzilla as following:
http://sources.redhat.com/bugzilla/show_bug.cgi?id=5190


------------------------------------------------------------------------------
--- glibc-2.6.1/misc/syslog.c.orig      2006-01-14 21:09:36.000000000 +0900
+++ glibc-2.6.1/misc/syslog.c   2007-12-05 15:01:41.000000000 +0900
@@ -62,6 +62,21 @@

  #define ftell(s) INTUSE(_IO_ftell) (s)

+#ifdef USE___THREAD
+static int     LogType = SOCK_DGRAM;   /* type of socket connection */
+static int     LogFile = -1;           /* fd for log */
+static __thread int    connected;      /* have done connect */
+static int     GLogStat;               /* status bits, set by openlog() */
+static __thread int    LogStat;        /* thread-local status bits, set by openlog() */
+static const char *GLogTag;            /* string to tag the entry with */
+static __thread const char *LogTag;    /* thread-local string to tag the entry with */
+static int     GLogFacility = LOG_USER;/* default facility code */
+static __thread int    LogFacility;    /* thread-local default facility code */
+static int     LogMask = 0xff;         /* mask of priorities to be logged */
+static int     connected_count = 0;    /* open count in process */
+extern char    *__progname;            /* Program name, from crt0. */
+
+#else
  static int     LogType = SOCK_DGRAM;   /* type of socket connection */
  static int     LogFile = -1;           /* fd for log */
  static int     connected;              /* have done connect */
@@ -70,6 +85,7 @@
  static int     LogFacility = LOG_USER; /* default facility code */
  static int     LogMask = 0xff;         /* mask of priorities to be logged */
  extern char    *__progname;            /* Program name, from crt0. */
+#endif

  /* Define the lock.  */
  __libc_lock_define_initialized (static, syslog_lock)
@@ -163,6 +179,10 @@

         /* Set default facility if none specified. */
         if ((pri & LOG_FACMASK) == 0)
+#ifdef USE___THREAD
+               if (!connected)
+                 LogFacility = GLogFacility;
+#endif
                 pri |= LogFacility;

         /* Build the message in a memory-buffer stream.  */
@@ -201,6 +221,14 @@
                                               __localtime_r (&now, &now_tm),
                                               _nl_C_locobj_ptr);
             msgoff = ftell (f);
+#ifdef USE___THREAD
+           if ( !connected )
+             {
+               LogTag      = GLogTag;
+               LogStat     = GLogStat;
+               LogFacility = GLogFacility;
+             }
+#endif
             if (LogTag == NULL)
               LogTag = __progname;
             if (LogTag != NULL)
@@ -335,11 +363,19 @@
  internal_function
  openlog_internal(const char *ident, int logstat, int logfac)
  {
+#ifdef USE___THREAD
+       if (ident != NULL)
+               GLogTag = LogTag = ident;
+       GLogStat = LogStat = logstat;
+       if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
+               GLogFacility = LogFacility = logfac;
+#else
         if (ident != NULL)
                 LogTag = ident;
         LogStat = logstat;
         if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
                 LogFacility = logfac;
+#endif

         int retry = 0;
         while (retry < 2) {
@@ -373,8 +409,14 @@
                                         ++retry;
                                         continue;
                                 }
-                       } else
+                       }
+                       else
+                       {
                                 connected = 1;
+#ifdef USE___THREAD
+                               connected_count++;
+#endif
+                       }
                 }
                 break;
         }
@@ -406,9 +448,19 @@
    if (!connected)
      return;

+#ifndef USE___THREAD
    __close (LogFile);
    LogFile = -1;
+#endif
    connected = 0;
+#ifdef USE___THREAD
+  connected_count--;
+  if ( connected_count )
+  {
+       __close (LogFile);
+       LogFile = -1;
+  }
+#endif
  }

  void
------------------------------------------------------------------------------

Best regards,

Kentaro MAKITA





More information about the Libc-alpha mailing list