]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
main: open /dev/null as stdin/out/err in daemonization
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 21 Feb 2018 11:50:14 +0000 (12:50 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Mon, 26 Feb 2018 12:42:04 +0000 (13:42 +0100)
chronyd doesn't normally write anything to stdout or stderr when running
as a daemon, but it is a good practice to replace them with descriptors
of /dev/null to prevent accidental writes to other files or sockets that
would otherwise take their place.

main.c

diff --git a/main.c b/main.c
index 8b3f34440f6aae0ce5f37242de7edfab913ba372..a2202e993ad1f559209a4211f5a3a8b5b9d8387e 100644 (file)
--- a/main.c
+++ b/main.c
@@ -292,6 +292,8 @@ write_pidfile(void)
 
 /* ================================================== */
 
+#define DEV_NULL "/dev/null"
+
 static void
 go_daemon(void)
 {
@@ -352,6 +354,13 @@ go_daemon(void)
       }
 
       LOG_SetParentFd(pipefd[1]);
+
+      /* Open /dev/null as new stdin/out/err */
+      errno = 0;
+      if (open(DEV_NULL, O_RDONLY) != STDIN_FILENO ||
+          open(DEV_NULL, O_WRONLY) != STDOUT_FILENO ||
+          open(DEV_NULL, O_RDWR) != STDERR_FILENO)
+        LOG_FATAL("Could not open %s : %s", DEV_NULL, strerror(errno));
     }
   }
 }