158f0484fSRodney W. Grimes /*- 21b88e35bSBruce Evans * Copyright (c) 1990, 1993, 1994 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 658f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 758f0484fSRodney W. Grimes * are met: 858f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 958f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1058f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1258f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1358f0484fSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 1458f0484fSRodney W. Grimes * must display the following acknowledgement: 1558f0484fSRodney W. Grimes * This product includes software developed by the University of 1658f0484fSRodney W. Grimes * California, Berkeley and its contributors. 1758f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 1858f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1958f0484fSRodney W. Grimes * without specific prior written permission. 2058f0484fSRodney W. Grimes * 2158f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2258f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2358f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2458f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2558f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2658f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2758f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2858f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2958f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3058f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3158f0484fSRodney W. Grimes * SUCH DAMAGE. 3258f0484fSRodney W. Grimes */ 3358f0484fSRodney W. Grimes 3458f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 351b88e35bSBruce Evans static char sccsid[] = "@(#)pty.c 8.3 (Berkeley) 5/16/94"; 3658f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 3758f0484fSRodney W. Grimes 3858f0484fSRodney W. Grimes #include <sys/types.h> 3958f0484fSRodney W. Grimes #include <sys/ioctl.h> 401b88e35bSBruce Evans #include <sys/stat.h> 411b88e35bSBruce Evans 4258f0484fSRodney W. Grimes #include <errno.h> 431b88e35bSBruce Evans #include <fcntl.h> 4458f0484fSRodney W. Grimes #include <grp.h> 451b88e35bSBruce Evans #include <stdio.h> 461b88e35bSBruce Evans #include <stdlib.h> 471b88e35bSBruce Evans #include <string.h> 481b88e35bSBruce Evans #include <termios.h> 491b88e35bSBruce Evans #include <unistd.h> 5058f0484fSRodney W. Grimes 5158f0484fSRodney W. Grimes openpty(amaster, aslave, name, termp, winp) 5258f0484fSRodney W. Grimes int *amaster, *aslave; 5358f0484fSRodney W. Grimes char *name; 5458f0484fSRodney W. Grimes struct termios *termp; 5558f0484fSRodney W. Grimes struct winsize *winp; 5658f0484fSRodney W. Grimes { 5758f0484fSRodney W. Grimes static char line[] = "/dev/ptyXX"; 5858f0484fSRodney W. Grimes register const char *cp1, *cp2; 5958f0484fSRodney W. Grimes register int master, slave, ttygid; 6058f0484fSRodney W. Grimes struct group *gr; 6158f0484fSRodney W. Grimes 6258f0484fSRodney W. Grimes if ((gr = getgrnam("tty")) != NULL) 6358f0484fSRodney W. Grimes ttygid = gr->gr_gid; 6458f0484fSRodney W. Grimes else 6558f0484fSRodney W. Grimes ttygid = -1; 6658f0484fSRodney W. Grimes 671338e009SJordan K. Hubbard for (cp1 = "pqrsPQRS"; *cp1; cp1++) { 6858f0484fSRodney W. Grimes line[8] = *cp1; 691338e009SJordan K. Hubbard for (cp2 = "0123456789abcdefghijklmnopqrstuv"; *cp2; cp2++) { 701b88e35bSBruce Evans line[5] = 'p'; 7158f0484fSRodney W. Grimes line[9] = *cp2; 7258f0484fSRodney W. Grimes if ((master = open(line, O_RDWR, 0)) == -1) { 7358f0484fSRodney W. Grimes if (errno == ENOENT) 7458f0484fSRodney W. Grimes return (-1); /* out of ptys */ 7558f0484fSRodney W. Grimes } else { 7658f0484fSRodney W. Grimes line[5] = 't'; 7758f0484fSRodney W. Grimes (void) chown(line, getuid(), ttygid); 7858f0484fSRodney W. Grimes (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP); 7958f0484fSRodney W. Grimes (void) revoke(line); 8058f0484fSRodney W. Grimes if ((slave = open(line, O_RDWR, 0)) != -1) { 8158f0484fSRodney W. Grimes *amaster = master; 8258f0484fSRodney W. Grimes *aslave = slave; 8358f0484fSRodney W. Grimes if (name) 8458f0484fSRodney W. Grimes strcpy(name, line); 8558f0484fSRodney W. Grimes if (termp) 8658f0484fSRodney W. Grimes (void) tcsetattr(slave, 8758f0484fSRodney W. Grimes TCSAFLUSH, termp); 8858f0484fSRodney W. Grimes if (winp) 8958f0484fSRodney W. Grimes (void) ioctl(slave, TIOCSWINSZ, 9058f0484fSRodney W. Grimes (char *)winp); 9158f0484fSRodney W. Grimes return (0); 9258f0484fSRodney W. Grimes } 9358f0484fSRodney W. Grimes (void) close(master); 9458f0484fSRodney W. Grimes } 9558f0484fSRodney W. Grimes } 9658f0484fSRodney W. Grimes } 9758f0484fSRodney W. Grimes errno = ENOENT; /* out of ptys */ 9858f0484fSRodney W. Grimes return (-1); 9958f0484fSRodney W. Grimes } 10058f0484fSRodney W. Grimes 10158f0484fSRodney W. Grimes forkpty(amaster, name, termp, winp) 10258f0484fSRodney W. Grimes int *amaster; 10358f0484fSRodney W. Grimes char *name; 10458f0484fSRodney W. Grimes struct termios *termp; 10558f0484fSRodney W. Grimes struct winsize *winp; 10658f0484fSRodney W. Grimes { 10758f0484fSRodney W. Grimes int master, slave, pid; 10858f0484fSRodney W. Grimes 10958f0484fSRodney W. Grimes if (openpty(&master, &slave, name, termp, winp) == -1) 11058f0484fSRodney W. Grimes return (-1); 11158f0484fSRodney W. Grimes switch (pid = fork()) { 11258f0484fSRodney W. Grimes case -1: 11358f0484fSRodney W. Grimes return (-1); 11458f0484fSRodney W. Grimes case 0: 11558f0484fSRodney W. Grimes /* 11658f0484fSRodney W. Grimes * child 11758f0484fSRodney W. Grimes */ 11858f0484fSRodney W. Grimes (void) close(master); 11958f0484fSRodney W. Grimes login_tty(slave); 12058f0484fSRodney W. Grimes return (0); 12158f0484fSRodney W. Grimes } 12258f0484fSRodney W. Grimes /* 12358f0484fSRodney W. Grimes * parent 12458f0484fSRodney W. Grimes */ 12558f0484fSRodney W. Grimes *amaster = master; 12658f0484fSRodney W. Grimes (void) close(slave); 12758f0484fSRodney W. Grimes return (pid); 12858f0484fSRodney W. Grimes } 129