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 | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 64 | /* populates server->tty.dev and server->tty.sysfs_devnode, using the tty kernel name */ |
| 65 | static int tty_find_device(struct console_server *server) |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 66 | { |
Andrew Jeffery | d3cb9c2 | 2023-05-03 17:13:50 +0930 | [diff] [blame] | 67 | char *tty_class_device_link = NULL; |
| 68 | char *tty_path_input_real = NULL; |
| 69 | char *tty_device_tty_dir = NULL; |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 70 | char *tty_vuart_lpc_addr = NULL; |
Andrew Jeffery | d3cb9c2 | 2023-05-03 17:13:50 +0930 | [diff] [blame] | 71 | char *tty_device_reldir = NULL; |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 72 | char *tty_sysfs_devnode = NULL; |
Andrew Jeffery | d3cb9c2 | 2023-05-03 17:13:50 +0930 | [diff] [blame] | 73 | char *tty_kname_real = NULL; |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 74 | char *tty_path_input = NULL; |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 75 | int rc; |
| 76 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 77 | server->tty.type = TTY_DEVICE_UNDEFINED; |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 78 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 79 | assert(server->tty.kname); |
| 80 | if (!strlen(server->tty.kname)) { |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 81 | warnx("TTY kname must not be empty"); |
| 82 | rc = -1; |
| 83 | goto out_free; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 84 | } |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 85 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 86 | if (server->tty.kname[0] == '/') { |
| 87 | tty_path_input = strdup(server->tty.kname); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 88 | if (!tty_path_input) { |
| 89 | rc = -1; |
| 90 | goto out_free; |
| 91 | } |
| 92 | } else { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 93 | rc = asprintf(&tty_path_input, "/dev/%s", server->tty.kname); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 94 | if (rc < 0) { |
| 95 | goto out_free; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /* 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] | 100 | tty_path_input_real = realpath(tty_path_input, NULL); |
| 101 | if (!tty_path_input_real) { |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 102 | warn("Can't find realpath for %s", tty_path_input); |
Andrew Jeffery | 15792aa | 2023-04-18 16:47:33 +0930 | [diff] [blame] | 103 | rc = -1; |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 104 | goto out_free; |
| 105 | } |
| 106 | |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 107 | /* |
| 108 | * Allow hooking obmc-console-server up to PTYs for testing |
| 109 | * |
| 110 | * https://amboar.github.io/notes/2023/05/02/testing-obmc-console-with-socat.html |
| 111 | */ |
| 112 | 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] | 113 | server->tty.type = TTY_DEVICE_PTY; |
| 114 | server->tty.dev = strdup(server->tty.kname); |
| 115 | rc = server->tty.dev ? 0 : -1; |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 116 | goto out_free; |
| 117 | } |
| 118 | |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 119 | tty_kname_real = basename(tty_path_input_real); |
| 120 | if (!tty_kname_real) { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 121 | warn("Can't find real name for %s", server->tty.kname); |
Andrew Jeffery | 15792aa | 2023-04-18 16:47:33 +0930 | [diff] [blame] | 122 | rc = -1; |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 123 | goto out_free; |
| 124 | } |
| 125 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 126 | rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s", |
| 127 | tty_kname_real); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 128 | if (rc < 0) { |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 129 | goto out_free; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 130 | } |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 131 | |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 132 | tty_device_tty_dir = realpath(tty_class_device_link, NULL); |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 133 | if (!tty_device_tty_dir) { |
| 134 | warn("Can't query sysfs for device %s", tty_kname_real); |
Andrew Jeffery | 15792aa | 2023-04-18 16:47:33 +0930 | [diff] [blame] | 135 | rc = -1; |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 136 | goto out_free; |
| 137 | } |
| 138 | |
| 139 | rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 140 | if (rc < 0) { |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 141 | goto out_free; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 142 | } |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 143 | |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 144 | tty_sysfs_devnode = realpath(tty_device_reldir, NULL); |
| 145 | if (!tty_sysfs_devnode) { |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 146 | warn("Can't find parent device for %s", tty_kname_real); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 147 | } |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 148 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 149 | rc = asprintf(&server->tty.dev, "/dev/%s", tty_kname_real); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 150 | if (rc < 0) { |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 151 | goto out_free; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 152 | } |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 153 | |
Oskar Senft | 955d140 | 2023-09-27 22:43:22 -0400 | [diff] [blame] | 154 | // Default to non-VUART |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 155 | server->tty.type = TTY_DEVICE_UART; |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 156 | |
Oskar Senft | 955d140 | 2023-09-27 22:43:22 -0400 | [diff] [blame] | 157 | /* Arbitrarily pick an attribute to differentiate UART vs VUART */ |
| 158 | if (tty_sysfs_devnode) { |
| 159 | rc = asprintf(&tty_vuart_lpc_addr, "%s/lpc_address", |
| 160 | tty_sysfs_devnode); |
| 161 | if (rc < 0) { |
| 162 | goto out_free; |
| 163 | } |
| 164 | |
| 165 | rc = access(tty_vuart_lpc_addr, F_OK); |
| 166 | if (!rc) { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 167 | server->tty.type = TTY_DEVICE_VUART; |
| 168 | server->tty.vuart.sysfs_devnode = |
Oskar Senft | 955d140 | 2023-09-27 22:43:22 -0400 | [diff] [blame] | 169 | strdup(tty_sysfs_devnode); |
| 170 | } |
| 171 | } |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 172 | |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 173 | rc = 0; |
| 174 | |
| 175 | out_free: |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 176 | free(tty_vuart_lpc_addr); |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 177 | free(tty_class_device_link); |
Andrew Jeffery | 982090d | 2024-07-09 17:31:27 +0930 | [diff] [blame] | 178 | free(tty_sysfs_devnode); |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 179 | free(tty_device_tty_dir); |
| 180 | free(tty_device_reldir); |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 181 | free(tty_path_input); |
| 182 | free(tty_path_input_real); |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 183 | return rc; |
| 184 | } |
| 185 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 186 | 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] | 187 | int value) |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 188 | { |
| 189 | char *path; |
| 190 | FILE *fp; |
| 191 | int rc; |
| 192 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 193 | assert(server->tty.type == TTY_DEVICE_VUART); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 194 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 195 | if (!server->tty.vuart.sysfs_devnode) { |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 196 | return -1; |
| 197 | } |
| 198 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 199 | rc = asprintf(&path, "%s/%s", server->tty.vuart.sysfs_devnode, name); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 200 | if (rc < 0) { |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 201 | return -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 202 | } |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 203 | |
| 204 | fp = fopen(path, "w"); |
| 205 | if (!fp) { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 206 | warn("Can't access attribute %s on device %s", name, |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 207 | server->tty.kname); |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 208 | rc = -1; |
| 209 | goto out_free; |
| 210 | } |
| 211 | setvbuf(fp, NULL, _IONBF, 0); |
| 212 | |
| 213 | rc = fprintf(fp, "0x%x", value); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 214 | if (rc < 0) { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 215 | warn("Error writing to %s attribute of device %s", name, |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 216 | server->tty.kname); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 217 | } |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 218 | fclose(fp); |
| 219 | |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 220 | out_free: |
| 221 | free(path); |
| 222 | return rc; |
| 223 | } |
| 224 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 225 | /** |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 226 | * Set termios attributes on the console tty. |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 227 | */ |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 228 | void tty_init_termios(struct console_server *server) |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 229 | { |
| 230 | struct termios termios; |
| 231 | int rc; |
| 232 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 233 | rc = tcgetattr(server->tty.fd, &termios); |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 234 | if (rc) { |
| 235 | warn("Can't read tty termios"); |
| 236 | return; |
| 237 | } |
| 238 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 239 | if (server->tty.type == TTY_DEVICE_UART && server->tty.uart.baud) { |
| 240 | if (cfsetspeed(&termios, server->tty.uart.baud) < 0) { |
| 241 | warn("Couldn't set speeds for %s", server->tty.kname); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 242 | } |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /* Set console to raw mode: we don't want any processing to occur on |
| 246 | * the underlying terminal input/output. |
| 247 | */ |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 248 | cfmakeraw(&termios); |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 249 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 250 | rc = tcsetattr(server->tty.fd, TCSANOW, &termios); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 251 | if (rc) { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 252 | warn("Can't set terminal options for %s", server->tty.kname); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 253 | } |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 257 | * Open and initialise the serial device |
| 258 | */ |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 259 | static void tty_init_vuart_io(struct console_server *server) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 260 | { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 261 | assert(server->tty.type == TTY_DEVICE_VUART); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 262 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 263 | if (server->tty.vuart.sirq) { |
| 264 | tty_set_sysfs_attr(server, "sirq", server->tty.vuart.sirq); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 265 | } |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 266 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 267 | if (server->tty.vuart.lpc_addr) { |
| 268 | tty_set_sysfs_attr(server, "lpc_address", |
| 269 | server->tty.vuart.lpc_addr); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 273 | static int tty_init_io(struct console_server *server) |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 274 | { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 275 | server->tty.fd = open(server->tty.dev, O_RDWR); |
| 276 | if (server->tty.fd <= 0) { |
| 277 | warn("Can't open tty %s", server->tty.dev); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | /* Disable character delay. We may want to later enable this when |
| 282 | * we detect larger amounts of data |
| 283 | */ |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 284 | fcntl(server->tty.fd, F_SETFL, FNDELAY); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 285 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 286 | tty_init_termios(server); |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 287 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 288 | server->active->pollfds[server->active->n_pollers].fd = server->tty.fd; |
| 289 | server->active->pollfds[server->active->n_pollers].events = POLLIN; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 290 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 294 | static int tty_init_vuart(struct console_server *server, struct config *config) |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 295 | { |
Andrew Jeffery | fd883a8 | 2023-04-18 22:18:25 +0930 | [diff] [blame] | 296 | unsigned long parsed; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 297 | const char *val; |
| 298 | char *endp; |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 299 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 300 | assert(server->tty.type == TTY_DEVICE_VUART); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 301 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 302 | val = config_get_value(config, "lpc-address"); |
| 303 | if (val) { |
Andrew Jeffery | fd883a8 | 2023-04-18 22:18:25 +0930 | [diff] [blame] | 304 | errno = 0; |
| 305 | parsed = strtoul(val, &endp, 0); |
| 306 | if (parsed == ULONG_MAX && errno == ERANGE) { |
| 307 | warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'", |
| 308 | val); |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | if (parsed > UINT16_MAX) { |
| 313 | warn("Invalid LPC address '%s'", val); |
| 314 | return -1; |
| 315 | } |
| 316 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 317 | server->tty.vuart.lpc_addr = (uint16_t)parsed; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 318 | if (endp == optarg) { |
| 319 | warn("Invalid LPC address: '%s'", val); |
| 320 | return -1; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | val = config_get_value(config, "sirq"); |
| 325 | if (val) { |
Andrew Jeffery | fd883a8 | 2023-04-18 22:18:25 +0930 | [diff] [blame] | 326 | errno = 0; |
| 327 | parsed = strtoul(val, &endp, 0); |
| 328 | if (parsed == ULONG_MAX && errno == ERANGE) { |
| 329 | warn("Cannot interpret 'sirq' value as an unsigned long: '%s'", |
| 330 | val); |
| 331 | } |
| 332 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 333 | if (parsed > 16) { |
Andrew Jeffery | fd883a8 | 2023-04-18 22:18:25 +0930 | [diff] [blame] | 334 | warn("Invalid LPC SERIRQ: '%s'", val); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 335 | } |
Andrew Jeffery | fd883a8 | 2023-04-18 22:18:25 +0930 | [diff] [blame] | 336 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 337 | server->tty.vuart.sirq = (int)parsed; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 338 | if (endp == optarg) { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 339 | warn("Invalid sirq: '%s'", val); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 340 | } |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 341 | } |
| 342 | |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 343 | return 0; |
| 344 | } |
| 345 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 346 | static int tty_init(struct console_server *server, struct config *config, |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 347 | const char *tty_arg) |
| 348 | { |
| 349 | const char *val; |
| 350 | int rc; |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 351 | |
Andrew Jeffery | d769eec | 2023-05-03 20:00:09 +0930 | [diff] [blame] | 352 | if (tty_arg) { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 353 | server->tty.kname = tty_arg; |
Andrew Jeffery | d769eec | 2023-05-03 20:00:09 +0930 | [diff] [blame] | 354 | } else if ((val = config_get_value(config, "upstream-tty"))) { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 355 | server->tty.kname = val; |
Andrew Jeffery | d769eec | 2023-05-03 20:00:09 +0930 | [diff] [blame] | 356 | } else { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 357 | warnx("Error: No TTY device specified"); |
| 358 | return -1; |
| 359 | } |
| 360 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 361 | rc = tty_find_device(server); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 362 | if (rc) { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 363 | return rc; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 364 | } |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 365 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 366 | switch (server->tty.type) { |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 367 | case TTY_DEVICE_VUART: |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 368 | rc = tty_init_vuart(server, config); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 369 | if (rc) { |
| 370 | return rc; |
| 371 | } |
| 372 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 373 | tty_init_vuart_io(server); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 374 | break; |
| 375 | case TTY_DEVICE_UART: |
| 376 | val = config_get_value(config, "baud"); |
| 377 | if (val) { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 378 | if (config_parse_baud(&server->tty.uart.baud, val)) { |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 379 | warnx("Invalid baud rate: '%s'", val); |
| 380 | } |
| 381 | } |
| 382 | break; |
| 383 | case TTY_DEVICE_PTY: |
| 384 | break; |
| 385 | case TTY_DEVICE_UNDEFINED: |
| 386 | default: |
| 387 | warnx("Cannot configure unrecognised TTY device"); |
| 388 | return -1; |
| 389 | } |
| 390 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 391 | return tty_init_io(server); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 392 | } |
| 393 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 394 | static void tty_fini(struct console_server *server) |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 395 | { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 396 | if (server->tty.type == TTY_DEVICE_VUART) { |
| 397 | free(server->tty.vuart.sysfs_devnode); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 398 | } |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 399 | free(server->tty.dev); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 400 | } |
| 401 | |
Zev Weiss | 7dc08ba | 2023-09-12 20:49:14 -0700 | [diff] [blame] | 402 | static int write_to_path(const char *path, const char *data) |
| 403 | { |
| 404 | int rc = 0; |
| 405 | FILE *f = fopen(path, "w"); |
| 406 | if (!f) { |
| 407 | return -1; |
| 408 | } |
| 409 | |
| 410 | if (fprintf(f, "%s", data) < 0) { |
| 411 | rc = -1; |
| 412 | } |
| 413 | |
| 414 | if (fclose(f)) { |
| 415 | rc = -1; |
| 416 | } |
| 417 | |
| 418 | return rc; |
| 419 | } |
| 420 | |
| 421 | #define ASPEED_UART_ROUTING_PATTERN \ |
| 422 | "/sys/bus/platform/drivers/aspeed-uart-routing/*.uart-routing" |
| 423 | |
| 424 | static void uart_routing_init(struct config *config) |
| 425 | { |
| 426 | const char *muxcfg; |
| 427 | const char *p; |
| 428 | size_t buflen; |
| 429 | char *sink; |
| 430 | char *source; |
| 431 | char *muxdir; |
| 432 | char *path; |
| 433 | glob_t globbuf; |
| 434 | |
| 435 | muxcfg = config_get_value(config, "aspeed-uart-routing"); |
| 436 | if (!muxcfg) { |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | /* Find the driver's sysfs directory */ |
| 441 | if (glob(ASPEED_UART_ROUTING_PATTERN, GLOB_ERR | GLOB_NOSORT, NULL, |
| 442 | &globbuf) != 0) { |
| 443 | warn("Couldn't find uart-routing driver directory, cannot apply config"); |
| 444 | return; |
| 445 | } |
| 446 | if (globbuf.gl_pathc != 1) { |
| 447 | warnx("Found %zd uart-routing driver directories, cannot apply config", |
| 448 | globbuf.gl_pathc); |
| 449 | goto out_free_glob; |
| 450 | } |
| 451 | muxdir = globbuf.gl_pathv[0]; |
| 452 | |
| 453 | /* |
| 454 | * Rather than faff about tracking a bunch of separate buffer sizes, |
| 455 | * just use one (worst-case) size for all of them -- +2 for a trailing |
| 456 | * NUL and a '/' separator to construct the sysfs file path. |
| 457 | */ |
| 458 | buflen = strlen(muxdir) + strlen(muxcfg) + 2; |
| 459 | |
| 460 | sink = malloc(buflen); |
| 461 | source = malloc(buflen); |
| 462 | path = malloc(buflen); |
| 463 | if (!path || !sink || !source) { |
| 464 | warnx("Out of memory applying uart routing config"); |
| 465 | goto out_free_bufs; |
| 466 | } |
| 467 | |
| 468 | p = muxcfg; |
| 469 | while (*p) { |
| 470 | ssize_t bytes_scanned; |
| 471 | |
| 472 | if (sscanf(p, " %[^:/ \t]:%[^: \t] %zn", sink, source, |
| 473 | &bytes_scanned) != 2) { |
| 474 | warnx("Invalid syntax in aspeed uart config: '%s' not applied", |
| 475 | p); |
| 476 | break; |
| 477 | } |
| 478 | p += bytes_scanned; |
| 479 | |
| 480 | /* |
| 481 | * Check that the sink name looks reasonable before proceeding |
| 482 | * (there are other writable files in the same directory that |
| 483 | * we shouldn't be touching, such as 'driver_override' and |
| 484 | * 'uevent'). |
| 485 | */ |
| 486 | if (strncmp(sink, "io", strlen("io")) != 0 && |
| 487 | strncmp(sink, "uart", strlen("uart")) != 0) { |
| 488 | warnx("Skipping invalid uart routing name '%s' (must be ioN or uartN)", |
| 489 | sink); |
| 490 | continue; |
| 491 | } |
| 492 | |
| 493 | snprintf(path, buflen, "%s/%s", muxdir, sink); |
| 494 | if (write_to_path(path, source)) { |
| 495 | warn("Failed to apply uart-routing config '%s:%s'", |
| 496 | sink, source); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | out_free_bufs: |
| 501 | free(path); |
| 502 | free(source); |
| 503 | free(sink); |
| 504 | out_free_glob: |
| 505 | globfree(&globbuf); |
| 506 | } |
| 507 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 508 | 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] | 509 | { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 510 | return write_buf_to_fd(console->server->tty.fd, data, len); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 511 | } |
| 512 | |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 513 | /* Prepare a socket name */ |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 514 | static int set_socket_info(struct console *console, struct config *config, |
| 515 | const char *console_id) |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 516 | { |
| 517 | ssize_t len; |
| 518 | |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 519 | /* Get console id */ |
| 520 | console->console_id = config_resolve_console_id(config, console_id); |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 521 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 522 | /* Get the socket name/path */ |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 523 | len = console_socket_path(console->socket_name, console->console_id); |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 524 | if (len < 0) { |
| 525 | warn("Failed to set socket path: %s", strerror(errno)); |
| 526 | return EXIT_FAILURE; |
| 527 | } |
| 528 | |
| 529 | /* Socket name is not a null terminated string hence save the length */ |
| 530 | console->socket_name_len = len; |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
Jeremy Kerr | d47963e | 2016-03-16 17:29:55 +0800 | [diff] [blame] | 535 | static void handlers_init(struct console *console, struct config *config) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 536 | { |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 537 | /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */ |
Andrew Jeffery | adedc33 | 2024-07-10 10:48:13 +0930 | [diff] [blame] | 538 | extern const struct handler_type *const __start_handlers[]; |
| 539 | extern const struct handler_type *const __stop_handlers[]; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 540 | /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */ |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 541 | size_t n_types; |
| 542 | int j = 0; |
| 543 | size_t i; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 544 | |
Andrew Jeffery | adedc33 | 2024-07-10 10:48:13 +0930 | [diff] [blame] | 545 | n_types = __stop_handlers - __start_handlers; |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 546 | console->handlers = calloc(n_types, sizeof(struct handler *)); |
| 547 | if (!console->handlers) { |
| 548 | err(EXIT_FAILURE, "malloc(handlers)"); |
| 549 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 550 | |
Andrew Jeffery | 079fc51 | 2024-07-09 16:51:56 +0930 | [diff] [blame] | 551 | printf("%zu handler type%s\n", n_types, n_types == 1 ? "" : "s"); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 552 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 553 | for (i = 0; i < n_types; i++) { |
Andrew Jeffery | adedc33 | 2024-07-10 10:48:13 +0930 | [diff] [blame] | 554 | const struct handler_type *type = __start_handlers[i]; |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 555 | struct handler *handler; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 556 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 557 | /* Should be picked up at build time by |
| 558 | * console_handler_register, but check anyway |
| 559 | */ |
| 560 | if (!type->init || !type->fini) { |
| 561 | errx(EXIT_FAILURE, |
| 562 | "invalid handler type %s: no init() / fini()", |
| 563 | type->name); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 564 | } |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 565 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 566 | handler = type->init(type, console, config); |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 567 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 568 | printf(" console '%s': handler %s [%sactive]\n", |
| 569 | console->console_id, type->name, handler ? "" : "in"); |
| 570 | |
| 571 | if (handler) { |
| 572 | handler->type = type; |
| 573 | console->handlers[j++] = handler; |
| 574 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 575 | } |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 576 | |
| 577 | console->n_handlers = j; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 578 | } |
| 579 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 580 | static void handlers_fini(struct console *console) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 581 | { |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 582 | struct handler *handler; |
| 583 | int i; |
| 584 | |
| 585 | for (i = 0; i < console->n_handlers; i++) { |
| 586 | handler = console->handlers[i]; |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 587 | handler->type->fini(handler); |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 588 | } |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 589 | |
| 590 | free(console->handlers); |
| 591 | console->handlers = NULL; |
| 592 | console->n_handlers = 0; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 593 | } |
| 594 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 595 | static int get_current_time(struct timeval *tv) |
| 596 | { |
| 597 | struct timespec t; |
| 598 | int rc; |
| 599 | |
| 600 | /* |
| 601 | * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to |
| 602 | * local time changes. However, a struct timeval is more |
| 603 | * convenient for calculations, so convert to that. |
| 604 | */ |
| 605 | rc = clock_gettime(CLOCK_MONOTONIC, &t); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 606 | if (rc) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 607 | return rc; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 608 | } |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 609 | |
| 610 | tv->tv_sec = t.tv_sec; |
| 611 | tv->tv_usec = t.tv_nsec / 1000; |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 616 | struct ringbuffer_consumer * |
| 617 | console_ringbuffer_consumer_register(struct console *console, |
| 618 | ringbuffer_poll_fn_t poll_fn, void *data) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 619 | { |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 620 | return ringbuffer_consumer_register(console->rb, poll_fn, data); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 621 | } |
| 622 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 623 | struct poller *console_poller_register(struct console *console, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 624 | struct handler *handler, |
| 625 | poller_event_fn_t poller_fn, |
| 626 | poller_timeout_fn_t timeout_fn, int fd, |
| 627 | int events, void *data) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 628 | { |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 629 | struct poller *poller; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 630 | long n; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 631 | |
| 632 | poller = malloc(sizeof(*poller)); |
| 633 | poller->remove = false; |
| 634 | poller->handler = handler; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 635 | poller->event_fn = poller_fn; |
| 636 | poller->timeout_fn = timeout_fn; |
Andrew Jeffery | 9cc8459 | 2024-07-09 15:44:30 +0930 | [diff] [blame] | 637 | timerclear(&poller->timeout); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 638 | poller->data = data; |
| 639 | |
| 640 | /* add one to our pollers array */ |
| 641 | n = console->n_pollers++; |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 642 | /* |
| 643 | * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a |
| 644 | * pointer type. |
| 645 | */ |
| 646 | /* NOLINTBEGIN(bugprone-sizeof-expression) */ |
| 647 | console->pollers = reallocarray(console->pollers, console->n_pollers, |
| 648 | sizeof(*console->pollers)); |
| 649 | /* NOLINTEND(bugprone-sizeof-expression) */ |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 650 | |
| 651 | console->pollers[n] = poller; |
| 652 | |
| 653 | /* increase pollfds array too */ |
Andrew Jeffery | 4e44c79 | 2023-05-12 15:57:46 +0930 | [diff] [blame] | 654 | console->pollfds = reallocarray( |
| 655 | console->pollfds, (MAX_INTERNAL_POLLFD + console->n_pollers), |
| 656 | sizeof(*console->pollfds)); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 657 | |
| 658 | /* shift the end pollfds up by one */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 659 | memcpy(&console->pollfds[n + 1], &console->pollfds[n], |
| 660 | sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 661 | |
| 662 | console->pollfds[n].fd = fd; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 663 | console->pollfds[n].events = (short)(events & 0x7fff); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 664 | |
| 665 | return poller; |
| 666 | } |
| 667 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 668 | void console_poller_unregister(struct console *console, struct poller *poller) |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 669 | { |
| 670 | int i; |
| 671 | |
| 672 | /* find the entry in our pollers array */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 673 | for (i = 0; i < console->n_pollers; i++) { |
| 674 | if (console->pollers[i] == poller) { |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 675 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 676 | } |
| 677 | } |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 678 | |
| 679 | assert(i < console->n_pollers); |
| 680 | |
| 681 | console->n_pollers--; |
| 682 | |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 683 | /* |
| 684 | * Remove the item from the pollers array... |
| 685 | * |
| 686 | * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a |
| 687 | * pointer type. |
| 688 | */ |
| 689 | /* NOLINTBEGIN(bugprone-sizeof-expression) */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 690 | memmove(&console->pollers[i], &console->pollers[i + 1], |
| 691 | sizeof(*console->pollers) * (console->n_pollers - i)); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 692 | |
Andrew Jeffery | 7851a39 | 2024-07-09 16:30:22 +0930 | [diff] [blame] | 693 | if (console->n_pollers == 0) { |
| 694 | free(console->pollers); |
| 695 | console->pollers = NULL; |
| 696 | } else { |
| 697 | console->pollers = reallocarray(console->pollers, |
| 698 | console->n_pollers, |
| 699 | sizeof(*console->pollers)); |
| 700 | } |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 701 | /* NOLINTEND(bugprone-sizeof-expression) */ |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 702 | |
| 703 | /* ... and the pollfds array */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 704 | memmove(&console->pollfds[i], &console->pollfds[i + 1], |
| 705 | sizeof(*console->pollfds) * |
| 706 | (MAX_INTERNAL_POLLFD + console->n_pollers - i)); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 707 | |
Andrew Jeffery | 4e44c79 | 2023-05-12 15:57:46 +0930 | [diff] [blame] | 708 | console->pollfds = reallocarray( |
| 709 | console->pollfds, (MAX_INTERNAL_POLLFD + console->n_pollers), |
| 710 | sizeof(*console->pollfds)); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 711 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 712 | free(poller); |
| 713 | } |
| 714 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 715 | void console_poller_set_events(struct console *console, struct poller *poller, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 716 | int events) |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 717 | { |
| 718 | int i; |
| 719 | |
| 720 | /* find the entry in our pollers array */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 721 | for (i = 0; i < console->n_pollers; i++) { |
| 722 | if (console->pollers[i] == poller) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 723 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 724 | } |
| 725 | } |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 726 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 727 | console->pollfds[i].events = (short)(events & 0x7fff); |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 728 | } |
| 729 | |
Andrew Jeffery | fd04832 | 2023-04-18 12:02:01 +0930 | [diff] [blame] | 730 | void console_poller_set_timeout(struct console *console __attribute__((unused)), |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 731 | struct poller *poller, const struct timeval *tv) |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 732 | { |
| 733 | struct timeval now; |
| 734 | int rc; |
| 735 | |
| 736 | rc = get_current_time(&now); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 737 | if (rc) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 738 | return; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 739 | } |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 740 | |
| 741 | timeradd(&now, tv, &poller->timeout); |
| 742 | } |
| 743 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 744 | static long get_poll_timeout(struct console *console, struct timeval *cur_time) |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 745 | { |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 746 | struct timeval *earliest; |
| 747 | struct timeval interval; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 748 | struct poller *poller; |
| 749 | int i; |
| 750 | |
| 751 | earliest = NULL; |
| 752 | |
| 753 | for (i = 0; i < console->n_pollers; i++) { |
| 754 | poller = console->pollers[i]; |
| 755 | |
| 756 | if (poller->timeout_fn && timerisset(&poller->timeout) && |
| 757 | (!earliest || |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 758 | (earliest && timercmp(&poller->timeout, earliest, <)))) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 759 | // poller is buffering data and needs the poll |
| 760 | // function to timeout. |
| 761 | earliest = &poller->timeout; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | if (earliest) { |
| 766 | if (timercmp(earliest, cur_time, >)) { |
| 767 | /* recalculate the timeout period, time period has |
| 768 | * not elapsed */ |
| 769 | timersub(earliest, cur_time, &interval); |
| 770 | return ((interval.tv_sec * 1000) + |
| 771 | (interval.tv_usec / 1000)); |
Andrew Jeffery | 0b7b047 | 2023-04-19 12:48:51 +0930 | [diff] [blame] | 772 | } /* return from poll immediately */ |
| 773 | return 0; |
| 774 | |
| 775 | } /* poll indefinitely */ |
| 776 | return -1; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | static int call_pollers(struct console *console, struct timeval *cur_time) |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 780 | { |
| 781 | struct poller *poller; |
| 782 | struct pollfd *pollfd; |
| 783 | enum poller_ret prc; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 784 | int i; |
| 785 | int rc; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 786 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 787 | rc = 0; |
| 788 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 789 | /* |
| 790 | * Process poll events by iterating through the pollers and pollfds |
| 791 | * in-step, calling any pollers that we've found revents for. |
| 792 | */ |
| 793 | for (i = 0; i < console->n_pollers; i++) { |
| 794 | poller = console->pollers[i]; |
| 795 | pollfd = &console->pollfds[i]; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 796 | prc = POLLER_OK; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 797 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 798 | /* process pending events... */ |
| 799 | if (pollfd->revents) { |
| 800 | prc = poller->event_fn(poller->handler, pollfd->revents, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 801 | poller->data); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 802 | if (prc == POLLER_EXIT) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 803 | rc = -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 804 | } else if (prc == POLLER_REMOVE) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 805 | poller->remove = true; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 806 | } |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 807 | } |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 808 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 809 | if ((prc == POLLER_OK) && poller->timeout_fn && |
| 810 | timerisset(&poller->timeout) && |
| 811 | timercmp(&poller->timeout, cur_time, <=)) { |
| 812 | /* One of the ringbuffer consumers is buffering the |
| 813 | data stream. The amount of idle time the consumer |
| 814 | desired has expired. Process the buffered data for |
| 815 | transmission. */ |
| 816 | timerclear(&poller->timeout); |
| 817 | prc = poller->timeout_fn(poller->handler, poller->data); |
| 818 | if (prc == POLLER_EXIT) { |
| 819 | rc = -1; |
| 820 | } else if (prc == POLLER_REMOVE) { |
| 821 | poller->remove = true; |
| 822 | } |
| 823 | } |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | /** |
| 827 | * Process deferred removals; restarting each time we unregister, as |
| 828 | * the array will have changed |
| 829 | */ |
| 830 | for (;;) { |
| 831 | bool removed = false; |
| 832 | |
| 833 | for (i = 0; i < console->n_pollers; i++) { |
| 834 | poller = console->pollers[i]; |
| 835 | if (poller->remove) { |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 836 | console_poller_unregister(console, poller); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 837 | removed = true; |
| 838 | break; |
| 839 | } |
| 840 | } |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 841 | if (!removed) { |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 842 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 843 | } |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | return rc; |
| 847 | } |
| 848 | |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 849 | static void sighandler(int signal) |
| 850 | { |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 851 | if (signal == SIGINT) { |
Andrew Jeffery | 553cb66 | 2024-07-09 15:14:56 +0930 | [diff] [blame] | 852 | sigint = 1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 853 | } |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 854 | } |
| 855 | |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 856 | static int run_console_iteration(struct console *console) |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 857 | { |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 858 | uint8_t buf[4096]; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 859 | struct timeval tv; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 860 | long timeout; |
| 861 | ssize_t rc; |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 862 | |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 863 | if (console->rb->size < sizeof(buf)) { |
| 864 | fprintf(stderr, "Ringbuffer size should be greater than %zuB\n", |
| 865 | sizeof(buf)); |
| 866 | return -1; |
| 867 | } |
| 868 | |
| 869 | if (sigint) { |
| 870 | fprintf(stderr, "Received interrupt, exiting\n"); |
| 871 | return -1; |
| 872 | } |
| 873 | |
| 874 | rc = get_current_time(&tv); |
| 875 | if (rc) { |
| 876 | warn("Failed to read current time"); |
| 877 | return -1; |
| 878 | } |
| 879 | |
| 880 | timeout = get_poll_timeout(console, &tv); |
| 881 | |
| 882 | rc = poll(console->pollfds, console->n_pollers + MAX_INTERNAL_POLLFD, |
| 883 | (int)timeout); |
| 884 | |
| 885 | if (rc < 0) { |
| 886 | if (errno == EINTR) { |
| 887 | return 0; |
| 888 | } |
| 889 | warn("poll error"); |
| 890 | return -1; |
| 891 | } |
| 892 | |
| 893 | /* process internal fd first */ |
| 894 | if (console->pollfds[console->n_pollers].revents) { |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 895 | rc = read(console->server->tty.fd, buf, sizeof(buf)); |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 896 | if (rc <= 0) { |
| 897 | warn("Error reading from tty device"); |
| 898 | return -1; |
| 899 | } |
| 900 | rc = ringbuffer_queue(console->rb, buf, rc); |
| 901 | if (rc) { |
| 902 | return -1; |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | if (console->pollfds[console->n_pollers + 1].revents) { |
| 907 | sd_bus_process(console->bus, NULL); |
| 908 | } |
| 909 | |
| 910 | /* ... and then the pollers */ |
| 911 | rc = call_pollers(console, &tv); |
| 912 | if (rc) { |
| 913 | return -1; |
| 914 | } |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | int run_console(struct console *console) |
| 919 | { |
| 920 | sighandler_t sighandler_save = signal(SIGINT, sighandler); |
| 921 | ssize_t rc = 0; |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 922 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 923 | for (;;) { |
Alexander Hansen | 6925740 | 2024-06-07 15:17:09 +0200 | [diff] [blame] | 924 | rc = run_console_iteration(console); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 925 | if (rc) { |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 926 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 927 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 928 | } |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 929 | |
| 930 | signal(SIGINT, sighandler_save); |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 931 | sd_bus_unref(console->bus); |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 932 | |
| 933 | return rc ? -1 : 0; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 934 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 935 | static const struct option options[] = { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 936 | { "config", required_argument, 0, 'c' }, |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 937 | { "console-id", required_argument, 0, 'i' }, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 938 | { 0, 0, 0, 0 }, |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 939 | }; |
| 940 | |
| 941 | int main(int argc, char **argv) |
| 942 | { |
Medicine Yeh | 7f2bfb9 | 2024-03-10 23:19:41 -0700 | [diff] [blame] | 943 | size_t buffer_size = default_buffer_size; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 944 | const char *config_filename = NULL; |
Vishwanatha Subbanna | 6221ce9 | 2016-07-20 05:35:45 -0500 | [diff] [blame] | 945 | const char *config_tty_kname = NULL; |
Medicine Yeh | 7f2bfb9 | 2024-03-10 23:19:41 -0700 | [diff] [blame] | 946 | const char *buffer_size_str = NULL; |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 947 | const char *console_id = NULL; |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 948 | struct console_server server = { 0 }; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 949 | struct console *console; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 950 | struct config *config; |
| 951 | int rc; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 952 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 953 | for (;;) { |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 954 | int c; |
| 955 | int idx; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 956 | |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 957 | c = getopt_long(argc, argv, "c:i:", options, &idx); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 958 | if (c == -1) { |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 959 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 960 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 961 | |
| 962 | switch (c) { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 963 | case 'c': |
| 964 | config_filename = optarg; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 965 | break; |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 966 | case 'i': |
| 967 | console_id = optarg; |
| 968 | break; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 969 | case 'h': |
| 970 | case '?': |
| 971 | usage(argv[0]); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 972 | return EXIT_SUCCESS; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 976 | if (optind < argc) { |
Andrew Jeffery | 91dde14 | 2020-02-12 22:46:27 +1030 | [diff] [blame] | 977 | config_tty_kname = argv[optind]; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 978 | } |
Vishwanatha Subbanna | 6221ce9 | 2016-07-20 05:35:45 -0500 | [diff] [blame] | 979 | |
Medicine Yeh | 7f2bfb9 | 2024-03-10 23:19:41 -0700 | [diff] [blame] | 980 | config = config_init(config_filename); |
Andrew Jeffery | 28a1761 | 2024-07-10 09:32:05 +0930 | [diff] [blame] | 981 | if (!config) { |
| 982 | return EXIT_FAILURE; |
| 983 | } |
Medicine Yeh | 7f2bfb9 | 2024-03-10 23:19:41 -0700 | [diff] [blame] | 984 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 985 | console = malloc(sizeof(struct console)); |
Andrew Jeffery | 28a1761 | 2024-07-10 09:32:05 +0930 | [diff] [blame] | 986 | if (!console) { |
| 987 | rc = -1; |
| 988 | goto out_config_fini; |
| 989 | } |
| 990 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 991 | memset(console, 0, sizeof(*console)); |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 992 | |
| 993 | server.active = console; |
| 994 | console->server = &server; |
| 995 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 996 | console->pollfds = |
| 997 | calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds)); |
Andrew Jeffery | 28a1761 | 2024-07-10 09:32:05 +0930 | [diff] [blame] | 998 | |
Medicine Yeh | 7f2bfb9 | 2024-03-10 23:19:41 -0700 | [diff] [blame] | 999 | buffer_size_str = config_get_value(config, "ringbuffer-size"); |
| 1000 | if (buffer_size_str) { |
| 1001 | rc = config_parse_bytesize(buffer_size_str, &buffer_size); |
| 1002 | if (rc) { |
| 1003 | warn("Invalid ringbuffer-size. Default to %zukB", |
| 1004 | buffer_size >> 10); |
| 1005 | } |
| 1006 | } |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 1007 | console->rb = ringbuffer_init(buffer_size); |
Andrew Jeffery | 3f8d5be | 2024-07-09 14:40:50 +0930 | [diff] [blame] | 1008 | if (!console->rb) { |
| 1009 | rc = -1; |
Andrew Jeffery | 28a1761 | 2024-07-10 09:32:05 +0930 | [diff] [blame] | 1010 | goto out_console_fini; |
Andrew Jeffery | 3f8d5be | 2024-07-09 14:40:50 +0930 | [diff] [blame] | 1011 | } |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 1012 | |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 1013 | if (set_socket_info(console, config, console_id)) { |
Andrew Jeffery | 29c59c4 | 2023-05-03 19:33:07 +0930 | [diff] [blame] | 1014 | rc = -1; |
Andrew Jeffery | 3f8d5be | 2024-07-09 14:40:50 +0930 | [diff] [blame] | 1015 | goto out_ringbuffer_fini; |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 1016 | } |
| 1017 | |
Zev Weiss | 7dc08ba | 2023-09-12 20:49:14 -0700 | [diff] [blame] | 1018 | uart_routing_init(config); |
| 1019 | |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 1020 | rc = tty_init(&server, config, config_tty_kname); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 1021 | if (rc) { |
Andrew Jeffery | 3f8d5be | 2024-07-09 14:40:50 +0930 | [diff] [blame] | 1022 | goto out_ringbuffer_fini; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 1023 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1024 | |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 1025 | rc = dbus_init(console, config); |
| 1026 | if (rc) { |
| 1027 | goto out_tty_fini; |
| 1028 | } |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 1029 | |
Jeremy Kerr | d47963e | 2016-03-16 17:29:55 +0800 | [diff] [blame] | 1030 | handlers_init(console, config); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1031 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 1032 | rc = run_console(console); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1033 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 1034 | handlers_fini(console); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1035 | |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 1036 | out_tty_fini: |
Alexander Hansen | c2b0d8c | 2024-06-07 14:35:44 +0200 | [diff] [blame] | 1037 | tty_fini(&server); |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 1038 | |
Andrew Jeffery | 3f8d5be | 2024-07-09 14:40:50 +0930 | [diff] [blame] | 1039 | out_ringbuffer_fini: |
| 1040 | ringbuffer_fini(console->rb); |
| 1041 | |
Andrew Jeffery | 28a1761 | 2024-07-10 09:32:05 +0930 | [diff] [blame] | 1042 | out_console_fini: |
Jeremy Kerr | 89ea819 | 2016-03-15 17:57:43 +0800 | [diff] [blame] | 1043 | free(console->pollers); |
| 1044 | free(console->pollfds); |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 1045 | free(console); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1046 | |
Andrew Jeffery | 28a1761 | 2024-07-10 09:32:05 +0930 | [diff] [blame] | 1047 | out_config_fini: |
| 1048 | config_fini(config); |
| 1049 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 1050 | return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
| 1051 | } |