Daemonise.c

From Organic Design wiki
Revision as of 11:51, 5 July 2006 by Nad (talk | contribs) (paste code from robs example proxy code)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

pid_t pid; int hNull;

if ((pid = fork()) < 0) { syslog(LOG_ERR, "fork: %m"); shutdown(evListen->h, 2); return errno; } else if (pid) return 0;

close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO);

hNull = open("/dev/null", O_RDWR); dup2(hNull, STDIN_FILENO); dup2(hNull, STDOUT_FILENO); dup2(hNull, STDERR_FILENO);

if(hNull > STDERR_FILENO) close(hNull);

setsid();