blob: 99cf1c798262fe6a1deef07378ebc4f30aaf386c [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 */
153 rc = asprintf(&tty_vuart_lpc_addr, "%s/lpc_addr", tty_sysfs_devnode);
154 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 Palsuleb14ca192023-03-31 09:49:00 -0500394/* Read console if from config and 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{
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930398 const char *resolved_id;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500399 ssize_t len;
400
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930401 if (console_id) {
402 resolved_id = console_id;
403 } else {
404 resolved_id = config_get_value(config, "console-id");
Andrew Jeffery9a8f30e2023-05-02 13:56:02 +0930405
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930406 /* socket-id is deprecated */
407 if (!resolved_id) {
408 resolved_id = config_get_value(config, "socket-id");
409 }
Andrew Jeffery9a8f30e2023-05-02 13:56:02 +0930410 }
411
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930412 if (!resolved_id) {
413 warnx("console-id was not specified");
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500414 return EXIT_FAILURE;
415 }
416
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930417 console->console_id = resolved_id;
418
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500419 /* Get the socket name/path */
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930420 len = console_socket_path(console->socket_name, resolved_id);
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500421 if (len < 0) {
422 warn("Failed to set socket path: %s", strerror(errno));
423 return EXIT_FAILURE;
424 }
425
426 /* Socket name is not a null terminated string hence save the length */
427 console->socket_name_len = len;
428
429 return 0;
430}
431
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800432static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800433{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930434 /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
435 extern struct handler *__start_handlers;
436 extern struct handler *__stop_handlers;
437 /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800438 struct handler *handler;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930439 int i;
440 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800441
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800442 console->n_handlers = &__stop_handlers - &__start_handlers;
443 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800444
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930445 printf("%ld handler%s\n", console->n_handlers,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930446 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800447
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800448 for (i = 0; i < console->n_handlers; i++) {
449 handler = console->handlers[i];
450
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800451 rc = 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930452 if (handler->init) {
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800453 rc = handler->init(handler, console, config);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930454 }
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800455
456 handler->active = rc == 0;
457
458 printf(" %s [%sactive]\n", handler->name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930459 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800460 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800461}
462
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800463static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800464{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800465 struct handler *handler;
466 int i;
467
468 for (i = 0; i < console->n_handlers; i++) {
469 handler = console->handlers[i];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930470 if (handler->fini && handler->active) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800471 handler->fini(handler);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930472 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800473 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800474}
475
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800476static int get_current_time(struct timeval *tv)
477{
478 struct timespec t;
479 int rc;
480
481 /*
482 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
483 * local time changes. However, a struct timeval is more
484 * convenient for calculations, so convert to that.
485 */
486 rc = clock_gettime(CLOCK_MONOTONIC, &t);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930487 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800488 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930489 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800490
491 tv->tv_sec = t.tv_sec;
492 tv->tv_usec = t.tv_nsec / 1000;
493
494 return 0;
495}
496
Andrew Jefferya72711a2023-04-18 18:19:41 +0930497struct ringbuffer_consumer *
498console_ringbuffer_consumer_register(struct console *console,
499 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800500{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800501 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800502}
503
Jeremy Kerr55c97122017-02-07 17:06:46 +0800504struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930505 struct handler *handler,
506 poller_event_fn_t poller_fn,
507 poller_timeout_fn_t timeout_fn, int fd,
508 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800509{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800510 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930511 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800512
513 poller = malloc(sizeof(*poller));
514 poller->remove = false;
515 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800516 poller->event_fn = poller_fn;
517 poller->timeout_fn = timeout_fn;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800518 poller->data = data;
519
520 /* add one to our pollers array */
521 n = console->n_pollers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930522 /*
523 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
524 * pointer type.
525 */
526 /* NOLINTBEGIN(bugprone-sizeof-expression) */
527 console->pollers = reallocarray(console->pollers, console->n_pollers,
528 sizeof(*console->pollers));
529 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800530
531 console->pollers[n] = poller;
532
533 /* increase pollfds array too */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930534 console->pollfds =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930535 reallocarray(console->pollfds,
536 (MAX_INTERNAL_POLLFD + console->n_pollers),
537 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800538
539 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930540 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
541 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800542
543 console->pollfds[n].fd = fd;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930544 console->pollfds[n].events = (short)(events & 0x7fff);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800545
546 return poller;
547}
548
Andrew Jefferya72711a2023-04-18 18:19:41 +0930549void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800550{
551 int i;
552
553 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930554 for (i = 0; i < console->n_pollers; i++) {
555 if (console->pollers[i] == poller) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800556 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930557 }
558 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800559
560 assert(i < console->n_pollers);
561
562 console->n_pollers--;
563
Andrew Jeffery91b52172023-04-19 12:42:14 +0930564 /*
565 * Remove the item from the pollers array...
566 *
567 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
568 * pointer type.
569 */
570 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930571 memmove(&console->pollers[i], &console->pollers[i + 1],
572 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800573
Andrew Jeffery91b52172023-04-19 12:42:14 +0930574 console->pollers = reallocarray(console->pollers, console->n_pollers,
575 sizeof(*console->pollers));
576 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800577
578 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930579 memmove(&console->pollfds[i], &console->pollfds[i + 1],
580 sizeof(*console->pollfds) *
581 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800582
Andrew Jefferya72711a2023-04-18 18:19:41 +0930583 console->pollfds =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930584 reallocarray(console->pollfds,
585 (MAX_INTERNAL_POLLFD + console->n_pollers),
586 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800587
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800588 free(poller);
589}
590
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800591void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930592 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800593{
594 int i;
595
596 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930597 for (i = 0; i < console->n_pollers; i++) {
598 if (console->pollers[i] == poller) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800599 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930600 }
601 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800602
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930603 console->pollfds[i].events = (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800604}
605
Andrew Jefferyfd048322023-04-18 12:02:01 +0930606void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930607 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800608{
609 struct timeval now;
610 int rc;
611
612 rc = get_current_time(&now);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930613 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800614 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930615 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800616
617 timeradd(&now, tv, &poller->timeout);
618}
619
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930620static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800621{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930622 struct timeval *earliest;
623 struct timeval interval;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800624 struct poller *poller;
625 int i;
626
627 earliest = NULL;
628
629 for (i = 0; i < console->n_pollers; i++) {
630 poller = console->pollers[i];
631
632 if (poller->timeout_fn && timerisset(&poller->timeout) &&
633 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930634 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800635 // poller is buffering data and needs the poll
636 // function to timeout.
637 earliest = &poller->timeout;
638 }
639 }
640
641 if (earliest) {
642 if (timercmp(earliest, cur_time, >)) {
643 /* recalculate the timeout period, time period has
644 * not elapsed */
645 timersub(earliest, cur_time, &interval);
646 return ((interval.tv_sec * 1000) +
647 (interval.tv_usec / 1000));
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930648 } /* return from poll immediately */
649 return 0;
650
651 } /* poll indefinitely */
652 return -1;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800653}
654
655static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800656{
657 struct poller *poller;
658 struct pollfd *pollfd;
659 enum poller_ret prc;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930660 int i;
661 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800662
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800663 rc = 0;
664
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800665 /*
666 * Process poll events by iterating through the pollers and pollfds
667 * in-step, calling any pollers that we've found revents for.
668 */
669 for (i = 0; i < console->n_pollers; i++) {
670 poller = console->pollers[i];
671 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800672 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800673
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800674 /* process pending events... */
675 if (pollfd->revents) {
676 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930677 poller->data);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930678 if (prc == POLLER_EXIT) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800679 rc = -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930680 } else if (prc == POLLER_REMOVE) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800681 poller->remove = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930682 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800683 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800684
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800685 if ((prc == POLLER_OK) && poller->timeout_fn &&
686 timerisset(&poller->timeout) &&
687 timercmp(&poller->timeout, cur_time, <=)) {
688 /* One of the ringbuffer consumers is buffering the
689 data stream. The amount of idle time the consumer
690 desired has expired. Process the buffered data for
691 transmission. */
692 timerclear(&poller->timeout);
693 prc = poller->timeout_fn(poller->handler, poller->data);
694 if (prc == POLLER_EXIT) {
695 rc = -1;
696 } else if (prc == POLLER_REMOVE) {
697 poller->remove = true;
698 }
699 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800700 }
701
702 /**
703 * Process deferred removals; restarting each time we unregister, as
704 * the array will have changed
705 */
706 for (;;) {
707 bool removed = false;
708
709 for (i = 0; i < console->n_pollers; i++) {
710 poller = console->pollers[i];
711 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800712 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800713 removed = true;
714 break;
715 }
716 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930717 if (!removed) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800718 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930719 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800720 }
721
722 return rc;
723}
724
Jeremy Kerr769cee12016-03-15 17:53:56 +0800725static void sighandler(int signal)
726{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930727 if (signal == SIGINT) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800728 sigint = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930729 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800730}
731
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800732int run_console(struct console *console)
733{
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930734 sighandler_t sighandler_save = signal(SIGINT, sighandler);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800735 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930736 long timeout;
737 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800738
739 rc = 0;
740
Jeremy Kerrd831f962016-01-29 17:18:01 +0800741 for (;;) {
742 uint8_t buf[4096];
743
Jeremy Kerr17641452017-02-07 22:09:13 +0800744 BUILD_ASSERT(sizeof(buf) <= buffer_size);
745
Jeremy Kerr769cee12016-03-15 17:53:56 +0800746 if (sigint) {
747 fprintf(stderr, "Received interrupt, exiting\n");
748 break;
749 }
750
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800751 rc = get_current_time(&tv);
752 if (rc) {
753 warn("Failed to read current time");
754 break;
755 }
756
757 timeout = get_poll_timeout(console, &tv);
758
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800759 rc = poll(console->pollfds,
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930760 console->n_pollers + MAX_INTERNAL_POLLFD,
761 (int)timeout);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800762
Jeremy Kerrd831f962016-01-29 17:18:01 +0800763 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800764 if (errno == EINTR) {
765 continue;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800766 }
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930767 warn("poll error");
768 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800769 }
770
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800771 /* process internal fd first */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800772 if (console->pollfds[console->n_pollers].revents) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930773 rc = read(console->tty.fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800774 if (rc <= 0) {
775 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800776 rc = -1;
777 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800778 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800779 rc = ringbuffer_queue(console->rb, buf, rc);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930780 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800781 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930782 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800783 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800784
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800785 if (console->pollfds[console->n_pollers + 1].revents) {
786 sd_bus_process(console->bus, NULL);
787 }
788
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800789 /* ... and then the pollers */
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800790 rc = call_pollers(console, &tv);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930791 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800792 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930793 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800794 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800795
796 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800797 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800798
799 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800800}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800801static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930802 { "config", required_argument, 0, 'c' },
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930803 { "console-id", required_argument, 0, 'i' },
Andrew Jefferya72711a2023-04-18 18:19:41 +0930804 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800805};
806
807int main(int argc, char **argv)
808{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800809 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500810 const char *config_tty_kname = NULL;
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930811 const char *console_id = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800812 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800813 struct config *config;
814 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800815
Jeremy Kerrd831f962016-01-29 17:18:01 +0800816 for (;;) {
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930817 int c;
818 int idx;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800819
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930820 c = getopt_long(argc, argv, "c:i:", options, &idx);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930821 if (c == -1) {
Jeremy Kerrd831f962016-01-29 17:18:01 +0800822 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930823 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800824
825 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800826 case 'c':
827 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800828 break;
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930829 case 'i':
830 console_id = optarg;
831 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800832 case 'h':
833 case '?':
834 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800835 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800836 }
837 }
838
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930839 if (optind < argc) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030840 config_tty_kname = argv[optind];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930841 }
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500842
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800843 console = malloc(sizeof(struct console));
844 memset(console, 0, sizeof(*console));
Andrew Jefferya72711a2023-04-18 18:19:41 +0930845 console->pollfds =
846 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Jeremy Kerrf733c852017-02-07 18:40:10 +0800847 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800848
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800849 config = config_init(config_filename);
850 if (!config) {
851 warnx("Can't read configuration, exiting.");
Andrew Jeffery29c59c42023-05-03 19:33:07 +0930852 rc = -1;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800853 goto out_free;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800854 }
855
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930856 if (set_socket_info(console, config, console_id)) {
Andrew Jeffery29c59c42023-05-03 19:33:07 +0930857 rc = -1;
858 goto out_config_fini;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500859 }
860
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930861 rc = tty_init(console, config, config_tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930862 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800863 goto out_config_fini;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930864 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800865
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800866 dbus_init(console, config);
867
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800868 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800869
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800870 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800871
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800872 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800873
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930874 tty_fini(console);
875
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800876out_config_fini:
877 config_fini(config);
878
Jeremy Kerr957818b2016-03-08 14:35:15 +0800879out_free:
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800880 free(console->pollers);
881 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800882 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800883
884 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
885}