blob: 4521ef78b747ff5090f18e6d9febfd163fc1bf8f [file] [log] [blame]
Jeremy Kerrd831f962016-01-29 17:18:01 +08001/**
2 * Console server process for OpenBMC
3 *
Jeremy Kerr9326d772016-03-17 17:15:02 +08004 * Copyright © 2016 IBM Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
Jeremy Kerrd831f962016-01-29 17:18:01 +080017 */
18
Jeremy Kerr329a35f2016-03-10 15:36:01 +080019#include <assert.h>
Jeremy Kerr769cee12016-03-15 17:53:56 +080020#include <errno.h>
21#include <signal.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080022#include <stdint.h>
23#include <stdbool.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <fcntl.h>
27#include <unistd.h>
28#include <err.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080029#include <string.h>
30#include <getopt.h>
Jeremy Kerr17217842016-01-29 18:44:21 +080031#include <limits.h>
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080032#include <time.h>
Jeremy Kerr54e95692016-03-24 17:07:56 +080033#include <termios.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080034
35#include <sys/types.h>
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -080036#include <sys/time.h>
Joel Stanley87e344c2016-09-01 00:00:51 +093037#include <poll.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080038
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080039#include "console-server.h"
Jeremy Kerrd831f962016-01-29 17:18:01 +080040
Andrew Jefferya72711a2023-04-18 18:19:41 +093041#define DBUS_ERR "org.openbmc.error"
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +080042#define DBUS_NAME "xyz.openbmc_project.console"
Andrew Jefferya72711a2023-04-18 18:19:41 +093043#define OBJ_NAME "/xyz/openbmc_project/console"
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +080044
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080045struct console {
Andrew Jefferya72711a2023-04-18 18:19:41 +093046 const char *tty_kname;
47 char *tty_sysfs_devnode;
48 char *tty_dev;
49 int tty_sirq;
Andrew Jefferyfd883a82023-04-18 22:18:25 +093050 uint16_t tty_lpc_addr;
Andrew Jefferya72711a2023-04-18 18:19:41 +093051 speed_t tty_baud;
52 int tty_fd;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080053
Andrew Jefferya72711a2023-04-18 18:19:41 +093054 struct ringbuffer *rb;
Jeremy Kerrf733c852017-02-07 18:40:10 +080055
Andrew Jefferya72711a2023-04-18 18:19:41 +093056 struct handler **handlers;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093057 long n_handlers;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080058
Andrew Jefferya72711a2023-04-18 18:19:41 +093059 struct poller **pollers;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093060 long n_pollers;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080061
Andrew Jefferya72711a2023-04-18 18:19:41 +093062 struct pollfd *pollfds;
63 struct sd_bus *bus;
Jeremy Kerrd831f962016-01-29 17:18:01 +080064};
65
Jeremy Kerr329a35f2016-03-10 15:36:01 +080066struct poller {
Andrew Jefferya72711a2023-04-18 18:19:41 +093067 struct handler *handler;
68 void *data;
69 poller_event_fn_t event_fn;
70 poller_timeout_fn_t timeout_fn;
71 struct timeval timeout;
72 bool remove;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080073};
74
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +080075/* we have two extra entry in the pollfds array for the VUART tty */
76enum internal_pollfds {
77 POLLFD_HOSTTTY = 0,
78 POLLFD_DBUS = 1,
79 MAX_INTERNAL_POLLFD = 2,
80};
Jeremy Kerr329a35f2016-03-10 15:36:01 +080081
Jeremy Kerrf733c852017-02-07 18:40:10 +080082/* size of the shared backlog ringbuffer */
Andrew Jeffery5db8c792023-04-18 21:48:24 +093083const size_t buffer_size = 128ul * 1024ul;
Jeremy Kerrf733c852017-02-07 18:40:10 +080084
Jeremy Kerr769cee12016-03-15 17:53:56 +080085/* state shared with the signal handler */
86static bool sigint;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080087
Jeremy Kerrd831f962016-01-29 17:18:01 +080088static void usage(const char *progname)
89{
90 fprintf(stderr,
Andrew Jefferya72711a2023-04-18 18:19:41 +093091 "usage: %s [options] <DEVICE>\n"
92 "\n"
93 "Options:\n"
94 " --config <FILE> Use FILE for configuration\n"
95 "",
Jeremy Kerrd831f962016-01-29 17:18:01 +080096 progname);
97}
98
Jeremy Kerr17217842016-01-29 18:44:21 +080099/* populates tty_dev and tty_sysfs_devnode, using the tty kernel name */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800100static int tty_find_device(struct console *console)
Jeremy Kerr17217842016-01-29 18:44:21 +0800101{
102 char *tty_class_device_link;
103 char *tty_device_tty_dir;
104 char *tty_device_reldir;
Yi Li45ad7672016-10-18 23:21:19 +0800105 char *tty_path_input;
106 char *tty_path_input_real;
107 char *tty_kname_real;
Jeremy Kerr17217842016-01-29 18:44:21 +0800108 int rc;
109
Jeremy Kerr17217842016-01-29 18:44:21 +0800110 tty_class_device_link = NULL;
111 tty_device_tty_dir = NULL;
112 tty_device_reldir = NULL;
Yi Li45ad7672016-10-18 23:21:19 +0800113 tty_path_input = NULL;
114 tty_path_input_real = NULL;
115 tty_kname_real = NULL;
Jeremy Kerr17217842016-01-29 18:44:21 +0800116
Yi Li45ad7672016-10-18 23:21:19 +0800117 /* udev may rename the tty name with a symbol link, try to resolve */
118 rc = asprintf(&tty_path_input, "/dev/%s", console->tty_kname);
Jeremy Kerr17217842016-01-29 18:44:21 +0800119 if (rc < 0)
120 return -1;
121
Yi Li45ad7672016-10-18 23:21:19 +0800122 tty_path_input_real = realpath(tty_path_input, NULL);
123 if (!tty_path_input_real) {
124 warn("Can't find realpath for /dev/%s", console->tty_kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930125 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800126 goto out_free;
127 }
128
129 tty_kname_real = basename(tty_path_input_real);
130 if (!tty_kname_real) {
131 warn("Can't find real name for /dev/%s", console->tty_kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930132 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800133 goto out_free;
134 }
135
Andrew Jefferya72711a2023-04-18 18:19:41 +0930136 rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s",
137 tty_kname_real);
Yi Li45ad7672016-10-18 23:21:19 +0800138 if (rc < 0)
139 goto out_free;
140
Jeremy Kerr17217842016-01-29 18:44:21 +0800141 tty_device_tty_dir = realpath(tty_class_device_link, NULL);
Yi Li45ad7672016-10-18 23:21:19 +0800142 if (!tty_device_tty_dir) {
143 warn("Can't query sysfs for device %s", tty_kname_real);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930144 rc = -1;
Jeremy Kerr17217842016-01-29 18:44:21 +0800145 goto out_free;
146 }
147
148 rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
149 if (rc < 0)
150 goto out_free;
151
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800152 console->tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
153 if (!console->tty_sysfs_devnode)
Yi Li45ad7672016-10-18 23:21:19 +0800154 warn("Can't find parent device for %s", tty_kname_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800155
Yi Li45ad7672016-10-18 23:21:19 +0800156 rc = asprintf(&console->tty_dev, "/dev/%s", tty_kname_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800157 if (rc < 0)
158 goto out_free;
159
160 rc = 0;
161
162out_free:
163 free(tty_class_device_link);
164 free(tty_device_tty_dir);
165 free(tty_device_reldir);
Yi Li45ad7672016-10-18 23:21:19 +0800166 free(tty_path_input);
167 free(tty_path_input_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800168 return rc;
169}
170
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800171static int tty_set_sysfs_attr(struct console *console, const char *name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930172 int value)
Jeremy Kerr957818b2016-03-08 14:35:15 +0800173{
174 char *path;
175 FILE *fp;
176 int rc;
177
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800178 rc = asprintf(&path, "%s/%s", console->tty_sysfs_devnode, name);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800179 if (rc < 0)
180 return -1;
181
182 fp = fopen(path, "w");
183 if (!fp) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930184 warn("Can't access attribute %s on device %s", name,
185 console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800186 rc = -1;
187 goto out_free;
188 }
189 setvbuf(fp, NULL, _IONBF, 0);
190
191 rc = fprintf(fp, "0x%x", value);
192 if (rc < 0)
Andrew Jefferya72711a2023-04-18 18:19:41 +0930193 warn("Error writing to %s attribute of device %s", name,
194 console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800195 fclose(fp);
196
Jeremy Kerr957818b2016-03-08 14:35:15 +0800197out_free:
198 free(path);
199 return rc;
200}
201
Jeremy Kerrd831f962016-01-29 17:18:01 +0800202/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700203 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800204 */
205static void tty_init_termios(struct console *console)
206{
207 struct termios termios;
208 int rc;
209
210 rc = tcgetattr(console->tty_fd, &termios);
211 if (rc) {
212 warn("Can't read tty termios");
213 return;
214 }
215
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700216 if (console->tty_baud) {
217 if (cfsetspeed(&termios, console->tty_baud) < 0)
218 warn("Couldn't set speeds for %s", console->tty_kname);
219 }
220
221 /* Set console to raw mode: we don't want any processing to occur on
222 * the underlying terminal input/output.
223 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800224 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700225
Jeremy Kerr54e95692016-03-24 17:07:56 +0800226 rc = tcsetattr(console->tty_fd, TCSANOW, &termios);
227 if (rc)
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700228 warn("Can't set terminal options for %s", console->tty_kname);
Jeremy Kerr54e95692016-03-24 17:07:56 +0800229}
230
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800231static void tty_change_baudrate(struct console *console)
232{
233 struct handler *handler;
234 int i, rc;
235
236 tty_init_termios(console);
237
238 for (i = 0; i < console->n_handlers; i++) {
239 handler = console->handlers[i];
240 if (!handler->baudrate)
241 continue;
242
243 rc = handler->baudrate(handler, console->tty_baud);
244 if (rc)
245 warnx("Can't set terminal baudrate for handler %s",
Andrew Jefferya72711a2023-04-18 18:19:41 +0930246 handler->name);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800247 }
248}
249
Jeremy Kerr54e95692016-03-24 17:07:56 +0800250/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800251 * Open and initialise the serial device
252 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800253static int tty_init_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800254{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800255 if (console->tty_sirq)
256 tty_set_sysfs_attr(console, "sirq", console->tty_sirq);
257 if (console->tty_lpc_addr)
258 tty_set_sysfs_attr(console, "lpc_address",
Andrew Jefferya72711a2023-04-18 18:19:41 +0930259 console->tty_lpc_addr);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800260
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800261 console->tty_fd = open(console->tty_dev, O_RDWR);
262 if (console->tty_fd <= 0) {
263 warn("Can't open tty %s", console->tty_dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800264 return -1;
265 }
266
267 /* Disable character delay. We may want to later enable this when
268 * we detect larger amounts of data
269 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800270 fcntl(console->tty_fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800271
Jeremy Kerr54e95692016-03-24 17:07:56 +0800272 tty_init_termios(console);
273
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800274 console->pollfds[console->n_pollers].fd = console->tty_fd;
275 console->pollfds[console->n_pollers].events = POLLIN;
276
Jeremy Kerrd831f962016-01-29 17:18:01 +0800277 return 0;
278}
279
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800280static int tty_init(struct console *console, struct config *config)
281{
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930282 unsigned long parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800283 const char *val;
284 char *endp;
285 int rc;
286
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800287 val = config_get_value(config, "lpc-address");
288 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930289 errno = 0;
290 parsed = strtoul(val, &endp, 0);
291 if (parsed == ULONG_MAX && errno == ERANGE) {
292 warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'",
293 val);
294 return -1;
295 }
296
297 if (parsed > UINT16_MAX) {
298 warn("Invalid LPC address '%s'", val);
299 return -1;
300 }
301
302 console->tty_lpc_addr = (uint16_t)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800303 if (endp == optarg) {
304 warn("Invalid LPC address: '%s'", val);
305 return -1;
306 }
307 }
308
309 val = config_get_value(config, "sirq");
310 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930311 errno = 0;
312 parsed = strtoul(val, &endp, 0);
313 if (parsed == ULONG_MAX && errno == ERANGE) {
314 warn("Cannot interpret 'sirq' value as an unsigned long: '%s'",
315 val);
316 }
317
318 if (parsed > 16)
319 warn("Invalid LPC SERIRQ: '%s'", val);
320
321 console->tty_sirq = (int)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800322 if (endp == optarg)
323 warn("Invalid sirq: '%s'", val);
324 }
325
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700326 val = config_get_value(config, "baud");
327 if (val) {
328 if (config_parse_baud(&console->tty_baud, val))
329 warnx("Invalid baud rate: '%s'", val);
330 }
331
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800332 if (!console->tty_kname) {
333 warnx("Error: No TTY device specified");
334 return -1;
335 }
336
337 rc = tty_find_device(console);
338 if (rc)
339 return rc;
340
341 rc = tty_init_io(console);
342 return rc;
343}
344
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800345int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800346{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800347 return write_buf_to_fd(console->tty_fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800348}
349
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800350static int method_set_baud_rate(sd_bus_message *msg, void *userdata,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930351 sd_bus_error *err)
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800352{
353 struct console *console = userdata;
354 uint32_t baudrate;
355 speed_t speed;
356 int r;
357
358 if (!console) {
359 sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
360 return sd_bus_reply_method_return(msg, "x", 0);
361 }
362
363 r = sd_bus_message_read(msg, "u", &baudrate);
364 if (r < 0) {
365 sd_bus_error_set_const(err, DBUS_ERR, "Bad message");
366 return sd_bus_reply_method_return(msg, "x", -EINVAL);
367 }
368
369 speed = parse_int_to_baud(baudrate);
370 if (!speed) {
371 warnx("Invalid baud rate: '%u'", baudrate);
372 return sd_bus_reply_method_return(msg, "x", -EINVAL);
373 }
374
375 console->tty_baud = speed;
376 tty_change_baudrate(console);
377
378 return sd_bus_reply_method_return(msg, "x", r);
379}
380
Andrew Jefferyfd048322023-04-18 12:02:01 +0930381static int get_handler(sd_bus *bus __attribute__((unused)),
382 const char *path __attribute__((unused)),
383 const char *interface __attribute__((unused)),
384 const char *property __attribute__((unused)),
385 sd_bus_message *reply, void *userdata,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930386 sd_bus_error *error __attribute__((unused)))
387{
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800388 struct console *console = userdata;
389 uint32_t baudrate;
390 int r;
391
392 baudrate = parse_baud_to_int(console->tty_baud);
393 if (!baudrate)
394 warnx("Invalid baud rate: '%d'", console->tty_baud);
395
396 r = sd_bus_message_append(reply, "u", baudrate);
397
398 return r;
399}
400
401static const sd_bus_vtable console_vtable[] = {
402 SD_BUS_VTABLE_START(0),
403 SD_BUS_METHOD("setBaudRate", "u", "x", method_set_baud_rate,
404 SD_BUS_VTABLE_UNPRIVILEGED),
405 SD_BUS_PROPERTY("baudrate", "u", get_handler, 0, 0),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930406 SD_BUS_VTABLE_END,
407};
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800408
Andrew Jefferya72711a2023-04-18 18:19:41 +0930409static void dbus_init(struct console *console,
410 struct config *config __attribute__((unused)))
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800411{
412 int dbus_poller = 0;
413 int fd, r;
414
415 if (!console) {
416 warnx("Couldn't get valid console");
417 return;
418 }
419
420 r = sd_bus_default_system(&console->bus);
421 if (r < 0) {
422 warnx("Failed to connect to system bus: %s", strerror(-r));
423 return;
424 }
425
426 r = sd_bus_add_object_vtable(console->bus, NULL, OBJ_NAME, DBUS_NAME,
427 console_vtable, console);
428 if (r < 0) {
429 warnx("Failed to issue method call: %s", strerror(-r));
430 return;
431 }
432
Andrew Jefferya72711a2023-04-18 18:19:41 +0930433 r = sd_bus_request_name(console->bus, DBUS_NAME,
434 SD_BUS_NAME_ALLOW_REPLACEMENT |
435 SD_BUS_NAME_REPLACE_EXISTING);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800436 if (r < 0) {
437 warnx("Failed to acquire service name: %s", strerror(-r));
438 return;
439 }
440
441 fd = sd_bus_get_fd(console->bus);
442 if (fd < 0) {
443 warnx("Couldn't get the bus file descriptor");
444 return;
445 }
446
447 dbus_poller = POLLFD_DBUS;
448
449 console->pollfds[dbus_poller].fd = fd;
450 console->pollfds[dbus_poller].events = POLLIN;
451}
452
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800453static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800454{
Andrew Jeffery750fb0c2023-04-19 11:48:50 +0930455 /* NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800456 extern struct handler *__start_handlers, *__stop_handlers;
457 struct handler *handler;
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800458 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800459
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800460 console->n_handlers = &__stop_handlers - &__start_handlers;
461 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800462
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930463 printf("%ld handler%s\n", console->n_handlers,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930464 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800465
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800466 for (i = 0; i < console->n_handlers; i++) {
467 handler = console->handlers[i];
468
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800469 rc = 0;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800470 if (handler->init)
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800471 rc = handler->init(handler, console, config);
472
473 handler->active = rc == 0;
474
475 printf(" %s [%sactive]\n", handler->name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930476 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800477 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800478}
479
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800480static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800481{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800482 struct handler *handler;
483 int i;
484
485 for (i = 0; i < console->n_handlers; i++) {
486 handler = console->handlers[i];
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800487 if (handler->fini && handler->active)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800488 handler->fini(handler);
489 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800490}
491
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800492static int get_current_time(struct timeval *tv)
493{
494 struct timespec t;
495 int rc;
496
497 /*
498 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
499 * local time changes. However, a struct timeval is more
500 * convenient for calculations, so convert to that.
501 */
502 rc = clock_gettime(CLOCK_MONOTONIC, &t);
503 if (rc)
504 return rc;
505
506 tv->tv_sec = t.tv_sec;
507 tv->tv_usec = t.tv_nsec / 1000;
508
509 return 0;
510}
511
Andrew Jefferya72711a2023-04-18 18:19:41 +0930512struct ringbuffer_consumer *
513console_ringbuffer_consumer_register(struct console *console,
514 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800515{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800516 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800517}
518
Jeremy Kerr55c97122017-02-07 17:06:46 +0800519struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930520 struct handler *handler,
521 poller_event_fn_t poller_fn,
522 poller_timeout_fn_t timeout_fn, int fd,
523 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800524{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800525 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930526 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800527
528 poller = malloc(sizeof(*poller));
529 poller->remove = false;
530 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800531 poller->event_fn = poller_fn;
532 poller->timeout_fn = timeout_fn;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800533 poller->data = data;
534
535 /* add one to our pollers array */
536 n = console->n_pollers++;
Andrew Jefferya72711a2023-04-18 18:19:41 +0930537 console->pollers =
538 realloc(console->pollers,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800539 sizeof(*console->pollers) * console->n_pollers);
540
541 console->pollers[n] = poller;
542
543 /* increase pollfds array too */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930544 console->pollfds =
545 realloc(console->pollfds,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800546 sizeof(*console->pollfds) *
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800547 (MAX_INTERNAL_POLLFD + console->n_pollers));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800548
549 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930550 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
551 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800552
553 console->pollfds[n].fd = fd;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930554 console->pollfds[n].events = (short)(events & 0x7fff);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800555
556 return poller;
557}
558
Andrew Jefferya72711a2023-04-18 18:19:41 +0930559void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800560{
561 int i;
562
563 /* find the entry in our pollers array */
564 for (i = 0; i < console->n_pollers; i++)
565 if (console->pollers[i] == poller)
566 break;
567
568 assert(i < console->n_pollers);
569
570 console->n_pollers--;
571
572 /* remove the item from the pollers array... */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930573 memmove(&console->pollers[i], &console->pollers[i + 1],
574 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800575
Andrew Jefferya72711a2023-04-18 18:19:41 +0930576 console->pollers =
577 realloc(console->pollers,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800578 sizeof(*console->pollers) * console->n_pollers);
579
580 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930581 memmove(&console->pollfds[i], &console->pollfds[i + 1],
582 sizeof(*console->pollfds) *
583 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800584
Andrew Jefferya72711a2023-04-18 18:19:41 +0930585 console->pollfds =
586 realloc(console->pollfds,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800587 sizeof(*console->pollfds) *
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800588 (MAX_INTERNAL_POLLFD + console->n_pollers));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800589
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800590 free(poller);
591}
592
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800593void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930594 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800595{
596 int i;
597
598 /* find the entry in our pollers array */
599 for (i = 0; i < console->n_pollers; i++)
600 if (console->pollers[i] == poller)
601 break;
602
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930603 console->pollfds[i].events = (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800604}
605
Andrew Jefferyfd048322023-04-18 12:02:01 +0930606void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930607 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800608{
609 struct timeval now;
610 int rc;
611
612 rc = get_current_time(&now);
613 if (rc)
614 return;
615
616 timeradd(&now, tv, &poller->timeout);
617}
618
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930619static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800620{
621 struct timeval *earliest, interval;
622 struct poller *poller;
623 int i;
624
625 earliest = NULL;
626
627 for (i = 0; i < console->n_pollers; i++) {
628 poller = console->pollers[i];
629
630 if (poller->timeout_fn && timerisset(&poller->timeout) &&
631 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930632 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800633 // poller is buffering data and needs the poll
634 // function to timeout.
635 earliest = &poller->timeout;
636 }
637 }
638
639 if (earliest) {
640 if (timercmp(earliest, cur_time, >)) {
641 /* recalculate the timeout period, time period has
642 * not elapsed */
643 timersub(earliest, cur_time, &interval);
644 return ((interval.tv_sec * 1000) +
645 (interval.tv_usec / 1000));
646 } else {
647 /* return from poll immediately */
648 return 0;
649 }
650 } else {
651 /* poll indefinitely */
652 return -1;
653 }
654}
655
656static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800657{
658 struct poller *poller;
659 struct pollfd *pollfd;
660 enum poller_ret prc;
661 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800662
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800663 rc = 0;
664
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800665 /*
666 * Process poll events by iterating through the pollers and pollfds
667 * in-step, calling any pollers that we've found revents for.
668 */
669 for (i = 0; i < console->n_pollers; i++) {
670 poller = console->pollers[i];
671 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800672 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800673
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800674 /* process pending events... */
675 if (pollfd->revents) {
676 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930677 poller->data);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800678 if (prc == POLLER_EXIT)
679 rc = -1;
680 else if (prc == POLLER_REMOVE)
681 poller->remove = true;
682 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800683
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800684 if ((prc == POLLER_OK) && poller->timeout_fn &&
685 timerisset(&poller->timeout) &&
686 timercmp(&poller->timeout, cur_time, <=)) {
687 /* One of the ringbuffer consumers is buffering the
688 data stream. The amount of idle time the consumer
689 desired has expired. Process the buffered data for
690 transmission. */
691 timerclear(&poller->timeout);
692 prc = poller->timeout_fn(poller->handler, poller->data);
693 if (prc == POLLER_EXIT) {
694 rc = -1;
695 } else if (prc == POLLER_REMOVE) {
696 poller->remove = true;
697 }
698 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800699 }
700
701 /**
702 * Process deferred removals; restarting each time we unregister, as
703 * the array will have changed
704 */
705 for (;;) {
706 bool removed = false;
707
708 for (i = 0; i < console->n_pollers; i++) {
709 poller = console->pollers[i];
710 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800711 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800712 removed = true;
713 break;
714 }
715 }
716 if (!removed)
717 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800718 }
719
720 return rc;
721}
722
Jeremy Kerr769cee12016-03-15 17:53:56 +0800723static void sighandler(int signal)
724{
725 if (signal == SIGINT)
726 sigint = true;
727}
728
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800729int run_console(struct console *console)
730{
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930731 sighandler_t sighandler_save = signal(SIGINT, sighandler);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800732 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930733 long timeout;
734 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800735
736 rc = 0;
737
Jeremy Kerrd831f962016-01-29 17:18:01 +0800738 for (;;) {
739 uint8_t buf[4096];
740
Jeremy Kerr17641452017-02-07 22:09:13 +0800741 BUILD_ASSERT(sizeof(buf) <= buffer_size);
742
Jeremy Kerr769cee12016-03-15 17:53:56 +0800743 if (sigint) {
744 fprintf(stderr, "Received interrupt, exiting\n");
745 break;
746 }
747
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800748 rc = get_current_time(&tv);
749 if (rc) {
750 warn("Failed to read current time");
751 break;
752 }
753
754 timeout = get_poll_timeout(console, &tv);
755
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800756 rc = poll(console->pollfds,
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930757 console->n_pollers + MAX_INTERNAL_POLLFD,
758 (int)timeout);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800759
Jeremy Kerrd831f962016-01-29 17:18:01 +0800760 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800761 if (errno == EINTR) {
762 continue;
763 } else {
764 warn("poll error");
765 break;
766 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800767 }
768
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800769 /* process internal fd first */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800770 if (console->pollfds[console->n_pollers].revents) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800771 rc = read(console->tty_fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800772 if (rc <= 0) {
773 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800774 rc = -1;
775 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800776 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800777 rc = ringbuffer_queue(console->rb, buf, rc);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800778 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800779 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800780 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800781
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800782 if (console->pollfds[console->n_pollers + 1].revents) {
783 sd_bus_process(console->bus, NULL);
784 }
785
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800786 /* ... and then the pollers */
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800787 rc = call_pollers(console, &tv);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800788 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800789 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800790 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800791
792 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800793 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800794
795 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800796}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800797static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930798 { "config", required_argument, 0, 'c' },
799 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800800};
801
802int main(int argc, char **argv)
803{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800804 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500805 const char *config_tty_kname = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800806 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800807 struct config *config;
808 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800809
Jeremy Kerr957818b2016-03-08 14:35:15 +0800810 rc = -1;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800811
812 for (;;) {
813 int c, idx;
814
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800815 c = getopt_long(argc, argv, "c:", options, &idx);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800816 if (c == -1)
817 break;
818
819 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800820 case 'c':
821 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800822 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800823 case 'h':
824 case '?':
825 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800826 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800827 }
828 }
829
Andrew Jeffery91dde142020-02-12 22:46:27 +1030830 if (optind < argc)
831 config_tty_kname = argv[optind];
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500832
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800833 console = malloc(sizeof(struct console));
834 memset(console, 0, sizeof(*console));
Andrew Jefferya72711a2023-04-18 18:19:41 +0930835 console->pollfds =
836 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Jeremy Kerrf733c852017-02-07 18:40:10 +0800837 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800838
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800839 config = config_init(config_filename);
840 if (!config) {
841 warnx("Can't read configuration, exiting.");
842 goto out_free;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800843 }
844
Andrew Jeffery91dde142020-02-12 22:46:27 +1030845 if (!config_tty_kname)
846 config_tty_kname = config_get_value(config, "upstream-tty");
847
848 if (!config_tty_kname) {
849 warnx("No TTY device specified");
850 usage(argv[0]);
851 return EXIT_FAILURE;
852 }
853
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500854 console->tty_kname = config_tty_kname;
855
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800856 rc = tty_init(console, config);
Jeremy Kerr17217842016-01-29 18:44:21 +0800857 if (rc)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800858 goto out_config_fini;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800859
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800860 dbus_init(console, config);
861
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800862 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800863
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800864 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800865
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800866 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800867
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800868out_config_fini:
869 config_fini(config);
870
Jeremy Kerr957818b2016-03-08 14:35:15 +0800871out_free:
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800872 free(console->pollers);
873 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800874 free(console->tty_sysfs_devnode);
875 free(console->tty_dev);
876 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800877
878 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
879}