Home | History | Annotate | Line # | Download | only in libutil
pty.c revision 1.6
      1 /*-
      2  * Copyright (c) 1990, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #if defined(LIBC_SCCS) && !defined(lint)
     35 #if 0
     36 static char sccsid[] = "@(#)pty.c	8.1 (Berkeley) 6/4/93";
     37 #else
     38 static char rcsid[] = "$NetBSD: pty.c,v 1.6 1996/05/15 21:42:33 jtc Exp $";
     39 #endif
     40 #endif /* LIBC_SCCS and not lint */
     41 
     42 #include <sys/cdefs.h>
     43 #include <sys/types.h>
     44 #include <sys/stat.h>
     45 #include <sys/ioctl.h>
     46 #include <fcntl.h>
     47 #include <termios.h>
     48 #include <errno.h>
     49 #include <unistd.h>
     50 #include <stdio.h>
     51 #include <string.h>
     52 #include <grp.h>
     53 #include <util.h>
     54 
     55 #ifdef i386
     56 /* PCVT conflicts with ttyv*. */
     57 #define TTY_LETTERS "pqrstuwxyzPQRST"
     58 #else
     59 #define TTY_LETTERS "pqrstuvwxyzPQRST"
     60 #endif
     61 
     62 int
     63 openpty(amaster, aslave, name, termp, winp)
     64 	int *amaster, *aslave;
     65 	char *name;
     66 	struct termios *termp;
     67 	struct winsize *winp;
     68 {
     69 	static char line[] = "/dev/ptyXX";
     70 	register const char *cp1, *cp2;
     71 	register int master, slave, ttygid;
     72 	struct group *gr;
     73 
     74 	if ((gr = getgrnam("tty")) != NULL)
     75 		ttygid = gr->gr_gid;
     76 	else
     77 		ttygid = -1;
     78 
     79 	for (cp1 = TTY_LETTERS; *cp1; cp1++) {
     80 		line[8] = *cp1;
     81 		for (cp2 = "0123456789abcdef"; *cp2; cp2++) {
     82 			line[9] = *cp2;
     83 			if ((master = open(line, O_RDWR, 0)) == -1) {
     84 				if (errno == ENOENT)
     85 					return (-1);	/* out of ptys */
     86 			} else {
     87 				line[5] = 't';
     88 				(void) chown(line, getuid(), ttygid);
     89 				(void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
     90 				(void) revoke(line);
     91 				if ((slave = open(line, O_RDWR, 0)) != -1) {
     92 					*amaster = master;
     93 					*aslave = slave;
     94 					if (name)
     95 						strcpy(name, line);
     96 					if (termp)
     97 						(void) tcsetattr(slave,
     98 							TCSAFLUSH, termp);
     99 					if (winp)
    100 						(void) ioctl(slave, TIOCSWINSZ,
    101 							(char *)winp);
    102 					return (0);
    103 				}
    104 				(void) close(master);
    105 				line[5] = 'p';
    106 			}
    107 		}
    108 	}
    109 	errno = ENOENT;	/* out of ptys */
    110 	return (-1);
    111 }
    112 
    113 pid_t
    114 forkpty(amaster, name, termp, winp)
    115 	int *amaster;
    116 	char *name;
    117 	struct termios *termp;
    118 	struct winsize *winp;
    119 {
    120 	int master, slave;
    121 	pid_t pid;
    122 
    123 	if (openpty(&master, &slave, name, termp, winp) == -1)
    124 		return (-1);
    125 	switch (pid = fork()) {
    126 	case -1:
    127 		return (-1);
    128 	case 0:
    129 		/*
    130 		 * child
    131 		 */
    132 		(void) close(master);
    133 		login_tty(slave);
    134 		return (0);
    135 	}
    136 	/*
    137 	 * parent
    138 	 */
    139 	*amaster = master;
    140 	(void) close(slave);
    141 	return (pid);
    142 }
    143