1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1990, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 33 #include <sys/types.h> 34 #include <sys/ioctl.h> 35 #include <sys/stat.h> 36 37 #include <errno.h> 38 #include <fcntl.h> 39 #include <grp.h> 40 #include <libutil.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <termios.h> 44 #include <unistd.h> 45 46 int 47 openpty(int *amaster, int *aslave, char *name, struct termios *termp, 48 struct winsize *winp) 49 { 50 const char *slavename; 51 int master, slave; 52 53 master = posix_openpt(O_RDWR|O_NOCTTY); 54 if (master == -1) 55 return (-1); 56 57 if (grantpt(master) == -1) 58 goto bad; 59 60 if (unlockpt(master) == -1) 61 goto bad; 62 63 slavename = ptsname(master); 64 if (slavename == NULL) 65 goto bad; 66 67 slave = open(slavename, O_RDWR); 68 if (slave == -1) 69 goto bad; 70 71 *amaster = master; 72 *aslave = slave; 73 74 if (name) 75 strcpy(name, slavename); 76 if (termp) 77 tcsetattr(slave, TCSAFLUSH, termp); 78 if (winp) 79 ioctl(slave, TIOCSWINSZ, (char *)winp); 80 81 return (0); 82 83 bad: close(master); 84 return (-1); 85 } 86 87 int 88 forkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp) 89 { 90 int master, slave, pid; 91 92 if (openpty(&master, &slave, name, termp, winp) == -1) 93 return (-1); 94 switch (pid = fork()) { 95 case -1: 96 (void)close(master); 97 (void)close(slave); 98 return (-1); 99 case 0: 100 /* 101 * child 102 */ 103 (void)close(master); 104 login_tty(slave); 105 return (0); 106 } 107 /* 108 * parent 109 */ 110 *amaster = master; 111 (void) close(slave); 112 return (pid); 113 } 114