summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorGergely Nagy <algernon@balabit.hu>2012-05-02 16:25:04 +0200
committerGergely Nagy <algernon@balabit.hu>2012-05-02 16:25:04 +0200
commitac2a1fdb38057e032b22c4e2ea43d5ab741222b8 (patch)
tree52f91b7c65314760932543b8825303e1122954fe /lib
parentb9751e1ca5f96ffdf49e38646709a3de9e25a208 (diff)
parente5faea1d639252017cc8321e9c2cb5d17b061e97 (diff)
Merge tag 'libumberlog-0.2.1' into debian
libumberlog 0.2.1 release
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile.am13
-rw-r--r--lib/buffer.c51
-rw-r--r--lib/libumberlog.ld5
-rw-r--r--lib/umberlog.c40
-rw-r--r--lib/umberlog.h1
-rw-r--r--lib/umberlog.rst8
6 files changed, 90 insertions, 28 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 169b0b2..b6b60b3 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -1,6 +1,6 @@
-LUL_CURRENT = 1
+LUL_CURRENT = 2
LUL_REVISION = 0
-LUL_AGE = 0
+LUL_AGE = 1
lib_LTLIBRARIES = libumberlog.la
libumberlog_la_LDFLAGS = -Wl,--version-script,${srcdir}/libumberlog.ld \
@@ -15,3 +15,12 @@ pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libumberlog.pc
EXTRA_DIST = umberlog.rst libumberlog.ld
+
+if ENABLE_MANS
+man3_MANS = umberlog.3
+CLEANFILES = umberlog.3
+EXTRA_DIST += umberlog.3
+
+umberlog.3: umberlog.rst
+ $(AM_V_GEN) $(RST2MAN) $< $@
+endif
diff --git a/lib/buffer.c b/lib/buffer.c
index 8bdb3ef..ef448bf 100644
--- a/lib/buffer.c
+++ b/lib/buffer.c
@@ -54,11 +54,20 @@ static const unsigned char json_exceptions[] =
0xff, '\0'
};
+static __thread ul_buffer_t escape_buffer;
+
+static void ul_buffer_finish (void) __attribute__((destructor));
+
+static void
+ul_buffer_finish (void)
+{
+ free (escape_buffer.msg);
+}
+
static inline char *
-_ul_str_escape (const char *str)
+_ul_str_escape (const char *str, char *dest, size_t *length)
{
const unsigned char *p;
- char *dest;
char *q;
static unsigned char exmap[256];
static int exmap_inited;
@@ -67,7 +76,7 @@ _ul_str_escape (const char *str)
return NULL;
p = (unsigned char *)str;
- q = dest = malloc (strlen (str) * 6 + 1);
+ q = dest;
if (!exmap_inited)
{
@@ -139,6 +148,8 @@ _ul_str_escape (const char *str)
}
*q = 0;
+ if (length)
+ *length = q - dest;
return dest;
}
@@ -169,38 +180,44 @@ ul_buffer_append (ul_buffer_t *buffer, const char *key, const char *value)
{
char *k, *v;
size_t lk, lv;
+ size_t orig_len = buffer->len;
- k = _ul_str_escape (key);
+ /* Append the key to the buffer */
+ escape_buffer.len = 0;
+ _ul_buffer_ensure_size (&escape_buffer, strlen (key) * 6 + 1);
+ k = _ul_str_escape (key, escape_buffer.msg, &lk);
if (!k)
return NULL;
- v = _ul_str_escape (value);
+
+ buffer = _ul_buffer_ensure_size (buffer, buffer->len + lk + 4);
+ if (!buffer)
+ return NULL;
+
+ memcpy (buffer->msg + buffer->len, "\"", 1);
+ memcpy (buffer->msg + buffer->len + 1, k, lk);
+ memcpy (buffer->msg + buffer->len + 1 + lk, "\":\"", 3);
+
+ /* Append the value to the buffer */
+ escape_buffer.len = 0;
+ _ul_buffer_ensure_size (&escape_buffer, strlen (value) * 6 + 1);
+ v = _ul_str_escape (value, escape_buffer.msg, &lv);
if (!v)
{
- free (k);
+ buffer->len = orig_len;
return NULL;
}
- lk = strlen (k);
- lv = strlen (v);
-
buffer = _ul_buffer_ensure_size (buffer, buffer->len + lk + lv + 6);
if (!buffer)
{
- free (k);
- free (v);
+ buffer->len = orig_len;
return NULL;
}
- memcpy (buffer->msg + buffer->len, "\"", 1);
- memcpy (buffer->msg + buffer->len + 1, k, lk);
- memcpy (buffer->msg + buffer->len + 1 + lk, "\":\"", 3);
memcpy (buffer->msg + buffer->len + 1 + lk + 3, v, lv);
memcpy (buffer->msg + buffer->len + 1 + lk + 3 + lv, "\",", 2);
buffer->len += lk + lv + 6;
- free (k);
- free (v);
-
return buffer;
}
diff --git a/lib/libumberlog.ld b/lib/libumberlog.ld
index 8cd48e7..225d51c 100644
--- a/lib/libumberlog.ld
+++ b/lib/libumberlog.ld
@@ -15,3 +15,8 @@ LIBUMBERLOG_0.1.0 {
facilitynames;
prioritynames;
};
+
+LIBUMBERLOG_0.2.1 {
+ global:
+ ul_closelog;
+} LIBUMBERLOG_0.1.0;
diff --git a/lib/umberlog.c b/lib/umberlog.c
index fe0a6b7..e4eb5d3 100644
--- a/lib/umberlog.c
+++ b/lib/umberlog.c
@@ -48,9 +48,11 @@
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));
+static void ul_finish (void) __attribute__((destructor));
static __thread struct
{
@@ -74,9 +76,16 @@ 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");
}
+static void
+ul_finish (void)
+{
+ free (ul_buffer.msg);
+}
+
void
ul_openlog (const char *ident, int option, int facility)
{
@@ -92,17 +101,28 @@ 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 (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 "<unknown>";
}
@@ -111,12 +131,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 "<unknown>";
}
@@ -309,7 +330,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 (),
@@ -341,6 +362,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)
{
@@ -395,8 +418,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)
{
@@ -500,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")));
diff --git a/lib/umberlog.h b/lib/umberlog.h
index 4245dae..51ed7be 100644
--- a/lib/umberlog.h
+++ b/lib/umberlog.h
@@ -41,6 +41,7 @@ char *ul_format (int priority, const char *msg_format, ...)
char *ul_vformat (int priority, const char *msg_format, va_list ap);
void ul_openlog (const char *ident, int option, int facility);
+void ul_closelog (void);
int ul_setlogmask (int mask);
int ul_syslog (int priority, const char *msg_format, ...)
diff --git a/lib/umberlog.rst b/lib/umberlog.rst
index c655263..d2cd354 100644
--- a/lib/umberlog.rst
+++ b/lib/umberlog.rst
@@ -7,7 +7,7 @@ CEE-enhanced syslog message generation
--------------------------------------
:Author: Gergely Nagy <algernon@balabit.hu>
-:Date: 2012-03-23
+:Date: 2012-04-28
:Manual section: 3
:Manual group: CEE-enhanced syslog Manual
@@ -19,6 +19,7 @@ SYNOPSIS
#include <umberlog.h>
void ul_openlog (const char *ident, int option, int facility);
+ void ul_closelog (void);
int ul_syslog (int priority, const char *format, ....);
int ul_vsyslog (int priority, const char *format, va_list ap);
@@ -37,6 +38,11 @@ the original **openlog()** function, which opens a connection to the
system logger for a program. The updated version adds support for a
number of new option flags, described below.
+**ul_closelog()** (also aliased to **closelog()**) is similar to
+**ul_openlog()** in that it is a wrapper around the original
+**closelog()**. It clears any settings set so far, to get back to a
+clean state.
+
**ul_legacy_syslog()** and **ul_legacy_vsyslog()** are both thin
layers over the original **syslog()** and **vsyslog()** functions, and
the library overrides the original functions with this two. The only