Skip to content

Instantly share code, notes, and snippets.

@scintill
Created June 5, 2017 00:27
Show Gist options
  • Save scintill/f332033cf0bd27a0fe693ccd162b61a2 to your computer and use it in GitHub Desktop.
Save scintill/f332033cf0bd27a0fe693ccd162b61a2 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);
}
@helmut72
Copy link

RUN gcc -fPIC -shared /build/syslog2stderr.c -o /usr/local/lib/syslog2stderr.so -ldl

... was needed that it worked for me. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment