Skip to contentSkip to conversation

close the syslog fd in daemon_init()

PATCH In review Scott Mayhew 2 participants 2 messages Sep 29, 2015 – Nov 4, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 close the syslog fd in daemon_init()

Commit 7addf9d (cleanup daemonization code) added the following line to mydaemon_init():

        dup2(pipefds[1], 3);

If we've already called vsyslog() before the fork(), then chances are fd 3 was being used for the syslog socket. In that case the next vsyslog() call will cause the data to appear on the read end of the pipe, causing the parent to exit with a nonzero status. If systemd is running, it will see the parent's nonzero exit status and will terminate the child as well.

So just call closelog() to close the fd. The next call to vsyslog() will open a new one if need be.

---
 support/nfs/mydaemon.c | 1 +
 1 file changed, 1 insertion(+)
support/nfs/mydaemon.c +1 -0
index 3391eff..701cfd9 100644
@@ -122,6 +122,7 @@ daemon_init(bool fg)
 	dup2(tempfd, 0);
 	dup2(tempfd, 1);
 	dup2(tempfd, 2);
+	closelog();
 	dup2(pipefds[1], 3);
 	pipefds[1] = 3;
 	closeall(4);
Signature · 1 line
1.9.3
#1 lore raw
Quoted 12 lines · Scott Mayhew

Commit 7addf9d (cleanup daemonization code) added the following line to mydaemon_init():

        dup2(pipefds[1], 3);

If we've already called vsyslog() before the fork(), then chances are fd 3 was being used for the syslog socket. In that case the next vsyslog() call will cause the data to appear on the read end of the pipe, causing the parent to exit with a nonzero status. If systemd is running, it will see the parent's nonzero exit status and will terminate the child as well.

Quoted 4 lines

So just call closelog() to close the fd. The next call to vsyslog() will open a new one if need be.

Committed... Actual this is the one I fixed the warning on...

steved.

Quoted 17 lines
---
 support/nfs/mydaemon.c | 1 +
 1 file changed, 1 insertion(+)
support/nfs/mydaemon.c +1 -0
index 3391eff..701cfd9 100644
@@ -122,6 +122,7 @@ daemon_init(bool fg)
 	dup2(tempfd, 0);
 	dup2(tempfd, 1);
 	dup2(tempfd, 2);
+	closelog();
 	dup2(pipefds[1], 3);
 	pipefds[1] = 3;
 	closeall(4);