Difference between revisions of "Daemonise.c"

From Organic Design wiki
(don't close stdout while testing)
m
Line 9: Line 9:
 
chdir("/");
 
chdir("/");
 
umask(0);
 
umask(0);
//close(STDIN_FILENO);
+
close(STDIN_FILENO);
//close(STDOUT_FILENO);
+
close(STDOUT_FILENO);
//close(STDERR_FILENO);
+
close(STDERR_FILENO);

Revision as of 22:53, 21 July 2006

// Convert running program into a system daemon pid_t pid = fork(); if (pid > 0) exit(EXIT_SUCCESS); if (pid < 0) exit(logErr("First fork() failed!")); if (setsid() < 0) exit(logErr("setsid() failed!")); pid = fork(); if (pid > 0) exit(EXIT_SUCCESS); if (pid < 0) exit(logErr("Second fork() failed!")); chdir("/"); umask(0); close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO);