xref: /minix3/lib/libutil/pty.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
10c3983b2SBen Gras /*	$NetBSD: pty.c,v 1.31 2009/02/20 16:44:06 christos Exp $	*/
20c3983b2SBen Gras 
30c3983b2SBen Gras /*-
40c3983b2SBen Gras  * Copyright (c) 1990, 1993, 1994
50c3983b2SBen Gras  *	The Regents of the University of California.  All rights reserved.
60c3983b2SBen Gras  *
70c3983b2SBen Gras  * Redistribution and use in source and binary forms, with or without
80c3983b2SBen Gras  * modification, are permitted provided that the following conditions
90c3983b2SBen Gras  * are met:
100c3983b2SBen Gras  * 1. Redistributions of source code must retain the above copyright
110c3983b2SBen Gras  *    notice, this list of conditions and the following disclaimer.
120c3983b2SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
130c3983b2SBen Gras  *    notice, this list of conditions and the following disclaimer in the
140c3983b2SBen Gras  *    documentation and/or other materials provided with the distribution.
150c3983b2SBen Gras  * 3. Neither the name of the University nor the names of its contributors
160c3983b2SBen Gras  *    may be used to endorse or promote products derived from this software
170c3983b2SBen Gras  *    without specific prior written permission.
180c3983b2SBen Gras  *
190c3983b2SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
200c3983b2SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
210c3983b2SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
220c3983b2SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
230c3983b2SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
240c3983b2SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
250c3983b2SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
260c3983b2SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
270c3983b2SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
280c3983b2SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
290c3983b2SBen Gras  * SUCH DAMAGE.
300c3983b2SBen Gras  */
310c3983b2SBen Gras 
320c3983b2SBen Gras #include <sys/cdefs.h>
330c3983b2SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
340c3983b2SBen Gras #if 0
350c3983b2SBen Gras static char sccsid[] = "@(#)pty.c	8.3 (Berkeley) 5/16/94";
360c3983b2SBen Gras #else
370c3983b2SBen Gras __RCSID("$NetBSD: pty.c,v 1.31 2009/02/20 16:44:06 christos Exp $");
380c3983b2SBen Gras #endif
390c3983b2SBen Gras #endif /* LIBC_SCCS and not lint */
400c3983b2SBen Gras 
410c3983b2SBen Gras #include <sys/types.h>
420c3983b2SBen Gras #include <sys/ioctl.h>
430c3983b2SBen Gras #include <sys/stat.h>
440c3983b2SBen Gras 
45*0a6a1f1dSLionel Sambuc #if defined(__minix)
46da21d850SDavid van Moolenbroek #include <stdlib.h>
47*0a6a1f1dSLionel Sambuc #endif /* defined(__minix) */
480c3983b2SBen Gras #include <assert.h>
490c3983b2SBen Gras #include <errno.h>
500c3983b2SBen Gras #include <fcntl.h>
510c3983b2SBen Gras #include <grp.h>
520c3983b2SBen Gras #include <stdio.h>
530c3983b2SBen Gras #include <string.h>
540c3983b2SBen Gras #include <termios.h>
550c3983b2SBen Gras #include <unistd.h>
560c3983b2SBen Gras #include <util.h>
570c3983b2SBen Gras 
580c3983b2SBen Gras /*
590c3983b2SBen Gras  * XXX: `v' removed until no ports are using console devices which use ttyv0
600c3983b2SBen Gras  */
610c3983b2SBen Gras #define TTY_LETTERS	"pqrstuwxyzPQRST"
620c3983b2SBen Gras #define TTY_OLD_SUFFIX	"0123456789abcdef"
630c3983b2SBen Gras #define TTY_NEW_SUFFIX	"ghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
640c3983b2SBen Gras 
650c3983b2SBen Gras int
openpty(int * amaster,int * aslave,char * name,struct termios * term,struct winsize * winp)660c3983b2SBen Gras openpty(int *amaster, int *aslave, char *name, struct termios *term,
670c3983b2SBen Gras 	struct winsize *winp)
680c3983b2SBen Gras {
690c3983b2SBen Gras 	char line[] = "/dev/XtyXX";
700c3983b2SBen Gras 	const char *cp1, *cp2, *cp, *linep;
710c3983b2SBen Gras 	int master, slave;
720c3983b2SBen Gras 	gid_t ttygid;
730c3983b2SBen Gras 	mode_t mode;
740c3983b2SBen Gras 	struct group grs, *grp;
750c3983b2SBen Gras 	char grbuf[1024];
760c3983b2SBen Gras 
770c3983b2SBen Gras 	_DIAGASSERT(amaster != NULL);
780c3983b2SBen Gras 	_DIAGASSERT(aslave != NULL);
790c3983b2SBen Gras 	/* name may be NULL */
800c3983b2SBen Gras 	/* term may be NULL */
810c3983b2SBen Gras 	/* winp may be NULL */
820c3983b2SBen Gras 
8384d9c625SLionel Sambuc #if !defined(__minix)
840c3983b2SBen Gras 	if ((master = open("/dev/ptm", O_RDWR)) != -1) {
850c3983b2SBen Gras 		struct ptmget pt;
860c3983b2SBen Gras 		if (ioctl(master, TIOCPTMGET, &pt) != -1) {
870c3983b2SBen Gras 			(void)close(master);
880c3983b2SBen Gras 			master = pt.cfd;
890c3983b2SBen Gras 			slave = pt.sfd;
900c3983b2SBen Gras 			linep = pt.sn;
910c3983b2SBen Gras 			goto gotit;
920c3983b2SBen Gras 		}
930c3983b2SBen Gras 		(void)close(master);
940c3983b2SBen Gras 	}
95*0a6a1f1dSLionel Sambuc #else
96da21d850SDavid van Moolenbroek 	/*
97da21d850SDavid van Moolenbroek 	 * On MINIX3, we implement non-root openpty(3) using Unix98 PTYs.
98da21d850SDavid van Moolenbroek 	 * If this fails, the fallback code below works for root only.
99da21d850SDavid van Moolenbroek 	 */
100da21d850SDavid van Moolenbroek 	if ((master = posix_openpt(O_RDWR | O_NOCTTY)) != -1) {
101da21d850SDavid van Moolenbroek 		if (grantpt(master) != -1 && unlockpt(master) != -1 &&
102da21d850SDavid van Moolenbroek 		    (linep = ptsname(master)) != NULL &&
103da21d850SDavid van Moolenbroek 		    (slave = open(linep, O_RDWR | O_NOCTTY)) != -1)
104da21d850SDavid van Moolenbroek 			goto gotit;
105da21d850SDavid van Moolenbroek 		(void)close(master);
106da21d850SDavid van Moolenbroek 	}
107*0a6a1f1dSLionel Sambuc #endif /* !defined(__minix) */
1080c3983b2SBen Gras 
1090c3983b2SBen Gras 	(void)getgrnam_r("tty", &grs, grbuf, sizeof(grbuf), &grp);
1100c3983b2SBen Gras 	if (grp != NULL) {
1110c3983b2SBen Gras 		ttygid = grp->gr_gid;
1120c3983b2SBen Gras 		mode = S_IRUSR|S_IWUSR|S_IWGRP;
1130c3983b2SBen Gras 	} else {
1140c3983b2SBen Gras 		ttygid = getgid();
1150c3983b2SBen Gras 		mode = S_IRUSR|S_IWUSR;
1160c3983b2SBen Gras 	}
1170c3983b2SBen Gras 
1180c3983b2SBen Gras 	for (cp1 = TTY_LETTERS; *cp1; cp1++) {
1190c3983b2SBen Gras 		line[8] = *cp1;
1200c3983b2SBen Gras 		for (cp = cp2 = TTY_OLD_SUFFIX TTY_NEW_SUFFIX; *cp2; cp2++) {
1210c3983b2SBen Gras 			line[5] = 'p';
1220c3983b2SBen Gras 			line[9] = *cp2;
123*0a6a1f1dSLionel Sambuc #if defined(__minix)
12459ba14bbSBen Gras 			if ((master = open(line, O_RDWR | O_NOCTTY, 0)) == -1) {
12584d9c625SLionel Sambuc #else
12684d9c625SLionel Sambuc 			if ((master = open(line, O_RDWR, 0)) == -1) {
127*0a6a1f1dSLionel Sambuc #endif /* defined(__minix) */
1280c3983b2SBen Gras 				if (errno != ENOENT)
1290c3983b2SBen Gras 					continue;	/* busy */
1300c3983b2SBen Gras 				if ((size_t)(cp2 - cp + 1) < sizeof(TTY_OLD_SUFFIX))
1310c3983b2SBen Gras 					return -1; /* out of ptys */
1320c3983b2SBen Gras 				else
1330c3983b2SBen Gras 					break;	/* out of ptys in this group */
1340c3983b2SBen Gras 			}
1350c3983b2SBen Gras 			line[5] = 't';
1360c3983b2SBen Gras 			linep = line;
1370c3983b2SBen Gras 			if (chown(line, getuid(), ttygid) == 0 &&
1380c3983b2SBen Gras 			    chmod(line, mode) == 0 &&
13984d9c625SLionel Sambuc #if !defined(__minix)
1400c3983b2SBen Gras 			    revoke(line) == 0 &&
14184d9c625SLionel Sambuc #endif /* !defined(__minix) */
142*0a6a1f1dSLionel Sambuc #if defined(__minix)
14359ba14bbSBen Gras 			(slave = open(line, O_RDWR | O_NOCTTY, 0)) != -1) {
14484d9c625SLionel Sambuc #else
14584d9c625SLionel Sambuc 			    (slave = open(line, O_RDWR, 0)) != -1) {
146*0a6a1f1dSLionel Sambuc #endif /* defined(__minix) */
14784d9c625SLionel Sambuc gotit:
1480c3983b2SBen Gras 				*amaster = master;
1490c3983b2SBen Gras 				*aslave = slave;
1500c3983b2SBen Gras 				if (name)
1510c3983b2SBen Gras 					(void)strcpy(name, linep);
1520c3983b2SBen Gras 				if (term)
1530c3983b2SBen Gras 					(void)tcsetattr(slave, TCSAFLUSH, term);
1540c3983b2SBen Gras 				if (winp)
1550c3983b2SBen Gras 					(void)ioctl(slave, TIOCSWINSZ, winp);
1560c3983b2SBen Gras 				return 0;
1570c3983b2SBen Gras 			}
1580c3983b2SBen Gras 			(void)close(master);
1590c3983b2SBen Gras 		}
1600c3983b2SBen Gras 	}
1610c3983b2SBen Gras 	errno = ENOENT;	/* out of ptys */
1620c3983b2SBen Gras 	return -1;
1630c3983b2SBen Gras }
1640c3983b2SBen Gras 
1650c3983b2SBen Gras pid_t
1660c3983b2SBen Gras forkpty(int *amaster, char *name, struct termios *term, struct winsize *winp)
1670c3983b2SBen Gras {
1680c3983b2SBen Gras 	int master, slave;
1690c3983b2SBen Gras 	pid_t pid;
1700c3983b2SBen Gras 
1710c3983b2SBen Gras 	_DIAGASSERT(amaster != NULL);
1720c3983b2SBen Gras 	/* name may be NULL */
1730c3983b2SBen Gras 	/* term may be NULL */
1740c3983b2SBen Gras 	/* winp may be NULL */
1750c3983b2SBen Gras 
1760c3983b2SBen Gras 	if (openpty(&master, &slave, name, term, winp) == -1)
1770c3983b2SBen Gras 		return -1;
1780c3983b2SBen Gras 	switch (pid = fork()) {
1790c3983b2SBen Gras 	case -1:
1800c3983b2SBen Gras 		return -1;
1810c3983b2SBen Gras 	case 0:
1820c3983b2SBen Gras 		/*
1830c3983b2SBen Gras 		 * child
1840c3983b2SBen Gras 		 */
1850c3983b2SBen Gras 		(void)close(master);
1860c3983b2SBen Gras 		login_tty(slave);
1870c3983b2SBen Gras 		return 0;
1880c3983b2SBen Gras 	}
1890c3983b2SBen Gras 	/*
1900c3983b2SBen Gras 	 * parent
1910c3983b2SBen Gras 	 */
1920c3983b2SBen Gras 	*amaster = master;
1930c3983b2SBen Gras 	(void)close(slave);
1940c3983b2SBen Gras 	return pid;
1950c3983b2SBen Gras }
196