Proper way to detach from control terminal?
Guy Harris
guy at auspex.UUCP
Sat Jan 28 17:02:51 AEST 1989
>I would like to know if there is a "proper" way to detach a process from
>its control terminal (once it has fork'ed and the parent has
>terminated).
Well, it depends on which flavor of UNIX you're using....
> 1. setpgrp(2)
>
> This is the method described in the System V section
> termio(7), and does seem to work.
One would hope it would, since that's the call you're supposed to use
for that in S5.
> The calling sequence I am using is simply
> setpgrp() for System V and setpgrp(0,getpid())
> for BSD based code. But, on BSD systems, the
> control terminal is still reported when a ps(1) is
> done.
That's because "setpgrp" *isn't* the call you're supposed to use on BSD
systems. "setpgrp" has the side effect of detaching you from your
controlling terminal on S5, but it doesn't have that side effect on BSD.
(The two were invented independently - at least in part - and for
different purposes; the BSD one is used for job control, and processes
are supposed to keep their controlling tty no matter whether they're in
the foreground or in the background.)
> 2. ioctl(...TIOCSPGRP...)
>
> Is using ioctl to set the process group any different
> than using the setpgrp() system call?
Yes, the system call sets the process group of a *process*, while the
"ioctl" (not available currently on vanilla S5) sets the process group
of a *terminal*. Basically, if you type something like your interrupt
character (RUBOUT, ^C, whatever), the terminal driver sends a signal to
the process group to which it belongs, which means it's delivered to all
processes that are currently in that process group.
> 3. ioctl(...TIOCNOTTY...)
>
> I have seen this used in the recently posted plp
> software, but cannot find any documentation on this
> setting other than "void tty association" in the
> header file <sys/ioctl.h> on a BSD system.
That's what "void tty association" means - "void" a process's
"association" with its controling "tty", i.e. detach itself from that
tty. ("Void where prohibited by law", on the other hand, is a command
telling you to urinate, say, on the White House lawn. :-)) The
4.3-tahoe TTY(4) manual page says (I think, earlier 4.3BSD manuals and
maybe the 4.2BSD manual said something similar):
A process can remove the association it has with its controlling
terminal by opening the file "/dev/tty" and issuing an
"ioctl(f, TIOCNOTTY, 0);"
This is often desirable in server processes.
> 4. closing all terminal files
>
> I don't know if I am reading things wrong, but it seems
> that some programs simple close stdin/stdout/stderr on
> startup and open something else as stdin. Does this
> really do anything?
It doesn't detach you from your controlling terminal, that's for
sure....
> Should I do this in concert with any/all of the
> above options?
Yes, you might want to do this if your program is, say, a daemon process
that's trying to run "cleanly" regardless of whether it's started from
"/etc/rc" or from a user's terminal; that way, it won't "hang on" to the
terminal (i.e., it won't hold it open, and won't issue messages to it).
Dave Lennert (formerly of HP, now of Sequent - right, Dave?) wrote a
fairly detailed paper on "how to write a daemon", going into details
about these sorts of things on various UNIX flavors. I think it
appeared in one of the UNIX magazines within the past few months.
More information about the Comp.unix.wizards
mailing list