1 1.1 christos /* 2 1.1 christos * Copyright (c) 2008 Nicholas Marriott <nicholas.marriott (at) gmail.com> 3 1.1 christos * 4 1.1 christos * Permission to use, copy, modify, and distribute this software for any 5 1.1 christos * purpose with or without fee is hereby granted, provided that the above 6 1.1 christos * copyright notice and this permission notice appear in all copies. 7 1.1 christos * 8 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 13 1.1 christos * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 14 1.1 christos * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 1.1 christos */ 16 1.1 christos 17 1.1 christos #include <sys/types.h> 18 1.1 christos #include <sys/ioctl.h> 19 1.1 christos 20 1.1 christos #include <fcntl.h> 21 1.1 christos #include <stdlib.h> 22 1.1 christos #include <unistd.h> 23 1.1 christos 24 1.1 christos #include "compat.h" 25 1.1 christos 26 1.1 christos void fatal(const char *, ...); 27 1.1 christos void fatalx(const char *, ...); 28 1.1 christos 29 1.1 christos pid_t 30 1.1 christos forkpty(int *master, char *name, struct termios *tio, struct winsize *ws) 31 1.1 christos { 32 1.1 christos int slave = -1; 33 1.1 christos char *path; 34 1.1 christos pid_t pid; 35 1.1 christos 36 1.1 christos if ((*master = open("/dev/ptmx", O_RDWR|O_NOCTTY)) == -1) 37 1.1 christos return (-1); 38 1.1 christos if (grantpt(*master) != 0) 39 1.1 christos goto out; 40 1.1 christos if (unlockpt(*master) != 0) 41 1.1 christos goto out; 42 1.1 christos 43 1.1 christos if ((path = ptsname(*master)) == NULL) 44 1.1 christos goto out; 45 1.1 christos if (name != NULL) 46 1.1 christos strlcpy(name, path, TTY_NAME_MAX); 47 1.1 christos if ((slave = open(path, O_RDWR|O_NOCTTY)) == -1) 48 1.1 christos goto out; 49 1.1 christos 50 1.1 christos switch (pid = fork()) { 51 1.1 christos case -1: 52 1.1 christos goto out; 53 1.1 christos case 0: 54 1.1 christos close(*master); 55 1.1 christos 56 1.1 christos setsid(); 57 1.1 christos if (ioctl(slave, TIOCSCTTY, NULL) == -1) 58 1.1 christos fatal("ioctl failed"); 59 1.1 christos 60 1.1 christos if (tio != NULL && tcsetattr(slave, TCSAFLUSH, tio) == -1) 61 1.1 christos fatal("tcsetattr failed"); 62 1.1 christos if (ioctl(slave, TIOCSWINSZ, ws) == -1) 63 1.1 christos fatal("ioctl failed"); 64 1.1 christos 65 1.1 christos dup2(slave, 0); 66 1.1 christos dup2(slave, 1); 67 1.1 christos dup2(slave, 2); 68 1.1 christos if (slave > 2) 69 1.1 christos close(slave); 70 1.1 christos return (0); 71 1.1 christos } 72 1.1 christos 73 1.1 christos close(slave); 74 1.1 christos return (pid); 75 1.1 christos 76 1.1 christos out: 77 1.1 christos if (*master != -1) 78 1.1 christos close(*master); 79 1.1 christos if (slave != -1) 80 1.1 christos close(slave); 81 1.1 christos return (-1); 82 1.1 christos } 83