blob: 02219cf75797612312f9c523dfb578927b580eb2 [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 Kerr17217842016-01-29 18:44:21 +080019#define _GNU_SOURCE
20
Jeremy Kerr329a35f2016-03-10 15:36:01 +080021#include <assert.h>
Jeremy Kerr769cee12016-03-15 17:53:56 +080022#include <errno.h>
23#include <signal.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080024#include <stdint.h>
25#include <stdbool.h>
26#include <stdlib.h>
27#include <stdio.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <err.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080031#include <string.h>
32#include <getopt.h>
Jeremy Kerr17217842016-01-29 18:44:21 +080033#include <limits.h>
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080034#include <time.h>
Jeremy Kerr54e95692016-03-24 17:07:56 +080035#include <termios.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080036
37#include <sys/types.h>
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080038#include <sys/time.h>
Joel Stanley87e344c2016-09-01 00:00:51 +093039#include <poll.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080040
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080041#include "console-server.h"
Jeremy Kerrd831f962016-01-29 17:18:01 +080042
Andrew Jefferya72711a2023-04-18 18:19:41 +093043#define DBUS_ERR "org.openbmc.error"
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +080044#define DBUS_NAME "xyz.openbmc_project.console"
Andrew Jefferya72711a2023-04-18 18:19:41 +093045#define OBJ_NAME "/xyz/openbmc_project/console"
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +080046
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080047struct console {
Andrew Jefferya72711a2023-04-18 18:19:41 +093048 const char *tty_kname;
49 char *tty_sysfs_devnode;
50 char *tty_dev;
51 int tty_sirq;
Andrew Jefferyfd883a82023-04-18 22:18:25 +093052 uint16_t tty_lpc_addr;
Andrew Jefferya72711a2023-04-18 18:19:41 +093053 speed_t tty_baud;
54 int tty_fd;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080055
Andrew Jefferya72711a2023-04-18 18:19:41 +093056 struct ringbuffer *rb;
Jeremy Kerrf733c852017-02-07 18:40:10 +080057
Andrew Jefferya72711a2023-04-18 18:19:41 +093058 struct handler **handlers;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093059 long n_handlers;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080060
Andrew Jefferya72711a2023-04-18 18:19:41 +093061 struct poller **pollers;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093062 long n_pollers;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080063
Andrew Jefferya72711a2023-04-18 18:19:41 +093064 struct pollfd *pollfds;
65 struct sd_bus *bus;
Jeremy Kerrd831f962016-01-29 17:18:01 +080066};
67
Jeremy Kerr329a35f2016-03-10 15:36:01 +080068struct poller {
Andrew Jefferya72711a2023-04-18 18:19:41 +093069 struct handler *handler;
70 void *data;
71 poller_event_fn_t event_fn;
72 poller_timeout_fn_t timeout_fn;
73 struct timeval timeout;
74 bool remove;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080075};
76
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +080077/* we have two extra entry in the pollfds array for the VUART tty */
78enum internal_pollfds {
79 POLLFD_HOSTTTY = 0,
80 POLLFD_DBUS = 1,
81 MAX_INTERNAL_POLLFD = 2,
82};
Jeremy Kerr329a35f2016-03-10 15:36:01 +080083
Jeremy Kerrf733c852017-02-07 18:40:10 +080084/* size of the shared backlog ringbuffer */
Andrew Jeffery5db8c792023-04-18 21:48:24 +093085const size_t buffer_size = 128ul * 1024ul;
Jeremy Kerrf733c852017-02-07 18:40:10 +080086
Jeremy Kerr769cee12016-03-15 17:53:56 +080087/* state shared with the signal handler */
88static bool sigint;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080089
Jeremy Kerrd831f962016-01-29 17:18:01 +080090static void usage(const char *progname)
91{
92 fprintf(stderr,
Andrew Jefferya72711a2023-04-18 18:19:41 +093093 "usage: %s [options] <DEVICE>\n"
94 "\n"
95 "Options:\n"
96 " --config <FILE> Use FILE for configuration\n"
97 "",
Jeremy Kerrd831f962016-01-29 17:18:01 +080098 progname);
99}
100
Jeremy Kerr17217842016-01-29 18:44:21 +0800101/* populates tty_dev and tty_sysfs_devnode, using the tty kernel name */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800102static int tty_find_device(struct console *console)
Jeremy Kerr17217842016-01-29 18:44:21 +0800103{
104 char *tty_class_device_link;
105 char *tty_device_tty_dir;
106 char *tty_device_reldir;
Yi Li45ad7672016-10-18 23:21:19 +0800107 char *tty_path_input;
108 char *tty_path_input_real;
109 char *tty_kname_real;
Jeremy Kerr17217842016-01-29 18:44:21 +0800110 int rc;
111
Jeremy Kerr17217842016-01-29 18:44:21 +0800112 tty_class_device_link = NULL;
113 tty_device_tty_dir = NULL;
114 tty_device_reldir = NULL;
Yi Li45ad7672016-10-18 23:21:19 +0800115 tty_path_input = NULL;
116 tty_path_input_real = NULL;
117 tty_kname_real = NULL;
Jeremy Kerr17217842016-01-29 18:44:21 +0800118
Yi Li45ad7672016-10-18 23:21:19 +0800119 /* udev may rename the tty name with a symbol link, try to resolve */
120 rc = asprintf(&tty_path_input, "/dev/%s", console->tty_kname);
Jeremy Kerr17217842016-01-29 18:44:21 +0800121 if (rc < 0)
122 return -1;
123
Yi Li45ad7672016-10-18 23:21:19 +0800124 tty_path_input_real = realpath(tty_path_input, NULL);
125 if (!tty_path_input_real) {
126 warn("Can't find realpath for /dev/%s", console->tty_kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930127 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800128 goto out_free;
129 }
130
131 tty_kname_real = basename(tty_path_input_real);
132 if (!tty_kname_real) {
133 warn("Can't find real name for /dev/%s", console->tty_kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930134 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800135 goto out_free;
136 }
137
Andrew Jefferya72711a2023-04-18 18:19:41 +0930138 rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s",
139 tty_kname_real);
Yi Li45ad7672016-10-18 23:21:19 +0800140 if (rc < 0)
141 goto out_free;
142
Jeremy Kerr17217842016-01-29 18:44:21 +0800143 tty_device_tty_dir = realpath(tty_class_device_link, NULL);
Yi Li45ad7672016-10-18 23:21:19 +0800144 if (!tty_device_tty_dir) {
145 warn("Can't query sysfs for device %s", tty_kname_real);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930146 rc = -1;
Jeremy Kerr17217842016-01-29 18:44:21 +0800147 goto out_free;
148 }
149
150 rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
151 if (rc < 0)
152 goto out_free;
153
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800154 console->tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
155 if (!console->tty_sysfs_devnode)
Yi Li45ad7672016-10-18 23:21:19 +0800156 warn("Can't find parent device for %s", tty_kname_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800157
Yi Li45ad7672016-10-18 23:21:19 +0800158 rc = asprintf(&console->tty_dev, "/dev/%s", tty_kname_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800159 if (rc < 0)
160 goto out_free;
161
162 rc = 0;
163
164out_free:
165 free(tty_class_device_link);
166 free(tty_device_tty_dir);
167 free(tty_device_reldir);
Yi Li45ad7672016-10-18 23:21:19 +0800168 free(tty_path_input);
169 free(tty_path_input_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800170 return rc;
171}
172
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800173static int tty_set_sysfs_attr(struct console *console, const char *name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930174 int value)
Jeremy Kerr957818b2016-03-08 14:35:15 +0800175{
176 char *path;
177 FILE *fp;
178 int rc;
179
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800180 rc = asprintf(&path, "%s/%s", console->tty_sysfs_devnode, name);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800181 if (rc < 0)
182 return -1;
183
184 fp = fopen(path, "w");
185 if (!fp) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930186 warn("Can't access attribute %s on device %s", name,
187 console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800188 rc = -1;
189 goto out_free;
190 }
191 setvbuf(fp, NULL, _IONBF, 0);
192
193 rc = fprintf(fp, "0x%x", value);
194 if (rc < 0)
Andrew Jefferya72711a2023-04-18 18:19:41 +0930195 warn("Error writing to %s attribute of device %s", name,
196 console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800197 fclose(fp);
198
Jeremy Kerr957818b2016-03-08 14:35:15 +0800199out_free:
200 free(path);
201 return rc;
202}
203
Jeremy Kerrd831f962016-01-29 17:18:01 +0800204/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700205 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800206 */
207static void tty_init_termios(struct console *console)
208{
209 struct termios termios;
210 int rc;
211
212 rc = tcgetattr(console->tty_fd, &termios);
213 if (rc) {
214 warn("Can't read tty termios");
215 return;
216 }
217
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700218 if (console->tty_baud) {
219 if (cfsetspeed(&termios, console->tty_baud) < 0)
220 warn("Couldn't set speeds for %s", console->tty_kname);
221 }
222
223 /* Set console to raw mode: we don't want any processing to occur on
224 * the underlying terminal input/output.
225 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800226 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700227
Jeremy Kerr54e95692016-03-24 17:07:56 +0800228 rc = tcsetattr(console->tty_fd, TCSANOW, &termios);
229 if (rc)
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700230 warn("Can't set terminal options for %s", console->tty_kname);
Jeremy Kerr54e95692016-03-24 17:07:56 +0800231}
232
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800233static void tty_change_baudrate(struct console *console)
234{
235 struct handler *handler;
236 int i, rc;
237
238 tty_init_termios(console);
239
240 for (i = 0; i < console->n_handlers; i++) {
241 handler = console->handlers[i];
242 if (!handler->baudrate)
243 continue;
244
245 rc = handler->baudrate(handler, console->tty_baud);
246 if (rc)
247 warnx("Can't set terminal baudrate for handler %s",
Andrew Jefferya72711a2023-04-18 18:19:41 +0930248 handler->name);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800249 }
250}
251
Jeremy Kerr54e95692016-03-24 17:07:56 +0800252/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800253 * Open and initialise the serial device
254 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800255static int tty_init_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800256{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800257 if (console->tty_sirq)
258 tty_set_sysfs_attr(console, "sirq", console->tty_sirq);
259 if (console->tty_lpc_addr)
260 tty_set_sysfs_attr(console, "lpc_address",
Andrew Jefferya72711a2023-04-18 18:19:41 +0930261 console->tty_lpc_addr);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800262
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800263 console->tty_fd = open(console->tty_dev, O_RDWR);
264 if (console->tty_fd <= 0) {
265 warn("Can't open tty %s", console->tty_dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800266 return -1;
267 }
268
269 /* Disable character delay. We may want to later enable this when
270 * we detect larger amounts of data
271 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800272 fcntl(console->tty_fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800273
Jeremy Kerr54e95692016-03-24 17:07:56 +0800274 tty_init_termios(console);
275
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800276 console->pollfds[console->n_pollers].fd = console->tty_fd;
277 console->pollfds[console->n_pollers].events = POLLIN;
278
Jeremy Kerrd831f962016-01-29 17:18:01 +0800279 return 0;
280}
281
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800282static int tty_init(struct console *console, struct config *config)
283{
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930284 unsigned long parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800285 const char *val;
286 char *endp;
287 int rc;
288
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800289 val = config_get_value(config, "lpc-address");
290 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930291 errno = 0;
292 parsed = strtoul(val, &endp, 0);
293 if (parsed == ULONG_MAX && errno == ERANGE) {
294 warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'",
295 val);
296 return -1;
297 }
298
299 if (parsed > UINT16_MAX) {
300 warn("Invalid LPC address '%s'", val);
301 return -1;
302 }
303
304 console->tty_lpc_addr = (uint16_t)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800305 if (endp == optarg) {
306 warn("Invalid LPC address: '%s'", val);
307 return -1;
308 }
309 }
310
311 val = config_get_value(config, "sirq");
312 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930313 errno = 0;
314 parsed = strtoul(val, &endp, 0);
315 if (parsed == ULONG_MAX && errno == ERANGE) {
316 warn("Cannot interpret 'sirq' value as an unsigned long: '%s'",
317 val);
318 }
319
320 if (parsed > 16)
321 warn("Invalid LPC SERIRQ: '%s'", val);
322
323 console->tty_sirq = (int)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800324 if (endp == optarg)
325 warn("Invalid sirq: '%s'", val);
326 }
327
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700328 val = config_get_value(config, "baud");
329 if (val) {
330 if (config_parse_baud(&console->tty_baud, val))
331 warnx("Invalid baud rate: '%s'", val);
332 }
333
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800334 if (!console->tty_kname) {
335 warnx("Error: No TTY device specified");
336 return -1;
337 }
338
339 rc = tty_find_device(console);
340 if (rc)
341 return rc;
342
343 rc = tty_init_io(console);
344 return rc;
345}
346
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800347int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800348{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800349 return write_buf_to_fd(console->tty_fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800350}
351
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800352static int method_set_baud_rate(sd_bus_message *msg, void *userdata,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930353 sd_bus_error *err)
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800354{
355 struct console *console = userdata;
356 uint32_t baudrate;
357 speed_t speed;
358 int r;
359
360 if (!console) {
361 sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
362 return sd_bus_reply_method_return(msg, "x", 0);
363 }
364
365 r = sd_bus_message_read(msg, "u", &baudrate);
366 if (r < 0) {
367 sd_bus_error_set_const(err, DBUS_ERR, "Bad message");
368 return sd_bus_reply_method_return(msg, "x", -EINVAL);
369 }
370
371 speed = parse_int_to_baud(baudrate);
372 if (!speed) {
373 warnx("Invalid baud rate: '%u'", baudrate);
374 return sd_bus_reply_method_return(msg, "x", -EINVAL);
375 }
376
377 console->tty_baud = speed;
378 tty_change_baudrate(console);
379
380 return sd_bus_reply_method_return(msg, "x", r);
381}
382
Andrew Jefferyfd048322023-04-18 12:02:01 +0930383static int get_handler(sd_bus *bus __attribute__((unused)),
384 const char *path __attribute__((unused)),
385 const char *interface __attribute__((unused)),
386 const char *property __attribute__((unused)),
387 sd_bus_message *reply, void *userdata,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930388 sd_bus_error *error __attribute__((unused)))
389{
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800390 struct console *console = userdata;
391 uint32_t baudrate;
392 int r;
393
394 baudrate = parse_baud_to_int(console->tty_baud);
395 if (!baudrate)
396 warnx("Invalid baud rate: '%d'", console->tty_baud);
397
398 r = sd_bus_message_append(reply, "u", baudrate);
399
400 return r;
401}
402
403static const sd_bus_vtable console_vtable[] = {
404 SD_BUS_VTABLE_START(0),
405 SD_BUS_METHOD("setBaudRate", "u", "x", method_set_baud_rate,
406 SD_BUS_VTABLE_UNPRIVILEGED),
407 SD_BUS_PROPERTY("baudrate", "u", get_handler, 0, 0),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930408 SD_BUS_VTABLE_END,
409};
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800410
Andrew Jefferya72711a2023-04-18 18:19:41 +0930411static void dbus_init(struct console *console,
412 struct config *config __attribute__((unused)))
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800413{
414 int dbus_poller = 0;
415 int fd, r;
416
417 if (!console) {
418 warnx("Couldn't get valid console");
419 return;
420 }
421
422 r = sd_bus_default_system(&console->bus);
423 if (r < 0) {
424 warnx("Failed to connect to system bus: %s", strerror(-r));
425 return;
426 }
427
428 r = sd_bus_add_object_vtable(console->bus, NULL, OBJ_NAME, DBUS_NAME,
429 console_vtable, console);
430 if (r < 0) {
431 warnx("Failed to issue method call: %s", strerror(-r));
432 return;
433 }
434
Andrew Jefferya72711a2023-04-18 18:19:41 +0930435 r = sd_bus_request_name(console->bus, DBUS_NAME,
436 SD_BUS_NAME_ALLOW_REPLACEMENT |
437 SD_BUS_NAME_REPLACE_EXISTING);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800438 if (r < 0) {
439 warnx("Failed to acquire service name: %s", strerror(-r));
440 return;
441 }
442
443 fd = sd_bus_get_fd(console->bus);
444 if (fd < 0) {
445 warnx("Couldn't get the bus file descriptor");
446 return;
447 }
448
449 dbus_poller = POLLFD_DBUS;
450
451 console->pollfds[dbus_poller].fd = fd;
452 console->pollfds[dbus_poller].events = POLLIN;
453}
454
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800455static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800456{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800457 extern struct handler *__start_handlers, *__stop_handlers;
458 struct handler *handler;
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800459 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800460
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800461 console->n_handlers = &__stop_handlers - &__start_handlers;
462 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800463
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930464 printf("%ld handler%s\n", console->n_handlers,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930465 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800466
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800467 for (i = 0; i < console->n_handlers; i++) {
468 handler = console->handlers[i];
469
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800470 rc = 0;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800471 if (handler->init)
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800472 rc = handler->init(handler, console, config);
473
474 handler->active = rc == 0;
475
476 printf(" %s [%sactive]\n", handler->name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930477 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800478 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800479}
480
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800481static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800482{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800483 struct handler *handler;
484 int i;
485
486 for (i = 0; i < console->n_handlers; i++) {
487 handler = console->handlers[i];
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800488 if (handler->fini && handler->active)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800489 handler->fini(handler);
490 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800491}
492
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800493static int get_current_time(struct timeval *tv)
494{
495 struct timespec t;
496 int rc;
497
498 /*
499 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
500 * local time changes. However, a struct timeval is more
501 * convenient for calculations, so convert to that.
502 */
503 rc = clock_gettime(CLOCK_MONOTONIC, &t);
504 if (rc)
505 return rc;
506
507 tv->tv_sec = t.tv_sec;
508 tv->tv_usec = t.tv_nsec / 1000;
509
510 return 0;
511}
512
Andrew Jefferya72711a2023-04-18 18:19:41 +0930513struct ringbuffer_consumer *
514console_ringbuffer_consumer_register(struct console *console,
515 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800516{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800517 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800518}
519
Jeremy Kerr55c97122017-02-07 17:06:46 +0800520struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930521 struct handler *handler,
522 poller_event_fn_t poller_fn,
523 poller_timeout_fn_t timeout_fn, int fd,
524 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800525{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800526 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930527 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800528
529 poller = malloc(sizeof(*poller));
530 poller->remove = false;
531 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800532 poller->event_fn = poller_fn;
533 poller->timeout_fn = timeout_fn;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800534 poller->data = data;
535
536 /* add one to our pollers array */
537 n = console->n_pollers++;
Andrew Jefferya72711a2023-04-18 18:19:41 +0930538 console->pollers =
539 realloc(console->pollers,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800540 sizeof(*console->pollers) * console->n_pollers);
541
542 console->pollers[n] = poller;
543
544 /* increase pollfds array too */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930545 console->pollfds =
546 realloc(console->pollfds,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800547 sizeof(*console->pollfds) *
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800548 (MAX_INTERNAL_POLLFD + console->n_pollers));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800549
550 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930551 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
552 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800553
554 console->pollfds[n].fd = fd;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930555 console->pollfds[n].events = (short)(events & 0x7fff);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800556
557 return poller;
558}
559
Andrew Jefferya72711a2023-04-18 18:19:41 +0930560void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800561{
562 int i;
563
564 /* find the entry in our pollers array */
565 for (i = 0; i < console->n_pollers; i++)
566 if (console->pollers[i] == poller)
567 break;
568
569 assert(i < console->n_pollers);
570
571 console->n_pollers--;
572
573 /* remove the item from the pollers array... */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930574 memmove(&console->pollers[i], &console->pollers[i + 1],
575 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800576
Andrew Jefferya72711a2023-04-18 18:19:41 +0930577 console->pollers =
578 realloc(console->pollers,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800579 sizeof(*console->pollers) * console->n_pollers);
580
581 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930582 memmove(&console->pollfds[i], &console->pollfds[i + 1],
583 sizeof(*console->pollfds) *
584 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800585
Andrew Jefferya72711a2023-04-18 18:19:41 +0930586 console->pollfds =
587 realloc(console->pollfds,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800588 sizeof(*console->pollfds) *
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800589 (MAX_INTERNAL_POLLFD + console->n_pollers));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800590
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800591 free(poller);
592}
593
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800594void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930595 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800596{
597 int i;
598
599 /* find the entry in our pollers array */
600 for (i = 0; i < console->n_pollers; i++)
601 if (console->pollers[i] == poller)
602 break;
603
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930604 console->pollfds[i].events = (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800605}
606
Andrew Jefferyfd048322023-04-18 12:02:01 +0930607void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930608 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800609{
610 struct timeval now;
611 int rc;
612
613 rc = get_current_time(&now);
614 if (rc)
615 return;
616
617 timeradd(&now, tv, &poller->timeout);
618}
619
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930620static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800621{
622 struct timeval *earliest, interval;
623 struct poller *poller;
624 int i;
625
626 earliest = NULL;
627
628 for (i = 0; i < console->n_pollers; i++) {
629 poller = console->pollers[i];
630
631 if (poller->timeout_fn && timerisset(&poller->timeout) &&
632 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930633 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800634 // poller is buffering data and needs the poll
635 // function to timeout.
636 earliest = &poller->timeout;
637 }
638 }
639
640 if (earliest) {
641 if (timercmp(earliest, cur_time, >)) {
642 /* recalculate the timeout period, time period has
643 * not elapsed */
644 timersub(earliest, cur_time, &interval);
645 return ((interval.tv_sec * 1000) +
646 (interval.tv_usec / 1000));
647 } else {
648 /* return from poll immediately */
649 return 0;
650 }
651 } else {
652 /* poll indefinitely */
653 return -1;
654 }
655}
656
657static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800658{
659 struct poller *poller;
660 struct pollfd *pollfd;
661 enum poller_ret prc;
662 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800663
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800664 rc = 0;
665
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800666 /*
667 * Process poll events by iterating through the pollers and pollfds
668 * in-step, calling any pollers that we've found revents for.
669 */
670 for (i = 0; i < console->n_pollers; i++) {
671 poller = console->pollers[i];
672 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800673 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800674
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800675 /* process pending events... */
676 if (pollfd->revents) {
677 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930678 poller->data);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800679 if (prc == POLLER_EXIT)
680 rc = -1;
681 else if (prc == POLLER_REMOVE)
682 poller->remove = true;
683 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800684
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800685 if ((prc == POLLER_OK) && poller->timeout_fn &&
686 timerisset(&poller->timeout) &&
687 timercmp(&poller->timeout, cur_time, <=)) {
688 /* One of the ringbuffer consumers is buffering the
689 data stream. The amount of idle time the consumer
690 desired has expired. Process the buffered data for
691 transmission. */
692 timerclear(&poller->timeout);
693 prc = poller->timeout_fn(poller->handler, poller->data);
694 if (prc == POLLER_EXIT) {
695 rc = -1;
696 } else if (prc == POLLER_REMOVE) {
697 poller->remove = true;
698 }
699 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800700 }
701
702 /**
703 * Process deferred removals; restarting each time we unregister, as
704 * the array will have changed
705 */
706 for (;;) {
707 bool removed = false;
708
709 for (i = 0; i < console->n_pollers; i++) {
710 poller = console->pollers[i];
711 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800712 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800713 removed = true;
714 break;
715 }
716 }
717 if (!removed)
718 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800719 }
720
721 return rc;
722}
723
Jeremy Kerr769cee12016-03-15 17:53:56 +0800724static void sighandler(int signal)
725{
726 if (signal == SIGINT)
727 sigint = true;
728}
729
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800730int run_console(struct console *console)
731{
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930732 sighandler_t sighandler_save = signal(SIGINT, sighandler);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800733 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930734 long timeout;
735 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800736
737 rc = 0;
738
Jeremy Kerrd831f962016-01-29 17:18:01 +0800739 for (;;) {
740 uint8_t buf[4096];
741
Jeremy Kerr17641452017-02-07 22:09:13 +0800742 BUILD_ASSERT(sizeof(buf) <= buffer_size);
743
Jeremy Kerr769cee12016-03-15 17:53:56 +0800744 if (sigint) {
745 fprintf(stderr, "Received interrupt, exiting\n");
746 break;
747 }
748
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800749 rc = get_current_time(&tv);
750 if (rc) {
751 warn("Failed to read current time");
752 break;
753 }
754
755 timeout = get_poll_timeout(console, &tv);
756
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800757 rc = poll(console->pollfds,
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930758 console->n_pollers + MAX_INTERNAL_POLLFD,
759 (int)timeout);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800760
Jeremy Kerrd831f962016-01-29 17:18:01 +0800761 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800762 if (errno == EINTR) {
763 continue;
764 } else {
765 warn("poll error");
766 break;
767 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800768 }
769
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800770 /* process internal fd first */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800771 if (console->pollfds[console->n_pollers].revents) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800772 rc = read(console->tty_fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800773 if (rc <= 0) {
774 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800775 rc = -1;
776 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800777 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800778 rc = ringbuffer_queue(console->rb, buf, rc);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800779 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800780 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800781 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800782
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800783 if (console->pollfds[console->n_pollers + 1].revents) {
784 sd_bus_process(console->bus, NULL);
785 }
786
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800787 /* ... and then the pollers */
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800788 rc = call_pollers(console, &tv);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800789 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800790 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800791 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800792
793 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800794 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800795
796 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800797}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800798static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930799 { "config", required_argument, 0, 'c' },
800 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800801};
802
803int main(int argc, char **argv)
804{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800805 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500806 const char *config_tty_kname = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800807 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800808 struct config *config;
809 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800810
Jeremy Kerr957818b2016-03-08 14:35:15 +0800811 rc = -1;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800812
813 for (;;) {
814 int c, idx;
815
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800816 c = getopt_long(argc, argv, "c:", options, &idx);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800817 if (c == -1)
818 break;
819
820 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800821 case 'c':
822 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800823 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800824 case 'h':
825 case '?':
826 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800827 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800828 }
829 }
830
Andrew Jeffery91dde142020-02-12 22:46:27 +1030831 if (optind < argc)
832 config_tty_kname = argv[optind];
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500833
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800834 console = malloc(sizeof(struct console));
835 memset(console, 0, sizeof(*console));
Andrew Jefferya72711a2023-04-18 18:19:41 +0930836 console->pollfds =
837 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Jeremy Kerrf733c852017-02-07 18:40:10 +0800838 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800839
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800840 config = config_init(config_filename);
841 if (!config) {
842 warnx("Can't read configuration, exiting.");
843 goto out_free;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800844 }
845
Andrew Jeffery91dde142020-02-12 22:46:27 +1030846 if (!config_tty_kname)
847 config_tty_kname = config_get_value(config, "upstream-tty");
848
849 if (!config_tty_kname) {
850 warnx("No TTY device specified");
851 usage(argv[0]);
852 return EXIT_FAILURE;
853 }
854
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500855 console->tty_kname = config_tty_kname;
856
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800857 rc = tty_init(console, config);
Jeremy Kerr17217842016-01-29 18:44:21 +0800858 if (rc)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800859 goto out_config_fini;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800860
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800861 dbus_init(console, config);
862
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800863 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800864
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800865 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800866
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800867 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800868
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800869out_config_fini:
870 config_fini(config);
871
Jeremy Kerr957818b2016-03-08 14:35:15 +0800872out_free:
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800873 free(console->pollers);
874 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800875 free(console->tty_sysfs_devnode);
876 free(console->tty_dev);
877 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800878
879 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
880}