Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

298 Process Relationships Chapter 9


FreeBSD Linux Mac OS X Solaris
Method 8.0 3.2.0 10.6.8 10

openwithoutO_NOCTTY •••
TIOCSCTTY ioctlcommand • •••

Figure 9.8 How various implementations allocate controlling terminals

Thereare times when a program wants to talk to the controlling terminal, regardless
of whether the standardinput or standardoutput is redirected. The way a program
guarantees that it is talking to the controlling terminal is toopenthe file/dev/tty.
This special file is a synonym within the kernel for the controlling terminal. Naturally,
if the program doesn’t have a controlling terminal, theopenof this device will fail.
The classic example is thegetpass( 3 )function, which reads a password(with
terminal echoing turned off, of course). This function is called by thecrypt( 1 )program
and can be used in a pipeline. For example,
crypt < salaries | lpr

decrypts the filesalariesand pipes the output to the print spooler.Becausecrypt
reads its input file on its standardinput, the standardinput can’t be used to enter the
password. Also,cryptis designed so that we have to enter the encryption password
each time we run the program, to prevent us from saving the password in a file (which
could be a security hole).
Thereare known ways to break the encoding used by thecryptprogram. See
Garfinkel et al.[ 2003 ]for moredetails on encrypting files.

9.7 tcgetpgrp, tcsetpgrp,and tcgetsid Functions


We need a way to tell the kernel which process group is the foreground process group,
so that the terminal device driver knows where to send the terminal input and the
terminal-generated signals (Figure9.7).
#include <unistd.h>
pid_t tcgetpgrp(intfd);
Returns: process group ID of foreground process group if OK,−1 on error
int tcsetpgrp(intfd,pid_tpgrpid);
Returns: 0 if OK,−1 on error

The functiontcgetpgrpreturns the process group ID of the foreground process group
associated with the terminal open onfd.
If the process has a controlling terminal, the process can calltcsetpgrpto set the
foreground process group ID topgrpid.The value ofpgrpidmust be the process group
ID of a process group in the same session, andfdmust refer to the controlling terminal
of the session.
Free download pdf