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