From 45211afd2fe62b361a3062a306c7305fa85479bc Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Wed, 29 Aug 2007 21:48:18 +0200 Subject: [PATCH] Add daemon_detach, and use it. Signed-off-by: Pierre Habouzit --- daemon.c | 22 ++++++++++++++++++++++ daemon.h | 2 ++ srsd.c | 4 ++++ 3 files changed, 28 insertions(+) diff --git a/daemon.c b/daemon.c index c3aac5f..392dccb 100644 --- a/daemon.c +++ b/daemon.c @@ -127,3 +127,25 @@ int accept_nonblock(int fd) 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; +} diff --git a/daemon.h b/daemon.h index 2fec199..e1b458f 100644 --- a/daemon.h +++ b/daemon.h @@ -39,4 +39,6 @@ int tcp_listen_nonblock(const struct sockaddr *addr, socklen_t len); int accept_nonblock(int fd); +int daemon_detach(void); + #endif diff --git a/srsd.c b/srsd.c index 467c2f2..b28b024 100644 --- a/srsd.c +++ b/srsd.c @@ -408,5 +408,9 @@ int main(int argc, char *argv[]) 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); } -- 2.20.1