For systems that cannot support fork() (like no-mmu Linux), use daemon() if
it is available for the daemonizing code.

Signed-off-by: Mike Frysinger <vap...@gentoo.org>
---
 configure.ac |    2 +-
 src/daemon.c |   24 ++++++++++++++++++------
 2 files changed, 19 insertions(+), 7 deletions(-)

diff --git a/configure.ac b/configure.ac
index e828f42..600b51d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -100,7 +100,7 @@ dnl
 dnl libc features
 dnl
 
-AC_CHECK_FUNCS(syslog)
+AC_CHECK_FUNCS(daemon fork syslog)
 if test $ac_cv_func_syslog = no; then
        # syslog is not in the default libraries.  See if it's in some other.
        for lib in bsd socket inet; do
diff --git a/src/daemon.c b/src/daemon.c
index 5ae79e0..192fa8d 100644
--- a/src/daemon.c
+++ b/src/daemon.c
@@ -17,6 +17,7 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#include "config.h"
 #include "daemon.h"
 
 #include <glib.h>
@@ -128,21 +129,28 @@ daemonize_set_user(void)
 static void
 daemonize_detach(void)
 {
-       pid_t pid;
-
        /* flush all file handles before duplicating the buffers */
 
        fflush(NULL);
 
+#ifdef HAVE_DAEMON
+
+       if (daemon(0, 1))
+               g_error("daemon() failed: %s", g_strerror(errno));
+
+#elif defined(HAVE_FORK)
+
        /* detach from parent process */
 
-       pid = fork();
-       if (pid < 0)
+       switch (fork()) {
+       case -1:
                g_error("fork() failed: %s", g_strerror(errno));
-
-       if (pid > 0)
+       case 0:
+               break;
+       default:
                /* exit the parent process */
                _exit(EXIT_SUCCESS);
+       }
 
        /* release the current working directory */
 
@@ -153,6 +161,10 @@ daemonize_detach(void)
 
        setsid();
 
+#else
+       g_error("no support for daemonizing");
+#endif
+
        g_debug("daemonized!");
 }
 
-- 
1.6.3.3


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Musicpd-dev-team mailing list
Musicpd-dev-team@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/musicpd-dev-team

Reply via email to