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> |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 34 | #include <termios.h> |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 35 | |
| 36 | #include <sys/types.h> |
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 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 41 | struct console { |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 42 | const char *tty_kname; |
| 43 | char *tty_sysfs_devnode; |
| 44 | char *tty_dev; |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 45 | int tty_sirq; |
| 46 | int tty_lpc_addr; |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 47 | speed_t tty_baud; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 48 | int tty_fd; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 49 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 50 | struct ringbuffer *rb; |
| 51 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 52 | struct handler **handlers; |
| 53 | int n_handlers; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 54 | |
| 55 | struct poller **pollers; |
| 56 | int n_pollers; |
| 57 | |
| 58 | struct pollfd *pollfds; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 59 | }; |
| 60 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 61 | struct poller { |
| 62 | struct handler *handler; |
| 63 | void *data; |
| 64 | poller_fn_t fn; |
| 65 | bool remove; |
| 66 | }; |
| 67 | |
| 68 | /* we have one extra entry in the pollfds array for the VUART tty */ |
| 69 | static const int n_internal_pollfds = 1; |
| 70 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 71 | /* size of the shared backlog ringbuffer */ |
| 72 | const size_t buffer_size = 128 * 1024; |
| 73 | |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 74 | /* state shared with the signal handler */ |
| 75 | static bool sigint; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 76 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 77 | static void usage(const char *progname) |
| 78 | { |
| 79 | fprintf(stderr, |
Vishwanatha Subbanna | 6221ce9 | 2016-07-20 05:35:45 -0500 | [diff] [blame] | 80 | "usage: %s [options] <DEVICE>\n" |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 81 | "\n" |
| 82 | "Options:\n" |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 83 | " --config <FILE> Use FILE for configuration\n" |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 84 | "", |
| 85 | progname); |
| 86 | } |
| 87 | |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 88 | /* populates tty_dev and tty_sysfs_devnode, using the tty kernel name */ |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 89 | static int tty_find_device(struct console *console) |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 90 | { |
| 91 | char *tty_class_device_link; |
| 92 | char *tty_device_tty_dir; |
| 93 | char *tty_device_reldir; |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 94 | char *tty_path_input; |
| 95 | char *tty_path_input_real; |
| 96 | char *tty_kname_real; |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 97 | int rc; |
| 98 | |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 99 | tty_class_device_link = NULL; |
| 100 | tty_device_tty_dir = NULL; |
| 101 | tty_device_reldir = NULL; |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 102 | tty_path_input = NULL; |
| 103 | tty_path_input_real = NULL; |
| 104 | tty_kname_real = NULL; |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 105 | |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 106 | /* udev may rename the tty name with a symbol link, try to resolve */ |
| 107 | rc = asprintf(&tty_path_input, "/dev/%s", console->tty_kname); |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 108 | if (rc < 0) |
| 109 | return -1; |
| 110 | |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 111 | tty_path_input_real = realpath(tty_path_input, NULL); |
| 112 | if (!tty_path_input_real) { |
| 113 | warn("Can't find realpath for /dev/%s", console->tty_kname); |
| 114 | goto out_free; |
| 115 | } |
| 116 | |
| 117 | tty_kname_real = basename(tty_path_input_real); |
| 118 | if (!tty_kname_real) { |
| 119 | warn("Can't find real name for /dev/%s", console->tty_kname); |
| 120 | goto out_free; |
| 121 | } |
| 122 | |
| 123 | rc = asprintf(&tty_class_device_link, |
| 124 | "/sys/class/tty/%s", tty_kname_real); |
| 125 | if (rc < 0) |
| 126 | goto out_free; |
| 127 | |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 128 | tty_device_tty_dir = realpath(tty_class_device_link, NULL); |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 129 | if (!tty_device_tty_dir) { |
| 130 | warn("Can't query sysfs for device %s", tty_kname_real); |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 131 | goto out_free; |
| 132 | } |
| 133 | |
| 134 | rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir); |
| 135 | if (rc < 0) |
| 136 | goto out_free; |
| 137 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 138 | console->tty_sysfs_devnode = realpath(tty_device_reldir, NULL); |
| 139 | if (!console->tty_sysfs_devnode) |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 140 | warn("Can't find parent device for %s", tty_kname_real); |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 141 | |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 142 | rc = asprintf(&console->tty_dev, "/dev/%s", tty_kname_real); |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 143 | if (rc < 0) |
| 144 | goto out_free; |
| 145 | |
| 146 | rc = 0; |
| 147 | |
| 148 | out_free: |
| 149 | free(tty_class_device_link); |
| 150 | free(tty_device_tty_dir); |
| 151 | free(tty_device_reldir); |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 152 | free(tty_path_input); |
| 153 | free(tty_path_input_real); |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 154 | return rc; |
| 155 | } |
| 156 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 157 | static int tty_set_sysfs_attr(struct console *console, const char *name, |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 158 | int value) |
| 159 | { |
| 160 | char *path; |
| 161 | FILE *fp; |
| 162 | int rc; |
| 163 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 164 | rc = asprintf(&path, "%s/%s", console->tty_sysfs_devnode, name); |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 165 | if (rc < 0) |
| 166 | return -1; |
| 167 | |
| 168 | fp = fopen(path, "w"); |
| 169 | if (!fp) { |
| 170 | warn("Can't access attribute %s on device %s", |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 171 | name, console->tty_kname); |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 172 | rc = -1; |
| 173 | goto out_free; |
| 174 | } |
| 175 | setvbuf(fp, NULL, _IONBF, 0); |
| 176 | |
| 177 | rc = fprintf(fp, "0x%x", value); |
| 178 | if (rc < 0) |
| 179 | warn("Error writing to %s attribute of device %s", |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 180 | name, console->tty_kname); |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 181 | fclose(fp); |
| 182 | |
| 183 | |
| 184 | |
| 185 | out_free: |
| 186 | free(path); |
| 187 | return rc; |
| 188 | } |
| 189 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 190 | /** |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 191 | * Set termios attributes on the console tty. |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 192 | */ |
| 193 | static void tty_init_termios(struct console *console) |
| 194 | { |
| 195 | struct termios termios; |
| 196 | int rc; |
| 197 | |
| 198 | rc = tcgetattr(console->tty_fd, &termios); |
| 199 | if (rc) { |
| 200 | warn("Can't read tty termios"); |
| 201 | return; |
| 202 | } |
| 203 | |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 204 | if (console->tty_baud) { |
| 205 | if (cfsetspeed(&termios, console->tty_baud) < 0) |
| 206 | warn("Couldn't set speeds for %s", console->tty_kname); |
| 207 | } |
| 208 | |
| 209 | /* Set console to raw mode: we don't want any processing to occur on |
| 210 | * the underlying terminal input/output. |
| 211 | */ |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 212 | cfmakeraw(&termios); |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 213 | |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 214 | rc = tcsetattr(console->tty_fd, TCSANOW, &termios); |
| 215 | if (rc) |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 216 | warn("Can't set terminal options for %s", console->tty_kname); |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 220 | * Open and initialise the serial device |
| 221 | */ |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 222 | static int tty_init_io(struct console *console) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 223 | { |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 224 | if (console->tty_sirq) |
| 225 | tty_set_sysfs_attr(console, "sirq", console->tty_sirq); |
| 226 | if (console->tty_lpc_addr) |
| 227 | tty_set_sysfs_attr(console, "lpc_address", |
| 228 | console->tty_lpc_addr); |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 229 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 230 | console->tty_fd = open(console->tty_dev, O_RDWR); |
| 231 | if (console->tty_fd <= 0) { |
| 232 | warn("Can't open tty %s", console->tty_dev); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | /* Disable character delay. We may want to later enable this when |
| 237 | * we detect larger amounts of data |
| 238 | */ |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 239 | fcntl(console->tty_fd, F_SETFL, FNDELAY); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 240 | |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 241 | tty_init_termios(console); |
| 242 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 243 | console->pollfds[console->n_pollers].fd = console->tty_fd; |
| 244 | console->pollfds[console->n_pollers].events = POLLIN; |
| 245 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 246 | return 0; |
| 247 | } |
| 248 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 249 | static int tty_init(struct console *console, struct config *config) |
| 250 | { |
| 251 | const char *val; |
| 252 | char *endp; |
| 253 | int rc; |
| 254 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 255 | val = config_get_value(config, "lpc-address"); |
| 256 | if (val) { |
| 257 | console->tty_lpc_addr = strtoul(val, &endp, 0); |
| 258 | if (endp == optarg) { |
| 259 | warn("Invalid LPC address: '%s'", val); |
| 260 | return -1; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | val = config_get_value(config, "sirq"); |
| 265 | if (val) { |
| 266 | console->tty_sirq = strtoul(val, &endp, 0); |
| 267 | if (endp == optarg) |
| 268 | warn("Invalid sirq: '%s'", val); |
| 269 | } |
| 270 | |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 271 | val = config_get_value(config, "baud"); |
| 272 | if (val) { |
| 273 | if (config_parse_baud(&console->tty_baud, val)) |
| 274 | warnx("Invalid baud rate: '%s'", val); |
| 275 | } |
| 276 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 277 | if (!console->tty_kname) { |
| 278 | warnx("Error: No TTY device specified"); |
| 279 | return -1; |
| 280 | } |
| 281 | |
| 282 | rc = tty_find_device(console); |
| 283 | if (rc) |
| 284 | return rc; |
| 285 | |
| 286 | rc = tty_init_io(console); |
| 287 | return rc; |
| 288 | } |
| 289 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 290 | |
| 291 | 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] | 292 | { |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 293 | return write_buf_to_fd(console->tty_fd, data, len); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 294 | } |
| 295 | |
Jeremy Kerr | d47963e | 2016-03-16 17:29:55 +0800 | [diff] [blame] | 296 | static void handlers_init(struct console *console, struct config *config) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 297 | { |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 298 | extern struct handler *__start_handlers, *__stop_handlers; |
| 299 | struct handler *handler; |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 300 | int i, rc; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 301 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 302 | console->n_handlers = &__stop_handlers - &__start_handlers; |
| 303 | console->handlers = &__start_handlers; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 304 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 305 | printf("%d handler%s\n", console->n_handlers, |
| 306 | console->n_handlers == 1 ? "" : "s"); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 307 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 308 | for (i = 0; i < console->n_handlers; i++) { |
| 309 | handler = console->handlers[i]; |
| 310 | |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 311 | rc = 0; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 312 | if (handler->init) |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 313 | rc = handler->init(handler, console, config); |
| 314 | |
| 315 | handler->active = rc == 0; |
| 316 | |
| 317 | printf(" %s [%sactive]\n", handler->name, |
| 318 | handler->active ? "" : "in"); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 319 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 320 | } |
| 321 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 322 | static void handlers_fini(struct console *console) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 323 | { |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 324 | struct handler *handler; |
| 325 | int i; |
| 326 | |
| 327 | for (i = 0; i < console->n_handlers; i++) { |
| 328 | handler = console->handlers[i]; |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 329 | if (handler->fini && handler->active) |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 330 | handler->fini(handler); |
| 331 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 332 | } |
| 333 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 334 | struct ringbuffer_consumer *console_ringbuffer_consumer_register( |
| 335 | struct console *console, |
| 336 | ringbuffer_poll_fn_t poll_fn, void *data) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 337 | { |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 338 | return ringbuffer_consumer_register(console->rb, poll_fn, data); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 339 | } |
| 340 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 341 | struct poller *console_poller_register(struct console *console, |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 342 | struct handler *handler, poller_fn_t poller_fn, |
| 343 | int fd, int events, void *data) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 344 | { |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 345 | struct poller *poller; |
| 346 | int n; |
| 347 | |
| 348 | poller = malloc(sizeof(*poller)); |
| 349 | poller->remove = false; |
| 350 | poller->handler = handler; |
| 351 | poller->fn = poller_fn; |
| 352 | poller->data = data; |
| 353 | |
| 354 | /* add one to our pollers array */ |
| 355 | n = console->n_pollers++; |
| 356 | console->pollers = realloc(console->pollers, |
| 357 | sizeof(*console->pollers) * console->n_pollers); |
| 358 | |
| 359 | console->pollers[n] = poller; |
| 360 | |
| 361 | /* increase pollfds array too */ |
| 362 | console->pollfds = realloc(console->pollfds, |
| 363 | sizeof(*console->pollfds) * |
| 364 | (n_internal_pollfds + console->n_pollers)); |
| 365 | |
| 366 | /* shift the end pollfds up by one */ |
| 367 | memcpy(&console->pollfds[n+n_internal_pollfds], |
| 368 | &console->pollfds[n], |
| 369 | sizeof(*console->pollfds) * n_internal_pollfds); |
| 370 | |
| 371 | console->pollfds[n].fd = fd; |
| 372 | console->pollfds[n].events = events; |
| 373 | |
| 374 | return poller; |
| 375 | } |
| 376 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 377 | void console_poller_unregister(struct console *console, |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 378 | struct poller *poller) |
| 379 | { |
| 380 | int i; |
| 381 | |
| 382 | /* find the entry in our pollers array */ |
| 383 | for (i = 0; i < console->n_pollers; i++) |
| 384 | if (console->pollers[i] == poller) |
| 385 | break; |
| 386 | |
| 387 | assert(i < console->n_pollers); |
| 388 | |
| 389 | console->n_pollers--; |
| 390 | |
| 391 | /* remove the item from the pollers array... */ |
| 392 | memmove(&console->pollers[i], &console->pollers[i+1], |
| 393 | sizeof(*console->pollers) |
| 394 | * (console->n_pollers - i)); |
| 395 | |
| 396 | console->pollers = realloc(console->pollers, |
| 397 | sizeof(*console->pollers) * console->n_pollers); |
| 398 | |
| 399 | /* ... and the pollfds array */ |
| 400 | memmove(&console->pollfds[i], &console->pollfds[i+1], |
| 401 | sizeof(*console->pollfds) * |
| 402 | (n_internal_pollfds + console->n_pollers - i)); |
| 403 | |
| 404 | console->pollfds = realloc(console->pollfds, |
| 405 | sizeof(*console->pollfds) * |
| 406 | (n_internal_pollfds + console->n_pollers)); |
| 407 | |
| 408 | |
| 409 | free(poller); |
| 410 | } |
| 411 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 412 | void console_poller_set_events(struct console *console, struct poller *poller, |
| 413 | int events) |
| 414 | { |
| 415 | int i; |
| 416 | |
| 417 | /* find the entry in our pollers array */ |
| 418 | for (i = 0; i < console->n_pollers; i++) |
| 419 | if (console->pollers[i] == poller) |
| 420 | break; |
| 421 | |
| 422 | console->pollfds[i].events = events; |
| 423 | } |
| 424 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 425 | static int call_pollers(struct console *console) |
| 426 | { |
| 427 | struct poller *poller; |
| 428 | struct pollfd *pollfd; |
| 429 | enum poller_ret prc; |
| 430 | int i, rc; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 431 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 432 | rc = 0; |
| 433 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 434 | /* |
| 435 | * Process poll events by iterating through the pollers and pollfds |
| 436 | * in-step, calling any pollers that we've found revents for. |
| 437 | */ |
| 438 | for (i = 0; i < console->n_pollers; i++) { |
| 439 | poller = console->pollers[i]; |
| 440 | pollfd = &console->pollfds[i]; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 441 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 442 | if (!pollfd->revents) |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 443 | continue; |
| 444 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 445 | prc = poller->fn(poller->handler, pollfd->revents, |
| 446 | poller->data); |
| 447 | if (prc == POLLER_EXIT) |
| 448 | rc = -1; |
| 449 | else if (prc == POLLER_REMOVE) |
| 450 | poller->remove = true; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * Process deferred removals; restarting each time we unregister, as |
| 455 | * the array will have changed |
| 456 | */ |
| 457 | for (;;) { |
| 458 | bool removed = false; |
| 459 | |
| 460 | for (i = 0; i < console->n_pollers; i++) { |
| 461 | poller = console->pollers[i]; |
| 462 | if (poller->remove) { |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 463 | console_poller_unregister(console, poller); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 464 | removed = true; |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | if (!removed) |
| 469 | break; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | return rc; |
| 473 | } |
| 474 | |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 475 | static void sighandler(int signal) |
| 476 | { |
| 477 | if (signal == SIGINT) |
| 478 | sigint = true; |
| 479 | } |
| 480 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 481 | int run_console(struct console *console) |
| 482 | { |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 483 | sighandler_t sighandler_save; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 484 | int rc; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 485 | |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 486 | sighandler_save = signal(SIGINT, sighandler); |
| 487 | |
| 488 | rc = 0; |
| 489 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 490 | for (;;) { |
| 491 | uint8_t buf[4096]; |
| 492 | |
Jeremy Kerr | 1764145 | 2017-02-07 22:09:13 +0800 | [diff] [blame] | 493 | BUILD_ASSERT(sizeof(buf) <= buffer_size); |
| 494 | |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 495 | if (sigint) { |
| 496 | fprintf(stderr, "Received interrupt, exiting\n"); |
| 497 | break; |
| 498 | } |
| 499 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 500 | rc = poll(console->pollfds, |
| 501 | console->n_pollers + n_internal_pollfds, -1); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 502 | if (rc < 0) { |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 503 | if (errno == EINTR) { |
| 504 | continue; |
| 505 | } else { |
| 506 | warn("poll error"); |
| 507 | break; |
| 508 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 509 | } |
| 510 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 511 | /* process internal fd first */ |
| 512 | BUILD_ASSERT(n_internal_pollfds == 1); |
| 513 | |
| 514 | if (console->pollfds[console->n_pollers].revents) { |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 515 | rc = read(console->tty_fd, buf, sizeof(buf)); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 516 | if (rc <= 0) { |
| 517 | warn("Error reading from tty device"); |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 518 | rc = -1; |
| 519 | break; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 520 | } |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 521 | rc = ringbuffer_queue(console->rb, buf, rc); |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 522 | if (rc) |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 523 | break; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 524 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 525 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 526 | /* ... and then the pollers */ |
| 527 | rc = call_pollers(console); |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 528 | if (rc) |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 529 | break; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 530 | } |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 531 | |
| 532 | signal(SIGINT, sighandler_save); |
| 533 | |
| 534 | return rc ? -1 : 0; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 535 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 536 | static const struct option options[] = { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 537 | { "config", required_argument, 0, 'c'}, |
Joel Stanley | f5858b5 | 2016-03-18 16:33:03 +1030 | [diff] [blame] | 538 | { 0, 0, 0, 0}, |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 539 | }; |
| 540 | |
| 541 | int main(int argc, char **argv) |
| 542 | { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 543 | const char *config_filename = NULL; |
Vishwanatha Subbanna | 6221ce9 | 2016-07-20 05:35:45 -0500 | [diff] [blame] | 544 | const char *config_tty_kname = NULL; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 545 | struct console *console; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 546 | struct config *config; |
| 547 | int rc; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 548 | |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 549 | rc = -1; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 550 | |
| 551 | for (;;) { |
| 552 | int c, idx; |
| 553 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 554 | c = getopt_long(argc, argv, "c:", options, &idx); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 555 | if (c == -1) |
| 556 | break; |
| 557 | |
| 558 | switch (c) { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 559 | case 'c': |
| 560 | config_filename = optarg; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 561 | break; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 562 | case 'h': |
| 563 | case '?': |
| 564 | usage(argv[0]); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 565 | return EXIT_SUCCESS; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
Vishwanatha Subbanna | 6221ce9 | 2016-07-20 05:35:45 -0500 | [diff] [blame] | 569 | if (optind >= argc) { |
| 570 | warnx("Required argument <DEVICE> missing"); |
| 571 | usage(argv[0]); |
| 572 | return EXIT_FAILURE; |
| 573 | } |
| 574 | |
| 575 | config_tty_kname = argv[optind]; |
| 576 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 577 | console = malloc(sizeof(struct console)); |
| 578 | memset(console, 0, sizeof(*console)); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 579 | console->pollfds = calloc(n_internal_pollfds, |
| 580 | sizeof(*console->pollfds)); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 581 | console->rb = ringbuffer_init(buffer_size); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 582 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 583 | config = config_init(config_filename); |
| 584 | if (!config) { |
| 585 | warnx("Can't read configuration, exiting."); |
| 586 | goto out_free; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 587 | } |
| 588 | |
Vishwanatha Subbanna | 6221ce9 | 2016-07-20 05:35:45 -0500 | [diff] [blame] | 589 | console->tty_kname = config_tty_kname; |
| 590 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 591 | rc = tty_init(console, config); |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 592 | if (rc) |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 593 | goto out_config_fini; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 594 | |
Jeremy Kerr | d47963e | 2016-03-16 17:29:55 +0800 | [diff] [blame] | 595 | handlers_init(console, config); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 596 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 597 | rc = run_console(console); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 598 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 599 | handlers_fini(console); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 600 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 601 | out_config_fini: |
| 602 | config_fini(config); |
| 603 | |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 604 | out_free: |
Jeremy Kerr | 89ea819 | 2016-03-15 17:57:43 +0800 | [diff] [blame] | 605 | free(console->pollers); |
| 606 | free(console->pollfds); |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 607 | free(console->tty_sysfs_devnode); |
| 608 | free(console->tty_dev); |
| 609 | free(console); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 610 | |
| 611 | return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
| 612 | } |