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 * 4. Neither the name of the University nor the names of its contributors 1458f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1558f0484fSRodney W. Grimes * without specific prior written permission. 1658f0484fSRodney W. Grimes * 1758f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1858f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1958f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2058f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2158f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2258f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2358f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2458f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2558f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2658f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2758f0484fSRodney W. Grimes * SUCH DAMAGE. 2858f0484fSRodney W. Grimes */ 2958f0484fSRodney W. Grimes 308719c58fSMatthew Dillon #include <sys/cdefs.h> 318719c58fSMatthew Dillon __FBSDID("$FreeBSD$"); 328719c58fSMatthew Dillon 3358f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 34673b7946SSteve Price #if 0 351b88e35bSBruce Evans static char sccsid[] = "@(#)pty.c 8.3 (Berkeley) 5/16/94"; 36673b7946SSteve Price #endif 3758f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 3858f0484fSRodney W. Grimes 3958f0484fSRodney W. Grimes #include <sys/types.h> 4058f0484fSRodney W. Grimes #include <sys/ioctl.h> 411b88e35bSBruce Evans #include <sys/stat.h> 421b88e35bSBruce Evans 4358f0484fSRodney W. Grimes #include <errno.h> 441b88e35bSBruce Evans #include <fcntl.h> 4558f0484fSRodney W. Grimes #include <grp.h> 460ebec5d3SMark Murray #include <libutil.h> 471b88e35bSBruce Evans #include <stdlib.h> 481b88e35bSBruce Evans #include <string.h> 491b88e35bSBruce Evans #include <termios.h> 50b4d2cfd9SDag-Erling Smørgrav #include <unistd.h> 5158f0484fSRodney W. Grimes 52c50897c3SJohn Baldwin int 53c50897c3SJohn Baldwin openpty(int *amaster, int *aslave, char *name, struct termios *termp, 54db256336SOlivier Houchard struct winsize *winp) 55db256336SOlivier Houchard { 56889befc4SJohn Baldwin const char *slavename; 57db256336SOlivier Houchard int master, slave; 58db256336SOlivier Houchard 598d333b3cSEd Schouten master = posix_openpt(O_RDWR|O_NOCTTY); 60db256336SOlivier Houchard if (master == -1) 61db256336SOlivier Houchard return (-1); 62db256336SOlivier Houchard 638d333b3cSEd Schouten if (grantpt(master) == -1) 648d333b3cSEd Schouten goto bad; 658d333b3cSEd Schouten 668d333b3cSEd Schouten if (unlockpt(master) == -1) 678d333b3cSEd Schouten goto bad; 68db256336SOlivier Houchard 69889befc4SJohn Baldwin slavename = ptsname(master); 708d333b3cSEd Schouten if (slavename == NULL) 718d333b3cSEd Schouten goto bad; 72889befc4SJohn Baldwin 73889befc4SJohn Baldwin slave = open(slavename, O_RDWR); 748d333b3cSEd Schouten if (slave == -1) 758d333b3cSEd Schouten goto bad; 76db256336SOlivier Houchard 77db256336SOlivier Houchard *amaster = master; 78db256336SOlivier Houchard *aslave = slave; 79db256336SOlivier Houchard 80db256336SOlivier Houchard if (name) 81c50897c3SJohn Baldwin strcpy(name, slavename); 82db256336SOlivier Houchard if (termp) 83db256336SOlivier Houchard tcsetattr(slave, TCSAFLUSH, termp); 84db256336SOlivier Houchard if (winp) 85db256336SOlivier Houchard ioctl(slave, TIOCSWINSZ, (char *)winp); 86db256336SOlivier Houchard 87db256336SOlivier Houchard return (0); 888d333b3cSEd Schouten 898d333b3cSEd Schouten bad: close(master); 908d333b3cSEd Schouten return (-1); 91db256336SOlivier Houchard } 92db256336SOlivier Houchard 933b7e1cc8SPeter Wemm int 94547fa0d9SMark Murray forkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp) 9558f0484fSRodney W. Grimes { 9658f0484fSRodney W. Grimes int master, slave, pid; 9758f0484fSRodney W. Grimes 9858f0484fSRodney W. Grimes if (openpty(&master, &slave, name, termp, winp) == -1) 9958f0484fSRodney W. Grimes return (-1); 10058f0484fSRodney W. Grimes switch (pid = fork()) { 10158f0484fSRodney W. Grimes case -1: 10258f0484fSRodney W. Grimes return (-1); 10358f0484fSRodney W. Grimes case 0: 10458f0484fSRodney W. Grimes /* 10558f0484fSRodney W. Grimes * child 10658f0484fSRodney W. Grimes */ 10758f0484fSRodney W. Grimes (void) close(master); 10858f0484fSRodney W. Grimes login_tty(slave); 10958f0484fSRodney W. Grimes return (0); 11058f0484fSRodney W. Grimes } 11158f0484fSRodney W. Grimes /* 11258f0484fSRodney W. Grimes * parent 11358f0484fSRodney W. Grimes */ 11458f0484fSRodney W. Grimes *amaster = master; 11558f0484fSRodney W. Grimes (void) close(slave); 11658f0484fSRodney W. Grimes return (pid); 11758f0484fSRodney W. Grimes } 118