From afcb5c504f7302165f34931864389cbc115b7b16 Mon Sep 17 00:00:00 2001 From: Miloslav Trmač Date: Tue, 24 Jul 2012 12:29:04 +0200 Subject: Make "facility" set by ul_openlog() per-process MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ... to match openlog(3). Signed-off-by: Miloslav Trmač --- lib/Makefile.am | 1 + lib/umberlog.c | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/Makefile.am b/lib/Makefile.am index b6b60b3..8c1bf3c 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -7,6 +7,7 @@ libumberlog_la_LDFLAGS = -Wl,--version-script,${srcdir}/libumberlog.ld \ -version-info ${LUL_CURRENT}:${LUL_REVISION}:${LUL_AGE} libumberlog_la_SOURCES = umberlog.c umberlog.h buffer.c buffer.h +libumberlog_la_LIBADD = -lpthread libumberlog_includedir = $(includedir) libumberlog_include_HEADERS = umberlog.h diff --git a/lib/umberlog.c b/lib/umberlog.c index a04b628..58f6bb0 100644 --- a/lib/umberlog.c +++ b/lib/umberlog.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -62,13 +63,20 @@ static __thread struct { int flags; - int facility; pid_t pid; uid_t uid; gid_t gid; const char *ident; char hostname[_POSIX_HOST_NAME_MAX + 1]; } ul_thread_data; +static struct +{ + /* The lock is used only to serialize writes; we assume that reads are safe + even when racing with writes, note that POSIX does not guarantee this (but + the BSD syslog does the same thing). */ + pthread_mutex_t lock; + int facility; +} ul_process_data = { PTHREAD_MUTEX_INITIALIZER, 0 }; static __thread ul_buffer_t ul_buffer; static __thread int ul_recurse; @@ -93,7 +101,9 @@ ul_openlog (const char *ident, int option, int facility) { old_openlog (ident, option, facility); ul_thread_data.flags = option; - ul_thread_data.facility = facility; + pthread_mutex_lock (&ul_process_data.lock); + ul_process_data.facility = facility; + pthread_mutex_unlock (&ul_process_data.lock); ul_thread_data.pid = getpid (); ul_thread_data.gid = getgid (); ul_thread_data.uid = getuid (); @@ -107,6 +117,9 @@ ul_closelog (void) { old_closelog (); memset (&ul_thread_data, 0, sizeof (ul_thread_data)); + pthread_mutex_lock (&ul_process_data.lock); + ul_process_data.facility = 0; + pthread_mutex_unlock (&ul_process_data.lock); } /** HELPERS **/ @@ -117,7 +130,7 @@ _find_facility (int prio) int fac = prio & LOG_FACMASK; if (fac == 0) - fac = ul_thread_data.facility; + fac = ul_process_data.facility; while (facilitynames[i].c_name != NULL && facilitynames[i].c_val != fac) -- cgit