+void daemonize(void)
+{
+ pid_t pid;
+
+ pid = fork();
+
+ if (pid < 0) {
+ logthing(LOGTHING_CRITICAL,
+ "Failed to fork into background: %d (%s)",
+ errno,
+ strerror(errno));
+ exit(EXIT_FAILURE);
+ } else if (pid > 0) {
+ logthing(LOGTHING_INFO, "Backgrounded as pid %d.", pid);
+ exit(EXIT_SUCCESS);
+ }
+
+ pid = setsid();
+
+ freopen("/dev/null", "r", stdin);
+ freopen("/dev/null", "w", stdout);
+ freopen("/dev/null", "w", stderr);
+
+ return;
+}
+