blob: 60d2d2e17110b9d4ea2c4c948d75817a1745d396 [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
Alexander Hansena6b29102024-07-10 15:33:37 +020041#include "console-mux.h"
42
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080043#include "console-server.h"
Alexander Hansen1e04f442024-06-12 16:35:58 +020044#include "config.h"
Jeremy Kerrd831f962016-01-29 17:18:01 +080045
Andrew Jeffery30ea6382023-05-03 16:56:10 +093046#define DEV_PTS_PATH "/dev/pts"
47
Medicine Yeh7f2bfb92024-03-10 23:19:41 -070048/* default size of the shared backlog ringbuffer */
49const size_t default_buffer_size = 128ul * 1024ul;
Jeremy Kerrf733c852017-02-07 18:40:10 +080050
Jeremy Kerr769cee12016-03-15 17:53:56 +080051/* state shared with the signal handler */
Andrew Jeffery553cb662024-07-09 15:14:56 +093052static volatile sig_atomic_t sigint;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080053
Jeremy Kerrd831f962016-01-29 17:18:01 +080054static void usage(const char *progname)
55{
56 fprintf(stderr,
Andrew Jefferya72711a2023-04-18 18:19:41 +093057 "usage: %s [options] <DEVICE>\n"
58 "\n"
59 "Options:\n"
Andrew Jeffery954be0f2023-05-03 21:01:59 +093060 " --config <FILE>\tUse FILE for configuration\n"
61 " --console-id <NAME>\tUse NAME in the UNIX domain socket address\n"
Andrew Jefferya72711a2023-04-18 18:19:41 +093062 "",
Jeremy Kerrd831f962016-01-29 17:18:01 +080063 progname);
64}
65
Alexander Hansen2325d5d2024-06-07 16:54:20 +020066static bool console_server_pollfd_reclaimable(struct pollfd *p)
67{
68 return p->fd == -1 && p->events == 0 && p->revents == ~0;
69}
70
71static ssize_t
72console_server_find_released_pollfd(struct console_server *server)
73{
74 for (size_t i = 0; i < server->capacity_pollfds; i++) {
75 struct pollfd *p = &server->pollfds[i];
76 if (console_server_pollfd_reclaimable(p)) {
77 return (ssize_t)i;
78 }
79 }
80 return -1;
81}
82
83// returns the index of that pollfd in server->pollfds
84// we cannot return a pointer because 'realloc' may move server->pollfds
85ssize_t console_server_request_pollfd(struct console_server *server, int fd,
86 short int events)
87{
88 ssize_t index;
89 struct pollfd *pollfd;
90
91 index = console_server_find_released_pollfd(server);
92
93 if (index < 0) {
94 const size_t newcap = server->capacity_pollfds + 1;
95
96 struct pollfd *newarr = reallocarray(server->pollfds, newcap,
97 sizeof(struct pollfd));
98 if (newarr == NULL) {
99 return -1;
100 }
101 server->pollfds = newarr;
102
103 index = (ssize_t)server->capacity_pollfds;
104
105 server->capacity_pollfds = newcap;
106 }
107
108 pollfd = &server->pollfds[index];
109 pollfd->fd = fd;
110 pollfd->events = events;
111 pollfd->revents = 0;
112
113 return index;
114}
115
116int console_server_release_pollfd(struct console_server *server,
117 size_t pollfd_index)
118{
119 if (pollfd_index >= server->capacity_pollfds) {
120 return -1;
121 }
122
123 struct pollfd *pfd = &server->pollfds[pollfd_index];
124
125 // mark pollfd as reclaimable
126
127 // ignore this file descriptor when calling 'poll'
128 // https://www.man7.org/linux/man-pages/man2/poll.2.html
129 pfd->fd = -1;
130 pfd->events = 0;
131 pfd->revents = ~0;
132
133 return 0;
134}
135
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200136/* populates server->tty.dev and server->tty.sysfs_devnode, using the tty kernel name */
137static int tty_find_device(struct console_server *server)
Jeremy Kerr17217842016-01-29 18:44:21 +0800138{
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +0930139 char *tty_class_device_link = NULL;
140 char *tty_path_input_real = NULL;
141 char *tty_device_tty_dir = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930142 char *tty_vuart_lpc_addr = NULL;
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +0930143 char *tty_device_reldir = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930144 char *tty_sysfs_devnode = NULL;
Andrew Jefferyd3cb9c22023-05-03 17:13:50 +0930145 char *tty_kname_real = NULL;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930146 char *tty_path_input = NULL;
Jeremy Kerr17217842016-01-29 18:44:21 +0800147 int rc;
148
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200149 server->tty.type = TTY_DEVICE_UNDEFINED;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930150
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200151 assert(server->tty.kname);
152 if (!strlen(server->tty.kname)) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930153 warnx("TTY kname must not be empty");
154 rc = -1;
155 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930156 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800157
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200158 if (server->tty.kname[0] == '/') {
159 tty_path_input = strdup(server->tty.kname);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930160 if (!tty_path_input) {
161 rc = -1;
162 goto out_free;
163 }
164 } else {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200165 rc = asprintf(&tty_path_input, "/dev/%s", server->tty.kname);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930166 if (rc < 0) {
167 goto out_free;
168 }
169 }
170
171 /* udev may rename the tty name with a symbol link, try to resolve */
Yi Li45ad7672016-10-18 23:21:19 +0800172 tty_path_input_real = realpath(tty_path_input, NULL);
173 if (!tty_path_input_real) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930174 warn("Can't find realpath for %s", tty_path_input);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930175 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800176 goto out_free;
177 }
178
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930179 /*
180 * Allow hooking obmc-console-server up to PTYs for testing
181 *
182 * https://amboar.github.io/notes/2023/05/02/testing-obmc-console-with-socat.html
183 */
184 if (!strncmp(DEV_PTS_PATH, tty_path_input_real, strlen(DEV_PTS_PATH))) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200185 server->tty.type = TTY_DEVICE_PTY;
186 server->tty.dev = strdup(server->tty.kname);
187 rc = server->tty.dev ? 0 : -1;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930188 goto out_free;
189 }
190
Yi Li45ad7672016-10-18 23:21:19 +0800191 tty_kname_real = basename(tty_path_input_real);
192 if (!tty_kname_real) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200193 warn("Can't find real name for %s", server->tty.kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930194 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800195 goto out_free;
196 }
197
Andrew Jefferya72711a2023-04-18 18:19:41 +0930198 rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s",
199 tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930200 if (rc < 0) {
Yi Li45ad7672016-10-18 23:21:19 +0800201 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930202 }
Yi Li45ad7672016-10-18 23:21:19 +0800203
Jeremy Kerr17217842016-01-29 18:44:21 +0800204 tty_device_tty_dir = realpath(tty_class_device_link, NULL);
Yi Li45ad7672016-10-18 23:21:19 +0800205 if (!tty_device_tty_dir) {
206 warn("Can't query sysfs for device %s", tty_kname_real);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930207 rc = -1;
Jeremy Kerr17217842016-01-29 18:44:21 +0800208 goto out_free;
209 }
210
211 rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930212 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800213 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930214 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800215
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930216 tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
217 if (!tty_sysfs_devnode) {
Yi Li45ad7672016-10-18 23:21:19 +0800218 warn("Can't find parent device for %s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930219 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800220
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200221 rc = asprintf(&server->tty.dev, "/dev/%s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930222 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800223 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930224 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800225
Oskar Senft955d1402023-09-27 22:43:22 -0400226 // Default to non-VUART
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200227 server->tty.type = TTY_DEVICE_UART;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930228
Oskar Senft955d1402023-09-27 22:43:22 -0400229 /* Arbitrarily pick an attribute to differentiate UART vs VUART */
230 if (tty_sysfs_devnode) {
231 rc = asprintf(&tty_vuart_lpc_addr, "%s/lpc_address",
232 tty_sysfs_devnode);
233 if (rc < 0) {
234 goto out_free;
235 }
236
237 rc = access(tty_vuart_lpc_addr, F_OK);
238 if (!rc) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200239 server->tty.type = TTY_DEVICE_VUART;
240 server->tty.vuart.sysfs_devnode =
Oskar Senft955d1402023-09-27 22:43:22 -0400241 strdup(tty_sysfs_devnode);
242 }
243 }
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930244
Jeremy Kerr17217842016-01-29 18:44:21 +0800245 rc = 0;
246
247out_free:
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930248 free(tty_vuart_lpc_addr);
Jeremy Kerr17217842016-01-29 18:44:21 +0800249 free(tty_class_device_link);
Andrew Jeffery982090d2024-07-09 17:31:27 +0930250 free(tty_sysfs_devnode);
Jeremy Kerr17217842016-01-29 18:44:21 +0800251 free(tty_device_tty_dir);
252 free(tty_device_reldir);
Yi Li45ad7672016-10-18 23:21:19 +0800253 free(tty_path_input);
254 free(tty_path_input_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800255 return rc;
256}
257
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200258static int tty_set_sysfs_attr(struct console_server *server, const char *name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930259 int value)
Jeremy Kerr957818b2016-03-08 14:35:15 +0800260{
261 char *path;
262 FILE *fp;
263 int rc;
264
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200265 assert(server->tty.type == TTY_DEVICE_VUART);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930266
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200267 if (!server->tty.vuart.sysfs_devnode) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930268 return -1;
269 }
270
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200271 rc = asprintf(&path, "%s/%s", server->tty.vuart.sysfs_devnode, name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930272 if (rc < 0) {
Jeremy Kerr957818b2016-03-08 14:35:15 +0800273 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930274 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800275
276 fp = fopen(path, "w");
277 if (!fp) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930278 warn("Can't access attribute %s on device %s", name,
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200279 server->tty.kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800280 rc = -1;
281 goto out_free;
282 }
283 setvbuf(fp, NULL, _IONBF, 0);
284
285 rc = fprintf(fp, "0x%x", value);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930286 if (rc < 0) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930287 warn("Error writing to %s attribute of device %s", name,
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200288 server->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930289 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800290 fclose(fp);
291
Jeremy Kerr957818b2016-03-08 14:35:15 +0800292out_free:
293 free(path);
294 return rc;
295}
296
Jeremy Kerrd831f962016-01-29 17:18:01 +0800297/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700298 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800299 */
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200300void tty_init_termios(struct console_server *server)
Jeremy Kerr54e95692016-03-24 17:07:56 +0800301{
302 struct termios termios;
303 int rc;
304
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200305 rc = tcgetattr(server->tty.fd, &termios);
Jeremy Kerr54e95692016-03-24 17:07:56 +0800306 if (rc) {
307 warn("Can't read tty termios");
308 return;
309 }
310
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200311 if (server->tty.type == TTY_DEVICE_UART && server->tty.uart.baud) {
312 if (cfsetspeed(&termios, server->tty.uart.baud) < 0) {
313 warn("Couldn't set speeds for %s", server->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930314 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700315 }
316
317 /* Set console to raw mode: we don't want any processing to occur on
318 * the underlying terminal input/output.
319 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800320 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700321
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200322 rc = tcsetattr(server->tty.fd, TCSANOW, &termios);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930323 if (rc) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200324 warn("Can't set terminal options for %s", server->tty.kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930325 }
Jeremy Kerr54e95692016-03-24 17:07:56 +0800326}
327
328/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800329 * Open and initialise the serial device
330 */
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200331static void tty_init_vuart_io(struct console_server *server)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800332{
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200333 assert(server->tty.type == TTY_DEVICE_VUART);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930334
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200335 if (server->tty.vuart.sirq) {
336 tty_set_sysfs_attr(server, "sirq", server->tty.vuart.sirq);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930337 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800338
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200339 if (server->tty.vuart.lpc_addr) {
340 tty_set_sysfs_attr(server, "lpc_address",
341 server->tty.vuart.lpc_addr);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930342 }
343}
344
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200345static int tty_init_io(struct console_server *server)
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930346{
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200347 server->tty.fd = open(server->tty.dev, O_RDWR);
348 if (server->tty.fd <= 0) {
349 warn("Can't open tty %s", server->tty.dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800350 return -1;
351 }
352
353 /* Disable character delay. We may want to later enable this when
354 * we detect larger amounts of data
355 */
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200356 fcntl(server->tty.fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800357
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200358 tty_init_termios(server);
Jeremy Kerr54e95692016-03-24 17:07:56 +0800359
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200360 ssize_t index =
361 console_server_request_pollfd(server, server->tty.fd, POLLIN);
362
363 if (index < 0) {
364 return -1;
365 }
366
367 server->tty_pollfd_index = (size_t)index;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800368
Jeremy Kerrd831f962016-01-29 17:18:01 +0800369 return 0;
370}
371
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200372static int tty_init_vuart(struct console_server *server, struct config *config)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800373{
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930374 unsigned long parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800375 const char *val;
376 char *endp;
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930377
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200378 assert(server->tty.type == TTY_DEVICE_VUART);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800379
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800380 val = config_get_value(config, "lpc-address");
381 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930382 errno = 0;
383 parsed = strtoul(val, &endp, 0);
384 if (parsed == ULONG_MAX && errno == ERANGE) {
385 warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'",
386 val);
387 return -1;
388 }
389
390 if (parsed > UINT16_MAX) {
391 warn("Invalid LPC address '%s'", val);
392 return -1;
393 }
394
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200395 server->tty.vuart.lpc_addr = (uint16_t)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800396 if (endp == optarg) {
397 warn("Invalid LPC address: '%s'", val);
398 return -1;
399 }
400 }
401
402 val = config_get_value(config, "sirq");
403 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930404 errno = 0;
405 parsed = strtoul(val, &endp, 0);
406 if (parsed == ULONG_MAX && errno == ERANGE) {
407 warn("Cannot interpret 'sirq' value as an unsigned long: '%s'",
408 val);
409 }
410
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930411 if (parsed > 16) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930412 warn("Invalid LPC SERIRQ: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930413 }
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930414
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200415 server->tty.vuart.sirq = (int)parsed;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930416 if (endp == optarg) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800417 warn("Invalid sirq: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930418 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800419 }
420
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930421 return 0;
422}
423
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200424static int tty_init(struct console_server *server, struct config *config,
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930425 const char *tty_arg)
426{
427 const char *val;
428 int rc;
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700429
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930430 if (tty_arg) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200431 server->tty.kname = tty_arg;
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930432 } else if ((val = config_get_value(config, "upstream-tty"))) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200433 server->tty.kname = val;
Andrew Jefferyd769eec2023-05-03 20:00:09 +0930434 } else {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800435 warnx("Error: No TTY device specified");
436 return -1;
437 }
438
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200439 rc = tty_find_device(server);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930440 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800441 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930442 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800443
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200444 switch (server->tty.type) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930445 case TTY_DEVICE_VUART:
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200446 rc = tty_init_vuart(server, config);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930447 if (rc) {
448 return rc;
449 }
450
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200451 tty_init_vuart_io(server);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930452 break;
453 case TTY_DEVICE_UART:
454 val = config_get_value(config, "baud");
455 if (val) {
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200456 if (config_parse_baud(&server->tty.uart.baud, val)) {
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930457 warnx("Invalid baud rate: '%s'", val);
458 }
459 }
460 break;
461 case TTY_DEVICE_PTY:
462 break;
463 case TTY_DEVICE_UNDEFINED:
464 default:
465 warnx("Cannot configure unrecognised TTY device");
466 return -1;
467 }
468
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200469 return tty_init_io(server);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930470}
471
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200472static void tty_fini(struct console_server *server)
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930473{
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200474 if (server->tty_pollfd_index < server->capacity_pollfds) {
475 console_server_release_pollfd(server, server->tty_pollfd_index);
476 server->tty_pollfd_index = SIZE_MAX;
477 }
478
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200479 if (server->tty.type == TTY_DEVICE_VUART) {
480 free(server->tty.vuart.sysfs_devnode);
Andrew Jeffery30ea6382023-05-03 16:56:10 +0930481 }
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200482
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200483 free(server->tty.dev);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800484}
485
Zev Weiss7dc08ba2023-09-12 20:49:14 -0700486static int write_to_path(const char *path, const char *data)
487{
488 int rc = 0;
489 FILE *f = fopen(path, "w");
490 if (!f) {
491 return -1;
492 }
493
494 if (fprintf(f, "%s", data) < 0) {
495 rc = -1;
496 }
497
498 if (fclose(f)) {
499 rc = -1;
500 }
501
502 return rc;
503}
504
505#define ASPEED_UART_ROUTING_PATTERN \
506 "/sys/bus/platform/drivers/aspeed-uart-routing/*.uart-routing"
507
508static void uart_routing_init(struct config *config)
509{
510 const char *muxcfg;
511 const char *p;
512 size_t buflen;
513 char *sink;
514 char *source;
515 char *muxdir;
516 char *path;
517 glob_t globbuf;
518
519 muxcfg = config_get_value(config, "aspeed-uart-routing");
520 if (!muxcfg) {
521 return;
522 }
523
524 /* Find the driver's sysfs directory */
525 if (glob(ASPEED_UART_ROUTING_PATTERN, GLOB_ERR | GLOB_NOSORT, NULL,
526 &globbuf) != 0) {
527 warn("Couldn't find uart-routing driver directory, cannot apply config");
528 return;
529 }
530 if (globbuf.gl_pathc != 1) {
531 warnx("Found %zd uart-routing driver directories, cannot apply config",
532 globbuf.gl_pathc);
533 goto out_free_glob;
534 }
535 muxdir = globbuf.gl_pathv[0];
536
537 /*
538 * Rather than faff about tracking a bunch of separate buffer sizes,
539 * just use one (worst-case) size for all of them -- +2 for a trailing
540 * NUL and a '/' separator to construct the sysfs file path.
541 */
542 buflen = strlen(muxdir) + strlen(muxcfg) + 2;
543
544 sink = malloc(buflen);
545 source = malloc(buflen);
546 path = malloc(buflen);
547 if (!path || !sink || !source) {
548 warnx("Out of memory applying uart routing config");
549 goto out_free_bufs;
550 }
551
552 p = muxcfg;
553 while (*p) {
554 ssize_t bytes_scanned;
555
556 if (sscanf(p, " %[^:/ \t]:%[^: \t] %zn", sink, source,
557 &bytes_scanned) != 2) {
558 warnx("Invalid syntax in aspeed uart config: '%s' not applied",
559 p);
560 break;
561 }
562 p += bytes_scanned;
563
564 /*
565 * Check that the sink name looks reasonable before proceeding
566 * (there are other writable files in the same directory that
567 * we shouldn't be touching, such as 'driver_override' and
568 * 'uevent').
569 */
570 if (strncmp(sink, "io", strlen("io")) != 0 &&
571 strncmp(sink, "uart", strlen("uart")) != 0) {
572 warnx("Skipping invalid uart routing name '%s' (must be ioN or uartN)",
573 sink);
574 continue;
575 }
576
577 snprintf(path, buflen, "%s/%s", muxdir, sink);
578 if (write_to_path(path, source)) {
579 warn("Failed to apply uart-routing config '%s:%s'",
580 sink, source);
581 }
582 }
583
584out_free_bufs:
585 free(path);
586 free(source);
587 free(sink);
588out_free_glob:
589 globfree(&globbuf);
590}
591
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800592int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800593{
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +0200594 return write_buf_to_fd(console->server->tty.fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800595}
596
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500597/* Prepare a socket name */
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930598static int set_socket_info(struct console *console, struct config *config,
599 const char *console_id)
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500600{
601 ssize_t len;
602
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500603 /* Get console id */
604 console->console_id = config_resolve_console_id(config, console_id);
Andrew Jeffery954be0f2023-05-03 21:01:59 +0930605
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500606 /* Get the socket name/path */
Ninad Palsule5ba20b52023-05-12 14:03:15 -0500607 len = console_socket_path(console->socket_name, console->console_id);
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500608 if (len < 0) {
609 warn("Failed to set socket path: %s", strerror(errno));
610 return EXIT_FAILURE;
611 }
612
613 /* Socket name is not a null terminated string hence save the length */
614 console->socket_name_len = len;
615
616 return 0;
617}
618
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800619static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800620{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930621 /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Andrew Jefferyadedc332024-07-10 10:48:13 +0930622 extern const struct handler_type *const __start_handlers[];
623 extern const struct handler_type *const __stop_handlers[];
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930624 /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerre2826c72024-07-05 10:54:21 +0800625 size_t n_types;
626 int j = 0;
627 size_t i;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800628
Andrew Jefferyadedc332024-07-10 10:48:13 +0930629 n_types = __stop_handlers - __start_handlers;
Jeremy Kerre2826c72024-07-05 10:54:21 +0800630 console->handlers = calloc(n_types, sizeof(struct handler *));
631 if (!console->handlers) {
632 err(EXIT_FAILURE, "malloc(handlers)");
633 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800634
Andrew Jeffery079fc512024-07-09 16:51:56 +0930635 printf("%zu handler type%s\n", n_types, n_types == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800636
Jeremy Kerre2826c72024-07-05 10:54:21 +0800637 for (i = 0; i < n_types; i++) {
Andrew Jefferyadedc332024-07-10 10:48:13 +0930638 const struct handler_type *type = __start_handlers[i];
Jeremy Kerre2826c72024-07-05 10:54:21 +0800639 struct handler *handler;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800640
Jeremy Kerre2826c72024-07-05 10:54:21 +0800641 /* Should be picked up at build time by
642 * console_handler_register, but check anyway
643 */
644 if (!type->init || !type->fini) {
645 errx(EXIT_FAILURE,
646 "invalid handler type %s: no init() / fini()",
647 type->name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930648 }
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800649
Jeremy Kerre2826c72024-07-05 10:54:21 +0800650 handler = type->init(type, console, config);
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800651
Jeremy Kerre2826c72024-07-05 10:54:21 +0800652 printf(" console '%s': handler %s [%sactive]\n",
653 console->console_id, type->name, handler ? "" : "in");
654
655 if (handler) {
656 handler->type = type;
657 console->handlers[j++] = handler;
658 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800659 }
Jeremy Kerre2826c72024-07-05 10:54:21 +0800660
661 console->n_handlers = j;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800662}
663
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800664static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800665{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800666 struct handler *handler;
667 int i;
668
669 for (i = 0; i < console->n_handlers; i++) {
670 handler = console->handlers[i];
Jeremy Kerre2826c72024-07-05 10:54:21 +0800671 handler->type->fini(handler);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800672 }
Jeremy Kerre2826c72024-07-05 10:54:21 +0800673
674 free(console->handlers);
675 console->handlers = NULL;
676 console->n_handlers = 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800677}
678
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800679static int get_current_time(struct timeval *tv)
680{
681 struct timespec t;
682 int rc;
683
684 /*
685 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
686 * local time changes. However, a struct timeval is more
687 * convenient for calculations, so convert to that.
688 */
689 rc = clock_gettime(CLOCK_MONOTONIC, &t);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930690 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800691 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930692 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800693
694 tv->tv_sec = t.tv_sec;
695 tv->tv_usec = t.tv_nsec / 1000;
696
697 return 0;
698}
699
Andrew Jefferya72711a2023-04-18 18:19:41 +0930700struct ringbuffer_consumer *
701console_ringbuffer_consumer_register(struct console *console,
702 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800703{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800704 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800705}
706
Jeremy Kerr55c97122017-02-07 17:06:46 +0800707struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930708 struct handler *handler,
709 poller_event_fn_t poller_fn,
710 poller_timeout_fn_t timeout_fn, int fd,
711 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800712{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800713 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930714 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800715
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200716 const ssize_t index = console_server_request_pollfd(
717 console->server, fd, (short)(events & 0x7fff));
718 if (index < 0) {
719 fprintf(stderr, "Error requesting pollfd\n");
720 return NULL;
721 }
722
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800723 poller = malloc(sizeof(*poller));
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200724 // TODO: check for error case of malloc here and release previously requested pollfd
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800725 poller->remove = false;
726 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800727 poller->event_fn = poller_fn;
728 poller->timeout_fn = timeout_fn;
Andrew Jeffery9cc84592024-07-09 15:44:30 +0930729 timerclear(&poller->timeout);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800730 poller->data = data;
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200731 poller->pollfd_index = index;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800732
733 /* add one to our pollers array */
734 n = console->n_pollers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930735 /*
736 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
737 * pointer type.
738 */
739 /* NOLINTBEGIN(bugprone-sizeof-expression) */
740 console->pollers = reallocarray(console->pollers, console->n_pollers,
741 sizeof(*console->pollers));
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200742 // TODO: check for the error case of reallocarray and release previously requested pollfd
Andrew Jeffery91b52172023-04-19 12:42:14 +0930743 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800744
745 console->pollers[n] = poller;
746
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800747 return poller;
748}
749
Andrew Jefferya72711a2023-04-18 18:19:41 +0930750void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800751{
752 int i;
753
754 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930755 for (i = 0; i < console->n_pollers; i++) {
756 if (console->pollers[i] == poller) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800757 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930758 }
759 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800760
761 assert(i < console->n_pollers);
762
763 console->n_pollers--;
764
Andrew Jeffery91b52172023-04-19 12:42:14 +0930765 /*
766 * Remove the item from the pollers array...
767 *
768 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
769 * pointer type.
770 */
771 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930772 memmove(&console->pollers[i], &console->pollers[i + 1],
773 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800774
Andrew Jeffery7851a392024-07-09 16:30:22 +0930775 if (console->n_pollers == 0) {
776 free(console->pollers);
777 console->pollers = NULL;
778 } else {
779 console->pollers = reallocarray(console->pollers,
780 console->n_pollers,
781 sizeof(*console->pollers));
782 }
Andrew Jeffery91b52172023-04-19 12:42:14 +0930783 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800784
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200785 console_server_release_pollfd(console->server, poller->pollfd_index);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800786
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800787 free(poller);
788}
789
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800790void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930791 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800792{
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200793 console->server->pollfds[poller->pollfd_index].events =
794 (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800795}
796
Andrew Jefferyfd048322023-04-18 12:02:01 +0930797void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930798 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800799{
800 struct timeval now;
801 int rc;
802
803 rc = get_current_time(&now);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930804 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800805 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930806 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800807
808 timeradd(&now, tv, &poller->timeout);
809}
810
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930811static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800812{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930813 struct timeval *earliest;
814 struct timeval interval;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800815 struct poller *poller;
816 int i;
817
818 earliest = NULL;
819
820 for (i = 0; i < console->n_pollers; i++) {
821 poller = console->pollers[i];
822
823 if (poller->timeout_fn && timerisset(&poller->timeout) &&
824 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930825 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800826 // poller is buffering data and needs the poll
827 // function to timeout.
828 earliest = &poller->timeout;
829 }
830 }
831
832 if (earliest) {
833 if (timercmp(earliest, cur_time, >)) {
834 /* recalculate the timeout period, time period has
835 * not elapsed */
836 timersub(earliest, cur_time, &interval);
837 return ((interval.tv_sec * 1000) +
838 (interval.tv_usec / 1000));
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930839 } /* return from poll immediately */
840 return 0;
841
842 } /* poll indefinitely */
843 return -1;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800844}
845
846static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800847{
848 struct poller *poller;
849 struct pollfd *pollfd;
850 enum poller_ret prc;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930851 int i;
852 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800853
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800854 rc = 0;
855
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800856 /*
857 * Process poll events by iterating through the pollers and pollfds
858 * in-step, calling any pollers that we've found revents for.
859 */
860 for (i = 0; i < console->n_pollers; i++) {
861 poller = console->pollers[i];
Alexander Hansen2325d5d2024-06-07 16:54:20 +0200862 pollfd = &console->server->pollfds[poller->pollfd_index];
863 if (pollfd->fd < 0) {
864 // pollfd has already been released
865 continue;
866 }
867
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800868 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800869
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800870 /* process pending events... */
871 if (pollfd->revents) {
872 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930873 poller->data);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930874 if (prc == POLLER_EXIT) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800875 rc = -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930876 } else if (prc == POLLER_REMOVE) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800877 poller->remove = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930878 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800879 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800880
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800881 if ((prc == POLLER_OK) && poller->timeout_fn &&
882 timerisset(&poller->timeout) &&
883 timercmp(&poller->timeout, cur_time, <=)) {
884 /* One of the ringbuffer consumers is buffering the
885 data stream. The amount of idle time the consumer
886 desired has expired. Process the buffered data for
887 transmission. */
888 timerclear(&poller->timeout);
889 prc = poller->timeout_fn(poller->handler, poller->data);
890 if (prc == POLLER_EXIT) {
891 rc = -1;
892 } else if (prc == POLLER_REMOVE) {
893 poller->remove = true;
894 }
895 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800896 }
897
898 /**
899 * Process deferred removals; restarting each time we unregister, as
900 * the array will have changed
901 */
902 for (;;) {
903 bool removed = false;
904
905 for (i = 0; i < console->n_pollers; i++) {
906 poller = console->pollers[i];
907 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800908 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800909 removed = true;
910 break;
911 }
912 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930913 if (!removed) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800914 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930915 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800916 }
917
918 return rc;
919}
920
Jeremy Kerr769cee12016-03-15 17:53:56 +0800921static void sighandler(int signal)
922{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930923 if (signal == SIGINT) {
Andrew Jeffery553cb662024-07-09 15:14:56 +0930924 sigint = 1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930925 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800926}
927
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200928static int run_console_per_console(struct console *console, size_t buf_size,
929 struct timeval *tv)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800930{
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200931 int rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800932
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200933 if (console->rb->size < buf_size) {
Alexander Hansen69257402024-06-07 15:17:09 +0200934 fprintf(stderr, "Ringbuffer size should be greater than %zuB\n",
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200935 buf_size);
Alexander Hansen69257402024-06-07 15:17:09 +0200936 return -1;
937 }
938
939 if (sigint) {
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200940 warnx("Received interrupt, exiting\n");
Alexander Hansen69257402024-06-07 15:17:09 +0200941 return -1;
942 }
943
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200944 /* ... and then the pollers */
945 rc = call_pollers(console, tv);
946 if (rc) {
947 return -1;
948 }
949
950 return 0;
951}
952
953static int run_console_iteration(struct console_server *server)
954{
955 struct timeval tv;
956 uint8_t buf[4096];
957 long timeout;
958 ssize_t rc;
959
Alexander Hansen69257402024-06-07 15:17:09 +0200960 rc = get_current_time(&tv);
961 if (rc) {
962 warn("Failed to read current time");
963 return -1;
964 }
965
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200966 timeout = get_poll_timeout(server->active, &tv);
Alexander Hansen69257402024-06-07 15:17:09 +0200967
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200968 rc = poll(server->pollfds, server->capacity_pollfds, (int)timeout);
969
970 if (sigint) {
971 warnx("Received interrupt, exiting\n");
972 return -1;
973 }
Alexander Hansen69257402024-06-07 15:17:09 +0200974
975 if (rc < 0) {
976 if (errno == EINTR) {
977 return 0;
978 }
979 warn("poll error");
980 return -1;
981 }
982
983 /* process internal fd first */
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200984 if (server->pollfds[server->tty_pollfd_index].revents) {
985 rc = read(server->tty.fd, buf, sizeof(buf));
Alexander Hansen69257402024-06-07 15:17:09 +0200986 if (rc <= 0) {
987 warn("Error reading from tty device");
988 return -1;
989 }
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200990
991 rc = ringbuffer_queue(server->active->rb, buf, rc);
Alexander Hansen69257402024-06-07 15:17:09 +0200992 if (rc) {
993 return -1;
994 }
995 }
996
Alexander Hansen312ecdc2024-07-10 15:33:37 +0200997 // process dbus
998 struct pollfd *dbus_pollfd =
999 &(server->pollfds[server->dbus_pollfd_index]);
1000 if (dbus_pollfd->revents) {
1001 sd_bus_process(server->bus, NULL);
Alexander Hansen69257402024-06-07 15:17:09 +02001002 }
1003
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001004 for (size_t i = 0; i < server->n_consoles; i++) {
1005 struct console *console = server->consoles[i];
1006
1007 rc = run_console_per_console(console, sizeof(buf), &tv);
1008 if (rc != 0) {
1009 return -1;
1010 }
Alexander Hansen69257402024-06-07 15:17:09 +02001011 }
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001012
Alexander Hansen69257402024-06-07 15:17:09 +02001013 return 0;
1014}
1015
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001016int run_server(struct console_server *server)
Alexander Hansen69257402024-06-07 15:17:09 +02001017{
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001018 sighandler_t sighandler_save;
Alexander Hansen69257402024-06-07 15:17:09 +02001019 ssize_t rc = 0;
Jeremy Kerr769cee12016-03-15 17:53:56 +08001020
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001021 if (server->n_consoles == 0) {
1022 warnx("no console configured for this server");
1023 return -1;
1024 }
1025
1026 sighandler_save = signal(SIGINT, sighandler);
Jeremy Kerrd831f962016-01-29 17:18:01 +08001027 for (;;) {
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001028 rc = run_console_iteration(server);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +09301029 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +08001030 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +09301031 }
Jeremy Kerrd831f962016-01-29 17:18:01 +08001032 }
Jeremy Kerr769cee12016-03-15 17:53:56 +08001033 signal(SIGINT, sighandler_save);
1034
1035 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +08001036}
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001037
Jeremy Kerrd831f962016-01-29 17:18:01 +08001038static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +09301039 { "config", required_argument, 0, 'c' },
Andrew Jeffery954be0f2023-05-03 21:01:59 +09301040 { "console-id", required_argument, 0, 'i' },
Andrew Jefferya72711a2023-04-18 18:19:41 +09301041 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +08001042};
1043
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001044static struct console *console_init(struct console_server *server,
1045 struct config *config,
1046 const char *console_id)
Jeremy Kerrd831f962016-01-29 17:18:01 +08001047{
Medicine Yeh7f2bfb92024-03-10 23:19:41 -07001048 size_t buffer_size = default_buffer_size;
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001049 const char *buffer_size_str = NULL;
1050 int rc;
1051
1052 struct console *console = calloc(1, sizeof(struct console));
1053 if (console == NULL) {
1054 return NULL;
1055 }
1056
1057 console->server = server;
1058 console->console_id = console_id;
1059
1060 buffer_size_str =
1061 config_get_section_value(config, console_id, "ringbuffer-size");
1062
1063 if (!buffer_size_str) {
1064 buffer_size_str = config_get_value(config, "ringbuffer-size");
1065 }
1066
1067 if (buffer_size_str) {
1068 rc = config_parse_bytesize(buffer_size_str, &buffer_size);
1069 if (rc) {
1070 warn("Invalid ringbuffer-size. Default to %zukB",
1071 buffer_size >> 10);
1072 }
1073 }
1074
1075 console->rb = ringbuffer_init(buffer_size);
1076 if (!console->rb) {
1077 goto cleanup_console;
1078 }
1079
Alexander Hansena6b29102024-07-10 15:33:37 +02001080 rc = console_mux_init(console, config);
1081 if (rc) {
1082 warnx("could not set mux gpios from config, exiting");
1083 goto cleanup_rb;
1084 }
1085
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001086 if (set_socket_info(console, config, console_id)) {
1087 warnx("set_socket_info failed");
1088 goto cleanup_rb;
1089 }
1090
1091 rc = dbus_init(console, config);
1092 if (rc != 0) {
1093 goto cleanup_rb;
1094 }
1095
1096 handlers_init(console, config);
1097
1098 return console;
1099
1100cleanup_rb:
1101 free(console->rb);
1102cleanup_console:
1103 free(console);
1104
1105 return NULL;
1106}
1107
1108static void console_fini(struct console *console)
1109{
1110 handlers_fini(console);
1111 ringbuffer_fini(console->rb);
1112 free(console->pollers);
1113 free(console);
1114}
1115
1116// 'opt_console_id' may be NULL
1117static int console_server_add_console(struct console_server *server,
1118 struct config *config,
1119 const char *opt_console_id)
1120{
1121 const char *console_id;
1122 struct console *console;
1123
1124 console_id = config_resolve_console_id(config, opt_console_id);
1125
1126 struct console **tmp = reallocarray(server->consoles,
1127 server->n_consoles + 1,
1128 sizeof(struct console *));
1129 if (tmp == NULL) {
1130 warnx("could not realloc server->consoles");
1131 return -1;
1132 }
1133 server->consoles = tmp;
1134
1135 console = console_init(server, config, console_id);
1136 if (console == NULL) {
1137 warnx("console_init failed");
1138 return -1;
1139 }
1140
1141 server->consoles[server->n_consoles++] = console;
1142
1143 return 0;
1144}
1145
1146// returns NULL on error
1147static struct console *
1148console_server_add_consoles(struct console_server *server,
1149 const char *arg_console_id)
1150{
1151 int rc;
1152
1153 const int nsections = config_count_sections(server->config);
1154 if (nsections < 0) {
1155 return NULL;
1156 }
1157
1158 if (nsections == 0) {
1159 const char *console_id = arg_console_id;
1160
1161 rc = console_server_add_console(server, server->config,
1162 console_id);
1163 if (rc != 0) {
1164 return NULL;
1165 }
1166 }
1167
1168 for (int i = 0; i < nsections; i++) {
1169 const char *console_id =
1170 config_get_section_name(server->config, i);
1171
1172 if (console_id == NULL) {
1173 warnx("no console id provided\n");
1174 return NULL;
1175 }
1176
1177 rc = console_server_add_console(server, server->config,
1178 console_id);
1179 if (rc != 0) {
1180 return NULL;
1181 }
1182 }
1183
1184 const char *initially_active =
1185 config_get_value(server->config, "active-console");
1186 if (!initially_active) {
1187 return server->consoles[0];
1188 }
1189
1190 printf("setting console-id '%s' as the initially active console\n",
1191 initially_active);
1192
1193 for (size_t i = 0; i < server->n_consoles; i++) {
1194 struct console *console = server->consoles[i];
1195
1196 if (strcmp(console->console_id, initially_active) == 0) {
1197 return console;
1198 }
1199 }
1200
1201 warnx("'active-console' '%s' not found among console ids\n",
1202 initially_active);
1203
1204 return NULL;
1205}
1206
1207int console_server_init(struct console_server *server,
1208 const char *config_filename,
1209 const char *config_tty_kname, const char *console_id)
1210{
1211 int rc;
1212 memset(server, 0, sizeof(struct console_server));
1213
1214 server->tty_pollfd_index = -1;
1215
1216 server->config = config_init(config_filename);
1217 if (server->config == NULL) {
1218 return -1;
1219 }
1220
Alexander Hansena6b29102024-07-10 15:33:37 +02001221 rc = console_server_mux_init(server);
1222 if (rc != 0) {
1223 return -1;
1224 }
1225
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001226 uart_routing_init(server->config);
1227
1228 rc = tty_init(server, server->config, config_tty_kname);
1229 if (rc != 0) {
1230 warnx("error during tty_init, exiting.\n");
1231 return -1;
1232 }
1233
1234 rc = dbus_server_init(server);
1235 if (rc != 0) {
1236 warnx("error during dbus init for console server");
1237 return -1;
1238 }
1239
Alexander Hansena6b29102024-07-10 15:33:37 +02001240 struct console *initial_active =
1241 console_server_add_consoles(server, console_id);
1242 if (initial_active == NULL) {
1243 return -1;
1244 }
1245
1246 rc = console_mux_activate(initial_active);
1247 if (rc != 0) {
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001248 return -1;
1249 }
1250
1251 return 0;
1252}
1253
1254void console_server_fini(struct console_server *server)
1255{
1256 for (size_t i = 0; i < server->n_consoles; i++) {
1257 console_fini(server->consoles[i]);
1258 }
1259
1260 free(server->consoles);
1261 dbus_server_fini(server);
1262 tty_fini(server);
1263 free(server->pollfds);
Alexander Hansena6b29102024-07-10 15:33:37 +02001264 console_server_mux_fini(server);
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001265 config_fini(server->config);
1266}
1267
1268int main(int argc, char **argv)
1269{
Jeremy Kerrd66195c2016-03-16 17:24:51 +08001270 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -05001271 const char *config_tty_kname = NULL;
Andrew Jeffery954be0f2023-05-03 21:01:59 +09301272 const char *console_id = NULL;
Alexander Hansenc2b0d8c2024-06-07 14:35:44 +02001273 struct console_server server = { 0 };
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001274 int rc = 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +08001275
Jeremy Kerrd831f962016-01-29 17:18:01 +08001276 for (;;) {
Andrew Jefferyb70f8712023-04-19 12:53:34 +09301277 int c;
1278 int idx;
Jeremy Kerrd831f962016-01-29 17:18:01 +08001279
Andrew Jeffery954be0f2023-05-03 21:01:59 +09301280 c = getopt_long(argc, argv, "c:i:", options, &idx);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +09301281 if (c == -1) {
Jeremy Kerrd831f962016-01-29 17:18:01 +08001282 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +09301283 }
Jeremy Kerrd831f962016-01-29 17:18:01 +08001284
1285 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +08001286 case 'c':
1287 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +08001288 break;
Andrew Jeffery954be0f2023-05-03 21:01:59 +09301289 case 'i':
1290 console_id = optarg;
1291 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +08001292 case 'h':
1293 case '?':
1294 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +08001295 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +08001296 }
1297 }
1298
Andrew Jeffery2834c5b2023-04-19 12:47:09 +09301299 if (optind < argc) {
Andrew Jeffery91dde142020-02-12 22:46:27 +10301300 config_tty_kname = argv[optind];
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001301 } else {
1302 errx(EXIT_FAILURE, "no tty device path has been provided\n");
Andrew Jeffery2834c5b2023-04-19 12:47:09 +09301303 }
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -05001304
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001305 rc = console_server_init(&server, config_filename, config_tty_kname,
1306 console_id);
1307
1308 if (rc == 0) {
1309 rc = run_server(&server);
Andrew Jeffery28a17612024-07-10 09:32:05 +09301310 }
Medicine Yeh7f2bfb92024-03-10 23:19:41 -07001311
Alexander Hansen312ecdc2024-07-10 15:33:37 +02001312 console_server_fini(&server);
Andrew Jeffery28a17612024-07-10 09:32:05 +09301313
Jeremy Kerrd831f962016-01-29 17:18:01 +08001314 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
1315}