blob: f52c719d8d4b39ce090a4400eb761aa8a3020643 [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{
62 char *tty_class_device_link;
63 char *tty_device_tty_dir;
64 char *tty_device_reldir;
Yi Li45ad7672016-10-18 23:21:19 +080065 char *tty_path_input;
66 char *tty_path_input_real;
67 char *tty_kname_real;
Jeremy Kerr17217842016-01-29 18:44:21 +080068 int rc;
69
Jeremy Kerr17217842016-01-29 18:44:21 +080070 tty_class_device_link = NULL;
71 tty_device_tty_dir = NULL;
72 tty_device_reldir = NULL;
Yi Li45ad7672016-10-18 23:21:19 +080073 tty_path_input = NULL;
74 tty_path_input_real = NULL;
75 tty_kname_real = NULL;
Jeremy Kerr17217842016-01-29 18:44:21 +080076
Yi Li45ad7672016-10-18 23:21:19 +080077 /* udev may rename the tty name with a symbol link, try to resolve */
78 rc = asprintf(&tty_path_input, "/dev/%s", console->tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093079 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +080080 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093081 }
Jeremy Kerr17217842016-01-29 18:44:21 +080082
Yi Li45ad7672016-10-18 23:21:19 +080083 tty_path_input_real = realpath(tty_path_input, NULL);
84 if (!tty_path_input_real) {
85 warn("Can't find realpath 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
90 tty_kname_real = basename(tty_path_input_real);
91 if (!tty_kname_real) {
92 warn("Can't find real name for /dev/%s", console->tty_kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +093093 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +080094 goto out_free;
95 }
96
Andrew Jefferya72711a2023-04-18 18:19:41 +093097 rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s",
98 tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093099 if (rc < 0) {
Yi Li45ad7672016-10-18 23:21:19 +0800100 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930101 }
Yi Li45ad7672016-10-18 23:21:19 +0800102
Jeremy Kerr17217842016-01-29 18:44:21 +0800103 tty_device_tty_dir = realpath(tty_class_device_link, NULL);
Yi Li45ad7672016-10-18 23:21:19 +0800104 if (!tty_device_tty_dir) {
105 warn("Can't query sysfs for device %s", tty_kname_real);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930106 rc = -1;
Jeremy Kerr17217842016-01-29 18:44:21 +0800107 goto out_free;
108 }
109
110 rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930111 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800112 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930113 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800114
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800115 console->tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930116 if (!console->tty_sysfs_devnode) {
Yi Li45ad7672016-10-18 23:21:19 +0800117 warn("Can't find parent device for %s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930118 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800119
Yi Li45ad7672016-10-18 23:21:19 +0800120 rc = asprintf(&console->tty_dev, "/dev/%s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930121 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800122 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930123 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800124
125 rc = 0;
126
127out_free:
128 free(tty_class_device_link);
129 free(tty_device_tty_dir);
130 free(tty_device_reldir);
Yi Li45ad7672016-10-18 23:21:19 +0800131 free(tty_path_input);
132 free(tty_path_input_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800133 return rc;
134}
135
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800136static int tty_set_sysfs_attr(struct console *console, const char *name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930137 int value)
Jeremy Kerr957818b2016-03-08 14:35:15 +0800138{
139 char *path;
140 FILE *fp;
141 int rc;
142
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800143 rc = asprintf(&path, "%s/%s", console->tty_sysfs_devnode, name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930144 if (rc < 0) {
Jeremy Kerr957818b2016-03-08 14:35:15 +0800145 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930146 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800147
148 fp = fopen(path, "w");
149 if (!fp) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930150 warn("Can't access attribute %s on device %s", name,
151 console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800152 rc = -1;
153 goto out_free;
154 }
155 setvbuf(fp, NULL, _IONBF, 0);
156
157 rc = fprintf(fp, "0x%x", value);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930158 if (rc < 0) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930159 warn("Error writing to %s attribute of device %s", name,
160 console->tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930161 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800162 fclose(fp);
163
Jeremy Kerr957818b2016-03-08 14:35:15 +0800164out_free:
165 free(path);
166 return rc;
167}
168
Jeremy Kerrd831f962016-01-29 17:18:01 +0800169/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700170 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800171 */
Ninad Palsulee258e512023-04-26 22:17:57 -0500172void tty_init_termios(struct console *console)
Jeremy Kerr54e95692016-03-24 17:07:56 +0800173{
174 struct termios termios;
175 int rc;
176
177 rc = tcgetattr(console->tty_fd, &termios);
178 if (rc) {
179 warn("Can't read tty termios");
180 return;
181 }
182
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700183 if (console->tty_baud) {
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930184 if (cfsetspeed(&termios, console->tty_baud) < 0) {
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700185 warn("Couldn't set speeds for %s", console->tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930186 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700187 }
188
189 /* Set console to raw mode: we don't want any processing to occur on
190 * the underlying terminal input/output.
191 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800192 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700193
Jeremy Kerr54e95692016-03-24 17:07:56 +0800194 rc = tcsetattr(console->tty_fd, TCSANOW, &termios);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930195 if (rc) {
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700196 warn("Can't set terminal options for %s", console->tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930197 }
Jeremy Kerr54e95692016-03-24 17:07:56 +0800198}
199
200/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800201 * Open and initialise the serial device
202 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800203static int tty_init_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800204{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930205 if (console->tty_sirq) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800206 tty_set_sysfs_attr(console, "sirq", console->tty_sirq);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930207 }
208 if (console->tty_lpc_addr) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800209 tty_set_sysfs_attr(console, "lpc_address",
Andrew Jefferya72711a2023-04-18 18:19:41 +0930210 console->tty_lpc_addr);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930211 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800212
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800213 console->tty_fd = open(console->tty_dev, O_RDWR);
214 if (console->tty_fd <= 0) {
215 warn("Can't open tty %s", console->tty_dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800216 return -1;
217 }
218
219 /* Disable character delay. We may want to later enable this when
220 * we detect larger amounts of data
221 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800222 fcntl(console->tty_fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800223
Jeremy Kerr54e95692016-03-24 17:07:56 +0800224 tty_init_termios(console);
225
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800226 console->pollfds[console->n_pollers].fd = console->tty_fd;
227 console->pollfds[console->n_pollers].events = POLLIN;
228
Jeremy Kerrd831f962016-01-29 17:18:01 +0800229 return 0;
230}
231
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800232static int tty_init(struct console *console, struct config *config)
233{
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930234 unsigned long parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800235 const char *val;
236 char *endp;
237 int rc;
238
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800239 val = config_get_value(config, "lpc-address");
240 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930241 errno = 0;
242 parsed = strtoul(val, &endp, 0);
243 if (parsed == ULONG_MAX && errno == ERANGE) {
244 warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'",
245 val);
246 return -1;
247 }
248
249 if (parsed > UINT16_MAX) {
250 warn("Invalid LPC address '%s'", val);
251 return -1;
252 }
253
254 console->tty_lpc_addr = (uint16_t)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800255 if (endp == optarg) {
256 warn("Invalid LPC address: '%s'", val);
257 return -1;
258 }
259 }
260
261 val = config_get_value(config, "sirq");
262 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930263 errno = 0;
264 parsed = strtoul(val, &endp, 0);
265 if (parsed == ULONG_MAX && errno == ERANGE) {
266 warn("Cannot interpret 'sirq' value as an unsigned long: '%s'",
267 val);
268 }
269
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930270 if (parsed > 16) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930271 warn("Invalid LPC SERIRQ: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930272 }
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930273
274 console->tty_sirq = (int)parsed;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930275 if (endp == optarg) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800276 warn("Invalid sirq: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930277 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800278 }
279
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700280 val = config_get_value(config, "baud");
281 if (val) {
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930282 if (config_parse_baud(&console->tty_baud, val)) {
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700283 warnx("Invalid baud rate: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930284 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700285 }
286
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800287 if (!console->tty_kname) {
288 warnx("Error: No TTY device specified");
289 return -1;
290 }
291
292 rc = tty_find_device(console);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930293 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800294 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930295 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800296
297 rc = tty_init_io(console);
298 return rc;
299}
300
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800301int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800302{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800303 return write_buf_to_fd(console->tty_fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800304}
305
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500306/* Read console if from config and prepare a socket name */
307static int set_socket_info(struct console *console, struct config *config)
308{
309 ssize_t len;
310
311 console->console_id = config_get_value(config, "socket-id");
312 if (!console->console_id) {
313 warnx("Error: The socket-id is not set in the config file");
314 return EXIT_FAILURE;
315 }
316
317 /* Get the socket name/path */
318 len = console_socket_path(console->socket_name, console->console_id);
319 if (len < 0) {
320 warn("Failed to set socket path: %s", strerror(errno));
321 return EXIT_FAILURE;
322 }
323
324 /* Socket name is not a null terminated string hence save the length */
325 console->socket_name_len = len;
326
327 return 0;
328}
329
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800330static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800331{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930332 /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
333 extern struct handler *__start_handlers;
334 extern struct handler *__stop_handlers;
335 /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800336 struct handler *handler;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930337 int i;
338 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800339
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800340 console->n_handlers = &__stop_handlers - &__start_handlers;
341 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800342
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930343 printf("%ld handler%s\n", console->n_handlers,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930344 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800345
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800346 for (i = 0; i < console->n_handlers; i++) {
347 handler = console->handlers[i];
348
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800349 rc = 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930350 if (handler->init) {
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800351 rc = handler->init(handler, console, config);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930352 }
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800353
354 handler->active = rc == 0;
355
356 printf(" %s [%sactive]\n", handler->name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930357 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800358 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800359}
360
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800361static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800362{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800363 struct handler *handler;
364 int i;
365
366 for (i = 0; i < console->n_handlers; i++) {
367 handler = console->handlers[i];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930368 if (handler->fini && handler->active) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800369 handler->fini(handler);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930370 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800371 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800372}
373
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800374static int get_current_time(struct timeval *tv)
375{
376 struct timespec t;
377 int rc;
378
379 /*
380 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
381 * local time changes. However, a struct timeval is more
382 * convenient for calculations, so convert to that.
383 */
384 rc = clock_gettime(CLOCK_MONOTONIC, &t);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930385 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800386 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930387 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800388
389 tv->tv_sec = t.tv_sec;
390 tv->tv_usec = t.tv_nsec / 1000;
391
392 return 0;
393}
394
Andrew Jefferya72711a2023-04-18 18:19:41 +0930395struct ringbuffer_consumer *
396console_ringbuffer_consumer_register(struct console *console,
397 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800398{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800399 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800400}
401
Jeremy Kerr55c97122017-02-07 17:06:46 +0800402struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930403 struct handler *handler,
404 poller_event_fn_t poller_fn,
405 poller_timeout_fn_t timeout_fn, int fd,
406 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800407{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800408 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930409 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800410
411 poller = malloc(sizeof(*poller));
412 poller->remove = false;
413 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800414 poller->event_fn = poller_fn;
415 poller->timeout_fn = timeout_fn;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800416 poller->data = data;
417
418 /* add one to our pollers array */
419 n = console->n_pollers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930420 /*
421 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
422 * pointer type.
423 */
424 /* NOLINTBEGIN(bugprone-sizeof-expression) */
425 console->pollers = reallocarray(console->pollers, console->n_pollers,
426 sizeof(*console->pollers));
427 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800428
429 console->pollers[n] = poller;
430
431 /* increase pollfds array too */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930432 console->pollfds =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930433 reallocarray(console->pollfds,
434 (MAX_INTERNAL_POLLFD + console->n_pollers),
435 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800436
437 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930438 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
439 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800440
441 console->pollfds[n].fd = fd;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930442 console->pollfds[n].events = (short)(events & 0x7fff);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800443
444 return poller;
445}
446
Andrew Jefferya72711a2023-04-18 18:19:41 +0930447void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800448{
449 int i;
450
451 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930452 for (i = 0; i < console->n_pollers; i++) {
453 if (console->pollers[i] == poller) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800454 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930455 }
456 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800457
458 assert(i < console->n_pollers);
459
460 console->n_pollers--;
461
Andrew Jeffery91b52172023-04-19 12:42:14 +0930462 /*
463 * Remove the item from the pollers array...
464 *
465 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
466 * pointer type.
467 */
468 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930469 memmove(&console->pollers[i], &console->pollers[i + 1],
470 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800471
Andrew Jeffery91b52172023-04-19 12:42:14 +0930472 console->pollers = reallocarray(console->pollers, console->n_pollers,
473 sizeof(*console->pollers));
474 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800475
476 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930477 memmove(&console->pollfds[i], &console->pollfds[i + 1],
478 sizeof(*console->pollfds) *
479 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800480
Andrew Jefferya72711a2023-04-18 18:19:41 +0930481 console->pollfds =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930482 reallocarray(console->pollfds,
483 (MAX_INTERNAL_POLLFD + console->n_pollers),
484 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800485
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800486 free(poller);
487}
488
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800489void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930490 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800491{
492 int i;
493
494 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930495 for (i = 0; i < console->n_pollers; i++) {
496 if (console->pollers[i] == poller) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800497 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930498 }
499 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800500
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930501 console->pollfds[i].events = (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800502}
503
Andrew Jefferyfd048322023-04-18 12:02:01 +0930504void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930505 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800506{
507 struct timeval now;
508 int rc;
509
510 rc = get_current_time(&now);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930511 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800512 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930513 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800514
515 timeradd(&now, tv, &poller->timeout);
516}
517
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930518static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800519{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930520 struct timeval *earliest;
521 struct timeval interval;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800522 struct poller *poller;
523 int i;
524
525 earliest = NULL;
526
527 for (i = 0; i < console->n_pollers; i++) {
528 poller = console->pollers[i];
529
530 if (poller->timeout_fn && timerisset(&poller->timeout) &&
531 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930532 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800533 // poller is buffering data and needs the poll
534 // function to timeout.
535 earliest = &poller->timeout;
536 }
537 }
538
539 if (earliest) {
540 if (timercmp(earliest, cur_time, >)) {
541 /* recalculate the timeout period, time period has
542 * not elapsed */
543 timersub(earliest, cur_time, &interval);
544 return ((interval.tv_sec * 1000) +
545 (interval.tv_usec / 1000));
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930546 } /* return from poll immediately */
547 return 0;
548
549 } /* poll indefinitely */
550 return -1;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800551}
552
553static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800554{
555 struct poller *poller;
556 struct pollfd *pollfd;
557 enum poller_ret prc;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930558 int i;
559 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800560
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800561 rc = 0;
562
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800563 /*
564 * Process poll events by iterating through the pollers and pollfds
565 * in-step, calling any pollers that we've found revents for.
566 */
567 for (i = 0; i < console->n_pollers; i++) {
568 poller = console->pollers[i];
569 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800570 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800571
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800572 /* process pending events... */
573 if (pollfd->revents) {
574 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930575 poller->data);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930576 if (prc == POLLER_EXIT) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800577 rc = -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930578 } else if (prc == POLLER_REMOVE) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800579 poller->remove = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930580 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800581 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800582
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800583 if ((prc == POLLER_OK) && poller->timeout_fn &&
584 timerisset(&poller->timeout) &&
585 timercmp(&poller->timeout, cur_time, <=)) {
586 /* One of the ringbuffer consumers is buffering the
587 data stream. The amount of idle time the consumer
588 desired has expired. Process the buffered data for
589 transmission. */
590 timerclear(&poller->timeout);
591 prc = poller->timeout_fn(poller->handler, poller->data);
592 if (prc == POLLER_EXIT) {
593 rc = -1;
594 } else if (prc == POLLER_REMOVE) {
595 poller->remove = true;
596 }
597 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800598 }
599
600 /**
601 * Process deferred removals; restarting each time we unregister, as
602 * the array will have changed
603 */
604 for (;;) {
605 bool removed = false;
606
607 for (i = 0; i < console->n_pollers; i++) {
608 poller = console->pollers[i];
609 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800610 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800611 removed = true;
612 break;
613 }
614 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930615 if (!removed) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800616 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930617 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800618 }
619
620 return rc;
621}
622
Jeremy Kerr769cee12016-03-15 17:53:56 +0800623static void sighandler(int signal)
624{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930625 if (signal == SIGINT) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800626 sigint = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930627 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800628}
629
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800630int run_console(struct console *console)
631{
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930632 sighandler_t sighandler_save = signal(SIGINT, sighandler);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800633 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930634 long timeout;
635 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800636
637 rc = 0;
638
Jeremy Kerrd831f962016-01-29 17:18:01 +0800639 for (;;) {
640 uint8_t buf[4096];
641
Jeremy Kerr17641452017-02-07 22:09:13 +0800642 BUILD_ASSERT(sizeof(buf) <= buffer_size);
643
Jeremy Kerr769cee12016-03-15 17:53:56 +0800644 if (sigint) {
645 fprintf(stderr, "Received interrupt, exiting\n");
646 break;
647 }
648
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800649 rc = get_current_time(&tv);
650 if (rc) {
651 warn("Failed to read current time");
652 break;
653 }
654
655 timeout = get_poll_timeout(console, &tv);
656
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800657 rc = poll(console->pollfds,
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930658 console->n_pollers + MAX_INTERNAL_POLLFD,
659 (int)timeout);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800660
Jeremy Kerrd831f962016-01-29 17:18:01 +0800661 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800662 if (errno == EINTR) {
663 continue;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800664 }
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930665 warn("poll error");
666 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800667 }
668
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800669 /* process internal fd first */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800670 if (console->pollfds[console->n_pollers].revents) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800671 rc = read(console->tty_fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800672 if (rc <= 0) {
673 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800674 rc = -1;
675 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800676 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800677 rc = ringbuffer_queue(console->rb, buf, rc);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930678 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800679 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930680 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800681 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800682
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800683 if (console->pollfds[console->n_pollers + 1].revents) {
684 sd_bus_process(console->bus, NULL);
685 }
686
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800687 /* ... and then the pollers */
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800688 rc = call_pollers(console, &tv);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930689 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800690 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930691 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800692 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800693
694 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800695 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800696
697 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800698}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800699static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930700 { "config", required_argument, 0, 'c' },
701 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800702};
703
704int main(int argc, char **argv)
705{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800706 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500707 const char *config_tty_kname = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800708 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800709 struct config *config;
710 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800711
Jeremy Kerr957818b2016-03-08 14:35:15 +0800712 rc = -1;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800713
714 for (;;) {
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930715 int c;
716 int idx;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800717
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800718 c = getopt_long(argc, argv, "c:", options, &idx);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930719 if (c == -1) {
Jeremy Kerrd831f962016-01-29 17:18:01 +0800720 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930721 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800722
723 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800724 case 'c':
725 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800726 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800727 case 'h':
728 case '?':
729 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800730 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800731 }
732 }
733
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930734 if (optind < argc) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030735 config_tty_kname = argv[optind];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930736 }
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500737
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800738 console = malloc(sizeof(struct console));
739 memset(console, 0, sizeof(*console));
Andrew Jefferya72711a2023-04-18 18:19:41 +0930740 console->pollfds =
741 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Jeremy Kerrf733c852017-02-07 18:40:10 +0800742 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800743
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800744 config = config_init(config_filename);
745 if (!config) {
746 warnx("Can't read configuration, exiting.");
747 goto out_free;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800748 }
749
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930750 if (!config_tty_kname) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030751 config_tty_kname = config_get_value(config, "upstream-tty");
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930752 }
Andrew Jeffery91dde142020-02-12 22:46:27 +1030753
754 if (!config_tty_kname) {
755 warnx("No TTY device specified");
756 usage(argv[0]);
757 return EXIT_FAILURE;
758 }
759
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500760 if (set_socket_info(console, config)) {
761 return EXIT_FAILURE;
762 }
763
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500764 console->tty_kname = config_tty_kname;
765
Ninad Palsule4e718692023-04-26 21:18:35 -0500766 console->console_id = config_get_value(config, "socket-id");
767 if (!console->console_id) {
768 warnx("Error: The socket-id is not set in the config file");
769 return EXIT_FAILURE;
770 }
771
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800772 rc = tty_init(console, config);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930773 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800774 goto out_config_fini;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930775 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800776
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800777 dbus_init(console, config);
778
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800779 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800780
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800781 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800782
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800783 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800784
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800785out_config_fini:
786 config_fini(config);
787
Jeremy Kerr957818b2016-03-08 14:35:15 +0800788out_free:
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800789 free(console->pollers);
790 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800791 free(console->tty_sysfs_devnode);
792 free(console->tty_dev);
793 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800794
795 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
796}