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 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 45 | /* size of the shared backlog ringbuffer */ |
Andrew Jeffery | 5db8c79 | 2023-04-18 21:48:24 +0930 | [diff] [blame] | 46 | const size_t 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) */ |
| 536 | extern struct handler *__start_handlers; |
| 537 | extern struct handler *__stop_handlers; |
| 538 | /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */ |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 539 | struct handler *handler; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 540 | int i; |
| 541 | int rc; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 542 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 543 | console->n_handlers = &__stop_handlers - &__start_handlers; |
| 544 | console->handlers = &__start_handlers; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 545 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 546 | printf("%ld handler%s\n", console->n_handlers, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 547 | console->n_handlers == 1 ? "" : "s"); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 548 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 549 | for (i = 0; i < console->n_handlers; i++) { |
| 550 | handler = console->handlers[i]; |
| 551 | |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 552 | rc = 0; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 553 | if (handler->init) { |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 554 | rc = handler->init(handler, console, config); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 555 | } |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 556 | |
| 557 | handler->active = rc == 0; |
| 558 | |
| 559 | printf(" %s [%sactive]\n", handler->name, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 560 | handler->active ? "" : "in"); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 561 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 562 | } |
| 563 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 564 | static void handlers_fini(struct console *console) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 565 | { |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 566 | struct handler *handler; |
| 567 | int i; |
| 568 | |
| 569 | for (i = 0; i < console->n_handlers; i++) { |
| 570 | handler = console->handlers[i]; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 571 | if (handler->fini && handler->active) { |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 572 | handler->fini(handler); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 573 | } |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 574 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 575 | } |
| 576 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 577 | static int get_current_time(struct timeval *tv) |
| 578 | { |
| 579 | struct timespec t; |
| 580 | int rc; |
| 581 | |
| 582 | /* |
| 583 | * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to |
| 584 | * local time changes. However, a struct timeval is more |
| 585 | * convenient for calculations, so convert to that. |
| 586 | */ |
| 587 | rc = clock_gettime(CLOCK_MONOTONIC, &t); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 588 | if (rc) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 589 | return rc; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 590 | } |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 591 | |
| 592 | tv->tv_sec = t.tv_sec; |
| 593 | tv->tv_usec = t.tv_nsec / 1000; |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 598 | struct ringbuffer_consumer * |
| 599 | console_ringbuffer_consumer_register(struct console *console, |
| 600 | ringbuffer_poll_fn_t poll_fn, void *data) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 601 | { |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 602 | return ringbuffer_consumer_register(console->rb, poll_fn, data); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 603 | } |
| 604 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 605 | struct poller *console_poller_register(struct console *console, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 606 | struct handler *handler, |
| 607 | poller_event_fn_t poller_fn, |
| 608 | poller_timeout_fn_t timeout_fn, int fd, |
| 609 | int events, void *data) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 610 | { |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 611 | struct poller *poller; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 612 | long n; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 613 | |
| 614 | poller = malloc(sizeof(*poller)); |
| 615 | poller->remove = false; |
| 616 | poller->handler = handler; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 617 | poller->event_fn = poller_fn; |
| 618 | poller->timeout_fn = timeout_fn; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 619 | poller->data = data; |
| 620 | |
| 621 | /* add one to our pollers array */ |
| 622 | n = console->n_pollers++; |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 623 | /* |
| 624 | * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a |
| 625 | * pointer type. |
| 626 | */ |
| 627 | /* NOLINTBEGIN(bugprone-sizeof-expression) */ |
| 628 | console->pollers = reallocarray(console->pollers, console->n_pollers, |
| 629 | sizeof(*console->pollers)); |
| 630 | /* NOLINTEND(bugprone-sizeof-expression) */ |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 631 | |
| 632 | console->pollers[n] = poller; |
| 633 | |
| 634 | /* increase pollfds array too */ |
Andrew Jeffery | 4e44c79 | 2023-05-12 15:57:46 +0930 | [diff] [blame] | 635 | console->pollfds = reallocarray( |
| 636 | console->pollfds, (MAX_INTERNAL_POLLFD + console->n_pollers), |
| 637 | sizeof(*console->pollfds)); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 638 | |
| 639 | /* shift the end pollfds up by one */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 640 | memcpy(&console->pollfds[n + 1], &console->pollfds[n], |
| 641 | sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 642 | |
| 643 | console->pollfds[n].fd = fd; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 644 | console->pollfds[n].events = (short)(events & 0x7fff); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 645 | |
| 646 | return poller; |
| 647 | } |
| 648 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 649 | void console_poller_unregister(struct console *console, struct poller *poller) |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 650 | { |
| 651 | int i; |
| 652 | |
| 653 | /* find the entry in our pollers array */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 654 | for (i = 0; i < console->n_pollers; i++) { |
| 655 | if (console->pollers[i] == poller) { |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 656 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 657 | } |
| 658 | } |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 659 | |
| 660 | assert(i < console->n_pollers); |
| 661 | |
| 662 | console->n_pollers--; |
| 663 | |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 664 | /* |
| 665 | * Remove the item from the pollers array... |
| 666 | * |
| 667 | * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a |
| 668 | * pointer type. |
| 669 | */ |
| 670 | /* NOLINTBEGIN(bugprone-sizeof-expression) */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 671 | memmove(&console->pollers[i], &console->pollers[i + 1], |
| 672 | sizeof(*console->pollers) * (console->n_pollers - i)); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 673 | |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 674 | console->pollers = reallocarray(console->pollers, console->n_pollers, |
| 675 | sizeof(*console->pollers)); |
| 676 | /* NOLINTEND(bugprone-sizeof-expression) */ |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 677 | |
| 678 | /* ... and the pollfds array */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 679 | memmove(&console->pollfds[i], &console->pollfds[i + 1], |
| 680 | sizeof(*console->pollfds) * |
| 681 | (MAX_INTERNAL_POLLFD + console->n_pollers - i)); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 682 | |
Andrew Jeffery | 4e44c79 | 2023-05-12 15:57:46 +0930 | [diff] [blame] | 683 | console->pollfds = reallocarray( |
| 684 | console->pollfds, (MAX_INTERNAL_POLLFD + console->n_pollers), |
| 685 | sizeof(*console->pollfds)); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 686 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 687 | free(poller); |
| 688 | } |
| 689 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 690 | void console_poller_set_events(struct console *console, struct poller *poller, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 691 | int events) |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 692 | { |
| 693 | int i; |
| 694 | |
| 695 | /* find the entry in our pollers array */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 696 | for (i = 0; i < console->n_pollers; i++) { |
| 697 | if (console->pollers[i] == poller) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 698 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 699 | } |
| 700 | } |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 701 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 702 | console->pollfds[i].events = (short)(events & 0x7fff); |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 703 | } |
| 704 | |
Andrew Jeffery | fd04832 | 2023-04-18 12:02:01 +0930 | [diff] [blame] | 705 | void console_poller_set_timeout(struct console *console __attribute__((unused)), |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 706 | struct poller *poller, const struct timeval *tv) |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 707 | { |
| 708 | struct timeval now; |
| 709 | int rc; |
| 710 | |
| 711 | rc = get_current_time(&now); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 712 | if (rc) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 713 | return; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 714 | } |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 715 | |
| 716 | timeradd(&now, tv, &poller->timeout); |
| 717 | } |
| 718 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 719 | static long get_poll_timeout(struct console *console, struct timeval *cur_time) |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 720 | { |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 721 | struct timeval *earliest; |
| 722 | struct timeval interval; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 723 | struct poller *poller; |
| 724 | int i; |
| 725 | |
| 726 | earliest = NULL; |
| 727 | |
| 728 | for (i = 0; i < console->n_pollers; i++) { |
| 729 | poller = console->pollers[i]; |
| 730 | |
| 731 | if (poller->timeout_fn && timerisset(&poller->timeout) && |
| 732 | (!earliest || |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 733 | (earliest && timercmp(&poller->timeout, earliest, <)))) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 734 | // poller is buffering data and needs the poll |
| 735 | // function to timeout. |
| 736 | earliest = &poller->timeout; |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | if (earliest) { |
| 741 | if (timercmp(earliest, cur_time, >)) { |
| 742 | /* recalculate the timeout period, time period has |
| 743 | * not elapsed */ |
| 744 | timersub(earliest, cur_time, &interval); |
| 745 | return ((interval.tv_sec * 1000) + |
| 746 | (interval.tv_usec / 1000)); |
Andrew Jeffery | 0b7b047 | 2023-04-19 12:48:51 +0930 | [diff] [blame] | 747 | } /* return from poll immediately */ |
| 748 | return 0; |
| 749 | |
| 750 | } /* poll indefinitely */ |
| 751 | return -1; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | static int call_pollers(struct console *console, struct timeval *cur_time) |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 755 | { |
| 756 | struct poller *poller; |
| 757 | struct pollfd *pollfd; |
| 758 | enum poller_ret prc; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 759 | int i; |
| 760 | int rc; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 761 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 762 | rc = 0; |
| 763 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 764 | /* |
| 765 | * Process poll events by iterating through the pollers and pollfds |
| 766 | * in-step, calling any pollers that we've found revents for. |
| 767 | */ |
| 768 | for (i = 0; i < console->n_pollers; i++) { |
| 769 | poller = console->pollers[i]; |
| 770 | pollfd = &console->pollfds[i]; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 771 | prc = POLLER_OK; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 772 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 773 | /* process pending events... */ |
| 774 | if (pollfd->revents) { |
| 775 | prc = poller->event_fn(poller->handler, pollfd->revents, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 776 | poller->data); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 777 | if (prc == POLLER_EXIT) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 778 | rc = -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 779 | } else if (prc == POLLER_REMOVE) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 780 | poller->remove = true; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 781 | } |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 782 | } |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 783 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 784 | if ((prc == POLLER_OK) && poller->timeout_fn && |
| 785 | timerisset(&poller->timeout) && |
| 786 | timercmp(&poller->timeout, cur_time, <=)) { |
| 787 | /* One of the ringbuffer consumers is buffering the |
| 788 | data stream. The amount of idle time the consumer |
| 789 | desired has expired. Process the buffered data for |
| 790 | transmission. */ |
| 791 | timerclear(&poller->timeout); |
| 792 | prc = poller->timeout_fn(poller->handler, poller->data); |
| 793 | if (prc == POLLER_EXIT) { |
| 794 | rc = -1; |
| 795 | } else if (prc == POLLER_REMOVE) { |
| 796 | poller->remove = true; |
| 797 | } |
| 798 | } |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /** |
| 802 | * Process deferred removals; restarting each time we unregister, as |
| 803 | * the array will have changed |
| 804 | */ |
| 805 | for (;;) { |
| 806 | bool removed = false; |
| 807 | |
| 808 | for (i = 0; i < console->n_pollers; i++) { |
| 809 | poller = console->pollers[i]; |
| 810 | if (poller->remove) { |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 811 | console_poller_unregister(console, poller); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 812 | removed = true; |
| 813 | break; |
| 814 | } |
| 815 | } |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 816 | if (!removed) { |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 817 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 818 | } |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | return rc; |
| 822 | } |
| 823 | |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 824 | static void sighandler(int signal) |
| 825 | { |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 826 | if (signal == SIGINT) { |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 827 | sigint = true; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 828 | } |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 829 | } |
| 830 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 831 | int run_console(struct console *console) |
| 832 | { |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 833 | sighandler_t sighandler_save = signal(SIGINT, sighandler); |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 834 | struct timeval tv; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 835 | long timeout; |
| 836 | ssize_t rc; |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 837 | |
| 838 | rc = 0; |
| 839 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 840 | for (;;) { |
| 841 | uint8_t buf[4096]; |
| 842 | |
Jeremy Kerr | 1764145 | 2017-02-07 22:09:13 +0800 | [diff] [blame] | 843 | BUILD_ASSERT(sizeof(buf) <= buffer_size); |
| 844 | |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 845 | if (sigint) { |
| 846 | fprintf(stderr, "Received interrupt, exiting\n"); |
| 847 | break; |
| 848 | } |
| 849 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 850 | rc = get_current_time(&tv); |
| 851 | if (rc) { |
| 852 | warn("Failed to read current time"); |
| 853 | break; |
| 854 | } |
| 855 | |
| 856 | timeout = get_poll_timeout(console, &tv); |
| 857 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 858 | rc = poll(console->pollfds, |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 859 | console->n_pollers + MAX_INTERNAL_POLLFD, |
| 860 | (int)timeout); |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 861 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 862 | if (rc < 0) { |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 863 | if (errno == EINTR) { |
| 864 | continue; |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 865 | } |
Andrew Jeffery | 0b7b047 | 2023-04-19 12:48:51 +0930 | [diff] [blame] | 866 | warn("poll error"); |
| 867 | break; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 868 | } |
| 869 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 870 | /* process internal fd first */ |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 871 | if (console->pollfds[console->n_pollers].revents) { |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 872 | rc = read(console->tty.fd, buf, sizeof(buf)); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 873 | if (rc <= 0) { |
| 874 | warn("Error reading from tty device"); |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 875 | rc = -1; |
| 876 | break; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 877 | } |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 878 | rc = ringbuffer_queue(console->rb, buf, rc); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 879 | if (rc) { |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 880 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 881 | } |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 882 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 883 | |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 884 | if (console->pollfds[console->n_pollers + 1].revents) { |
| 885 | sd_bus_process(console->bus, NULL); |
| 886 | } |
| 887 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 888 | /* ... and then the pollers */ |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 889 | rc = call_pollers(console, &tv); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 890 | if (rc) { |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 891 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 892 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 893 | } |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 894 | |
| 895 | signal(SIGINT, sighandler_save); |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 896 | sd_bus_unref(console->bus); |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 897 | |
| 898 | return rc ? -1 : 0; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 899 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 900 | static const struct option options[] = { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 901 | { "config", required_argument, 0, 'c' }, |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 902 | { "console-id", required_argument, 0, 'i' }, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 903 | { 0, 0, 0, 0 }, |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 904 | }; |
| 905 | |
| 906 | int main(int argc, char **argv) |
| 907 | { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 908 | const char *config_filename = NULL; |
Vishwanatha Subbanna | 6221ce9 | 2016-07-20 05:35:45 -0500 | [diff] [blame] | 909 | const char *config_tty_kname = NULL; |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 910 | const char *console_id = NULL; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 911 | struct console *console; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 912 | struct config *config; |
| 913 | int rc; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 914 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 915 | for (;;) { |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 916 | int c; |
| 917 | int idx; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 918 | |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 919 | c = getopt_long(argc, argv, "c:i:", options, &idx); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 920 | if (c == -1) { |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 921 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 922 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 923 | |
| 924 | switch (c) { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 925 | case 'c': |
| 926 | config_filename = optarg; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 927 | break; |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 928 | case 'i': |
| 929 | console_id = optarg; |
| 930 | break; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 931 | case 'h': |
| 932 | case '?': |
| 933 | usage(argv[0]); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 934 | return EXIT_SUCCESS; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 938 | if (optind < argc) { |
Andrew Jeffery | 91dde14 | 2020-02-12 22:46:27 +1030 | [diff] [blame] | 939 | config_tty_kname = argv[optind]; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 940 | } |
Vishwanatha Subbanna | 6221ce9 | 2016-07-20 05:35:45 -0500 | [diff] [blame] | 941 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 942 | console = malloc(sizeof(struct console)); |
| 943 | memset(console, 0, sizeof(*console)); |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 944 | console->pollfds = |
| 945 | calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds)); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 946 | console->rb = ringbuffer_init(buffer_size); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 947 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 948 | config = config_init(config_filename); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 949 | |
Andrew Jeffery | 954be0f | 2023-05-03 21:01:59 +0930 | [diff] [blame] | 950 | if (set_socket_info(console, config, console_id)) { |
Andrew Jeffery | 29c59c4 | 2023-05-03 19:33:07 +0930 | [diff] [blame] | 951 | rc = -1; |
| 952 | goto out_config_fini; |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 953 | } |
| 954 | |
Zev Weiss | 7dc08ba | 2023-09-12 20:49:14 -0700 | [diff] [blame] | 955 | uart_routing_init(config); |
| 956 | |
Andrew Jeffery | d769eec | 2023-05-03 20:00:09 +0930 | [diff] [blame] | 957 | rc = tty_init(console, config, config_tty_kname); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 958 | if (rc) { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 959 | goto out_config_fini; |
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 | |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 962 | dbus_init(console, config); |
| 963 | |
Jeremy Kerr | d47963e | 2016-03-16 17:29:55 +0800 | [diff] [blame] | 964 | handlers_init(console, config); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 965 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 966 | rc = run_console(console); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 967 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 968 | handlers_fini(console); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 969 | |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 970 | tty_fini(console); |
| 971 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 972 | out_config_fini: |
| 973 | config_fini(config); |
| 974 | |
Jeremy Kerr | 89ea819 | 2016-03-15 17:57:43 +0800 | [diff] [blame] | 975 | free(console->pollers); |
| 976 | free(console->pollfds); |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 977 | free(console); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 978 | |
| 979 | return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
| 980 | } |