pty.c revision 173755
125184Sjkh/*- 2113674Smtm * Copyright (c) 1990, 1993, 1994 3113674Smtm * The Regents of the University of California. All rights reserved. 4113674Smtm * 5113674Smtm * Redistribution and use in source and binary forms, with or without 6113674Smtm * modification, are permitted provided that the following conditions 7113674Smtm * are met: 8113674Smtm * 1. Redistributions of source code must retain the above copyright 9113674Smtm * notice, this list of conditions and the following disclaimer. 10113674Smtm * 2. Redistributions in binary form must reproduce the above copyright 11113674Smtm * notice, this list of conditions and the following disclaimer in the 12113674Smtm * documentation and/or other materials provided with the distribution. 13113674Smtm * 4. Neither the name of the University nor the names of its contributors 14113674Smtm * may be used to endorse or promote products derived from this software 15113674Smtm * without specific prior written permission. 16113674Smtm * 17113674Smtm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18113674Smtm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19113674Smtm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20113674Smtm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21113674Smtm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22113674Smtm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23113674Smtm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24113674Smtm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2550472Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2666830Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27252015Shrs * SUCH DAMAGE. 2825184Sjkh */ 29252015Shrs 30252015Shrs#include <sys/cdefs.h> 31252015Shrs__FBSDID("$FreeBSD: head/lib/libutil/pty.c 173755 2007-11-19 20:49:42Z jhb $"); 32113674Smtm 33113674Smtm#if defined(LIBC_SCCS) && !defined(lint) 34113674Smtm#if 0 35113674Smtmstatic char sccsid[] = "@(#)pty.c 8.3 (Berkeley) 5/16/94"; 3625184Sjkh#endif 37178356Ssam#endif /* LIBC_SCCS and not lint */ 38197147Shrs 39225560Sbrueffer#include <sys/types.h> 40178356Ssam#include <sys/ioctl.h> 41178356Ssam#include <sys/stat.h> 42178356Ssam 43178356Ssam#include <errno.h> 44178356Ssam#include <fcntl.h> 45178356Ssam#include <grp.h> 46178356Ssam#include <libutil.h> 47178356Ssam#include <stdlib.h> 48178356Ssam#include <string.h> 49178356Ssam#include <termios.h> 50178356Ssam#include <unistd.h> 51256040Shrs 52256040Shrsint __use_pts(void); 53256040Shrs 54256040Shrsstatic int 55197139Shrsnew_openpty(int *amaster, int *aslave, char *name, struct termios *termp, 56178356Ssam struct winsize *winp) 57178356Ssam{ 58178356Ssam int master, slave; 59178356Ssam 60197139Shrs master = posix_openpt(O_RDWR); 61225560Sbrueffer if (master == -1) 62197147Shrs return (-1); 63178356Ssam 64178356Ssam if (grantpt(master) == -1) { 65178356Ssam close(master); 66178356Ssam return (-1); 67178356Ssam } 68178356Ssam 69178356Ssam slave = open(ptsname(master), O_RDWR); 70197139Shrs if (slave == -1) { 71178356Ssam close(master); 72256040Shrs return (-1); 73256040Shrs } 74256040Shrs 75256040Shrs if (unlockpt(master) == -1) { 76178356Ssam close(master); 77178356Ssam close(slave); 78197139Shrs return (-1); 79178356Ssam } 80178356Ssam 81178356Ssam *amaster = master; 82178356Ssam *aslave = slave; 83256255Shrs 84256255Shrs if (name) 85256255Shrs strcpy(name, ptsname(master)); 86256255Shrs if (termp) 87256255Shrs tcsetattr(slave, TCSAFLUSH, termp); 88256255Shrs if (winp) 89256255Shrs ioctl(slave, TIOCSWINSZ, (char *)winp); 90256255Shrs 91256255Shrs return (0); 92256255Shrs} 93256255Shrs 94256255Shrsint 95256255Shrsopenpty(int *amaster, int *aslave, char *name, struct termios *termp, struct winsize *winp) 96256255Shrs{ 97256255Shrs char line[] = "/dev/ptyXX"; 98256255Shrs const char *cp1, *cp2; 99256255Shrs int master, slave, ttygid; 100256255Shrs struct group *gr; 101256255Shrs 102256255Shrs if (__use_pts()) 103256255Shrs return (new_openpty(amaster, aslave, name, termp, winp)); 104256255Shrs 105256255Shrs if ((gr = getgrnam("tty")) != NULL) 106256255Shrs ttygid = gr->gr_gid; 107256255Shrs else 108256255Shrs ttygid = -1; 109256255Shrs 110256255Shrs for (cp1 = "pqrsPQRSlmnoLMNO"; *cp1; cp1++) { 111256255Shrs line[8] = *cp1; 112256255Shrs for (cp2 = "0123456789abcdefghijklmnopqrstuv"; *cp2; cp2++) { 113256255Shrs line[5] = 'p'; 114256255Shrs line[9] = *cp2; 115256255Shrs if ((master = open(line, O_RDWR, 0)) == -1) { 116256255Shrs if (errno == ENOENT) 117256255Shrs break; /* try the next pty group */ 118113674Smtm } else { 119113674Smtm line[5] = 't'; 120113674Smtm (void) chown(line, getuid(), ttygid); 121113674Smtm (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP); 122147088Sbrooks (void) revoke(line); 123147088Sbrooks if ((slave = open(line, O_RDWR, 0)) != -1) { 124113674Smtm *amaster = master; 125113674Smtm *aslave = slave; 126113674Smtm if (name) 127197139Shrs strcpy(name, line); 128147088Sbrooks if (termp) 129147088Sbrooks (void) tcsetattr(slave, 130222515Sbz TCSAFLUSH, termp); 131222515Sbz if (winp) 132222515Sbz (void) ioctl(slave, TIOCSWINSZ, 133222515Sbz (char *)winp); 134222515Sbz return (0); 135197139Shrs } 136256040Shrs (void) close(master); 137222733Shrs } 138222733Shrs } 139222746Shrs } 140222733Shrs errno = ENOENT; /* out of ptys */ 141212574Shrs return (-1); 142197139Shrs} 143222733Shrs 144222733Shrsint 145222733Shrsforkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp) 146253520Shrs{ 147253520Shrs int master, slave, pid; 148253520Shrs 149253520Shrs if (openpty(&master, &slave, name, termp, winp) == -1) 150253520Shrs return (-1); 151253520Shrs switch (pid = fork()) { 152253520Shrs case -1: 153253520Shrs return (-1); 154253520Shrs case 0: 155253520Shrs /* 156253520Shrs * child 157253520Shrs */ 158230453Shrs (void) close(master); 159222733Shrs login_tty(slave); 160222733Shrs return (0); 161225521Shrs } 162225521Shrs /* 163225521Shrs * parent 164225521Shrs */ 165225521Shrs *amaster = master; 166225521Shrs (void) close(slave); 167212574Shrs return (pid); 168252015Shrs} 169197526Shrs