/ Published in: C
Skeleton daemon process for unix
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** * Skeleton daemon code. * Author: Sukanta K. Hazra <sukanta at hazra dot net> */ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <syslog.h> #include <stdlib.h> #include <unistd.h> /** * This function forks out a child process and exits the parent */ int daemon_init(void) { pid_t pid; if ((pid = fork()) < 0) { return -1; } else if (pid !=0) { } /* child continues */ setsid(); /* become session leader */ chdir("/"); /* change working directory */ umask(0); /* clear our file mode creation mask */ close(STDIN_FILENO); /* close the unneeded file descriptors */ close(STDOUT_FILENO); close(STDERR_FILENO); } int main(int argc, char *argv[]) { daemon_init(); /* Execute the processing code here */ /* For logging you might want to use syslog or open a log file */ syslog(LOG_ERR, "SKH: Entering the main code section"); return 0; }