Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/nslcd
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2013-01-18 14:03:24 +0100
committerArthur de Jong <arthur@arthurdejong.org>2013-01-18 14:03:24 +0100
commitb01cd224e2880b17e91c01c1697ee267a7d7fd2a (patch)
treea187b52c95bf4ef264ec24e88e25c30736a55020 /nslcd
parentd86497bc393606519eff0900020dec8bf3a6055f (diff)
use pthreads thread-local storage as fallback mechanism if compiler doesn't provide a keyword for TLS
git-svn-id: http://arthurdejong.org/svn/nss-pam-ldapd/nss-pam-ldapd@1922 ef36b2f9-881f-0410-afb5-c4e39611909c
Diffstat (limited to 'nslcd')
-rw-r--r--nslcd/log.c49
1 files changed, 48 insertions, 1 deletions
diff --git a/nslcd/log.c b/nslcd/log.c
index 6613a4c..1911b5c 100644
--- a/nslcd/log.c
+++ b/nslcd/log.c
@@ -30,6 +30,7 @@
#include <errno.h>
#include <string.h>
#include <time.h>
+#include <pthread.h>
#include "log.h"
@@ -43,12 +44,29 @@ static int prelogging_loglevel = LOG_INFO;
/* 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 @@ void log_startlogging(void)
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 @@ void log_clearsession(void)
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 @@ void log_newsession(void)
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_newsession(void)
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 @@ void log_setrequest(const char *format, ...)
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 @@ void log_log(int pri, const char *format, ...)
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);