Network programming: demonize

Demonizing the Server

A server, to be a real server shall be able to run as a demon, it shall run in background and it shall not be linked to a terminal. To do this I'll add two functions to the simple server:

  1. demonize
  2. detachFromTerminal

The first function sets up the process to ignore child, quit, and hup signals, and then it uses a fork() to duplicate the process. The father process will be terminated as soon as possible while the child process will execute a detachFromTerminal and then it will continue its esecution.

The detachFromTerminal function sets the calling process as the leader of a new session, the it associates the streams linked to stdin, stdout and stderr with the /dev/null device.

/*+--------------------------------------------------------------+
  |            demonize.c  -  description                        |
  |                -------------------                           |
  | begin      : 08/01/2010 13.00                                |
  | copyright  : (C) 2010 xAppSoftware                           |
  | author     : Luigi D'Andrea                                  |
  | email      : gg1 ( at ) xappsoftware.com                     |
  | compiling  : gcc -o server server.c                          |
  |                                                              |
  | Latest version on http://www.xappsoftware.com                |
  +--------------------------------------------------------------+
  | demonize library may be redistributed and modified under     |
  | certain conditions. This software is distributed on an       |
  | "AS IS" basis WITHOUT WARRANTY OF ANY KIND, either express or|
  ! implied. See the file License.txt for details.               |
  +--------------------------------------------------------------+*/
/*+--------------------------------------------------------------+
  | SYSTEM INCLUDES                                              |
  +--------------------------------------------------------------+*/
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <string.h>

/*+--------------------------------------------------------------+
  | SPECIFIC INCLUDES                                            |
  +--------------------------------------------------------------+*/
#include "demonize.h"

/*+--------------------------------------------------------------+
  | Function name :  demonize                                    |
  | Parameters    :  the executable name                         |
  | Description   :  Demonizes the caller application            |
  +--------------------------------------------------------------+*/
void demonize(char *arg)
{
	int      childpid;

ignore the SIGHUP, SIGCHLD and the SIGQUIT signal

(void) signal(SIGHUP, SIG_IGN);
	(void) signal(SIGCHLD, SIG_IGN);
	(void) signal(SIGQUIT, SIG_IGN);

Create a new process to use as daemon

                   if ((childpid=fork()) < 0)
        {
               printf("An error occurred while demonizing %s...\n", arg);
        }
        else
        {

The new process will be detached from the terminal

        if (!childpid) /* child */ 
        { 
              printf("Becoming a daemon...\n"); 
              detachFromTerminal(); 
        }
        else /* father */ 
       {

The father process will quit suddenly

            exit(EXIT_SUCCESS); 
        } 
    }
}
/*+--------------------------------------------------------------+ 
  | Function name : detachFromTerminal                           |
  | Parameters : None                                            |
  | Description : Detaches from the terminal the calling         |
  | application.                                                 |
  +--------------------------------------------------------------+*/
void detachFromTerminal(void)
{

Sets the process as session leader

 
    if(setsid()==-1) /* detach from the terminal */

    { 
        perror("setsid()"); 
        exit(EXIT_FAILURE); 
    }
Associates stdin, stdout and stderr to the /dev/null device

    if (freopen("/dev/null", "r", stdin) == NULL) 
    { 
        fprintf(stderr, "Unable to replace stdin with /dev/null: %s\n",  strerror(errno)); 
        exit(EXIT_FAILURE); 
    }
    if (freopen("/dev/null", "w", stdout) == NULL) 
    { 
        fprintf(stderr, "Unable to replace stdout with /dev/null: %s\n",  strerror(errno)); 
        exit(EXIT_FAILURE);
    }
    if (freopen("/dev/null", "w", stderr) == NULL)
    { 
        fprintf(stderr, "Unable to replace stderr with /dev/null: %s\n",  strerror(errno));
        exit(EXIT_FAILURE);
    }
}

SIMPLE UDP CLIENT/SERVER

Network programming index:

The Server

The Client

Demonizing the Server

The source code

Posted by at January 14, 2010
Filed in category: linux, The Prince: C, and tagged with: , , , , ,

2 Responses to Network programming: demonize

  1. Pingback: Network programming: simple udp client server #1

  2. Pingback: Network programming: simple udp client server #2

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <font color="" face="" size=""> <span style="">

Statistical data collected by Statpress SEOlution (blogcraft).