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