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 | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 19 | #include <assert.h> |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <signal.h> |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <stdio.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <unistd.h> |
| 28 | #include <err.h> |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 29 | #include <string.h> |
| 30 | #include <getopt.h> |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 31 | #include <limits.h> |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 32 | #include <time.h> |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 33 | #include <termios.h> |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 34 | |
| 35 | #include <sys/types.h> |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 36 | #include <sys/time.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 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 41 | #define DBUS_ERR "org.openbmc.error" |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 42 | #define DBUS_NAME "xyz.openbmc_project.console" |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 43 | #define OBJ_NAME "/xyz/openbmc_project/console" |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 44 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 45 | struct console { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 46 | const char *tty_kname; |
| 47 | char *tty_sysfs_devnode; |
| 48 | char *tty_dev; |
| 49 | int tty_sirq; |
Andrew Jeffery | fd883a8 | 2023-04-18 22:18:25 +0930 | [diff] [blame] | 50 | uint16_t tty_lpc_addr; |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 51 | speed_t tty_baud; |
| 52 | int tty_fd; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 53 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 54 | struct ringbuffer *rb; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 55 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 56 | struct handler **handlers; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 57 | long n_handlers; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 58 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 59 | struct poller **pollers; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 60 | long n_pollers; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 61 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 62 | struct pollfd *pollfds; |
| 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 { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 67 | struct handler *handler; |
| 68 | void *data; |
| 69 | poller_event_fn_t event_fn; |
| 70 | poller_timeout_fn_t timeout_fn; |
| 71 | struct timeval timeout; |
| 72 | bool remove; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 73 | }; |
| 74 | |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 75 | /* we have two extra entry in the pollfds array for the VUART tty */ |
| 76 | enum internal_pollfds { |
| 77 | POLLFD_HOSTTTY = 0, |
| 78 | POLLFD_DBUS = 1, |
| 79 | MAX_INTERNAL_POLLFD = 2, |
| 80 | }; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 81 | |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 82 | /* size of the shared backlog ringbuffer */ |
Andrew Jeffery | 5db8c79 | 2023-04-18 21:48:24 +0930 | [diff] [blame] | 83 | const size_t buffer_size = 128ul * 1024ul; |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 84 | |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 85 | /* state shared with the signal handler */ |
| 86 | static bool sigint; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 87 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 88 | static void usage(const char *progname) |
| 89 | { |
| 90 | fprintf(stderr, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 91 | "usage: %s [options] <DEVICE>\n" |
| 92 | "\n" |
| 93 | "Options:\n" |
| 94 | " --config <FILE> Use FILE for configuration\n" |
| 95 | "", |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 96 | progname); |
| 97 | } |
| 98 | |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 99 | /* populates tty_dev and tty_sysfs_devnode, using the tty kernel name */ |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 100 | static int tty_find_device(struct console *console) |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 101 | { |
| 102 | char *tty_class_device_link; |
| 103 | char *tty_device_tty_dir; |
| 104 | char *tty_device_reldir; |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 105 | char *tty_path_input; |
| 106 | char *tty_path_input_real; |
| 107 | char *tty_kname_real; |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 108 | int rc; |
| 109 | |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 110 | tty_class_device_link = NULL; |
| 111 | tty_device_tty_dir = NULL; |
| 112 | tty_device_reldir = NULL; |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 113 | tty_path_input = NULL; |
| 114 | tty_path_input_real = NULL; |
| 115 | tty_kname_real = NULL; |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 116 | |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 117 | /* udev may rename the tty name with a symbol link, try to resolve */ |
| 118 | rc = asprintf(&tty_path_input, "/dev/%s", console->tty_kname); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 119 | if (rc < 0) { |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 120 | return -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 121 | } |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 122 | |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 123 | tty_path_input_real = realpath(tty_path_input, NULL); |
| 124 | if (!tty_path_input_real) { |
| 125 | warn("Can't find realpath for /dev/%s", console->tty_kname); |
Andrew Jeffery | 15792aa | 2023-04-18 16:47:33 +0930 | [diff] [blame] | 126 | rc = -1; |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 127 | goto out_free; |
| 128 | } |
| 129 | |
| 130 | tty_kname_real = basename(tty_path_input_real); |
| 131 | if (!tty_kname_real) { |
| 132 | warn("Can't find real name for /dev/%s", console->tty_kname); |
Andrew Jeffery | 15792aa | 2023-04-18 16:47:33 +0930 | [diff] [blame] | 133 | rc = -1; |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 134 | goto out_free; |
| 135 | } |
| 136 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 137 | rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s", |
| 138 | tty_kname_real); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 139 | if (rc < 0) { |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 140 | goto out_free; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 141 | } |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 142 | |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 143 | tty_device_tty_dir = realpath(tty_class_device_link, NULL); |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 144 | if (!tty_device_tty_dir) { |
| 145 | warn("Can't query sysfs for device %s", tty_kname_real); |
Andrew Jeffery | 15792aa | 2023-04-18 16:47:33 +0930 | [diff] [blame] | 146 | rc = -1; |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 147 | goto out_free; |
| 148 | } |
| 149 | |
| 150 | rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 151 | if (rc < 0) { |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 152 | goto out_free; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 153 | } |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 154 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 155 | console->tty_sysfs_devnode = realpath(tty_device_reldir, NULL); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 156 | if (!console->tty_sysfs_devnode) { |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 157 | warn("Can't find parent device for %s", tty_kname_real); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 158 | } |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 159 | |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 160 | rc = asprintf(&console->tty_dev, "/dev/%s", tty_kname_real); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 161 | if (rc < 0) { |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 162 | goto out_free; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 163 | } |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 164 | |
| 165 | rc = 0; |
| 166 | |
| 167 | out_free: |
| 168 | free(tty_class_device_link); |
| 169 | free(tty_device_tty_dir); |
| 170 | free(tty_device_reldir); |
Yi Li | 45ad767 | 2016-10-18 23:21:19 +0800 | [diff] [blame] | 171 | free(tty_path_input); |
| 172 | free(tty_path_input_real); |
Jeremy Kerr | 1721784 | 2016-01-29 18:44:21 +0800 | [diff] [blame] | 173 | return rc; |
| 174 | } |
| 175 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 176 | static int tty_set_sysfs_attr(struct console *console, const char *name, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 177 | int value) |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 178 | { |
| 179 | char *path; |
| 180 | FILE *fp; |
| 181 | int rc; |
| 182 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 183 | rc = asprintf(&path, "%s/%s", console->tty_sysfs_devnode, name); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 184 | if (rc < 0) { |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 185 | return -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 186 | } |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 187 | |
| 188 | fp = fopen(path, "w"); |
| 189 | if (!fp) { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 190 | warn("Can't access attribute %s on device %s", name, |
| 191 | console->tty_kname); |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 192 | rc = -1; |
| 193 | goto out_free; |
| 194 | } |
| 195 | setvbuf(fp, NULL, _IONBF, 0); |
| 196 | |
| 197 | rc = fprintf(fp, "0x%x", value); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 198 | if (rc < 0) { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 199 | warn("Error writing to %s attribute of device %s", name, |
| 200 | console->tty_kname); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 201 | } |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 202 | fclose(fp); |
| 203 | |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 204 | out_free: |
| 205 | free(path); |
| 206 | return rc; |
| 207 | } |
| 208 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 209 | /** |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 210 | * Set termios attributes on the console tty. |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 211 | */ |
| 212 | static void tty_init_termios(struct console *console) |
| 213 | { |
| 214 | struct termios termios; |
| 215 | int rc; |
| 216 | |
| 217 | rc = tcgetattr(console->tty_fd, &termios); |
| 218 | if (rc) { |
| 219 | warn("Can't read tty termios"); |
| 220 | return; |
| 221 | } |
| 222 | |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 223 | if (console->tty_baud) { |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 224 | if (cfsetspeed(&termios, console->tty_baud) < 0) { |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 225 | warn("Couldn't set speeds for %s", console->tty_kname); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 226 | } |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* Set console to raw mode: we don't want any processing to occur on |
| 230 | * the underlying terminal input/output. |
| 231 | */ |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 232 | cfmakeraw(&termios); |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 233 | |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 234 | rc = tcsetattr(console->tty_fd, TCSANOW, &termios); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 235 | if (rc) { |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 236 | warn("Can't set terminal options for %s", console->tty_kname); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 237 | } |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 238 | } |
| 239 | |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 240 | static void tty_change_baudrate(struct console *console) |
| 241 | { |
| 242 | struct handler *handler; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame^] | 243 | int i; |
| 244 | int rc; |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 245 | |
| 246 | tty_init_termios(console); |
| 247 | |
| 248 | for (i = 0; i < console->n_handlers; i++) { |
| 249 | handler = console->handlers[i]; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 250 | if (!handler->baudrate) { |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 251 | continue; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 252 | } |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 253 | |
| 254 | rc = handler->baudrate(handler, console->tty_baud); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 255 | if (rc) { |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 256 | warnx("Can't set terminal baudrate for handler %s", |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 257 | handler->name); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 258 | } |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 262 | /** |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 263 | * Open and initialise the serial device |
| 264 | */ |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 265 | static int tty_init_io(struct console *console) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 266 | { |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 267 | if (console->tty_sirq) { |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 268 | tty_set_sysfs_attr(console, "sirq", console->tty_sirq); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 269 | } |
| 270 | if (console->tty_lpc_addr) { |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 271 | tty_set_sysfs_attr(console, "lpc_address", |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 272 | console->tty_lpc_addr); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 273 | } |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 274 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 275 | console->tty_fd = open(console->tty_dev, O_RDWR); |
| 276 | if (console->tty_fd <= 0) { |
| 277 | warn("Can't open tty %s", console->tty_dev); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 278 | return -1; |
| 279 | } |
| 280 | |
| 281 | /* Disable character delay. We may want to later enable this when |
| 282 | * we detect larger amounts of data |
| 283 | */ |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 284 | fcntl(console->tty_fd, F_SETFL, FNDELAY); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 285 | |
Jeremy Kerr | 54e9569 | 2016-03-24 17:07:56 +0800 | [diff] [blame] | 286 | tty_init_termios(console); |
| 287 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 288 | console->pollfds[console->n_pollers].fd = console->tty_fd; |
| 289 | console->pollfds[console->n_pollers].events = POLLIN; |
| 290 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 291 | return 0; |
| 292 | } |
| 293 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 294 | static int tty_init(struct console *console, struct config *config) |
| 295 | { |
Andrew Jeffery | fd883a8 | 2023-04-18 22:18:25 +0930 | [diff] [blame] | 296 | unsigned long parsed; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 297 | const char *val; |
| 298 | char *endp; |
| 299 | int rc; |
| 300 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 301 | val = config_get_value(config, "lpc-address"); |
| 302 | if (val) { |
Andrew Jeffery | fd883a8 | 2023-04-18 22:18:25 +0930 | [diff] [blame] | 303 | errno = 0; |
| 304 | parsed = strtoul(val, &endp, 0); |
| 305 | if (parsed == ULONG_MAX && errno == ERANGE) { |
| 306 | warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'", |
| 307 | val); |
| 308 | return -1; |
| 309 | } |
| 310 | |
| 311 | if (parsed > UINT16_MAX) { |
| 312 | warn("Invalid LPC address '%s'", val); |
| 313 | return -1; |
| 314 | } |
| 315 | |
| 316 | console->tty_lpc_addr = (uint16_t)parsed; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 317 | if (endp == optarg) { |
| 318 | warn("Invalid LPC address: '%s'", val); |
| 319 | return -1; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | val = config_get_value(config, "sirq"); |
| 324 | if (val) { |
Andrew Jeffery | fd883a8 | 2023-04-18 22:18:25 +0930 | [diff] [blame] | 325 | errno = 0; |
| 326 | parsed = strtoul(val, &endp, 0); |
| 327 | if (parsed == ULONG_MAX && errno == ERANGE) { |
| 328 | warn("Cannot interpret 'sirq' value as an unsigned long: '%s'", |
| 329 | val); |
| 330 | } |
| 331 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 332 | if (parsed > 16) { |
Andrew Jeffery | fd883a8 | 2023-04-18 22:18:25 +0930 | [diff] [blame] | 333 | warn("Invalid LPC SERIRQ: '%s'", val); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 334 | } |
Andrew Jeffery | fd883a8 | 2023-04-18 22:18:25 +0930 | [diff] [blame] | 335 | |
| 336 | console->tty_sirq = (int)parsed; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 337 | if (endp == optarg) { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 338 | warn("Invalid sirq: '%s'", val); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 339 | } |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 340 | } |
| 341 | |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 342 | val = config_get_value(config, "baud"); |
| 343 | if (val) { |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 344 | if (config_parse_baud(&console->tty_baud, val)) { |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 345 | warnx("Invalid baud rate: '%s'", val); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 346 | } |
Benjamin Fair | c7fbcd4 | 2018-06-04 14:39:01 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 349 | if (!console->tty_kname) { |
| 350 | warnx("Error: No TTY device specified"); |
| 351 | return -1; |
| 352 | } |
| 353 | |
| 354 | rc = tty_find_device(console); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 355 | if (rc) { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 356 | return rc; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 357 | } |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 358 | |
| 359 | rc = tty_init_io(console); |
| 360 | return rc; |
| 361 | } |
| 362 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 363 | 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] | 364 | { |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 365 | return write_buf_to_fd(console->tty_fd, data, len); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 366 | } |
| 367 | |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 368 | static int method_set_baud_rate(sd_bus_message *msg, void *userdata, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 369 | sd_bus_error *err) |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 370 | { |
| 371 | struct console *console = userdata; |
| 372 | uint32_t baudrate; |
| 373 | speed_t speed; |
| 374 | int r; |
| 375 | |
| 376 | if (!console) { |
| 377 | sd_bus_error_set_const(err, DBUS_ERR, "Internal error"); |
| 378 | return sd_bus_reply_method_return(msg, "x", 0); |
| 379 | } |
| 380 | |
| 381 | r = sd_bus_message_read(msg, "u", &baudrate); |
| 382 | if (r < 0) { |
| 383 | sd_bus_error_set_const(err, DBUS_ERR, "Bad message"); |
| 384 | return sd_bus_reply_method_return(msg, "x", -EINVAL); |
| 385 | } |
| 386 | |
| 387 | speed = parse_int_to_baud(baudrate); |
| 388 | if (!speed) { |
| 389 | warnx("Invalid baud rate: '%u'", baudrate); |
| 390 | return sd_bus_reply_method_return(msg, "x", -EINVAL); |
| 391 | } |
| 392 | |
| 393 | console->tty_baud = speed; |
| 394 | tty_change_baudrate(console); |
| 395 | |
| 396 | return sd_bus_reply_method_return(msg, "x", r); |
| 397 | } |
| 398 | |
Andrew Jeffery | fd04832 | 2023-04-18 12:02:01 +0930 | [diff] [blame] | 399 | static int get_handler(sd_bus *bus __attribute__((unused)), |
| 400 | const char *path __attribute__((unused)), |
| 401 | const char *interface __attribute__((unused)), |
| 402 | const char *property __attribute__((unused)), |
| 403 | sd_bus_message *reply, void *userdata, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 404 | sd_bus_error *error __attribute__((unused))) |
| 405 | { |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 406 | struct console *console = userdata; |
| 407 | uint32_t baudrate; |
| 408 | int r; |
| 409 | |
| 410 | baudrate = parse_baud_to_int(console->tty_baud); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 411 | if (!baudrate) { |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 412 | warnx("Invalid baud rate: '%d'", console->tty_baud); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 413 | } |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 414 | |
| 415 | r = sd_bus_message_append(reply, "u", baudrate); |
| 416 | |
| 417 | return r; |
| 418 | } |
| 419 | |
| 420 | static const sd_bus_vtable console_vtable[] = { |
| 421 | SD_BUS_VTABLE_START(0), |
| 422 | SD_BUS_METHOD("setBaudRate", "u", "x", method_set_baud_rate, |
| 423 | SD_BUS_VTABLE_UNPRIVILEGED), |
| 424 | SD_BUS_PROPERTY("baudrate", "u", get_handler, 0, 0), |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 425 | SD_BUS_VTABLE_END, |
| 426 | }; |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 427 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 428 | static void dbus_init(struct console *console, |
| 429 | struct config *config __attribute__((unused))) |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 430 | { |
| 431 | int dbus_poller = 0; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame^] | 432 | int fd; |
| 433 | int r; |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 434 | |
| 435 | if (!console) { |
| 436 | warnx("Couldn't get valid console"); |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | r = sd_bus_default_system(&console->bus); |
| 441 | if (r < 0) { |
| 442 | warnx("Failed to connect to system bus: %s", strerror(-r)); |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | r = sd_bus_add_object_vtable(console->bus, NULL, OBJ_NAME, DBUS_NAME, |
| 447 | console_vtable, console); |
| 448 | if (r < 0) { |
| 449 | warnx("Failed to issue method call: %s", strerror(-r)); |
| 450 | return; |
| 451 | } |
| 452 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 453 | r = sd_bus_request_name(console->bus, DBUS_NAME, |
| 454 | SD_BUS_NAME_ALLOW_REPLACEMENT | |
| 455 | SD_BUS_NAME_REPLACE_EXISTING); |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 456 | if (r < 0) { |
| 457 | warnx("Failed to acquire service name: %s", strerror(-r)); |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | fd = sd_bus_get_fd(console->bus); |
| 462 | if (fd < 0) { |
| 463 | warnx("Couldn't get the bus file descriptor"); |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | dbus_poller = POLLFD_DBUS; |
| 468 | |
| 469 | console->pollfds[dbus_poller].fd = fd; |
| 470 | console->pollfds[dbus_poller].events = POLLIN; |
| 471 | } |
| 472 | |
Jeremy Kerr | d47963e | 2016-03-16 17:29:55 +0800 | [diff] [blame] | 473 | static void handlers_init(struct console *console, struct config *config) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 474 | { |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame^] | 475 | /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */ |
| 476 | extern struct handler *__start_handlers; |
| 477 | extern struct handler *__stop_handlers; |
| 478 | /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */ |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 479 | struct handler *handler; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame^] | 480 | int i; |
| 481 | int rc; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 482 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 483 | console->n_handlers = &__stop_handlers - &__start_handlers; |
| 484 | console->handlers = &__start_handlers; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 485 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 486 | printf("%ld handler%s\n", console->n_handlers, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 487 | console->n_handlers == 1 ? "" : "s"); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 488 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 489 | for (i = 0; i < console->n_handlers; i++) { |
| 490 | handler = console->handlers[i]; |
| 491 | |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 492 | rc = 0; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 493 | if (handler->init) { |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 494 | rc = handler->init(handler, console, config); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 495 | } |
Jeremy Kerr | 021b91f | 2016-04-28 11:51:52 +0800 | [diff] [blame] | 496 | |
| 497 | handler->active = rc == 0; |
| 498 | |
| 499 | printf(" %s [%sactive]\n", handler->name, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 500 | handler->active ? "" : "in"); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 501 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 502 | } |
| 503 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 504 | static void handlers_fini(struct console *console) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 505 | { |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 506 | struct handler *handler; |
| 507 | int i; |
| 508 | |
| 509 | for (i = 0; i < console->n_handlers; i++) { |
| 510 | handler = console->handlers[i]; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 511 | if (handler->fini && handler->active) { |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 512 | handler->fini(handler); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 513 | } |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 514 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 515 | } |
| 516 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 517 | static int get_current_time(struct timeval *tv) |
| 518 | { |
| 519 | struct timespec t; |
| 520 | int rc; |
| 521 | |
| 522 | /* |
| 523 | * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to |
| 524 | * local time changes. However, a struct timeval is more |
| 525 | * convenient for calculations, so convert to that. |
| 526 | */ |
| 527 | rc = clock_gettime(CLOCK_MONOTONIC, &t); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 528 | if (rc) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 529 | return rc; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 530 | } |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 531 | |
| 532 | tv->tv_sec = t.tv_sec; |
| 533 | tv->tv_usec = t.tv_nsec / 1000; |
| 534 | |
| 535 | return 0; |
| 536 | } |
| 537 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 538 | struct ringbuffer_consumer * |
| 539 | console_ringbuffer_consumer_register(struct console *console, |
| 540 | ringbuffer_poll_fn_t poll_fn, void *data) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 541 | { |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 542 | return ringbuffer_consumer_register(console->rb, poll_fn, data); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 543 | } |
| 544 | |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 545 | struct poller *console_poller_register(struct console *console, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 546 | struct handler *handler, |
| 547 | poller_event_fn_t poller_fn, |
| 548 | poller_timeout_fn_t timeout_fn, int fd, |
| 549 | int events, void *data) |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 550 | { |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 551 | struct poller *poller; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 552 | long n; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 553 | |
| 554 | poller = malloc(sizeof(*poller)); |
| 555 | poller->remove = false; |
| 556 | poller->handler = handler; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 557 | poller->event_fn = poller_fn; |
| 558 | poller->timeout_fn = timeout_fn; |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 559 | poller->data = data; |
| 560 | |
| 561 | /* add one to our pollers array */ |
| 562 | n = console->n_pollers++; |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 563 | /* |
| 564 | * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a |
| 565 | * pointer type. |
| 566 | */ |
| 567 | /* NOLINTBEGIN(bugprone-sizeof-expression) */ |
| 568 | console->pollers = reallocarray(console->pollers, console->n_pollers, |
| 569 | sizeof(*console->pollers)); |
| 570 | /* NOLINTEND(bugprone-sizeof-expression) */ |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 571 | |
| 572 | console->pollers[n] = poller; |
| 573 | |
| 574 | /* increase pollfds array too */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 575 | console->pollfds = |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 576 | reallocarray(console->pollfds, |
| 577 | (MAX_INTERNAL_POLLFD + console->n_pollers), |
| 578 | sizeof(*console->pollfds)); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 579 | |
| 580 | /* shift the end pollfds up by one */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 581 | memcpy(&console->pollfds[n + 1], &console->pollfds[n], |
| 582 | sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 583 | |
| 584 | console->pollfds[n].fd = fd; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 585 | console->pollfds[n].events = (short)(events & 0x7fff); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 586 | |
| 587 | return poller; |
| 588 | } |
| 589 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 590 | void console_poller_unregister(struct console *console, struct poller *poller) |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 591 | { |
| 592 | int i; |
| 593 | |
| 594 | /* find the entry in our pollers array */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 595 | for (i = 0; i < console->n_pollers; i++) { |
| 596 | if (console->pollers[i] == poller) { |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 597 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 598 | } |
| 599 | } |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 600 | |
| 601 | assert(i < console->n_pollers); |
| 602 | |
| 603 | console->n_pollers--; |
| 604 | |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 605 | /* |
| 606 | * Remove the item from the pollers array... |
| 607 | * |
| 608 | * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a |
| 609 | * pointer type. |
| 610 | */ |
| 611 | /* NOLINTBEGIN(bugprone-sizeof-expression) */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 612 | memmove(&console->pollers[i], &console->pollers[i + 1], |
| 613 | sizeof(*console->pollers) * (console->n_pollers - i)); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 614 | |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 615 | console->pollers = reallocarray(console->pollers, console->n_pollers, |
| 616 | sizeof(*console->pollers)); |
| 617 | /* NOLINTEND(bugprone-sizeof-expression) */ |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 618 | |
| 619 | /* ... and the pollfds array */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 620 | memmove(&console->pollfds[i], &console->pollfds[i + 1], |
| 621 | sizeof(*console->pollfds) * |
| 622 | (MAX_INTERNAL_POLLFD + console->n_pollers - i)); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 623 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 624 | console->pollfds = |
Andrew Jeffery | 91b5217 | 2023-04-19 12:42:14 +0930 | [diff] [blame] | 625 | reallocarray(console->pollfds, |
| 626 | (MAX_INTERNAL_POLLFD + console->n_pollers), |
| 627 | sizeof(*console->pollfds)); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 628 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 629 | free(poller); |
| 630 | } |
| 631 | |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 632 | void console_poller_set_events(struct console *console, struct poller *poller, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 633 | int events) |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 634 | { |
| 635 | int i; |
| 636 | |
| 637 | /* find the entry in our pollers array */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 638 | for (i = 0; i < console->n_pollers; i++) { |
| 639 | if (console->pollers[i] == poller) { |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 640 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 641 | } |
| 642 | } |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 643 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 644 | console->pollfds[i].events = (short)(events & 0x7fff); |
Jeremy Kerr | 6b1fed2 | 2017-02-07 21:40:38 +0800 | [diff] [blame] | 645 | } |
| 646 | |
Andrew Jeffery | fd04832 | 2023-04-18 12:02:01 +0930 | [diff] [blame] | 647 | void console_poller_set_timeout(struct console *console __attribute__((unused)), |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 648 | struct poller *poller, const struct timeval *tv) |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 649 | { |
| 650 | struct timeval now; |
| 651 | int rc; |
| 652 | |
| 653 | rc = get_current_time(&now); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 654 | if (rc) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 655 | return; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 656 | } |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 657 | |
| 658 | timeradd(&now, tv, &poller->timeout); |
| 659 | } |
| 660 | |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 661 | static long get_poll_timeout(struct console *console, struct timeval *cur_time) |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 662 | { |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame^] | 663 | struct timeval *earliest; |
| 664 | struct timeval interval; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 665 | struct poller *poller; |
| 666 | int i; |
| 667 | |
| 668 | earliest = NULL; |
| 669 | |
| 670 | for (i = 0; i < console->n_pollers; i++) { |
| 671 | poller = console->pollers[i]; |
| 672 | |
| 673 | if (poller->timeout_fn && timerisset(&poller->timeout) && |
| 674 | (!earliest || |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 675 | (earliest && timercmp(&poller->timeout, earliest, <)))) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 676 | // poller is buffering data and needs the poll |
| 677 | // function to timeout. |
| 678 | earliest = &poller->timeout; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | if (earliest) { |
| 683 | if (timercmp(earliest, cur_time, >)) { |
| 684 | /* recalculate the timeout period, time period has |
| 685 | * not elapsed */ |
| 686 | timersub(earliest, cur_time, &interval); |
| 687 | return ((interval.tv_sec * 1000) + |
| 688 | (interval.tv_usec / 1000)); |
Andrew Jeffery | 0b7b047 | 2023-04-19 12:48:51 +0930 | [diff] [blame] | 689 | } /* return from poll immediately */ |
| 690 | return 0; |
| 691 | |
| 692 | } /* poll indefinitely */ |
| 693 | return -1; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | static int call_pollers(struct console *console, struct timeval *cur_time) |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 697 | { |
| 698 | struct poller *poller; |
| 699 | struct pollfd *pollfd; |
| 700 | enum poller_ret prc; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame^] | 701 | int i; |
| 702 | int rc; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 703 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 704 | rc = 0; |
| 705 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 706 | /* |
| 707 | * Process poll events by iterating through the pollers and pollfds |
| 708 | * in-step, calling any pollers that we've found revents for. |
| 709 | */ |
| 710 | for (i = 0; i < console->n_pollers; i++) { |
| 711 | poller = console->pollers[i]; |
| 712 | pollfd = &console->pollfds[i]; |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 713 | prc = POLLER_OK; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 714 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 715 | /* process pending events... */ |
| 716 | if (pollfd->revents) { |
| 717 | prc = poller->event_fn(poller->handler, pollfd->revents, |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 718 | poller->data); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 719 | if (prc == POLLER_EXIT) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 720 | rc = -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 721 | } else if (prc == POLLER_REMOVE) { |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 722 | poller->remove = true; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 723 | } |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 724 | } |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 725 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 726 | if ((prc == POLLER_OK) && poller->timeout_fn && |
| 727 | timerisset(&poller->timeout) && |
| 728 | timercmp(&poller->timeout, cur_time, <=)) { |
| 729 | /* One of the ringbuffer consumers is buffering the |
| 730 | data stream. The amount of idle time the consumer |
| 731 | desired has expired. Process the buffered data for |
| 732 | transmission. */ |
| 733 | timerclear(&poller->timeout); |
| 734 | prc = poller->timeout_fn(poller->handler, poller->data); |
| 735 | if (prc == POLLER_EXIT) { |
| 736 | rc = -1; |
| 737 | } else if (prc == POLLER_REMOVE) { |
| 738 | poller->remove = true; |
| 739 | } |
| 740 | } |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 741 | } |
| 742 | |
| 743 | /** |
| 744 | * Process deferred removals; restarting each time we unregister, as |
| 745 | * the array will have changed |
| 746 | */ |
| 747 | for (;;) { |
| 748 | bool removed = false; |
| 749 | |
| 750 | for (i = 0; i < console->n_pollers; i++) { |
| 751 | poller = console->pollers[i]; |
| 752 | if (poller->remove) { |
Jeremy Kerr | 55c9712 | 2017-02-07 17:06:46 +0800 | [diff] [blame] | 753 | console_poller_unregister(console, poller); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 754 | removed = true; |
| 755 | break; |
| 756 | } |
| 757 | } |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 758 | if (!removed) { |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 759 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 760 | } |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | return rc; |
| 764 | } |
| 765 | |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 766 | static void sighandler(int signal) |
| 767 | { |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 768 | if (signal == SIGINT) { |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 769 | sigint = true; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 770 | } |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 771 | } |
| 772 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 773 | int run_console(struct console *console) |
| 774 | { |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 775 | sighandler_t sighandler_save = signal(SIGINT, sighandler); |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 776 | struct timeval tv; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 777 | long timeout; |
| 778 | ssize_t rc; |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 779 | |
| 780 | rc = 0; |
| 781 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 782 | for (;;) { |
| 783 | uint8_t buf[4096]; |
| 784 | |
Jeremy Kerr | 1764145 | 2017-02-07 22:09:13 +0800 | [diff] [blame] | 785 | BUILD_ASSERT(sizeof(buf) <= buffer_size); |
| 786 | |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 787 | if (sigint) { |
| 788 | fprintf(stderr, "Received interrupt, exiting\n"); |
| 789 | break; |
| 790 | } |
| 791 | |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 792 | rc = get_current_time(&tv); |
| 793 | if (rc) { |
| 794 | warn("Failed to read current time"); |
| 795 | break; |
| 796 | } |
| 797 | |
| 798 | timeout = get_poll_timeout(console, &tv); |
| 799 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 800 | rc = poll(console->pollfds, |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 801 | console->n_pollers + MAX_INTERNAL_POLLFD, |
| 802 | (int)timeout); |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 803 | |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 804 | if (rc < 0) { |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 805 | if (errno == EINTR) { |
| 806 | continue; |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 807 | } |
Andrew Jeffery | 0b7b047 | 2023-04-19 12:48:51 +0930 | [diff] [blame] | 808 | warn("poll error"); |
| 809 | break; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 810 | } |
| 811 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 812 | /* process internal fd first */ |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 813 | if (console->pollfds[console->n_pollers].revents) { |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 814 | rc = read(console->tty_fd, buf, sizeof(buf)); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 815 | if (rc <= 0) { |
| 816 | warn("Error reading from tty device"); |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 817 | rc = -1; |
| 818 | break; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 819 | } |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 820 | rc = ringbuffer_queue(console->rb, buf, rc); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 821 | if (rc) { |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 822 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 823 | } |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 824 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 825 | |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 826 | if (console->pollfds[console->n_pollers + 1].revents) { |
| 827 | sd_bus_process(console->bus, NULL); |
| 828 | } |
| 829 | |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 830 | /* ... and then the pollers */ |
Johnathan Mantey | 1cecc5d | 2019-02-28 15:01:46 -0800 | [diff] [blame] | 831 | rc = call_pollers(console, &tv); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 832 | if (rc) { |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 833 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 834 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 835 | } |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 836 | |
| 837 | signal(SIGINT, sighandler_save); |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 838 | sd_bus_unref(console->bus); |
Jeremy Kerr | 769cee1 | 2016-03-15 17:53:56 +0800 | [diff] [blame] | 839 | |
| 840 | return rc ? -1 : 0; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 841 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 842 | static const struct option options[] = { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 843 | { "config", required_argument, 0, 'c' }, |
| 844 | { 0, 0, 0, 0 }, |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 845 | }; |
| 846 | |
| 847 | int main(int argc, char **argv) |
| 848 | { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 849 | const char *config_filename = NULL; |
Vishwanatha Subbanna | 6221ce9 | 2016-07-20 05:35:45 -0500 | [diff] [blame] | 850 | const char *config_tty_kname = NULL; |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 851 | struct console *console; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 852 | struct config *config; |
| 853 | int rc; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 854 | |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 855 | rc = -1; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 856 | |
| 857 | for (;;) { |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame^] | 858 | int c; |
| 859 | int idx; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 860 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 861 | c = getopt_long(argc, argv, "c:", options, &idx); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 862 | if (c == -1) { |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 863 | break; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 864 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 865 | |
| 866 | switch (c) { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 867 | case 'c': |
| 868 | config_filename = optarg; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 869 | break; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 870 | case 'h': |
| 871 | case '?': |
| 872 | usage(argv[0]); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 873 | return EXIT_SUCCESS; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 874 | } |
| 875 | } |
| 876 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 877 | if (optind < argc) { |
Andrew Jeffery | 91dde14 | 2020-02-12 22:46:27 +1030 | [diff] [blame] | 878 | config_tty_kname = argv[optind]; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 879 | } |
Vishwanatha Subbanna | 6221ce9 | 2016-07-20 05:35:45 -0500 | [diff] [blame] | 880 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 881 | console = malloc(sizeof(struct console)); |
| 882 | memset(console, 0, sizeof(*console)); |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 883 | console->pollfds = |
| 884 | calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds)); |
Jeremy Kerr | f733c85 | 2017-02-07 18:40:10 +0800 | [diff] [blame] | 885 | console->rb = ringbuffer_init(buffer_size); |
Jeremy Kerr | 329a35f | 2016-03-10 15:36:01 +0800 | [diff] [blame] | 886 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 887 | config = config_init(config_filename); |
| 888 | if (!config) { |
| 889 | warnx("Can't read configuration, exiting."); |
| 890 | goto out_free; |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 891 | } |
| 892 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 893 | if (!config_tty_kname) { |
Andrew Jeffery | 91dde14 | 2020-02-12 22:46:27 +1030 | [diff] [blame] | 894 | config_tty_kname = config_get_value(config, "upstream-tty"); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 895 | } |
Andrew Jeffery | 91dde14 | 2020-02-12 22:46:27 +1030 | [diff] [blame] | 896 | |
| 897 | if (!config_tty_kname) { |
| 898 | warnx("No TTY device specified"); |
| 899 | usage(argv[0]); |
| 900 | return EXIT_FAILURE; |
| 901 | } |
| 902 | |
Vishwanatha Subbanna | 6221ce9 | 2016-07-20 05:35:45 -0500 | [diff] [blame] | 903 | console->tty_kname = config_tty_kname; |
| 904 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 905 | rc = tty_init(console, config); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 906 | if (rc) { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 907 | goto out_config_fini; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 908 | } |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 909 | |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 910 | dbus_init(console, config); |
| 911 | |
Jeremy Kerr | d47963e | 2016-03-16 17:29:55 +0800 | [diff] [blame] | 912 | handlers_init(console, config); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 913 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 914 | rc = run_console(console); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 915 | |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 916 | handlers_fini(console); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 917 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 918 | out_config_fini: |
| 919 | config_fini(config); |
| 920 | |
Jeremy Kerr | 957818b | 2016-03-08 14:35:15 +0800 | [diff] [blame] | 921 | out_free: |
Jeremy Kerr | 89ea819 | 2016-03-15 17:57:43 +0800 | [diff] [blame] | 922 | free(console->pollers); |
| 923 | free(console->pollfds); |
Jeremy Kerr | 1a0e03b | 2016-03-08 17:57:11 +0800 | [diff] [blame] | 924 | free(console->tty_sysfs_devnode); |
| 925 | free(console->tty_dev); |
| 926 | free(console); |
Jeremy Kerr | d831f96 | 2016-01-29 17:18:01 +0800 | [diff] [blame] | 927 | |
| 928 | return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
| 929 | } |