xref: /openbsd-src/lib/libutil/pty.c (revision 47911bd667ac77dc523b8a13ef40b012dbffa741)
1 /*	$OpenBSD: pty.c,v 1.9 2002/06/09 22:18:43 fgsch 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 const char rcsid[] = "$Id: pty.c,v 1.9 2002/06/09 22:18:43 fgsch 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 	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 						/*
91 						 * Manual page says "at least
92 						 * 16 characters".
93 						 */
94 						strlcpy(name, line, 16);
95 					}
96 					if (termp)
97 						(void) tcsetattr(slave,
98 						    TCSAFLUSH, termp);
99 					if (winp)
100 						(void) ioctl(slave, TIOCSWINSZ,
101 						    (char *)winp);
102 					return (0);
103 				}
104 				(void) close(master);
105 			}
106 		}
107 	}
108 	errno = ENOENT;	/* out of ptys */
109 	return (-1);
110 }
111 
112 pid_t
113 forkpty(amaster, name, termp, winp)
114 	int *amaster;
115 	char *name;
116 	struct termios *termp;
117 	struct winsize *winp;
118 {
119 	int master, slave;
120 	pid_t pid;
121 
122 	if (openpty(&master, &slave, name, termp, winp) == -1)
123 		return (-1);
124 	switch (pid = fork()) {
125 	case -1:
126 		return (-1);
127 	case 0:
128 		/*
129 		 * child
130 		 */
131 		(void) close(master);
132 		login_tty(slave);
133 		return (0);
134 	}
135 	/*
136 	 * parent
137 	 */
138 	*amaster = master;
139 	(void) close(slave);
140 	return (pid);
141 }
142