Return to Snippet

Revision: 23866
at February 16, 2010 04:40 by sukantahazra


Initial Code
/**
  * 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) {
        exit(0); /* parent goes bye-bye */
    }

    /* 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;
}

Initial URL

                                

Initial Description
Skeleton daemon process for unix

Initial Title
Unix daemon process

Initial Tags
unix, linux

Initial Language
C