Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!unido!fauern!opal!tmpmbx!scuzzy!src From: src@scuzzy.in-berlin.de (Heiko Blume) Newsgroups: comp.unix.questions Subject: Re: Starting a daemon on a SVR3 Message-ID: <1991Jan08.143350.3740@scuzzy.in-berlin.de> Date: 8 Jan 91 14:33:50 GMT References: <25360@adm.brl.mil> <1752@dschub.dsc.com> <1991Jan03.132149.3565@scuzzy.in-berlin.de> <1991Jan7.120554.884@erbe.se> Organization: Contributed Software Lines: 53 prc@erbe.se (Robert Claeson) writes: >In article <1991Jan03.132149.3565@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@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