Starting a daemon on a SVR3
Heiko Blume
src at scuzzy.in-berlin.de
Wed Jan 9 01:33:50 AEST 1991
prc at erbe.se (Robert Claeson) writes:
>In article <1991Jan03.132149.3565 at scuzzy.in-berlin.de> I wrote:
>>[close()ing all fd's]
>But is it really nessecary to close all files in order to create a daemon
>under System V? I haven't done it, and yet everything seems to work as
>it should.
no, it's necessary to close all file descriptors that refer to the tty
that the daemon is started from, *plus* calling setpgrp(),
so that it runs independant.
(or, better said, all ttys that you don't want to get endangered)
that way you avoid getting SIGHUP etc., don't keep init from
respawning getty on ports with modem control, etc pepe.
to ensure that, you just close everything.
the following works on my sys v box (isc 2.2.1), it's called
in the child after a successul fork():
/*
* generic function to make a daemon out of a simple fork()ed process.
* however, if the daemon open()s a tty later it will be associated with
* that tty thereafter, even if a close() is issued.
*/
void daemonize() {
int fds=sysconf(_SC_OPEN_MAX); /* posixish */
while(fds)
(void) close(--fds); /* close ALL file descriptors */
setpgrp(); /* disassociate from terminal */
sigset(SIGHUP,SIG_IGN)
sigset(SIGINT,SIG_IGN);
sigset(SIGQUIT,SIG_IGN); /* don't disturb me */
/* etc */
}
if you want to use redirection from/to regular files with your
daemon you should use
[...]
while(fds)
if(isatty(--fds)) (void) close(fds);
[...]
thereby closing only file desriptors that are associated with a terminal
device.
--
Heiko Blume <-+-> src at scuzzy.in-berlin.de <-+-> (+49 30) 691 88 93
public source archive [HST V.42bis]:
scuzzy Any ACU,f 38400 6919520 gin:--gin: nuucp sword: nuucp
uucp scuzzy!/src/README /your/home
More information about the Comp.unix.questions
mailing list