xref: /openbsd-src/lib/libutil/pty.c (revision a4afd6dad3fba28f80e70208181c06c482259988)
1 /*	$OpenBSD: pty.c,v 1.5 1996/08/09 00:26:15 deraadt 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. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #if defined(LIBC_SCCS) && !defined(lint)
36 /* from: static char sccsid[] = "@(#)pty.c	8.1 (Berkeley) 6/4/93"; */
37 static char *rcsid = "$Id: pty.c,v 1.5 1996/08/09 00:26:15 deraadt Exp $";
38 #endif /* LIBC_SCCS and not lint */
39 
40 #include <sys/cdefs.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/ioctl.h>
44 #include <fcntl.h>
45 #include <termios.h>
46 #include <errno.h>
47 #include <unistd.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <grp.h>
51 
52 #include "util.h"
53 
54 #define TTY_LETTERS "pqrstuvwxyzPQRST"
55 
56 int
57 openpty(amaster, aslave, name, termp, winp)
58 	int *amaster, *aslave;
59 	char *name;
60 	struct termios *termp;
61 	struct winsize *winp;
62 {
63 	static char line[] = "/dev/ptyXX";
64 	register const char *cp1, *cp2;
65 	register int master, slave, ttygid;
66 	struct group *gr;
67 
68 	if ((gr = getgrnam("tty")) != NULL)
69 		ttygid = gr->gr_gid;
70 	else
71 		ttygid = -1;
72 
73 	for (cp1 = TTY_LETTERS; *cp1; cp1++) {
74 		line[8] = *cp1;
75 		for (cp2 = "0123456789abcdef"; *cp2; cp2++) {
76 			line[9] = *cp2;
77 			line[5] = 'p';
78 			if ((master = open(line, O_RDWR, 0)) == -1) {
79 				if (errno == ENOENT)
80 					return (-1);	/* out of ptys */
81 			} else {
82 				line[5] = 't';
83 				(void) chown(line, getuid(), ttygid);
84 				(void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
85 				(void) revoke(line);
86 				if ((slave = open(line, O_RDWR, 0)) != -1) {
87 					*amaster = master;
88 					*aslave = slave;
89 					if (name)
90 						strcpy(name, line);
91 					if (termp)
92 						(void) tcsetattr(slave,
93 							TCSAFLUSH, termp);
94 					if (winp)
95 						(void) ioctl(slave, TIOCSWINSZ,
96 							(char *)winp);
97 					return (0);
98 				}
99 				(void) close(master);
100 			}
101 		}
102 	}
103 	errno = ENOENT;	/* out of ptys */
104 	return (-1);
105 }
106 
107 pid_t
108 forkpty(amaster, name, termp, winp)
109 	int *amaster;
110 	char *name;
111 	struct termios *termp;
112 	struct winsize *winp;
113 {
114 	int master, slave;
115 	pid_t pid;
116 
117 	if (openpty(&master, &slave, name, termp, winp) == -1)
118 		return (-1);
119 	switch (pid = fork()) {
120 	case -1:
121 		return (-1);
122 	case 0:
123 		/*
124 		 * child
125 		 */
126 		(void) close(master);
127 		login_tty(slave);
128 		return (0);
129 	}
130 	/*
131 	 * parent
132 	 */
133 	*amaster = master;
134 	(void) close(slave);
135 	return (pid);
136 }
137