diff --git a/examples/helpers.c b/examples/helpers.c index 26fbce7..a8c06ca 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; } \ No newline at end of file