xref: /freebsd/lib/libutil/pty.c (revision 0ebec5d3c836a1a8ba2d3b4b06c57c5ecb104485)
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 
563b7e1cc8SPeter Wemm int
5758f0484fSRodney W. Grimes openpty(amaster, aslave, name, termp, winp)
5858f0484fSRodney W. Grimes 	int *amaster, *aslave;
5958f0484fSRodney W. Grimes 	char *name;
6058f0484fSRodney W. Grimes 	struct termios *termp;
6158f0484fSRodney W. Grimes 	struct winsize *winp;
6258f0484fSRodney W. Grimes {
635095f191SJohn Birrell 	char line[] = "/dev/ptyXX";
64be04b6d1SDavid E. O'Brien 	const char *cp1, *cp2;
65be04b6d1SDavid E. O'Brien 	int master, slave, ttygid;
6658f0484fSRodney W. Grimes 	struct group *gr;
6758f0484fSRodney W. Grimes 
6858f0484fSRodney W. Grimes 	if ((gr = getgrnam("tty")) != NULL)
6958f0484fSRodney W. Grimes 		ttygid = gr->gr_gid;
7058f0484fSRodney W. Grimes 	else
7158f0484fSRodney W. Grimes 		ttygid = -1;
7258f0484fSRodney W. Grimes 
731338e009SJordan K. Hubbard 	for (cp1 = "pqrsPQRS"; *cp1; cp1++) {
7458f0484fSRodney W. Grimes 		line[8] = *cp1;
751338e009SJordan K. Hubbard 		for (cp2 = "0123456789abcdefghijklmnopqrstuv"; *cp2; cp2++) {
761b88e35bSBruce Evans 			line[5] = 'p';
7758f0484fSRodney W. Grimes 			line[9] = *cp2;
7858f0484fSRodney W. Grimes 			if ((master = open(line, O_RDWR, 0)) == -1) {
7958f0484fSRodney W. Grimes 				if (errno == ENOENT)
80f6f1b6b6SBill Fumerola 					break; /* try the next pty group */
8158f0484fSRodney W. Grimes 			} else {
8258f0484fSRodney W. Grimes 				line[5] = 't';
8358f0484fSRodney W. Grimes 				(void) chown(line, getuid(), ttygid);
8458f0484fSRodney W. Grimes 				(void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
8558f0484fSRodney W. Grimes 				(void) revoke(line);
8658f0484fSRodney W. Grimes 				if ((slave = open(line, O_RDWR, 0)) != -1) {
8758f0484fSRodney W. Grimes 					*amaster = master;
8858f0484fSRodney W. Grimes 					*aslave = slave;
8958f0484fSRodney W. Grimes 					if (name)
9058f0484fSRodney W. Grimes 						strcpy(name, line);
9158f0484fSRodney W. Grimes 					if (termp)
9258f0484fSRodney W. Grimes 						(void) tcsetattr(slave,
9358f0484fSRodney W. Grimes 							TCSAFLUSH, termp);
9458f0484fSRodney W. Grimes 					if (winp)
9558f0484fSRodney W. Grimes 						(void) ioctl(slave, TIOCSWINSZ,
9658f0484fSRodney W. Grimes 							(char *)winp);
9758f0484fSRodney W. Grimes 					return (0);
9858f0484fSRodney W. Grimes 				}
9958f0484fSRodney W. Grimes 				(void) close(master);
10058f0484fSRodney W. Grimes 			}
10158f0484fSRodney W. Grimes 		}
10258f0484fSRodney W. Grimes 	}
10358f0484fSRodney W. Grimes 	errno = ENOENT;	/* out of ptys */
10458f0484fSRodney W. Grimes 	return (-1);
10558f0484fSRodney W. Grimes }
10658f0484fSRodney W. Grimes 
10751295a4dSJordan K. Hubbard int
10858f0484fSRodney W. Grimes forkpty(amaster, name, termp, winp)
10958f0484fSRodney W. Grimes 	int *amaster;
11058f0484fSRodney W. Grimes 	char *name;
11158f0484fSRodney W. Grimes 	struct termios *termp;
11258f0484fSRodney W. Grimes 	struct winsize *winp;
11358f0484fSRodney W. Grimes {
11458f0484fSRodney W. Grimes 	int master, slave, pid;
11558f0484fSRodney W. Grimes 
11658f0484fSRodney W. Grimes 	if (openpty(&master, &slave, name, termp, winp) == -1)
11758f0484fSRodney W. Grimes 		return (-1);
11858f0484fSRodney W. Grimes 	switch (pid = fork()) {
11958f0484fSRodney W. Grimes 	case -1:
12058f0484fSRodney W. Grimes 		return (-1);
12158f0484fSRodney W. Grimes 	case 0:
12258f0484fSRodney W. Grimes 		/*
12358f0484fSRodney W. Grimes 		 * child
12458f0484fSRodney W. Grimes 		 */
12558f0484fSRodney W. Grimes 		(void) close(master);
12658f0484fSRodney W. Grimes 		login_tty(slave);
12758f0484fSRodney W. Grimes 		return (0);
12858f0484fSRodney W. Grimes 	}
12958f0484fSRodney W. Grimes 	/*
13058f0484fSRodney W. Grimes 	 * parent
13158f0484fSRodney W. Grimes 	 */
13258f0484fSRodney W. Grimes 	*amaster = master;
13358f0484fSRodney W. Grimes 	(void) close(slave);
13458f0484fSRodney W. Grimes 	return (pid);
13558f0484fSRodney W. Grimes }
136