home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
GEMini Atari
/
GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso
/
files
/
telecomm
/
nhclb120
/
daemon.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-09-26
|
965b
|
61 lines
/* Detach a daemon process from login session context */
#include <signal.h>
#include <stdio.h>
#if (defined(SUNOS4) || defined(BSD))
#include <sys/param.h>
#define _NFILE NOFILE
#endif
#ifndef _NFILE
#define _NFILE 20
#endif
#ifdef SYSV
extern void exit();
#else
extern int exit();
#endif
void
daemon()
{
int fd;
FILE *fp; /* pointer to current descriptor */
/* if started by init there's no need to detach */
if (getppid() == 1)
goto out;
/* ensure process is not a process group leader */
if (fork() != 0)
exit(0); /* parent */
/* child */
(void)setpgrp(); /* lose ctrl term, chg proc grp */
(void)signal(SIGHUP, SIG_IGN); /* immune from pgrp death */
if (fork() != 0) /* become non pgrp leader */
exit(0); /* first child */
/* close all file descriptors */
out:
_cleanup();
for (fd = 0; fd < _NFILE; fd++) {
(void)close(fd);
}
/* (void)chdir("/"); /* move off any mounted file system */
(void)umask(0);
return;
}