Xref: utzoo comp.unix.wizards:22386 comp.bugs.4bsd:1566 Path: utzoo!attcan!uunet!zephyr.ens.tek.com!tektronix!sequent!keany From: keany@sequent.UUCP (Bernard Keany) Newsgroups: comp.unix.wizards,comp.bugs.4bsd Subject: Re: more BSD strangeness -- syslogd.c Message-ID: <36447@sequent.UUCP> Date: 8 Jun 90 21:21:00 GMT References: <1990Jun8.070904.7466@athena.mit.edu> Reply-To: keany@crg3.UUCP (Bernie Keany) Organization: Sequent Computer Systems, Inc Lines: 45 In article <1990Jun8.070904.7466@athena.mit.edu> jik@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@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