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