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