Jeremy Kerr | 9326d77 | 2016-03-17 17:15:02 +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 | */ |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 16 | |
Johnathan Mantey | a395a77 | 2019-03-19 14:28:53 -0700 | [diff] [blame] | 17 | #pragma once |
| 18 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 19 | #include <poll.h> |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 20 | #include <stdbool.h> |
Benjamin Fair | fcbdea9 | 2018-06-04 14:19:25 -0700 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | #include <termios.h> /* for speed_t */ |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 23 | #include <time.h> |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 24 | #include <systemd/sd-bus.h> |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 25 | #include <sys/time.h> |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 26 | #include <sys/un.h> |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 27 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 28 | struct console; |
Jeremy Kerr | d47963e | 2016-03-16 17:29:55 +0800 | [diff] [blame] | 29 | struct config; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 30 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 31 | /* Handler API. |
| 32 | * |
Jeremy Kerr | 1b57501 | 2017-02-08 11:22:23 +0800 | [diff] [blame] | 33 | * Console data handlers: these implement the functions that process |
| 34 | * data coming out of the main tty device. |
| 35 | * |
| 36 | * Handlers are registered at link time using the console_handler_register() |
| 37 | * macro. We call each handler's ->init() function at startup, and ->fini() at |
| 38 | * exit. |
| 39 | * |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 40 | * Handlers will almost always want to register a ringbuffer consumer, which |
| 41 | * provides data coming from the tty. Use cosole_register_ringbuffer_consumer() |
| 42 | * for this. To send data to the tty, use console_data_out(). |
Jeremy Kerr | 1b57501 | 2017-02-08 11:22:23 +0800 | [diff] [blame] | 43 | * |
| 44 | * If a handler needs to monitor a separate file descriptor for events, use the |
| 45 | * poller API, through console_poller_register(). |
| 46 | */ |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 47 | struct handler; |
| 48 | |
| 49 | struct handler_type { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 50 | const char *name; |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 51 | struct handler *(*init)(const struct handler_type *type, |
| 52 | struct console *console, struct config *config); |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 53 | void (*fini)(struct handler *handler); |
| 54 | int (*baudrate)(struct handler *handler, speed_t baudrate); |
Alexander Hansen | a6b2910 | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 55 | void (*deselect)(struct handler *handler); |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | struct handler { |
| 59 | const struct handler_type *type; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 60 | }; |
| 61 | |
Andrew Jeffery | 750fb0c | 2023-04-19 11:48:50 +0930 | [diff] [blame] | 62 | /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 63 | #define __handler_name(n) __handler_##n |
| 64 | #define _handler_name(n) __handler_name(n) |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 65 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 66 | #ifndef __clang__ |
| 67 | #define handler_type_check(h) BUILD_ASSERT_OR_ZERO((h)->init && (h)->fini) |
| 68 | #else |
| 69 | /* clang doesn't seem to be able to constify the type ops */ |
| 70 | #define handler_type_check(h) 0 |
| 71 | #endif |
| 72 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 73 | #define console_handler_register(h) \ |
| 74 | static const __attribute__((section("handlers"))) \ |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 75 | __attribute__((used)) struct handler_type * \ |
| 76 | _handler_name(__COUNTER__) = (h) + handler_type_check(h) |
Andrew Jeffery | 750fb0c | 2023-04-19 11:48:50 +0930 | [diff] [blame] | 77 | /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */ |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 78 | |
| 79 | int console_data_out(struct console *console, const uint8_t *data, size_t len); |
| 80 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 81 | enum poller_ret { |
| 82 | POLLER_OK = 0, |
| 83 | POLLER_REMOVE, |
| 84 | POLLER_EXIT, |
| 85 | }; |
| 86 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 87 | typedef char(socket_path_t)[sizeof(((struct sockaddr_un *)NULL)->sun_path)]; |
| 88 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 89 | typedef enum poller_ret (*poller_event_fn_t)(struct handler *handler, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 90 | int revents, void *data); |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 91 | typedef enum poller_ret (*poller_timeout_fn_t)(struct handler *handler, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 92 | void *data); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 93 | |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 94 | enum tty_device { |
| 95 | TTY_DEVICE_UNDEFINED = 0, |
| 96 | TTY_DEVICE_VUART, |
| 97 | TTY_DEVICE_UART, |
| 98 | TTY_DEVICE_PTY, |
| 99 | }; |
| 100 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 101 | struct console_server { |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 102 | struct { |
| 103 | const char *kname; |
| 104 | char *dev; |
| 105 | int fd; |
| 106 | enum tty_device type; |
| 107 | union { |
| 108 | struct { |
| 109 | char *sysfs_devnode; |
| 110 | int sirq; |
| 111 | uint16_t lpc_addr; |
| 112 | } vuart; |
| 113 | struct { |
| 114 | speed_t baud; |
| 115 | } uart; |
| 116 | }; |
| 117 | } tty; |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 118 | |
Alexander Hansen | 2325d5d | 2024-06-07 16:54:20 +0200 | [diff] [blame] | 119 | // All the pollfds are stored here, |
| 120 | // so 'poll' can operate on them. |
| 121 | // The other 'pollfd*' are just pointers to this array. |
| 122 | struct pollfd *pollfds; |
| 123 | size_t capacity_pollfds; |
| 124 | |
| 125 | // index into pollfds |
| 126 | size_t tty_pollfd_index; |
| 127 | |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 128 | struct config *config; |
| 129 | |
| 130 | // the currently active console |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 131 | struct console *active; |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 132 | |
| 133 | struct console **consoles; |
| 134 | size_t n_consoles; |
| 135 | |
| 136 | // index into (struct console_server)->pollfds |
| 137 | size_t dbus_pollfd_index; |
| 138 | |
| 139 | struct sd_bus *bus; |
Alexander Hansen | a6b2910 | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 140 | |
| 141 | // may be NULL in case there is no mux |
| 142 | struct console_mux *mux; |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 143 | }; |
| 144 | |
| 145 | struct console { |
| 146 | // point back to the console server |
| 147 | // which we are a member of |
| 148 | struct console_server *server; |
| 149 | |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 150 | const char *console_id; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 151 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 152 | /* Socket name starts with null character hence we need length */ |
| 153 | socket_path_t socket_name; |
| 154 | ssize_t socket_name_len; |
| 155 | |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 156 | struct ringbuffer *rb; |
| 157 | |
| 158 | struct handler **handlers; |
| 159 | long n_handlers; |
| 160 | |
| 161 | struct poller **pollers; |
| 162 | long n_pollers; |
Alexander Hansen | a6b2910 | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 163 | |
| 164 | // values to configure the mux |
| 165 | unsigned long mux_index; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | /* poller API */ |
| 169 | struct poller { |
| 170 | struct handler *handler; |
| 171 | void *data; |
| 172 | poller_event_fn_t event_fn; |
| 173 | poller_timeout_fn_t timeout_fn; |
| 174 | struct timeval timeout; |
| 175 | bool remove; |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 176 | |
Alexander Hansen | 2325d5d | 2024-06-07 16:54:20 +0200 | [diff] [blame] | 177 | // index into (struct console_server)->pollfds |
| 178 | size_t pollfd_index; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 179 | }; |
| 180 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 181 | struct poller *console_poller_register(struct console *console, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 182 | struct handler *handler, |
Andrew Jeffery | 6422215 | 2023-04-19 12:51:24 +0930 | [diff] [blame] | 183 | poller_event_fn_t poller_fn, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 184 | poller_timeout_fn_t timeout_fn, int fd, |
| 185 | int events, void *data); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 186 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 187 | void console_poller_unregister(struct console *console, struct poller *poller); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 188 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 189 | void console_poller_set_events(struct console *console, struct poller *poller, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 190 | int events); |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 191 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 192 | void console_poller_set_timeout(struct console *console, struct poller *poller, |
Andrew Jeffery | 6422215 | 2023-04-19 12:51:24 +0930 | [diff] [blame] | 193 | const struct timeval *tv); |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 194 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 195 | /* ringbuffer API */ |
Andrew Jeffery | 1b86844 | 2023-04-19 13:16:32 +0930 | [diff] [blame] | 196 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 197 | enum ringbuffer_poll_ret { |
| 198 | RINGBUFFER_POLL_OK = 0, |
| 199 | RINGBUFFER_POLL_REMOVE, |
| 200 | }; |
| 201 | |
| 202 | typedef enum ringbuffer_poll_ret (*ringbuffer_poll_fn_t)(void *data, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 203 | size_t force_len); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 204 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 205 | struct ringbuffer_consumer; |
| 206 | |
Andrew Jeffery | 1b86844 | 2023-04-19 13:16:32 +0930 | [diff] [blame] | 207 | struct ringbuffer { |
| 208 | uint8_t *buf; |
| 209 | size_t size; |
| 210 | size_t tail; |
| 211 | struct ringbuffer_consumer **consumers; |
| 212 | int n_consumers; |
| 213 | }; |
| 214 | |
| 215 | struct ringbuffer_consumer { |
| 216 | struct ringbuffer *rb; |
| 217 | ringbuffer_poll_fn_t poll_fn; |
| 218 | void *poll_data; |
| 219 | size_t pos; |
| 220 | }; |
| 221 | |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 222 | struct ringbuffer *ringbuffer_init(size_t size); |
| 223 | void ringbuffer_fini(struct ringbuffer *rb); |
| 224 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 225 | struct ringbuffer_consumer * |
| 226 | ringbuffer_consumer_register(struct ringbuffer *rb, |
| 227 | ringbuffer_poll_fn_t poll_fn, void *data); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 228 | |
| 229 | void ringbuffer_consumer_unregister(struct ringbuffer_consumer *rbc); |
| 230 | |
| 231 | int ringbuffer_queue(struct ringbuffer *rb, uint8_t *data, size_t len); |
| 232 | |
| 233 | size_t ringbuffer_dequeue_peek(struct ringbuffer_consumer *rbc, size_t offset, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 234 | uint8_t **data); |
Jeremy Kerr | c9775ce | 2017-02-07 16:25:34 +0800 | [diff] [blame] | 235 | |
| 236 | int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len); |
| 237 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 238 | size_t ringbuffer_len(struct ringbuffer_consumer *rbc); |
| 239 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 240 | /* console wrapper around ringbuffer consumer registration */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 241 | struct ringbuffer_consumer * |
| 242 | console_ringbuffer_consumer_register(struct console *console, |
| 243 | ringbuffer_poll_fn_t poll_fn, void *data); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 244 | |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 245 | /* Console server API */ |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 246 | void tty_init_termios(struct console_server *server); |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 247 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 248 | /* socket paths */ |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 249 | ssize_t console_socket_path(socket_path_t path, const char *id); |
Andrew Jeffery | 5e7c078 | 2020-02-10 12:12:36 +1030 | [diff] [blame] | 250 | ssize_t console_socket_path_readable(const struct sockaddr_un *addr, |
| 251 | size_t addrlen, socket_path_t path); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 252 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 253 | /* utils */ |
| 254 | int write_buf_to_fd(int fd, const uint8_t *buf, size_t len); |
| 255 | |
Alexander Hansen | 312ecdc | 2024-07-10 15:33:37 +0200 | [diff] [blame] | 256 | /* console_server dbus */ |
| 257 | int dbus_server_init(struct console_server *server); |
| 258 | void dbus_server_fini(struct console_server *server); |
| 259 | |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 260 | /* console-dbus API */ |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 261 | int dbus_init(struct console *console, |
| 262 | struct config *config __attribute__((unused))); |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 263 | |
Ninad Palsule | bd992c9 | 2023-05-09 18:30:01 -0500 | [diff] [blame] | 264 | /* socket-handler API */ |
| 265 | int dbus_create_socket_consumer(struct console *console); |
| 266 | |
Patrick Williams | 397fd03 | 2023-02-10 05:50:21 -0600 | [diff] [blame] | 267 | #ifndef offsetof |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 268 | #define offsetof(type, member) ((unsigned long)&((type *)NULL)->member) |
Patrick Williams | 397fd03 | 2023-02-10 05:50:21 -0600 | [diff] [blame] | 269 | #endif |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 270 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 271 | #define container_of(ptr, type, member) \ |
Patrick Williams | b965c22 | 2024-08-16 15:21:28 -0400 | [diff] [blame^] | 272 | ((type *)((void *)((ptr) - offsetof(type, member)))) |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 273 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 274 | #define BUILD_ASSERT(c) \ |
| 275 | do { \ |
| 276 | char __c[(c) ? 1 : -1] __attribute__((unused)); \ |
Jeremy Kerr | 658cbda | 2016-03-09 18:10:00 +0800 | [diff] [blame] | 277 | } while (0) |
Jeremy Kerr | a71b395 | 2024-07-05 11:52:49 +0800 | [diff] [blame] | 278 | |
| 279 | #define BUILD_ASSERT_OR_ZERO(c) (sizeof(char[(c) ? 1 : -1]) - 1) |
Alexander Hansen | 2325d5d | 2024-06-07 16:54:20 +0200 | [diff] [blame] | 280 | |
| 281 | // returns the index of that pollfd in server->pollfds |
| 282 | // we cannot return a pointer because 'realloc' may move server->pollfds |
| 283 | ssize_t console_server_request_pollfd(struct console_server *server, int fd, |
| 284 | short int events); |
| 285 | |
| 286 | int console_server_release_pollfd(struct console_server *server, |
| 287 | size_t pollfd_index); |