[comp.unix.wizards] Daemonizing question...

auvhess@auvc6.tamu.edu (David K. Hess) (02/22/91)

Quick question which should get a quick answer. I am daemonizing
a process using the standard steps as lifted from inetd.c.

	fork()
	close(open descriptors)
	open("/")
	dup(0) 
	open("/dev/tty")
	ioctl(tty,TIOCNOTTY)

I understand all these steps except the open("/") and the subsequent
dup() of the resultant file descriptor. Can somebody e-mail the reasons
for and effects of this step? I've looked through every manual and man 
page that might remotely help and came up with nothing.

Thanks.

Dave Hess
Graduate Student
Texas A&M University
auvhess@auvsun1.tamu.edu

auvhess@auvc6.tamu.edu (David K. Hess) (02/23/91)

Thanks to Jonathan Kamens and Xing Wu for the answer to this one. Due to
two requests for the answer I'm posting it here.

>	fork()
>	close(open descriptors)
>	open("/")
>	dup(0)
>	open("/dev/tty")
>	ioctl(tty,TIOCNOTTY)

The reason for the open("/") and dup(0) steps is so that any eventual
child process that might be fork()-exec()'d off has valid file
descriptors 0, 1, and 2 to play with and still not have a controlling
terminal. The reason "/" is used is historical. Apparently when the
daemonizing code was written "/dev/null" (a more likely candidate) was
not gauranteed to exist under all flavors of Unix (may still not for
all I know ;-).

Thanks for the help.

Dave Hess
Graduate Student
Texas A&M University
auvhess@auvsun1.tamu.edu