From c0736303e0bfbf71437c73524c1e7b35c839541d Mon Sep 17 00:00:00 2001 From: leonid-ed Date: Wed, 25 May 2016 18:01:48 +0300 Subject: [PATCH] [helpers] Added an ability to redirect all output to syslog in case of daemon mode; --- examples/helpers.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/examples/helpers.c b/examples/helpers.c index aea3f97..5314aab 100644 --- a/examples/helpers.c +++ b/examples/helpers.c @@ -21,9 +21,9 @@ * */ +#define _GNU_SOURCE #include #include -#define __USE_GNU #include #include #include @@ -38,6 +38,7 @@ #include #include #include +#include int openlistener_socket(int port) { @@ -117,6 +118,27 @@ int normalize_path(char *path) return 0; } +/* syslog stuff */ + +static size_t writer(void *cookie, char const *data, size_t len) +{ + (void)cookie; + syslog(LOG_DEBUG, "%.*s", (int)len, data); + return len; +} +static int noop(void) { return 0; } + +static cookie_io_functions_t log_fns = { + (void*) noop, (void*) writer, (void*) noop, (void*) noop +}; + +void tosyslog(FILE **pfp) +{ + setvbuf(*pfp = fopencookie(NULL, "w", log_fns), NULL, _IOLBF, 0); +} + +/* daemonization stuff */ + #define DM_MAX_CLOSE 8192 int daemonize(void) @@ -166,5 +188,9 @@ int daemonize(void) return EBADFD; } + /* redirect output to syslog as well */ + tosyslog(&stdout); + tosyslog(&stderr); + return 0; }