Path: utzoo!censor!geac!torsqnt!news-server.csri.toronto.edu!cs.utexas.edu!usc!ucsd!hub.ucsb.edu!dschub!neptune!jky From: jky@neptune.dsc.com (Jacob Kan Yu) Newsgroups: comp.unix.questions Subject: Re: Starting a daemon on a SVR3 Message-ID: <1752@dschub.dsc.com> Date: 2 Jan 91 21:36:03 GMT References: <25360@adm.brl.mil> Sender: news@dschub.dsc.com Lines: 54 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). > 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). What's about this: for ( i = 0 ; i < NOFILE ; i++ ) close( i ); The problem I have is that "NOFILE" is 20 for System V/386 Release 3.0 and is 60 for Release 3.2 and the comments in "" says this number is no longer a constant and should be removed in the next release. Anybody have better idea? >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 am not sure about this ioctl call but you probably should have your program to ignore "SIGHUP" signal. >-- >Christophe Wolfhugel | Email: wolf@grasp1.univ-lyon1.fr >INSA Lyon - Departement Informatique | Fax: (+33) 72 44 08 00 >69621 Villeurbanne Cedex, France | (Attn-To: Wolfhugel - C 210) Jacob Yu Internet: jky%neptune%dschub@hub.ucsb.edu Uucp: pyramid!ucsbcsl!dschub!neptune!jky Life is but a walking shadow. > Disclaimer: these are my own opinions, not my employer's. Yes, these are my own opinions too.