summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorFelix Fietkau <nbd@openwrt.org>2012-12-30 22:13:33 +0100
committerFelix Fietkau <nbd@openwrt.org>2012-12-30 22:13:33 +0100
commit88dc4e3e572b015e9240d22de2a07f3ca6c39e00 (patch)
treef09c68a8ff737d06bc7714e6fa11dea4a4b923f7 /main.c
parent32760c47e28d2a4313359cbff4bd93c905cc85dd (diff)
add forking
Diffstat (limited to 'main.c')
-rw-r--r--main.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/main.c b/main.c
index 66d12b1..41b8eba 100644
--- a/main.c
+++ b/main.c
@@ -136,6 +136,7 @@ int main(int argc, char **argv)
bool nofork = false;
char *port;
int opt, ch;
+ int cur_fd;
init_defaults();
signal(SIGPIPE, SIG_IGN);
@@ -251,5 +252,31 @@ int main(int argc, char **argv)
uh_config_parse();
+ /* fork (if not disabled) */
+ if (!nofork) {
+ switch (fork()) {
+ case -1:
+ perror("fork()");
+ exit(1);
+
+ case 0:
+ /* daemon setup */
+ if (chdir("/"))
+ perror("chdir()");
+
+ cur_fd = open("/dev/null", O_WRONLY);
+ if (cur_fd > 0) {
+ dup2(cur_fd, 0);
+ dup2(cur_fd, 1);
+ dup2(cur_fd, 2);
+ }
+
+ break;
+
+ default:
+ exit(0);
+ }
+ }
+
return run_server();
}