/*
** CVS: $Id: daemon.c,v 1.4 2001/01/11 04:03:57 zhangsz Exp $
** daemon.c 
** created at 2000/08/22
** By Zhang Shuzhong.
** Functions of daemon process.
*/
#include     "./include/iptrans.h"
#include     <sys/stat.h>
#include     <fcntl.h>

#define	MAXFD	64

/*@ignore@*/
int
InitDaemon(void)
{
    pid_t        pid;

    if ( (pid = fork()) < 0)
        return FAILURE;
    else if (pid != 0)
        exit(0);        /* parent goes bye-bye */
    /* child continues */
    setsid();            /* become session leader */
//    chdir("/");            /* change working direactory */
    umask(0);            /* clear our file mode creation mask */
    return SUCCESS;
}


void
DaemonInit(const char *pname, int facility)
{
	int		i;
	pid_t	pid;

	if ( (pid = fork()) < 0)		 /* parent terminates */
		err_sys("%s: %s: fork (%d)", 
				FILE_MACRO, FUNCTION_MACRO, LINE_MACRO);
    else if (pid != 0)
        exit(0);        /* parent goes bye-bye */

	/* 41st child continues */
	setsid();				/* become session leader */

	signal(SIGHUP, SIG_IGN);
	
	if ( (pid = fork()) < 0)		 /* 1st child terminates */
		err_sys("%s: %s: fork (%d)", 
				FILE_MACRO, FUNCTION_MACRO, LINE_MACRO);
    else if (pid != 0)
        exit(0);        /* 1st child goes bye-bye */


//	chdir("/");				/* change working directory */

	umask(0);				/* clear our file mode creation mask */

	for (i = 0; i < MAXFD; i++)
		close(i);

	openlog(pname, LOG_PID, facility);
}
/*@end@*/
