]> git.tuebingen.mpg.de Git - dss.git/blobdiff - daemon.c
Fix --config-file for relative paths.
[dss.git] / daemon.c
index 86e89066aedf048757cc2f4e55674e909a0168a0..ad73061c14ec5916284f2ab67438306d6484827d 100644 (file)
--- a/daemon.c
+++ b/daemon.c
@@ -1,8 +1,4 @@
-/*
- * Copyright (C) 1997-2010 Andre Noll <maan@tuebingen.mpg.de>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* SPDX-License-Identifier: GPL-2.0 */
 
 /** \file daemon.c Some helpers for programs that detach from the console. */
 
 
 /** \file daemon.c Some helpers for programs that detach from the console. */
 
  *
  * \sa fork(2), setsid(2), dup(2).
  */
  *
  * \sa fork(2), setsid(2), dup(2).
  */
-void daemon_init(void)
+int daemon_init(void)
 {
        pid_t pid;
 {
        pid_t pid;
-       int null;
+       int null, fd[2];
 
        DSS_INFO_LOG(("daemonizing\n"));
 
        DSS_INFO_LOG(("daemonizing\n"));
+       if (pipe(fd) < 0)
+               goto err;
        pid = fork();
        if (pid < 0)
                goto err;
        pid = fork();
        if (pid < 0)
                goto err;
-       if (pid)
-               exit(EXIT_SUCCESS); /* parent exits */
+       if (pid) {
+               /*
+                * The parent process exits once it has received one byte from
+                * the reading end of the pipe. If the child exits before it
+                * was able to complete its setup (acquire the lock on the
+                * semaphore), the read() below will return zero. In this case
+                * we let the parent die unsuccessfully.
+                */
+               char c;
+               int ret;
+               close(fd[1]);
+               ret = read(fd[0], &c, 1);
+               if (ret <= 0) {
+                       DSS_EMERG_LOG(("child terminated unexpectedly\n"));
+                       exit(EXIT_FAILURE);
+               }
+               exit(EXIT_SUCCESS);
+       }
+       close(fd[0]);
        /* become session leader */
        if (setsid() < 0)
                goto err;
        /* become session leader */
        if (setsid() < 0)
                goto err;
-       umask(0);
        null = open("/dev/null", O_RDWR);
        if (null < 0)
                goto err;
        null = open("/dev/null", O_RDWR);
        if (null < 0)
                goto err;
@@ -56,7 +70,7 @@ void daemon_init(void)
        if (dup2(null, STDERR_FILENO) < 0)
                goto err;
        close(null);
        if (dup2(null, STDERR_FILENO) < 0)
                goto err;
        close(null);
-       return;
+       return fd[1];
 err:
        DSS_EMERG_LOG(("fatal: %s\n", strerror(errno)));
        exit(EXIT_FAILURE);
 err:
        DSS_EMERG_LOG(("fatal: %s\n", strerror(errno)));
        exit(EXIT_FAILURE);