1 /* $OpenBSD: pty.c,v 1.17 2012/07/09 14:26:40 nicm Exp $ */ 2 3 /*- 4 * Copyright (c) 1990, 1993 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 #include <sys/cdefs.h> 33 #include <sys/types.h> 34 #include <sys/stat.h> 35 #include <sys/ioctl.h> 36 #include <fcntl.h> 37 #include <termios.h> 38 #include <errno.h> 39 #include <unistd.h> 40 #include <stdio.h> 41 #include <string.h> 42 #include <grp.h> 43 #include <sys/tty.h> 44 45 #include "util.h" 46 47 int 48 openpty(int *amaster, int *aslave, char *name, struct termios *termp, 49 struct winsize *winp) 50 { 51 int master, slave, fd; 52 struct ptmget ptm; 53 54 /* 55 * Use /dev/ptm and the PTMGET ioctl to get a properly set up and 56 * owned pty/tty pair. 57 */ 58 fd = open(PATH_PTMDEV, O_RDWR, 0); 59 if (fd == -1) 60 return (-1); 61 if ((ioctl(fd, PTMGET, &ptm) == -1)) { 62 close(fd); 63 return (-1); 64 } 65 close(fd); 66 master = ptm.cfd; 67 slave = ptm.sfd; 68 if (name) { 69 /* 70 * Manual page says "at least 16 characters". 71 */ 72 strlcpy(name, ptm.sn, 16); 73 } 74 *amaster = master; 75 *aslave = slave; 76 if (termp) 77 (void) tcsetattr(slave, TCSAFLUSH, termp); 78 if (winp) 79 (void) ioctl(slave, TIOCSWINSZ, winp); 80 return (0); 81 } 82 83 pid_t 84 forkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp) 85 { 86 int master, slave; 87 pid_t pid; 88 89 if (openpty(&master, &slave, name, termp, winp) == -1) 90 return (-1); 91 switch (pid = fork()) { 92 case -1: 93 return (-1); 94 case 0: 95 /* 96 * child 97 */ 98 (void) close(master); 99 login_tty(slave); 100 return (0); 101 } 102 /* 103 * parent 104 */ 105 *amaster = master; 106 (void) close(slave); 107 return (pid); 108 } 109