Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define _GNU_SOURCE |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <err.h> |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 21 | #include <errno.h> |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 22 | #include <fcntl.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 25 | #include <string.h> |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 26 | #include <unistd.h> |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 27 | #include <termios.h> |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 28 | |
| 29 | #include "console-server.h" |
| 30 | |
| 31 | struct tty_handler { |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 32 | struct handler handler; |
| 33 | struct console *console; |
| 34 | struct ringbuffer_consumer *rbc; |
| 35 | struct poller *poller; |
| 36 | int fd; |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 37 | int fd_flags; |
| 38 | bool blocked; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | static struct tty_handler *to_tty_handler(struct handler *handler) |
| 42 | { |
| 43 | return container_of(handler, struct tty_handler, handler); |
| 44 | } |
| 45 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 46 | static void tty_set_fd_blocking(struct tty_handler *th, bool fd_blocking) |
| 47 | { |
| 48 | int flags; |
| 49 | |
| 50 | flags = th->fd_flags & ~O_NONBLOCK; |
| 51 | if (!fd_blocking) |
| 52 | flags |= O_NONBLOCK; |
| 53 | |
| 54 | if (flags != th->fd_flags) { |
| 55 | fcntl(th->fd, F_SETFL, flags); |
| 56 | th->fd_flags = flags; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * A "blocked" handler indicates that the last write returned EAGAIN |
| 62 | * (==EWOULDBLOCK), so we know not to continue writing (for non-forced output), |
| 63 | * as it'll just return EAGAIN again. |
| 64 | * |
| 65 | * Once we detect this, we watch for POLLOUT in the poller events. A |
| 66 | * POLLOUT indicates that the fd is no longer blocking, so we clear |
| 67 | * blocked mode and can continue writing. |
| 68 | */ |
| 69 | static void tty_set_blocked(struct tty_handler *th, bool blocked) |
| 70 | { |
| 71 | int events; |
| 72 | |
| 73 | if (blocked == th->blocked) |
| 74 | return; |
| 75 | |
| 76 | th->blocked = blocked; |
| 77 | events = POLLIN; |
| 78 | |
| 79 | if (th->blocked) |
| 80 | events |= POLLOUT; |
| 81 | |
| 82 | console_poller_set_events(th->console, th->poller, events); |
| 83 | } |
| 84 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 85 | static int tty_drain_queue(struct tty_handler *th, size_t force_len) |
| 86 | { |
| 87 | size_t len, total_len; |
| 88 | ssize_t wlen; |
| 89 | uint8_t *buf; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 90 | |
| 91 | /* if we're forcing data, we need to clear non-blocking mode */ |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 92 | if (force_len) |
| 93 | tty_set_fd_blocking(th, true); |
| 94 | |
| 95 | /* no point writing, we'll just see -EAGAIN */ |
| 96 | else if (th->blocked) |
| 97 | return 0; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 98 | |
| 99 | total_len = 0; |
| 100 | |
| 101 | for (;;) { |
| 102 | len = ringbuffer_dequeue_peek(th->rbc, total_len, &buf); |
| 103 | if (!len) |
| 104 | break; |
| 105 | |
| 106 | /* write as little as possible while blocking */ |
| 107 | if (force_len && force_len < total_len + len) |
| 108 | len = force_len - total_len; |
| 109 | |
| 110 | wlen = write(th->fd, buf, len); |
| 111 | if (wlen < 0) { |
| 112 | if (errno == EINTR) |
| 113 | continue; |
| 114 | if ((errno == EAGAIN || errno == EWOULDBLOCK) |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 115 | && !force_len) { |
| 116 | tty_set_blocked(th, true); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 117 | break; |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 118 | } |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 119 | warn("failed writing to local tty; disabling"); |
| 120 | return -1; |
| 121 | } |
| 122 | |
| 123 | total_len += wlen; |
| 124 | |
| 125 | if (force_len && total_len >= force_len) |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | ringbuffer_dequeue_commit(th->rbc, total_len); |
| 130 | |
| 131 | if (force_len) |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 132 | tty_set_fd_blocking(th, false); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | static enum ringbuffer_poll_ret tty_ringbuffer_poll(void *arg, size_t force_len) |
| 138 | { |
| 139 | struct tty_handler *th = arg; |
| 140 | int rc; |
| 141 | |
| 142 | rc = tty_drain_queue(th, force_len); |
| 143 | if (rc) { |
| 144 | console_poller_unregister(th->console, th->poller); |
| 145 | return RINGBUFFER_POLL_REMOVE; |
| 146 | } |
| 147 | |
| 148 | return RINGBUFFER_POLL_OK; |
| 149 | } |
| 150 | |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 151 | static enum poller_ret tty_poll(struct handler *handler, |
| 152 | int events, void __attribute__((unused)) *data) |
| 153 | { |
| 154 | struct tty_handler *th = to_tty_handler(handler); |
| 155 | uint8_t buf[4096]; |
| 156 | ssize_t len; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 157 | int rc; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 158 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 159 | if (events & POLLIN) { |
| 160 | len = read(th->fd, buf, sizeof(buf)); |
Jeremy Kerr | 67eab04 | 2017-07-12 12:08:35 +0800 | [diff] [blame] | 161 | if (len <= 0) |
| 162 | goto err; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 163 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 164 | console_data_out(th->console, buf, len); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 165 | } |
| 166 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 167 | if (events & POLLOUT) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 168 | tty_set_blocked(th, false); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 169 | rc = tty_drain_queue(th, 0); |
Jeremy Kerr | 67eab04 | 2017-07-12 12:08:35 +0800 | [diff] [blame] | 170 | if (rc) |
| 171 | goto err; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 172 | } |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 173 | |
| 174 | return POLLER_OK; |
Jeremy Kerr | 67eab04 | 2017-07-12 12:08:35 +0800 | [diff] [blame] | 175 | |
| 176 | err: |
| 177 | th->poller = NULL; |
| 178 | close(th->fd); |
| 179 | ringbuffer_consumer_unregister(th->rbc); |
| 180 | return POLLER_REMOVE; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 181 | } |
| 182 | |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 183 | static int set_terminal_baud(struct tty_handler *th, const char *tty_name, |
| 184 | const char *desired_baud) { |
| 185 | struct termios term_options; |
| 186 | speed_t speed; |
| 187 | |
Benjamin Fair | fcbdea9 | 2018-06-04 14:19:25 -0700 | [diff] [blame] | 188 | if (config_parse_baud(&speed, desired_baud) != 0) { |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 189 | fprintf(stderr, "%s is not a valid baud rate for terminal %s\n", |
| 190 | desired_baud, tty_name); |
| 191 | return -1; |
| 192 | } |
| 193 | |
| 194 | if (tcgetattr(th->fd, &term_options) < 0) { |
| 195 | warn("Can't get config for %s", tty_name); |
| 196 | return -1; |
| 197 | } |
| 198 | |
| 199 | if (cfsetspeed(&term_options, speed) < 0) { |
| 200 | warn("Couldn't set speeds for %s", tty_name); |
| 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | if (tcsetattr(th->fd, TCSAFLUSH, &term_options) < 0) { |
| 205 | warn("Couldn't commit terminal options for %s", tty_name); |
| 206 | return -1; |
| 207 | } |
| 208 | printf("Set %s terminal baud rate to %s\n", tty_name, desired_baud); |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
Xo Wang | 81408bd | 2017-04-18 16:43:07 -0700 | [diff] [blame] | 213 | static int make_terminal_raw(struct tty_handler *th, const char *tty_name) { |
| 214 | struct termios term_options; |
| 215 | |
| 216 | if (tcgetattr(th->fd, &term_options) < 0) { |
| 217 | warn("Can't get config for %s", tty_name); |
| 218 | return -1; |
| 219 | } |
| 220 | |
| 221 | /* Disable various input and output processing including character |
| 222 | * translation, line edit (canonical) mode, flow control, and special signal |
| 223 | * generating characters. */ |
| 224 | cfmakeraw(&term_options); |
| 225 | |
| 226 | if (tcsetattr(th->fd, TCSAFLUSH, &term_options) < 0) { |
| 227 | warn("Couldn't commit terminal options for %s", tty_name); |
| 228 | return -1; |
| 229 | } |
| 230 | printf("Set %s for raw byte handling\n", tty_name); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 235 | static int tty_init(struct handler *handler, struct console *console, |
| 236 | struct config *config __attribute__((unused))) |
| 237 | { |
| 238 | struct tty_handler *th = to_tty_handler(handler); |
| 239 | const char *tty_name; |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 240 | const char *tty_baud; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 241 | char *tty_path; |
Jeremy Kerr | bc506fd | 2017-02-07 09:24:48 +0800 | [diff] [blame] | 242 | int rc; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 243 | |
| 244 | tty_name = config_get_value(config, "local-tty"); |
| 245 | if (!tty_name) |
| 246 | return -1; |
| 247 | |
| 248 | rc = asprintf(&tty_path, "/dev/%s", tty_name); |
| 249 | if (!rc) |
| 250 | return -1; |
| 251 | |
Jeremy Kerr | bc506fd | 2017-02-07 09:24:48 +0800 | [diff] [blame] | 252 | th->fd = open(tty_path, O_RDWR | O_NONBLOCK); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 253 | if (th->fd < 0) { |
| 254 | warn("Can't open %s; disabling local tty", tty_name); |
| 255 | free(tty_path); |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | free(tty_path); |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 260 | th->fd_flags = fcntl(th->fd, F_GETFL, 0); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 261 | |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 262 | tty_baud = config_get_value(config, "local-tty-baud"); |
| 263 | if (tty_baud != NULL) |
| 264 | if (set_terminal_baud(th, tty_name, tty_baud) != 0) |
| 265 | fprintf(stderr, "Couldn't set baud rate for %s to %s\n", |
| 266 | tty_name, tty_baud); |
| 267 | |
Xo Wang | 81408bd | 2017-04-18 16:43:07 -0700 | [diff] [blame] | 268 | if (make_terminal_raw(th, tty_name) != 0) |
| 269 | fprintf(stderr, "Couldn't make %s a raw terminal\n", tty_name); |
| 270 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 271 | th->poller = console_poller_register(console, handler, tty_poll, |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 272 | th->fd, POLLIN, NULL); |
| 273 | th->console = console; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 274 | th->rbc = console_ringbuffer_consumer_register(console, |
| 275 | tty_ringbuffer_poll, th); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 280 | static void tty_fini(struct handler *handler) |
| 281 | { |
| 282 | struct tty_handler *th = to_tty_handler(handler); |
| 283 | if (th->poller) |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 284 | console_poller_unregister(th->console, th->poller); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 285 | close(th->fd); |
| 286 | } |
| 287 | |
| 288 | static struct tty_handler tty_handler = { |
| 289 | .handler = { |
| 290 | .name = "tty", |
| 291 | .init = tty_init, |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 292 | .fini = tty_fini, |
| 293 | }, |
| 294 | }; |
| 295 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 296 | console_handler_register(&tty_handler.handler); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 297 | |