blob: 4f894a4a090027668e5e7aba6c87d0f2877a7390 [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
Andrew Jeffery30ea6382023-05-03 16:56:10 +093042#define DEV_PTS_PATH "/dev/pts"
43
Jeremy Kerrf733c852017-02-07 18:40:10 +080044/* size of the shared backlog ringbuffer */
Andrew Jeffery5db8c792023-04-18 21:48:24 +093045const size_t buffer_size = 128ul * 1024ul;
Jeremy Kerrf733c852017-02-07 18:40:10 +080046
Jeremy Kerr769cee12016-03-15 17:53:56 +080047/* state shared with the signal handler */
48static bool sigint;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080049
Jeremy Kerrd831f962016-01-29 17:18:01 +080050static void usage(const char *progname)
51{
52 fprintf(stderr,
Andrew Jefferya72711a2023-04-18 18:19:41 +093053 "usage: %s [options] <DEVICE>\n"
54 "\n"
55 "Options:\n"
Andrew Jeffery954be0f2023-05-03 21:01:59 +093056 " --config <FILE>\tUse FILE for configuration\n"
57 " --console-id <NAME>\tUse NAME in the UNIX domain socket address\n"
Andrew Jefferya72711a2023-04-18 18:19:41 +093058 "",
Jeremy Kerrd831f962016-01-29 17:18:01 +080059 progname);
60}
61
Andrew Jeffery30ea6382023-05-03 16:56:10 +093062/* populates console->tty.dev and console->tty.sysfs_devnode, using the tty kernel name */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080063static int tty_find_device(struct console *console)
Jeremy Kerr17217842016-01-29 18:44:21 +080064{
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +093065 char *tty_class_device_link = NULL;
66 char *tty_path_input_real = NULL;
67 char *tty_device_tty_dir = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +093068 char *tty_vuart_lpc_addr = NULL;
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +093069 char *tty_device_reldir = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +093070 char *tty_sysfs_devnode = NULL;
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +093071 char *tty_kname_real = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +093072 char *tty_path_input = NULL;
Jeremy Kerr17217842016-01-29 18:44:21 +080073 int rc;
74
Andrew Jeffery30ea6382023-05-03 16:56:10 +093075 console->tty.type = TTY_DEVICE_UNDEFINED;
76
77 assert(console->tty.kname);
78 if (!strlen(console->tty.kname)) {
79 warnx("TTY kname must not be empty");
80 rc = -1;
81 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093082 }
Jeremy Kerr17217842016-01-29 18:44:21 +080083
Andrew Jeffery30ea6382023-05-03 16:56:10 +093084 if (console->tty.kname[0] == '/') {
85 tty_path_input = strdup(console->tty.kname);
86 if (!tty_path_input) {
87 rc = -1;
88 goto out_free;
89 }
90 } else {
91 rc = asprintf(&tty_path_input, "/dev/%s", console->tty.kname);
92 if (rc < 0) {
93 goto out_free;
94 }
95 }
96
97 /* udev may rename the tty name with a symbol link, try to resolve */
Yi Li45ad7672016-10-18 23:21:19 +080098 tty_path_input_real = realpath(tty_path_input, NULL);
99 if (!tty_path_input_real) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930100 warn("Can't find realpath for %s", tty_path_input);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930101 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800102 goto out_free;
103 }
104
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930105 /*
106 * Allow hooking obmc-console-server up to PTYs for testing
107 *
108 * https://amboar.github.io/notes/2023/05/02/testing-obmc-console-with-socat.html
109 */
110 if (!strncmp(DEV_PTS_PATH, tty_path_input_real, strlen(DEV_PTS_PATH))) {
111 console->tty.type = TTY_DEVICE_PTY;
112 console->tty.dev = strdup(console->tty.kname);
113 rc = console->tty.dev ? 0 : -1;
114 goto out_free;
115 }
116
Yi Li45ad7672016-10-18 23:21:19 +0800117 tty_kname_real = basename(tty_path_input_real);
118 if (!tty_kname_real) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930119 warn("Can't find real name for %s", console->tty.kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930120 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800121 goto out_free;
122 }
123
Andrew Jefferya72711a2023-04-18 18:19:41 +0930124 rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s",
125 tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930126 if (rc < 0) {
Yi Li45ad7672016-10-18 23:21:19 +0800127 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930128 }
Yi Li45ad7672016-10-18 23:21:19 +0800129
Jeremy Kerr17217842016-01-29 18:44:21 +0800130 tty_device_tty_dir = realpath(tty_class_device_link, NULL);
Yi Li45ad7672016-10-18 23:21:19 +0800131 if (!tty_device_tty_dir) {
132 warn("Can't query sysfs for device %s", tty_kname_real);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930133 rc = -1;
Jeremy Kerr17217842016-01-29 18:44:21 +0800134 goto out_free;
135 }
136
137 rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930138 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800139 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930140 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800141
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930142 tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
143 if (!tty_sysfs_devnode) {
Yi Li45ad7672016-10-18 23:21:19 +0800144 warn("Can't find parent device for %s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930145 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800146
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930147 rc = asprintf(&console->tty.dev, "/dev/%s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930148 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800149 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930150 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800151
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930152 /* Arbitrarily pick an attribute to differentiate UART vs VUART */
Andrew Jeffery7c02ae12023-06-05 18:45:38 +0930153 rc = asprintf(&tty_vuart_lpc_addr, "%s/lpc_address", tty_sysfs_devnode);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930154 if (rc < 0) {
155 goto out_free;
156 }
157
158 rc = access(tty_vuart_lpc_addr, F_OK);
159 console->tty.type = (!rc) ? TTY_DEVICE_VUART : TTY_DEVICE_UART;
160
Jeremy Kerr17217842016-01-29 18:44:21 +0800161 rc = 0;
162
163out_free:
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930164 free(tty_vuart_lpc_addr);
Jeremy Kerr17217842016-01-29 18:44:21 +0800165 free(tty_class_device_link);
166 free(tty_device_tty_dir);
167 free(tty_device_reldir);
Yi Li45ad7672016-10-18 23:21:19 +0800168 free(tty_path_input);
169 free(tty_path_input_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800170 return rc;
171}
172
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800173static int tty_set_sysfs_attr(struct console *console, const char *name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930174 int value)
Jeremy Kerr957818b2016-03-08 14:35:15 +0800175{
176 char *path;
177 FILE *fp;
178 int rc;
179
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930180 assert(console->tty.type == TTY_DEVICE_VUART);
181
182 if (!console->tty.vuart.sysfs_devnode) {
183 return -1;
184 }
185
186 rc = asprintf(&path, "%s/%s", console->tty.vuart.sysfs_devnode, name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930187 if (rc < 0) {
Jeremy Kerr957818b2016-03-08 14:35:15 +0800188 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930189 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800190
191 fp = fopen(path, "w");
192 if (!fp) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930193 warn("Can't access attribute %s on device %s", name,
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930194 console->tty.kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800195 rc = -1;
196 goto out_free;
197 }
198 setvbuf(fp, NULL, _IONBF, 0);
199
200 rc = fprintf(fp, "0x%x", value);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930201 if (rc < 0) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930202 warn("Error writing to %s attribute of device %s", name,
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930203 console->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930204 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800205 fclose(fp);
206
Jeremy Kerr957818b2016-03-08 14:35:15 +0800207out_free:
208 free(path);
209 return rc;
210}
211
Jeremy Kerrd831f962016-01-29 17:18:01 +0800212/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700213 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800214 */
Ninad Palsulee258e512023-04-26 22:17:57 -0500215void tty_init_termios(struct console *console)
Jeremy Kerr54e95692016-03-24 17:07:56 +0800216{
217 struct termios termios;
218 int rc;
219
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930220 rc = tcgetattr(console->tty.fd, &termios);
Jeremy Kerr54e95692016-03-24 17:07:56 +0800221 if (rc) {
222 warn("Can't read tty termios");
223 return;
224 }
225
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930226 if (console->tty.type == TTY_DEVICE_UART && console->tty.uart.baud) {
227 if (cfsetspeed(&termios, console->tty.uart.baud) < 0) {
228 warn("Couldn't set speeds for %s", console->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930229 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700230 }
231
232 /* Set console to raw mode: we don't want any processing to occur on
233 * the underlying terminal input/output.
234 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800235 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700236
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930237 rc = tcsetattr(console->tty.fd, TCSANOW, &termios);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930238 if (rc) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930239 warn("Can't set terminal options for %s", console->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930240 }
Jeremy Kerr54e95692016-03-24 17:07:56 +0800241}
242
243/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800244 * Open and initialise the serial device
245 */
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930246static void tty_init_vuart_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800247{
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930248 assert(console->tty.type == TTY_DEVICE_VUART);
249
250 if (console->tty.vuart.sirq) {
251 tty_set_sysfs_attr(console, "sirq", console->tty.vuart.sirq);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930252 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800253
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930254 if (console->tty.vuart.lpc_addr) {
255 tty_set_sysfs_attr(console, "lpc_address",
256 console->tty.vuart.lpc_addr);
257 }
258}
259
260static int tty_init_io(struct console *console)
261{
262 console->tty.fd = open(console->tty.dev, O_RDWR);
263 if (console->tty.fd <= 0) {
264 warn("Can't open tty %s", console->tty.dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800265 return -1;
266 }
267
268 /* Disable character delay. We may want to later enable this when
269 * we detect larger amounts of data
270 */
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930271 fcntl(console->tty.fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800272
Jeremy Kerr54e95692016-03-24 17:07:56 +0800273 tty_init_termios(console);
274
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930275 console->pollfds[console->n_pollers].fd = console->tty.fd;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800276 console->pollfds[console->n_pollers].events = POLLIN;
277
Jeremy Kerrd831f962016-01-29 17:18:01 +0800278 return 0;
279}
280
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930281static int tty_init_vuart(struct console *console, struct config *config)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800282{
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930283 unsigned long parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800284 const char *val;
285 char *endp;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930286
287 assert(console->tty.type == TTY_DEVICE_VUART);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800288
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800289 val = config_get_value(config, "lpc-address");
290 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930291 errno = 0;
292 parsed = strtoul(val, &endp, 0);
293 if (parsed == ULONG_MAX && errno == ERANGE) {
294 warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'",
295 val);
296 return -1;
297 }
298
299 if (parsed > UINT16_MAX) {
300 warn("Invalid LPC address '%s'", val);
301 return -1;
302 }
303
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930304 console->tty.vuart.lpc_addr = (uint16_t)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800305 if (endp == optarg) {
306 warn("Invalid LPC address: '%s'", val);
307 return -1;
308 }
309 }
310
311 val = config_get_value(config, "sirq");
312 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930313 errno = 0;
314 parsed = strtoul(val, &endp, 0);
315 if (parsed == ULONG_MAX && errno == ERANGE) {
316 warn("Cannot interpret 'sirq' value as an unsigned long: '%s'",
317 val);
318 }
319
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930320 if (parsed > 16) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930321 warn("Invalid LPC SERIRQ: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930322 }
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930323
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930324 console->tty.vuart.sirq = (int)parsed;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930325 if (endp == optarg) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800326 warn("Invalid sirq: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930327 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800328 }
329
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930330 return 0;
331}
332
333static int tty_init(struct console *console, struct config *config,
334 const char *tty_arg)
335{
336 const char *val;
337 int rc;
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700338
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930339 if (tty_arg) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930340 console->tty.kname = tty_arg;
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930341 } else if ((val = config_get_value(config, "upstream-tty"))) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930342 console->tty.kname = val;
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930343 } else {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800344 warnx("Error: No TTY device specified");
345 return -1;
346 }
347
348 rc = tty_find_device(console);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930349 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800350 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930351 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800352
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930353 switch (console->tty.type) {
354 case TTY_DEVICE_VUART:
355 rc = tty_init_vuart(console, config);
356 if (rc) {
357 return rc;
358 }
359
360 tty_init_vuart_io(console);
361 break;
362 case TTY_DEVICE_UART:
363 val = config_get_value(config, "baud");
364 if (val) {
365 if (config_parse_baud(&console->tty.uart.baud, val)) {
366 warnx("Invalid baud rate: '%s'", val);
367 }
368 }
369 break;
370 case TTY_DEVICE_PTY:
371 break;
372 case TTY_DEVICE_UNDEFINED:
373 default:
374 warnx("Cannot configure unrecognised TTY device");
375 return -1;
376 }
377
378 return tty_init_io(console);
379}
380
381static void tty_fini(struct console *console)
382{
383 if (console->tty.type == TTY_DEVICE_VUART) {
384 free(console->tty.vuart.sysfs_devnode);
385 }
386 free(console->tty.dev);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800387}
388
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800389int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800390{
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930391 return write_buf_to_fd(console->tty.fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800392}
393
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500394/* Prepare a socket name */
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930395static int set_socket_info(struct console *console, struct config *config,
396 const char *console_id)
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500397{
398 ssize_t len;
399
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500400 /* Get console id */
401 console->console_id = config_resolve_console_id(config, console_id);
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930402
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500403 /* Get the socket name/path */
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500404 len = console_socket_path(console->socket_name, console->console_id);
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500405 if (len < 0) {
406 warn("Failed to set socket path: %s", strerror(errno));
407 return EXIT_FAILURE;
408 }
409
410 /* Socket name is not a null terminated string hence save the length */
411 console->socket_name_len = len;
412
413 return 0;
414}
415
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800416static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800417{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930418 /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
419 extern struct handler *__start_handlers;
420 extern struct handler *__stop_handlers;
421 /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800422 struct handler *handler;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930423 int i;
424 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800425
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800426 console->n_handlers = &__stop_handlers - &__start_handlers;
427 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800428
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930429 printf("%ld handler%s\n", console->n_handlers,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930430 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800431
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800432 for (i = 0; i < console->n_handlers; i++) {
433 handler = console->handlers[i];
434
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800435 rc = 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930436 if (handler->init) {
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800437 rc = handler->init(handler, console, config);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930438 }
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800439
440 handler->active = rc == 0;
441
442 printf(" %s [%sactive]\n", handler->name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930443 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800444 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800445}
446
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800447static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800448{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800449 struct handler *handler;
450 int i;
451
452 for (i = 0; i < console->n_handlers; i++) {
453 handler = console->handlers[i];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930454 if (handler->fini && handler->active) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800455 handler->fini(handler);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930456 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800457 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800458}
459
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800460static int get_current_time(struct timeval *tv)
461{
462 struct timespec t;
463 int rc;
464
465 /*
466 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
467 * local time changes. However, a struct timeval is more
468 * convenient for calculations, so convert to that.
469 */
470 rc = clock_gettime(CLOCK_MONOTONIC, &t);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930471 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800472 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930473 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800474
475 tv->tv_sec = t.tv_sec;
476 tv->tv_usec = t.tv_nsec / 1000;
477
478 return 0;
479}
480
Andrew Jefferya72711a2023-04-18 18:19:41 +0930481struct ringbuffer_consumer *
482console_ringbuffer_consumer_register(struct console *console,
483 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800484{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800485 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800486}
487
Jeremy Kerr55c97122017-02-07 17:06:46 +0800488struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930489 struct handler *handler,
490 poller_event_fn_t poller_fn,
491 poller_timeout_fn_t timeout_fn, int fd,
492 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800493{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800494 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930495 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800496
497 poller = malloc(sizeof(*poller));
498 poller->remove = false;
499 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800500 poller->event_fn = poller_fn;
501 poller->timeout_fn = timeout_fn;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800502 poller->data = data;
503
504 /* add one to our pollers array */
505 n = console->n_pollers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930506 /*
507 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
508 * pointer type.
509 */
510 /* NOLINTBEGIN(bugprone-sizeof-expression) */
511 console->pollers = reallocarray(console->pollers, console->n_pollers,
512 sizeof(*console->pollers));
513 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800514
515 console->pollers[n] = poller;
516
517 /* increase pollfds array too */
Andrew Jeffery4e44c792023-05-12 15:57:46 +0930518 console->pollfds = reallocarray(
519 console->pollfds, (MAX_INTERNAL_POLLFD + console->n_pollers),
520 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800521
522 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930523 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
524 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800525
526 console->pollfds[n].fd = fd;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930527 console->pollfds[n].events = (short)(events & 0x7fff);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800528
529 return poller;
530}
531
Andrew Jefferya72711a2023-04-18 18:19:41 +0930532void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800533{
534 int i;
535
536 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930537 for (i = 0; i < console->n_pollers; i++) {
538 if (console->pollers[i] == poller) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800539 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930540 }
541 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800542
543 assert(i < console->n_pollers);
544
545 console->n_pollers--;
546
Andrew Jeffery91b52172023-04-19 12:42:14 +0930547 /*
548 * Remove the item from the pollers array...
549 *
550 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
551 * pointer type.
552 */
553 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930554 memmove(&console->pollers[i], &console->pollers[i + 1],
555 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800556
Andrew Jeffery91b52172023-04-19 12:42:14 +0930557 console->pollers = reallocarray(console->pollers, console->n_pollers,
558 sizeof(*console->pollers));
559 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800560
561 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930562 memmove(&console->pollfds[i], &console->pollfds[i + 1],
563 sizeof(*console->pollfds) *
564 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800565
Andrew Jeffery4e44c792023-05-12 15:57:46 +0930566 console->pollfds = reallocarray(
567 console->pollfds, (MAX_INTERNAL_POLLFD + console->n_pollers),
568 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800569
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800570 free(poller);
571}
572
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800573void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930574 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800575{
576 int i;
577
578 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930579 for (i = 0; i < console->n_pollers; i++) {
580 if (console->pollers[i] == poller) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800581 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930582 }
583 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800584
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930585 console->pollfds[i].events = (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800586}
587
Andrew Jefferyfd048322023-04-18 12:02:01 +0930588void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930589 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800590{
591 struct timeval now;
592 int rc;
593
594 rc = get_current_time(&now);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930595 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800596 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930597 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800598
599 timeradd(&now, tv, &poller->timeout);
600}
601
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930602static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800603{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930604 struct timeval *earliest;
605 struct timeval interval;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800606 struct poller *poller;
607 int i;
608
609 earliest = NULL;
610
611 for (i = 0; i < console->n_pollers; i++) {
612 poller = console->pollers[i];
613
614 if (poller->timeout_fn && timerisset(&poller->timeout) &&
615 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930616 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800617 // poller is buffering data and needs the poll
618 // function to timeout.
619 earliest = &poller->timeout;
620 }
621 }
622
623 if (earliest) {
624 if (timercmp(earliest, cur_time, >)) {
625 /* recalculate the timeout period, time period has
626 * not elapsed */
627 timersub(earliest, cur_time, &interval);
628 return ((interval.tv_sec * 1000) +
629 (interval.tv_usec / 1000));
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930630 } /* return from poll immediately */
631 return 0;
632
633 } /* poll indefinitely */
634 return -1;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800635}
636
637static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800638{
639 struct poller *poller;
640 struct pollfd *pollfd;
641 enum poller_ret prc;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930642 int i;
643 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800644
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800645 rc = 0;
646
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800647 /*
648 * Process poll events by iterating through the pollers and pollfds
649 * in-step, calling any pollers that we've found revents for.
650 */
651 for (i = 0; i < console->n_pollers; i++) {
652 poller = console->pollers[i];
653 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800654 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800655
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800656 /* process pending events... */
657 if (pollfd->revents) {
658 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930659 poller->data);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930660 if (prc == POLLER_EXIT) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800661 rc = -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930662 } else if (prc == POLLER_REMOVE) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800663 poller->remove = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930664 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800665 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800666
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800667 if ((prc == POLLER_OK) && poller->timeout_fn &&
668 timerisset(&poller->timeout) &&
669 timercmp(&poller->timeout, cur_time, <=)) {
670 /* One of the ringbuffer consumers is buffering the
671 data stream. The amount of idle time the consumer
672 desired has expired. Process the buffered data for
673 transmission. */
674 timerclear(&poller->timeout);
675 prc = poller->timeout_fn(poller->handler, poller->data);
676 if (prc == POLLER_EXIT) {
677 rc = -1;
678 } else if (prc == POLLER_REMOVE) {
679 poller->remove = true;
680 }
681 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800682 }
683
684 /**
685 * Process deferred removals; restarting each time we unregister, as
686 * the array will have changed
687 */
688 for (;;) {
689 bool removed = false;
690
691 for (i = 0; i < console->n_pollers; i++) {
692 poller = console->pollers[i];
693 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800694 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800695 removed = true;
696 break;
697 }
698 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930699 if (!removed) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800700 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930701 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800702 }
703
704 return rc;
705}
706
Jeremy Kerr769cee12016-03-15 17:53:56 +0800707static void sighandler(int signal)
708{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930709 if (signal == SIGINT) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800710 sigint = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930711 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800712}
713
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800714int run_console(struct console *console)
715{
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930716 sighandler_t sighandler_save = signal(SIGINT, sighandler);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800717 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930718 long timeout;
719 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800720
721 rc = 0;
722
Jeremy Kerrd831f962016-01-29 17:18:01 +0800723 for (;;) {
724 uint8_t buf[4096];
725
Jeremy Kerr17641452017-02-07 22:09:13 +0800726 BUILD_ASSERT(sizeof(buf) <= buffer_size);
727
Jeremy Kerr769cee12016-03-15 17:53:56 +0800728 if (sigint) {
729 fprintf(stderr, "Received interrupt, exiting\n");
730 break;
731 }
732
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800733 rc = get_current_time(&tv);
734 if (rc) {
735 warn("Failed to read current time");
736 break;
737 }
738
739 timeout = get_poll_timeout(console, &tv);
740
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800741 rc = poll(console->pollfds,
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930742 console->n_pollers + MAX_INTERNAL_POLLFD,
743 (int)timeout);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800744
Jeremy Kerrd831f962016-01-29 17:18:01 +0800745 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800746 if (errno == EINTR) {
747 continue;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800748 }
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930749 warn("poll error");
750 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800751 }
752
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800753 /* process internal fd first */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800754 if (console->pollfds[console->n_pollers].revents) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930755 rc = read(console->tty.fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800756 if (rc <= 0) {
757 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800758 rc = -1;
759 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800760 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800761 rc = ringbuffer_queue(console->rb, buf, rc);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930762 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800763 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930764 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800765 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800766
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800767 if (console->pollfds[console->n_pollers + 1].revents) {
768 sd_bus_process(console->bus, NULL);
769 }
770
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800771 /* ... and then the pollers */
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800772 rc = call_pollers(console, &tv);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930773 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800774 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930775 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800776 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800777
778 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800779 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800780
781 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800782}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800783static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930784 { "config", required_argument, 0, 'c' },
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930785 { "console-id", required_argument, 0, 'i' },
Andrew Jefferya72711a2023-04-18 18:19:41 +0930786 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800787};
788
789int main(int argc, char **argv)
790{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800791 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500792 const char *config_tty_kname = NULL;
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930793 const char *console_id = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800794 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800795 struct config *config;
796 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800797
Jeremy Kerrd831f962016-01-29 17:18:01 +0800798 for (;;) {
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930799 int c;
800 int idx;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800801
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930802 c = getopt_long(argc, argv, "c:i:", options, &idx);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930803 if (c == -1) {
Jeremy Kerrd831f962016-01-29 17:18:01 +0800804 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930805 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800806
807 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800808 case 'c':
809 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800810 break;
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930811 case 'i':
812 console_id = optarg;
813 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800814 case 'h':
815 case '?':
816 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800817 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800818 }
819 }
820
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930821 if (optind < argc) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030822 config_tty_kname = argv[optind];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930823 }
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500824
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800825 console = malloc(sizeof(struct console));
826 memset(console, 0, sizeof(*console));
Andrew Jefferya72711a2023-04-18 18:19:41 +0930827 console->pollfds =
828 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Jeremy Kerrf733c852017-02-07 18:40:10 +0800829 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800830
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800831 config = config_init(config_filename);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800832
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930833 if (set_socket_info(console, config, console_id)) {
Andrew Jeffery29c59c42023-05-03 19:33:07 +0930834 rc = -1;
835 goto out_config_fini;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500836 }
837
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930838 rc = tty_init(console, config, config_tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930839 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800840 goto out_config_fini;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930841 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800842
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800843 dbus_init(console, config);
844
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800845 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800846
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800847 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800848
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800849 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800850
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930851 tty_fini(console);
852
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800853out_config_fini:
854 config_fini(config);
855
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800856 free(console->pollers);
857 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800858 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800859
860 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
861}