blob: b7a046bf69b54f77bd0193127a82369ef5fce1a9 [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 Hansen2325d5d2024-06-07 16:54:20 +020064static bool console_server_pollfd_reclaimable(struct pollfd *p)
65{
66 return p->fd == -1 && p->events == 0 && p->revents == ~0;
67}
68
69static ssize_t
70console_server_find_released_pollfd(struct console_server *server)
71{
72 for (size_t i = 0; i < server->capacity_pollfds; i++) {
73 struct pollfd *p = &server->pollfds[i];
74 if (console_server_pollfd_reclaimable(p)) {
75 return (ssize_t)i;
76 }
77 }
78 return -1;
79}
80
81// returns the index of that pollfd in server->pollfds
82// we cannot return a pointer because 'realloc' may move server->pollfds
83ssize_t console_server_request_pollfd(struct console_server *server, int fd,
84 short int events)
85{
86 ssize_t index;
87 struct pollfd *pollfd;
88
89 index = console_server_find_released_pollfd(server);
90
91 if (index < 0) {
92 const size_t newcap = server->capacity_pollfds + 1;
93
94 struct pollfd *newarr = reallocarray(server->pollfds, newcap,
95 sizeof(struct pollfd));
96 if (newarr == NULL) {
97 return -1;
98 }
99 server->pollfds = newarr;
100
101 index = (ssize_t)server->capacity_pollfds;
102
103 server->capacity_pollfds = newcap;
104 }
105
106 pollfd = &server->pollfds[index];
107 pollfd->fd = fd;
108 pollfd->events = events;
109 pollfd->revents = 0;
110
111 return index;
112}
113
114int console_server_release_pollfd(struct console_server *server,
115 size_t pollfd_index)
116{
117 if (pollfd_index >= server->capacity_pollfds) {
118 return -1;
119 }
120
121 struct pollfd *pfd = &server->pollfds[pollfd_index];
122
123 // mark pollfd as reclaimable
124
125 // ignore this file descriptor when calling 'poll'
126 // https://www.man7.org/linux/man-pages/man2/poll.2.html
127 pfd->fd = -1;
128 pfd->events = 0;
129 pfd->revents = ~0;
130
131 return 0;
132}
133
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200134/* populates server->tty.dev and server->tty.sysfs_devnode, using the tty kernel name */
135static int tty_find_device(struct console_server *server)
Jeremy Kerr17217842016-01-29 18:44:21 +0800136{
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +0930137 char *tty_class_device_link = NULL;
138 char *tty_path_input_real = NULL;
139 char *tty_device_tty_dir = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930140 char *tty_vuart_lpc_addr = NULL;
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +0930141 char *tty_device_reldir = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930142 char *tty_sysfs_devnode = NULL;
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +0930143 char *tty_kname_real = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930144 char *tty_path_input = NULL;
Jeremy Kerr17217842016-01-29 18:44:21 +0800145 int rc;
146
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200147 server->tty.type = TTY_DEVICE_UNDEFINED;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930148
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200149 assert(server->tty.kname);
150 if (!strlen(server->tty.kname)) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930151 warnx("TTY kname must not be empty");
152 rc = -1;
153 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930154 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800155
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200156 if (server->tty.kname[0] == '/') {
157 tty_path_input = strdup(server->tty.kname);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930158 if (!tty_path_input) {
159 rc = -1;
160 goto out_free;
161 }
162 } else {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200163 rc = asprintf(&tty_path_input, "/dev/%s", server->tty.kname);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930164 if (rc < 0) {
165 goto out_free;
166 }
167 }
168
169 /* udev may rename the tty name with a symbol link, try to resolve */
Yi Li45ad7672016-10-18 23:21:19 +0800170 tty_path_input_real = realpath(tty_path_input, NULL);
171 if (!tty_path_input_real) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930172 warn("Can't find realpath for %s", tty_path_input);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930173 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800174 goto out_free;
175 }
176
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930177 /*
178 * Allow hooking obmc-console-server up to PTYs for testing
179 *
180 * https://amboar.github.io/notes/2023/05/02/testing-obmc-console-with-socat.html
181 */
182 if (!strncmp(DEV_PTS_PATH, tty_path_input_real, strlen(DEV_PTS_PATH))) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200183 server->tty.type = TTY_DEVICE_PTY;
184 server->tty.dev = strdup(server->tty.kname);
185 rc = server->tty.dev ? 0 : -1;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930186 goto out_free;
187 }
188
Yi Li45ad7672016-10-18 23:21:19 +0800189 tty_kname_real = basename(tty_path_input_real);
190 if (!tty_kname_real) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200191 warn("Can't find real name for %s", server->tty.kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930192 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800193 goto out_free;
194 }
195
Andrew Jefferya72711a2023-04-18 18:19:41 +0930196 rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s",
197 tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930198 if (rc < 0) {
Yi Li45ad7672016-10-18 23:21:19 +0800199 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930200 }
Yi Li45ad7672016-10-18 23:21:19 +0800201
Jeremy Kerr17217842016-01-29 18:44:21 +0800202 tty_device_tty_dir = realpath(tty_class_device_link, NULL);
Yi Li45ad7672016-10-18 23:21:19 +0800203 if (!tty_device_tty_dir) {
204 warn("Can't query sysfs for device %s", tty_kname_real);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930205 rc = -1;
Jeremy Kerr17217842016-01-29 18:44:21 +0800206 goto out_free;
207 }
208
209 rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930210 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800211 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930212 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800213
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930214 tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
215 if (!tty_sysfs_devnode) {
Yi Li45ad7672016-10-18 23:21:19 +0800216 warn("Can't find parent device for %s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930217 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800218
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200219 rc = asprintf(&server->tty.dev, "/dev/%s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930220 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800221 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930222 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800223
Oskar Senft955d1402023-09-27 22:43:22 -0400224 // Default to non-VUART
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200225 server->tty.type = TTY_DEVICE_UART;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930226
Oskar Senft955d1402023-09-27 22:43:22 -0400227 /* Arbitrarily pick an attribute to differentiate UART vs VUART */
228 if (tty_sysfs_devnode) {
229 rc = asprintf(&tty_vuart_lpc_addr, "%s/lpc_address",
230 tty_sysfs_devnode);
231 if (rc < 0) {
232 goto out_free;
233 }
234
235 rc = access(tty_vuart_lpc_addr, F_OK);
236 if (!rc) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200237 server->tty.type = TTY_DEVICE_VUART;
238 server->tty.vuart.sysfs_devnode =
Oskar Senft955d1402023-09-27 22:43:22 -0400239 strdup(tty_sysfs_devnode);
240 }
241 }
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930242
Jeremy Kerr17217842016-01-29 18:44:21 +0800243 rc = 0;
244
245out_free:
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930246 free(tty_vuart_lpc_addr);
Jeremy Kerr17217842016-01-29 18:44:21 +0800247 free(tty_class_device_link);
Andrew Jeffery982090d2024-07-09 17:31:27 +0930248 free(tty_sysfs_devnode);
Jeremy Kerr17217842016-01-29 18:44:21 +0800249 free(tty_device_tty_dir);
250 free(tty_device_reldir);
Yi Li45ad7672016-10-18 23:21:19 +0800251 free(tty_path_input);
252 free(tty_path_input_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800253 return rc;
254}
255
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200256static int tty_set_sysfs_attr(struct console_server *server, const char *name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930257 int value)
Jeremy Kerr957818b2016-03-08 14:35:15 +0800258{
259 char *path;
260 FILE *fp;
261 int rc;
262
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200263 assert(server->tty.type == TTY_DEVICE_VUART);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930264
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200265 if (!server->tty.vuart.sysfs_devnode) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930266 return -1;
267 }
268
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200269 rc = asprintf(&path, "%s/%s", server->tty.vuart.sysfs_devnode, name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930270 if (rc < 0) {
Jeremy Kerr957818b2016-03-08 14:35:15 +0800271 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930272 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800273
274 fp = fopen(path, "w");
275 if (!fp) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930276 warn("Can't access attribute %s on device %s", name,
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200277 server->tty.kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800278 rc = -1;
279 goto out_free;
280 }
281 setvbuf(fp, NULL, _IONBF, 0);
282
283 rc = fprintf(fp, "0x%x", value);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930284 if (rc < 0) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930285 warn("Error writing to %s attribute of device %s", name,
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200286 server->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930287 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800288 fclose(fp);
289
Jeremy Kerr957818b2016-03-08 14:35:15 +0800290out_free:
291 free(path);
292 return rc;
293}
294
Jeremy Kerrd831f962016-01-29 17:18:01 +0800295/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700296 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800297 */
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200298void tty_init_termios(struct console_server *server)
Jeremy Kerr54e95692016-03-24 17:07:56 +0800299{
300 struct termios termios;
301 int rc;
302
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200303 rc = tcgetattr(server->tty.fd, &termios);
Jeremy Kerr54e95692016-03-24 17:07:56 +0800304 if (rc) {
305 warn("Can't read tty termios");
306 return;
307 }
308
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200309 if (server->tty.type == TTY_DEVICE_UART && server->tty.uart.baud) {
310 if (cfsetspeed(&termios, server->tty.uart.baud) < 0) {
311 warn("Couldn't set speeds for %s", server->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930312 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700313 }
314
315 /* Set console to raw mode: we don't want any processing to occur on
316 * the underlying terminal input/output.
317 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800318 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700319
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200320 rc = tcsetattr(server->tty.fd, TCSANOW, &termios);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930321 if (rc) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200322 warn("Can't set terminal options for %s", server->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930323 }
Jeremy Kerr54e95692016-03-24 17:07:56 +0800324}
325
326/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800327 * Open and initialise the serial device
328 */
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200329static void tty_init_vuart_io(struct console_server *server)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800330{
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200331 assert(server->tty.type == TTY_DEVICE_VUART);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930332
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200333 if (server->tty.vuart.sirq) {
334 tty_set_sysfs_attr(server, "sirq", server->tty.vuart.sirq);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930335 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800336
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200337 if (server->tty.vuart.lpc_addr) {
338 tty_set_sysfs_attr(server, "lpc_address",
339 server->tty.vuart.lpc_addr);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930340 }
341}
342
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200343static int tty_init_io(struct console_server *server)
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930344{
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200345 server->tty.fd = open(server->tty.dev, O_RDWR);
346 if (server->tty.fd <= 0) {
347 warn("Can't open tty %s", server->tty.dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800348 return -1;
349 }
350
351 /* Disable character delay. We may want to later enable this when
352 * we detect larger amounts of data
353 */
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200354 fcntl(server->tty.fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800355
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200356 tty_init_termios(server);
Jeremy Kerr54e95692016-03-24 17:07:56 +0800357
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200358 ssize_t index =
359 console_server_request_pollfd(server, server->tty.fd, POLLIN);
360
361 if (index < 0) {
362 return -1;
363 }
364
365 server->tty_pollfd_index = (size_t)index;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800366
Jeremy Kerrd831f962016-01-29 17:18:01 +0800367 return 0;
368}
369
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200370static int tty_init_vuart(struct console_server *server, struct config *config)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800371{
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930372 unsigned long parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800373 const char *val;
374 char *endp;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930375
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200376 assert(server->tty.type == TTY_DEVICE_VUART);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800377
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800378 val = config_get_value(config, "lpc-address");
379 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930380 errno = 0;
381 parsed = strtoul(val, &endp, 0);
382 if (parsed == ULONG_MAX && errno == ERANGE) {
383 warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'",
384 val);
385 return -1;
386 }
387
388 if (parsed > UINT16_MAX) {
389 warn("Invalid LPC address '%s'", val);
390 return -1;
391 }
392
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200393 server->tty.vuart.lpc_addr = (uint16_t)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800394 if (endp == optarg) {
395 warn("Invalid LPC address: '%s'", val);
396 return -1;
397 }
398 }
399
400 val = config_get_value(config, "sirq");
401 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930402 errno = 0;
403 parsed = strtoul(val, &endp, 0);
404 if (parsed == ULONG_MAX && errno == ERANGE) {
405 warn("Cannot interpret 'sirq' value as an unsigned long: '%s'",
406 val);
407 }
408
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930409 if (parsed > 16) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930410 warn("Invalid LPC SERIRQ: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930411 }
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930412
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200413 server->tty.vuart.sirq = (int)parsed;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930414 if (endp == optarg) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800415 warn("Invalid sirq: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930416 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800417 }
418
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930419 return 0;
420}
421
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200422static int tty_init(struct console_server *server, struct config *config,
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930423 const char *tty_arg)
424{
425 const char *val;
426 int rc;
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700427
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930428 if (tty_arg) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200429 server->tty.kname = tty_arg;
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930430 } else if ((val = config_get_value(config, "upstream-tty"))) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200431 server->tty.kname = val;
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930432 } else {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800433 warnx("Error: No TTY device specified");
434 return -1;
435 }
436
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200437 rc = tty_find_device(server);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930438 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800439 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930440 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800441
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200442 switch (server->tty.type) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930443 case TTY_DEVICE_VUART:
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200444 rc = tty_init_vuart(server, config);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930445 if (rc) {
446 return rc;
447 }
448
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200449 tty_init_vuart_io(server);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930450 break;
451 case TTY_DEVICE_UART:
452 val = config_get_value(config, "baud");
453 if (val) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200454 if (config_parse_baud(&server->tty.uart.baud, val)) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930455 warnx("Invalid baud rate: '%s'", val);
456 }
457 }
458 break;
459 case TTY_DEVICE_PTY:
460 break;
461 case TTY_DEVICE_UNDEFINED:
462 default:
463 warnx("Cannot configure unrecognised TTY device");
464 return -1;
465 }
466
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200467 return tty_init_io(server);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930468}
469
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200470static void tty_fini(struct console_server *server)
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930471{
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200472 if (server->tty_pollfd_index < server->capacity_pollfds) {
473 console_server_release_pollfd(server, server->tty_pollfd_index);
474 server->tty_pollfd_index = SIZE_MAX;
475 }
476
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200477 if (server->tty.type == TTY_DEVICE_VUART) {
478 free(server->tty.vuart.sysfs_devnode);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930479 }
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200480
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200481 free(server->tty.dev);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800482}
483
Zev Weiss7dc08ba2023-09-12 20:49:14 -0700484static int write_to_path(const char *path, const char *data)
485{
486 int rc = 0;
487 FILE *f = fopen(path, "w");
488 if (!f) {
489 return -1;
490 }
491
492 if (fprintf(f, "%s", data) < 0) {
493 rc = -1;
494 }
495
496 if (fclose(f)) {
497 rc = -1;
498 }
499
500 return rc;
501}
502
503#define ASPEED_UART_ROUTING_PATTERN \
504 "/sys/bus/platform/drivers/aspeed-uart-routing/*.uart-routing"
505
506static void uart_routing_init(struct config *config)
507{
508 const char *muxcfg;
509 const char *p;
510 size_t buflen;
511 char *sink;
512 char *source;
513 char *muxdir;
514 char *path;
515 glob_t globbuf;
516
517 muxcfg = config_get_value(config, "aspeed-uart-routing");
518 if (!muxcfg) {
519 return;
520 }
521
522 /* Find the driver's sysfs directory */
523 if (glob(ASPEED_UART_ROUTING_PATTERN, GLOB_ERR | GLOB_NOSORT, NULL,
524 &globbuf) != 0) {
525 warn("Couldn't find uart-routing driver directory, cannot apply config");
526 return;
527 }
528 if (globbuf.gl_pathc != 1) {
529 warnx("Found %zd uart-routing driver directories, cannot apply config",
530 globbuf.gl_pathc);
531 goto out_free_glob;
532 }
533 muxdir = globbuf.gl_pathv[0];
534
535 /*
536 * Rather than faff about tracking a bunch of separate buffer sizes,
537 * just use one (worst-case) size for all of them -- +2 for a trailing
538 * NUL and a '/' separator to construct the sysfs file path.
539 */
540 buflen = strlen(muxdir) + strlen(muxcfg) + 2;
541
542 sink = malloc(buflen);
543 source = malloc(buflen);
544 path = malloc(buflen);
545 if (!path || !sink || !source) {
546 warnx("Out of memory applying uart routing config");
547 goto out_free_bufs;
548 }
549
550 p = muxcfg;
551 while (*p) {
552 ssize_t bytes_scanned;
553
554 if (sscanf(p, " %[^:/ \t]:%[^: \t] %zn", sink, source,
555 &bytes_scanned) != 2) {
556 warnx("Invalid syntax in aspeed uart config: '%s' not applied",
557 p);
558 break;
559 }
560 p += bytes_scanned;
561
562 /*
563 * Check that the sink name looks reasonable before proceeding
564 * (there are other writable files in the same directory that
565 * we shouldn't be touching, such as 'driver_override' and
566 * 'uevent').
567 */
568 if (strncmp(sink, "io", strlen("io")) != 0 &&
569 strncmp(sink, "uart", strlen("uart")) != 0) {
570 warnx("Skipping invalid uart routing name '%s' (must be ioN or uartN)",
571 sink);
572 continue;
573 }
574
575 snprintf(path, buflen, "%s/%s", muxdir, sink);
576 if (write_to_path(path, source)) {
577 warn("Failed to apply uart-routing config '%s:%s'",
578 sink, source);
579 }
580 }
581
582out_free_bufs:
583 free(path);
584 free(source);
585 free(sink);
586out_free_glob:
587 globfree(&globbuf);
588}
589
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800590int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800591{
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200592 return write_buf_to_fd(console->server->tty.fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800593}
594
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500595/* Prepare a socket name */
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930596static int set_socket_info(struct console *console, struct config *config,
597 const char *console_id)
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500598{
599 ssize_t len;
600
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500601 /* Get console id */
602 console->console_id = config_resolve_console_id(config, console_id);
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930603
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500604 /* Get the socket name/path */
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500605 len = console_socket_path(console->socket_name, console->console_id);
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500606 if (len < 0) {
607 warn("Failed to set socket path: %s", strerror(errno));
608 return EXIT_FAILURE;
609 }
610
611 /* Socket name is not a null terminated string hence save the length */
612 console->socket_name_len = len;
613
614 return 0;
615}
616
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800617static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800618{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930619 /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Andrew Jefferyadedc332024-07-10 10:48:13 +0930620 extern const struct handler_type *const __start_handlers[];
621 extern const struct handler_type *const __stop_handlers[];
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930622 /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerre2826c72024-07-05 10:54:21 +0800623 size_t n_types;
624 int j = 0;
625 size_t i;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800626
Andrew Jefferyadedc332024-07-10 10:48:13 +0930627 n_types = __stop_handlers - __start_handlers;
Jeremy Kerre2826c72024-07-05 10:54:21 +0800628 console->handlers = calloc(n_types, sizeof(struct handler *));
629 if (!console->handlers) {
630 err(EXIT_FAILURE, "malloc(handlers)");
631 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800632
Andrew Jeffery079fc512024-07-09 16:51:56 +0930633 printf("%zu handler type%s\n", n_types, n_types == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800634
Jeremy Kerre2826c72024-07-05 10:54:21 +0800635 for (i = 0; i < n_types; i++) {
Andrew Jefferyadedc332024-07-10 10:48:13 +0930636 const struct handler_type *type = __start_handlers[i];
Jeremy Kerre2826c72024-07-05 10:54:21 +0800637 struct handler *handler;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800638
Jeremy Kerre2826c72024-07-05 10:54:21 +0800639 /* Should be picked up at build time by
640 * console_handler_register, but check anyway
641 */
642 if (!type->init || !type->fini) {
643 errx(EXIT_FAILURE,
644 "invalid handler type %s: no init() / fini()",
645 type->name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930646 }
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800647
Jeremy Kerre2826c72024-07-05 10:54:21 +0800648 handler = type->init(type, console, config);
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800649
Jeremy Kerre2826c72024-07-05 10:54:21 +0800650 printf(" console '%s': handler %s [%sactive]\n",
651 console->console_id, type->name, handler ? "" : "in");
652
653 if (handler) {
654 handler->type = type;
655 console->handlers[j++] = handler;
656 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800657 }
Jeremy Kerre2826c72024-07-05 10:54:21 +0800658
659 console->n_handlers = j;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800660}
661
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800662static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800663{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800664 struct handler *handler;
665 int i;
666
667 for (i = 0; i < console->n_handlers; i++) {
668 handler = console->handlers[i];
Jeremy Kerre2826c72024-07-05 10:54:21 +0800669 handler->type->fini(handler);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800670 }
Jeremy Kerre2826c72024-07-05 10:54:21 +0800671
672 free(console->handlers);
673 console->handlers = NULL;
674 console->n_handlers = 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800675}
676
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800677static int get_current_time(struct timeval *tv)
678{
679 struct timespec t;
680 int rc;
681
682 /*
683 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
684 * local time changes. However, a struct timeval is more
685 * convenient for calculations, so convert to that.
686 */
687 rc = clock_gettime(CLOCK_MONOTONIC, &t);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930688 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800689 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930690 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800691
692 tv->tv_sec = t.tv_sec;
693 tv->tv_usec = t.tv_nsec / 1000;
694
695 return 0;
696}
697
Andrew Jefferya72711a2023-04-18 18:19:41 +0930698struct ringbuffer_consumer *
699console_ringbuffer_consumer_register(struct console *console,
700 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800701{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800702 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800703}
704
Jeremy Kerr55c97122017-02-07 17:06:46 +0800705struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930706 struct handler *handler,
707 poller_event_fn_t poller_fn,
708 poller_timeout_fn_t timeout_fn, int fd,
709 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800710{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800711 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930712 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800713
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200714 const ssize_t index = console_server_request_pollfd(
715 console->server, fd, (short)(events & 0x7fff));
716 if (index < 0) {
717 fprintf(stderr, "Error requesting pollfd\n");
718 return NULL;
719 }
720
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800721 poller = malloc(sizeof(*poller));
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200722 // TODO: check for error case of malloc here and release previously requested pollfd
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800723 poller->remove = false;
724 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800725 poller->event_fn = poller_fn;
726 poller->timeout_fn = timeout_fn;
Andrew Jeffery9cc84592024-07-09 15:44:30 +0930727 timerclear(&poller->timeout);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800728 poller->data = data;
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200729 poller->pollfd_index = index;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800730
731 /* add one to our pollers array */
732 n = console->n_pollers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930733 /*
734 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
735 * pointer type.
736 */
737 /* NOLINTBEGIN(bugprone-sizeof-expression) */
738 console->pollers = reallocarray(console->pollers, console->n_pollers,
739 sizeof(*console->pollers));
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200740 // TODO: check for the error case of reallocarray and release previously requested pollfd
Andrew Jeffery91b52172023-04-19 12:42:14 +0930741 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800742
743 console->pollers[n] = poller;
744
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800745 return poller;
746}
747
Andrew Jefferya72711a2023-04-18 18:19:41 +0930748void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800749{
750 int i;
751
752 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930753 for (i = 0; i < console->n_pollers; i++) {
754 if (console->pollers[i] == poller) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800755 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930756 }
757 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800758
759 assert(i < console->n_pollers);
760
761 console->n_pollers--;
762
Andrew Jeffery91b52172023-04-19 12:42:14 +0930763 /*
764 * Remove the item from the pollers array...
765 *
766 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
767 * pointer type.
768 */
769 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930770 memmove(&console->pollers[i], &console->pollers[i + 1],
771 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800772
Andrew Jeffery7851a392024-07-09 16:30:22 +0930773 if (console->n_pollers == 0) {
774 free(console->pollers);
775 console->pollers = NULL;
776 } else {
777 console->pollers = reallocarray(console->pollers,
778 console->n_pollers,
779 sizeof(*console->pollers));
780 }
Andrew Jeffery91b52172023-04-19 12:42:14 +0930781 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800782
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200783 console_server_release_pollfd(console->server, poller->pollfd_index);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800784
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800785 free(poller);
786}
787
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800788void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930789 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800790{
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200791 console->server->pollfds[poller->pollfd_index].events =
792 (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800793}
794
Andrew Jefferyfd048322023-04-18 12:02:01 +0930795void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930796 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800797{
798 struct timeval now;
799 int rc;
800
801 rc = get_current_time(&now);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930802 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800803 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930804 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800805
806 timeradd(&now, tv, &poller->timeout);
807}
808
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930809static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800810{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930811 struct timeval *earliest;
812 struct timeval interval;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800813 struct poller *poller;
814 int i;
815
816 earliest = NULL;
817
818 for (i = 0; i < console->n_pollers; i++) {
819 poller = console->pollers[i];
820
821 if (poller->timeout_fn && timerisset(&poller->timeout) &&
822 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930823 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800824 // poller is buffering data and needs the poll
825 // function to timeout.
826 earliest = &poller->timeout;
827 }
828 }
829
830 if (earliest) {
831 if (timercmp(earliest, cur_time, >)) {
832 /* recalculate the timeout period, time period has
833 * not elapsed */
834 timersub(earliest, cur_time, &interval);
835 return ((interval.tv_sec * 1000) +
836 (interval.tv_usec / 1000));
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930837 } /* return from poll immediately */
838 return 0;
839
840 } /* poll indefinitely */
841 return -1;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800842}
843
844static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800845{
846 struct poller *poller;
847 struct pollfd *pollfd;
848 enum poller_ret prc;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930849 int i;
850 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800851
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800852 rc = 0;
853
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800854 /*
855 * Process poll events by iterating through the pollers and pollfds
856 * in-step, calling any pollers that we've found revents for.
857 */
858 for (i = 0; i < console->n_pollers; i++) {
859 poller = console->pollers[i];
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200860 pollfd = &console->server->pollfds[poller->pollfd_index];
861 if (pollfd->fd < 0) {
862 // pollfd has already been released
863 continue;
864 }
865
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800866 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800867
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800868 /* process pending events... */
869 if (pollfd->revents) {
870 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930871 poller->data);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930872 if (prc == POLLER_EXIT) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800873 rc = -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930874 } else if (prc == POLLER_REMOVE) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800875 poller->remove = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930876 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800877 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800878
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800879 if ((prc == POLLER_OK) && poller->timeout_fn &&
880 timerisset(&poller->timeout) &&
881 timercmp(&poller->timeout, cur_time, <=)) {
882 /* One of the ringbuffer consumers is buffering the
883 data stream. The amount of idle time the consumer
884 desired has expired. Process the buffered data for
885 transmission. */
886 timerclear(&poller->timeout);
887 prc = poller->timeout_fn(poller->handler, poller->data);
888 if (prc == POLLER_EXIT) {
889 rc = -1;
890 } else if (prc == POLLER_REMOVE) {
891 poller->remove = true;
892 }
893 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800894 }
895
896 /**
897 * Process deferred removals; restarting each time we unregister, as
898 * the array will have changed
899 */
900 for (;;) {
901 bool removed = false;
902
903 for (i = 0; i < console->n_pollers; i++) {
904 poller = console->pollers[i];
905 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800906 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800907 removed = true;
908 break;
909 }
910 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930911 if (!removed) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800912 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930913 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800914 }
915
916 return rc;
917}
918
Jeremy Kerr769cee12016-03-15 17:53:56 +0800919static void sighandler(int signal)
920{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930921 if (signal == SIGINT) {
Andrew Jeffery553cb662024-07-09 15:14:56 +0930922 sigint = 1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930923 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800924}
925
Alexander Hansen69257402024-06-07 15:17:09 +0200926static int run_console_iteration(struct console *console)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800927{
Alexander Hansen69257402024-06-07 15:17:09 +0200928 uint8_t buf[4096];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800929 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930930 long timeout;
931 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800932
Alexander Hansen69257402024-06-07 15:17:09 +0200933 if (console->rb->size < sizeof(buf)) {
934 fprintf(stderr, "Ringbuffer size should be greater than %zuB\n",
935 sizeof(buf));
936 return -1;
937 }
938
939 if (sigint) {
940 fprintf(stderr, "Received interrupt, exiting\n");
941 return -1;
942 }
943
944 rc = get_current_time(&tv);
945 if (rc) {
946 warn("Failed to read current time");
947 return -1;
948 }
949
950 timeout = get_poll_timeout(console, &tv);
951
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200952 rc = poll(console->server->pollfds, console->server->capacity_pollfds,
Alexander Hansen69257402024-06-07 15:17:09 +0200953 (int)timeout);
954
955 if (rc < 0) {
956 if (errno == EINTR) {
957 return 0;
958 }
959 warn("poll error");
960 return -1;
961 }
962
963 /* process internal fd first */
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200964 if (console->server->pollfds[console->server->tty_pollfd_index].revents) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200965 rc = read(console->server->tty.fd, buf, sizeof(buf));
Alexander Hansen69257402024-06-07 15:17:09 +0200966 if (rc <= 0) {
967 warn("Error reading from tty device");
968 return -1;
969 }
970 rc = ringbuffer_queue(console->rb, buf, rc);
971 if (rc) {
972 return -1;
973 }
974 }
975
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200976 if (console->server->pollfds[console->dbus_pollfd_index].revents) {
Alexander Hansen69257402024-06-07 15:17:09 +0200977 sd_bus_process(console->bus, NULL);
978 }
979
980 /* ... and then the pollers */
981 rc = call_pollers(console, &tv);
982 if (rc) {
983 return -1;
984 }
985 return 0;
986}
987
988int run_console(struct console *console)
989{
990 sighandler_t sighandler_save = signal(SIGINT, sighandler);
991 ssize_t rc = 0;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800992
Jeremy Kerrd831f962016-01-29 17:18:01 +0800993 for (;;) {
Alexander Hansen69257402024-06-07 15:17:09 +0200994 rc = run_console_iteration(console);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930995 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800996 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930997 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800998 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800999
1000 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +08001001 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +08001002
1003 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +08001004}
Jeremy Kerrd831f962016-01-29 17:18:01 +08001005static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +09301006 { "config", required_argument, 0, 'c' },
Andrew Jeffery954be0f2023-05-03 21:01:59 +09301007 { "console-id", required_argument, 0, 'i' },
Andrew Jefferya72711a2023-04-18 18:19:41 +09301008 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +08001009};
1010
1011int main(int argc, char **argv)
1012{
Medicine Yeh7f2bfb92024-03-10 23:19:41 -07001013 size_t buffer_size = default_buffer_size;
Jeremy Kerrd66195c2016-03-16 17:24:51 +08001014 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -05001015 const char *config_tty_kname = NULL;
Medicine Yeh7f2bfb92024-03-10 23:19:41 -07001016 const char *buffer_size_str = NULL;
Andrew Jeffery954be0f2023-05-03 21:01:59 +09301017 const char *console_id = NULL;
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +02001018 struct console_server server = { 0 };
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08001019 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +08001020 struct config *config;
1021 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +08001022
Jeremy Kerrd831f962016-01-29 17:18:01 +08001023 for (;;) {
Andrew Jefferyb70f8712023-04-19 12:53:34 +09301024 int c;
1025 int idx;
Jeremy Kerrd831f962016-01-29 17:18:01 +08001026
Andrew Jeffery954be0f2023-05-03 21:01:59 +09301027 c = getopt_long(argc, argv, "c:i:", options, &idx);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +09301028 if (c == -1) {
Jeremy Kerrd831f962016-01-29 17:18:01 +08001029 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +09301030 }
Jeremy Kerrd831f962016-01-29 17:18:01 +08001031
1032 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +08001033 case 'c':
1034 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +08001035 break;
Andrew Jeffery954be0f2023-05-03 21:01:59 +09301036 case 'i':
1037 console_id = optarg;
1038 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +08001039 case 'h':
1040 case '?':
1041 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +08001042 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +08001043 }
1044 }
1045
Andrew Jeffery2834c5b2023-04-19 12:47:09 +09301046 if (optind < argc) {
Andrew Jeffery91dde142020-02-12 22:46:27 +10301047 config_tty_kname = argv[optind];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +09301048 }
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -05001049
Medicine Yeh7f2bfb92024-03-10 23:19:41 -07001050 config = config_init(config_filename);
Andrew Jeffery28a17612024-07-10 09:32:05 +09301051 if (!config) {
1052 return EXIT_FAILURE;
1053 }
Medicine Yeh7f2bfb92024-03-10 23:19:41 -07001054
Jeremy Kerrd66195c2016-03-16 17:24:51 +08001055 console = malloc(sizeof(struct console));
Andrew Jeffery28a17612024-07-10 09:32:05 +09301056 if (!console) {
1057 rc = -1;
1058 goto out_config_fini;
1059 }
1060
Jeremy Kerrd66195c2016-03-16 17:24:51 +08001061 memset(console, 0, sizeof(*console));
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +02001062
1063 server.active = console;
1064 console->server = &server;
1065
Alexander Hansen2325d5d2024-06-07 16:54:20 +02001066 server.pollfds = NULL;
1067 server.capacity_pollfds = 0;
Andrew Jeffery28a17612024-07-10 09:32:05 +09301068
Medicine Yeh7f2bfb92024-03-10 23:19:41 -07001069 buffer_size_str = config_get_value(config, "ringbuffer-size");
1070 if (buffer_size_str) {
1071 rc = config_parse_bytesize(buffer_size_str, &buffer_size);
1072 if (rc) {
1073 warn("Invalid ringbuffer-size. Default to %zukB",
1074 buffer_size >> 10);
1075 }
1076 }
Jeremy Kerrf733c852017-02-07 18:40:10 +08001077 console->rb = ringbuffer_init(buffer_size);
Andrew Jeffery3f8d5be2024-07-09 14:40:50 +09301078 if (!console->rb) {
1079 rc = -1;
Andrew Jeffery28a17612024-07-10 09:32:05 +09301080 goto out_console_fini;
Andrew Jeffery3f8d5be2024-07-09 14:40:50 +09301081 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +08001082
Andrew Jeffery954be0f2023-05-03 21:01:59 +09301083 if (set_socket_info(console, config, console_id)) {
Andrew Jeffery29c59c42023-05-03 19:33:07 +09301084 rc = -1;
Andrew Jeffery3f8d5be2024-07-09 14:40:50 +09301085 goto out_ringbuffer_fini;
Ninad Palsuleb14ca192023-03-31 09:49:00 -05001086 }
1087
Zev Weiss7dc08ba2023-09-12 20:49:14 -07001088 uart_routing_init(config);
1089
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +02001090 rc = tty_init(&server, config, config_tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +09301091 if (rc) {
Andrew Jeffery3f8d5be2024-07-09 14:40:50 +09301092 goto out_ringbuffer_fini;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +09301093 }
Jeremy Kerrd831f962016-01-29 17:18:01 +08001094
Andrew Jeffery498a4a82024-07-09 15:07:05 +09301095 rc = dbus_init(console, config);
1096 if (rc) {
1097 goto out_tty_fini;
1098 }
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +08001099
Jeremy Kerrd47963e2016-03-16 17:29:55 +08001100 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001101
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08001102 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001103
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08001104 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001105
Andrew Jeffery498a4a82024-07-09 15:07:05 +09301106out_tty_fini:
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +02001107 tty_fini(&server);
Andrew Jeffery30ea6382023-05-03 16:56:10 +09301108
Andrew Jeffery3f8d5be2024-07-09 14:40:50 +09301109out_ringbuffer_fini:
1110 ringbuffer_fini(console->rb);
1111
Andrew Jeffery28a17612024-07-10 09:32:05 +09301112out_console_fini:
Jeremy Kerr89ea8192016-03-15 17:57:43 +08001113 free(console->pollers);
Alexander Hansen2325d5d2024-06-07 16:54:20 +02001114 free(server.pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +08001115 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001116
Andrew Jeffery28a17612024-07-10 09:32:05 +09301117out_config_fini:
1118 config_fini(config);
1119
Jeremy Kerrd831f962016-01-29 17:18:01 +08001120 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
1121}