lists.arthurdejong.org
RSS feed

nss-pam-ldapd commit: r1922 - nss-pam-ldapd/nslcd

[Date Prev][Date Next] [Thread Prev][Thread Next]

nss-pam-ldapd commit: r1922 - nss-pam-ldapd/nslcd



Author: arthur
Date: Fri Jan 18 14:03:24 2013
New Revision: 1922
URL: http://arthurdejong.org/viewvc/nss-pam-ldapd?revision=1922&view=revision

Log:
use pthreads thread-local storage as fallback mechanism if compiler doesn't 
provide a keyword for TLS

Modified:
   nss-pam-ldapd/nslcd/log.c

Modified: nss-pam-ldapd/nslcd/log.c
==============================================================================
--- nss-pam-ldapd/nslcd/log.c   Fri Jan 18 14:02:37 2013        (r1921)
+++ nss-pam-ldapd/nslcd/log.c   Fri Jan 18 14:03:24 2013        (r1922)
@@ -30,6 +30,7 @@
 #include <errno.h>
 #include <string.h>
 #include <time.h>
+#include <pthread.h>
 
 #include "log.h"
 
@@ -43,12 +44,29 @@
 /* loglevel to use before logging to syslog */
 static int loglevel = LOG_INFO;
 
+#define MAX_REQUESTID_LENGTH 40
+
+#ifdef TLS
+
 /* the session id that is set for this thread */
 static TLS char *sessionid = NULL;
 
 /* the request identifier that is set for this thread */
 static TLS char *requestid = NULL;
-#define MAX_REQUESTID_LENGTH 40
+
+#else /* no TLS, use pthreads */
+
+static pthread_once_t tls_init_once = PTHREAD_ONCE_INIT;
+static pthread_key_t sessionid_key;
+static pthread_key_t requestid_key;
+
+static void tls_init_keys(void)
+{
+  pthread_key_create(&sessionid_key, NULL);
+  pthread_key_create(&requestid_key, NULL);
+}
+
+#endif /* no TLS, use pthreads */
 
 /* set loglevel when no logging is configured */
 void log_setdefaultloglevel(int pri)
@@ -68,6 +86,12 @@
    log_newsession */
 void log_clearsession(void)
 {
+#ifndef TLS
+  char *sessionid, *requestid;
+  pthread_once(&tls_init_once, tls_init_keys);
+  sessionid = pthread_getspecific(sessionid_key);
+  requestid = pthread_getspecific(requestid_key);
+#endif /* no TLS */
   /* set the session id to empty */
   if (sessionid != NULL)
     sessionid[0] = '\0';
@@ -80,6 +104,12 @@
    and set it to a new value */
 void log_newsession(void)
 {
+#ifndef TLS
+  char *sessionid, *requestid;
+  pthread_once(&tls_init_once, tls_init_keys);
+  sessionid = pthread_getspecific(sessionid_key);
+  requestid = pthread_getspecific(requestid_key);
+#endif /* no TLS */
   /* ensure that sessionid can hold a string */
   if (sessionid == NULL)
   {
@@ -89,6 +119,9 @@
       fprintf(stderr, "malloc() failed: %s", strerror(errno));
       return; /* silently fail */
     }
+#ifndef TLS
+    pthread_setspecific(sessionid_key, sessionid);
+#endif /* no TLS */
   }
   sprintf(sessionid, "%06x", (int)(rand() & 0xffffff));
   /* set the request id to empty */
@@ -101,6 +134,11 @@
 void log_setrequest(const char *format, ...)
 {
   va_list ap;
+#ifndef TLS
+  char *requestid;
+  pthread_once(&tls_init_once, tls_init_keys);
+  requestid = pthread_getspecific(requestid_key);
+#endif /* no TLS */
   /* ensure that requestid can hold a string */
   if (requestid == NULL)
   {
@@ -110,6 +148,9 @@
       fprintf(stderr, "malloc() failed: %s", strerror(errno));
       return; /* silently fail */
     }
+#ifndef TLS
+    pthread_setspecific(requestid_key, requestid);
+#endif /* no TLS */
   }
   /* make the message */
   va_start(ap, format);
@@ -124,6 +165,12 @@
   int res;
   char buffer[200];
   va_list ap;
+#ifndef TLS
+  char *sessionid, *requestid;
+  pthread_once(&tls_init_once, tls_init_keys);
+  sessionid = pthread_getspecific(sessionid_key);
+  requestid = pthread_getspecific(requestid_key);
+#endif /* no TLS */
   /* make the message */
   va_start(ap, format);
   res = vsnprintf(buffer, sizeof(buffer), format, ap);
-- 
To unsubscribe send an email to
nss-pam-ldapd-commits-unsubscribe@lists.arthurdejong.org or see
http://lists.arthurdejong.org/nss-pam-ldapd-commits/