pty.c revision 17141
11573Srgrimes/*-
211681Sbde * Copyright (c) 1990, 1993, 1994
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * Redistribution and use in source and binary forms, with or without
61573Srgrimes * modification, are permitted provided that the following conditions
71573Srgrimes * are met:
81573Srgrimes * 1. Redistributions of source code must retain the above copyright
91573Srgrimes *    notice, this list of conditions and the following disclaimer.
101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111573Srgrimes *    notice, this list of conditions and the following disclaimer in the
121573Srgrimes *    documentation and/or other materials provided with the distribution.
131573Srgrimes * 3. All advertising materials mentioning features or use of this software
141573Srgrimes *    must display the following acknowledgement:
151573Srgrimes *	This product includes software developed by the University of
161573Srgrimes *	California, Berkeley and its contributors.
171573Srgrimes * 4. Neither the name of the University nor the names of its contributors
181573Srgrimes *    may be used to endorse or promote products derived from this software
191573Srgrimes *    without specific prior written permission.
201573Srgrimes *
211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311573Srgrimes * SUCH DAMAGE.
321573Srgrimes */
331573Srgrimes
341573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
3511681Sbdestatic char sccsid[] = "@(#)pty.c	8.3 (Berkeley) 5/16/94";
361573Srgrimes#endif /* LIBC_SCCS and not lint */
371573Srgrimes
381573Srgrimes#include <sys/types.h>
3911681Sbde#include <sys/ioctl.h>
401573Srgrimes#include <sys/stat.h>
4111681Sbde
4211681Sbde#include <errno.h>
431573Srgrimes#include <fcntl.h>
4411681Sbde#include <grp.h>
4511681Sbde#include <stdio.h>
4611681Sbde#include <stdlib.h>
4711681Sbde#include <string.h>
481573Srgrimes#include <termios.h>
491573Srgrimes#include <unistd.h>
5013137Speter#include <libutil.h>
511573Srgrimes
5213137Speterint
531573Srgrimesopenpty(amaster, aslave, name, termp, winp)
541573Srgrimes	int *amaster, *aslave;
551573Srgrimes	char *name;
561573Srgrimes	struct termios *termp;
571573Srgrimes	struct winsize *winp;
581573Srgrimes{
591573Srgrimes	static char line[] = "/dev/ptyXX";
601573Srgrimes	register const char *cp1, *cp2;
611573Srgrimes	register int master, slave, ttygid;
621573Srgrimes	struct group *gr;
631573Srgrimes
641573Srgrimes	if ((gr = getgrnam("tty")) != NULL)
651573Srgrimes		ttygid = gr->gr_gid;
661573Srgrimes	else
671573Srgrimes		ttygid = -1;
681573Srgrimes
696264Sjkh	for (cp1 = "pqrsPQRS"; *cp1; cp1++) {
701573Srgrimes		line[8] = *cp1;
716264Sjkh		for (cp2 = "0123456789abcdefghijklmnopqrstuv"; *cp2; cp2++) {
7211681Sbde			line[5] = 'p';
731573Srgrimes			line[9] = *cp2;
741573Srgrimes			if ((master = open(line, O_RDWR, 0)) == -1) {
751573Srgrimes				if (errno == ENOENT)
761573Srgrimes					return (-1);	/* out of ptys */
771573Srgrimes			} else {
781573Srgrimes				line[5] = 't';
791573Srgrimes				(void) chown(line, getuid(), ttygid);
801573Srgrimes				(void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
811573Srgrimes				(void) revoke(line);
821573Srgrimes				if ((slave = open(line, O_RDWR, 0)) != -1) {
831573Srgrimes					*amaster = master;
841573Srgrimes					*aslave = slave;
851573Srgrimes					if (name)
861573Srgrimes						strcpy(name, line);
871573Srgrimes					if (termp)
888870Srgrimes						(void) tcsetattr(slave,
891573Srgrimes							TCSAFLUSH, termp);
901573Srgrimes					if (winp)
918870Srgrimes						(void) ioctl(slave, TIOCSWINSZ,
921573Srgrimes							(char *)winp);
931573Srgrimes					return (0);
941573Srgrimes				}
951573Srgrimes				(void) close(master);
961573Srgrimes			}
971573Srgrimes		}
981573Srgrimes	}
991573Srgrimes	errno = ENOENT;	/* out of ptys */
1001573Srgrimes	return (-1);
1011573Srgrimes}
1021573Srgrimes
10317141Sjkhint
1041573Srgrimesforkpty(amaster, name, termp, winp)
1051573Srgrimes	int *amaster;
1061573Srgrimes	char *name;
1071573Srgrimes	struct termios *termp;
1081573Srgrimes	struct winsize *winp;
1091573Srgrimes{
1101573Srgrimes	int master, slave, pid;
1111573Srgrimes
1121573Srgrimes	if (openpty(&master, &slave, name, termp, winp) == -1)
1131573Srgrimes		return (-1);
1141573Srgrimes	switch (pid = fork()) {
1151573Srgrimes	case -1:
1161573Srgrimes		return (-1);
1171573Srgrimes	case 0:
1188870Srgrimes		/*
1191573Srgrimes		 * child
1201573Srgrimes		 */
1211573Srgrimes		(void) close(master);
1221573Srgrimes		login_tty(slave);
1231573Srgrimes		return (0);
1241573Srgrimes	}
1251573Srgrimes	/*
1261573Srgrimes	 * parent
1271573Srgrimes	 */
1281573Srgrimes	*amaster = master;
1291573Srgrimes	(void) close(slave);
1301573Srgrimes	return (pid);
1311573Srgrimes}
132