PseudoTerm is a factory for ForkResult objects describing pty connections, or else the system call failure that prevented their setup. The algorithm used here is loosely based on that of [8]; see pp. 692-693 there for an example in C.
For system V platforms, and in particular Linux and Solaris, the slave becomes the controlling terminal on open(). The ioctl call with TIOCSCTTY is used on BSD (and OS X) to acquire a controlling terminal; linux also allows this call, although it is, as stated above, not necessary.
For now, the child_image() procedure calls acquire_ct(), and so performs the ioctl operation unconditionally. As an alternative, the PseudoTerm factory could have architecture-specific control, and although the resulting mode data would be undesirable, it may be necessary to add such in the future.
PseudoTerm is a *non-reentrant* factory for pty setup in support of coprocesses; the plumbing() method may be used as a replacement for fork(), with the pid parameter indicating to the caller whether the return is in the parent or child.
The object bifurcation that allows this to work occurs because the singleton heap allocation is of MAP_PRIVATE memory, and changes to such memory are private (in practice, by COW) to each process, and the parent and child each get, in effect, individual copies of the PseudoTerm object.
Making the plumbing method reentrant, and so thread-safe, would require that: the three scalar reference data members be replaced with a stack of tuples; a lock be used to control access to the stack; calls to the ptsname() system call, which is not reentrant, be locked; and that the return value for the slave pty name be copied out into distinct strings, those also stored on the stack. Note additionally that the strerror() function used in error reporting is also not required to be reentrant.
Bill Pippin 2010-01-14