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