blob: 596e2270f948aa3430413b575077f4a826afeb44 [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>
Zev Weiss7dc08ba2023-09-12 20:49:14 -070031#include <glob.h>
Jeremy Kerr17217842016-01-29 18:44:21 +080032#include <limits.h>
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080033#include <time.h>
Jeremy Kerr54e95692016-03-24 17:07:56 +080034#include <termios.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080035
36#include <sys/types.h>
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080037#include <sys/time.h>
Ninad Palsuleb14ca192023-03-31 09:49:00 -050038#include <sys/socket.h>
Joel Stanley87e344c2016-09-01 00:00:51 +093039#include <poll.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080040
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080041#include "console-server.h"
Jeremy Kerrd831f962016-01-29 17:18:01 +080042
Andrew Jeffery30ea6382023-05-03 16:56:10 +093043#define DEV_PTS_PATH "/dev/pts"
44
Medicine Yeh7f2bfb92024-03-10 23:19:41 -070045/* default size of the shared backlog ringbuffer */
46const size_t default_buffer_size = 128ul * 1024ul;
Jeremy Kerrf733c852017-02-07 18:40:10 +080047
Jeremy Kerr769cee12016-03-15 17:53:56 +080048/* state shared with the signal handler */
Andrew Jeffery553cb662024-07-09 15:14:56 +093049static volatile sig_atomic_t sigint;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080050
Jeremy Kerrd831f962016-01-29 17:18:01 +080051static void usage(const char *progname)
52{
53 fprintf(stderr,
Andrew Jefferya72711a2023-04-18 18:19:41 +093054 "usage: %s [options] <DEVICE>\n"
55 "\n"
56 "Options:\n"
Andrew Jeffery954be0f2023-05-03 21:01:59 +093057 " --config <FILE>\tUse FILE for configuration\n"
58 " --console-id <NAME>\tUse NAME in the UNIX domain socket address\n"
Andrew Jefferya72711a2023-04-18 18:19:41 +093059 "",
Jeremy Kerrd831f962016-01-29 17:18:01 +080060 progname);
61}
62
Andrew Jeffery30ea6382023-05-03 16:56:10 +093063/* populates console->tty.dev and console->tty.sysfs_devnode, using the tty kernel name */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080064static int tty_find_device(struct console *console)
Jeremy Kerr17217842016-01-29 18:44:21 +080065{
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +093066 char *tty_class_device_link = NULL;
67 char *tty_path_input_real = NULL;
68 char *tty_device_tty_dir = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +093069 char *tty_vuart_lpc_addr = NULL;
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +093070 char *tty_device_reldir = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +093071 char *tty_sysfs_devnode = NULL;
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +093072 char *tty_kname_real = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +093073 char *tty_path_input = NULL;
Jeremy Kerr17217842016-01-29 18:44:21 +080074 int rc;
75
Andrew Jeffery30ea6382023-05-03 16:56:10 +093076 console->tty.type = TTY_DEVICE_UNDEFINED;
77
78 assert(console->tty.kname);
79 if (!strlen(console->tty.kname)) {
80 warnx("TTY kname must not be empty");
81 rc = -1;
82 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093083 }
Jeremy Kerr17217842016-01-29 18:44:21 +080084
Andrew Jeffery30ea6382023-05-03 16:56:10 +093085 if (console->tty.kname[0] == '/') {
86 tty_path_input = strdup(console->tty.kname);
87 if (!tty_path_input) {
88 rc = -1;
89 goto out_free;
90 }
91 } else {
92 rc = asprintf(&tty_path_input, "/dev/%s", console->tty.kname);
93 if (rc < 0) {
94 goto out_free;
95 }
96 }
97
98 /* udev may rename the tty name with a symbol link, try to resolve */
Yi Li45ad7672016-10-18 23:21:19 +080099 tty_path_input_real = realpath(tty_path_input, NULL);
100 if (!tty_path_input_real) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930101 warn("Can't find realpath for %s", tty_path_input);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930102 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800103 goto out_free;
104 }
105
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930106 /*
107 * Allow hooking obmc-console-server up to PTYs for testing
108 *
109 * https://amboar.github.io/notes/2023/05/02/testing-obmc-console-with-socat.html
110 */
111 if (!strncmp(DEV_PTS_PATH, tty_path_input_real, strlen(DEV_PTS_PATH))) {
112 console->tty.type = TTY_DEVICE_PTY;
113 console->tty.dev = strdup(console->tty.kname);
114 rc = console->tty.dev ? 0 : -1;
115 goto out_free;
116 }
117
Yi Li45ad7672016-10-18 23:21:19 +0800118 tty_kname_real = basename(tty_path_input_real);
119 if (!tty_kname_real) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930120 warn("Can't find real name for %s", console->tty.kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930121 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800122 goto out_free;
123 }
124
Andrew Jefferya72711a2023-04-18 18:19:41 +0930125 rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s",
126 tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930127 if (rc < 0) {
Yi Li45ad7672016-10-18 23:21:19 +0800128 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930129 }
Yi Li45ad7672016-10-18 23:21:19 +0800130
Jeremy Kerr17217842016-01-29 18:44:21 +0800131 tty_device_tty_dir = realpath(tty_class_device_link, NULL);
Yi Li45ad7672016-10-18 23:21:19 +0800132 if (!tty_device_tty_dir) {
133 warn("Can't query sysfs for device %s", tty_kname_real);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930134 rc = -1;
Jeremy Kerr17217842016-01-29 18:44:21 +0800135 goto out_free;
136 }
137
138 rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930139 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800140 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930141 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800142
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930143 tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
144 if (!tty_sysfs_devnode) {
Yi Li45ad7672016-10-18 23:21:19 +0800145 warn("Can't find parent device for %s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930146 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800147
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930148 rc = asprintf(&console->tty.dev, "/dev/%s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930149 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800150 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930151 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800152
Oskar Senft955d1402023-09-27 22:43:22 -0400153 // Default to non-VUART
154 console->tty.type = TTY_DEVICE_UART;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930155
Oskar Senft955d1402023-09-27 22:43:22 -0400156 /* Arbitrarily pick an attribute to differentiate UART vs VUART */
157 if (tty_sysfs_devnode) {
158 rc = asprintf(&tty_vuart_lpc_addr, "%s/lpc_address",
159 tty_sysfs_devnode);
160 if (rc < 0) {
161 goto out_free;
162 }
163
164 rc = access(tty_vuart_lpc_addr, F_OK);
165 if (!rc) {
166 console->tty.type = TTY_DEVICE_VUART;
167 console->tty.vuart.sysfs_devnode =
168 strdup(tty_sysfs_devnode);
169 }
170 }
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930171
Jeremy Kerr17217842016-01-29 18:44:21 +0800172 rc = 0;
173
174out_free:
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930175 free(tty_vuart_lpc_addr);
Jeremy Kerr17217842016-01-29 18:44:21 +0800176 free(tty_class_device_link);
Andrew Jeffery982090d2024-07-09 17:31:27 +0930177 free(tty_sysfs_devnode);
Jeremy Kerr17217842016-01-29 18:44:21 +0800178 free(tty_device_tty_dir);
179 free(tty_device_reldir);
Yi Li45ad7672016-10-18 23:21:19 +0800180 free(tty_path_input);
181 free(tty_path_input_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800182 return rc;
183}
184
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800185static int tty_set_sysfs_attr(struct console *console, const char *name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930186 int value)
Jeremy Kerr957818b2016-03-08 14:35:15 +0800187{
188 char *path;
189 FILE *fp;
190 int rc;
191
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930192 assert(console->tty.type == TTY_DEVICE_VUART);
193
194 if (!console->tty.vuart.sysfs_devnode) {
195 return -1;
196 }
197
198 rc = asprintf(&path, "%s/%s", console->tty.vuart.sysfs_devnode, name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930199 if (rc < 0) {
Jeremy Kerr957818b2016-03-08 14:35:15 +0800200 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930201 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800202
203 fp = fopen(path, "w");
204 if (!fp) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930205 warn("Can't access attribute %s on device %s", name,
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930206 console->tty.kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800207 rc = -1;
208 goto out_free;
209 }
210 setvbuf(fp, NULL, _IONBF, 0);
211
212 rc = fprintf(fp, "0x%x", value);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930213 if (rc < 0) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930214 warn("Error writing to %s attribute of device %s", name,
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930215 console->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930216 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800217 fclose(fp);
218
Jeremy Kerr957818b2016-03-08 14:35:15 +0800219out_free:
220 free(path);
221 return rc;
222}
223
Jeremy Kerrd831f962016-01-29 17:18:01 +0800224/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700225 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800226 */
Ninad Palsulee258e512023-04-26 22:17:57 -0500227void tty_init_termios(struct console *console)
Jeremy Kerr54e95692016-03-24 17:07:56 +0800228{
229 struct termios termios;
230 int rc;
231
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930232 rc = tcgetattr(console->tty.fd, &termios);
Jeremy Kerr54e95692016-03-24 17:07:56 +0800233 if (rc) {
234 warn("Can't read tty termios");
235 return;
236 }
237
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930238 if (console->tty.type == TTY_DEVICE_UART && console->tty.uart.baud) {
239 if (cfsetspeed(&termios, console->tty.uart.baud) < 0) {
240 warn("Couldn't set speeds for %s", console->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930241 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700242 }
243
244 /* Set console to raw mode: we don't want any processing to occur on
245 * the underlying terminal input/output.
246 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800247 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700248
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930249 rc = tcsetattr(console->tty.fd, TCSANOW, &termios);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930250 if (rc) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930251 warn("Can't set terminal options for %s", console->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930252 }
Jeremy Kerr54e95692016-03-24 17:07:56 +0800253}
254
255/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800256 * Open and initialise the serial device
257 */
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930258static void tty_init_vuart_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800259{
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930260 assert(console->tty.type == TTY_DEVICE_VUART);
261
262 if (console->tty.vuart.sirq) {
263 tty_set_sysfs_attr(console, "sirq", console->tty.vuart.sirq);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930264 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800265
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930266 if (console->tty.vuart.lpc_addr) {
267 tty_set_sysfs_attr(console, "lpc_address",
268 console->tty.vuart.lpc_addr);
269 }
270}
271
272static int tty_init_io(struct console *console)
273{
274 console->tty.fd = open(console->tty.dev, O_RDWR);
275 if (console->tty.fd <= 0) {
276 warn("Can't open tty %s", console->tty.dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800277 return -1;
278 }
279
280 /* Disable character delay. We may want to later enable this when
281 * we detect larger amounts of data
282 */
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930283 fcntl(console->tty.fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800284
Jeremy Kerr54e95692016-03-24 17:07:56 +0800285 tty_init_termios(console);
286
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930287 console->pollfds[console->n_pollers].fd = console->tty.fd;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800288 console->pollfds[console->n_pollers].events = POLLIN;
289
Jeremy Kerrd831f962016-01-29 17:18:01 +0800290 return 0;
291}
292
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930293static int tty_init_vuart(struct console *console, struct config *config)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800294{
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930295 unsigned long parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800296 const char *val;
297 char *endp;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930298
299 assert(console->tty.type == TTY_DEVICE_VUART);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800300
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800301 val = config_get_value(config, "lpc-address");
302 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930303 errno = 0;
304 parsed = strtoul(val, &endp, 0);
305 if (parsed == ULONG_MAX && errno == ERANGE) {
306 warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'",
307 val);
308 return -1;
309 }
310
311 if (parsed > UINT16_MAX) {
312 warn("Invalid LPC address '%s'", val);
313 return -1;
314 }
315
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930316 console->tty.vuart.lpc_addr = (uint16_t)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800317 if (endp == optarg) {
318 warn("Invalid LPC address: '%s'", val);
319 return -1;
320 }
321 }
322
323 val = config_get_value(config, "sirq");
324 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930325 errno = 0;
326 parsed = strtoul(val, &endp, 0);
327 if (parsed == ULONG_MAX && errno == ERANGE) {
328 warn("Cannot interpret 'sirq' value as an unsigned long: '%s'",
329 val);
330 }
331
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930332 if (parsed > 16) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930333 warn("Invalid LPC SERIRQ: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930334 }
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930335
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930336 console->tty.vuart.sirq = (int)parsed;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930337 if (endp == optarg) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800338 warn("Invalid sirq: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930339 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800340 }
341
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930342 return 0;
343}
344
345static int tty_init(struct console *console, struct config *config,
346 const char *tty_arg)
347{
348 const char *val;
349 int rc;
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700350
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930351 if (tty_arg) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930352 console->tty.kname = tty_arg;
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930353 } else if ((val = config_get_value(config, "upstream-tty"))) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930354 console->tty.kname = val;
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930355 } else {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800356 warnx("Error: No TTY device specified");
357 return -1;
358 }
359
360 rc = tty_find_device(console);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930361 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800362 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930363 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800364
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930365 switch (console->tty.type) {
366 case TTY_DEVICE_VUART:
367 rc = tty_init_vuart(console, config);
368 if (rc) {
369 return rc;
370 }
371
372 tty_init_vuart_io(console);
373 break;
374 case TTY_DEVICE_UART:
375 val = config_get_value(config, "baud");
376 if (val) {
377 if (config_parse_baud(&console->tty.uart.baud, val)) {
378 warnx("Invalid baud rate: '%s'", val);
379 }
380 }
381 break;
382 case TTY_DEVICE_PTY:
383 break;
384 case TTY_DEVICE_UNDEFINED:
385 default:
386 warnx("Cannot configure unrecognised TTY device");
387 return -1;
388 }
389
390 return tty_init_io(console);
391}
392
393static void tty_fini(struct console *console)
394{
395 if (console->tty.type == TTY_DEVICE_VUART) {
396 free(console->tty.vuart.sysfs_devnode);
397 }
398 free(console->tty.dev);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800399}
400
Zev Weiss7dc08ba2023-09-12 20:49:14 -0700401static int write_to_path(const char *path, const char *data)
402{
403 int rc = 0;
404 FILE *f = fopen(path, "w");
405 if (!f) {
406 return -1;
407 }
408
409 if (fprintf(f, "%s", data) < 0) {
410 rc = -1;
411 }
412
413 if (fclose(f)) {
414 rc = -1;
415 }
416
417 return rc;
418}
419
420#define ASPEED_UART_ROUTING_PATTERN \
421 "/sys/bus/platform/drivers/aspeed-uart-routing/*.uart-routing"
422
423static void uart_routing_init(struct config *config)
424{
425 const char *muxcfg;
426 const char *p;
427 size_t buflen;
428 char *sink;
429 char *source;
430 char *muxdir;
431 char *path;
432 glob_t globbuf;
433
434 muxcfg = config_get_value(config, "aspeed-uart-routing");
435 if (!muxcfg) {
436 return;
437 }
438
439 /* Find the driver's sysfs directory */
440 if (glob(ASPEED_UART_ROUTING_PATTERN, GLOB_ERR | GLOB_NOSORT, NULL,
441 &globbuf) != 0) {
442 warn("Couldn't find uart-routing driver directory, cannot apply config");
443 return;
444 }
445 if (globbuf.gl_pathc != 1) {
446 warnx("Found %zd uart-routing driver directories, cannot apply config",
447 globbuf.gl_pathc);
448 goto out_free_glob;
449 }
450 muxdir = globbuf.gl_pathv[0];
451
452 /*
453 * Rather than faff about tracking a bunch of separate buffer sizes,
454 * just use one (worst-case) size for all of them -- +2 for a trailing
455 * NUL and a '/' separator to construct the sysfs file path.
456 */
457 buflen = strlen(muxdir) + strlen(muxcfg) + 2;
458
459 sink = malloc(buflen);
460 source = malloc(buflen);
461 path = malloc(buflen);
462 if (!path || !sink || !source) {
463 warnx("Out of memory applying uart routing config");
464 goto out_free_bufs;
465 }
466
467 p = muxcfg;
468 while (*p) {
469 ssize_t bytes_scanned;
470
471 if (sscanf(p, " %[^:/ \t]:%[^: \t] %zn", sink, source,
472 &bytes_scanned) != 2) {
473 warnx("Invalid syntax in aspeed uart config: '%s' not applied",
474 p);
475 break;
476 }
477 p += bytes_scanned;
478
479 /*
480 * Check that the sink name looks reasonable before proceeding
481 * (there are other writable files in the same directory that
482 * we shouldn't be touching, such as 'driver_override' and
483 * 'uevent').
484 */
485 if (strncmp(sink, "io", strlen("io")) != 0 &&
486 strncmp(sink, "uart", strlen("uart")) != 0) {
487 warnx("Skipping invalid uart routing name '%s' (must be ioN or uartN)",
488 sink);
489 continue;
490 }
491
492 snprintf(path, buflen, "%s/%s", muxdir, sink);
493 if (write_to_path(path, source)) {
494 warn("Failed to apply uart-routing config '%s:%s'",
495 sink, source);
496 }
497 }
498
499out_free_bufs:
500 free(path);
501 free(source);
502 free(sink);
503out_free_glob:
504 globfree(&globbuf);
505}
506
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800507int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800508{
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930509 return write_buf_to_fd(console->tty.fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800510}
511
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500512/* Prepare a socket name */
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930513static int set_socket_info(struct console *console, struct config *config,
514 const char *console_id)
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500515{
516 ssize_t len;
517
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500518 /* Get console id */
519 console->console_id = config_resolve_console_id(config, console_id);
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930520
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500521 /* Get the socket name/path */
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500522 len = console_socket_path(console->socket_name, console->console_id);
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500523 if (len < 0) {
524 warn("Failed to set socket path: %s", strerror(errno));
525 return EXIT_FAILURE;
526 }
527
528 /* Socket name is not a null terminated string hence save the length */
529 console->socket_name_len = len;
530
531 return 0;
532}
533
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800534static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800535{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930536 /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerre2826c72024-07-05 10:54:21 +0800537 extern const struct handler_type *const __start_handlers;
538 extern const struct handler_type *const __stop_handlers;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930539 /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerre2826c72024-07-05 10:54:21 +0800540 size_t n_types;
541 int j = 0;
542 size_t i;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800543
Jeremy Kerre2826c72024-07-05 10:54:21 +0800544 n_types = &__stop_handlers - &__start_handlers;
545 console->handlers = calloc(n_types, sizeof(struct handler *));
546 if (!console->handlers) {
547 err(EXIT_FAILURE, "malloc(handlers)");
548 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800549
Andrew Jeffery079fc512024-07-09 16:51:56 +0930550 printf("%zu handler type%s\n", n_types, n_types == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800551
Jeremy Kerre2826c72024-07-05 10:54:21 +0800552 for (i = 0; i < n_types; i++) {
553 const struct handler_type *type = &__start_handlers[i];
554 struct handler *handler;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800555
Jeremy Kerre2826c72024-07-05 10:54:21 +0800556 /* Should be picked up at build time by
557 * console_handler_register, but check anyway
558 */
559 if (!type->init || !type->fini) {
560 errx(EXIT_FAILURE,
561 "invalid handler type %s: no init() / fini()",
562 type->name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930563 }
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800564
Jeremy Kerre2826c72024-07-05 10:54:21 +0800565 handler = type->init(type, console, config);
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800566
Jeremy Kerre2826c72024-07-05 10:54:21 +0800567 printf(" console '%s': handler %s [%sactive]\n",
568 console->console_id, type->name, handler ? "" : "in");
569
570 if (handler) {
571 handler->type = type;
572 console->handlers[j++] = handler;
573 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800574 }
Jeremy Kerre2826c72024-07-05 10:54:21 +0800575
576 console->n_handlers = j;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800577}
578
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800579static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800580{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800581 struct handler *handler;
582 int i;
583
584 for (i = 0; i < console->n_handlers; i++) {
585 handler = console->handlers[i];
Jeremy Kerre2826c72024-07-05 10:54:21 +0800586 handler->type->fini(handler);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800587 }
Jeremy Kerre2826c72024-07-05 10:54:21 +0800588
589 free(console->handlers);
590 console->handlers = NULL;
591 console->n_handlers = 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800592}
593
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800594static int get_current_time(struct timeval *tv)
595{
596 struct timespec t;
597 int rc;
598
599 /*
600 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
601 * local time changes. However, a struct timeval is more
602 * convenient for calculations, so convert to that.
603 */
604 rc = clock_gettime(CLOCK_MONOTONIC, &t);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930605 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800606 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930607 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800608
609 tv->tv_sec = t.tv_sec;
610 tv->tv_usec = t.tv_nsec / 1000;
611
612 return 0;
613}
614
Andrew Jefferya72711a2023-04-18 18:19:41 +0930615struct ringbuffer_consumer *
616console_ringbuffer_consumer_register(struct console *console,
617 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800618{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800619 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800620}
621
Jeremy Kerr55c97122017-02-07 17:06:46 +0800622struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930623 struct handler *handler,
624 poller_event_fn_t poller_fn,
625 poller_timeout_fn_t timeout_fn, int fd,
626 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800627{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800628 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930629 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800630
631 poller = malloc(sizeof(*poller));
632 poller->remove = false;
633 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800634 poller->event_fn = poller_fn;
635 poller->timeout_fn = timeout_fn;
Andrew Jeffery9cc84592024-07-09 15:44:30 +0930636 timerclear(&poller->timeout);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800637 poller->data = data;
638
639 /* add one to our pollers array */
640 n = console->n_pollers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930641 /*
642 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
643 * pointer type.
644 */
645 /* NOLINTBEGIN(bugprone-sizeof-expression) */
646 console->pollers = reallocarray(console->pollers, console->n_pollers,
647 sizeof(*console->pollers));
648 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800649
650 console->pollers[n] = poller;
651
652 /* increase pollfds array too */
Andrew Jeffery4e44c792023-05-12 15:57:46 +0930653 console->pollfds = reallocarray(
654 console->pollfds, (MAX_INTERNAL_POLLFD + console->n_pollers),
655 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800656
657 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930658 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
659 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800660
661 console->pollfds[n].fd = fd;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930662 console->pollfds[n].events = (short)(events & 0x7fff);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800663
664 return poller;
665}
666
Andrew Jefferya72711a2023-04-18 18:19:41 +0930667void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800668{
669 int i;
670
671 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930672 for (i = 0; i < console->n_pollers; i++) {
673 if (console->pollers[i] == poller) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800674 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930675 }
676 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800677
678 assert(i < console->n_pollers);
679
680 console->n_pollers--;
681
Andrew Jeffery91b52172023-04-19 12:42:14 +0930682 /*
683 * Remove the item from the pollers array...
684 *
685 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
686 * pointer type.
687 */
688 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930689 memmove(&console->pollers[i], &console->pollers[i + 1],
690 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800691
Andrew Jeffery7851a392024-07-09 16:30:22 +0930692 if (console->n_pollers == 0) {
693 free(console->pollers);
694 console->pollers = NULL;
695 } else {
696 console->pollers = reallocarray(console->pollers,
697 console->n_pollers,
698 sizeof(*console->pollers));
699 }
Andrew Jeffery91b52172023-04-19 12:42:14 +0930700 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800701
702 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930703 memmove(&console->pollfds[i], &console->pollfds[i + 1],
704 sizeof(*console->pollfds) *
705 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800706
Andrew Jeffery4e44c792023-05-12 15:57:46 +0930707 console->pollfds = reallocarray(
708 console->pollfds, (MAX_INTERNAL_POLLFD + console->n_pollers),
709 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800710
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800711 free(poller);
712}
713
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800714void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930715 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800716{
717 int i;
718
719 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930720 for (i = 0; i < console->n_pollers; i++) {
721 if (console->pollers[i] == poller) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800722 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930723 }
724 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800725
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930726 console->pollfds[i].events = (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800727}
728
Andrew Jefferyfd048322023-04-18 12:02:01 +0930729void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930730 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800731{
732 struct timeval now;
733 int rc;
734
735 rc = get_current_time(&now);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930736 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800737 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930738 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800739
740 timeradd(&now, tv, &poller->timeout);
741}
742
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930743static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800744{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930745 struct timeval *earliest;
746 struct timeval interval;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800747 struct poller *poller;
748 int i;
749
750 earliest = NULL;
751
752 for (i = 0; i < console->n_pollers; i++) {
753 poller = console->pollers[i];
754
755 if (poller->timeout_fn && timerisset(&poller->timeout) &&
756 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930757 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800758 // poller is buffering data and needs the poll
759 // function to timeout.
760 earliest = &poller->timeout;
761 }
762 }
763
764 if (earliest) {
765 if (timercmp(earliest, cur_time, >)) {
766 /* recalculate the timeout period, time period has
767 * not elapsed */
768 timersub(earliest, cur_time, &interval);
769 return ((interval.tv_sec * 1000) +
770 (interval.tv_usec / 1000));
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930771 } /* return from poll immediately */
772 return 0;
773
774 } /* poll indefinitely */
775 return -1;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800776}
777
778static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800779{
780 struct poller *poller;
781 struct pollfd *pollfd;
782 enum poller_ret prc;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930783 int i;
784 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800785
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800786 rc = 0;
787
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800788 /*
789 * Process poll events by iterating through the pollers and pollfds
790 * in-step, calling any pollers that we've found revents for.
791 */
792 for (i = 0; i < console->n_pollers; i++) {
793 poller = console->pollers[i];
794 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800795 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800796
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800797 /* process pending events... */
798 if (pollfd->revents) {
799 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930800 poller->data);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930801 if (prc == POLLER_EXIT) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800802 rc = -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930803 } else if (prc == POLLER_REMOVE) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800804 poller->remove = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930805 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800806 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800807
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800808 if ((prc == POLLER_OK) && poller->timeout_fn &&
809 timerisset(&poller->timeout) &&
810 timercmp(&poller->timeout, cur_time, <=)) {
811 /* One of the ringbuffer consumers is buffering the
812 data stream. The amount of idle time the consumer
813 desired has expired. Process the buffered data for
814 transmission. */
815 timerclear(&poller->timeout);
816 prc = poller->timeout_fn(poller->handler, poller->data);
817 if (prc == POLLER_EXIT) {
818 rc = -1;
819 } else if (prc == POLLER_REMOVE) {
820 poller->remove = true;
821 }
822 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800823 }
824
825 /**
826 * Process deferred removals; restarting each time we unregister, as
827 * the array will have changed
828 */
829 for (;;) {
830 bool removed = false;
831
832 for (i = 0; i < console->n_pollers; i++) {
833 poller = console->pollers[i];
834 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800835 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800836 removed = true;
837 break;
838 }
839 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930840 if (!removed) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800841 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930842 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800843 }
844
845 return rc;
846}
847
Jeremy Kerr769cee12016-03-15 17:53:56 +0800848static void sighandler(int signal)
849{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930850 if (signal == SIGINT) {
Andrew Jeffery553cb662024-07-09 15:14:56 +0930851 sigint = 1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930852 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800853}
854
Alexander Hansen69257402024-06-07 15:17:09 +0200855static int run_console_iteration(struct console *console)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800856{
Alexander Hansen69257402024-06-07 15:17:09 +0200857 uint8_t buf[4096];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800858 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930859 long timeout;
860 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800861
Alexander Hansen69257402024-06-07 15:17:09 +0200862 if (console->rb->size < sizeof(buf)) {
863 fprintf(stderr, "Ringbuffer size should be greater than %zuB\n",
864 sizeof(buf));
865 return -1;
866 }
867
868 if (sigint) {
869 fprintf(stderr, "Received interrupt, exiting\n");
870 return -1;
871 }
872
873 rc = get_current_time(&tv);
874 if (rc) {
875 warn("Failed to read current time");
876 return -1;
877 }
878
879 timeout = get_poll_timeout(console, &tv);
880
881 rc = poll(console->pollfds, console->n_pollers + MAX_INTERNAL_POLLFD,
882 (int)timeout);
883
884 if (rc < 0) {
885 if (errno == EINTR) {
886 return 0;
887 }
888 warn("poll error");
889 return -1;
890 }
891
892 /* process internal fd first */
893 if (console->pollfds[console->n_pollers].revents) {
894 rc = read(console->tty.fd, buf, sizeof(buf));
895 if (rc <= 0) {
896 warn("Error reading from tty device");
897 return -1;
898 }
899 rc = ringbuffer_queue(console->rb, buf, rc);
900 if (rc) {
901 return -1;
902 }
903 }
904
905 if (console->pollfds[console->n_pollers + 1].revents) {
906 sd_bus_process(console->bus, NULL);
907 }
908
909 /* ... and then the pollers */
910 rc = call_pollers(console, &tv);
911 if (rc) {
912 return -1;
913 }
914 return 0;
915}
916
917int run_console(struct console *console)
918{
919 sighandler_t sighandler_save = signal(SIGINT, sighandler);
920 ssize_t rc = 0;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800921
Jeremy Kerrd831f962016-01-29 17:18:01 +0800922 for (;;) {
Alexander Hansen69257402024-06-07 15:17:09 +0200923 rc = run_console_iteration(console);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930924 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800925 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930926 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800927 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800928
929 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800930 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800931
932 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800933}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800934static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930935 { "config", required_argument, 0, 'c' },
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930936 { "console-id", required_argument, 0, 'i' },
Andrew Jefferya72711a2023-04-18 18:19:41 +0930937 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800938};
939
940int main(int argc, char **argv)
941{
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700942 size_t buffer_size = default_buffer_size;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800943 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500944 const char *config_tty_kname = NULL;
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700945 const char *buffer_size_str = NULL;
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930946 const char *console_id = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800947 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800948 struct config *config;
949 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800950
Jeremy Kerrd831f962016-01-29 17:18:01 +0800951 for (;;) {
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930952 int c;
953 int idx;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800954
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930955 c = getopt_long(argc, argv, "c:i:", options, &idx);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930956 if (c == -1) {
Jeremy Kerrd831f962016-01-29 17:18:01 +0800957 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930958 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800959
960 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800961 case 'c':
962 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800963 break;
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930964 case 'i':
965 console_id = optarg;
966 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800967 case 'h':
968 case '?':
969 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800970 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800971 }
972 }
973
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930974 if (optind < argc) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030975 config_tty_kname = argv[optind];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930976 }
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500977
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700978 config = config_init(config_filename);
979
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800980 console = malloc(sizeof(struct console));
981 memset(console, 0, sizeof(*console));
Andrew Jefferya72711a2023-04-18 18:19:41 +0930982 console->pollfds =
983 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700984 buffer_size_str = config_get_value(config, "ringbuffer-size");
985 if (buffer_size_str) {
986 rc = config_parse_bytesize(buffer_size_str, &buffer_size);
987 if (rc) {
988 warn("Invalid ringbuffer-size. Default to %zukB",
989 buffer_size >> 10);
990 }
991 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800992 console->rb = ringbuffer_init(buffer_size);
Andrew Jeffery3f8d5be2024-07-09 14:40:50 +0930993 if (!console->rb) {
994 rc = -1;
995 goto out_config_fini;
996 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800997
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930998 if (set_socket_info(console, config, console_id)) {
Andrew Jeffery29c59c42023-05-03 19:33:07 +0930999 rc = -1;
Andrew Jeffery3f8d5be2024-07-09 14:40:50 +09301000 goto out_ringbuffer_fini;
Ninad Palsuleb14ca192023-03-31 09:49:00 -05001001 }
1002
Zev Weiss7dc08ba2023-09-12 20:49:14 -07001003 uart_routing_init(config);
1004
Andrew Jefferyd769eec2023-05-03 20:00:09 +09301005 rc = tty_init(console, config, config_tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +09301006 if (rc) {
Andrew Jeffery3f8d5be2024-07-09 14:40:50 +09301007 goto out_ringbuffer_fini;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +09301008 }
Jeremy Kerrd831f962016-01-29 17:18:01 +08001009
Andrew Jeffery498a4a82024-07-09 15:07:05 +09301010 rc = dbus_init(console, config);
1011 if (rc) {
1012 goto out_tty_fini;
1013 }
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +08001014
Jeremy Kerrd47963e2016-03-16 17:29:55 +08001015 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001016
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08001017 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001018
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08001019 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001020
Andrew Jeffery498a4a82024-07-09 15:07:05 +09301021out_tty_fini:
Andrew Jeffery30ea6382023-05-03 16:56:10 +09301022 tty_fini(console);
1023
Andrew Jeffery3f8d5be2024-07-09 14:40:50 +09301024out_ringbuffer_fini:
1025 ringbuffer_fini(console->rb);
1026
Jeremy Kerrd66195c2016-03-16 17:24:51 +08001027out_config_fini:
1028 config_fini(config);
1029
Jeremy Kerr89ea8192016-03-15 17:57:43 +08001030 free(console->pollers);
1031 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08001032 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001033
1034 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
1035}