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