Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!think.com!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!erbe.se!prc From: prc@erbe.se (Robert Claeson) Newsgroups: comp.unix.questions Subject: Re: Starting a daemon on a SVR3 Message-ID: <1991Jan7.120329.787@erbe.se> Date: 7 Jan 91 12:03:29 GMT References: <25360@adm.brl.mil> Organization: ERBE DATA AB, Jakobsberg, Sweden Lines: 47 In article <25360@adm.brl.mil> wolf@grasp1.univ-lyon1.fr writes: >On an old AIX, I use following code to start a daemon (which does not run >with root privileges). [Source code deleted.] > setbuf(stdout,NULL); printf("Listserv v0.99: daemon started\n\r"); > if (fork()) exit (0); /* Dissociate us from tty */ > for (i=getdtablesize()-1; i>=0; i--) > (void)close(i); > (void)open("/",0); > (void)dup2(0,1); > (void)dup2(0,2); > i=open("/dev/tty",2); > if (i>=0) { > ioctl(i,TIOCNOTTY,(char *)0); > (void)close(i); > } >This works fine, but I get trouble when porting this daemon to an SRV3. >getdtablesize does not exist anymore, so I close all open files manually >with a fixed loop (I put 4 in first index). >The ioctl call does also not exist, and I found no equivalence. >After having executed the new code it loads itself in background and >ps shows me it does not depend anymore on a tty. Despite this, when >I loggoff, the process is being killed. Can anyone explain me what I >have to do to start properly a daemon? Thanx. I believe that something along these lines should work: setbuf(stdout,NULL); printf("Listserv v0.99: daemon started\n\r"); if (fork()) /* Disaccociate us from controlling tty */ exit(0); setpgrp(); /* Start a new process group */ if (fork()) /* ...and disaccociate us again */ exit(0); I only have a vague memory of why the second fork() is needed. -- Robert Claeson |Reasonable mailers: rclaeson@erbe.se ERBE DATA AB | Dumb mailers: rclaeson%erbe.se@sunet.se Jakobsberg, Sweden | Perverse mailers: rclaeson%erbe.se@encore.com Any opinions expressed herein definitely belongs to me and not to my employer.