blob: 3440fb27605b48d8338d05dd3e08c7f50329c766 [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"
56 " --config <FILE> Use FILE for configuration\n"
57 "",
Jeremy Kerrd831f962016-01-29 17:18:01 +080058 progname);
59}
60
Andrew Jeffery30ea6382023-05-03 16:56:10 +093061/* populates console->tty.dev and console->tty.sysfs_devnode, using the tty kernel name */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080062static int tty_find_device(struct console *console)
Jeremy Kerr17217842016-01-29 18:44:21 +080063{
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +093064 char *tty_class_device_link = NULL;
65 char *tty_path_input_real = NULL;
66 char *tty_device_tty_dir = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +093067 char *tty_vuart_lpc_addr = NULL;
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +093068 char *tty_device_reldir = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +093069 char *tty_sysfs_devnode = NULL;
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +093070 char *tty_kname_real = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +093071 char *tty_path_input = NULL;
Jeremy Kerr17217842016-01-29 18:44:21 +080072 int rc;
73
Andrew Jeffery30ea6382023-05-03 16:56:10 +093074 console->tty.type = TTY_DEVICE_UNDEFINED;
75
76 assert(console->tty.kname);
77 if (!strlen(console->tty.kname)) {
78 warnx("TTY kname must not be empty");
79 rc = -1;
80 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093081 }
Jeremy Kerr17217842016-01-29 18:44:21 +080082
Andrew Jeffery30ea6382023-05-03 16:56:10 +093083 if (console->tty.kname[0] == '/') {
84 tty_path_input = strdup(console->tty.kname);
85 if (!tty_path_input) {
86 rc = -1;
87 goto out_free;
88 }
89 } else {
90 rc = asprintf(&tty_path_input, "/dev/%s", console->tty.kname);
91 if (rc < 0) {
92 goto out_free;
93 }
94 }
95
96 /* udev may rename the tty name with a symbol link, try to resolve */
Yi Li45ad7672016-10-18 23:21:19 +080097 tty_path_input_real = realpath(tty_path_input, NULL);
98 if (!tty_path_input_real) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +093099 warn("Can't find realpath for %s", tty_path_input);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930100 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800101 goto out_free;
102 }
103
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930104 /*
105 * Allow hooking obmc-console-server up to PTYs for testing
106 *
107 * https://amboar.github.io/notes/2023/05/02/testing-obmc-console-with-socat.html
108 */
109 if (!strncmp(DEV_PTS_PATH, tty_path_input_real, strlen(DEV_PTS_PATH))) {
110 console->tty.type = TTY_DEVICE_PTY;
111 console->tty.dev = strdup(console->tty.kname);
112 rc = console->tty.dev ? 0 : -1;
113 goto out_free;
114 }
115
Yi Li45ad7672016-10-18 23:21:19 +0800116 tty_kname_real = basename(tty_path_input_real);
117 if (!tty_kname_real) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930118 warn("Can't find real name for %s", console->tty.kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930119 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800120 goto out_free;
121 }
122
Andrew Jefferya72711a2023-04-18 18:19:41 +0930123 rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s",
124 tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930125 if (rc < 0) {
Yi Li45ad7672016-10-18 23:21:19 +0800126 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930127 }
Yi Li45ad7672016-10-18 23:21:19 +0800128
Jeremy Kerr17217842016-01-29 18:44:21 +0800129 tty_device_tty_dir = realpath(tty_class_device_link, NULL);
Yi Li45ad7672016-10-18 23:21:19 +0800130 if (!tty_device_tty_dir) {
131 warn("Can't query sysfs for device %s", tty_kname_real);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930132 rc = -1;
Jeremy Kerr17217842016-01-29 18:44:21 +0800133 goto out_free;
134 }
135
136 rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930137 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800138 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930139 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800140
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930141 tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
142 if (!tty_sysfs_devnode) {
Yi Li45ad7672016-10-18 23:21:19 +0800143 warn("Can't find parent device for %s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930144 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800145
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930146 rc = asprintf(&console->tty.dev, "/dev/%s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930147 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800148 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930149 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800150
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930151 /* Arbitrarily pick an attribute to differentiate UART vs VUART */
152 rc = asprintf(&tty_vuart_lpc_addr, "%s/lpc_addr", tty_sysfs_devnode);
153 if (rc < 0) {
154 goto out_free;
155 }
156
157 rc = access(tty_vuart_lpc_addr, F_OK);
158 console->tty.type = (!rc) ? TTY_DEVICE_VUART : TTY_DEVICE_UART;
159
Jeremy Kerr17217842016-01-29 18:44:21 +0800160 rc = 0;
161
162out_free:
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930163 free(tty_vuart_lpc_addr);
Jeremy Kerr17217842016-01-29 18:44:21 +0800164 free(tty_class_device_link);
165 free(tty_device_tty_dir);
166 free(tty_device_reldir);
Yi Li45ad7672016-10-18 23:21:19 +0800167 free(tty_path_input);
168 free(tty_path_input_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800169 return rc;
170}
171
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800172static int tty_set_sysfs_attr(struct console *console, const char *name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930173 int value)
Jeremy Kerr957818b2016-03-08 14:35:15 +0800174{
175 char *path;
176 FILE *fp;
177 int rc;
178
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930179 assert(console->tty.type == TTY_DEVICE_VUART);
180
181 if (!console->tty.vuart.sysfs_devnode) {
182 return -1;
183 }
184
185 rc = asprintf(&path, "%s/%s", console->tty.vuart.sysfs_devnode, name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930186 if (rc < 0) {
Jeremy Kerr957818b2016-03-08 14:35:15 +0800187 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930188 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800189
190 fp = fopen(path, "w");
191 if (!fp) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930192 warn("Can't access attribute %s on device %s", name,
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930193 console->tty.kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800194 rc = -1;
195 goto out_free;
196 }
197 setvbuf(fp, NULL, _IONBF, 0);
198
199 rc = fprintf(fp, "0x%x", value);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930200 if (rc < 0) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930201 warn("Error writing to %s attribute of device %s", name,
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930202 console->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930203 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800204 fclose(fp);
205
Jeremy Kerr957818b2016-03-08 14:35:15 +0800206out_free:
207 free(path);
208 return rc;
209}
210
Jeremy Kerrd831f962016-01-29 17:18:01 +0800211/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700212 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800213 */
Ninad Palsulee258e512023-04-26 22:17:57 -0500214void tty_init_termios(struct console *console)
Jeremy Kerr54e95692016-03-24 17:07:56 +0800215{
216 struct termios termios;
217 int rc;
218
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930219 rc = tcgetattr(console->tty.fd, &termios);
Jeremy Kerr54e95692016-03-24 17:07:56 +0800220 if (rc) {
221 warn("Can't read tty termios");
222 return;
223 }
224
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930225 if (console->tty.type == TTY_DEVICE_UART && console->tty.uart.baud) {
226 if (cfsetspeed(&termios, console->tty.uart.baud) < 0) {
227 warn("Couldn't set speeds for %s", console->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930228 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700229 }
230
231 /* Set console to raw mode: we don't want any processing to occur on
232 * the underlying terminal input/output.
233 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800234 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700235
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930236 rc = tcsetattr(console->tty.fd, TCSANOW, &termios);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930237 if (rc) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930238 warn("Can't set terminal options for %s", console->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930239 }
Jeremy Kerr54e95692016-03-24 17:07:56 +0800240}
241
242/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800243 * Open and initialise the serial device
244 */
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930245static void tty_init_vuart_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800246{
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930247 assert(console->tty.type == TTY_DEVICE_VUART);
248
249 if (console->tty.vuart.sirq) {
250 tty_set_sysfs_attr(console, "sirq", console->tty.vuart.sirq);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930251 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800252
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930253 if (console->tty.vuart.lpc_addr) {
254 tty_set_sysfs_attr(console, "lpc_address",
255 console->tty.vuart.lpc_addr);
256 }
257}
258
259static int tty_init_io(struct console *console)
260{
261 console->tty.fd = open(console->tty.dev, O_RDWR);
262 if (console->tty.fd <= 0) {
263 warn("Can't open tty %s", console->tty.dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800264 return -1;
265 }
266
267 /* Disable character delay. We may want to later enable this when
268 * we detect larger amounts of data
269 */
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930270 fcntl(console->tty.fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800271
Jeremy Kerr54e95692016-03-24 17:07:56 +0800272 tty_init_termios(console);
273
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930274 console->pollfds[console->n_pollers].fd = console->tty.fd;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800275 console->pollfds[console->n_pollers].events = POLLIN;
276
Jeremy Kerrd831f962016-01-29 17:18:01 +0800277 return 0;
278}
279
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930280static int tty_init_vuart(struct console *console, struct config *config)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800281{
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930282 unsigned long parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800283 const char *val;
284 char *endp;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930285
286 assert(console->tty.type == TTY_DEVICE_VUART);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800287
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800288 val = config_get_value(config, "lpc-address");
289 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930290 errno = 0;
291 parsed = strtoul(val, &endp, 0);
292 if (parsed == ULONG_MAX && errno == ERANGE) {
293 warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'",
294 val);
295 return -1;
296 }
297
298 if (parsed > UINT16_MAX) {
299 warn("Invalid LPC address '%s'", val);
300 return -1;
301 }
302
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930303 console->tty.vuart.lpc_addr = (uint16_t)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800304 if (endp == optarg) {
305 warn("Invalid LPC address: '%s'", val);
306 return -1;
307 }
308 }
309
310 val = config_get_value(config, "sirq");
311 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930312 errno = 0;
313 parsed = strtoul(val, &endp, 0);
314 if (parsed == ULONG_MAX && errno == ERANGE) {
315 warn("Cannot interpret 'sirq' value as an unsigned long: '%s'",
316 val);
317 }
318
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930319 if (parsed > 16) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930320 warn("Invalid LPC SERIRQ: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930321 }
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930322
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930323 console->tty.vuart.sirq = (int)parsed;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930324 if (endp == optarg) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800325 warn("Invalid sirq: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930326 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800327 }
328
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930329 return 0;
330}
331
332static int tty_init(struct console *console, struct config *config,
333 const char *tty_arg)
334{
335 const char *val;
336 int rc;
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700337
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930338 if (tty_arg) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930339 console->tty.kname = tty_arg;
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930340 } else if ((val = config_get_value(config, "upstream-tty"))) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930341 console->tty.kname = val;
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930342 } else {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800343 warnx("Error: No TTY device specified");
344 return -1;
345 }
346
347 rc = tty_find_device(console);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930348 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800349 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930350 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800351
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930352 switch (console->tty.type) {
353 case TTY_DEVICE_VUART:
354 rc = tty_init_vuart(console, config);
355 if (rc) {
356 return rc;
357 }
358
359 tty_init_vuart_io(console);
360 break;
361 case TTY_DEVICE_UART:
362 val = config_get_value(config, "baud");
363 if (val) {
364 if (config_parse_baud(&console->tty.uart.baud, val)) {
365 warnx("Invalid baud rate: '%s'", val);
366 }
367 }
368 break;
369 case TTY_DEVICE_PTY:
370 break;
371 case TTY_DEVICE_UNDEFINED:
372 default:
373 warnx("Cannot configure unrecognised TTY device");
374 return -1;
375 }
376
377 return tty_init_io(console);
378}
379
380static void tty_fini(struct console *console)
381{
382 if (console->tty.type == TTY_DEVICE_VUART) {
383 free(console->tty.vuart.sysfs_devnode);
384 }
385 free(console->tty.dev);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800386}
387
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800388int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800389{
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930390 return write_buf_to_fd(console->tty.fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800391}
392
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500393/* Read console if from config and prepare a socket name */
394static int set_socket_info(struct console *console, struct config *config)
395{
396 ssize_t len;
397
Andrew Jeffery9a8f30e2023-05-02 13:56:02 +0930398 console->console_id = config_get_value(config, "console-id");
399
400 /* socket-id is deprecated */
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500401 if (!console->console_id) {
Andrew Jeffery9a8f30e2023-05-02 13:56:02 +0930402 console->console_id = config_get_value(config, "socket-id");
403 }
404
405 if (!console->console_id) {
406 warnx("Error: The console-id is not set in the config file");
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500407 return EXIT_FAILURE;
408 }
409
410 /* Get the socket name/path */
411 len = console_socket_path(console->socket_name, console->console_id);
412 if (len < 0) {
413 warn("Failed to set socket path: %s", strerror(errno));
414 return EXIT_FAILURE;
415 }
416
417 /* Socket name is not a null terminated string hence save the length */
418 console->socket_name_len = len;
419
420 return 0;
421}
422
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800423static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800424{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930425 /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
426 extern struct handler *__start_handlers;
427 extern struct handler *__stop_handlers;
428 /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800429 struct handler *handler;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930430 int i;
431 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800432
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800433 console->n_handlers = &__stop_handlers - &__start_handlers;
434 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800435
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930436 printf("%ld handler%s\n", console->n_handlers,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930437 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800438
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800439 for (i = 0; i < console->n_handlers; i++) {
440 handler = console->handlers[i];
441
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800442 rc = 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930443 if (handler->init) {
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800444 rc = handler->init(handler, console, config);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930445 }
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800446
447 handler->active = rc == 0;
448
449 printf(" %s [%sactive]\n", handler->name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930450 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800451 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800452}
453
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800454static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800455{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800456 struct handler *handler;
457 int i;
458
459 for (i = 0; i < console->n_handlers; i++) {
460 handler = console->handlers[i];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930461 if (handler->fini && handler->active) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800462 handler->fini(handler);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930463 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800464 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800465}
466
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800467static int get_current_time(struct timeval *tv)
468{
469 struct timespec t;
470 int rc;
471
472 /*
473 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
474 * local time changes. However, a struct timeval is more
475 * convenient for calculations, so convert to that.
476 */
477 rc = clock_gettime(CLOCK_MONOTONIC, &t);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930478 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800479 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930480 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800481
482 tv->tv_sec = t.tv_sec;
483 tv->tv_usec = t.tv_nsec / 1000;
484
485 return 0;
486}
487
Andrew Jefferya72711a2023-04-18 18:19:41 +0930488struct ringbuffer_consumer *
489console_ringbuffer_consumer_register(struct console *console,
490 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800491{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800492 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800493}
494
Jeremy Kerr55c97122017-02-07 17:06:46 +0800495struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930496 struct handler *handler,
497 poller_event_fn_t poller_fn,
498 poller_timeout_fn_t timeout_fn, int fd,
499 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800500{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800501 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930502 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800503
504 poller = malloc(sizeof(*poller));
505 poller->remove = false;
506 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800507 poller->event_fn = poller_fn;
508 poller->timeout_fn = timeout_fn;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800509 poller->data = data;
510
511 /* add one to our pollers array */
512 n = console->n_pollers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930513 /*
514 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
515 * pointer type.
516 */
517 /* NOLINTBEGIN(bugprone-sizeof-expression) */
518 console->pollers = reallocarray(console->pollers, console->n_pollers,
519 sizeof(*console->pollers));
520 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800521
522 console->pollers[n] = poller;
523
524 /* increase pollfds array too */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930525 console->pollfds =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930526 reallocarray(console->pollfds,
527 (MAX_INTERNAL_POLLFD + console->n_pollers),
528 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800529
530 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930531 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
532 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800533
534 console->pollfds[n].fd = fd;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930535 console->pollfds[n].events = (short)(events & 0x7fff);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800536
537 return poller;
538}
539
Andrew Jefferya72711a2023-04-18 18:19:41 +0930540void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800541{
542 int i;
543
544 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930545 for (i = 0; i < console->n_pollers; i++) {
546 if (console->pollers[i] == poller) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800547 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930548 }
549 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800550
551 assert(i < console->n_pollers);
552
553 console->n_pollers--;
554
Andrew Jeffery91b52172023-04-19 12:42:14 +0930555 /*
556 * Remove the item from the pollers array...
557 *
558 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
559 * pointer type.
560 */
561 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930562 memmove(&console->pollers[i], &console->pollers[i + 1],
563 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800564
Andrew Jeffery91b52172023-04-19 12:42:14 +0930565 console->pollers = reallocarray(console->pollers, console->n_pollers,
566 sizeof(*console->pollers));
567 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800568
569 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930570 memmove(&console->pollfds[i], &console->pollfds[i + 1],
571 sizeof(*console->pollfds) *
572 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800573
Andrew Jefferya72711a2023-04-18 18:19:41 +0930574 console->pollfds =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930575 reallocarray(console->pollfds,
576 (MAX_INTERNAL_POLLFD + console->n_pollers),
577 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800578
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800579 free(poller);
580}
581
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800582void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930583 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800584{
585 int i;
586
587 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930588 for (i = 0; i < console->n_pollers; i++) {
589 if (console->pollers[i] == poller) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800590 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930591 }
592 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800593
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930594 console->pollfds[i].events = (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800595}
596
Andrew Jefferyfd048322023-04-18 12:02:01 +0930597void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930598 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800599{
600 struct timeval now;
601 int rc;
602
603 rc = get_current_time(&now);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930604 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800605 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930606 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800607
608 timeradd(&now, tv, &poller->timeout);
609}
610
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930611static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800612{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930613 struct timeval *earliest;
614 struct timeval interval;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800615 struct poller *poller;
616 int i;
617
618 earliest = NULL;
619
620 for (i = 0; i < console->n_pollers; i++) {
621 poller = console->pollers[i];
622
623 if (poller->timeout_fn && timerisset(&poller->timeout) &&
624 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930625 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800626 // poller is buffering data and needs the poll
627 // function to timeout.
628 earliest = &poller->timeout;
629 }
630 }
631
632 if (earliest) {
633 if (timercmp(earliest, cur_time, >)) {
634 /* recalculate the timeout period, time period has
635 * not elapsed */
636 timersub(earliest, cur_time, &interval);
637 return ((interval.tv_sec * 1000) +
638 (interval.tv_usec / 1000));
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930639 } /* return from poll immediately */
640 return 0;
641
642 } /* poll indefinitely */
643 return -1;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800644}
645
646static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800647{
648 struct poller *poller;
649 struct pollfd *pollfd;
650 enum poller_ret prc;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930651 int i;
652 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800653
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800654 rc = 0;
655
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800656 /*
657 * Process poll events by iterating through the pollers and pollfds
658 * in-step, calling any pollers that we've found revents for.
659 */
660 for (i = 0; i < console->n_pollers; i++) {
661 poller = console->pollers[i];
662 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800663 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800664
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800665 /* process pending events... */
666 if (pollfd->revents) {
667 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930668 poller->data);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930669 if (prc == POLLER_EXIT) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800670 rc = -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930671 } else if (prc == POLLER_REMOVE) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800672 poller->remove = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930673 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800674 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800675
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800676 if ((prc == POLLER_OK) && poller->timeout_fn &&
677 timerisset(&poller->timeout) &&
678 timercmp(&poller->timeout, cur_time, <=)) {
679 /* One of the ringbuffer consumers is buffering the
680 data stream. The amount of idle time the consumer
681 desired has expired. Process the buffered data for
682 transmission. */
683 timerclear(&poller->timeout);
684 prc = poller->timeout_fn(poller->handler, poller->data);
685 if (prc == POLLER_EXIT) {
686 rc = -1;
687 } else if (prc == POLLER_REMOVE) {
688 poller->remove = true;
689 }
690 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800691 }
692
693 /**
694 * Process deferred removals; restarting each time we unregister, as
695 * the array will have changed
696 */
697 for (;;) {
698 bool removed = false;
699
700 for (i = 0; i < console->n_pollers; i++) {
701 poller = console->pollers[i];
702 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800703 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800704 removed = true;
705 break;
706 }
707 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930708 if (!removed) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800709 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930710 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800711 }
712
713 return rc;
714}
715
Jeremy Kerr769cee12016-03-15 17:53:56 +0800716static void sighandler(int signal)
717{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930718 if (signal == SIGINT) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800719 sigint = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930720 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800721}
722
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800723int run_console(struct console *console)
724{
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930725 sighandler_t sighandler_save = signal(SIGINT, sighandler);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800726 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930727 long timeout;
728 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800729
730 rc = 0;
731
Jeremy Kerrd831f962016-01-29 17:18:01 +0800732 for (;;) {
733 uint8_t buf[4096];
734
Jeremy Kerr17641452017-02-07 22:09:13 +0800735 BUILD_ASSERT(sizeof(buf) <= buffer_size);
736
Jeremy Kerr769cee12016-03-15 17:53:56 +0800737 if (sigint) {
738 fprintf(stderr, "Received interrupt, exiting\n");
739 break;
740 }
741
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800742 rc = get_current_time(&tv);
743 if (rc) {
744 warn("Failed to read current time");
745 break;
746 }
747
748 timeout = get_poll_timeout(console, &tv);
749
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800750 rc = poll(console->pollfds,
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930751 console->n_pollers + MAX_INTERNAL_POLLFD,
752 (int)timeout);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800753
Jeremy Kerrd831f962016-01-29 17:18:01 +0800754 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800755 if (errno == EINTR) {
756 continue;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800757 }
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930758 warn("poll error");
759 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800760 }
761
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800762 /* process internal fd first */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800763 if (console->pollfds[console->n_pollers].revents) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930764 rc = read(console->tty.fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800765 if (rc <= 0) {
766 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800767 rc = -1;
768 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800769 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800770 rc = ringbuffer_queue(console->rb, buf, rc);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930771 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800772 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930773 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800774 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800775
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800776 if (console->pollfds[console->n_pollers + 1].revents) {
777 sd_bus_process(console->bus, NULL);
778 }
779
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800780 /* ... and then the pollers */
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800781 rc = call_pollers(console, &tv);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930782 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800783 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930784 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800785 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800786
787 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800788 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800789
790 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800791}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800792static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930793 { "config", required_argument, 0, 'c' },
794 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800795};
796
797int main(int argc, char **argv)
798{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800799 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500800 const char *config_tty_kname = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800801 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800802 struct config *config;
803 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800804
Jeremy Kerrd831f962016-01-29 17:18:01 +0800805 for (;;) {
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930806 int c;
807 int idx;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800808
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800809 c = getopt_long(argc, argv, "c:", options, &idx);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930810 if (c == -1) {
Jeremy Kerrd831f962016-01-29 17:18:01 +0800811 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930812 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800813
814 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800815 case 'c':
816 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800817 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800818 case 'h':
819 case '?':
820 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800821 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800822 }
823 }
824
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930825 if (optind < argc) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030826 config_tty_kname = argv[optind];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930827 }
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500828
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800829 console = malloc(sizeof(struct console));
830 memset(console, 0, sizeof(*console));
Andrew Jefferya72711a2023-04-18 18:19:41 +0930831 console->pollfds =
832 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Jeremy Kerrf733c852017-02-07 18:40:10 +0800833 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800834
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800835 config = config_init(config_filename);
836 if (!config) {
837 warnx("Can't read configuration, exiting.");
Andrew Jeffery29c59c42023-05-03 19:33:07 +0930838 rc = -1;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800839 goto out_free;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800840 }
841
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500842 if (set_socket_info(console, config)) {
Andrew Jeffery29c59c42023-05-03 19:33:07 +0930843 rc = -1;
844 goto out_config_fini;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500845 }
846
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930847 rc = tty_init(console, config, config_tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930848 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800849 goto out_config_fini;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930850 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800851
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800852 dbus_init(console, config);
853
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800854 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800855
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800856 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800857
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800858 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800859
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930860 tty_fini(console);
861
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800862out_config_fini:
863 config_fini(config);
864
Jeremy Kerr957818b2016-03-08 14:35:15 +0800865out_free:
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800866 free(console->pollers);
867 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800868 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800869
870 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
871}