blob: fa61e7130cc6aa905aade0fb789f38899dbb7bea [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"
Alexander Hansen1e04f442024-06-12 16:35:58 +020042#include "config.h"
Jeremy Kerrd831f962016-01-29 17:18:01 +080043
Andrew Jeffery30ea6382023-05-03 16:56:10 +093044#define DEV_PTS_PATH "/dev/pts"
45
Medicine Yeh7f2bfb92024-03-10 23:19:41 -070046/* default size of the shared backlog ringbuffer */
47const size_t default_buffer_size = 128ul * 1024ul;
Jeremy Kerrf733c852017-02-07 18:40:10 +080048
Jeremy Kerr769cee12016-03-15 17:53:56 +080049/* state shared with the signal handler */
Andrew Jeffery553cb662024-07-09 15:14:56 +093050static volatile sig_atomic_t sigint;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080051
Jeremy Kerrd831f962016-01-29 17:18:01 +080052static void usage(const char *progname)
53{
54 fprintf(stderr,
Andrew Jefferya72711a2023-04-18 18:19:41 +093055 "usage: %s [options] <DEVICE>\n"
56 "\n"
57 "Options:\n"
Andrew Jeffery954be0f2023-05-03 21:01:59 +093058 " --config <FILE>\tUse FILE for configuration\n"
59 " --console-id <NAME>\tUse NAME in the UNIX domain socket address\n"
Andrew Jefferya72711a2023-04-18 18:19:41 +093060 "",
Jeremy Kerrd831f962016-01-29 17:18:01 +080061 progname);
62}
63
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +020064/* populates server->tty.dev and server->tty.sysfs_devnode, using the tty kernel name */
65static int tty_find_device(struct console_server *server)
Jeremy Kerr17217842016-01-29 18:44:21 +080066{
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +093067 char *tty_class_device_link = NULL;
68 char *tty_path_input_real = NULL;
69 char *tty_device_tty_dir = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +093070 char *tty_vuart_lpc_addr = NULL;
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +093071 char *tty_device_reldir = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +093072 char *tty_sysfs_devnode = NULL;
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +093073 char *tty_kname_real = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +093074 char *tty_path_input = NULL;
Jeremy Kerr17217842016-01-29 18:44:21 +080075 int rc;
76
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +020077 server->tty.type = TTY_DEVICE_UNDEFINED;
Andrew Jeffery30ea6382023-05-03 16:56:10 +093078
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +020079 assert(server->tty.kname);
80 if (!strlen(server->tty.kname)) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +093081 warnx("TTY kname must not be empty");
82 rc = -1;
83 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093084 }
Jeremy Kerr17217842016-01-29 18:44:21 +080085
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +020086 if (server->tty.kname[0] == '/') {
87 tty_path_input = strdup(server->tty.kname);
Andrew Jeffery30ea6382023-05-03 16:56:10 +093088 if (!tty_path_input) {
89 rc = -1;
90 goto out_free;
91 }
92 } else {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +020093 rc = asprintf(&tty_path_input, "/dev/%s", server->tty.kname);
Andrew Jeffery30ea6382023-05-03 16:56:10 +093094 if (rc < 0) {
95 goto out_free;
96 }
97 }
98
99 /* udev may rename the tty name with a symbol link, try to resolve */
Yi Li45ad7672016-10-18 23:21:19 +0800100 tty_path_input_real = realpath(tty_path_input, NULL);
101 if (!tty_path_input_real) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930102 warn("Can't find realpath for %s", tty_path_input);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930103 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800104 goto out_free;
105 }
106
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930107 /*
108 * Allow hooking obmc-console-server up to PTYs for testing
109 *
110 * https://amboar.github.io/notes/2023/05/02/testing-obmc-console-with-socat.html
111 */
112 if (!strncmp(DEV_PTS_PATH, tty_path_input_real, strlen(DEV_PTS_PATH))) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200113 server->tty.type = TTY_DEVICE_PTY;
114 server->tty.dev = strdup(server->tty.kname);
115 rc = server->tty.dev ? 0 : -1;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930116 goto out_free;
117 }
118
Yi Li45ad7672016-10-18 23:21:19 +0800119 tty_kname_real = basename(tty_path_input_real);
120 if (!tty_kname_real) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200121 warn("Can't find real name for %s", server->tty.kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930122 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800123 goto out_free;
124 }
125
Andrew Jefferya72711a2023-04-18 18:19:41 +0930126 rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s",
127 tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930128 if (rc < 0) {
Yi Li45ad7672016-10-18 23:21:19 +0800129 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930130 }
Yi Li45ad7672016-10-18 23:21:19 +0800131
Jeremy Kerr17217842016-01-29 18:44:21 +0800132 tty_device_tty_dir = realpath(tty_class_device_link, NULL);
Yi Li45ad7672016-10-18 23:21:19 +0800133 if (!tty_device_tty_dir) {
134 warn("Can't query sysfs for device %s", tty_kname_real);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930135 rc = -1;
Jeremy Kerr17217842016-01-29 18:44:21 +0800136 goto out_free;
137 }
138
139 rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930140 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800141 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930142 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800143
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930144 tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
145 if (!tty_sysfs_devnode) {
Yi Li45ad7672016-10-18 23:21:19 +0800146 warn("Can't find parent device for %s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930147 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800148
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200149 rc = asprintf(&server->tty.dev, "/dev/%s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930150 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800151 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930152 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800153
Oskar Senft955d1402023-09-27 22:43:22 -0400154 // Default to non-VUART
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200155 server->tty.type = TTY_DEVICE_UART;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930156
Oskar Senft955d1402023-09-27 22:43:22 -0400157 /* Arbitrarily pick an attribute to differentiate UART vs VUART */
158 if (tty_sysfs_devnode) {
159 rc = asprintf(&tty_vuart_lpc_addr, "%s/lpc_address",
160 tty_sysfs_devnode);
161 if (rc < 0) {
162 goto out_free;
163 }
164
165 rc = access(tty_vuart_lpc_addr, F_OK);
166 if (!rc) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200167 server->tty.type = TTY_DEVICE_VUART;
168 server->tty.vuart.sysfs_devnode =
Oskar Senft955d1402023-09-27 22:43:22 -0400169 strdup(tty_sysfs_devnode);
170 }
171 }
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930172
Jeremy Kerr17217842016-01-29 18:44:21 +0800173 rc = 0;
174
175out_free:
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930176 free(tty_vuart_lpc_addr);
Jeremy Kerr17217842016-01-29 18:44:21 +0800177 free(tty_class_device_link);
Andrew Jeffery982090d2024-07-09 17:31:27 +0930178 free(tty_sysfs_devnode);
Jeremy Kerr17217842016-01-29 18:44:21 +0800179 free(tty_device_tty_dir);
180 free(tty_device_reldir);
Yi Li45ad7672016-10-18 23:21:19 +0800181 free(tty_path_input);
182 free(tty_path_input_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800183 return rc;
184}
185
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200186static int tty_set_sysfs_attr(struct console_server *server, const char *name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930187 int value)
Jeremy Kerr957818b2016-03-08 14:35:15 +0800188{
189 char *path;
190 FILE *fp;
191 int rc;
192
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200193 assert(server->tty.type == TTY_DEVICE_VUART);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930194
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200195 if (!server->tty.vuart.sysfs_devnode) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930196 return -1;
197 }
198
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200199 rc = asprintf(&path, "%s/%s", server->tty.vuart.sysfs_devnode, name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930200 if (rc < 0) {
Jeremy Kerr957818b2016-03-08 14:35:15 +0800201 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930202 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800203
204 fp = fopen(path, "w");
205 if (!fp) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930206 warn("Can't access attribute %s on device %s", name,
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200207 server->tty.kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800208 rc = -1;
209 goto out_free;
210 }
211 setvbuf(fp, NULL, _IONBF, 0);
212
213 rc = fprintf(fp, "0x%x", value);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930214 if (rc < 0) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930215 warn("Error writing to %s attribute of device %s", name,
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200216 server->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930217 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800218 fclose(fp);
219
Jeremy Kerr957818b2016-03-08 14:35:15 +0800220out_free:
221 free(path);
222 return rc;
223}
224
Jeremy Kerrd831f962016-01-29 17:18:01 +0800225/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700226 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800227 */
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200228void tty_init_termios(struct console_server *server)
Jeremy Kerr54e95692016-03-24 17:07:56 +0800229{
230 struct termios termios;
231 int rc;
232
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200233 rc = tcgetattr(server->tty.fd, &termios);
Jeremy Kerr54e95692016-03-24 17:07:56 +0800234 if (rc) {
235 warn("Can't read tty termios");
236 return;
237 }
238
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200239 if (server->tty.type == TTY_DEVICE_UART && server->tty.uart.baud) {
240 if (cfsetspeed(&termios, server->tty.uart.baud) < 0) {
241 warn("Couldn't set speeds for %s", server->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930242 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700243 }
244
245 /* Set console to raw mode: we don't want any processing to occur on
246 * the underlying terminal input/output.
247 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800248 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700249
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200250 rc = tcsetattr(server->tty.fd, TCSANOW, &termios);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930251 if (rc) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200252 warn("Can't set terminal options for %s", server->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930253 }
Jeremy Kerr54e95692016-03-24 17:07:56 +0800254}
255
256/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800257 * Open and initialise the serial device
258 */
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200259static void tty_init_vuart_io(struct console_server *server)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800260{
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200261 assert(server->tty.type == TTY_DEVICE_VUART);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930262
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200263 if (server->tty.vuart.sirq) {
264 tty_set_sysfs_attr(server, "sirq", server->tty.vuart.sirq);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930265 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800266
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200267 if (server->tty.vuart.lpc_addr) {
268 tty_set_sysfs_attr(server, "lpc_address",
269 server->tty.vuart.lpc_addr);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930270 }
271}
272
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200273static int tty_init_io(struct console_server *server)
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930274{
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200275 server->tty.fd = open(server->tty.dev, O_RDWR);
276 if (server->tty.fd <= 0) {
277 warn("Can't open tty %s", server->tty.dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800278 return -1;
279 }
280
281 /* Disable character delay. We may want to later enable this when
282 * we detect larger amounts of data
283 */
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200284 fcntl(server->tty.fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800285
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200286 tty_init_termios(server);
Jeremy Kerr54e95692016-03-24 17:07:56 +0800287
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200288 server->active->pollfds[server->active->n_pollers].fd = server->tty.fd;
289 server->active->pollfds[server->active->n_pollers].events = POLLIN;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800290
Jeremy Kerrd831f962016-01-29 17:18:01 +0800291 return 0;
292}
293
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200294static int tty_init_vuart(struct console_server *server, struct config *config)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800295{
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930296 unsigned long parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800297 const char *val;
298 char *endp;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930299
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200300 assert(server->tty.type == TTY_DEVICE_VUART);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800301
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800302 val = config_get_value(config, "lpc-address");
303 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930304 errno = 0;
305 parsed = strtoul(val, &endp, 0);
306 if (parsed == ULONG_MAX && errno == ERANGE) {
307 warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'",
308 val);
309 return -1;
310 }
311
312 if (parsed > UINT16_MAX) {
313 warn("Invalid LPC address '%s'", val);
314 return -1;
315 }
316
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200317 server->tty.vuart.lpc_addr = (uint16_t)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800318 if (endp == optarg) {
319 warn("Invalid LPC address: '%s'", val);
320 return -1;
321 }
322 }
323
324 val = config_get_value(config, "sirq");
325 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930326 errno = 0;
327 parsed = strtoul(val, &endp, 0);
328 if (parsed == ULONG_MAX && errno == ERANGE) {
329 warn("Cannot interpret 'sirq' value as an unsigned long: '%s'",
330 val);
331 }
332
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930333 if (parsed > 16) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930334 warn("Invalid LPC SERIRQ: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930335 }
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930336
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200337 server->tty.vuart.sirq = (int)parsed;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930338 if (endp == optarg) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800339 warn("Invalid sirq: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930340 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800341 }
342
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930343 return 0;
344}
345
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200346static int tty_init(struct console_server *server, struct config *config,
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930347 const char *tty_arg)
348{
349 const char *val;
350 int rc;
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700351
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930352 if (tty_arg) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200353 server->tty.kname = tty_arg;
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930354 } else if ((val = config_get_value(config, "upstream-tty"))) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200355 server->tty.kname = val;
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930356 } else {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800357 warnx("Error: No TTY device specified");
358 return -1;
359 }
360
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200361 rc = tty_find_device(server);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930362 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800363 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930364 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800365
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200366 switch (server->tty.type) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930367 case TTY_DEVICE_VUART:
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200368 rc = tty_init_vuart(server, config);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930369 if (rc) {
370 return rc;
371 }
372
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200373 tty_init_vuart_io(server);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930374 break;
375 case TTY_DEVICE_UART:
376 val = config_get_value(config, "baud");
377 if (val) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200378 if (config_parse_baud(&server->tty.uart.baud, val)) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930379 warnx("Invalid baud rate: '%s'", val);
380 }
381 }
382 break;
383 case TTY_DEVICE_PTY:
384 break;
385 case TTY_DEVICE_UNDEFINED:
386 default:
387 warnx("Cannot configure unrecognised TTY device");
388 return -1;
389 }
390
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200391 return tty_init_io(server);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930392}
393
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200394static void tty_fini(struct console_server *server)
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930395{
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200396 if (server->tty.type == TTY_DEVICE_VUART) {
397 free(server->tty.vuart.sysfs_devnode);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930398 }
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200399 free(server->tty.dev);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800400}
401
Zev Weiss7dc08ba2023-09-12 20:49:14 -0700402static int write_to_path(const char *path, const char *data)
403{
404 int rc = 0;
405 FILE *f = fopen(path, "w");
406 if (!f) {
407 return -1;
408 }
409
410 if (fprintf(f, "%s", data) < 0) {
411 rc = -1;
412 }
413
414 if (fclose(f)) {
415 rc = -1;
416 }
417
418 return rc;
419}
420
421#define ASPEED_UART_ROUTING_PATTERN \
422 "/sys/bus/platform/drivers/aspeed-uart-routing/*.uart-routing"
423
424static void uart_routing_init(struct config *config)
425{
426 const char *muxcfg;
427 const char *p;
428 size_t buflen;
429 char *sink;
430 char *source;
431 char *muxdir;
432 char *path;
433 glob_t globbuf;
434
435 muxcfg = config_get_value(config, "aspeed-uart-routing");
436 if (!muxcfg) {
437 return;
438 }
439
440 /* Find the driver's sysfs directory */
441 if (glob(ASPEED_UART_ROUTING_PATTERN, GLOB_ERR | GLOB_NOSORT, NULL,
442 &globbuf) != 0) {
443 warn("Couldn't find uart-routing driver directory, cannot apply config");
444 return;
445 }
446 if (globbuf.gl_pathc != 1) {
447 warnx("Found %zd uart-routing driver directories, cannot apply config",
448 globbuf.gl_pathc);
449 goto out_free_glob;
450 }
451 muxdir = globbuf.gl_pathv[0];
452
453 /*
454 * Rather than faff about tracking a bunch of separate buffer sizes,
455 * just use one (worst-case) size for all of them -- +2 for a trailing
456 * NUL and a '/' separator to construct the sysfs file path.
457 */
458 buflen = strlen(muxdir) + strlen(muxcfg) + 2;
459
460 sink = malloc(buflen);
461 source = malloc(buflen);
462 path = malloc(buflen);
463 if (!path || !sink || !source) {
464 warnx("Out of memory applying uart routing config");
465 goto out_free_bufs;
466 }
467
468 p = muxcfg;
469 while (*p) {
470 ssize_t bytes_scanned;
471
472 if (sscanf(p, " %[^:/ \t]:%[^: \t] %zn", sink, source,
473 &bytes_scanned) != 2) {
474 warnx("Invalid syntax in aspeed uart config: '%s' not applied",
475 p);
476 break;
477 }
478 p += bytes_scanned;
479
480 /*
481 * Check that the sink name looks reasonable before proceeding
482 * (there are other writable files in the same directory that
483 * we shouldn't be touching, such as 'driver_override' and
484 * 'uevent').
485 */
486 if (strncmp(sink, "io", strlen("io")) != 0 &&
487 strncmp(sink, "uart", strlen("uart")) != 0) {
488 warnx("Skipping invalid uart routing name '%s' (must be ioN or uartN)",
489 sink);
490 continue;
491 }
492
493 snprintf(path, buflen, "%s/%s", muxdir, sink);
494 if (write_to_path(path, source)) {
495 warn("Failed to apply uart-routing config '%s:%s'",
496 sink, source);
497 }
498 }
499
500out_free_bufs:
501 free(path);
502 free(source);
503 free(sink);
504out_free_glob:
505 globfree(&globbuf);
506}
507
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800508int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800509{
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200510 return write_buf_to_fd(console->server->tty.fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800511}
512
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500513/* Prepare a socket name */
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930514static int set_socket_info(struct console *console, struct config *config,
515 const char *console_id)
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500516{
517 ssize_t len;
518
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500519 /* Get console id */
520 console->console_id = config_resolve_console_id(config, console_id);
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930521
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500522 /* Get the socket name/path */
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500523 len = console_socket_path(console->socket_name, console->console_id);
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500524 if (len < 0) {
525 warn("Failed to set socket path: %s", strerror(errno));
526 return EXIT_FAILURE;
527 }
528
529 /* Socket name is not a null terminated string hence save the length */
530 console->socket_name_len = len;
531
532 return 0;
533}
534
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800535static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800536{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930537 /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Andrew Jefferyadedc332024-07-10 10:48:13 +0930538 extern const struct handler_type *const __start_handlers[];
539 extern const struct handler_type *const __stop_handlers[];
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930540 /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerre2826c72024-07-05 10:54:21 +0800541 size_t n_types;
542 int j = 0;
543 size_t i;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800544
Andrew Jefferyadedc332024-07-10 10:48:13 +0930545 n_types = __stop_handlers - __start_handlers;
Jeremy Kerre2826c72024-07-05 10:54:21 +0800546 console->handlers = calloc(n_types, sizeof(struct handler *));
547 if (!console->handlers) {
548 err(EXIT_FAILURE, "malloc(handlers)");
549 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800550
Andrew Jeffery079fc512024-07-09 16:51:56 +0930551 printf("%zu handler type%s\n", n_types, n_types == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800552
Jeremy Kerre2826c72024-07-05 10:54:21 +0800553 for (i = 0; i < n_types; i++) {
Andrew Jefferyadedc332024-07-10 10:48:13 +0930554 const struct handler_type *type = __start_handlers[i];
Jeremy Kerre2826c72024-07-05 10:54:21 +0800555 struct handler *handler;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800556
Jeremy Kerre2826c72024-07-05 10:54:21 +0800557 /* Should be picked up at build time by
558 * console_handler_register, but check anyway
559 */
560 if (!type->init || !type->fini) {
561 errx(EXIT_FAILURE,
562 "invalid handler type %s: no init() / fini()",
563 type->name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930564 }
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800565
Jeremy Kerre2826c72024-07-05 10:54:21 +0800566 handler = type->init(type, console, config);
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800567
Jeremy Kerre2826c72024-07-05 10:54:21 +0800568 printf(" console '%s': handler %s [%sactive]\n",
569 console->console_id, type->name, handler ? "" : "in");
570
571 if (handler) {
572 handler->type = type;
573 console->handlers[j++] = handler;
574 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800575 }
Jeremy Kerre2826c72024-07-05 10:54:21 +0800576
577 console->n_handlers = j;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800578}
579
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800580static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800581{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800582 struct handler *handler;
583 int i;
584
585 for (i = 0; i < console->n_handlers; i++) {
586 handler = console->handlers[i];
Jeremy Kerre2826c72024-07-05 10:54:21 +0800587 handler->type->fini(handler);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800588 }
Jeremy Kerre2826c72024-07-05 10:54:21 +0800589
590 free(console->handlers);
591 console->handlers = NULL;
592 console->n_handlers = 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800593}
594
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800595static int get_current_time(struct timeval *tv)
596{
597 struct timespec t;
598 int rc;
599
600 /*
601 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
602 * local time changes. However, a struct timeval is more
603 * convenient for calculations, so convert to that.
604 */
605 rc = clock_gettime(CLOCK_MONOTONIC, &t);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930606 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800607 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930608 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800609
610 tv->tv_sec = t.tv_sec;
611 tv->tv_usec = t.tv_nsec / 1000;
612
613 return 0;
614}
615
Andrew Jefferya72711a2023-04-18 18:19:41 +0930616struct ringbuffer_consumer *
617console_ringbuffer_consumer_register(struct console *console,
618 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800619{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800620 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800621}
622
Jeremy Kerr55c97122017-02-07 17:06:46 +0800623struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930624 struct handler *handler,
625 poller_event_fn_t poller_fn,
626 poller_timeout_fn_t timeout_fn, int fd,
627 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800628{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800629 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930630 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800631
632 poller = malloc(sizeof(*poller));
633 poller->remove = false;
634 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800635 poller->event_fn = poller_fn;
636 poller->timeout_fn = timeout_fn;
Andrew Jeffery9cc84592024-07-09 15:44:30 +0930637 timerclear(&poller->timeout);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800638 poller->data = data;
639
640 /* add one to our pollers array */
641 n = console->n_pollers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930642 /*
643 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
644 * pointer type.
645 */
646 /* NOLINTBEGIN(bugprone-sizeof-expression) */
647 console->pollers = reallocarray(console->pollers, console->n_pollers,
648 sizeof(*console->pollers));
649 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800650
651 console->pollers[n] = poller;
652
653 /* increase pollfds array too */
Andrew Jeffery4e44c792023-05-12 15:57:46 +0930654 console->pollfds = reallocarray(
655 console->pollfds, (MAX_INTERNAL_POLLFD + console->n_pollers),
656 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800657
658 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930659 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
660 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800661
662 console->pollfds[n].fd = fd;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930663 console->pollfds[n].events = (short)(events & 0x7fff);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800664
665 return poller;
666}
667
Andrew Jefferya72711a2023-04-18 18:19:41 +0930668void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800669{
670 int i;
671
672 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930673 for (i = 0; i < console->n_pollers; i++) {
674 if (console->pollers[i] == poller) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800675 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930676 }
677 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800678
679 assert(i < console->n_pollers);
680
681 console->n_pollers--;
682
Andrew Jeffery91b52172023-04-19 12:42:14 +0930683 /*
684 * Remove the item from the pollers array...
685 *
686 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
687 * pointer type.
688 */
689 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930690 memmove(&console->pollers[i], &console->pollers[i + 1],
691 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800692
Andrew Jeffery7851a392024-07-09 16:30:22 +0930693 if (console->n_pollers == 0) {
694 free(console->pollers);
695 console->pollers = NULL;
696 } else {
697 console->pollers = reallocarray(console->pollers,
698 console->n_pollers,
699 sizeof(*console->pollers));
700 }
Andrew Jeffery91b52172023-04-19 12:42:14 +0930701 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800702
703 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930704 memmove(&console->pollfds[i], &console->pollfds[i + 1],
705 sizeof(*console->pollfds) *
706 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800707
Andrew Jeffery4e44c792023-05-12 15:57:46 +0930708 console->pollfds = reallocarray(
709 console->pollfds, (MAX_INTERNAL_POLLFD + console->n_pollers),
710 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800711
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800712 free(poller);
713}
714
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800715void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930716 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800717{
718 int i;
719
720 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930721 for (i = 0; i < console->n_pollers; i++) {
722 if (console->pollers[i] == poller) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800723 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930724 }
725 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800726
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930727 console->pollfds[i].events = (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800728}
729
Andrew Jefferyfd048322023-04-18 12:02:01 +0930730void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930731 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800732{
733 struct timeval now;
734 int rc;
735
736 rc = get_current_time(&now);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930737 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800738 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930739 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800740
741 timeradd(&now, tv, &poller->timeout);
742}
743
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930744static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800745{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930746 struct timeval *earliest;
747 struct timeval interval;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800748 struct poller *poller;
749 int i;
750
751 earliest = NULL;
752
753 for (i = 0; i < console->n_pollers; i++) {
754 poller = console->pollers[i];
755
756 if (poller->timeout_fn && timerisset(&poller->timeout) &&
757 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930758 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800759 // poller is buffering data and needs the poll
760 // function to timeout.
761 earliest = &poller->timeout;
762 }
763 }
764
765 if (earliest) {
766 if (timercmp(earliest, cur_time, >)) {
767 /* recalculate the timeout period, time period has
768 * not elapsed */
769 timersub(earliest, cur_time, &interval);
770 return ((interval.tv_sec * 1000) +
771 (interval.tv_usec / 1000));
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930772 } /* return from poll immediately */
773 return 0;
774
775 } /* poll indefinitely */
776 return -1;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800777}
778
779static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800780{
781 struct poller *poller;
782 struct pollfd *pollfd;
783 enum poller_ret prc;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930784 int i;
785 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800786
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800787 rc = 0;
788
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800789 /*
790 * Process poll events by iterating through the pollers and pollfds
791 * in-step, calling any pollers that we've found revents for.
792 */
793 for (i = 0; i < console->n_pollers; i++) {
794 poller = console->pollers[i];
795 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800796 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800797
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800798 /* process pending events... */
799 if (pollfd->revents) {
800 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930801 poller->data);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930802 if (prc == POLLER_EXIT) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800803 rc = -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930804 } else if (prc == POLLER_REMOVE) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800805 poller->remove = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930806 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800807 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800808
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800809 if ((prc == POLLER_OK) && poller->timeout_fn &&
810 timerisset(&poller->timeout) &&
811 timercmp(&poller->timeout, cur_time, <=)) {
812 /* One of the ringbuffer consumers is buffering the
813 data stream. The amount of idle time the consumer
814 desired has expired. Process the buffered data for
815 transmission. */
816 timerclear(&poller->timeout);
817 prc = poller->timeout_fn(poller->handler, poller->data);
818 if (prc == POLLER_EXIT) {
819 rc = -1;
820 } else if (prc == POLLER_REMOVE) {
821 poller->remove = true;
822 }
823 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800824 }
825
826 /**
827 * Process deferred removals; restarting each time we unregister, as
828 * the array will have changed
829 */
830 for (;;) {
831 bool removed = false;
832
833 for (i = 0; i < console->n_pollers; i++) {
834 poller = console->pollers[i];
835 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800836 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800837 removed = true;
838 break;
839 }
840 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930841 if (!removed) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800842 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930843 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800844 }
845
846 return rc;
847}
848
Jeremy Kerr769cee12016-03-15 17:53:56 +0800849static void sighandler(int signal)
850{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930851 if (signal == SIGINT) {
Andrew Jeffery553cb662024-07-09 15:14:56 +0930852 sigint = 1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930853 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800854}
855
Alexander Hansen69257402024-06-07 15:17:09 +0200856static int run_console_iteration(struct console *console)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800857{
Alexander Hansen69257402024-06-07 15:17:09 +0200858 uint8_t buf[4096];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800859 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930860 long timeout;
861 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800862
Alexander Hansen69257402024-06-07 15:17:09 +0200863 if (console->rb->size < sizeof(buf)) {
864 fprintf(stderr, "Ringbuffer size should be greater than %zuB\n",
865 sizeof(buf));
866 return -1;
867 }
868
869 if (sigint) {
870 fprintf(stderr, "Received interrupt, exiting\n");
871 return -1;
872 }
873
874 rc = get_current_time(&tv);
875 if (rc) {
876 warn("Failed to read current time");
877 return -1;
878 }
879
880 timeout = get_poll_timeout(console, &tv);
881
882 rc = poll(console->pollfds, console->n_pollers + MAX_INTERNAL_POLLFD,
883 (int)timeout);
884
885 if (rc < 0) {
886 if (errno == EINTR) {
887 return 0;
888 }
889 warn("poll error");
890 return -1;
891 }
892
893 /* process internal fd first */
894 if (console->pollfds[console->n_pollers].revents) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200895 rc = read(console->server->tty.fd, buf, sizeof(buf));
Alexander Hansen69257402024-06-07 15:17:09 +0200896 if (rc <= 0) {
897 warn("Error reading from tty device");
898 return -1;
899 }
900 rc = ringbuffer_queue(console->rb, buf, rc);
901 if (rc) {
902 return -1;
903 }
904 }
905
906 if (console->pollfds[console->n_pollers + 1].revents) {
907 sd_bus_process(console->bus, NULL);
908 }
909
910 /* ... and then the pollers */
911 rc = call_pollers(console, &tv);
912 if (rc) {
913 return -1;
914 }
915 return 0;
916}
917
918int run_console(struct console *console)
919{
920 sighandler_t sighandler_save = signal(SIGINT, sighandler);
921 ssize_t rc = 0;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800922
Jeremy Kerrd831f962016-01-29 17:18:01 +0800923 for (;;) {
Alexander Hansen69257402024-06-07 15:17:09 +0200924 rc = run_console_iteration(console);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930925 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800926 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930927 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800928 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800929
930 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800931 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800932
933 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800934}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800935static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930936 { "config", required_argument, 0, 'c' },
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930937 { "console-id", required_argument, 0, 'i' },
Andrew Jefferya72711a2023-04-18 18:19:41 +0930938 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800939};
940
941int main(int argc, char **argv)
942{
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700943 size_t buffer_size = default_buffer_size;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800944 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500945 const char *config_tty_kname = NULL;
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700946 const char *buffer_size_str = NULL;
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930947 const char *console_id = NULL;
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200948 struct console_server server = { 0 };
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800949 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800950 struct config *config;
951 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800952
Jeremy Kerrd831f962016-01-29 17:18:01 +0800953 for (;;) {
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930954 int c;
955 int idx;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800956
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930957 c = getopt_long(argc, argv, "c:i:", options, &idx);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930958 if (c == -1) {
Jeremy Kerrd831f962016-01-29 17:18:01 +0800959 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930960 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800961
962 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800963 case 'c':
964 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800965 break;
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930966 case 'i':
967 console_id = optarg;
968 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800969 case 'h':
970 case '?':
971 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800972 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800973 }
974 }
975
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930976 if (optind < argc) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030977 config_tty_kname = argv[optind];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930978 }
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500979
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700980 config = config_init(config_filename);
Andrew Jeffery28a17612024-07-10 09:32:05 +0930981 if (!config) {
982 return EXIT_FAILURE;
983 }
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700984
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800985 console = malloc(sizeof(struct console));
Andrew Jeffery28a17612024-07-10 09:32:05 +0930986 if (!console) {
987 rc = -1;
988 goto out_config_fini;
989 }
990
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800991 memset(console, 0, sizeof(*console));
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200992
993 server.active = console;
994 console->server = &server;
995
Andrew Jefferya72711a2023-04-18 18:19:41 +0930996 console->pollfds =
997 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Andrew Jeffery28a17612024-07-10 09:32:05 +0930998
Medicine Yeh7f2bfb92024-03-10 23:19:41 -0700999 buffer_size_str = config_get_value(config, "ringbuffer-size");
1000 if (buffer_size_str) {
1001 rc = config_parse_bytesize(buffer_size_str, &buffer_size);
1002 if (rc) {
1003 warn("Invalid ringbuffer-size. Default to %zukB",
1004 buffer_size >> 10);
1005 }
1006 }
Jeremy Kerrf733c852017-02-07 18:40:10 +08001007 console->rb = ringbuffer_init(buffer_size);
Andrew Jeffery3f8d5be2024-07-09 14:40:50 +09301008 if (!console->rb) {
1009 rc = -1;
Andrew Jeffery28a17612024-07-10 09:32:05 +09301010 goto out_console_fini;
Andrew Jeffery3f8d5be2024-07-09 14:40:50 +09301011 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +08001012
Andrew Jeffery954be0f2023-05-03 21:01:59 +09301013 if (set_socket_info(console, config, console_id)) {
Andrew Jeffery29c59c42023-05-03 19:33:07 +09301014 rc = -1;
Andrew Jeffery3f8d5be2024-07-09 14:40:50 +09301015 goto out_ringbuffer_fini;
Ninad Palsuleb14ca192023-03-31 09:49:00 -05001016 }
1017
Zev Weiss7dc08ba2023-09-12 20:49:14 -07001018 uart_routing_init(config);
1019
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +02001020 rc = tty_init(&server, config, config_tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +09301021 if (rc) {
Andrew Jeffery3f8d5be2024-07-09 14:40:50 +09301022 goto out_ringbuffer_fini;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +09301023 }
Jeremy Kerrd831f962016-01-29 17:18:01 +08001024
Andrew Jeffery498a4a82024-07-09 15:07:05 +09301025 rc = dbus_init(console, config);
1026 if (rc) {
1027 goto out_tty_fini;
1028 }
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +08001029
Jeremy Kerrd47963e2016-03-16 17:29:55 +08001030 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001031
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08001032 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001033
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08001034 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001035
Andrew Jeffery498a4a82024-07-09 15:07:05 +09301036out_tty_fini:
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +02001037 tty_fini(&server);
Andrew Jeffery30ea6382023-05-03 16:56:10 +09301038
Andrew Jeffery3f8d5be2024-07-09 14:40:50 +09301039out_ringbuffer_fini:
1040 ringbuffer_fini(console->rb);
1041
Andrew Jeffery28a17612024-07-10 09:32:05 +09301042out_console_fini:
Jeremy Kerr89ea8192016-03-15 17:57:43 +08001043 free(console->pollers);
1044 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08001045 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001046
Andrew Jeffery28a17612024-07-10 09:32:05 +09301047out_config_fini:
1048 config_fini(config);
1049
Jeremy Kerrd831f962016-01-29 17:18:01 +08001050 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
1051}