Skip to content

Instantly share code, notes, and snippets.

@cornet
Forked from scintill/README.md
Created September 17, 2019 23:06
Show Gist options
  • Save cornet/e7a331468e59cd91967674747716f7df to your computer and use it in GitHub Desktop.
Save cornet/e7a331468e59cd91967674747716f7df to your computer and use it in GitHub Desktop.
redirect syslog to stderr

LD_PRELOAD this as a library to output a binary's output from syslog() on stderr as well as any syslog daemon that's listening (useful for Docker, if you want all log output to be collected by Docker's standard output handling.) Beware though, if the program changes its stderr file descriptor (closes it, points it at /dev/null, pipes it out to a socket), this can have surprising results. For example, when I tried this with cyrus-imapd, I was getting syslog output sent to remote clients because stderr and stdout were remapped that way (not maintaing the original fd that Docker set up.)

Example Docker implementation

COPY syslog2stderr.c /build
RUN gcc -fPIC -shared /build/syslog2stderr.c -o /usr/local/lib/syslog2stderr.so
RUN echo /usr/local/lib/syslog2stderr.so >> /etc/ld.so.preload
#define _GNU_SOURCE
#include <syslog.h>
#include <dlfcn.h>
void openlog(const char *ident, int option, int facility) {
void (*original_openlog)(const char *, int, int) = dlsym(RTLD_NEXT, "openlog");
original_openlog(ident, option | LOG_PERROR, facility);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment