1 /*- 2 * Copyright (c) 1990, 1993, 1994 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. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)pty.c 8.3 (Berkeley) 5/16/94 30 * $FreeBSD: src/lib/libutil/pty.c,v 1.10 1999/08/28 00:05:51 peter Exp $ 31 */ 32 33 #include <sys/types.h> 34 #include <sys/ioctl.h> 35 #include <sys/stat.h> 36 37 #include <errno.h> 38 #include <fcntl.h> 39 #include <grp.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <termios.h> 43 #include <unistd.h> 44 45 #include "libutil.h" 46 47 int 48 openpty(int *amaster, int *aslave, char *name, struct termios *termp, 49 struct winsize *winp) 50 { 51 char line[] = "/dev/ptyXX"; 52 const char *cp1, *cp2; 53 int master, slave, ttygid; 54 struct group *gr; 55 const char *slave_name; 56 57 if ((gr = getgrnam("tty")) != NULL) 58 ttygid = gr->gr_gid; 59 else 60 ttygid = -1; 61 62 master = posix_openpt(O_RDWR|O_NOCTTY); 63 if (master == -1) 64 goto fallback; 65 66 if (grantpt(master) != 0) { 67 close(master); 68 goto fallback; 69 } 70 71 if (unlockpt(master) != 0) { 72 close(master); 73 goto fallback; 74 } 75 76 slave_name = ptsname(master); 77 if (slave_name == NULL) { 78 close(master); 79 goto fallback; 80 } 81 82 slave = open(slave_name, O_RDWR); 83 if (slave == -1) { 84 close(master); 85 goto fallback; 86 } 87 88 *amaster = master; 89 *aslave = slave; 90 91 if (name) 92 strcpy(name, slave_name); 93 if (termp) 94 tcsetattr(slave, TCSAFLUSH, termp); 95 if (winp) 96 ioctl(slave, TIOCSWINSZ, (char *)winp); 97 98 return (0); 99 100 fallback: 101 for (cp1 = "pqrsPQRS"; *cp1; cp1++) { 102 line[8] = *cp1; 103 for (cp2 = "0123456789abcdefghijklmnopqrstuv"; *cp2; cp2++) { 104 line[5] = 'p'; 105 line[9] = *cp2; 106 if ((master = open(line, O_RDWR, 0)) == -1) { 107 if (errno == ENOENT) 108 return (-1); /* out of ptys */ 109 } else { 110 line[5] = 't'; 111 (void) chown(line, getuid(), ttygid); 112 (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP); 113 (void) revoke(line); 114 if ((slave = open(line, O_RDWR, 0)) != -1) { 115 *amaster = master; 116 *aslave = slave; 117 if (name) 118 strcpy(name, line); 119 if (termp) 120 (void) tcsetattr(slave, 121 TCSAFLUSH, termp); 122 if (winp) 123 (void) ioctl(slave, TIOCSWINSZ, 124 (char *)winp); 125 return (0); 126 } 127 (void) close(master); 128 } 129 } 130 } 131 errno = ENOENT; /* out of ptys */ 132 return (-1); 133 } 134 135 int 136 forkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp) 137 { 138 int master, slave, pid; 139 140 if (openpty(&master, &slave, name, termp, winp) == -1) 141 return (-1); 142 switch (pid = fork()) { 143 case -1: 144 return (-1); 145 case 0: 146 /* 147 * child 148 */ 149 (void) close(master); 150 login_tty(slave); 151 return (0); 152 } 153 /* 154 * parent 155 */ 156 *amaster = master; 157 (void) close(slave); 158 return (pid); 159 } 160