1 /* $OpenBSD: pty.c,v 1.12 2004/04/11 18:04:36 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.12 2004/04/11 18:04:36 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 #include <sys/tty.h> 48 49 #include "util.h" 50 51 #define TTY_LETTERS "pqrstuvwxyzPQRST" 52 #define TTY_SUFFIX "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 53 54 int 55 openpty(amaster, aslave, name, termp, winp) 56 int *amaster, *aslave; 57 char *name; 58 struct termios *termp; 59 struct winsize *winp; 60 { 61 char line[] = "/dev/ptyXX"; 62 const char *cp1, *cp2; 63 int master, slave, ttygid; 64 struct group *gr; 65 struct ptmget ptm; 66 int fd; 67 68 /* Try to use /dev/ptm and the PTMGET ioctl to get a properly set up 69 * and owned pty/tty pair. If this fails, (because we might not have 70 * the ptm device, etc.) fall back to using the traditional method 71 * of walking through the pty entries in /dev for the moment, until 72 * there is less chance of people being seriously boned by running 73 * kernels without /dev/ptm in them. 74 */ 75 76 fd = open(PATH_PTMDEV, O_RDWR, 0); 77 if (fd == -1) 78 goto walkit; 79 if ((ioctl(fd, PTMGET, &ptm) == -1)) { 80 close(fd); 81 goto walkit; 82 } 83 close(fd); 84 master = ptm.cfd; 85 slave = ptm.sfd; 86 if (name) { 87 /* 88 * Manual page says "at least 16 characters". 89 */ 90 strlcpy(name, ptm.sn, 16); 91 } 92 *amaster = master; 93 *aslave = slave; 94 if (termp) 95 (void) tcsetattr(slave, TCSAFLUSH, termp); 96 if (winp) 97 (void) ioctl(slave, TIOCSWINSZ, (char *)winp); 98 return (0); 99 walkit: 100 if ((gr = getgrnam("tty")) != NULL) 101 ttygid = gr->gr_gid; 102 else 103 ttygid = -1; 104 105 for (cp1 = TTY_LETTERS; *cp1; cp1++) { 106 line[8] = *cp1; 107 for (cp2 = TTY_SUFFIX; *cp2; cp2++) { 108 line[9] = *cp2; 109 line[5] = 'p'; 110 if ((master = open(line, O_RDWR, 0)) == -1) { 111 if (errno == ENOENT) 112 return (-1); /* out of ptys */ 113 } else { 114 line[5] = 't'; 115 (void) chown(line, getuid(), ttygid); 116 (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP); 117 (void) revoke(line); 118 if ((slave = open(line, O_RDWR, 0)) != -1) { 119 *amaster = master; 120 *aslave = slave; 121 if (name) { 122 /* 123 * Manual page says "at least 124 * 16 characters". 125 */ 126 strlcpy(name, line, 16); 127 } 128 if (termp) 129 (void) tcsetattr(slave, 130 TCSAFLUSH, termp); 131 if (winp) 132 (void) ioctl(slave, TIOCSWINSZ, 133 (char *)winp); 134 return (0); 135 } 136 (void) close(master); 137 } 138 } 139 } 140 errno = ENOENT; /* out of ptys */ 141 return (-1); 142 } 143 144 pid_t 145 forkpty(amaster, name, termp, winp) 146 int *amaster; 147 char *name; 148 struct termios *termp; 149 struct winsize *winp; 150 { 151 int master, slave; 152 pid_t pid; 153 154 if (openpty(&master, &slave, name, termp, winp) == -1) 155 return (-1); 156 switch (pid = fork()) { 157 case -1: 158 return (-1); 159 case 0: 160 /* 161 * child 162 */ 163 (void) close(master); 164 login_tty(slave); 165 return (0); 166 } 167 /* 168 * parent 169 */ 170 *amaster = master; 171 (void) close(slave); 172 return (pid); 173 } 174