pty.c revision 121193
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 3484225Sdillon#include <sys/cdefs.h> 3584225Sdillon__FBSDID("$FreeBSD: head/lib/libutil/pty.c 121193 2003-10-18 10:04:16Z markm $"); 3684225Sdillon 371573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 3828179Ssteve#if 0 3911681Sbdestatic char sccsid[] = "@(#)pty.c 8.3 (Berkeley) 5/16/94"; 4028179Ssteve#endif 411573Srgrimes#endif /* LIBC_SCCS and not lint */ 421573Srgrimes 431573Srgrimes#include <sys/types.h> 4411681Sbde#include <sys/ioctl.h> 451573Srgrimes#include <sys/stat.h> 4611681Sbde 4711681Sbde#include <errno.h> 481573Srgrimes#include <fcntl.h> 4911681Sbde#include <grp.h> 50116344Smarkm#include <libutil.h> 5111681Sbde#include <stdlib.h> 5211681Sbde#include <string.h> 531573Srgrimes#include <termios.h> 541573Srgrimes#include <unistd.h> 551573Srgrimes 5613137Speterint 57121193Smarkmopenpty(int *amaster, int *aslave, char *name, struct termios *termp, struct winsize *winp) 581573Srgrimes{ 5947449Sjb char line[] = "/dev/ptyXX"; 6092913Sobrien const char *cp1, *cp2; 6192913Sobrien 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) 7674068Sbillf break; /* try the next pty group */ 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 104121193Smarkmforkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp) 1051573Srgrimes{ 1061573Srgrimes int master, slave, pid; 1071573Srgrimes 1081573Srgrimes if (openpty(&master, &slave, name, termp, winp) == -1) 1091573Srgrimes return (-1); 1101573Srgrimes switch (pid = fork()) { 1111573Srgrimes case -1: 1121573Srgrimes return (-1); 1131573Srgrimes case 0: 1148870Srgrimes /* 1151573Srgrimes * child 1161573Srgrimes */ 1171573Srgrimes (void) close(master); 1181573Srgrimes login_tty(slave); 1191573Srgrimes return (0); 1201573Srgrimes } 1211573Srgrimes /* 1221573Srgrimes * parent 1231573Srgrimes */ 1241573Srgrimes *amaster = master; 1251573Srgrimes (void) close(slave); 1261573Srgrimes return (pid); 1271573Srgrimes} 128