Fix: Detach from controlling TTY if daemonized

This commit is contained in:
Thomas Ries 2007-11-28 20:51:45 +00:00
parent 26ca852a45
commit 6859b0b9b3
2 changed files with 19 additions and 0 deletions

View File

@ -1,5 +1,6 @@
0.6.1
=====
28-Nov-2007: - Fix: Detach from controlling TTY if daemonized
22-Nov-2007: - Working on code that that will allow siproxd to use
separate interfaces in its "in front of NAT routes" setup.
( UA1[LAN0]--siproxd--LAN1--NAT--Internet--UA2 )

View File

@ -25,6 +25,7 @@
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <netinet/in.h>
#include <arpa/inet.h>
@ -215,6 +216,23 @@ int main (int argc, char *argv[])
if (fork()!=0) exit(0);
log_set_stderr(0);
/* detach STDIN/OUT/ERR file */
i=open("/dev/null", O_RDWR);
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
if (dup2(i, STDIN_FILENO) <0) {
WARN("detaching STDIN failed: %s",strerror(errno));
}
if (dup2(i, STDOUT_FILENO) <0) {
WARN("detaching STDOUT failed: %s",strerror(errno));
}
if (dup2(i, STDERR_FILENO) <0) {
WARN("detaching STDERR failed: %s",strerror(errno));
}
close(i);
INFO("daemonized, pid=%i", getpid());
}