Last active
August 29, 2015 14:14
-
-
Save mihids/6d5f24c63e888d203169 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
| #include <sys/types.h> | |
| #include <sys/stat.h> | |
| #include <fcntl.h> | |
| #define EXIT_SUCCESS 0 | |
| #define EXIT_FAILURE 1 | |
| static void daemonize(void) | |
| { | |
| pid_t pid, sid; | |
| int fd; | |
| /* already a daemon */ | |
| if ( getppid() == 1 ) return; | |
| /* Fork off the parent process */ | |
| pid = fork(); | |
| if (pid < 0) | |
| { | |
| exit(EXIT_FAILURE); | |
| } | |
| if (pid > 0) | |
| { | |
| exit(EXIT_SUCCESS); /*Killing the Parent Process*/ | |
| } | |
| /* At this point we are executing as the child process */ | |
| /* Create a new SID for the child process */ | |
| sid = setsid(); | |
| if (sid < 0) | |
| { | |
| exit(EXIT_FAILURE); | |
| } | |
| /* Change the current working directory. */ | |
| if ((chdir("/")) < 0) | |
| { | |
| exit(EXIT_FAILURE); | |
| } | |
| fd = open("/dev/null",O_RDWR, 0); | |
| if (fd != -1) | |
| { | |
| dup2 (fd, STDIN_FILENO); | |
| dup2 (fd, STDOUT_FILENO); | |
| dup2 (fd, STDERR_FILENO); | |
| if (fd > 2) | |
| { | |
| close (fd); | |
| } | |
| } | |
| /*resettign File Creation Mask */ | |
| umask(027); | |
| } | |
| int main( int argc, char *argv[] ) | |
| { | |
| daemonize(); | |
| while(1) | |
| { | |
| /* Now we are a daemon -- do the work for which we were paid */ | |
| sleep(10); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment