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