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