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