blob: 3b9445130dc8e177bf370fdb1e57b30ad68e81de [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>
Ninad Palsuleb14ca192023-03-31 09:49:00 -050037#include <sys/socket.h>
Joel Stanley87e344c2016-09-01 00:00:51 +093038#include <poll.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080039
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080040#include "console-server.h"
Jeremy Kerrd831f962016-01-29 17:18:01 +080041
Jeremy Kerrf733c852017-02-07 18:40:10 +080042/* size of the shared backlog ringbuffer */
Andrew Jeffery5db8c792023-04-18 21:48:24 +093043const size_t buffer_size = 128ul * 1024ul;
Jeremy Kerrf733c852017-02-07 18:40:10 +080044
Jeremy Kerr769cee12016-03-15 17:53:56 +080045/* state shared with the signal handler */
46static bool sigint;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080047
Jeremy Kerrd831f962016-01-29 17:18:01 +080048static void usage(const char *progname)
49{
50 fprintf(stderr,
Andrew Jefferya72711a2023-04-18 18:19:41 +093051 "usage: %s [options] <DEVICE>\n"
52 "\n"
53 "Options:\n"
54 " --config <FILE> Use FILE for configuration\n"
55 "",
Jeremy Kerrd831f962016-01-29 17:18:01 +080056 progname);
57}
58
Jeremy Kerr17217842016-01-29 18:44:21 +080059/* populates tty_dev and tty_sysfs_devnode, using the tty kernel name */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080060static int tty_find_device(struct console *console)
Jeremy Kerr17217842016-01-29 18:44:21 +080061{
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +093062 char *tty_class_device_link = NULL;
63 char *tty_path_input_real = NULL;
64 char *tty_device_tty_dir = NULL;
65 char *tty_device_reldir = NULL;
66 char *tty_path_input = NULL;
67 char *tty_kname_real = NULL;
Jeremy Kerr17217842016-01-29 18:44:21 +080068 int rc;
69
Yi Li45ad7672016-10-18 23:21:19 +080070 /* udev may rename the tty name with a symbol link, try to resolve */
71 rc = asprintf(&tty_path_input, "/dev/%s", console->tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093072 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +080073 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093074 }
Jeremy Kerr17217842016-01-29 18:44:21 +080075
Yi Li45ad7672016-10-18 23:21:19 +080076 tty_path_input_real = realpath(tty_path_input, NULL);
77 if (!tty_path_input_real) {
78 warn("Can't find realpath for /dev/%s", console->tty_kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +093079 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +080080 goto out_free;
81 }
82
83 tty_kname_real = basename(tty_path_input_real);
84 if (!tty_kname_real) {
85 warn("Can't find real name for /dev/%s", console->tty_kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +093086 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +080087 goto out_free;
88 }
89
Andrew Jefferya72711a2023-04-18 18:19:41 +093090 rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s",
91 tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093092 if (rc < 0) {
Yi Li45ad7672016-10-18 23:21:19 +080093 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093094 }
Yi Li45ad7672016-10-18 23:21:19 +080095
Jeremy Kerr17217842016-01-29 18:44:21 +080096 tty_device_tty_dir = realpath(tty_class_device_link, NULL);
Yi Li45ad7672016-10-18 23:21:19 +080097 if (!tty_device_tty_dir) {
98 warn("Can't query sysfs for device %s", tty_kname_real);
Andrew Jeffery15792aa2023-04-18 16:47:33 +093099 rc = -1;
Jeremy Kerr17217842016-01-29 18:44:21 +0800100 goto out_free;
101 }
102
103 rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930104 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800105 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930106 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800107
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800108 console->tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930109 if (!console->tty_sysfs_devnode) {
Yi Li45ad7672016-10-18 23:21:19 +0800110 warn("Can't find parent device for %s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930111 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800112
Yi Li45ad7672016-10-18 23:21:19 +0800113 rc = asprintf(&console->tty_dev, "/dev/%s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930114 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800115 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930116 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800117
118 rc = 0;
119
120out_free:
121 free(tty_class_device_link);
122 free(tty_device_tty_dir);
123 free(tty_device_reldir);
Yi Li45ad7672016-10-18 23:21:19 +0800124 free(tty_path_input);
125 free(tty_path_input_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800126 return rc;
127}
128
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800129static int tty_set_sysfs_attr(struct console *console, const char *name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930130 int value)
Jeremy Kerr957818b2016-03-08 14:35:15 +0800131{
132 char *path;
133 FILE *fp;
134 int rc;
135
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800136 rc = asprintf(&path, "%s/%s", console->tty_sysfs_devnode, name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930137 if (rc < 0) {
Jeremy Kerr957818b2016-03-08 14:35:15 +0800138 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930139 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800140
141 fp = fopen(path, "w");
142 if (!fp) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930143 warn("Can't access attribute %s on device %s", name,
144 console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800145 rc = -1;
146 goto out_free;
147 }
148 setvbuf(fp, NULL, _IONBF, 0);
149
150 rc = fprintf(fp, "0x%x", value);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930151 if (rc < 0) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930152 warn("Error writing to %s attribute of device %s", name,
153 console->tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930154 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800155 fclose(fp);
156
Jeremy Kerr957818b2016-03-08 14:35:15 +0800157out_free:
158 free(path);
159 return rc;
160}
161
Jeremy Kerrd831f962016-01-29 17:18:01 +0800162/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700163 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800164 */
Ninad Palsulee258e512023-04-26 22:17:57 -0500165void tty_init_termios(struct console *console)
Jeremy Kerr54e95692016-03-24 17:07:56 +0800166{
167 struct termios termios;
168 int rc;
169
170 rc = tcgetattr(console->tty_fd, &termios);
171 if (rc) {
172 warn("Can't read tty termios");
173 return;
174 }
175
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700176 if (console->tty_baud) {
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930177 if (cfsetspeed(&termios, console->tty_baud) < 0) {
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700178 warn("Couldn't set speeds for %s", console->tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930179 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700180 }
181
182 /* Set console to raw mode: we don't want any processing to occur on
183 * the underlying terminal input/output.
184 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800185 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700186
Jeremy Kerr54e95692016-03-24 17:07:56 +0800187 rc = tcsetattr(console->tty_fd, TCSANOW, &termios);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930188 if (rc) {
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700189 warn("Can't set terminal options for %s", console->tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930190 }
Jeremy Kerr54e95692016-03-24 17:07:56 +0800191}
192
193/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800194 * Open and initialise the serial device
195 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800196static int tty_init_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800197{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930198 if (console->tty_sirq) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800199 tty_set_sysfs_attr(console, "sirq", console->tty_sirq);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930200 }
201 if (console->tty_lpc_addr) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800202 tty_set_sysfs_attr(console, "lpc_address",
Andrew Jefferya72711a2023-04-18 18:19:41 +0930203 console->tty_lpc_addr);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930204 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800205
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800206 console->tty_fd = open(console->tty_dev, O_RDWR);
207 if (console->tty_fd <= 0) {
208 warn("Can't open tty %s", console->tty_dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800209 return -1;
210 }
211
212 /* Disable character delay. We may want to later enable this when
213 * we detect larger amounts of data
214 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800215 fcntl(console->tty_fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800216
Jeremy Kerr54e95692016-03-24 17:07:56 +0800217 tty_init_termios(console);
218
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800219 console->pollfds[console->n_pollers].fd = console->tty_fd;
220 console->pollfds[console->n_pollers].events = POLLIN;
221
Jeremy Kerrd831f962016-01-29 17:18:01 +0800222 return 0;
223}
224
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930225static int tty_init(struct console *console, struct config *config,
226 const char *tty_arg)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800227{
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930228 unsigned long parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800229 const char *val;
230 char *endp;
231 int rc;
232
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800233 val = config_get_value(config, "lpc-address");
234 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930235 errno = 0;
236 parsed = strtoul(val, &endp, 0);
237 if (parsed == ULONG_MAX && errno == ERANGE) {
238 warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'",
239 val);
240 return -1;
241 }
242
243 if (parsed > UINT16_MAX) {
244 warn("Invalid LPC address '%s'", val);
245 return -1;
246 }
247
248 console->tty_lpc_addr = (uint16_t)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800249 if (endp == optarg) {
250 warn("Invalid LPC address: '%s'", val);
251 return -1;
252 }
253 }
254
255 val = config_get_value(config, "sirq");
256 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930257 errno = 0;
258 parsed = strtoul(val, &endp, 0);
259 if (parsed == ULONG_MAX && errno == ERANGE) {
260 warn("Cannot interpret 'sirq' value as an unsigned long: '%s'",
261 val);
262 }
263
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930264 if (parsed > 16) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930265 warn("Invalid LPC SERIRQ: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930266 }
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930267
268 console->tty_sirq = (int)parsed;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930269 if (endp == optarg) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800270 warn("Invalid sirq: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930271 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800272 }
273
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700274 val = config_get_value(config, "baud");
275 if (val) {
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930276 if (config_parse_baud(&console->tty_baud, val)) {
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700277 warnx("Invalid baud rate: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930278 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700279 }
280
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930281 if (tty_arg) {
282 console->tty_kname = tty_arg;
283 } else if ((val = config_get_value(config, "upstream-tty"))) {
284 console->tty_kname = val;
285 } else {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800286 warnx("Error: No TTY device specified");
287 return -1;
288 }
289
290 rc = tty_find_device(console);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930291 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800292 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930293 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800294
295 rc = tty_init_io(console);
296 return rc;
297}
298
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800299int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800300{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800301 return write_buf_to_fd(console->tty_fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800302}
303
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500304/* Read console if from config and prepare a socket name */
305static int set_socket_info(struct console *console, struct config *config)
306{
307 ssize_t len;
308
309 console->console_id = config_get_value(config, "socket-id");
310 if (!console->console_id) {
311 warnx("Error: The socket-id is not set in the config file");
312 return EXIT_FAILURE;
313 }
314
315 /* Get the socket name/path */
316 len = console_socket_path(console->socket_name, console->console_id);
317 if (len < 0) {
318 warn("Failed to set socket path: %s", strerror(errno));
319 return EXIT_FAILURE;
320 }
321
322 /* Socket name is not a null terminated string hence save the length */
323 console->socket_name_len = len;
324
325 return 0;
326}
327
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800328static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800329{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930330 /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
331 extern struct handler *__start_handlers;
332 extern struct handler *__stop_handlers;
333 /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800334 struct handler *handler;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930335 int i;
336 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800337
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800338 console->n_handlers = &__stop_handlers - &__start_handlers;
339 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800340
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930341 printf("%ld handler%s\n", console->n_handlers,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930342 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800343
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800344 for (i = 0; i < console->n_handlers; i++) {
345 handler = console->handlers[i];
346
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800347 rc = 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930348 if (handler->init) {
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800349 rc = handler->init(handler, console, config);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930350 }
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800351
352 handler->active = rc == 0;
353
354 printf(" %s [%sactive]\n", handler->name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930355 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800356 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800357}
358
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800359static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800360{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800361 struct handler *handler;
362 int i;
363
364 for (i = 0; i < console->n_handlers; i++) {
365 handler = console->handlers[i];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930366 if (handler->fini && handler->active) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800367 handler->fini(handler);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930368 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800369 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800370}
371
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800372static int get_current_time(struct timeval *tv)
373{
374 struct timespec t;
375 int rc;
376
377 /*
378 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
379 * local time changes. However, a struct timeval is more
380 * convenient for calculations, so convert to that.
381 */
382 rc = clock_gettime(CLOCK_MONOTONIC, &t);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930383 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800384 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930385 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800386
387 tv->tv_sec = t.tv_sec;
388 tv->tv_usec = t.tv_nsec / 1000;
389
390 return 0;
391}
392
Andrew Jefferya72711a2023-04-18 18:19:41 +0930393struct ringbuffer_consumer *
394console_ringbuffer_consumer_register(struct console *console,
395 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800396{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800397 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800398}
399
Jeremy Kerr55c97122017-02-07 17:06:46 +0800400struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930401 struct handler *handler,
402 poller_event_fn_t poller_fn,
403 poller_timeout_fn_t timeout_fn, int fd,
404 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800405{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800406 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930407 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800408
409 poller = malloc(sizeof(*poller));
410 poller->remove = false;
411 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800412 poller->event_fn = poller_fn;
413 poller->timeout_fn = timeout_fn;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800414 poller->data = data;
415
416 /* add one to our pollers array */
417 n = console->n_pollers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930418 /*
419 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
420 * pointer type.
421 */
422 /* NOLINTBEGIN(bugprone-sizeof-expression) */
423 console->pollers = reallocarray(console->pollers, console->n_pollers,
424 sizeof(*console->pollers));
425 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800426
427 console->pollers[n] = poller;
428
429 /* increase pollfds array too */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930430 console->pollfds =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930431 reallocarray(console->pollfds,
432 (MAX_INTERNAL_POLLFD + console->n_pollers),
433 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800434
435 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930436 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
437 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800438
439 console->pollfds[n].fd = fd;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930440 console->pollfds[n].events = (short)(events & 0x7fff);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800441
442 return poller;
443}
444
Andrew Jefferya72711a2023-04-18 18:19:41 +0930445void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800446{
447 int i;
448
449 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930450 for (i = 0; i < console->n_pollers; i++) {
451 if (console->pollers[i] == poller) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800452 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930453 }
454 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800455
456 assert(i < console->n_pollers);
457
458 console->n_pollers--;
459
Andrew Jeffery91b52172023-04-19 12:42:14 +0930460 /*
461 * Remove the item from the pollers array...
462 *
463 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
464 * pointer type.
465 */
466 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930467 memmove(&console->pollers[i], &console->pollers[i + 1],
468 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800469
Andrew Jeffery91b52172023-04-19 12:42:14 +0930470 console->pollers = reallocarray(console->pollers, console->n_pollers,
471 sizeof(*console->pollers));
472 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800473
474 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930475 memmove(&console->pollfds[i], &console->pollfds[i + 1],
476 sizeof(*console->pollfds) *
477 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800478
Andrew Jefferya72711a2023-04-18 18:19:41 +0930479 console->pollfds =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930480 reallocarray(console->pollfds,
481 (MAX_INTERNAL_POLLFD + console->n_pollers),
482 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800483
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800484 free(poller);
485}
486
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800487void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930488 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800489{
490 int i;
491
492 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930493 for (i = 0; i < console->n_pollers; i++) {
494 if (console->pollers[i] == poller) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800495 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930496 }
497 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800498
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930499 console->pollfds[i].events = (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800500}
501
Andrew Jefferyfd048322023-04-18 12:02:01 +0930502void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930503 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800504{
505 struct timeval now;
506 int rc;
507
508 rc = get_current_time(&now);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930509 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800510 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930511 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800512
513 timeradd(&now, tv, &poller->timeout);
514}
515
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930516static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800517{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930518 struct timeval *earliest;
519 struct timeval interval;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800520 struct poller *poller;
521 int i;
522
523 earliest = NULL;
524
525 for (i = 0; i < console->n_pollers; i++) {
526 poller = console->pollers[i];
527
528 if (poller->timeout_fn && timerisset(&poller->timeout) &&
529 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930530 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800531 // poller is buffering data and needs the poll
532 // function to timeout.
533 earliest = &poller->timeout;
534 }
535 }
536
537 if (earliest) {
538 if (timercmp(earliest, cur_time, >)) {
539 /* recalculate the timeout period, time period has
540 * not elapsed */
541 timersub(earliest, cur_time, &interval);
542 return ((interval.tv_sec * 1000) +
543 (interval.tv_usec / 1000));
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930544 } /* return from poll immediately */
545 return 0;
546
547 } /* poll indefinitely */
548 return -1;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800549}
550
551static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800552{
553 struct poller *poller;
554 struct pollfd *pollfd;
555 enum poller_ret prc;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930556 int i;
557 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800558
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800559 rc = 0;
560
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800561 /*
562 * Process poll events by iterating through the pollers and pollfds
563 * in-step, calling any pollers that we've found revents for.
564 */
565 for (i = 0; i < console->n_pollers; i++) {
566 poller = console->pollers[i];
567 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800568 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800569
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800570 /* process pending events... */
571 if (pollfd->revents) {
572 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930573 poller->data);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930574 if (prc == POLLER_EXIT) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800575 rc = -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930576 } else if (prc == POLLER_REMOVE) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800577 poller->remove = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930578 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800579 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800580
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800581 if ((prc == POLLER_OK) && poller->timeout_fn &&
582 timerisset(&poller->timeout) &&
583 timercmp(&poller->timeout, cur_time, <=)) {
584 /* One of the ringbuffer consumers is buffering the
585 data stream. The amount of idle time the consumer
586 desired has expired. Process the buffered data for
587 transmission. */
588 timerclear(&poller->timeout);
589 prc = poller->timeout_fn(poller->handler, poller->data);
590 if (prc == POLLER_EXIT) {
591 rc = -1;
592 } else if (prc == POLLER_REMOVE) {
593 poller->remove = true;
594 }
595 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800596 }
597
598 /**
599 * Process deferred removals; restarting each time we unregister, as
600 * the array will have changed
601 */
602 for (;;) {
603 bool removed = false;
604
605 for (i = 0; i < console->n_pollers; i++) {
606 poller = console->pollers[i];
607 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800608 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800609 removed = true;
610 break;
611 }
612 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930613 if (!removed) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800614 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930615 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800616 }
617
618 return rc;
619}
620
Jeremy Kerr769cee12016-03-15 17:53:56 +0800621static void sighandler(int signal)
622{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930623 if (signal == SIGINT) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800624 sigint = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930625 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800626}
627
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800628int run_console(struct console *console)
629{
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930630 sighandler_t sighandler_save = signal(SIGINT, sighandler);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800631 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930632 long timeout;
633 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800634
635 rc = 0;
636
Jeremy Kerrd831f962016-01-29 17:18:01 +0800637 for (;;) {
638 uint8_t buf[4096];
639
Jeremy Kerr17641452017-02-07 22:09:13 +0800640 BUILD_ASSERT(sizeof(buf) <= buffer_size);
641
Jeremy Kerr769cee12016-03-15 17:53:56 +0800642 if (sigint) {
643 fprintf(stderr, "Received interrupt, exiting\n");
644 break;
645 }
646
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800647 rc = get_current_time(&tv);
648 if (rc) {
649 warn("Failed to read current time");
650 break;
651 }
652
653 timeout = get_poll_timeout(console, &tv);
654
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800655 rc = poll(console->pollfds,
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930656 console->n_pollers + MAX_INTERNAL_POLLFD,
657 (int)timeout);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800658
Jeremy Kerrd831f962016-01-29 17:18:01 +0800659 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800660 if (errno == EINTR) {
661 continue;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800662 }
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930663 warn("poll error");
664 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800665 }
666
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800667 /* process internal fd first */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800668 if (console->pollfds[console->n_pollers].revents) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800669 rc = read(console->tty_fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800670 if (rc <= 0) {
671 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800672 rc = -1;
673 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800674 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800675 rc = ringbuffer_queue(console->rb, buf, rc);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930676 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800677 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930678 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800679 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800680
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800681 if (console->pollfds[console->n_pollers + 1].revents) {
682 sd_bus_process(console->bus, NULL);
683 }
684
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800685 /* ... and then the pollers */
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800686 rc = call_pollers(console, &tv);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930687 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800688 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930689 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800690 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800691
692 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800693 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800694
695 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800696}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800697static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930698 { "config", required_argument, 0, 'c' },
699 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800700};
701
702int main(int argc, char **argv)
703{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800704 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500705 const char *config_tty_kname = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800706 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800707 struct config *config;
708 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800709
Jeremy Kerrd831f962016-01-29 17:18:01 +0800710 for (;;) {
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930711 int c;
712 int idx;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800713
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800714 c = getopt_long(argc, argv, "c:", options, &idx);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930715 if (c == -1) {
Jeremy Kerrd831f962016-01-29 17:18:01 +0800716 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930717 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800718
719 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800720 case 'c':
721 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800722 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800723 case 'h':
724 case '?':
725 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800726 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800727 }
728 }
729
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930730 if (optind < argc) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030731 config_tty_kname = argv[optind];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930732 }
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500733
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800734 console = malloc(sizeof(struct console));
735 memset(console, 0, sizeof(*console));
Andrew Jefferya72711a2023-04-18 18:19:41 +0930736 console->pollfds =
737 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Jeremy Kerrf733c852017-02-07 18:40:10 +0800738 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800739
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800740 config = config_init(config_filename);
741 if (!config) {
742 warnx("Can't read configuration, exiting.");
Andrew Jeffery29c59c42023-05-03 19:33:07 +0930743 rc = -1;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800744 goto out_free;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800745 }
746
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500747 if (set_socket_info(console, config)) {
Andrew Jeffery29c59c42023-05-03 19:33:07 +0930748 rc = -1;
749 goto out_config_fini;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500750 }
751
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930752 rc = tty_init(console, config, config_tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930753 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800754 goto out_config_fini;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930755 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800756
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800757 dbus_init(console, config);
758
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800759 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800760
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800761 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800762
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800763 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800764
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800765out_config_fini:
766 config_fini(config);
767
Jeremy Kerr957818b2016-03-08 14:35:15 +0800768out_free:
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800769 free(console->pollers);
770 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800771 free(console->tty_sysfs_devnode);
772 free(console->tty_dev);
773 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800774
775 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
776}