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 | |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 17 | #include <assert.h> |
| 18 | #include <err.h> |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 19 | #include <errno.h> |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 20 | #include <fcntl.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 23 | #include <string.h> |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 24 | #include <unistd.h> |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 25 | #include <termios.h> |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 26 | |
| 27 | #include "console-server.h" |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 28 | #include "config.h" |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 29 | |
| 30 | struct tty_handler { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 31 | struct handler handler; |
| 32 | struct console *console; |
| 33 | struct ringbuffer_consumer *rbc; |
| 34 | struct poller *poller; |
| 35 | int fd; |
| 36 | int fd_flags; |
| 37 | bool blocked; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | static struct tty_handler *to_tty_handler(struct handler *handler) |
| 41 | { |
| 42 | return container_of(handler, struct tty_handler, handler); |
| 43 | } |
| 44 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 45 | static void tty_set_fd_blocking(struct tty_handler *th, bool fd_blocking) |
| 46 | { |
| 47 | int flags; |
| 48 | |
| 49 | flags = th->fd_flags & ~O_NONBLOCK; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 50 | if (!fd_blocking) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 51 | flags |= O_NONBLOCK; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 52 | } |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 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 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 73 | if (blocked == th->blocked) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 74 | return; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 75 | } |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 76 | |
| 77 | th->blocked = blocked; |
| 78 | events = POLLIN; |
| 79 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 80 | if (th->blocked) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 81 | events |= POLLOUT; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 82 | } |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 83 | |
| 84 | console_poller_set_events(th->console, th->poller, events); |
| 85 | } |
| 86 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 87 | static int tty_drain_queue(struct tty_handler *th, size_t force_len) |
| 88 | { |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 89 | size_t len; |
| 90 | size_t total_len; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 91 | ssize_t wlen; |
| 92 | uint8_t *buf; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 93 | |
| 94 | /* if we're forcing data, we need to clear non-blocking mode */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 95 | if (force_len) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 96 | tty_set_fd_blocking(th, true); |
| 97 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 98 | /* no point writing, we'll just see -EAGAIN */ |
| 99 | } else if (th->blocked) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 100 | return 0; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 101 | } |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 102 | |
| 103 | total_len = 0; |
| 104 | |
| 105 | for (;;) { |
| 106 | len = ringbuffer_dequeue_peek(th->rbc, total_len, &buf); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 107 | if (!len) { |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 108 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 109 | } |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 110 | |
| 111 | /* write as little as possible while blocking */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 112 | if (force_len && force_len < total_len + len) { |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 113 | len = force_len - total_len; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 114 | } |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 115 | |
| 116 | wlen = write(th->fd, buf, len); |
| 117 | if (wlen < 0) { |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 118 | if (errno == EINTR) { |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 119 | continue; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 120 | } |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 121 | if ((errno == EAGAIN || errno == EWOULDBLOCK) && |
| 122 | !force_len) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 123 | tty_set_blocked(th, true); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 124 | break; |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 125 | } |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 126 | warn("failed writing to local tty; disabling"); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | total_len += wlen; |
| 131 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 132 | if (force_len && total_len >= force_len) { |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 133 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 134 | } |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | ringbuffer_dequeue_commit(th->rbc, total_len); |
| 138 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 139 | if (force_len) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 140 | tty_set_fd_blocking(th, false); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 141 | } |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static enum ringbuffer_poll_ret tty_ringbuffer_poll(void *arg, size_t force_len) |
| 147 | { |
| 148 | struct tty_handler *th = arg; |
| 149 | int rc; |
| 150 | |
| 151 | rc = tty_drain_queue(th, force_len); |
| 152 | if (rc) { |
| 153 | console_poller_unregister(th->console, th->poller); |
| 154 | return RINGBUFFER_POLL_REMOVE; |
| 155 | } |
| 156 | |
| 157 | return RINGBUFFER_POLL_OK; |
| 158 | } |
| 159 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 160 | static enum poller_ret tty_poll(struct handler *handler, int events, |
| 161 | void __attribute__((unused)) * data) |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 162 | { |
| 163 | struct tty_handler *th = to_tty_handler(handler); |
| 164 | uint8_t buf[4096]; |
| 165 | ssize_t len; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 166 | int rc; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 167 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 168 | if (events & POLLIN) { |
| 169 | len = read(th->fd, buf, sizeof(buf)); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 170 | if (len <= 0) { |
Jeremy Kerr | 67eab04 | 2017-07-12 12:08:35 +0800 | [diff] [blame] | 171 | goto err; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 172 | } |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 173 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 174 | console_data_out(th->console, buf, len); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 175 | } |
| 176 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 177 | if (events & POLLOUT) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 178 | tty_set_blocked(th, false); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 179 | rc = tty_drain_queue(th, 0); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 180 | if (rc) { |
Jeremy Kerr | 67eab04 | 2017-07-12 12:08:35 +0800 | [diff] [blame] | 181 | goto err; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 182 | } |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 183 | } |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 184 | |
| 185 | return POLLER_OK; |
Jeremy Kerr | 67eab04 | 2017-07-12 12:08:35 +0800 | [diff] [blame] | 186 | |
| 187 | err: |
| 188 | th->poller = NULL; |
| 189 | close(th->fd); |
| 190 | ringbuffer_consumer_unregister(th->rbc); |
| 191 | return POLLER_REMOVE; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 192 | } |
| 193 | |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 194 | static int set_terminal_baud(struct tty_handler *th, const char *tty_name, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 195 | speed_t speed) |
| 196 | { |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 197 | struct termios term_options; |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 198 | |
| 199 | if (tcgetattr(th->fd, &term_options) < 0) { |
| 200 | warn("Can't get config for %s", tty_name); |
| 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | if (cfsetspeed(&term_options, speed) < 0) { |
| 205 | warn("Couldn't set speeds for %s", tty_name); |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | if (tcsetattr(th->fd, TCSAFLUSH, &term_options) < 0) { |
| 210 | warn("Couldn't commit terminal options for %s", tty_name); |
| 211 | return -1; |
| 212 | } |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 213 | |
| 214 | return 0; |
| 215 | } |
| 216 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 217 | static int make_terminal_raw(struct tty_handler *th, const char *tty_name) |
| 218 | { |
Xo Wang | 81408bd | 2017-04-18 16:43:07 -0700 | [diff] [blame] | 219 | struct termios term_options; |
| 220 | |
| 221 | if (tcgetattr(th->fd, &term_options) < 0) { |
| 222 | warn("Can't get config for %s", tty_name); |
| 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | /* Disable various input and output processing including character |
| 227 | * translation, line edit (canonical) mode, flow control, and special signal |
| 228 | * generating characters. */ |
| 229 | cfmakeraw(&term_options); |
| 230 | |
| 231 | if (tcsetattr(th->fd, TCSAFLUSH, &term_options) < 0) { |
| 232 | warn("Couldn't commit terminal options for %s", tty_name); |
| 233 | return -1; |
| 234 | } |
| 235 | printf("Set %s for raw byte handling\n", tty_name); |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 240 | static struct handler *tty_init(const struct handler_type *type |
| 241 | __attribute__((unused)), |
| 242 | struct console *console, |
| 243 | struct config *config __attribute__((unused))) |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 244 | { |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 245 | struct tty_handler *th; |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 246 | speed_t desired_speed; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 247 | const char *tty_name; |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 248 | const char *tty_baud; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 249 | char *tty_path; |
Jeremy Kerr | bc506fd | 2017-02-07 09:24:48 +0800 | [diff] [blame] | 250 | int rc; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 251 | |
| 252 | tty_name = config_get_value(config, "local-tty"); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 253 | if (!tty_name) { |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 254 | return NULL; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 255 | } |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 256 | |
| 257 | rc = asprintf(&tty_path, "/dev/%s", tty_name); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 258 | if (!rc) { |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 259 | return NULL; |
| 260 | } |
| 261 | |
| 262 | th = malloc(sizeof(*th)); |
| 263 | if (!th) { |
| 264 | return NULL; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 265 | } |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 266 | |
Jeremy Kerr | bc506fd | 2017-02-07 09:24:48 +0800 | [diff] [blame] | 267 | th->fd = open(tty_path, O_RDWR | O_NONBLOCK); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 268 | if (th->fd < 0) { |
| 269 | warn("Can't open %s; disabling local tty", tty_name); |
| 270 | free(tty_path); |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 271 | free(th); |
| 272 | return NULL; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | free(tty_path); |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 276 | th->fd_flags = fcntl(th->fd, F_GETFL, 0); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 277 | |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 278 | tty_baud = config_get_value(config, "local-tty-baud"); |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 279 | if (tty_baud != NULL) { |
| 280 | rc = config_parse_baud(&desired_speed, tty_baud); |
| 281 | if (rc) { |
| 282 | fprintf(stderr, "%s is not a valid baud rate\n", |
| 283 | tty_baud); |
| 284 | } else { |
| 285 | rc = set_terminal_baud(th, tty_name, desired_speed); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 286 | if (rc) { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 287 | fprintf(stderr, |
| 288 | "Couldn't set baud rate for %s to %s\n", |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 289 | tty_name, tty_baud); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 290 | } |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 291 | } |
| 292 | } |
Xo Wang | c5ef8ea | 2017-04-17 16:20:43 -0700 | [diff] [blame] | 293 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 294 | if (make_terminal_raw(th, tty_name) != 0) { |
Xo Wang | 81408bd | 2017-04-18 16:43:07 -0700 | [diff] [blame] | 295 | fprintf(stderr, "Couldn't make %s a raw terminal\n", tty_name); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 296 | } |
Xo Wang | 81408bd | 2017-04-18 16:43:07 -0700 | [diff] [blame] | 297 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 298 | th->poller = console_poller_register(console, &th->handler, tty_poll, |
| 299 | NULL, th->fd, POLLIN, NULL); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 300 | th->console = console; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 301 | th->rbc = console_ringbuffer_consumer_register(console, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 302 | tty_ringbuffer_poll, th); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 303 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 304 | return &th->handler; |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 305 | } |
| 306 | |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 307 | static void tty_fini(struct handler *handler) |
| 308 | { |
| 309 | struct tty_handler *th = to_tty_handler(handler); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 310 | if (th->poller) { |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 311 | console_poller_unregister(th->console, th->poller); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 312 | } |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 313 | close(th->fd); |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 314 | free(th); |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 315 | } |
| 316 | |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 317 | static int tty_baudrate(struct handler *handler, speed_t baudrate) |
| 318 | { |
| 319 | const char *tty_name = "local-tty"; |
| 320 | struct tty_handler *th = to_tty_handler(handler); |
| 321 | |
| 322 | if (baudrate == 0) { |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | if (set_terminal_baud(th, tty_name, baudrate) != 0) { |
| 327 | fprintf(stderr, "Couldn't set baud rate for %s to %d\n", |
| 328 | tty_name, baudrate); |
| 329 | return -1; |
| 330 | } |
| 331 | return 0; |
| 332 | } |
| 333 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 334 | static const struct handler_type tty_handler = { |
| 335 | .name = "tty", |
| 336 | .init = tty_init, |
| 337 | .fini = tty_fini, |
| 338 | .baudrate = tty_baudrate, |
Jeremy Kerr | bc1e893 | 2016-04-28 12:27:30 +0800 | [diff] [blame] | 339 | }; |
| 340 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 341 | console_handler_register(&tty_handler); |