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