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