raforg / daemon

turns other processes into daemons
http://libslack.org/daemon/
GNU General Public License v2.0
13 stars 1 forks source link

syslog ident is always "daemon", even when using the "-n" option #5

Open e477 opened 6 months ago

e477 commented 6 months ago

I'm using daemon with the -o option to redirect all output to syslog. Despite also using the -n option to explicitly set a name all syslog entries have the ident daemon. This makes the syslog impractical when using more than one daemon.

Example:

daemon -n example -o user.info /usr/bin/ls /

libslack/msg.c sets the syslog ident correctly when initializing the msg object and calling openlog. However, the result of openlog does not survive the subsequent forks. Logging still works because syslog will call openlog implicitly when required, but then the ident is undefined and at least glibc will fall back to using the executable's name which is daemon of course.

As a simple workaround I've commented out the calls to prog_err_syslog and prog_dbg_syslog (line 5158f) which delays the call to openlog till after the fork when the regular output starts. The syslog ident is correct then.

A better way would be to re-initialize syslog output after a fork, but I've not yet found out how to do this safely and thus don't have a patch ready.

raforg commented 5 months ago

Hi! Thanks for spotting that and reporting it. Here's a little patch that fixes it. add-name-to-client-syslog.patch

Technically, I also need to change libslack/msg.c to move openlog() and closelog() to immediately around each call to syslog() (just on the off chance that different syslog message destinations have different identifiers), but that doesn't affect this problem.

Please let me know if this works for you too. I can't put it in as a commit yet because I'm in the middle of a bunch of commits that aren't public yet. This will go in after they're finished (along with the change to libslack/msg.c).