Signed-off-by: Pierre Habouzit <madcoder@debian.org>
return sock;
}
+
+int daemon_detach(void)
+{
+ pid_t pid;
+
+ close(STDIN_FILENO);
+ close(STDOUT_FILENO);
+ close(STDERR_FILENO);
+
+ open("/dev/null", O_RDWR);
+ open("/dev/null", O_RDWR);
+ open("/dev/null", O_RDWR);
+
+ pid = fork();
+ if (pid < 0)
+ return -1;
+ if (pid)
+ exit(0);
+
+ setsid();
+ return 0;
+}
int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len);
int accept_nonblock(int fd);
+int daemon_detach(void);
+
#endif
return EXIT_FAILURE;
}
+ if (daemon_detach() < 0) {
+ syslog(LOG_CRIT, "unable to fork");
+ return EXIT_FAILURE;
+ }
return main_loop(srs, argv[optind], port_enc, port_dec);
}