xref: /freebsd/lib/libutil/pty.c (revision 9336e0699bda8a301cd2bfa37106b6ec5e32012e)
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  * 4. 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 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char sccsid[] = "@(#)pty.c	8.3 (Berkeley) 5/16/94";
36 #endif
37 #endif /* LIBC_SCCS and not lint */
38 
39 #include <sys/types.h>
40 #include <sys/ioctl.h>
41 #include <sys/stat.h>
42 
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <grp.h>
46 #include <libutil.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <termios.h>
50 #include <unistd.h>
51 
52 int __use_pts(void);
53 
54 static int
55 new_openpty(int *amaster, int *aslave, char *name, struct termios *termp,
56     struct winsize *winp)
57 {
58 	const char *slavename;
59 	int master, slave;
60 
61 	master = posix_openpt(O_RDWR);
62 	if (master == -1)
63 		return (-1);
64 
65 	if (grantpt(master) == -1) {
66 		close(master);
67 		return (-1);
68 	}
69 
70 	slavename = ptsname(master);
71 	if (slavename == NULL) {
72 		close(master);
73 		return (-1);
74 	}
75 
76 	if (revoke(slavename) == -1) {
77 		close(master);
78 		return (-1);
79 	}
80 
81 	slave = open(slavename, O_RDWR);
82 	if (slave == -1) {
83 		close(master);
84 		return (-1);
85 	}
86 
87 	if (unlockpt(master) == -1) {
88 		close(master);
89 		close(slave);
90 		return (-1);
91 	}
92 
93 	*amaster = master;
94 	*aslave = slave;
95 
96 	if (name)
97 		strcpy(name, ptsname(master));
98 	if (termp)
99 		tcsetattr(slave, TCSAFLUSH, termp);
100 	if (winp)
101 		ioctl(slave, TIOCSWINSZ, (char *)winp);
102 
103 	return (0);
104 }
105 
106 int
107 openpty(int *amaster, int *aslave, char *name, struct termios *termp, struct winsize *winp)
108 {
109 	char line[] = "/dev/ptyXX";
110 	const char *cp1, *cp2;
111 	int master, slave, ttygid;
112 	struct group *gr;
113 
114 	if (__use_pts())
115 		return (new_openpty(amaster, aslave, name, termp, winp));
116 
117 	if ((gr = getgrnam("tty")) != NULL)
118 		ttygid = gr->gr_gid;
119 	else
120 		ttygid = -1;
121 
122 	for (cp1 = "pqrsPQRSlmnoLMNO"; *cp1; cp1++) {
123 		line[8] = *cp1;
124 		for (cp2 = "0123456789abcdefghijklmnopqrstuv"; *cp2; cp2++) {
125 			line[5] = 'p';
126 			line[9] = *cp2;
127 			if ((master = open(line, O_RDWR, 0)) == -1) {
128 				if (errno == ENOENT)
129 					break; /* try the next pty group */
130 			} else {
131 				line[5] = 't';
132 				(void) chown(line, getuid(), ttygid);
133 				(void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
134 				(void) revoke(line);
135 				if ((slave = open(line, O_RDWR, 0)) != -1) {
136 					*amaster = master;
137 					*aslave = slave;
138 					if (name)
139 						strcpy(name, line);
140 					if (termp)
141 						(void) tcsetattr(slave,
142 							TCSAFLUSH, termp);
143 					if (winp)
144 						(void) ioctl(slave, TIOCSWINSZ,
145 							(char *)winp);
146 					return (0);
147 				}
148 				(void) close(master);
149 			}
150 		}
151 	}
152 	errno = ENOENT;	/* out of ptys */
153 	return (-1);
154 }
155 
156 int
157 forkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp)
158 {
159 	int master, slave, pid;
160 
161 	if (openpty(&master, &slave, name, termp, winp) == -1)
162 		return (-1);
163 	switch (pid = fork()) {
164 	case -1:
165 		return (-1);
166 	case 0:
167 		/*
168 		 * child
169 		 */
170 		(void) close(master);
171 		login_tty(slave);
172 		return (0);
173 	}
174 	/*
175 	 * parent
176 	 */
177 	*amaster = master;
178 	(void) close(slave);
179 	return (pid);
180 }
181