blob: 3c71904b45f7e0214a693fd9682fc43a3fa2d65e [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
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +080043#define DBUS_ERR "org.openbmc.error"
44#define DBUS_NAME "xyz.openbmc_project.console"
45#define OBJ_NAME "/xyz/openbmc_project/console"
46
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080047struct console {
Jeremy Kerr17217842016-01-29 18:44:21 +080048 const char *tty_kname;
49 char *tty_sysfs_devnode;
50 char *tty_dev;
Jeremy Kerr957818b2016-03-08 14:35:15 +080051 int tty_sirq;
52 int tty_lpc_addr;
Benjamin Fairc7fbcd42018-06-04 14:39:01 -070053 speed_t tty_baud;
Jeremy Kerrd831f962016-01-29 17:18:01 +080054 int tty_fd;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080055
Jeremy Kerrf733c852017-02-07 18:40:10 +080056 struct ringbuffer *rb;
57
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080058 struct handler **handlers;
59 int n_handlers;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080060
61 struct poller **pollers;
62 int n_pollers;
63
64 struct pollfd *pollfds;
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +080065 struct sd_bus *bus;
Jeremy Kerrd831f962016-01-29 17:18:01 +080066};
67
Jeremy Kerr329a35f2016-03-10 15:36:01 +080068struct poller {
69 struct handler *handler;
70 void *data;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080071 poller_event_fn_t event_fn;
72 poller_timeout_fn_t timeout_fn;
73 struct timeval timeout;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080074 bool remove;
75};
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 */
85const size_t buffer_size = 128 * 1024;
86
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,
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -050093"usage: %s [options] <DEVICE>\n"
Jeremy Kerrd831f962016-01-29 17:18:01 +080094"\n"
95"Options:\n"
Jeremy Kerrd66195c2016-03-16 17:24:51 +080096" --config <FILE> Use FILE for configuration\n"
Jeremy Kerrd831f962016-01-29 17:18:01 +080097"",
98 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
138 rc = asprintf(&tty_class_device_link,
139 "/sys/class/tty/%s", tty_kname_real);
140 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,
Jeremy Kerr957818b2016-03-08 14:35:15 +0800174 int value)
175{
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) {
186 warn("Can't access attribute %s on device %s",
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800187 name, 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)
195 warn("Error writing to %s attribute of device %s",
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800196 name, console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800197 fclose(fp);
198
199
200
201out_free:
202 free(path);
203 return rc;
204}
205
Jeremy Kerrd831f962016-01-29 17:18:01 +0800206/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700207 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800208 */
209static void tty_init_termios(struct console *console)
210{
211 struct termios termios;
212 int rc;
213
214 rc = tcgetattr(console->tty_fd, &termios);
215 if (rc) {
216 warn("Can't read tty termios");
217 return;
218 }
219
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700220 if (console->tty_baud) {
221 if (cfsetspeed(&termios, console->tty_baud) < 0)
222 warn("Couldn't set speeds for %s", console->tty_kname);
223 }
224
225 /* Set console to raw mode: we don't want any processing to occur on
226 * the underlying terminal input/output.
227 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800228 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700229
Jeremy Kerr54e95692016-03-24 17:07:56 +0800230 rc = tcsetattr(console->tty_fd, TCSANOW, &termios);
231 if (rc)
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700232 warn("Can't set terminal options for %s", console->tty_kname);
Jeremy Kerr54e95692016-03-24 17:07:56 +0800233}
234
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800235
236static void tty_change_baudrate(struct console *console)
237{
238 struct handler *handler;
239 int i, rc;
240
241 tty_init_termios(console);
242
243 for (i = 0; i < console->n_handlers; i++) {
244 handler = console->handlers[i];
245 if (!handler->baudrate)
246 continue;
247
248 rc = handler->baudrate(handler, console->tty_baud);
249 if (rc)
250 warnx("Can't set terminal baudrate for handler %s",
251 handler->name);
252 }
253}
254
Jeremy Kerr54e95692016-03-24 17:07:56 +0800255/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800256 * Open and initialise the serial device
257 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800258static int tty_init_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800259{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800260 if (console->tty_sirq)
261 tty_set_sysfs_attr(console, "sirq", console->tty_sirq);
262 if (console->tty_lpc_addr)
263 tty_set_sysfs_attr(console, "lpc_address",
264 console->tty_lpc_addr);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800265
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800266 console->tty_fd = open(console->tty_dev, O_RDWR);
267 if (console->tty_fd <= 0) {
268 warn("Can't open tty %s", console->tty_dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800269 return -1;
270 }
271
272 /* Disable character delay. We may want to later enable this when
273 * we detect larger amounts of data
274 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800275 fcntl(console->tty_fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800276
Jeremy Kerr54e95692016-03-24 17:07:56 +0800277 tty_init_termios(console);
278
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800279 console->pollfds[console->n_pollers].fd = console->tty_fd;
280 console->pollfds[console->n_pollers].events = POLLIN;
281
Jeremy Kerrd831f962016-01-29 17:18:01 +0800282 return 0;
283}
284
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800285static int tty_init(struct console *console, struct config *config)
286{
287 const char *val;
288 char *endp;
289 int rc;
290
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800291 val = config_get_value(config, "lpc-address");
292 if (val) {
293 console->tty_lpc_addr = strtoul(val, &endp, 0);
294 if (endp == optarg) {
295 warn("Invalid LPC address: '%s'", val);
296 return -1;
297 }
298 }
299
300 val = config_get_value(config, "sirq");
301 if (val) {
302 console->tty_sirq = strtoul(val, &endp, 0);
303 if (endp == optarg)
304 warn("Invalid sirq: '%s'", val);
305 }
306
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700307 val = config_get_value(config, "baud");
308 if (val) {
309 if (config_parse_baud(&console->tty_baud, val))
310 warnx("Invalid baud rate: '%s'", val);
311 }
312
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800313 if (!console->tty_kname) {
314 warnx("Error: No TTY device specified");
315 return -1;
316 }
317
318 rc = tty_find_device(console);
319 if (rc)
320 return rc;
321
322 rc = tty_init_io(console);
323 return rc;
324}
325
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800326
327int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800328{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800329 return write_buf_to_fd(console->tty_fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800330}
331
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800332static int method_set_baud_rate(sd_bus_message *msg, void *userdata,
333 sd_bus_error *err)
334{
335 struct console *console = userdata;
336 uint32_t baudrate;
337 speed_t speed;
338 int r;
339
340 if (!console) {
341 sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
342 return sd_bus_reply_method_return(msg, "x", 0);
343 }
344
345 r = sd_bus_message_read(msg, "u", &baudrate);
346 if (r < 0) {
347 sd_bus_error_set_const(err, DBUS_ERR, "Bad message");
348 return sd_bus_reply_method_return(msg, "x", -EINVAL);
349 }
350
351 speed = parse_int_to_baud(baudrate);
352 if (!speed) {
353 warnx("Invalid baud rate: '%u'", baudrate);
354 return sd_bus_reply_method_return(msg, "x", -EINVAL);
355 }
356
357 console->tty_baud = speed;
358 tty_change_baudrate(console);
359
360 return sd_bus_reply_method_return(msg, "x", r);
361}
362
Andrew Jefferyfd048322023-04-18 12:02:01 +0930363static int get_handler(sd_bus *bus __attribute__((unused)),
364 const char *path __attribute__((unused)),
365 const char *interface __attribute__((unused)),
366 const char *property __attribute__((unused)),
367 sd_bus_message *reply, void *userdata,
368 sd_bus_error *error __attribute__((unused))) {
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800369 struct console *console = userdata;
370 uint32_t baudrate;
371 int r;
372
373 baudrate = parse_baud_to_int(console->tty_baud);
374 if (!baudrate)
375 warnx("Invalid baud rate: '%d'", console->tty_baud);
376
377 r = sd_bus_message_append(reply, "u", baudrate);
378
379 return r;
380}
381
382static const sd_bus_vtable console_vtable[] = {
383 SD_BUS_VTABLE_START(0),
384 SD_BUS_METHOD("setBaudRate", "u", "x", method_set_baud_rate,
385 SD_BUS_VTABLE_UNPRIVILEGED),
386 SD_BUS_PROPERTY("baudrate", "u", get_handler, 0, 0),
387 SD_BUS_VTABLE_END,};
388
Andrew Jefferyfd048322023-04-18 12:02:01 +0930389static void dbus_init(struct console *console, struct config *config __attribute__((unused)))
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800390{
391 int dbus_poller = 0;
392 int fd, r;
393
394 if (!console) {
395 warnx("Couldn't get valid console");
396 return;
397 }
398
399 r = sd_bus_default_system(&console->bus);
400 if (r < 0) {
401 warnx("Failed to connect to system bus: %s", strerror(-r));
402 return;
403 }
404
405 r = sd_bus_add_object_vtable(console->bus, NULL, OBJ_NAME, DBUS_NAME,
406 console_vtable, console);
407 if (r < 0) {
408 warnx("Failed to issue method call: %s", strerror(-r));
409 return;
410 }
411
412 r = sd_bus_request_name(console->bus, DBUS_NAME, SD_BUS_NAME_ALLOW_REPLACEMENT
413 |SD_BUS_NAME_REPLACE_EXISTING);
414 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,
441 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,
453 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
Jeremy Kerrf733c852017-02-07 18:40:10 +0800489struct ringbuffer_consumer *console_ringbuffer_consumer_register(
490 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,
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800497 struct handler *handler, poller_event_fn_t poller_fn,
498 poller_timeout_fn_t timeout_fn, int fd,
499 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800500{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800501 struct poller *poller;
502 int n;
503
504 poller = malloc(sizeof(*poller));
505 poller->remove = false;
506 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800507 poller->event_fn = poller_fn;
508 poller->timeout_fn = timeout_fn;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800509 poller->data = data;
510
511 /* add one to our pollers array */
512 n = console->n_pollers++;
513 console->pollers = realloc(console->pollers,
514 sizeof(*console->pollers) * console->n_pollers);
515
516 console->pollers[n] = poller;
517
518 /* increase pollfds array too */
519 console->pollfds = realloc(console->pollfds,
520 sizeof(*console->pollfds) *
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800521 (MAX_INTERNAL_POLLFD + console->n_pollers));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800522
523 /* shift the end pollfds up by one */
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800524 memcpy(&console->pollfds[n+1],
525 &console->pollfds[n],
526 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800527
528 console->pollfds[n].fd = fd;
529 console->pollfds[n].events = events;
530
531 return poller;
532}
533
Jeremy Kerr55c97122017-02-07 17:06:46 +0800534void console_poller_unregister(struct console *console,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800535 struct poller *poller)
536{
537 int i;
538
539 /* find the entry in our pollers array */
540 for (i = 0; i < console->n_pollers; i++)
541 if (console->pollers[i] == poller)
542 break;
543
544 assert(i < console->n_pollers);
545
546 console->n_pollers--;
547
548 /* remove the item from the pollers array... */
549 memmove(&console->pollers[i], &console->pollers[i+1],
550 sizeof(*console->pollers)
551 * (console->n_pollers - i));
552
553 console->pollers = realloc(console->pollers,
554 sizeof(*console->pollers) * console->n_pollers);
555
556 /* ... and the pollfds array */
557 memmove(&console->pollfds[i], &console->pollfds[i+1],
558 sizeof(*console->pollfds) *
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800559 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800560
561 console->pollfds = realloc(console->pollfds,
562 sizeof(*console->pollfds) *
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800563 (MAX_INTERNAL_POLLFD + console->n_pollers));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800564
565
566 free(poller);
567}
568
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800569void console_poller_set_events(struct console *console, struct poller *poller,
570 int events)
571{
572 int i;
573
574 /* find the entry in our pollers array */
575 for (i = 0; i < console->n_pollers; i++)
576 if (console->pollers[i] == poller)
577 break;
578
579 console->pollfds[i].events = events;
580}
581
Andrew Jefferyfd048322023-04-18 12:02:01 +0930582void console_poller_set_timeout(struct console *console __attribute__((unused)),
583 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800584{
585 struct timeval now;
586 int rc;
587
588 rc = get_current_time(&now);
589 if (rc)
590 return;
591
592 timeradd(&now, tv, &poller->timeout);
593}
594
595static int get_poll_timeout(struct console *console, struct timeval *cur_time)
596{
597 struct timeval *earliest, interval;
598 struct poller *poller;
599 int i;
600
601 earliest = NULL;
602
603 for (i = 0; i < console->n_pollers; i++) {
604 poller = console->pollers[i];
605
606 if (poller->timeout_fn && timerisset(&poller->timeout) &&
607 (!earliest ||
608 (earliest && timercmp(&poller->timeout, earliest, <)))){
609 // poller is buffering data and needs the poll
610 // function to timeout.
611 earliest = &poller->timeout;
612 }
613 }
614
615 if (earliest) {
616 if (timercmp(earliest, cur_time, >)) {
617 /* recalculate the timeout period, time period has
618 * not elapsed */
619 timersub(earliest, cur_time, &interval);
620 return ((interval.tv_sec * 1000) +
621 (interval.tv_usec / 1000));
622 } else {
623 /* return from poll immediately */
624 return 0;
625 }
626 } else {
627 /* poll indefinitely */
628 return -1;
629 }
630}
631
632static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800633{
634 struct poller *poller;
635 struct pollfd *pollfd;
636 enum poller_ret prc;
637 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800638
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800639 rc = 0;
640
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800641 /*
642 * Process poll events by iterating through the pollers and pollfds
643 * in-step, calling any pollers that we've found revents for.
644 */
645 for (i = 0; i < console->n_pollers; i++) {
646 poller = console->pollers[i];
647 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800648 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800649
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800650 /* process pending events... */
651 if (pollfd->revents) {
652 prc = poller->event_fn(poller->handler, pollfd->revents,
653 poller->data);
654 if (prc == POLLER_EXIT)
655 rc = -1;
656 else if (prc == POLLER_REMOVE)
657 poller->remove = true;
658 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800659
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800660 if ((prc == POLLER_OK) && poller->timeout_fn &&
661 timerisset(&poller->timeout) &&
662 timercmp(&poller->timeout, cur_time, <=)) {
663 /* One of the ringbuffer consumers is buffering the
664 data stream. The amount of idle time the consumer
665 desired has expired. Process the buffered data for
666 transmission. */
667 timerclear(&poller->timeout);
668 prc = poller->timeout_fn(poller->handler, poller->data);
669 if (prc == POLLER_EXIT) {
670 rc = -1;
671 } else if (prc == POLLER_REMOVE) {
672 poller->remove = true;
673 }
674 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800675 }
676
677 /**
678 * Process deferred removals; restarting each time we unregister, as
679 * the array will have changed
680 */
681 for (;;) {
682 bool removed = false;
683
684 for (i = 0; i < console->n_pollers; i++) {
685 poller = console->pollers[i];
686 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800687 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800688 removed = true;
689 break;
690 }
691 }
692 if (!removed)
693 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800694 }
695
696 return rc;
697}
698
Jeremy Kerr769cee12016-03-15 17:53:56 +0800699static void sighandler(int signal)
700{
701 if (signal == SIGINT)
702 sigint = true;
703}
704
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800705int run_console(struct console *console)
706{
Jeremy Kerr769cee12016-03-15 17:53:56 +0800707 sighandler_t sighandler_save;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800708 struct timeval tv;
709 int rc, timeout;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800710
Jeremy Kerr769cee12016-03-15 17:53:56 +0800711 sighandler_save = signal(SIGINT, sighandler);
712
713 rc = 0;
714
Jeremy Kerrd831f962016-01-29 17:18:01 +0800715 for (;;) {
716 uint8_t buf[4096];
717
Jeremy Kerr17641452017-02-07 22:09:13 +0800718 BUILD_ASSERT(sizeof(buf) <= buffer_size);
719
Jeremy Kerr769cee12016-03-15 17:53:56 +0800720 if (sigint) {
721 fprintf(stderr, "Received interrupt, exiting\n");
722 break;
723 }
724
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800725 rc = get_current_time(&tv);
726 if (rc) {
727 warn("Failed to read current time");
728 break;
729 }
730
731 timeout = get_poll_timeout(console, &tv);
732
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800733 rc = poll(console->pollfds,
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800734 console->n_pollers + MAX_INTERNAL_POLLFD,
735 timeout);
736
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[] = {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800775 { "config", required_argument, 0, 'c'},
Joel Stanleyf5858b52016-03-18 16:33:03 +1030776 { 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));
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800812 console->pollfds = calloc(MAX_INTERNAL_POLLFD,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800813 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}