How to divorce a process from the controlling tty (was syslogd.c)

Mark A. Rosenstein mar at athena.mit.edu
Wed Jun 13 03:53:38 AEST 1990


In article <11388 at pixar.UUCP>, bp at pixar.UUCP (Bruce Perens) writes:
|> Here's how I would divorce a process from a controlling tty. Feel
|> free to criticize and correct this:
	[some code deleted]
|> 	/*
|> 	 * Here's the fun part. See if there is still a controlling
|> 	 * tty by opening /dev/tty. If you don't have a controlling
|> 	 * tty, the open will fail! If it succeeds, do ioctl TIOCNOTTY
|> 	 * on that fd, and close it again.
|> 	 */
|> 	 if ( (tty_fd = open("/dev/tty", 0)) >= 0 ) {
|> 		ioctl(tty_fd, TIOCNOTTY, 0);
|> 		close(tty_fd);
|> 	 }

Some code like that caused a heisenbug (you know, one where observing it
affects the outcome) here last year.  We were restarting our nameserver
regularly from a daemon.  If the daemon were restarted by someone netting
into the machine, then the daemon's controlling tty was a pseudo-tty.
When the nameserver got to the code fragment above, if no one was currently
logged in on that pseudo-tty, then it would hang.  When we noticed it
wasn't running and logged in to check on it, we would usually log in on
the correct pseudo-tty to get it running again and not be able to tell
that it had stopped.

I fixed this by including the non-blocking I/O flag in the open(), but
that requires a kernel mod from standard 4.3BSD for that to work.  Does
anyone know a better solution for this?
					-Mark

                Variables won't and constants aren't.



More information about the Comp.unix.wizards mailing list