blob: bcbd4dbdebc40d738e00ff5ae67c71e451b66908 [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;
52 int tty_lpc_addr;
53 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;
59 int n_handlers;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080060
Andrew Jefferya72711a2023-04-18 18:19:41 +093061 struct poller **pollers;
62 int 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{
284 const char *val;
285 char *endp;
286 int rc;
287
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800288 val = config_get_value(config, "lpc-address");
289 if (val) {
290 console->tty_lpc_addr = strtoul(val, &endp, 0);
291 if (endp == optarg) {
292 warn("Invalid LPC address: '%s'", val);
293 return -1;
294 }
295 }
296
297 val = config_get_value(config, "sirq");
298 if (val) {
299 console->tty_sirq = strtoul(val, &endp, 0);
300 if (endp == optarg)
301 warn("Invalid sirq: '%s'", val);
302 }
303
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700304 val = config_get_value(config, "baud");
305 if (val) {
306 if (config_parse_baud(&console->tty_baud, val))
307 warnx("Invalid baud rate: '%s'", val);
308 }
309
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800310 if (!console->tty_kname) {
311 warnx("Error: No TTY device specified");
312 return -1;
313 }
314
315 rc = tty_find_device(console);
316 if (rc)
317 return rc;
318
319 rc = tty_init_io(console);
320 return rc;
321}
322
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800323int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800324{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800325 return write_buf_to_fd(console->tty_fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800326}
327
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800328static int method_set_baud_rate(sd_bus_message *msg, void *userdata,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930329 sd_bus_error *err)
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800330{
331 struct console *console = userdata;
332 uint32_t baudrate;
333 speed_t speed;
334 int r;
335
336 if (!console) {
337 sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
338 return sd_bus_reply_method_return(msg, "x", 0);
339 }
340
341 r = sd_bus_message_read(msg, "u", &baudrate);
342 if (r < 0) {
343 sd_bus_error_set_const(err, DBUS_ERR, "Bad message");
344 return sd_bus_reply_method_return(msg, "x", -EINVAL);
345 }
346
347 speed = parse_int_to_baud(baudrate);
348 if (!speed) {
349 warnx("Invalid baud rate: '%u'", baudrate);
350 return sd_bus_reply_method_return(msg, "x", -EINVAL);
351 }
352
353 console->tty_baud = speed;
354 tty_change_baudrate(console);
355
356 return sd_bus_reply_method_return(msg, "x", r);
357}
358
Andrew Jefferyfd048322023-04-18 12:02:01 +0930359static int get_handler(sd_bus *bus __attribute__((unused)),
360 const char *path __attribute__((unused)),
361 const char *interface __attribute__((unused)),
362 const char *property __attribute__((unused)),
363 sd_bus_message *reply, void *userdata,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930364 sd_bus_error *error __attribute__((unused)))
365{
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800366 struct console *console = userdata;
367 uint32_t baudrate;
368 int r;
369
370 baudrate = parse_baud_to_int(console->tty_baud);
371 if (!baudrate)
372 warnx("Invalid baud rate: '%d'", console->tty_baud);
373
374 r = sd_bus_message_append(reply, "u", baudrate);
375
376 return r;
377}
378
379static const sd_bus_vtable console_vtable[] = {
380 SD_BUS_VTABLE_START(0),
381 SD_BUS_METHOD("setBaudRate", "u", "x", method_set_baud_rate,
382 SD_BUS_VTABLE_UNPRIVILEGED),
383 SD_BUS_PROPERTY("baudrate", "u", get_handler, 0, 0),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930384 SD_BUS_VTABLE_END,
385};
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800386
Andrew Jefferya72711a2023-04-18 18:19:41 +0930387static void dbus_init(struct console *console,
388 struct config *config __attribute__((unused)))
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800389{
390 int dbus_poller = 0;
391 int fd, r;
392
393 if (!console) {
394 warnx("Couldn't get valid console");
395 return;
396 }
397
398 r = sd_bus_default_system(&console->bus);
399 if (r < 0) {
400 warnx("Failed to connect to system bus: %s", strerror(-r));
401 return;
402 }
403
404 r = sd_bus_add_object_vtable(console->bus, NULL, OBJ_NAME, DBUS_NAME,
405 console_vtable, console);
406 if (r < 0) {
407 warnx("Failed to issue method call: %s", strerror(-r));
408 return;
409 }
410
Andrew Jefferya72711a2023-04-18 18:19:41 +0930411 r = sd_bus_request_name(console->bus, DBUS_NAME,
412 SD_BUS_NAME_ALLOW_REPLACEMENT |
413 SD_BUS_NAME_REPLACE_EXISTING);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800414 if (r < 0) {
415 warnx("Failed to acquire service name: %s", strerror(-r));
416 return;
417 }
418
419 fd = sd_bus_get_fd(console->bus);
420 if (fd < 0) {
421 warnx("Couldn't get the bus file descriptor");
422 return;
423 }
424
425 dbus_poller = POLLFD_DBUS;
426
427 console->pollfds[dbus_poller].fd = fd;
428 console->pollfds[dbus_poller].events = POLLIN;
429}
430
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800431static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800432{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800433 extern struct handler *__start_handlers, *__stop_handlers;
434 struct handler *handler;
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800435 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800436
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800437 console->n_handlers = &__stop_handlers - &__start_handlers;
438 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800439
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800440 printf("%d handler%s\n", console->n_handlers,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930441 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800442
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800443 for (i = 0; i < console->n_handlers; i++) {
444 handler = console->handlers[i];
445
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800446 rc = 0;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800447 if (handler->init)
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800448 rc = handler->init(handler, console, config);
449
450 handler->active = rc == 0;
451
452 printf(" %s [%sactive]\n", handler->name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930453 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800454 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800455}
456
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800457static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800458{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800459 struct handler *handler;
460 int i;
461
462 for (i = 0; i < console->n_handlers; i++) {
463 handler = console->handlers[i];
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800464 if (handler->fini && handler->active)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800465 handler->fini(handler);
466 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800467}
468
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800469static int get_current_time(struct timeval *tv)
470{
471 struct timespec t;
472 int rc;
473
474 /*
475 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
476 * local time changes. However, a struct timeval is more
477 * convenient for calculations, so convert to that.
478 */
479 rc = clock_gettime(CLOCK_MONOTONIC, &t);
480 if (rc)
481 return rc;
482
483 tv->tv_sec = t.tv_sec;
484 tv->tv_usec = t.tv_nsec / 1000;
485
486 return 0;
487}
488
Andrew Jefferya72711a2023-04-18 18:19:41 +0930489struct ringbuffer_consumer *
490console_ringbuffer_consumer_register(struct console *console,
491 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800492{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800493 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800494}
495
Jeremy Kerr55c97122017-02-07 17:06:46 +0800496struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930497 struct handler *handler,
498 poller_event_fn_t poller_fn,
499 poller_timeout_fn_t timeout_fn, int fd,
500 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800501{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800502 struct poller *poller;
503 int n;
504
505 poller = malloc(sizeof(*poller));
506 poller->remove = false;
507 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800508 poller->event_fn = poller_fn;
509 poller->timeout_fn = timeout_fn;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800510 poller->data = data;
511
512 /* add one to our pollers array */
513 n = console->n_pollers++;
Andrew Jefferya72711a2023-04-18 18:19:41 +0930514 console->pollers =
515 realloc(console->pollers,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800516 sizeof(*console->pollers) * console->n_pollers);
517
518 console->pollers[n] = poller;
519
520 /* increase pollfds array too */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930521 console->pollfds =
522 realloc(console->pollfds,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800523 sizeof(*console->pollfds) *
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800524 (MAX_INTERNAL_POLLFD + console->n_pollers));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800525
526 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930527 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
528 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800529
530 console->pollfds[n].fd = fd;
531 console->pollfds[n].events = events;
532
533 return poller;
534}
535
Andrew Jefferya72711a2023-04-18 18:19:41 +0930536void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800537{
538 int i;
539
540 /* find the entry in our pollers array */
541 for (i = 0; i < console->n_pollers; i++)
542 if (console->pollers[i] == poller)
543 break;
544
545 assert(i < console->n_pollers);
546
547 console->n_pollers--;
548
549 /* remove the item from the pollers array... */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930550 memmove(&console->pollers[i], &console->pollers[i + 1],
551 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800552
Andrew Jefferya72711a2023-04-18 18:19:41 +0930553 console->pollers =
554 realloc(console->pollers,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800555 sizeof(*console->pollers) * console->n_pollers);
556
557 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930558 memmove(&console->pollfds[i], &console->pollfds[i + 1],
559 sizeof(*console->pollfds) *
560 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800561
Andrew Jefferya72711a2023-04-18 18:19:41 +0930562 console->pollfds =
563 realloc(console->pollfds,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800564 sizeof(*console->pollfds) *
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800565 (MAX_INTERNAL_POLLFD + console->n_pollers));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800566
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800567 free(poller);
568}
569
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800570void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930571 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800572{
573 int i;
574
575 /* find the entry in our pollers array */
576 for (i = 0; i < console->n_pollers; i++)
577 if (console->pollers[i] == poller)
578 break;
579
580 console->pollfds[i].events = events;
581}
582
Andrew Jefferyfd048322023-04-18 12:02:01 +0930583void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930584 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800585{
586 struct timeval now;
587 int rc;
588
589 rc = get_current_time(&now);
590 if (rc)
591 return;
592
593 timeradd(&now, tv, &poller->timeout);
594}
595
596static int get_poll_timeout(struct console *console, struct timeval *cur_time)
597{
598 struct timeval *earliest, interval;
599 struct poller *poller;
600 int i;
601
602 earliest = NULL;
603
604 for (i = 0; i < console->n_pollers; i++) {
605 poller = console->pollers[i];
606
607 if (poller->timeout_fn && timerisset(&poller->timeout) &&
608 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930609 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800610 // poller is buffering data and needs the poll
611 // function to timeout.
612 earliest = &poller->timeout;
613 }
614 }
615
616 if (earliest) {
617 if (timercmp(earliest, cur_time, >)) {
618 /* recalculate the timeout period, time period has
619 * not elapsed */
620 timersub(earliest, cur_time, &interval);
621 return ((interval.tv_sec * 1000) +
622 (interval.tv_usec / 1000));
623 } else {
624 /* return from poll immediately */
625 return 0;
626 }
627 } else {
628 /* poll indefinitely */
629 return -1;
630 }
631}
632
633static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800634{
635 struct poller *poller;
636 struct pollfd *pollfd;
637 enum poller_ret prc;
638 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800639
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800640 rc = 0;
641
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800642 /*
643 * Process poll events by iterating through the pollers and pollfds
644 * in-step, calling any pollers that we've found revents for.
645 */
646 for (i = 0; i < console->n_pollers; i++) {
647 poller = console->pollers[i];
648 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800649 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800650
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800651 /* process pending events... */
652 if (pollfd->revents) {
653 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930654 poller->data);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800655 if (prc == POLLER_EXIT)
656 rc = -1;
657 else if (prc == POLLER_REMOVE)
658 poller->remove = true;
659 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800660
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800661 if ((prc == POLLER_OK) && poller->timeout_fn &&
662 timerisset(&poller->timeout) &&
663 timercmp(&poller->timeout, cur_time, <=)) {
664 /* One of the ringbuffer consumers is buffering the
665 data stream. The amount of idle time the consumer
666 desired has expired. Process the buffered data for
667 transmission. */
668 timerclear(&poller->timeout);
669 prc = poller->timeout_fn(poller->handler, poller->data);
670 if (prc == POLLER_EXIT) {
671 rc = -1;
672 } else if (prc == POLLER_REMOVE) {
673 poller->remove = true;
674 }
675 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800676 }
677
678 /**
679 * Process deferred removals; restarting each time we unregister, as
680 * the array will have changed
681 */
682 for (;;) {
683 bool removed = false;
684
685 for (i = 0; i < console->n_pollers; i++) {
686 poller = console->pollers[i];
687 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800688 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800689 removed = true;
690 break;
691 }
692 }
693 if (!removed)
694 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800695 }
696
697 return rc;
698}
699
Jeremy Kerr769cee12016-03-15 17:53:56 +0800700static void sighandler(int signal)
701{
702 if (signal == SIGINT)
703 sigint = true;
704}
705
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800706int run_console(struct console *console)
707{
Jeremy Kerr769cee12016-03-15 17:53:56 +0800708 sighandler_t sighandler_save;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800709 struct timeval tv;
710 int rc, timeout;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800711
Jeremy Kerr769cee12016-03-15 17:53:56 +0800712 sighandler_save = signal(SIGINT, sighandler);
713
714 rc = 0;
715
Jeremy Kerrd831f962016-01-29 17:18:01 +0800716 for (;;) {
717 uint8_t buf[4096];
718
Jeremy Kerr17641452017-02-07 22:09:13 +0800719 BUILD_ASSERT(sizeof(buf) <= buffer_size);
720
Jeremy Kerr769cee12016-03-15 17:53:56 +0800721 if (sigint) {
722 fprintf(stderr, "Received interrupt, exiting\n");
723 break;
724 }
725
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800726 rc = get_current_time(&tv);
727 if (rc) {
728 warn("Failed to read current time");
729 break;
730 }
731
732 timeout = get_poll_timeout(console, &tv);
733
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800734 rc = poll(console->pollfds,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930735 console->n_pollers + MAX_INTERNAL_POLLFD, timeout);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800736
Jeremy Kerrd831f962016-01-29 17:18:01 +0800737 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800738 if (errno == EINTR) {
739 continue;
740 } else {
741 warn("poll error");
742 break;
743 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800744 }
745
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800746 /* process internal fd first */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800747 if (console->pollfds[console->n_pollers].revents) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800748 rc = read(console->tty_fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800749 if (rc <= 0) {
750 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800751 rc = -1;
752 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800753 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800754 rc = ringbuffer_queue(console->rb, buf, rc);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800755 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800756 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800757 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800758
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800759 if (console->pollfds[console->n_pollers + 1].revents) {
760 sd_bus_process(console->bus, NULL);
761 }
762
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800763 /* ... and then the pollers */
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800764 rc = call_pollers(console, &tv);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800765 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800766 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800767 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800768
769 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800770 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800771
772 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800773}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800774static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930775 { "config", required_argument, 0, 'c' },
776 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800777};
778
779int main(int argc, char **argv)
780{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800781 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500782 const char *config_tty_kname = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800783 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800784 struct config *config;
785 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800786
Jeremy Kerr957818b2016-03-08 14:35:15 +0800787 rc = -1;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800788
789 for (;;) {
790 int c, idx;
791
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800792 c = getopt_long(argc, argv, "c:", options, &idx);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800793 if (c == -1)
794 break;
795
796 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800797 case 'c':
798 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800799 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800800 case 'h':
801 case '?':
802 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800803 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800804 }
805 }
806
Andrew Jeffery91dde142020-02-12 22:46:27 +1030807 if (optind < argc)
808 config_tty_kname = argv[optind];
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500809
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800810 console = malloc(sizeof(struct console));
811 memset(console, 0, sizeof(*console));
Andrew Jefferya72711a2023-04-18 18:19:41 +0930812 console->pollfds =
813 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Jeremy Kerrf733c852017-02-07 18:40:10 +0800814 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800815
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800816 config = config_init(config_filename);
817 if (!config) {
818 warnx("Can't read configuration, exiting.");
819 goto out_free;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800820 }
821
Andrew Jeffery91dde142020-02-12 22:46:27 +1030822 if (!config_tty_kname)
823 config_tty_kname = config_get_value(config, "upstream-tty");
824
825 if (!config_tty_kname) {
826 warnx("No TTY device specified");
827 usage(argv[0]);
828 return EXIT_FAILURE;
829 }
830
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500831 console->tty_kname = config_tty_kname;
832
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800833 rc = tty_init(console, config);
Jeremy Kerr17217842016-01-29 18:44:21 +0800834 if (rc)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800835 goto out_config_fini;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800836
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800837 dbus_init(console, config);
838
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800839 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800840
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800841 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800842
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800843 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800844
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800845out_config_fini:
846 config_fini(config);
847
Jeremy Kerr957818b2016-03-08 14:35:15 +0800848out_free:
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800849 free(console->pollers);
850 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800851 free(console->tty_sysfs_devnode);
852 free(console->tty_dev);
853 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800854
855 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
856}