Created
March 31, 2014 07:08
-
-
Save oxnz/9886860 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
void deamonize() { | |
if (fork()) | |
exit(0); // exit parent process | |
setsid(); // become session leader, discard controlling terminal | |
signal(SIGINT, SIG_IGN); | |
signal(SIGCHLD, SIG_IGN); | |
signal(SIGHUP, SIG_IGN); | |
signal(SIGQUIT, SIG_IGN); | |
signal(SIGPIPE, SIG_IGN); | |
if (fork()) | |
exit(0); // no more session leader, cannot reopen a contorl terminal | |
for (int i = 0; i < 3; ++i) | |
close(i); // close file dscriptor | |
chdir("/"); | |
umask(0); | |
} | |
int main() { | |
deamonize(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment