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, |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 184 | speed_t speed) { |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 185 | struct termios term_options; |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 186 | |
| 187 | if (tcgetattr(th->fd, &term_options) < 0) { |
| 188 | warn("Can't get config for %s", tty_name); |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | if (cfsetspeed(&term_options, speed) < 0) { |
| 193 | warn("Couldn't set speeds for %s", tty_name); |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | if (tcsetattr(th->fd, TCSAFLUSH, &term_options) < 0) { |
| 198 | warn("Couldn't commit terminal options for %s", tty_name); |
| 199 | return -1; |
| 200 | } |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
Xo Wang | 81408bd | 2017-04-18 16:43:07 -0700 | [diff] [blame] | 205 | static int make_terminal_raw(struct tty_handler *th, const char *tty_name) { |
| 206 | struct termios term_options; |
| 207 | |
| 208 | if (tcgetattr(th->fd, &term_options) < 0) { |
| 209 | warn("Can't get config for %s", tty_name); |
| 210 | return -1; |
| 211 | } |
| 212 | |
| 213 | /* Disable various input and output processing including character |
| 214 | * translation, line edit (canonical) mode, flow control, and special signal |
| 215 | * generating characters. */ |
| 216 | cfmakeraw(&term_options); |
| 217 | |
| 218 | if (tcsetattr(th->fd, TCSAFLUSH, &term_options) < 0) { |
| 219 | warn("Couldn't commit terminal options for %s", tty_name); |
| 220 | return -1; |
| 221 | } |
| 222 | printf("Set %s for raw byte handling\n", tty_name); |
| 223 | |
| 224 | return 0; |
| 225 | } |
| 226 | |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 227 | static int tty_init(struct handler *handler, struct console *console, |
| 228 | struct config *config __attribute__((unused))) |
| 229 | { |
| 230 | struct tty_handler *th = to_tty_handler(handler); |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 231 | speed_t desired_speed; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 232 | const char *tty_name; |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 233 | const char *tty_baud; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 234 | char *tty_path; |
Jeremy Kerr | bc506fd | 2017-02-07 09:24:48 +0800 | [diff] [blame] | 235 | int rc; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 236 | |
| 237 | tty_name = config_get_value(config, "local-tty"); |
| 238 | if (!tty_name) |
| 239 | return -1; |
| 240 | |
| 241 | rc = asprintf(&tty_path, "/dev/%s", tty_name); |
| 242 | if (!rc) |
| 243 | return -1; |
| 244 | |
Jeremy Kerr | bc506fd | 2017-02-07 09:24:48 +0800 | [diff] [blame] | 245 | th->fd = open(tty_path, O_RDWR | O_NONBLOCK); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 246 | if (th->fd < 0) { |
| 247 | warn("Can't open %s; disabling local tty", tty_name); |
| 248 | free(tty_path); |
| 249 | return -1; |
| 250 | } |
| 251 | |
| 252 | free(tty_path); |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 253 | th->fd_flags = fcntl(th->fd, F_GETFL, 0); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 254 | |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 255 | tty_baud = config_get_value(config, "local-tty-baud"); |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 256 | if (tty_baud != NULL) { |
| 257 | rc = config_parse_baud(&desired_speed, tty_baud); |
| 258 | if (rc) { |
| 259 | fprintf(stderr, "%s is not a valid baud rate\n", |
| 260 | tty_baud); |
| 261 | } else { |
| 262 | rc = set_terminal_baud(th, tty_name, desired_speed); |
| 263 | if (rc) |
| 264 | fprintf(stderr, "Couldn't set baud rate for %s to %s\n", |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 265 | tty_name, tty_baud); |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 266 | } |
| 267 | } |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 268 | |
Xo Wang | 81408bd | 2017-04-18 16:43:07 -0700 | [diff] [blame] | 269 | if (make_terminal_raw(th, tty_name) != 0) |
| 270 | fprintf(stderr, "Couldn't make %s a raw terminal\n", tty_name); |
| 271 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 272 | th->poller = console_poller_register(console, handler, tty_poll, NULL, |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 273 | th->fd, POLLIN, NULL); |
| 274 | th->console = console; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 275 | th->rbc = console_ringbuffer_consumer_register(console, |
| 276 | tty_ringbuffer_poll, th); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 281 | static void tty_fini(struct handler *handler) |
| 282 | { |
| 283 | struct tty_handler *th = to_tty_handler(handler); |
| 284 | if (th->poller) |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 285 | console_poller_unregister(th->console, th->poller); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 286 | close(th->fd); |
| 287 | } |
| 288 | |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 289 | static int tty_baudrate(struct handler *handler, speed_t baudrate) |
| 290 | { |
| 291 | const char *tty_name = "local-tty"; |
| 292 | struct tty_handler *th = to_tty_handler(handler); |
| 293 | |
| 294 | if (baudrate == 0) { |
| 295 | return -1; |
| 296 | } |
| 297 | |
| 298 | if (set_terminal_baud(th, tty_name, baudrate) != 0) { |
| 299 | fprintf(stderr, "Couldn't set baud rate for %s to %d\n", |
| 300 | tty_name, baudrate); |
| 301 | return -1; |
| 302 | } |
| 303 | return 0; |
| 304 | } |
| 305 | |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 306 | static struct tty_handler tty_handler = { |
| 307 | .handler = { |
| 308 | .name = "tty", |
| 309 | .init = tty_init, |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 310 | .fini = tty_fini, |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 311 | .baudrate = tty_baudrate, |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 312 | }, |
| 313 | }; |
| 314 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 315 | console_handler_register(&tty_handler.handler); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 316 | |