Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1 | /** |
| 2 | * Console server process for OpenBMC |
| 3 | * |
Jeremy Kerr | 9326d77 | 2016-03-17 17:15:02 +0800 | [diff] [blame] | 4 | * Copyright © 2016 IBM Corporation |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 17 | */ |
| 18 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 19 | #include <assert.h> |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <signal.h> |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <stdio.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <unistd.h> |
| 28 | #include <err.h> |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 29 | #include <string.h> |
| 30 | #include <getopt.h> |
Zev Weiss | 7dc08ba | 2023-09-12 20:49:14 -0700 | [diff] [blame] | 31 | #include <glob.h> |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 32 | #include <limits.h> |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 33 | #include <time.h> |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 34 | #include <termios.h> |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 35 | |
| 36 | #include <sys/types.h> |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 37 | #include <sys/time.h> |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 38 | #include <sys/socket.h> |
Joel Stanley | 87e344c | 2016-09-01 00:00:51 +0930 | [diff] [blame] | 39 | #include <poll.h> |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 40 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 41 | #include "console-server.h" |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 42 | #include "config.h" |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 43 | |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 44 | #define DEV_PTS_PATH "/dev/pts" |
| 45 | |
Medicine Yeh | 7f2bfb9 | 2024-03-10 23:19:41 -0700 | [diff] [blame] | 46 | /* default size of the shared backlog ringbuffer */ |
| 47 | const size_t default_buffer_size = 128ul * 1024ul; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 48 | |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 49 | /* state shared with the signal handler */ |
Andrew Jeffery | 553cb66 | 2024-07-09 15:14:56 +0930 | [diff] [blame] | 50 | static volatile sig_atomic_t sigint; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 51 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 52 | static void usage(const char *progname) |
| 53 | { |
| 54 | fprintf(stderr, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 55 | "usage: %s [options] <DEVICE>\n" |
| 56 | "\n" |
| 57 | "Options:\n" |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 58 | " --config <FILE>\tUse FILE for configuration\n" |
| 59 | " --console-id <NAME>\tUse NAME in the UNIX domain socket address\n" |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 60 | "", |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 61 | progname); |
| 62 | } |
| 63 | |
Alexander Hansen | 2325d5d | 2024-06-07 16:54:20 +0200 | [diff] [blame] | 64 | static bool console_server_pollfd_reclaimable(struct pollfd *p) |
| 65 | { |
| 66 | return p->fd == -1 && p->events == 0 && p->revents == ~0; |
| 67 | } |
| 68 | |
| 69 | static ssize_t |
| 70 | console_server_find_released_pollfd(struct console_server *server) |
| 71 | { |
| 72 | for (size_t i = 0; i < server->capacity_pollfds; i++) { |
| 73 | struct pollfd *p = &server->pollfds[i]; |
| 74 | if (console_server_pollfd_reclaimable(p)) { |
| 75 | return (ssize_t)i; |
| 76 | } |
| 77 | } |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | // returns the index of that pollfd in server->pollfds |
| 82 | // we cannot return a pointer because 'realloc' may move server->pollfds |
| 83 | ssize_t console_server_request_pollfd(struct console_server *server, int fd, |
| 84 | short int events) |
| 85 | { |
| 86 | ssize_t index; |
| 87 | struct pollfd *pollfd; |
| 88 | |
| 89 | index = console_server_find_released_pollfd(server); |
| 90 | |
| 91 | if (index < 0) { |
| 92 | const size_t newcap = server->capacity_pollfds + 1; |
| 93 | |
| 94 | struct pollfd *newarr = reallocarray(server->pollfds, newcap, |
| 95 | sizeof(struct pollfd)); |
| 96 | if (newarr == NULL) { |
| 97 | return -1; |
| 98 | } |
| 99 | server->pollfds = newarr; |
| 100 | |
| 101 | index = (ssize_t)server->capacity_pollfds; |
| 102 | |
| 103 | server->capacity_pollfds = newcap; |
| 104 | } |
| 105 | |
| 106 | pollfd = &server->pollfds[index]; |
| 107 | pollfd->fd = fd; |
| 108 | pollfd->events = events; |
| 109 | pollfd->revents = 0; |
| 110 | |
| 111 | return index; |
| 112 | } |
| 113 | |
| 114 | int console_server_release_pollfd(struct console_server *server, |
| 115 | size_t pollfd_index) |
| 116 | { |
| 117 | if (pollfd_index >= server->capacity_pollfds) { |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | struct pollfd *pfd = &server->pollfds[pollfd_index]; |
| 122 | |
| 123 | // mark pollfd as reclaimable |
| 124 | |
| 125 | // ignore this file descriptor when calling 'poll' |
| 126 | // https://www.man7.org/linux/man-pages/man2/poll.2.html |
| 127 | pfd->fd = -1; |
| 128 | pfd->events = 0; |
| 129 | pfd->revents = ~0; |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 134 | /* populates server->tty.dev and server->tty.sysfs_devnode, using the tty kernel name */ |
| 135 | static int tty_find_device(struct console_server *server) |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 136 | { |
Andrew Jeffery | d3cb9c2 | 2023-05-03 17:13:50 +0930 | [diff] [blame] | 137 | char *tty_class_device_link = NULL; |
| 138 | char *tty_path_input_real = NULL; |
| 139 | char *tty_device_tty_dir = NULL; |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 140 | char *tty_vuart_lpc_addr = NULL; |
Andrew Jeffery | d3cb9c2 | 2023-05-03 17:13:50 +0930 | [diff] [blame] | 141 | char *tty_device_reldir = NULL; |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 142 | char *tty_sysfs_devnode = NULL; |
Andrew Jeffery | d3cb9c2 | 2023-05-03 17:13:50 +0930 | [diff] [blame] | 143 | char *tty_kname_real = NULL; |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 144 | char *tty_path_input = NULL; |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 145 | int rc; |
| 146 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 147 | server->tty.type = TTY_DEVICE_UNDEFINED; |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 148 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 149 | assert(server->tty.kname); |
| 150 | if (!strlen(server->tty.kname)) { |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 151 | warnx("TTY kname must not be empty"); |
| 152 | rc = -1; |
| 153 | goto out_free; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 154 | } |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 155 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 156 | if (server->tty.kname[0] == '/') { |
| 157 | tty_path_input = strdup(server->tty.kname); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 158 | if (!tty_path_input) { |
| 159 | rc = -1; |
| 160 | goto out_free; |
| 161 | } |
| 162 | } else { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 163 | rc = asprintf(&tty_path_input, "/dev/%s", server->tty.kname); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 164 | if (rc < 0) { |
| 165 | goto out_free; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /* udev may rename the tty name with a symbol link, try to resolve */ |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 170 | tty_path_input_real = realpath(tty_path_input, NULL); |
| 171 | if (!tty_path_input_real) { |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 172 | warn("Can't find realpath for %s", tty_path_input); |
Andrew Jeffery | 15792aa | 2023-04-18 16:47:33 +0930 | [diff] [blame] | 173 | rc = -1; |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 174 | goto out_free; |
| 175 | } |
| 176 | |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 177 | /* |
| 178 | * Allow hooking obmc-console-server up to PTYs for testing |
| 179 | * |
| 180 | * https://amboar.github.io/notes/2023/05/02/testing-obmc-console-with-socat.html |
| 181 | */ |
| 182 | if (!strncmp(DEV_PTS_PATH, tty_path_input_real, strlen(DEV_PTS_PATH))) { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 183 | server->tty.type = TTY_DEVICE_PTY; |
| 184 | server->tty.dev = strdup(server->tty.kname); |
| 185 | rc = server->tty.dev ? 0 : -1; |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 186 | goto out_free; |
| 187 | } |
| 188 | |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 189 | tty_kname_real = basename(tty_path_input_real); |
| 190 | if (!tty_kname_real) { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 191 | warn("Can't find real name for %s", server->tty.kname); |
Andrew Jeffery | 15792aa | 2023-04-18 16:47:33 +0930 | [diff] [blame] | 192 | rc = -1; |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 193 | goto out_free; |
| 194 | } |
| 195 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 196 | rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s", |
| 197 | tty_kname_real); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 198 | if (rc < 0) { |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 199 | goto out_free; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 200 | } |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 201 | |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 202 | tty_device_tty_dir = realpath(tty_class_device_link, NULL); |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 203 | if (!tty_device_tty_dir) { |
| 204 | warn("Can't query sysfs for device %s", tty_kname_real); |
Andrew Jeffery | 15792aa | 2023-04-18 16:47:33 +0930 | [diff] [blame] | 205 | rc = -1; |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 206 | goto out_free; |
| 207 | } |
| 208 | |
| 209 | rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 210 | if (rc < 0) { |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 211 | goto out_free; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 212 | } |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 213 | |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 214 | tty_sysfs_devnode = realpath(tty_device_reldir, NULL); |
| 215 | if (!tty_sysfs_devnode) { |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 216 | warn("Can't find parent device for %s", tty_kname_real); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 217 | } |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 218 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 219 | rc = asprintf(&server->tty.dev, "/dev/%s", tty_kname_real); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 220 | if (rc < 0) { |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 221 | goto out_free; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 222 | } |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 223 | |
Oskar Senft | 955d140 | 2023-09-27 22:43:22 -0400 | [diff] [blame] | 224 | // Default to non-VUART |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 225 | server->tty.type = TTY_DEVICE_UART; |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 226 | |
Oskar Senft | 955d140 | 2023-09-27 22:43:22 -0400 | [diff] [blame] | 227 | /* Arbitrarily pick an attribute to differentiate UART vs VUART */ |
| 228 | if (tty_sysfs_devnode) { |
| 229 | rc = asprintf(&tty_vuart_lpc_addr, "%s/lpc_address", |
| 230 | tty_sysfs_devnode); |
| 231 | if (rc < 0) { |
| 232 | goto out_free; |
| 233 | } |
| 234 | |
| 235 | rc = access(tty_vuart_lpc_addr, F_OK); |
| 236 | if (!rc) { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 237 | server->tty.type = TTY_DEVICE_VUART; |
| 238 | server->tty.vuart.sysfs_devnode = |
Oskar Senft | 955d140 | 2023-09-27 22:43:22 -0400 | [diff] [blame] | 239 | strdup(tty_sysfs_devnode); |
| 240 | } |
| 241 | } |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 242 | |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 243 | rc = 0; |
| 244 | |
| 245 | out_free: |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 246 | free(tty_vuart_lpc_addr); |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 247 | free(tty_class_device_link); |
Andrew Jeffery | 982090d | 2024-07-09 17:31:27 +0930 | [diff] [blame] | 248 | free(tty_sysfs_devnode); |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 249 | free(tty_device_tty_dir); |
| 250 | free(tty_device_reldir); |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 251 | free(tty_path_input); |
| 252 | free(tty_path_input_real); |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 253 | return rc; |
| 254 | } |
| 255 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 256 | static int tty_set_sysfs_attr(struct console_server *server, const char *name, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 257 | int value) |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 258 | { |
| 259 | char *path; |
| 260 | FILE *fp; |
| 261 | int rc; |
| 262 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 263 | assert(server->tty.type == TTY_DEVICE_VUART); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 264 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 265 | if (!server->tty.vuart.sysfs_devnode) { |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 266 | return -1; |
| 267 | } |
| 268 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 269 | rc = asprintf(&path, "%s/%s", server->tty.vuart.sysfs_devnode, name); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 270 | if (rc < 0) { |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 271 | return -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 272 | } |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 273 | |
| 274 | fp = fopen(path, "w"); |
| 275 | if (!fp) { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 276 | warn("Can't access attribute %s on device %s", name, |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 277 | server->tty.kname); |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 278 | rc = -1; |
| 279 | goto out_free; |
| 280 | } |
| 281 | setvbuf(fp, NULL, _IONBF, 0); |
| 282 | |
| 283 | rc = fprintf(fp, "0x%x", value); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 284 | if (rc < 0) { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 285 | warn("Error writing to %s attribute of device %s", name, |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 286 | server->tty.kname); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 287 | } |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 288 | fclose(fp); |
| 289 | |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 290 | out_free: |
| 291 | free(path); |
| 292 | return rc; |
| 293 | } |
| 294 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 295 | /** |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 296 | * Set termios attributes on the console tty. |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 297 | */ |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 298 | void tty_init_termios(struct console_server *server) |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 299 | { |
| 300 | struct termios termios; |
| 301 | int rc; |
| 302 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 303 | rc = tcgetattr(server->tty.fd, &termios); |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 304 | if (rc) { |
| 305 | warn("Can't read tty termios"); |
| 306 | return; |
| 307 | } |
| 308 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 309 | if (server->tty.type == TTY_DEVICE_UART && server->tty.uart.baud) { |
| 310 | if (cfsetspeed(&termios, server->tty.uart.baud) < 0) { |
| 311 | warn("Couldn't set speeds for %s", server->tty.kname); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 312 | } |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* Set console to raw mode: we don't want any processing to occur on |
| 316 | * the underlying terminal input/output. |
| 317 | */ |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 318 | cfmakeraw(&termios); |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 319 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 320 | rc = tcsetattr(server->tty.fd, TCSANOW, &termios); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 321 | if (rc) { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 322 | warn("Can't set terminal options for %s", server->tty.kname); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 323 | } |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /** |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 327 | * Open and initialise the serial device |
| 328 | */ |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 329 | static void tty_init_vuart_io(struct console_server *server) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 330 | { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 331 | assert(server->tty.type == TTY_DEVICE_VUART); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 332 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 333 | if (server->tty.vuart.sirq) { |
| 334 | tty_set_sysfs_attr(server, "sirq", server->tty.vuart.sirq); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 335 | } |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 336 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 337 | if (server->tty.vuart.lpc_addr) { |
| 338 | tty_set_sysfs_attr(server, "lpc_address", |
| 339 | server->tty.vuart.lpc_addr); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 343 | static int tty_init_io(struct console_server *server) |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 344 | { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 345 | server->tty.fd = open(server->tty.dev, O_RDWR); |
| 346 | if (server->tty.fd <= 0) { |
| 347 | warn("Can't open tty %s", server->tty.dev); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 348 | return -1; |
| 349 | } |
| 350 | |
| 351 | /* Disable character delay. We may want to later enable this when |
| 352 | * we detect larger amounts of data |
| 353 | */ |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 354 | fcntl(server->tty.fd, F_SETFL, FNDELAY); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 355 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 356 | tty_init_termios(server); |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 357 | |
Alexander Hansen | 2325d5d | 2024-06-07 16:54:20 +0200 | [diff] [blame] | 358 | ssize_t index = |
| 359 | console_server_request_pollfd(server, server->tty.fd, POLLIN); |
| 360 | |
| 361 | if (index < 0) { |
| 362 | return -1; |
| 363 | } |
| 364 | |
| 365 | server->tty_pollfd_index = (size_t)index; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 366 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 367 | return 0; |
| 368 | } |
| 369 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 370 | static int tty_init_vuart(struct console_server *server, struct config *config) |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 371 | { |
Andrew Jeffery | fd883a8 | 2023-04-18 22:18:25 +0930 | [diff] [blame] | 372 | unsigned long parsed; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 373 | const char *val; |
| 374 | char *endp; |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 375 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 376 | assert(server->tty.type == TTY_DEVICE_VUART); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 377 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 378 | val = config_get_value(config, "lpc-address"); |
| 379 | if (val) { |
Andrew Jeffery | fd883a8 | 2023-04-18 22:18:25 +0930 | [diff] [blame] | 380 | errno = 0; |
| 381 | parsed = strtoul(val, &endp, 0); |
| 382 | if (parsed == ULONG_MAX && errno == ERANGE) { |
| 383 | warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'", |
| 384 | val); |
| 385 | return -1; |
| 386 | } |
| 387 | |
| 388 | if (parsed > UINT16_MAX) { |
| 389 | warn("Invalid LPC address '%s'", val); |
| 390 | return -1; |
| 391 | } |
| 392 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 393 | server->tty.vuart.lpc_addr = (uint16_t)parsed; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 394 | if (endp == optarg) { |
| 395 | warn("Invalid LPC address: '%s'", val); |
| 396 | return -1; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | val = config_get_value(config, "sirq"); |
| 401 | if (val) { |
Andrew Jeffery | fd883a8 | 2023-04-18 22:18:25 +0930 | [diff] [blame] | 402 | errno = 0; |
| 403 | parsed = strtoul(val, &endp, 0); |
| 404 | if (parsed == ULONG_MAX && errno == ERANGE) { |
| 405 | warn("Cannot interpret 'sirq' value as an unsigned long: '%s'", |
| 406 | val); |
| 407 | } |
| 408 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 409 | if (parsed > 16) { |
Andrew Jeffery | fd883a8 | 2023-04-18 22:18:25 +0930 | [diff] [blame] | 410 | warn("Invalid LPC SERIRQ: '%s'", val); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 411 | } |
Andrew Jeffery | fd883a8 | 2023-04-18 22:18:25 +0930 | [diff] [blame] | 412 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 413 | server->tty.vuart.sirq = (int)parsed; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 414 | if (endp == optarg) { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 415 | warn("Invalid sirq: '%s'", val); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 416 | } |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 417 | } |
| 418 | |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 422 | static int tty_init(struct console_server *server, struct config *config, |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 423 | const char *tty_arg) |
| 424 | { |
| 425 | const char *val; |
| 426 | int rc; |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 427 | |
Andrew Jeffery | d769eec | 2023-05-03 20:00:09 +0930 | [diff] [blame] | 428 | if (tty_arg) { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 429 | server->tty.kname = tty_arg; |
Andrew Jeffery | d769eec | 2023-05-03 20:00:09 +0930 | [diff] [blame] | 430 | } else if ((val = config_get_value(config, "upstream-tty"))) { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 431 | server->tty.kname = val; |
Andrew Jeffery | d769eec | 2023-05-03 20:00:09 +0930 | [diff] [blame] | 432 | } else { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 433 | warnx("Error: No TTY device specified"); |
| 434 | return -1; |
| 435 | } |
| 436 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 437 | rc = tty_find_device(server); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 438 | if (rc) { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 439 | return rc; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 440 | } |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 441 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 442 | switch (server->tty.type) { |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 443 | case TTY_DEVICE_VUART: |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 444 | rc = tty_init_vuart(server, config); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 445 | if (rc) { |
| 446 | return rc; |
| 447 | } |
| 448 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 449 | tty_init_vuart_io(server); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 450 | break; |
| 451 | case TTY_DEVICE_UART: |
| 452 | val = config_get_value(config, "baud"); |
| 453 | if (val) { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 454 | if (config_parse_baud(&server->tty.uart.baud, val)) { |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 455 | warnx("Invalid baud rate: '%s'", val); |
| 456 | } |
| 457 | } |
| 458 | break; |
| 459 | case TTY_DEVICE_PTY: |
| 460 | break; |
| 461 | case TTY_DEVICE_UNDEFINED: |
| 462 | default: |
| 463 | warnx("Cannot configure unrecognised TTY device"); |
| 464 | return -1; |
| 465 | } |
| 466 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 467 | return tty_init_io(server); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 468 | } |
| 469 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 470 | static void tty_fini(struct console_server *server) |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 471 | { |
Alexander Hansen | 2325d5d | 2024-06-07 16:54:20 +0200 | [diff] [blame] | 472 | if (server->tty_pollfd_index < server->capacity_pollfds) { |
| 473 | console_server_release_pollfd(server, server->tty_pollfd_index); |
| 474 | server->tty_pollfd_index = SIZE_MAX; |
| 475 | } |
| 476 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 477 | if (server->tty.type == TTY_DEVICE_VUART) { |
| 478 | free(server->tty.vuart.sysfs_devnode); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 479 | } |
Alexander Hansen | 2325d5d | 2024-06-07 16:54:20 +0200 | [diff] [blame] | 480 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 481 | free(server->tty.dev); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 482 | } |
| 483 | |
Zev Weiss | 7dc08ba | 2023-09-12 20:49:14 -0700 | [diff] [blame] | 484 | static int write_to_path(const char *path, const char *data) |
| 485 | { |
| 486 | int rc = 0; |
| 487 | FILE *f = fopen(path, "w"); |
| 488 | if (!f) { |
| 489 | return -1; |
| 490 | } |
| 491 | |
| 492 | if (fprintf(f, "%s", data) < 0) { |
| 493 | rc = -1; |
| 494 | } |
| 495 | |
| 496 | if (fclose(f)) { |
| 497 | rc = -1; |
| 498 | } |
| 499 | |
| 500 | return rc; |
| 501 | } |
| 502 | |
| 503 | #define ASPEED_UART_ROUTING_PATTERN \ |
| 504 | "/sys/bus/platform/drivers/aspeed-uart-routing/*.uart-routing" |
| 505 | |
| 506 | static void uart_routing_init(struct config *config) |
| 507 | { |
| 508 | const char *muxcfg; |
| 509 | const char *p; |
| 510 | size_t buflen; |
| 511 | char *sink; |
| 512 | char *source; |
| 513 | char *muxdir; |
| 514 | char *path; |
| 515 | glob_t globbuf; |
| 516 | |
| 517 | muxcfg = config_get_value(config, "aspeed-uart-routing"); |
| 518 | if (!muxcfg) { |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | /* Find the driver's sysfs directory */ |
| 523 | if (glob(ASPEED_UART_ROUTING_PATTERN, GLOB_ERR | GLOB_NOSORT, NULL, |
| 524 | &globbuf) != 0) { |
| 525 | warn("Couldn't find uart-routing driver directory, cannot apply config"); |
| 526 | return; |
| 527 | } |
| 528 | if (globbuf.gl_pathc != 1) { |
| 529 | warnx("Found %zd uart-routing driver directories, cannot apply config", |
| 530 | globbuf.gl_pathc); |
| 531 | goto out_free_glob; |
| 532 | } |
| 533 | muxdir = globbuf.gl_pathv[0]; |
| 534 | |
| 535 | /* |
| 536 | * Rather than faff about tracking a bunch of separate buffer sizes, |
| 537 | * just use one (worst-case) size for all of them -- +2 for a trailing |
| 538 | * NUL and a '/' separator to construct the sysfs file path. |
| 539 | */ |
| 540 | buflen = strlen(muxdir) + strlen(muxcfg) + 2; |
| 541 | |
| 542 | sink = malloc(buflen); |
| 543 | source = malloc(buflen); |
| 544 | path = malloc(buflen); |
| 545 | if (!path || !sink || !source) { |
| 546 | warnx("Out of memory applying uart routing config"); |
| 547 | goto out_free_bufs; |
| 548 | } |
| 549 | |
| 550 | p = muxcfg; |
| 551 | while (*p) { |
| 552 | ssize_t bytes_scanned; |
| 553 | |
| 554 | if (sscanf(p, " %[^:/ \t]:%[^: \t] %zn", sink, source, |
| 555 | &bytes_scanned) != 2) { |
| 556 | warnx("Invalid syntax in aspeed uart config: '%s' not applied", |
| 557 | p); |
| 558 | break; |
| 559 | } |
| 560 | p += bytes_scanned; |
| 561 | |
| 562 | /* |
| 563 | * Check that the sink name looks reasonable before proceeding |
| 564 | * (there are other writable files in the same directory that |
| 565 | * we shouldn't be touching, such as 'driver_override' and |
| 566 | * 'uevent'). |
| 567 | */ |
| 568 | if (strncmp(sink, "io", strlen("io")) != 0 && |
| 569 | strncmp(sink, "uart", strlen("uart")) != 0) { |
| 570 | warnx("Skipping invalid uart routing name '%s' (must be ioN or uartN)", |
| 571 | sink); |
| 572 | continue; |
| 573 | } |
| 574 | |
| 575 | snprintf(path, buflen, "%s/%s", muxdir, sink); |
| 576 | if (write_to_path(path, source)) { |
| 577 | warn("Failed to apply uart-routing config '%s:%s'", |
| 578 | sink, source); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | out_free_bufs: |
| 583 | free(path); |
| 584 | free(source); |
| 585 | free(sink); |
| 586 | out_free_glob: |
| 587 | globfree(&globbuf); |
| 588 | } |
| 589 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 590 | int console_data_out(struct console *console, const uint8_t *data, size_t len) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 591 | { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 592 | return write_buf_to_fd(console->server->tty.fd, data, len); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 593 | } |
| 594 | |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 595 | /* Prepare a socket name */ |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 596 | static int set_socket_info(struct console *console, struct config *config, |
| 597 | const char *console_id) |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 598 | { |
| 599 | ssize_t len; |
| 600 | |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 601 | /* Get console id */ |
| 602 | console->console_id = config_resolve_console_id(config, console_id); |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 603 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 604 | /* Get the socket name/path */ |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 605 | len = console_socket_path(console->socket_name, console->console_id); |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 606 | if (len < 0) { |
| 607 | warn("Failed to set socket path: %s", strerror(errno)); |
| 608 | return EXIT_FAILURE; |
| 609 | } |
| 610 | |
| 611 | /* Socket name is not a null terminated string hence save the length */ |
| 612 | console->socket_name_len = len; |
| 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
Jeremy Kerr | d47963e | 2016-03-16 17:29:55 +0800 | [diff] [blame] | 617 | static void handlers_init(struct console *console, struct config *config) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 618 | { |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 619 | /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */ |
Andrew Jeffery | adedc33 | 2024-07-10 10:48:13 +0930 | [diff] [blame] | 620 | extern const struct handler_type *const __start_handlers[]; |
| 621 | extern const struct handler_type *const __stop_handlers[]; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 622 | /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */ |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 623 | size_t n_types; |
| 624 | int j = 0; |
| 625 | size_t i; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 626 | |
Andrew Jeffery | adedc33 | 2024-07-10 10:48:13 +0930 | [diff] [blame] | 627 | n_types = __stop_handlers - __start_handlers; |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 628 | console->handlers = calloc(n_types, sizeof(struct handler *)); |
| 629 | if (!console->handlers) { |
| 630 | err(EXIT_FAILURE, "malloc(handlers)"); |
| 631 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 632 | |
Andrew Jeffery | 079fc51 | 2024-07-09 16:51:56 +0930 | [diff] [blame] | 633 | printf("%zu handler type%s\n", n_types, n_types == 1 ? "" : "s"); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 634 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 635 | for (i = 0; i < n_types; i++) { |
Andrew Jeffery | adedc33 | 2024-07-10 10:48:13 +0930 | [diff] [blame] | 636 | const struct handler_type *type = __start_handlers[i]; |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 637 | struct handler *handler; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 638 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 639 | /* Should be picked up at build time by |
| 640 | * console_handler_register, but check anyway |
| 641 | */ |
| 642 | if (!type->init || !type->fini) { |
| 643 | errx(EXIT_FAILURE, |
| 644 | "invalid handler type %s: no init() / fini()", |
| 645 | type->name); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 646 | } |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 647 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 648 | handler = type->init(type, console, config); |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 649 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 650 | printf(" console '%s': handler %s [%sactive]\n", |
| 651 | console->console_id, type->name, handler ? "" : "in"); |
| 652 | |
| 653 | if (handler) { |
| 654 | handler->type = type; |
| 655 | console->handlers[j++] = handler; |
| 656 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 657 | } |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 658 | |
| 659 | console->n_handlers = j; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 660 | } |
| 661 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 662 | static void handlers_fini(struct console *console) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 663 | { |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 664 | struct handler *handler; |
| 665 | int i; |
| 666 | |
| 667 | for (i = 0; i < console->n_handlers; i++) { |
| 668 | handler = console->handlers[i]; |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 669 | handler->type->fini(handler); |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 670 | } |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 671 | |
| 672 | free(console->handlers); |
| 673 | console->handlers = NULL; |
| 674 | console->n_handlers = 0; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 675 | } |
| 676 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 677 | static int get_current_time(struct timeval *tv) |
| 678 | { |
| 679 | struct timespec t; |
| 680 | int rc; |
| 681 | |
| 682 | /* |
| 683 | * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to |
| 684 | * local time changes. However, a struct timeval is more |
| 685 | * convenient for calculations, so convert to that. |
| 686 | */ |
| 687 | rc = clock_gettime(CLOCK_MONOTONIC, &t); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 688 | if (rc) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 689 | return rc; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 690 | } |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 691 | |
| 692 | tv->tv_sec = t.tv_sec; |
| 693 | tv->tv_usec = t.tv_nsec / 1000; |
| 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 698 | struct ringbuffer_consumer * |
| 699 | console_ringbuffer_consumer_register(struct console *console, |
| 700 | ringbuffer_poll_fn_t poll_fn, void *data) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 701 | { |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 702 | return ringbuffer_consumer_register(console->rb, poll_fn, data); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 703 | } |
| 704 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 705 | struct poller *console_poller_register(struct console *console, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 706 | struct handler *handler, |
| 707 | poller_event_fn_t poller_fn, |
| 708 | poller_timeout_fn_t timeout_fn, int fd, |
| 709 | int events, void *data) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 710 | { |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 711 | struct poller *poller; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 712 | long n; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 713 | |
Alexander Hansen | 2325d5d | 2024-06-07 16:54:20 +0200 | [diff] [blame] | 714 | const ssize_t index = console_server_request_pollfd( |
| 715 | console->server, fd, (short)(events & 0x7fff)); |
| 716 | if (index < 0) { |
| 717 | fprintf(stderr, "Error requesting pollfd\n"); |
| 718 | return NULL; |
| 719 | } |
| 720 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 721 | poller = malloc(sizeof(*poller)); |
Alexander Hansen | 2325d5d | 2024-06-07 16:54:20 +0200 | [diff] [blame] | 722 | // TODO: check for error case of malloc here and release previously requested pollfd |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 723 | poller->remove = false; |
| 724 | poller->handler = handler; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 725 | poller->event_fn = poller_fn; |
| 726 | poller->timeout_fn = timeout_fn; |
Andrew Jeffery | 9cc8459 | 2024-07-09 15:44:30 +0930 | [diff] [blame] | 727 | timerclear(&poller->timeout); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 728 | poller->data = data; |
Alexander Hansen | 2325d5d | 2024-06-07 16:54:20 +0200 | [diff] [blame] | 729 | poller->pollfd_index = index; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 730 | |
| 731 | /* add one to our pollers array */ |
| 732 | n = console->n_pollers++; |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 733 | /* |
| 734 | * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a |
| 735 | * pointer type. |
| 736 | */ |
| 737 | /* NOLINTBEGIN(bugprone-sizeof-expression) */ |
| 738 | console->pollers = reallocarray(console->pollers, console->n_pollers, |
| 739 | sizeof(*console->pollers)); |
Alexander Hansen | 2325d5d | 2024-06-07 16:54:20 +0200 | [diff] [blame] | 740 | // TODO: check for the error case of reallocarray and release previously requested pollfd |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 741 | /* NOLINTEND(bugprone-sizeof-expression) */ |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 742 | |
| 743 | console->pollers[n] = poller; |
| 744 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 745 | return poller; |
| 746 | } |
| 747 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 748 | void console_poller_unregister(struct console *console, struct poller *poller) |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 749 | { |
| 750 | int i; |
| 751 | |
| 752 | /* find the entry in our pollers array */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 753 | for (i = 0; i < console->n_pollers; i++) { |
| 754 | if (console->pollers[i] == poller) { |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 755 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 756 | } |
| 757 | } |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 758 | |
| 759 | assert(i < console->n_pollers); |
| 760 | |
| 761 | console->n_pollers--; |
| 762 | |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 763 | /* |
| 764 | * Remove the item from the pollers array... |
| 765 | * |
| 766 | * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a |
| 767 | * pointer type. |
| 768 | */ |
| 769 | /* NOLINTBEGIN(bugprone-sizeof-expression) */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 770 | memmove(&console->pollers[i], &console->pollers[i + 1], |
| 771 | sizeof(*console->pollers) * (console->n_pollers - i)); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 772 | |
Andrew Jeffery | 7851a39 | 2024-07-09 16:30:22 +0930 | [diff] [blame] | 773 | if (console->n_pollers == 0) { |
| 774 | free(console->pollers); |
| 775 | console->pollers = NULL; |
| 776 | } else { |
| 777 | console->pollers = reallocarray(console->pollers, |
| 778 | console->n_pollers, |
| 779 | sizeof(*console->pollers)); |
| 780 | } |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 781 | /* NOLINTEND(bugprone-sizeof-expression) */ |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 782 | |
Alexander Hansen | 2325d5d | 2024-06-07 16:54:20 +0200 | [diff] [blame] | 783 | console_server_release_pollfd(console->server, poller->pollfd_index); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 784 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 785 | free(poller); |
| 786 | } |
| 787 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 788 | void console_poller_set_events(struct console *console, struct poller *poller, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 789 | int events) |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 790 | { |
Alexander Hansen | 2325d5d | 2024-06-07 16:54:20 +0200 | [diff] [blame] | 791 | console->server->pollfds[poller->pollfd_index].events = |
| 792 | (short)(events & 0x7fff); |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 793 | } |
| 794 | |
Andrew Jeffery | fd04832 | 2023-04-18 12:02:01 +0930 | [diff] [blame] | 795 | void console_poller_set_timeout(struct console *console __attribute__((unused)), |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 796 | struct poller *poller, const struct timeval *tv) |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 797 | { |
| 798 | struct timeval now; |
| 799 | int rc; |
| 800 | |
| 801 | rc = get_current_time(&now); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 802 | if (rc) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 803 | return; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 804 | } |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 805 | |
| 806 | timeradd(&now, tv, &poller->timeout); |
| 807 | } |
| 808 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 809 | static long get_poll_timeout(struct console *console, struct timeval *cur_time) |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 810 | { |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 811 | struct timeval *earliest; |
| 812 | struct timeval interval; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 813 | struct poller *poller; |
| 814 | int i; |
| 815 | |
| 816 | earliest = NULL; |
| 817 | |
| 818 | for (i = 0; i < console->n_pollers; i++) { |
| 819 | poller = console->pollers[i]; |
| 820 | |
| 821 | if (poller->timeout_fn && timerisset(&poller->timeout) && |
| 822 | (!earliest || |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 823 | (earliest && timercmp(&poller->timeout, earliest, <)))) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 824 | // poller is buffering data and needs the poll |
| 825 | // function to timeout. |
| 826 | earliest = &poller->timeout; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | if (earliest) { |
| 831 | if (timercmp(earliest, cur_time, >)) { |
| 832 | /* recalculate the timeout period, time period has |
| 833 | * not elapsed */ |
| 834 | timersub(earliest, cur_time, &interval); |
| 835 | return ((interval.tv_sec * 1000) + |
| 836 | (interval.tv_usec / 1000)); |
Andrew Jeffery | 0b7b047 | 2023-04-19 12:48:51 +0930 | [diff] [blame] | 837 | } /* return from poll immediately */ |
| 838 | return 0; |
| 839 | |
| 840 | } /* poll indefinitely */ |
| 841 | return -1; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | static int call_pollers(struct console *console, struct timeval *cur_time) |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 845 | { |
| 846 | struct poller *poller; |
| 847 | struct pollfd *pollfd; |
| 848 | enum poller_ret prc; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 849 | int i; |
| 850 | int rc; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 851 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 852 | rc = 0; |
| 853 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 854 | /* |
| 855 | * Process poll events by iterating through the pollers and pollfds |
| 856 | * in-step, calling any pollers that we've found revents for. |
| 857 | */ |
| 858 | for (i = 0; i < console->n_pollers; i++) { |
| 859 | poller = console->pollers[i]; |
Alexander Hansen | 2325d5d | 2024-06-07 16:54:20 +0200 | [diff] [blame] | 860 | pollfd = &console->server->pollfds[poller->pollfd_index]; |
| 861 | if (pollfd->fd < 0) { |
| 862 | // pollfd has already been released |
| 863 | continue; |
| 864 | } |
| 865 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 866 | prc = POLLER_OK; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 867 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 868 | /* process pending events... */ |
| 869 | if (pollfd->revents) { |
| 870 | prc = poller->event_fn(poller->handler, pollfd->revents, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 871 | poller->data); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 872 | if (prc == POLLER_EXIT) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 873 | rc = -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 874 | } else if (prc == POLLER_REMOVE) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 875 | poller->remove = true; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 876 | } |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 877 | } |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 878 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 879 | if ((prc == POLLER_OK) && poller->timeout_fn && |
| 880 | timerisset(&poller->timeout) && |
| 881 | timercmp(&poller->timeout, cur_time, <=)) { |
| 882 | /* One of the ringbuffer consumers is buffering the |
| 883 | data stream. The amount of idle time the consumer |
| 884 | desired has expired. Process the buffered data for |
| 885 | transmission. */ |
| 886 | timerclear(&poller->timeout); |
| 887 | prc = poller->timeout_fn(poller->handler, poller->data); |
| 888 | if (prc == POLLER_EXIT) { |
| 889 | rc = -1; |
| 890 | } else if (prc == POLLER_REMOVE) { |
| 891 | poller->remove = true; |
| 892 | } |
| 893 | } |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | /** |
| 897 | * Process deferred removals; restarting each time we unregister, as |
| 898 | * the array will have changed |
| 899 | */ |
| 900 | for (;;) { |
| 901 | bool removed = false; |
| 902 | |
| 903 | for (i = 0; i < console->n_pollers; i++) { |
| 904 | poller = console->pollers[i]; |
| 905 | if (poller->remove) { |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 906 | console_poller_unregister(console, poller); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 907 | removed = true; |
| 908 | break; |
| 909 | } |
| 910 | } |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 911 | if (!removed) { |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 912 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 913 | } |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | return rc; |
| 917 | } |
| 918 | |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 919 | static void sighandler(int signal) |
| 920 | { |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 921 | if (signal == SIGINT) { |
Andrew Jeffery | 553cb66 | 2024-07-09 15:14:56 +0930 | [diff] [blame] | 922 | sigint = 1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 923 | } |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 924 | } |
| 925 | |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 926 | static int run_console_per_console(struct console *console, size_t buf_size, |
| 927 | struct timeval *tv) |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 928 | { |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 929 | int rc; |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 930 | |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 931 | if (console->rb->size < buf_size) { |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 932 | fprintf(stderr, "Ringbuffer size should be greater than %zuB\n", |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 933 | buf_size); |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 934 | return -1; |
| 935 | } |
| 936 | |
| 937 | if (sigint) { |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 938 | warnx("Received interrupt, exiting\n"); |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 939 | return -1; |
| 940 | } |
| 941 | |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 942 | /* ... and then the pollers */ |
| 943 | rc = call_pollers(console, tv); |
| 944 | if (rc) { |
| 945 | return -1; |
| 946 | } |
| 947 | |
| 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | static int run_console_iteration(struct console_server *server) |
| 952 | { |
| 953 | struct timeval tv; |
| 954 | uint8_t buf[4096]; |
| 955 | long timeout; |
| 956 | ssize_t rc; |
| 957 | |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 958 | rc = get_current_time(&tv); |
| 959 | if (rc) { |
| 960 | warn("Failed to read current time"); |
| 961 | return -1; |
| 962 | } |
| 963 | |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 964 | timeout = get_poll_timeout(server->active, &tv); |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 965 | |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 966 | rc = poll(server->pollfds, server->capacity_pollfds, (int)timeout); |
| 967 | |
| 968 | if (sigint) { |
| 969 | warnx("Received interrupt, exiting\n"); |
| 970 | return -1; |
| 971 | } |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 972 | |
| 973 | if (rc < 0) { |
| 974 | if (errno == EINTR) { |
| 975 | return 0; |
| 976 | } |
| 977 | warn("poll error"); |
| 978 | return -1; |
| 979 | } |
| 980 | |
| 981 | /* process internal fd first */ |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 982 | if (server->pollfds[server->tty_pollfd_index].revents) { |
| 983 | rc = read(server->tty.fd, buf, sizeof(buf)); |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 984 | if (rc <= 0) { |
| 985 | warn("Error reading from tty device"); |
| 986 | return -1; |
| 987 | } |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 988 | |
| 989 | rc = ringbuffer_queue(server->active->rb, buf, rc); |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 990 | if (rc) { |
| 991 | return -1; |
| 992 | } |
| 993 | } |
| 994 | |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 995 | // process dbus |
| 996 | struct pollfd *dbus_pollfd = |
| 997 | &(server->pollfds[server->dbus_pollfd_index]); |
| 998 | if (dbus_pollfd->revents) { |
| 999 | sd_bus_process(server->bus, NULL); |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 1000 | } |
| 1001 | |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 1002 | for (size_t i = 0; i < server->n_consoles; i++) { |
| 1003 | struct console *console = server->consoles[i]; |
| 1004 | |
| 1005 | rc = run_console_per_console(console, sizeof(buf), &tv); |
| 1006 | if (rc != 0) { |
| 1007 | return -1; |
| 1008 | } |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 1009 | } |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 1010 | |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 1011 | return 0; |
| 1012 | } |
| 1013 | |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 1014 | int run_server(struct console_server *server) |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 1015 | { |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 1016 | sighandler_t sighandler_save; |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 1017 | ssize_t rc = 0; |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 1018 | |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 1019 | if (server->n_consoles == 0) { |
| 1020 | warnx("no console configured for this server"); |
| 1021 | return -1; |
| 1022 | } |
| 1023 | |
| 1024 | sighandler_save = signal(SIGINT, sighandler); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1025 | for (;;) { |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 1026 | rc = run_console_iteration(server); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 1027 | if (rc) { |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 1028 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 1029 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1030 | } |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 1031 | signal(SIGINT, sighandler_save); |
| 1032 | |
| 1033 | return rc ? -1 : 0; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1034 | } |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 1035 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1036 | static const struct option options[] = { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 1037 | { "config", required_argument, 0, 'c' }, |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 1038 | { "console-id", required_argument, 0, 'i' }, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 1039 | { 0, 0, 0, 0 }, |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1040 | }; |
| 1041 | |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 1042 | static struct console *console_init(struct console_server *server, |
| 1043 | struct config *config, |
| 1044 | const char *console_id) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1045 | { |
Medicine Yeh | 7f2bfb9 | 2024-03-10 23:19:41 -0700 | [diff] [blame] | 1046 | size_t buffer_size = default_buffer_size; |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 1047 | const char *buffer_size_str = NULL; |
| 1048 | int rc; |
| 1049 | |
| 1050 | struct console *console = calloc(1, sizeof(struct console)); |
| 1051 | if (console == NULL) { |
| 1052 | return NULL; |
| 1053 | } |
| 1054 | |
| 1055 | console->server = server; |
| 1056 | console->console_id = console_id; |
| 1057 | |
| 1058 | buffer_size_str = |
| 1059 | config_get_section_value(config, console_id, "ringbuffer-size"); |
| 1060 | |
| 1061 | if (!buffer_size_str) { |
| 1062 | buffer_size_str = config_get_value(config, "ringbuffer-size"); |
| 1063 | } |
| 1064 | |
| 1065 | if (buffer_size_str) { |
| 1066 | rc = config_parse_bytesize(buffer_size_str, &buffer_size); |
| 1067 | if (rc) { |
| 1068 | warn("Invalid ringbuffer-size. Default to %zukB", |
| 1069 | buffer_size >> 10); |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | console->rb = ringbuffer_init(buffer_size); |
| 1074 | if (!console->rb) { |
| 1075 | goto cleanup_console; |
| 1076 | } |
| 1077 | |
| 1078 | if (set_socket_info(console, config, console_id)) { |
| 1079 | warnx("set_socket_info failed"); |
| 1080 | goto cleanup_rb; |
| 1081 | } |
| 1082 | |
| 1083 | rc = dbus_init(console, config); |
| 1084 | if (rc != 0) { |
| 1085 | goto cleanup_rb; |
| 1086 | } |
| 1087 | |
| 1088 | handlers_init(console, config); |
| 1089 | |
| 1090 | return console; |
| 1091 | |
| 1092 | cleanup_rb: |
| 1093 | free(console->rb); |
| 1094 | cleanup_console: |
| 1095 | free(console); |
| 1096 | |
| 1097 | return NULL; |
| 1098 | } |
| 1099 | |
| 1100 | static void console_fini(struct console *console) |
| 1101 | { |
| 1102 | handlers_fini(console); |
| 1103 | ringbuffer_fini(console->rb); |
| 1104 | free(console->pollers); |
| 1105 | free(console); |
| 1106 | } |
| 1107 | |
| 1108 | // 'opt_console_id' may be NULL |
| 1109 | static int console_server_add_console(struct console_server *server, |
| 1110 | struct config *config, |
| 1111 | const char *opt_console_id) |
| 1112 | { |
| 1113 | const char *console_id; |
| 1114 | struct console *console; |
| 1115 | |
| 1116 | console_id = config_resolve_console_id(config, opt_console_id); |
| 1117 | |
| 1118 | struct console **tmp = reallocarray(server->consoles, |
| 1119 | server->n_consoles + 1, |
| 1120 | sizeof(struct console *)); |
| 1121 | if (tmp == NULL) { |
| 1122 | warnx("could not realloc server->consoles"); |
| 1123 | return -1; |
| 1124 | } |
| 1125 | server->consoles = tmp; |
| 1126 | |
| 1127 | console = console_init(server, config, console_id); |
| 1128 | if (console == NULL) { |
| 1129 | warnx("console_init failed"); |
| 1130 | return -1; |
| 1131 | } |
| 1132 | |
| 1133 | server->consoles[server->n_consoles++] = console; |
| 1134 | |
| 1135 | return 0; |
| 1136 | } |
| 1137 | |
| 1138 | // returns NULL on error |
| 1139 | static struct console * |
| 1140 | console_server_add_consoles(struct console_server *server, |
| 1141 | const char *arg_console_id) |
| 1142 | { |
| 1143 | int rc; |
| 1144 | |
| 1145 | const int nsections = config_count_sections(server->config); |
| 1146 | if (nsections < 0) { |
| 1147 | return NULL; |
| 1148 | } |
| 1149 | |
| 1150 | if (nsections == 0) { |
| 1151 | const char *console_id = arg_console_id; |
| 1152 | |
| 1153 | rc = console_server_add_console(server, server->config, |
| 1154 | console_id); |
| 1155 | if (rc != 0) { |
| 1156 | return NULL; |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | for (int i = 0; i < nsections; i++) { |
| 1161 | const char *console_id = |
| 1162 | config_get_section_name(server->config, i); |
| 1163 | |
| 1164 | if (console_id == NULL) { |
| 1165 | warnx("no console id provided\n"); |
| 1166 | return NULL; |
| 1167 | } |
| 1168 | |
| 1169 | rc = console_server_add_console(server, server->config, |
| 1170 | console_id); |
| 1171 | if (rc != 0) { |
| 1172 | return NULL; |
| 1173 | } |
| 1174 | } |
| 1175 | |
| 1176 | const char *initially_active = |
| 1177 | config_get_value(server->config, "active-console"); |
| 1178 | if (!initially_active) { |
| 1179 | return server->consoles[0]; |
| 1180 | } |
| 1181 | |
| 1182 | printf("setting console-id '%s' as the initially active console\n", |
| 1183 | initially_active); |
| 1184 | |
| 1185 | for (size_t i = 0; i < server->n_consoles; i++) { |
| 1186 | struct console *console = server->consoles[i]; |
| 1187 | |
| 1188 | if (strcmp(console->console_id, initially_active) == 0) { |
| 1189 | return console; |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | warnx("'active-console' '%s' not found among console ids\n", |
| 1194 | initially_active); |
| 1195 | |
| 1196 | return NULL; |
| 1197 | } |
| 1198 | |
| 1199 | int console_server_init(struct console_server *server, |
| 1200 | const char *config_filename, |
| 1201 | const char *config_tty_kname, const char *console_id) |
| 1202 | { |
| 1203 | int rc; |
| 1204 | memset(server, 0, sizeof(struct console_server)); |
| 1205 | |
| 1206 | server->tty_pollfd_index = -1; |
| 1207 | |
| 1208 | server->config = config_init(config_filename); |
| 1209 | if (server->config == NULL) { |
| 1210 | return -1; |
| 1211 | } |
| 1212 | |
| 1213 | uart_routing_init(server->config); |
| 1214 | |
| 1215 | rc = tty_init(server, server->config, config_tty_kname); |
| 1216 | if (rc != 0) { |
| 1217 | warnx("error during tty_init, exiting.\n"); |
| 1218 | return -1; |
| 1219 | } |
| 1220 | |
| 1221 | rc = dbus_server_init(server); |
| 1222 | if (rc != 0) { |
| 1223 | warnx("error during dbus init for console server"); |
| 1224 | return -1; |
| 1225 | } |
| 1226 | |
| 1227 | server->active = console_server_add_consoles(server, console_id); |
| 1228 | if (server->active == NULL) { |
| 1229 | return -1; |
| 1230 | } |
| 1231 | |
| 1232 | return 0; |
| 1233 | } |
| 1234 | |
| 1235 | void console_server_fini(struct console_server *server) |
| 1236 | { |
| 1237 | for (size_t i = 0; i < server->n_consoles; i++) { |
| 1238 | console_fini(server->consoles[i]); |
| 1239 | } |
| 1240 | |
| 1241 | free(server->consoles); |
| 1242 | dbus_server_fini(server); |
| 1243 | tty_fini(server); |
| 1244 | free(server->pollfds); |
| 1245 | config_fini(server->config); |
| 1246 | } |
| 1247 | |
| 1248 | int main(int argc, char **argv) |
| 1249 | { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 1250 | const char *config_filename = NULL; |
Vishwanatha Subbanna | 6221ce9 | 2016-07-20 05:35:45 -0500 | [diff] [blame] | 1251 | const char *config_tty_kname = NULL; |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 1252 | const char *console_id = NULL; |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 1253 | struct console_server server = { 0 }; |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 1254 | int rc = 0; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1255 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1256 | for (;;) { |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 1257 | int c; |
| 1258 | int idx; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1259 | |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 1260 | c = getopt_long(argc, argv, "c:i:", options, &idx); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 1261 | if (c == -1) { |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1262 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 1263 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1264 | |
| 1265 | switch (c) { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 1266 | case 'c': |
| 1267 | config_filename = optarg; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1268 | break; |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 1269 | case 'i': |
| 1270 | console_id = optarg; |
| 1271 | break; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1272 | case 'h': |
| 1273 | case '?': |
| 1274 | usage(argv[0]); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 1275 | return EXIT_SUCCESS; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1276 | } |
| 1277 | } |
| 1278 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 1279 | if (optind < argc) { |
Andrew Jeffery | 91dde14 | 2020-02-12 22:46:27 +1030 | [diff] [blame] | 1280 | config_tty_kname = argv[optind]; |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 1281 | } else { |
| 1282 | errx(EXIT_FAILURE, "no tty device path has been provided\n"); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 1283 | } |
Vishwanatha Subbanna | 6221ce9 | 2016-07-20 05:35:45 -0500 | [diff] [blame] | 1284 | |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 1285 | rc = console_server_init(&server, config_filename, config_tty_kname, |
| 1286 | console_id); |
| 1287 | |
| 1288 | if (rc == 0) { |
| 1289 | rc = run_server(&server); |
Andrew Jeffery | 28a1761 | 2024-07-10 09:32:05 +0930 | [diff] [blame] | 1290 | } |
Medicine Yeh | 7f2bfb9 | 2024-03-10 23:19:41 -0700 | [diff] [blame] | 1291 | |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame^] | 1292 | console_server_fini(&server); |
Andrew Jeffery | 28a1761 | 2024-07-10 09:32:05 +0930 | [diff] [blame] | 1293 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1294 | return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
| 1295 | } |