158f0484fSRodney W. Grimes /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 41b88e35bSBruce Evans * Copyright (c) 1990, 1993, 1994 558f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 658f0484fSRodney W. Grimes * 758f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 858f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 958f0484fSRodney W. Grimes * are met: 1058f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1258f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1358f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1458f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 1658f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1758f0484fSRodney W. Grimes * without specific prior written permission. 1858f0484fSRodney W. Grimes * 1958f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2058f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2158f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2258f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2358f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2458f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2558f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2658f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2758f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2858f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2958f0484fSRodney W. Grimes * SUCH DAMAGE. 3058f0484fSRodney W. Grimes */ 3158f0484fSRodney W. Grimes 328719c58fSMatthew Dillon #include <sys/cdefs.h> 338719c58fSMatthew Dillon __FBSDID("$FreeBSD$"); 348719c58fSMatthew Dillon 3558f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 36673b7946SSteve Price #if 0 371b88e35bSBruce Evans static char sccsid[] = "@(#)pty.c 8.3 (Berkeley) 5/16/94"; 38673b7946SSteve Price #endif 3958f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 4058f0484fSRodney W. Grimes 4158f0484fSRodney W. Grimes #include <sys/types.h> 4258f0484fSRodney W. Grimes #include <sys/ioctl.h> 431b88e35bSBruce Evans #include <sys/stat.h> 441b88e35bSBruce Evans 4558f0484fSRodney W. Grimes #include <errno.h> 461b88e35bSBruce Evans #include <fcntl.h> 4758f0484fSRodney W. Grimes #include <grp.h> 480ebec5d3SMark Murray #include <libutil.h> 491b88e35bSBruce Evans #include <stdlib.h> 501b88e35bSBruce Evans #include <string.h> 511b88e35bSBruce Evans #include <termios.h> 52b4d2cfd9SDag-Erling Smørgrav #include <unistd.h> 5358f0484fSRodney W. Grimes 54c50897c3SJohn Baldwin int 55c50897c3SJohn Baldwin openpty(int *amaster, int *aslave, char *name, struct termios *termp, 56db256336SOlivier Houchard struct winsize *winp) 57db256336SOlivier Houchard { 58889befc4SJohn Baldwin const char *slavename; 59db256336SOlivier Houchard int master, slave; 60db256336SOlivier Houchard 618d333b3cSEd Schouten master = posix_openpt(O_RDWR|O_NOCTTY); 62db256336SOlivier Houchard if (master == -1) 63db256336SOlivier Houchard return (-1); 64db256336SOlivier Houchard 658d333b3cSEd Schouten if (grantpt(master) == -1) 668d333b3cSEd Schouten goto bad; 678d333b3cSEd Schouten 688d333b3cSEd Schouten if (unlockpt(master) == -1) 698d333b3cSEd Schouten goto bad; 70db256336SOlivier Houchard 71889befc4SJohn Baldwin slavename = ptsname(master); 728d333b3cSEd Schouten if (slavename == NULL) 738d333b3cSEd Schouten goto bad; 74889befc4SJohn Baldwin 75889befc4SJohn Baldwin slave = open(slavename, O_RDWR); 768d333b3cSEd Schouten if (slave == -1) 778d333b3cSEd Schouten goto bad; 78db256336SOlivier Houchard 79db256336SOlivier Houchard *amaster = master; 80db256336SOlivier Houchard *aslave = slave; 81db256336SOlivier Houchard 82db256336SOlivier Houchard if (name) 83c50897c3SJohn Baldwin strcpy(name, slavename); 84db256336SOlivier Houchard if (termp) 85db256336SOlivier Houchard tcsetattr(slave, TCSAFLUSH, termp); 86db256336SOlivier Houchard if (winp) 87db256336SOlivier Houchard ioctl(slave, TIOCSWINSZ, (char *)winp); 88db256336SOlivier Houchard 89db256336SOlivier Houchard return (0); 908d333b3cSEd Schouten 918d333b3cSEd Schouten bad: close(master); 928d333b3cSEd Schouten return (-1); 93db256336SOlivier Houchard } 94db256336SOlivier Houchard 953b7e1cc8SPeter Wemm int 96547fa0d9SMark Murray forkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp) 9758f0484fSRodney W. Grimes { 9858f0484fSRodney W. Grimes int master, slave, pid; 9958f0484fSRodney W. Grimes 10058f0484fSRodney W. Grimes if (openpty(&master, &slave, name, termp, winp) == -1) 10158f0484fSRodney W. Grimes return (-1); 10258f0484fSRodney W. Grimes switch (pid = fork()) { 10358f0484fSRodney W. Grimes case -1: 104*7e8fba78SWarner Losh (void)close(slave); 10558f0484fSRodney W. Grimes return (-1); 10658f0484fSRodney W. Grimes case 0: 10758f0484fSRodney W. Grimes /* 10858f0484fSRodney W. Grimes * child 10958f0484fSRodney W. Grimes */ 11058f0484fSRodney W. Grimes (void)close(master); 11158f0484fSRodney W. Grimes login_tty(slave); 11258f0484fSRodney W. Grimes return (0); 11358f0484fSRodney W. Grimes } 11458f0484fSRodney W. Grimes /* 11558f0484fSRodney W. Grimes * parent 11658f0484fSRodney W. Grimes */ 11758f0484fSRodney W. Grimes *amaster = master; 11858f0484fSRodney W. Grimes (void) close(slave); 11958f0484fSRodney W. Grimes return (pid); 12058f0484fSRodney W. Grimes } 121