diff dcclib/daemon.c @ 0:c7f6b056b673

First import of vendor version
author Peter Gervai <grin@grin.hu>
date Tue, 10 Mar 2009 13:49:58 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dcclib/daemon.c	Tue Mar 10 13:49:58 2009 +0100
@@ -0,0 +1,48 @@
+/* compatibility hack for old systems lacking daemon() */
+
+#include "dcc_config.h"
+#include "dcc_paths.h"
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <fcntl.h>
+
+int
+dcc_daemon(int nochdir, int noclose)
+{
+	int retv;
+
+	if (!nochdir) {
+		if (chdir("/") == -1)
+			perror("chdir /");
+	}
+	retv = fork();
+	if (retv == -1)
+		return -1;		/* fork() failed */
+
+	if (retv != 0)
+		_exit(0);		/* parent of new child */
+
+	/* fork again after setsid() so that the PID is not the session
+	 * group leader so that opening a tty device won't make a
+	 * controlling terminal. */
+	setsid();
+	retv = fork();
+	if (retv == -1) {
+		perror("fork");		/* second fork() failed */
+		exit(1);		/* cannot tell caller */
+	}
+	if (retv != 0)
+		_exit(0);		/* parent of final child */
+
+	if (!noclose) {
+		close(0);
+		close(1);
+		close(2);
+		open(_PATH_DEVNULL, O_RDWR, 0666);
+		dup2(0, 1);
+		dup2(0, 2);
+	}
+	return 0;
+}