| Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2008 The Android Open Source Project | 
 | 3 |  * All rights reserved. | 
 | 4 |  * | 
 | 5 |  * Redistribution and use in source and binary forms, with or without | 
 | 6 |  * modification, are permitted provided that the following conditions | 
 | 7 |  * are met: | 
 | 8 |  *  * Redistributions of source code must retain the above copyright | 
 | 9 |  *    notice, this list of conditions and the following disclaimer. | 
 | 10 |  *  * Redistributions in binary form must reproduce the above copyright | 
 | 11 |  *    notice, this list of conditions and the following disclaimer in | 
 | 12 |  *    the documentation and/or other materials provided with the | 
 | 13 |  *    distribution. | 
 | 14 |  * | 
 | 15 |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
 | 16 |  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
 | 17 |  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
 | 18 |  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
 | 19 |  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
 | 20 |  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
 | 21 |  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
 | 22 |  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
 | 23 |  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
 | 24 |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
 | 25 |  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
 | 26 |  * SUCH DAMAGE. | 
 | 27 |  */ | 
 | 28 |  | 
 | 29 | #include <errno.h> | 
 | 30 | #include <fcntl.h> | 
| Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 31 | #include <pty.h> | 
| Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 32 | #include <stdio.h> | 
 | 33 | #include <stdlib.h> | 
 | 34 | #include <sys/ioctl.h> | 
 | 35 | #include <termios.h> | 
 | 36 | #include <unistd.h> | 
| Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 37 | #include <utmp.h> | 
| Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 38 |  | 
| Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 39 | #include "bionic/pthread_internal.h" | 
| Elliott Hughes | a381fe8 | 2014-12-09 20:30:23 -0800 | [diff] [blame] | 40 |  | 
| Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 41 | int getpt() { | 
| Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 42 |   return posix_openpt(O_RDWR|O_NOCTTY); | 
 | 43 | } | 
 | 44 |  | 
 | 45 | int grantpt(int) { | 
 | 46 |   return 0; | 
 | 47 | } | 
 | 48 |  | 
 | 49 | int posix_openpt(int flags) { | 
 | 50 |   return open("/dev/ptmx", flags); | 
 | 51 | } | 
 | 52 |  | 
 | 53 | char* ptsname(int fd) { | 
| Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 54 |   bionic_tls& tls = __get_bionic_tls(); | 
 | 55 |   char* buf = tls.ptsname_buf; | 
 | 56 |   int error = ptsname_r(fd, buf, sizeof(tls.ptsname_buf)); | 
| Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 57 |   return (error == 0) ? buf : NULL; | 
| Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 58 | } | 
 | 59 |  | 
 | 60 | int ptsname_r(int fd, char* buf, size_t len) { | 
 | 61 |   if (buf == NULL) { | 
 | 62 |     errno = EINVAL; | 
 | 63 |     return errno; | 
 | 64 |   } | 
 | 65 |  | 
 | 66 |   unsigned int pty_num; | 
 | 67 |   if (ioctl(fd, TIOCGPTN, &pty_num) != 0) { | 
 | 68 |     errno = ENOTTY; | 
 | 69 |     return errno; | 
 | 70 |   } | 
 | 71 |  | 
 | 72 |   if (snprintf(buf, len, "/dev/pts/%u", pty_num) >= static_cast<int>(len)) { | 
 | 73 |     errno = ERANGE; | 
 | 74 |     return errno; | 
 | 75 |   } | 
 | 76 |  | 
 | 77 |   return 0; | 
 | 78 | } | 
 | 79 |  | 
 | 80 | char* ttyname(int fd) { | 
| Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 81 |   bionic_tls& tls = __get_bionic_tls(); | 
 | 82 |   char* buf = tls.ttyname_buf; | 
 | 83 |   int error = ttyname_r(fd, buf, sizeof(tls.ttyname_buf)); | 
| Elliott Hughes | 6170693 | 2015-03-31 10:56:58 -0700 | [diff] [blame] | 84 |   return (error == 0) ? buf : NULL; | 
| Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 85 | } | 
 | 86 |  | 
 | 87 | int ttyname_r(int fd, char* buf, size_t len) { | 
 | 88 |   if (buf == NULL) { | 
 | 89 |     errno = EINVAL; | 
 | 90 |     return errno; | 
 | 91 |   } | 
 | 92 |  | 
 | 93 |   if (!isatty(fd)) { | 
 | 94 |     return errno; | 
 | 95 |   } | 
 | 96 |  | 
 | 97 |   char path[64]; | 
 | 98 |   snprintf(path, sizeof(path), "/proc/self/fd/%d", fd); | 
 | 99 |  | 
 | 100 |   ssize_t count = readlink(path, buf, len); | 
 | 101 |   if (count == -1) { | 
 | 102 |     return errno; | 
 | 103 |   } | 
 | 104 |   if (static_cast<size_t>(count) == len) { | 
 | 105 |     errno = ERANGE; | 
 | 106 |     return errno; | 
 | 107 |   } | 
 | 108 |   buf[count] = '\0'; | 
 | 109 |   return 0; | 
 | 110 | } | 
 | 111 |  | 
 | 112 | int unlockpt(int fd) { | 
 | 113 |   int unlock = 0; | 
 | 114 |   return ioctl(fd, TIOCSPTLCK, &unlock); | 
 | 115 | } | 
| Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 116 |  | 
 | 117 | int openpty(int* master, int* slave, char* name, const termios* t, const winsize* ws) { | 
 | 118 |   *master = getpt(); | 
 | 119 |   if (*master == -1) { | 
 | 120 |     return -1; | 
 | 121 |   } | 
 | 122 |  | 
 | 123 |   if (grantpt(*master) == -1 || unlockpt(*master) == -1) { | 
 | 124 |     close(*master); | 
 | 125 |     return -1; | 
 | 126 |   } | 
 | 127 |  | 
 | 128 |   char buf[32]; | 
 | 129 |   if (name == NULL) { | 
 | 130 |     name = buf; | 
 | 131 |   } | 
 | 132 |   if (ptsname_r(*master, name, sizeof(buf)) != 0) { | 
 | 133 |     close(*master); | 
 | 134 |     return -1; | 
 | 135 |   } | 
 | 136 |  | 
 | 137 |   *slave = open(name, O_RDWR|O_NOCTTY); | 
 | 138 |   if (*slave == -1) { | 
 | 139 |     close(*master); | 
 | 140 |     return -1; | 
 | 141 |   } | 
 | 142 |  | 
 | 143 |   if (t != NULL) { | 
 | 144 |     tcsetattr(*slave, TCSAFLUSH, t); | 
 | 145 |   } | 
 | 146 |   if (ws != NULL) { | 
 | 147 |     ioctl(*slave, TIOCSWINSZ, ws); | 
 | 148 |   } | 
 | 149 |  | 
 | 150 |   return 0; | 
 | 151 | } | 
 | 152 |  | 
| Josh Gao | 6d7c1ee | 2016-03-04 18:04:41 -0800 | [diff] [blame] | 153 | int forkpty(int* amaster, char* name, const termios* t, const winsize* ws) { | 
 | 154 |   int master; | 
| Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 155 |   int slave; | 
| Josh Gao | 6d7c1ee | 2016-03-04 18:04:41 -0800 | [diff] [blame] | 156 |   if (openpty(&master, &slave, name, t, ws) == -1) { | 
| Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 157 |     return -1; | 
 | 158 |   } | 
 | 159 |  | 
 | 160 |   pid_t pid = fork(); | 
 | 161 |   if (pid == -1) { | 
| Josh Gao | 6d7c1ee | 2016-03-04 18:04:41 -0800 | [diff] [blame] | 162 |     close(master); | 
| Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 163 |     close(slave); | 
 | 164 |     return -1; | 
 | 165 |   } | 
 | 166 |  | 
 | 167 |   if (pid == 0) { | 
 | 168 |     // Child. | 
| Josh Gao | 6d7c1ee | 2016-03-04 18:04:41 -0800 | [diff] [blame] | 169 |     *amaster = -1; | 
 | 170 |     close(master); | 
| Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 171 |     if (login_tty(slave) == -1) { | 
 | 172 |       _exit(1); | 
 | 173 |     } | 
 | 174 |     return 0; | 
 | 175 |   } | 
 | 176 |  | 
 | 177 |   // Parent. | 
| Josh Gao | 6d7c1ee | 2016-03-04 18:04:41 -0800 | [diff] [blame] | 178 |   *amaster = master; | 
| Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 179 |   close(slave); | 
 | 180 |   return pid; | 
 | 181 | } | 
 | 182 |  | 
 | 183 | int login_tty(int fd) { | 
 | 184 |   setsid(); | 
 | 185 |  | 
 | 186 |   if (ioctl(fd, TIOCSCTTY, NULL) == -1) { | 
 | 187 |     return -1; | 
 | 188 |   } | 
 | 189 |  | 
 | 190 |   dup2(fd, STDIN_FILENO); | 
 | 191 |   dup2(fd, STDOUT_FILENO); | 
 | 192 |   dup2(fd, STDERR_FILENO); | 
 | 193 |   if (fd > STDERR_FILENO) { | 
 | 194 |     close(fd); | 
 | 195 |   } | 
 | 196 |  | 
 | 197 |   return 0; | 
 | 198 | } |