Proper way to detach from control terminal?

Larry McVoy lm at snafu.Sun.COM
Tue Jan 31 12:21:09 AEST 1989


>I wrote:
>>	/*
>>	 * TIOCNOTTY will get rid of my tty & set my pgrp to 0
>>	 */
>>	ioctl(0, TIOCNOTTY, 0);

Guy wrote:
>RU sure?  I think TIOCNOTTY only works on "/dev/tty" - it's caught not
>by the standard line discipline, say, but by the "/dev/tty" driver.

I say: As usual, oh Master, little grasshopper has made mistake.  So solly.

Do it thusly:

	fd = open("/dev/tty", 2);
	if (fd != -1) ioctl(fd....)

>>The problem comes when you don't want a ctty but you do want to talk to a tty.
>>Open() has the unfortunate side effect of handing out cttys' even when you
>>don't want one.  You can do the following in the specified environment:
>
>Note that you have to do those operations *after* you've opened the tty
>in question, and gotten it as a controlling tty; doing it before won't
>help. 

Indeed, quite correct.

>>POSIX:
>>	Do a setsid().
>
>Or use the O_NOCTTY flag when opening the tty, which prevents "open"
>from making it a controlling tty in the first place.

Unfortunately, the most common case is that you have gotten your ctty
from login or whatever, so the NOCTTY doesn't help.

Larry McVoy, Lachman Associates.			  My opinions are that.



More information about the Comp.unix.wizards mailing list