Tech Nuske

Tech Nuske

Saturday, 14 April 2012

Convert your C Program into Linux Daemon



v What is Daemon?
A daemon (or service) is a background process that is designed to run autonomously, with little or no user intervention. The Apache web server http daemon (httpd) is one such example of a daemon. It waits in the background listening on specific ports, and serves up pages or processes scripts, based on the type of request.

v What Daemon is going to do?
A Daemon can be made to do one specific thing such as writing log to some event or managing many mailboxes on multiple domains.

Daemons should never have direct communication with a user through a terminal. In fact, a daemon shouldn't communicate directly with a user at all. All communication should pass through some sort of interface (which you may or may not have to write), which can be GUI, or as simple as a signal set.

v Steps to create a Daemon.
1.      Fork off the parent process (using fork system call)
2.      Change file mode mask (using umask system call)
3.      Open any logs for writing (optional but recommended)
4.      Create a unique Session ID (using setsid system call)
5.      Change the current working directory to a safe place (using chdir system call)
6.      Close standard file descriptors
7.      Enter actual daemon code

v Example to implement above steps :

int main(void) {
       
        /* process ID and Session ID */
        pid_t pid, sid;
       
        /* Fork off the parent process */
        pid = fork();
        if (pid ==  -1) {
                exit(EXIT_FAILURE);
        }
        /* If forking success, then
           We can exit the parent process. */
        if (pid > 0) {
                exit(EXIT_SUCCESS);
        }

        /* Change the file mode mask */
        umask(0);
               
        /* Open any logs here */       
               
        /* Create a new session (SID) for the child process */
        sid = setsid();
        if (sid < 0) {
                /* Log the failure */
                exit(EXIT_FAILURE);
        }
       
        /* Change the current working directory */
        if ((chdir("/")) < 0) {
                /* Log the failure */
                exit(EXIT_FAILURE);
        }
       
        /* Close out the standard file descriptors */
        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
       
        /* Daemon-specific initialization goes here */
       
        /* The Big Loop */
        while (1) {

           /* Do Actual task of Daemon here ... */
           
           sleep(_SOME_SECOND_); /* wait for specific seconds */
        }
   exit(EXIT_SUCCESS);
}

That’s it.  Now, your C program is running as a Daemon.

5 comments:

  1. Thanks for sharing such a useful and informative article. I definitely got to learn something new

    ReplyDelete
  2. Nice information

    ReplyDelete
  3. Why do you have to dork from a parent process? Why not just have the process close stdin, stdout, stderr, etc. without doing a fork first?

    ReplyDelete
  4. Actually, Making a daemon means, making it silent and make it leave terminal (or whoever has called it) asap. If we'll not fork from parent, then parent will never leave terminal, though if it closes stdin or stdout.

    ReplyDelete