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