blob: d06cc329c7b5c76de5a78a42c2ef65f5adc25a0c [file] [log] [blame]
Jeremy Kerrd831f962016-01-29 17:18:01 +08001/**
2 * Console server process for OpenBMC
3 *
Jeremy Kerr9326d772016-03-17 17:15:02 +08004 * 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 Kerrd831f962016-01-29 17:18:01 +080017 */
18
Jeremy Kerr329a35f2016-03-10 15:36:01 +080019#include <assert.h>
Jeremy Kerr769cee12016-03-15 17:53:56 +080020#include <errno.h>
21#include <signal.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080022#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 Kerrd831f962016-01-29 17:18:01 +080029#include <string.h>
30#include <getopt.h>
Jeremy Kerr17217842016-01-29 18:44:21 +080031#include <limits.h>
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080032#include <time.h>
Jeremy Kerr54e95692016-03-24 17:07:56 +080033#include <termios.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080034
35#include <sys/types.h>
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080036#include <sys/time.h>
Joel Stanley87e344c2016-09-01 00:00:51 +093037#include <poll.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080038
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080039#include "console-server.h"
Jeremy Kerrd831f962016-01-29 17:18:01 +080040
Jeremy Kerrf733c852017-02-07 18:40:10 +080041/* size of the shared backlog ringbuffer */
Andrew Jeffery5db8c792023-04-18 21:48:24 +093042const size_t buffer_size = 128ul * 1024ul;
Jeremy Kerrf733c852017-02-07 18:40:10 +080043
Jeremy Kerr769cee12016-03-15 17:53:56 +080044/* state shared with the signal handler */
45static bool sigint;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080046
Jeremy Kerrd831f962016-01-29 17:18:01 +080047static void usage(const char *progname)
48{
49 fprintf(stderr,
Andrew Jefferya72711a2023-04-18 18:19:41 +093050 "usage: %s [options] <DEVICE>\n"
51 "\n"
52 "Options:\n"
53 " --config <FILE> Use FILE for configuration\n"
54 "",
Jeremy Kerrd831f962016-01-29 17:18:01 +080055 progname);
56}
57
Jeremy Kerr17217842016-01-29 18:44:21 +080058/* populates tty_dev and tty_sysfs_devnode, using the tty kernel name */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080059static int tty_find_device(struct console *console)
Jeremy Kerr17217842016-01-29 18:44:21 +080060{
61 char *tty_class_device_link;
62 char *tty_device_tty_dir;
63 char *tty_device_reldir;
Yi Li45ad7672016-10-18 23:21:19 +080064 char *tty_path_input;
65 char *tty_path_input_real;
66 char *tty_kname_real;
Jeremy Kerr17217842016-01-29 18:44:21 +080067 int rc;
68
Jeremy Kerr17217842016-01-29 18:44:21 +080069 tty_class_device_link = NULL;
70 tty_device_tty_dir = NULL;
71 tty_device_reldir = NULL;
Yi Li45ad7672016-10-18 23:21:19 +080072 tty_path_input = NULL;
73 tty_path_input_real = NULL;
74 tty_kname_real = NULL;
Jeremy Kerr17217842016-01-29 18:44:21 +080075
Yi Li45ad7672016-10-18 23:21:19 +080076 /* udev may rename the tty name with a symbol link, try to resolve */
77 rc = asprintf(&tty_path_input, "/dev/%s", console->tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093078 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +080079 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093080 }
Jeremy Kerr17217842016-01-29 18:44:21 +080081
Yi Li45ad7672016-10-18 23:21:19 +080082 tty_path_input_real = realpath(tty_path_input, NULL);
83 if (!tty_path_input_real) {
84 warn("Can't find realpath for /dev/%s", console->tty_kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +093085 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +080086 goto out_free;
87 }
88
89 tty_kname_real = basename(tty_path_input_real);
90 if (!tty_kname_real) {
91 warn("Can't find real name for /dev/%s", console->tty_kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +093092 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +080093 goto out_free;
94 }
95
Andrew Jefferya72711a2023-04-18 18:19:41 +093096 rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s",
97 tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093098 if (rc < 0) {
Yi Li45ad7672016-10-18 23:21:19 +080099 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930100 }
Yi Li45ad7672016-10-18 23:21:19 +0800101
Jeremy Kerr17217842016-01-29 18:44:21 +0800102 tty_device_tty_dir = realpath(tty_class_device_link, NULL);
Yi Li45ad7672016-10-18 23:21:19 +0800103 if (!tty_device_tty_dir) {
104 warn("Can't query sysfs for device %s", tty_kname_real);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930105 rc = -1;
Jeremy Kerr17217842016-01-29 18:44:21 +0800106 goto out_free;
107 }
108
109 rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930110 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800111 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930112 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800113
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800114 console->tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930115 if (!console->tty_sysfs_devnode) {
Yi Li45ad7672016-10-18 23:21:19 +0800116 warn("Can't find parent device for %s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930117 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800118
Yi Li45ad7672016-10-18 23:21:19 +0800119 rc = asprintf(&console->tty_dev, "/dev/%s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930120 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800121 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930122 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800123
124 rc = 0;
125
126out_free:
127 free(tty_class_device_link);
128 free(tty_device_tty_dir);
129 free(tty_device_reldir);
Yi Li45ad7672016-10-18 23:21:19 +0800130 free(tty_path_input);
131 free(tty_path_input_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800132 return rc;
133}
134
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800135static int tty_set_sysfs_attr(struct console *console, const char *name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930136 int value)
Jeremy Kerr957818b2016-03-08 14:35:15 +0800137{
138 char *path;
139 FILE *fp;
140 int rc;
141
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800142 rc = asprintf(&path, "%s/%s", console->tty_sysfs_devnode, name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930143 if (rc < 0) {
Jeremy Kerr957818b2016-03-08 14:35:15 +0800144 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930145 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800146
147 fp = fopen(path, "w");
148 if (!fp) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930149 warn("Can't access attribute %s on device %s", name,
150 console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800151 rc = -1;
152 goto out_free;
153 }
154 setvbuf(fp, NULL, _IONBF, 0);
155
156 rc = fprintf(fp, "0x%x", value);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930157 if (rc < 0) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930158 warn("Error writing to %s attribute of device %s", name,
159 console->tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930160 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800161 fclose(fp);
162
Jeremy Kerr957818b2016-03-08 14:35:15 +0800163out_free:
164 free(path);
165 return rc;
166}
167
Jeremy Kerrd831f962016-01-29 17:18:01 +0800168/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700169 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800170 */
Ninad Palsulee258e512023-04-26 22:17:57 -0500171void tty_init_termios(struct console *console)
Jeremy Kerr54e95692016-03-24 17:07:56 +0800172{
173 struct termios termios;
174 int rc;
175
176 rc = tcgetattr(console->tty_fd, &termios);
177 if (rc) {
178 warn("Can't read tty termios");
179 return;
180 }
181
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700182 if (console->tty_baud) {
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930183 if (cfsetspeed(&termios, console->tty_baud) < 0) {
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700184 warn("Couldn't set speeds for %s", console->tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930185 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700186 }
187
188 /* Set console to raw mode: we don't want any processing to occur on
189 * the underlying terminal input/output.
190 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800191 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700192
Jeremy Kerr54e95692016-03-24 17:07:56 +0800193 rc = tcsetattr(console->tty_fd, TCSANOW, &termios);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930194 if (rc) {
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700195 warn("Can't set terminal options for %s", console->tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930196 }
Jeremy Kerr54e95692016-03-24 17:07:56 +0800197}
198
199/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800200 * Open and initialise the serial device
201 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800202static int tty_init_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800203{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930204 if (console->tty_sirq) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800205 tty_set_sysfs_attr(console, "sirq", console->tty_sirq);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930206 }
207 if (console->tty_lpc_addr) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800208 tty_set_sysfs_attr(console, "lpc_address",
Andrew Jefferya72711a2023-04-18 18:19:41 +0930209 console->tty_lpc_addr);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930210 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800211
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800212 console->tty_fd = open(console->tty_dev, O_RDWR);
213 if (console->tty_fd <= 0) {
214 warn("Can't open tty %s", console->tty_dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800215 return -1;
216 }
217
218 /* Disable character delay. We may want to later enable this when
219 * we detect larger amounts of data
220 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800221 fcntl(console->tty_fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800222
Jeremy Kerr54e95692016-03-24 17:07:56 +0800223 tty_init_termios(console);
224
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800225 console->pollfds[console->n_pollers].fd = console->tty_fd;
226 console->pollfds[console->n_pollers].events = POLLIN;
227
Jeremy Kerrd831f962016-01-29 17:18:01 +0800228 return 0;
229}
230
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800231static int tty_init(struct console *console, struct config *config)
232{
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930233 unsigned long parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800234 const char *val;
235 char *endp;
236 int rc;
237
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800238 val = config_get_value(config, "lpc-address");
239 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930240 errno = 0;
241 parsed = strtoul(val, &endp, 0);
242 if (parsed == ULONG_MAX && errno == ERANGE) {
243 warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'",
244 val);
245 return -1;
246 }
247
248 if (parsed > UINT16_MAX) {
249 warn("Invalid LPC address '%s'", val);
250 return -1;
251 }
252
253 console->tty_lpc_addr = (uint16_t)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800254 if (endp == optarg) {
255 warn("Invalid LPC address: '%s'", val);
256 return -1;
257 }
258 }
259
260 val = config_get_value(config, "sirq");
261 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930262 errno = 0;
263 parsed = strtoul(val, &endp, 0);
264 if (parsed == ULONG_MAX && errno == ERANGE) {
265 warn("Cannot interpret 'sirq' value as an unsigned long: '%s'",
266 val);
267 }
268
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930269 if (parsed > 16) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930270 warn("Invalid LPC SERIRQ: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930271 }
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930272
273 console->tty_sirq = (int)parsed;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930274 if (endp == optarg) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800275 warn("Invalid sirq: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930276 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800277 }
278
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700279 val = config_get_value(config, "baud");
280 if (val) {
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930281 if (config_parse_baud(&console->tty_baud, val)) {
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700282 warnx("Invalid baud rate: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930283 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700284 }
285
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800286 if (!console->tty_kname) {
287 warnx("Error: No TTY device specified");
288 return -1;
289 }
290
291 rc = tty_find_device(console);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930292 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800293 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930294 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800295
296 rc = tty_init_io(console);
297 return rc;
298}
299
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800300int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800301{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800302 return write_buf_to_fd(console->tty_fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800303}
304
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800305static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800306{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930307 /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
308 extern struct handler *__start_handlers;
309 extern struct handler *__stop_handlers;
310 /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800311 struct handler *handler;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930312 int i;
313 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800314
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800315 console->n_handlers = &__stop_handlers - &__start_handlers;
316 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800317
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930318 printf("%ld handler%s\n", console->n_handlers,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930319 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800320
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800321 for (i = 0; i < console->n_handlers; i++) {
322 handler = console->handlers[i];
323
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800324 rc = 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930325 if (handler->init) {
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800326 rc = handler->init(handler, console, config);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930327 }
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800328
329 handler->active = rc == 0;
330
331 printf(" %s [%sactive]\n", handler->name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930332 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800333 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800334}
335
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800336static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800337{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800338 struct handler *handler;
339 int i;
340
341 for (i = 0; i < console->n_handlers; i++) {
342 handler = console->handlers[i];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930343 if (handler->fini && handler->active) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800344 handler->fini(handler);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930345 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800346 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800347}
348
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800349static int get_current_time(struct timeval *tv)
350{
351 struct timespec t;
352 int rc;
353
354 /*
355 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
356 * local time changes. However, a struct timeval is more
357 * convenient for calculations, so convert to that.
358 */
359 rc = clock_gettime(CLOCK_MONOTONIC, &t);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930360 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800361 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930362 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800363
364 tv->tv_sec = t.tv_sec;
365 tv->tv_usec = t.tv_nsec / 1000;
366
367 return 0;
368}
369
Andrew Jefferya72711a2023-04-18 18:19:41 +0930370struct ringbuffer_consumer *
371console_ringbuffer_consumer_register(struct console *console,
372 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800373{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800374 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800375}
376
Jeremy Kerr55c97122017-02-07 17:06:46 +0800377struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930378 struct handler *handler,
379 poller_event_fn_t poller_fn,
380 poller_timeout_fn_t timeout_fn, int fd,
381 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800382{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800383 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930384 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800385
386 poller = malloc(sizeof(*poller));
387 poller->remove = false;
388 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800389 poller->event_fn = poller_fn;
390 poller->timeout_fn = timeout_fn;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800391 poller->data = data;
392
393 /* add one to our pollers array */
394 n = console->n_pollers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930395 /*
396 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
397 * pointer type.
398 */
399 /* NOLINTBEGIN(bugprone-sizeof-expression) */
400 console->pollers = reallocarray(console->pollers, console->n_pollers,
401 sizeof(*console->pollers));
402 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800403
404 console->pollers[n] = poller;
405
406 /* increase pollfds array too */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930407 console->pollfds =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930408 reallocarray(console->pollfds,
409 (MAX_INTERNAL_POLLFD + console->n_pollers),
410 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800411
412 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930413 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
414 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800415
416 console->pollfds[n].fd = fd;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930417 console->pollfds[n].events = (short)(events & 0x7fff);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800418
419 return poller;
420}
421
Andrew Jefferya72711a2023-04-18 18:19:41 +0930422void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800423{
424 int i;
425
426 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930427 for (i = 0; i < console->n_pollers; i++) {
428 if (console->pollers[i] == poller) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800429 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930430 }
431 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800432
433 assert(i < console->n_pollers);
434
435 console->n_pollers--;
436
Andrew Jeffery91b52172023-04-19 12:42:14 +0930437 /*
438 * Remove the item from the pollers array...
439 *
440 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
441 * pointer type.
442 */
443 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930444 memmove(&console->pollers[i], &console->pollers[i + 1],
445 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800446
Andrew Jeffery91b52172023-04-19 12:42:14 +0930447 console->pollers = reallocarray(console->pollers, console->n_pollers,
448 sizeof(*console->pollers));
449 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800450
451 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930452 memmove(&console->pollfds[i], &console->pollfds[i + 1],
453 sizeof(*console->pollfds) *
454 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800455
Andrew Jefferya72711a2023-04-18 18:19:41 +0930456 console->pollfds =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930457 reallocarray(console->pollfds,
458 (MAX_INTERNAL_POLLFD + console->n_pollers),
459 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800460
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800461 free(poller);
462}
463
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800464void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930465 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800466{
467 int i;
468
469 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930470 for (i = 0; i < console->n_pollers; i++) {
471 if (console->pollers[i] == poller) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800472 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930473 }
474 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800475
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930476 console->pollfds[i].events = (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800477}
478
Andrew Jefferyfd048322023-04-18 12:02:01 +0930479void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930480 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800481{
482 struct timeval now;
483 int rc;
484
485 rc = get_current_time(&now);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930486 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800487 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930488 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800489
490 timeradd(&now, tv, &poller->timeout);
491}
492
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930493static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800494{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930495 struct timeval *earliest;
496 struct timeval interval;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800497 struct poller *poller;
498 int i;
499
500 earliest = NULL;
501
502 for (i = 0; i < console->n_pollers; i++) {
503 poller = console->pollers[i];
504
505 if (poller->timeout_fn && timerisset(&poller->timeout) &&
506 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930507 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800508 // poller is buffering data and needs the poll
509 // function to timeout.
510 earliest = &poller->timeout;
511 }
512 }
513
514 if (earliest) {
515 if (timercmp(earliest, cur_time, >)) {
516 /* recalculate the timeout period, time period has
517 * not elapsed */
518 timersub(earliest, cur_time, &interval);
519 return ((interval.tv_sec * 1000) +
520 (interval.tv_usec / 1000));
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930521 } /* return from poll immediately */
522 return 0;
523
524 } /* poll indefinitely */
525 return -1;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800526}
527
528static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800529{
530 struct poller *poller;
531 struct pollfd *pollfd;
532 enum poller_ret prc;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930533 int i;
534 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800535
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800536 rc = 0;
537
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800538 /*
539 * Process poll events by iterating through the pollers and pollfds
540 * in-step, calling any pollers that we've found revents for.
541 */
542 for (i = 0; i < console->n_pollers; i++) {
543 poller = console->pollers[i];
544 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800545 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800546
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800547 /* process pending events... */
548 if (pollfd->revents) {
549 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930550 poller->data);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930551 if (prc == POLLER_EXIT) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800552 rc = -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930553 } else if (prc == POLLER_REMOVE) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800554 poller->remove = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930555 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800556 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800557
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800558 if ((prc == POLLER_OK) && poller->timeout_fn &&
559 timerisset(&poller->timeout) &&
560 timercmp(&poller->timeout, cur_time, <=)) {
561 /* One of the ringbuffer consumers is buffering the
562 data stream. The amount of idle time the consumer
563 desired has expired. Process the buffered data for
564 transmission. */
565 timerclear(&poller->timeout);
566 prc = poller->timeout_fn(poller->handler, poller->data);
567 if (prc == POLLER_EXIT) {
568 rc = -1;
569 } else if (prc == POLLER_REMOVE) {
570 poller->remove = true;
571 }
572 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800573 }
574
575 /**
576 * Process deferred removals; restarting each time we unregister, as
577 * the array will have changed
578 */
579 for (;;) {
580 bool removed = false;
581
582 for (i = 0; i < console->n_pollers; i++) {
583 poller = console->pollers[i];
584 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800585 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800586 removed = true;
587 break;
588 }
589 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930590 if (!removed) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800591 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930592 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800593 }
594
595 return rc;
596}
597
Jeremy Kerr769cee12016-03-15 17:53:56 +0800598static void sighandler(int signal)
599{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930600 if (signal == SIGINT) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800601 sigint = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930602 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800603}
604
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800605int run_console(struct console *console)
606{
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930607 sighandler_t sighandler_save = signal(SIGINT, sighandler);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800608 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930609 long timeout;
610 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800611
612 rc = 0;
613
Jeremy Kerrd831f962016-01-29 17:18:01 +0800614 for (;;) {
615 uint8_t buf[4096];
616
Jeremy Kerr17641452017-02-07 22:09:13 +0800617 BUILD_ASSERT(sizeof(buf) <= buffer_size);
618
Jeremy Kerr769cee12016-03-15 17:53:56 +0800619 if (sigint) {
620 fprintf(stderr, "Received interrupt, exiting\n");
621 break;
622 }
623
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800624 rc = get_current_time(&tv);
625 if (rc) {
626 warn("Failed to read current time");
627 break;
628 }
629
630 timeout = get_poll_timeout(console, &tv);
631
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800632 rc = poll(console->pollfds,
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930633 console->n_pollers + MAX_INTERNAL_POLLFD,
634 (int)timeout);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800635
Jeremy Kerrd831f962016-01-29 17:18:01 +0800636 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800637 if (errno == EINTR) {
638 continue;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800639 }
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930640 warn("poll error");
641 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800642 }
643
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800644 /* process internal fd first */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800645 if (console->pollfds[console->n_pollers].revents) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800646 rc = read(console->tty_fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800647 if (rc <= 0) {
648 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800649 rc = -1;
650 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800651 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800652 rc = ringbuffer_queue(console->rb, buf, rc);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930653 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800654 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930655 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800656 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800657
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800658 if (console->pollfds[console->n_pollers + 1].revents) {
659 sd_bus_process(console->bus, NULL);
660 }
661
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800662 /* ... and then the pollers */
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800663 rc = call_pollers(console, &tv);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930664 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800665 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930666 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800667 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800668
669 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800670 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800671
672 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800673}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800674static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930675 { "config", required_argument, 0, 'c' },
676 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800677};
678
679int main(int argc, char **argv)
680{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800681 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500682 const char *config_tty_kname = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800683 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800684 struct config *config;
685 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800686
Jeremy Kerr957818b2016-03-08 14:35:15 +0800687 rc = -1;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800688
689 for (;;) {
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930690 int c;
691 int idx;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800692
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800693 c = getopt_long(argc, argv, "c:", options, &idx);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930694 if (c == -1) {
Jeremy Kerrd831f962016-01-29 17:18:01 +0800695 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930696 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800697
698 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800699 case 'c':
700 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800701 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800702 case 'h':
703 case '?':
704 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800705 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800706 }
707 }
708
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930709 if (optind < argc) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030710 config_tty_kname = argv[optind];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930711 }
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500712
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800713 console = malloc(sizeof(struct console));
714 memset(console, 0, sizeof(*console));
Andrew Jefferya72711a2023-04-18 18:19:41 +0930715 console->pollfds =
716 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Jeremy Kerrf733c852017-02-07 18:40:10 +0800717 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800718
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800719 config = config_init(config_filename);
720 if (!config) {
721 warnx("Can't read configuration, exiting.");
722 goto out_free;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800723 }
724
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930725 if (!config_tty_kname) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030726 config_tty_kname = config_get_value(config, "upstream-tty");
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930727 }
Andrew Jeffery91dde142020-02-12 22:46:27 +1030728
729 if (!config_tty_kname) {
730 warnx("No TTY device specified");
731 usage(argv[0]);
732 return EXIT_FAILURE;
733 }
734
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500735 console->tty_kname = config_tty_kname;
736
Ninad Palsule4e718692023-04-26 21:18:35 -0500737 console->console_id = config_get_value(config, "socket-id");
738 if (!console->console_id) {
739 warnx("Error: The socket-id is not set in the config file");
740 return EXIT_FAILURE;
741 }
742
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800743 rc = tty_init(console, config);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930744 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800745 goto out_config_fini;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930746 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800747
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800748 dbus_init(console, config);
749
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800750 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800751
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800752 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800753
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800754 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800755
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800756out_config_fini:
757 config_fini(config);
758
Jeremy Kerr957818b2016-03-08 14:35:15 +0800759out_free:
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800760 free(console->pollers);
761 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800762 free(console->tty_sysfs_devnode);
763 free(console->tty_dev);
764 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800765
766 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
767}