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 | 4d215aa | 2017-10-18 15:54:56 -0700 | [diff] [blame] | 40 | #include "private/FdPath.h" |
Elliott Hughes | a381fe8 | 2014-12-09 20:30:23 -0800 | [diff] [blame] | 41 | |
Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 42 | int getpt() { |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 43 | return posix_openpt(O_RDWR|O_NOCTTY); |
| 44 | } |
| 45 | |
| 46 | int grantpt(int) { |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | int posix_openpt(int flags) { |
| 51 | return open("/dev/ptmx", flags); |
| 52 | } |
| 53 | |
| 54 | char* ptsname(int fd) { |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 55 | bionic_tls& tls = __get_bionic_tls(); |
| 56 | char* buf = tls.ptsname_buf; |
| 57 | int error = ptsname_r(fd, buf, sizeof(tls.ptsname_buf)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 58 | return (error == 0) ? buf : nullptr; |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | int ptsname_r(int fd, char* buf, size_t len) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 62 | if (buf == nullptr) { |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 63 | errno = EINVAL; |
| 64 | return errno; |
| 65 | } |
| 66 | |
| 67 | unsigned int pty_num; |
| 68 | if (ioctl(fd, TIOCGPTN, &pty_num) != 0) { |
| 69 | errno = ENOTTY; |
| 70 | return errno; |
| 71 | } |
| 72 | |
| 73 | if (snprintf(buf, len, "/dev/pts/%u", pty_num) >= static_cast<int>(len)) { |
| 74 | errno = ERANGE; |
| 75 | return errno; |
| 76 | } |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | char* ttyname(int fd) { |
Josh Gao | 5e2285d | 2017-02-22 12:19:05 -0800 | [diff] [blame] | 82 | bionic_tls& tls = __get_bionic_tls(); |
| 83 | char* buf = tls.ttyname_buf; |
| 84 | int error = ttyname_r(fd, buf, sizeof(tls.ttyname_buf)); |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 85 | return (error == 0) ? buf : nullptr; |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | int ttyname_r(int fd, char* buf, size_t len) { |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 89 | if (buf == nullptr) { |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 90 | errno = EINVAL; |
| 91 | return errno; |
| 92 | } |
| 93 | |
| 94 | if (!isatty(fd)) { |
| 95 | return errno; |
| 96 | } |
| 97 | |
Elliott Hughes | 4d215aa | 2017-10-18 15:54:56 -0700 | [diff] [blame] | 98 | ssize_t count = readlink(FdPath(fd).c_str(), buf, len); |
Elliott Hughes | 4916706 | 2014-07-25 17:24:00 -0700 | [diff] [blame] | 99 | if (count == -1) { |
| 100 | return errno; |
| 101 | } |
| 102 | if (static_cast<size_t>(count) == len) { |
| 103 | errno = ERANGE; |
| 104 | return errno; |
| 105 | } |
| 106 | buf[count] = '\0'; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int unlockpt(int fd) { |
| 111 | int unlock = 0; |
| 112 | return ioctl(fd, TIOCSPTLCK, &unlock); |
| 113 | } |
Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 114 | |
| 115 | int openpty(int* master, int* slave, char* name, const termios* t, const winsize* ws) { |
| 116 | *master = getpt(); |
| 117 | if (*master == -1) { |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | if (grantpt(*master) == -1 || unlockpt(*master) == -1) { |
| 122 | close(*master); |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | char buf[32]; |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 127 | if (name == nullptr) { |
Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 128 | name = buf; |
| 129 | } |
| 130 | if (ptsname_r(*master, name, sizeof(buf)) != 0) { |
| 131 | close(*master); |
| 132 | return -1; |
| 133 | } |
| 134 | |
| 135 | *slave = open(name, O_RDWR|O_NOCTTY); |
| 136 | if (*slave == -1) { |
| 137 | close(*master); |
| 138 | return -1; |
| 139 | } |
| 140 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 141 | if (t != nullptr) { |
Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 142 | tcsetattr(*slave, TCSAFLUSH, t); |
| 143 | } |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 144 | if (ws != nullptr) { |
Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 145 | ioctl(*slave, TIOCSWINSZ, ws); |
| 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
Josh Gao | 6d7c1ee | 2016-03-04 18:04:41 -0800 | [diff] [blame] | 151 | int forkpty(int* amaster, char* name, const termios* t, const winsize* ws) { |
| 152 | int master; |
Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 153 | int slave; |
Josh Gao | 6d7c1ee | 2016-03-04 18:04:41 -0800 | [diff] [blame] | 154 | if (openpty(&master, &slave, name, t, ws) == -1) { |
Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | pid_t pid = fork(); |
| 159 | if (pid == -1) { |
Josh Gao | 6d7c1ee | 2016-03-04 18:04:41 -0800 | [diff] [blame] | 160 | close(master); |
Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 161 | close(slave); |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | if (pid == 0) { |
| 166 | // Child. |
Josh Gao | 6d7c1ee | 2016-03-04 18:04:41 -0800 | [diff] [blame] | 167 | *amaster = -1; |
| 168 | close(master); |
Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 169 | if (login_tty(slave) == -1) { |
| 170 | _exit(1); |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | // Parent. |
Josh Gao | 6d7c1ee | 2016-03-04 18:04:41 -0800 | [diff] [blame] | 176 | *amaster = master; |
Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 177 | close(slave); |
| 178 | return pid; |
| 179 | } |
| 180 | |
| 181 | int login_tty(int fd) { |
| 182 | setsid(); |
| 183 | |
Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 184 | if (ioctl(fd, TIOCSCTTY, nullptr) == -1) { |
Elliott Hughes | 65f0df7 | 2014-12-03 14:39:20 -0800 | [diff] [blame] | 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | dup2(fd, STDIN_FILENO); |
| 189 | dup2(fd, STDOUT_FILENO); |
| 190 | dup2(fd, STDERR_FILENO); |
| 191 | if (fd > STDERR_FILENO) { |
| 192 | close(fd); |
| 193 | } |
| 194 | |
| 195 | return 0; |
| 196 | } |