Add Book to My BookshelfPurchase This Book Online

Appendix D - Pseudo-Terminals

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

BSD Pseudo-Terminals
On BSD systems, where pseudo-terminals were first implemented, master pseudo-terminals have device names like /dev/ptyXX, and slave pseudo-terminals have names like /dev/ttyXX. The procedure for opening a pseudo-terminal is to cycle through all the possible masters, trying to open one. If the open fails, the device is already in use. Once the master side is open, the slave side can also be opened. The code looks something like this:
    char *s, *t;
    int master, slave;
    char mastername[32], slavename[32];
    ·
    ·
    ·
    for (s = "pqrs"; *s != '\0'; s++) {
        for (t = "0123456789abcdef"; *t != '\0'; t++) {
            sprintf(mastername, "/dev/pty%c%c", *s, *t);
   
            if ((master = open(mastername, O_RDWR)) >= 0)
                goto out;
        }
    }
    if (*s == '\0' && *t == '\0')
        /* all pseudo-terminals in use */
    sprintf(slavename, "/dev/tty%c%c", *s, *t);
    slave = open(slavename, O_RDWR);
    ·
    ·
    ·
There are problems with this approach. If the number of pseudo-terminals is ever increased, the program will have to be modified to know about the new device names. Further, there is a race condition between opening the master and opening the slave. This race condition presents certain security problems.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback