The Linux Programming Interface

(nextflipdebug5) #1

1382 Chapter 64


z change the permissions on the slave so that the owner has read and write per-
missions, and group has write permission.

The reason for changing the group of the terminal to tty and enabling group write
permission is that the wall(1) and write(1) programs are set-group-ID programs
owned by the tty group.
On Linux, a pseudoterminal slave is automatically configured in the above
manner, which is why calling grantpt() isn’t needed (but still should be done).

Because it may create a child process, SUSv3 says that the behavior of grantpt()
is unspecified if the calling program has installed a handler for SIGCHLD.

64.2.3 Unlocking the Slave: unlockpt()


The unlockpt() function removes an internal lock on the slave corresponding to the
pseudoterminal master referred to by the file descriptor mfd. The purpose of this
locking mechanism is to allow the calling process to perform whatever initialization
is required for the pseudoterminal slave (e.g., calling grantpt()) before another pro-
cess is allowed to open it.

An attempt to open a pseudoterminal slave before it has been unlocked with
unlockpt() fails with the error EIO.

64.2.4 Obtaining the Name of the Slave: ptsname()


The ptsname() function returns the name of the pseudoterminal slave correspond-
ing to the pseudoterminal master referred to by the file descriptor mfd.

On Linux (as on most implementations), ptsname() returns a name of the form /dev/
pts/nn, where nn is replaced by a number that uniquely identifies this pseudotermi-
nal slave.
The buffer used to return the slave name is normally statically allocated. It is
thus overwritten by subsequent calls to ptsname().

#define _XOPEN_SOURCE 500
#include <stdlib.h>

int unlockpt(int mfd);
Returns 0 on success, or –1 on error

#define _XOPEN_SOURCE 500
#include <stdlib.h>

char *ptsname(int mfd);
Returns pointer to (possibly statically allocated) string on success,
or NULL on error
Free download pdf