aboutsummaryrefslogtreecommitdiffstats
path: root/src/daemon.c
diff options
context:
space:
mode:
authorMike Frysinger <vapier@gentoo.org>2009-08-12 21:03:02 -0400
committerMax Kellermann <max@duempel.org>2009-08-13 17:25:30 +0200
commit499ed62dd790949c571517b54ef0e96fad26b16b (patch)
tree540ee08426ccabf4bb12b8058dc4491f9bbf7a55 /src/daemon.c
parentc3e02bec3b69c386de72a2c66a63ecfaafa0db9e (diff)
downloadmpd-499ed62dd790949c571517b54ef0e96fad26b16b.tar.gz
mpd-499ed62dd790949c571517b54ef0e96fad26b16b.tar.xz
mpd-499ed62dd790949c571517b54ef0e96fad26b16b.zip
use daemon() when the C library provides it
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 <vapier@gentoo.org>
Diffstat (limited to '')
-rw-r--r--src/daemon.c24
1 files changed, 18 insertions, 6 deletions
diff --git a/src/daemon.c b/src/daemon.c
index 5ae79e050..192fa8d37 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!");
}