1 /* $OpenBSD: pty.c,v 1.15 2006/03/30 20:44:19 deraadt Exp $ */ 2 3 /*- 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #include <sys/types.h> 34 #include <sys/stat.h> 35 #include <sys/ioctl.h> 36 #include <fcntl.h> 37 #include <termios.h> 38 #include <errno.h> 39 #include <unistd.h> 40 #include <stdio.h> 41 #include <string.h> 42 #include <grp.h> 43 #include <sys/tty.h> 44 45 #include "util.h" 46 47 #define TTY_LETTERS "pqrstuvwxyzPQRST" 48 #define TTY_SUFFIX "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 49 50 int 51 openpty(int *amaster, int *aslave, char *name, struct termios *termp, 52 struct winsize *winp) 53 { 54 char line[] = "/dev/ptyXX"; 55 const char *cp1, *cp2; 56 int master, slave, fd; 57 struct ptmget ptm; 58 struct group *gr; 59 gid_t ttygid; 60 61 /* 62 * Try to use /dev/ptm and the PTMGET ioctl to get a properly set up 63 * and owned pty/tty pair. If this fails, (because we might not have 64 * the ptm device, etc.) fall back to using the traditional method 65 * of walking through the pty entries in /dev for the moment, until 66 * there is less chance of people being seriously boned by running 67 * kernels without /dev/ptm in them. 68 */ 69 fd = open(PATH_PTMDEV, O_RDWR, 0); 70 if (fd == -1) 71 goto walkit; 72 if ((ioctl(fd, PTMGET, &ptm) == -1)) { 73 close(fd); 74 goto walkit; 75 } 76 close(fd); 77 master = ptm.cfd; 78 slave = ptm.sfd; 79 if (name) { 80 /* 81 * Manual page says "at least 16 characters". 82 */ 83 strlcpy(name, ptm.sn, 16); 84 } 85 *amaster = master; 86 *aslave = slave; 87 if (termp) 88 (void) tcsetattr(slave, TCSAFLUSH, termp); 89 if (winp) 90 (void) ioctl(slave, TIOCSWINSZ, winp); 91 return (0); 92 walkit: 93 if ((gr = getgrnam("tty")) != NULL) 94 ttygid = gr->gr_gid; 95 else 96 ttygid = (gid_t)-1; 97 98 for (cp1 = TTY_LETTERS; *cp1; cp1++) { 99 line[8] = *cp1; 100 for (cp2 = TTY_SUFFIX; *cp2; cp2++) { 101 line[9] = *cp2; 102 line[5] = 'p'; 103 if ((master = open(line, O_RDWR, 0)) == -1) { 104 if (errno == ENOENT) 105 return (-1); /* out of ptys */ 106 } else { 107 line[5] = 't'; 108 (void) chown(line, getuid(), ttygid); 109 (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP); 110 (void) revoke(line); 111 if ((slave = open(line, O_RDWR, 0)) != -1) { 112 *amaster = master; 113 *aslave = slave; 114 if (name) { 115 /* 116 * Manual page says "at least 117 * 16 characters". 118 */ 119 strlcpy(name, line, 16); 120 } 121 if (termp) 122 (void) tcsetattr(slave, 123 TCSAFLUSH, termp); 124 if (winp) 125 (void) ioctl(slave, TIOCSWINSZ, 126 winp); 127 return (0); 128 } 129 (void) close(master); 130 } 131 } 132 } 133 errno = ENOENT; /* out of ptys */ 134 return (-1); 135 } 136 137 pid_t 138 forkpty(amaster, name, termp, winp) 139 int *amaster; 140 char *name; 141 struct termios *termp; 142 struct winsize *winp; 143 { 144 int master, slave; 145 pid_t pid; 146 147 if (openpty(&master, &slave, name, termp, winp) == -1) 148 return (-1); 149 switch (pid = fork()) { 150 case -1: 151 return (-1); 152 case 0: 153 /* 154 * child 155 */ 156 (void) close(master); 157 login_tty(slave); 158 return (0); 159 } 160 /* 161 * parent 162 */ 163 *amaster = master; 164 (void) close(slave); 165 return (pid); 166 } 167