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