Difference between revisions of "Daemonise.c"

From Organic Design wiki
(paste code from robs example proxy code)
 
m
Line 17: Line 17:
 
dup2(hNull, STDERR_FILENO);
 
dup2(hNull, STDERR_FILENO);
  
if(hNull > STDERR_FILENO) close(hNull);
+
if (hNull > STDERR_FILENO) close(hNull);
  
 
setsid();
 
setsid();

Revision as of 11:52, 5 July 2006

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();