more BSD strangeness -- syslogd.c

Bernard Keany keany at sequent.UUCP
Sat Jun 9 07:21:00 AEST 1990


In article <1990Jun8.070904.7466 at athena.mit.edu> jik at athena.mit.edu (Jonathan I. Kamens) writes:
>
>  Here's another little gem of code from BSD4.3 that I don't quite
>understand.  Pulled from etc/syslogd.c:
>
>	if (!Debug) {
>		if (fork())
>			exit(0);
>		for (i = 0; i < 10; i++)
>			(void) close(i);
>		(void) open("/", 0);
>		(void) dup2(0, 1);
>		(void) dup2(0, 2);
>		untty();
>	}
>
>OK, so why is it opening "/"?  Is this a poor man's equivalent to
>/dev/null, and if so, why doesn't it just open /dev/null?
>
>Jonathan Kamens			              USnail:
>MIT Project Athena				11 Ashford Terrace
>jik at Athena.MIT.EDU				Allston, MA  02134
>Office: 617-253-8495			      Home: 617-782-0710


The answer as printed in a 12/88 UNIXWORLD article by Dave Lennert
titled "How to Write Unix Daemons" appears below ... Dave gave me a
copy when I started working on such things here and it's been invaluable.

"Change the working directory to /. Each process has a current working
directory, and the kernel holds this directory file open during the life
of the process. If a process has a current directory in a mounted file
system, the file system is "in use" and cannot be dismounted by the
administrator without first finding and killing the offending process.
(The hard part is finding the process!) Unless a process explicitly
alters its current directory via a chdir(2) call, it inherits the current
directory of its parent. When launched from an interactive shell, the
current directory (of the shell and thus the daemon) will be whatever
the user has most recently selected via the cd(1) command.
 Thus daemons should adopt a current directory that is not located on a
mounted file system (assuming that the daemon's purpose allows this). The
most reliable choice is the root directory."
-- 
Bernie Keany		"That's my story and I'm sticking to it"
..sequent!keany



More information about the Comp.unix.wizards mailing list