xref: /freebsd/lib/libutil/pty.c (revision db256336b89f29cb60b18c0e551493c847ca758d)
158f0484fSRodney W. Grimes /*-
21b88e35bSBruce Evans  * Copyright (c) 1990, 1993, 1994
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
658f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
758f0484fSRodney W. Grimes  * are met:
858f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
958f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1058f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1258f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1358f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1458f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1558f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1658f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
1758f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
1858f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1958f0484fSRodney W. Grimes  *    without specific prior written permission.
2058f0484fSRodney W. Grimes  *
2158f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2258f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2358f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2458f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2558f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2658f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2758f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2858f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2958f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3058f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3158f0484fSRodney W. Grimes  * SUCH DAMAGE.
3258f0484fSRodney W. Grimes  */
3358f0484fSRodney W. Grimes 
348719c58fSMatthew Dillon #include <sys/cdefs.h>
358719c58fSMatthew Dillon __FBSDID("$FreeBSD$");
368719c58fSMatthew Dillon 
3758f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
38673b7946SSteve Price #if 0
391b88e35bSBruce Evans static char sccsid[] = "@(#)pty.c	8.3 (Berkeley) 5/16/94";
40673b7946SSteve Price #endif
4158f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
4258f0484fSRodney W. Grimes 
4358f0484fSRodney W. Grimes #include <sys/types.h>
4458f0484fSRodney W. Grimes #include <sys/ioctl.h>
451b88e35bSBruce Evans #include <sys/stat.h>
461b88e35bSBruce Evans 
4758f0484fSRodney W. Grimes #include <errno.h>
481b88e35bSBruce Evans #include <fcntl.h>
4958f0484fSRodney W. Grimes #include <grp.h>
500ebec5d3SMark Murray #include <libutil.h>
511b88e35bSBruce Evans #include <stdlib.h>
521b88e35bSBruce Evans #include <string.h>
531b88e35bSBruce Evans #include <termios.h>
541b88e35bSBruce Evans #include <unistd.h>
5558f0484fSRodney W. Grimes 
56db256336SOlivier Houchard int __use_pts(void);
57db256336SOlivier Houchard 
58db256336SOlivier Houchard static int
59db256336SOlivier Houchard new_openpty(int *amaster, int *aslave, char *name, struct termios *termp,
60db256336SOlivier Houchard     struct winsize *winp)
61db256336SOlivier Houchard {
62db256336SOlivier Houchard 	int master, slave;
63db256336SOlivier Houchard 
64db256336SOlivier Houchard 	master = posix_openpt(O_RDWR);
65db256336SOlivier Houchard 	if (master == -1)
66db256336SOlivier Houchard 		return (-1);
67db256336SOlivier Houchard 
68db256336SOlivier Houchard 	if (grantpt(master) == -1) {
69db256336SOlivier Houchard 		close(master);
70db256336SOlivier Houchard 		return (-1);
71db256336SOlivier Houchard 	}
72db256336SOlivier Houchard 
73db256336SOlivier Houchard 	slave = open(ptsname(master), O_RDWR);
74db256336SOlivier Houchard 	if (slave == -1) {
75db256336SOlivier Houchard 		close(master);
76db256336SOlivier Houchard 		return (-1);
77db256336SOlivier Houchard 	}
78db256336SOlivier Houchard 
79db256336SOlivier Houchard 	if (unlockpt(master) == -1) {
80db256336SOlivier Houchard 		close(master);
81db256336SOlivier Houchard 		close(slave);
82db256336SOlivier Houchard 		return (-1);
83db256336SOlivier Houchard 	}
84db256336SOlivier Houchard 
85db256336SOlivier Houchard 	*amaster = master;
86db256336SOlivier Houchard 	*aslave = slave;
87db256336SOlivier Houchard 
88db256336SOlivier Houchard 	if (name)
89db256336SOlivier Houchard 		strcpy(name, ptsname(master));
90db256336SOlivier Houchard 	if (termp)
91db256336SOlivier Houchard 		tcsetattr(slave, TCSAFLUSH, termp);
92db256336SOlivier Houchard 	if (winp)
93db256336SOlivier Houchard 		ioctl(slave, TIOCSWINSZ, (char *)winp);
94db256336SOlivier Houchard 
95db256336SOlivier Houchard 	return (0);
96db256336SOlivier Houchard }
97db256336SOlivier Houchard 
983b7e1cc8SPeter Wemm int
99547fa0d9SMark Murray openpty(int *amaster, int *aslave, char *name, struct termios *termp, struct winsize *winp)
10058f0484fSRodney W. Grimes {
1015095f191SJohn Birrell 	char line[] = "/dev/ptyXX";
102be04b6d1SDavid E. O'Brien 	const char *cp1, *cp2;
103be04b6d1SDavid E. O'Brien 	int master, slave, ttygid;
10458f0484fSRodney W. Grimes 	struct group *gr;
10558f0484fSRodney W. Grimes 
106db256336SOlivier Houchard 	if (__use_pts())
107db256336SOlivier Houchard 		return (new_openpty(amaster, aslave, name, termp, winp));
108db256336SOlivier Houchard 
10958f0484fSRodney W. Grimes 	if ((gr = getgrnam("tty")) != NULL)
11058f0484fSRodney W. Grimes 		ttygid = gr->gr_gid;
11158f0484fSRodney W. Grimes 	else
11258f0484fSRodney W. Grimes 		ttygid = -1;
11358f0484fSRodney W. Grimes 
1141338e009SJordan K. Hubbard 	for (cp1 = "pqrsPQRS"; *cp1; cp1++) {
11558f0484fSRodney W. Grimes 		line[8] = *cp1;
1161338e009SJordan K. Hubbard 		for (cp2 = "0123456789abcdefghijklmnopqrstuv"; *cp2; cp2++) {
1171b88e35bSBruce Evans 			line[5] = 'p';
11858f0484fSRodney W. Grimes 			line[9] = *cp2;
11958f0484fSRodney W. Grimes 			if ((master = open(line, O_RDWR, 0)) == -1) {
12058f0484fSRodney W. Grimes 				if (errno == ENOENT)
121f6f1b6b6SBill Fumerola 					break; /* try the next pty group */
12258f0484fSRodney W. Grimes 			} else {
12358f0484fSRodney W. Grimes 				line[5] = 't';
12458f0484fSRodney W. Grimes 				(void) chown(line, getuid(), ttygid);
12558f0484fSRodney W. Grimes 				(void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
12658f0484fSRodney W. Grimes 				(void) revoke(line);
12758f0484fSRodney W. Grimes 				if ((slave = open(line, O_RDWR, 0)) != -1) {
12858f0484fSRodney W. Grimes 					*amaster = master;
12958f0484fSRodney W. Grimes 					*aslave = slave;
13058f0484fSRodney W. Grimes 					if (name)
13158f0484fSRodney W. Grimes 						strcpy(name, line);
13258f0484fSRodney W. Grimes 					if (termp)
13358f0484fSRodney W. Grimes 						(void) tcsetattr(slave,
13458f0484fSRodney W. Grimes 							TCSAFLUSH, termp);
13558f0484fSRodney W. Grimes 					if (winp)
13658f0484fSRodney W. Grimes 						(void) ioctl(slave, TIOCSWINSZ,
13758f0484fSRodney W. Grimes 							(char *)winp);
13858f0484fSRodney W. Grimes 					return (0);
13958f0484fSRodney W. Grimes 				}
14058f0484fSRodney W. Grimes 				(void) close(master);
14158f0484fSRodney W. Grimes 			}
14258f0484fSRodney W. Grimes 		}
14358f0484fSRodney W. Grimes 	}
14458f0484fSRodney W. Grimes 	errno = ENOENT;	/* out of ptys */
14558f0484fSRodney W. Grimes 	return (-1);
14658f0484fSRodney W. Grimes }
14758f0484fSRodney W. Grimes 
14851295a4dSJordan K. Hubbard int
149547fa0d9SMark Murray forkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp)
15058f0484fSRodney W. Grimes {
15158f0484fSRodney W. Grimes 	int master, slave, pid;
15258f0484fSRodney W. Grimes 
15358f0484fSRodney W. Grimes 	if (openpty(&master, &slave, name, termp, winp) == -1)
15458f0484fSRodney W. Grimes 		return (-1);
15558f0484fSRodney W. Grimes 	switch (pid = fork()) {
15658f0484fSRodney W. Grimes 	case -1:
15758f0484fSRodney W. Grimes 		return (-1);
15858f0484fSRodney W. Grimes 	case 0:
15958f0484fSRodney W. Grimes 		/*
16058f0484fSRodney W. Grimes 		 * child
16158f0484fSRodney W. Grimes 		 */
16258f0484fSRodney W. Grimes 		(void) close(master);
16358f0484fSRodney W. Grimes 		login_tty(slave);
16458f0484fSRodney W. Grimes 		return (0);
16558f0484fSRodney W. Grimes 	}
16658f0484fSRodney W. Grimes 	/*
16758f0484fSRodney W. Grimes 	 * parent
16858f0484fSRodney W. Grimes 	 */
16958f0484fSRodney W. Grimes 	*amaster = master;
17058f0484fSRodney W. Grimes 	(void) close(slave);
17158f0484fSRodney W. Grimes 	return (pid);
17258f0484fSRodney W. Grimes }
173