1*d76e3cd5Satatat /* $NetBSD: pty.c,v 1.19 2002/03/09 20:09:28 atatat Exp $ */ 2f5646a08Schristos 361f28255Scgd /*- 45e3388b0Sperry * Copyright (c) 1990, 1993, 1994 53a724adbScgd * The Regents of the University of California. All rights reserved. 661f28255Scgd * 761f28255Scgd * Redistribution and use in source and binary forms, with or without 861f28255Scgd * modification, are permitted provided that the following conditions 961f28255Scgd * are met: 1061f28255Scgd * 1. Redistributions of source code must retain the above copyright 1161f28255Scgd * notice, this list of conditions and the following disclaimer. 1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright 1361f28255Scgd * notice, this list of conditions and the following disclaimer in the 1461f28255Scgd * documentation and/or other materials provided with the distribution. 1561f28255Scgd * 3. All advertising materials mentioning features or use of this software 1661f28255Scgd * must display the following acknowledgement: 1761f28255Scgd * This product includes software developed by the University of 1861f28255Scgd * California, Berkeley and its contributors. 1961f28255Scgd * 4. Neither the name of the University nor the names of its contributors 2061f28255Scgd * may be used to endorse or promote products derived from this software 2161f28255Scgd * without specific prior written permission. 2261f28255Scgd * 2361f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2461f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2561f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2661f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2761f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2861f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2961f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3061f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3161f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3261f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3361f28255Scgd * SUCH DAMAGE. 3461f28255Scgd */ 3561f28255Scgd 36f5646a08Schristos #include <sys/cdefs.h> 3761f28255Scgd #if defined(LIBC_SCCS) && !defined(lint) 38e42a9484Sjtc #if 0 395e3388b0Sperry static char sccsid[] = "@(#)pty.c 8.3 (Berkeley) 5/16/94"; 40e42a9484Sjtc #else 41*d76e3cd5Satatat __RCSID("$NetBSD: pty.c,v 1.19 2002/03/09 20:09:28 atatat Exp $"); 42e42a9484Sjtc #endif 4361f28255Scgd #endif /* LIBC_SCCS and not lint */ 4461f28255Scgd 4561f28255Scgd #include <sys/types.h> 4661f28255Scgd #include <sys/ioctl.h> 475e3388b0Sperry #include <sys/stat.h> 485e3388b0Sperry 49b48252f3Slukem #include <assert.h> 5061f28255Scgd #include <errno.h> 515e3388b0Sperry #include <fcntl.h> 525e3388b0Sperry #include <grp.h> 5361f28255Scgd #include <stdio.h> 5461f28255Scgd #include <string.h> 555e3388b0Sperry #include <termios.h> 565e3388b0Sperry #include <unistd.h> 57e42a9484Sjtc #include <util.h> 5861f28255Scgd 594db962fcSlukem /* 604db962fcSlukem * XXX: `v' removed until no ports are using console devices which use ttyv0 614db962fcSlukem */ 624db962fcSlukem #define TTY_LETTERS "pqrstuwxyzPQRST" 63*d76e3cd5Satatat #define TTY_OLD_SUFFIX "0123456789abcdef" 64*d76e3cd5Satatat #define TTY_NEW_SUFFIX "ghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" 658347998aSpk 668347998aSpk int 67fce98185Sad openpty(int *amaster, int *aslave, char *name, struct termios *termp, 68fce98185Sad struct winsize *winp) 6961f28255Scgd { 707cc5f9cdSmycroft static char line[] = "/dev/XtyXX"; 71*d76e3cd5Satatat const char *cp1, *cp2, *cp; 72c6652502Stls int master, slave, tries = 0; 73983a6b1fSchristos gid_t ttygid; 7461f28255Scgd struct group *gr; 7561f28255Scgd 76b48252f3Slukem _DIAGASSERT(amaster != NULL); 77b48252f3Slukem _DIAGASSERT(aslave != NULL); 78b48252f3Slukem /* name may be NULL */ 79b48252f3Slukem /* termp may be NULL */ 80b48252f3Slukem /* winp may be NULL */ 81b48252f3Slukem 8261f28255Scgd if ((gr = getgrnam("tty")) != NULL) 8361f28255Scgd ttygid = gr->gr_gid; 8461f28255Scgd else 85983a6b1fSchristos ttygid = (gid_t) -1; 8661f28255Scgd 87e42a9484Sjtc for (cp1 = TTY_LETTERS; *cp1; cp1++) { 88c6652502Stls tries = 0; 8961f28255Scgd line[8] = *cp1; 90*d76e3cd5Satatat for (cp = cp2 = TTY_OLD_SUFFIX TTY_NEW_SUFFIX; *cp2; cp2++) { 915e3388b0Sperry line[5] = 'p'; 9261f28255Scgd line[9] = *cp2; 9361f28255Scgd if ((master = open(line, O_RDWR, 0)) == -1) { 94*d76e3cd5Satatat if (errno == ENOENT) { 95*d76e3cd5Satatat if (cp2 - cp + 1 < sizeof(TTY_OLD_SUFFIX)) 9661f28255Scgd return (-1); /* out of ptys */ 97*d76e3cd5Satatat else 98*d76e3cd5Satatat break; 99c6652502Stls } 10061f28255Scgd } else { 10161f28255Scgd line[5] = 't'; 10261f28255Scgd (void) chown(line, getuid(), ttygid); 10361f28255Scgd (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP); 10461f28255Scgd (void) revoke(line); 10561f28255Scgd if ((slave = open(line, O_RDWR, 0)) != -1) { 10661f28255Scgd *amaster = master; 10761f28255Scgd *aslave = slave; 10861f28255Scgd if (name) 10961f28255Scgd strcpy(name, line); 11061f28255Scgd if (termp) 11161f28255Scgd (void) tcsetattr(slave, 11261f28255Scgd TCSAFLUSH, termp); 11361f28255Scgd if (winp) 11461f28255Scgd (void) ioctl(slave, TIOCSWINSZ, 115983a6b1fSchristos winp); 11661f28255Scgd return (0); 11761f28255Scgd } 11861f28255Scgd (void) close(master); 11961f28255Scgd } 12061f28255Scgd } 12161f28255Scgd } 12261f28255Scgd errno = ENOENT; /* out of ptys */ 12361f28255Scgd return (-1); 12461f28255Scgd } 12561f28255Scgd 1268347998aSpk pid_t 127fce98185Sad forkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp) 12861f28255Scgd { 1298347998aSpk int master, slave; 1308347998aSpk pid_t pid; 13161f28255Scgd 132b48252f3Slukem _DIAGASSERT(amaster != NULL); 133b48252f3Slukem /* name may be NULL */ 134b48252f3Slukem /* termp may be NULL */ 135b48252f3Slukem /* winp may be NULL */ 136b48252f3Slukem 13761f28255Scgd if (openpty(&master, &slave, name, termp, winp) == -1) 13861f28255Scgd return (-1); 13961f28255Scgd switch (pid = fork()) { 14061f28255Scgd case -1: 14161f28255Scgd return (-1); 14261f28255Scgd case 0: 14361f28255Scgd /* 14461f28255Scgd * child 14561f28255Scgd */ 14661f28255Scgd (void) close(master); 14761f28255Scgd login_tty(slave); 14861f28255Scgd return (0); 14961f28255Scgd } 15061f28255Scgd /* 15161f28255Scgd * parent 15261f28255Scgd */ 15361f28255Scgd *amaster = master; 15461f28255Scgd (void) close(slave); 15561f28255Scgd return (pid); 15661f28255Scgd } 157