Skip to content

Instantly share code, notes, and snippets.

@oxnz
Created March 31, 2014 07:08
Show Gist options
  • Save oxnz/9886860 to your computer and use it in GitHub Desktop.
Save oxnz/9886860 to your computer and use it in GitHub Desktop.
#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