From c568a2c036ee555faa7b24cdfe07d65af0a15361 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 16 Apr 2012 00:36:37 +0200 Subject: Free up the JSON buffer on exit This adds a destructor that simply frees up the JSON buffer, so that to not upset valgrind and similar tools too much. Signed-off-by: Gergely Nagy --- lib/umberlog.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'lib/umberlog.c') diff --git a/lib/umberlog.c b/lib/umberlog.c index fe0a6b7..84bd64b 100644 --- a/lib/umberlog.c +++ b/lib/umberlog.c @@ -51,6 +51,7 @@ static void (*old_openlog) (); static int (*old_setlogmask) (); static void ul_init (void) __attribute__((constructor)); +static void ul_finish (void) __attribute__((destructor)); static __thread struct { @@ -77,6 +78,12 @@ ul_init (void) old_setlogmask = dlsym (RTLD_NEXT, "setlogmask"); } +static void +ul_finish (void) +{ + free (ul_buffer.msg); +} + void ul_openlog (const char *ident, int option, int facility) { -- cgit From 32887a13759b27f5b29f585d2b278c225be1fa0d Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Fri, 20 Apr 2012 12:37:35 +0200 Subject: Properly reset the JSON buffer in all cases. Previously the JSON buffer was only reset when ul_vformat() was called, but not everything goes through that, so some messages ended up being garbled. The proper thing to do is to reset in _ul_vformat(), as everything goes through that, and that's the right place to do this anyway. Reported-by: Peter Czanik Signed-off-by: Gergely Nagy --- lib/umberlog.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/umberlog.c') diff --git a/lib/umberlog.c b/lib/umberlog.c index 84bd64b..d07a24a 100644 --- a/lib/umberlog.c +++ b/lib/umberlog.c @@ -348,6 +348,8 @@ _ul_vformat (ul_buffer_t *buffer, int format_version, if (!value) return NULL; + ul_buffer_reset (buffer); + buffer = ul_buffer_append (buffer, "msg", value); if (buffer == NULL) { @@ -402,8 +404,6 @@ ul_vformat (int priority, const char *msg_format, va_list ap) const char *msg; ul_buffer_t *buffer = &ul_buffer; - ul_buffer_reset (buffer); - msg = _ul_vformat_str (buffer, 1, priority, msg_format, ap); if (!msg) { -- cgit From 5509ee80130cb781d0c88d411021a98b5fab4556 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Fri, 20 Apr 2012 13:14:29 +0200 Subject: Resolve facility & priority properly The priority passed to the syslog() call can contain a facility embedded, so we need to extract it to find both the facility and the priority. This also means that the facility set at openlog()-time can be overridden later, so _find_facility() has to take the priority value into account. This patch makes libumberlog do just the above: extract both priority and facility from the priority passed to syslog(), and if facility is 0, use the default set at openlog()-time, if any. Added a test case to test this expected behaviour. Reported-by: Peter Czanik Signed-off-by: Gergely Nagy --- lib/umberlog.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'lib/umberlog.c') diff --git a/lib/umberlog.c b/lib/umberlog.c index d07a24a..ad25709 100644 --- a/lib/umberlog.c +++ b/lib/umberlog.c @@ -101,15 +101,19 @@ ul_openlog (const char *ident, int option, int facility) /** HELPERS **/ static inline const char * -_find_facility (void) +_find_facility (int prio) { int i = 0; + int fac = prio & LOG_FACMASK; + + if (fac == 0) + fac = ul_sys_settings.facility; while (facilitynames[i].c_name != NULL && - facilitynames[i].c_val != ul_sys_settings.facility) + facilitynames[i].c_val != fac) i++; - if (facilitynames[i].c_val == ul_sys_settings.facility) + if (facilitynames[i].c_val == fac) return facilitynames[i].c_name; return ""; } @@ -118,12 +122,13 @@ static inline const char * _find_prio (int prio) { int i = 0; + int pri = LOG_PRI (prio); while (prioritynames[i].c_name != NULL && - prioritynames[i].c_val != prio) + prioritynames[i].c_val != pri) i++; - if (prioritynames[i].c_val == prio) + if (prioritynames[i].c_val == pri) return prioritynames[i].c_name; return ""; } @@ -316,7 +321,7 @@ _ul_discover (ul_buffer_t *buffer, int priority) buffer = _ul_json_append (buffer, "pid", "%d", _find_pid (), - "facility", "%s", _find_facility (), + "facility", "%s", _find_facility (priority), "priority", "%s", _find_prio (priority), "program", "%s", ul_sys_settings.ident, "uid", "%d", _get_uid (), -- cgit From 8cfef10830350c565e8ee917f83b9e97016a0a62 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sat, 28 Apr 2012 18:18:32 +0200 Subject: Add a closelog() wrapper to clear the environment Our openlog() wrapper fills in a couple of variables, and those were kept around even after a closelog(), and thus, affected ul_format() calls even after a closelog(). This in turn, made one of the test cases fail, as that was relying on the default behaviour, which was modified due to an openlog() in an earlier test case. We now wrap closelog() aswell, and NULL out our settings to get a clean state, and add a test case to verify this behaviour aswell. Signed-off-by: Gergely Nagy --- lib/umberlog.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'lib/umberlog.c') diff --git a/lib/umberlog.c b/lib/umberlog.c index ad25709..e4eb5d3 100644 --- a/lib/umberlog.c +++ b/lib/umberlog.c @@ -48,6 +48,7 @@ static void (*old_syslog) (); static void (*old_vsyslog) (); static void (*old_openlog) (); +static void (*old_closelog) (); static int (*old_setlogmask) (); static void ul_init (void) __attribute__((constructor)); @@ -75,6 +76,7 @@ ul_init (void) old_syslog = dlsym (RTLD_NEXT, "syslog"); old_vsyslog = dlsym (RTLD_NEXT, "vsyslog"); old_openlog = dlsym (RTLD_NEXT, "openlog"); + old_closelog = dlsym (RTLD_NEXT, "closelog"); old_setlogmask = dlsym (RTLD_NEXT, "setlogmask"); } @@ -99,6 +101,13 @@ ul_openlog (const char *ident, int option, int facility) gethostname (ul_sys_settings.hostname, _POSIX_HOST_NAME_MAX); } +void +ul_closelog (void) +{ + old_closelog (); + memset (&ul_sys_settings, 0, sizeof (ul_sys_settings)); +} + /** HELPERS **/ static inline const char * _find_facility (int prio) @@ -512,6 +521,9 @@ __vsyslog_chk (int __pri, int __flag, __const char *__fmt, va_list ap) void openlog (const char *ident, int option, int facility) __attribute__((alias ("ul_openlog"))); +void closelog (void) + __attribute__((alias ("ul_closelog"))); + #undef syslog void syslog (int priority, const char *msg_format, ...) __attribute__((alias ("ul_legacy_syslog"))); -- cgit