blob: 273f3cdde979e1afca84259d45693f055c65925d [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{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800455 extern struct handler *__start_handlers, *__stop_handlers;
456 struct handler *handler;
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800457 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800458
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800459 console->n_handlers = &__stop_handlers - &__start_handlers;
460 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800461
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930462 printf("%ld handler%s\n", console->n_handlers,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930463 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800464
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800465 for (i = 0; i < console->n_handlers; i++) {
466 handler = console->handlers[i];
467
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800468 rc = 0;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800469 if (handler->init)
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800470 rc = handler->init(handler, console, config);
471
472 handler->active = rc == 0;
473
474 printf(" %s [%sactive]\n", handler->name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930475 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800476 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800477}
478
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800479static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800480{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800481 struct handler *handler;
482 int i;
483
484 for (i = 0; i < console->n_handlers; i++) {
485 handler = console->handlers[i];
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800486 if (handler->fini && handler->active)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800487 handler->fini(handler);
488 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800489}
490
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800491static int get_current_time(struct timeval *tv)
492{
493 struct timespec t;
494 int rc;
495
496 /*
497 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
498 * local time changes. However, a struct timeval is more
499 * convenient for calculations, so convert to that.
500 */
501 rc = clock_gettime(CLOCK_MONOTONIC, &t);
502 if (rc)
503 return rc;
504
505 tv->tv_sec = t.tv_sec;
506 tv->tv_usec = t.tv_nsec / 1000;
507
508 return 0;
509}
510
Andrew Jefferya72711a2023-04-18 18:19:41 +0930511struct ringbuffer_consumer *
512console_ringbuffer_consumer_register(struct console *console,
513 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800514{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800515 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800516}
517
Jeremy Kerr55c97122017-02-07 17:06:46 +0800518struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930519 struct handler *handler,
520 poller_event_fn_t poller_fn,
521 poller_timeout_fn_t timeout_fn, int fd,
522 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800523{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800524 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930525 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800526
527 poller = malloc(sizeof(*poller));
528 poller->remove = false;
529 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800530 poller->event_fn = poller_fn;
531 poller->timeout_fn = timeout_fn;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800532 poller->data = data;
533
534 /* add one to our pollers array */
535 n = console->n_pollers++;
Andrew Jefferya72711a2023-04-18 18:19:41 +0930536 console->pollers =
537 realloc(console->pollers,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800538 sizeof(*console->pollers) * console->n_pollers);
539
540 console->pollers[n] = poller;
541
542 /* increase pollfds array too */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930543 console->pollfds =
544 realloc(console->pollfds,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800545 sizeof(*console->pollfds) *
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800546 (MAX_INTERNAL_POLLFD + console->n_pollers));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800547
548 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930549 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
550 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800551
552 console->pollfds[n].fd = fd;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930553 console->pollfds[n].events = (short)(events & 0x7fff);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800554
555 return poller;
556}
557
Andrew Jefferya72711a2023-04-18 18:19:41 +0930558void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800559{
560 int i;
561
562 /* find the entry in our pollers array */
563 for (i = 0; i < console->n_pollers; i++)
564 if (console->pollers[i] == poller)
565 break;
566
567 assert(i < console->n_pollers);
568
569 console->n_pollers--;
570
571 /* remove the item from the pollers array... */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930572 memmove(&console->pollers[i], &console->pollers[i + 1],
573 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800574
Andrew Jefferya72711a2023-04-18 18:19:41 +0930575 console->pollers =
576 realloc(console->pollers,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800577 sizeof(*console->pollers) * console->n_pollers);
578
579 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930580 memmove(&console->pollfds[i], &console->pollfds[i + 1],
581 sizeof(*console->pollfds) *
582 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800583
Andrew Jefferya72711a2023-04-18 18:19:41 +0930584 console->pollfds =
585 realloc(console->pollfds,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800586 sizeof(*console->pollfds) *
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800587 (MAX_INTERNAL_POLLFD + console->n_pollers));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800588
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800589 free(poller);
590}
591
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800592void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930593 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800594{
595 int i;
596
597 /* find the entry in our pollers array */
598 for (i = 0; i < console->n_pollers; i++)
599 if (console->pollers[i] == poller)
600 break;
601
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930602 console->pollfds[i].events = (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800603}
604
Andrew Jefferyfd048322023-04-18 12:02:01 +0930605void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930606 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800607{
608 struct timeval now;
609 int rc;
610
611 rc = get_current_time(&now);
612 if (rc)
613 return;
614
615 timeradd(&now, tv, &poller->timeout);
616}
617
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930618static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800619{
620 struct timeval *earliest, interval;
621 struct poller *poller;
622 int i;
623
624 earliest = NULL;
625
626 for (i = 0; i < console->n_pollers; i++) {
627 poller = console->pollers[i];
628
629 if (poller->timeout_fn && timerisset(&poller->timeout) &&
630 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930631 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800632 // poller is buffering data and needs the poll
633 // function to timeout.
634 earliest = &poller->timeout;
635 }
636 }
637
638 if (earliest) {
639 if (timercmp(earliest, cur_time, >)) {
640 /* recalculate the timeout period, time period has
641 * not elapsed */
642 timersub(earliest, cur_time, &interval);
643 return ((interval.tv_sec * 1000) +
644 (interval.tv_usec / 1000));
645 } else {
646 /* return from poll immediately */
647 return 0;
648 }
649 } else {
650 /* poll indefinitely */
651 return -1;
652 }
653}
654
655static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800656{
657 struct poller *poller;
658 struct pollfd *pollfd;
659 enum poller_ret prc;
660 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800661
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800662 rc = 0;
663
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800664 /*
665 * Process poll events by iterating through the pollers and pollfds
666 * in-step, calling any pollers that we've found revents for.
667 */
668 for (i = 0; i < console->n_pollers; i++) {
669 poller = console->pollers[i];
670 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800671 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800672
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800673 /* process pending events... */
674 if (pollfd->revents) {
675 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930676 poller->data);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800677 if (prc == POLLER_EXIT)
678 rc = -1;
679 else if (prc == POLLER_REMOVE)
680 poller->remove = true;
681 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800682
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800683 if ((prc == POLLER_OK) && poller->timeout_fn &&
684 timerisset(&poller->timeout) &&
685 timercmp(&poller->timeout, cur_time, <=)) {
686 /* One of the ringbuffer consumers is buffering the
687 data stream. The amount of idle time the consumer
688 desired has expired. Process the buffered data for
689 transmission. */
690 timerclear(&poller->timeout);
691 prc = poller->timeout_fn(poller->handler, poller->data);
692 if (prc == POLLER_EXIT) {
693 rc = -1;
694 } else if (prc == POLLER_REMOVE) {
695 poller->remove = true;
696 }
697 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800698 }
699
700 /**
701 * Process deferred removals; restarting each time we unregister, as
702 * the array will have changed
703 */
704 for (;;) {
705 bool removed = false;
706
707 for (i = 0; i < console->n_pollers; i++) {
708 poller = console->pollers[i];
709 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800710 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800711 removed = true;
712 break;
713 }
714 }
715 if (!removed)
716 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800717 }
718
719 return rc;
720}
721
Jeremy Kerr769cee12016-03-15 17:53:56 +0800722static void sighandler(int signal)
723{
724 if (signal == SIGINT)
725 sigint = true;
726}
727
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800728int run_console(struct console *console)
729{
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930730 sighandler_t sighandler_save = signal(SIGINT, sighandler);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800731 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930732 long timeout;
733 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800734
735 rc = 0;
736
Jeremy Kerrd831f962016-01-29 17:18:01 +0800737 for (;;) {
738 uint8_t buf[4096];
739
Jeremy Kerr17641452017-02-07 22:09:13 +0800740 BUILD_ASSERT(sizeof(buf) <= buffer_size);
741
Jeremy Kerr769cee12016-03-15 17:53:56 +0800742 if (sigint) {
743 fprintf(stderr, "Received interrupt, exiting\n");
744 break;
745 }
746
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800747 rc = get_current_time(&tv);
748 if (rc) {
749 warn("Failed to read current time");
750 break;
751 }
752
753 timeout = get_poll_timeout(console, &tv);
754
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800755 rc = poll(console->pollfds,
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930756 console->n_pollers + MAX_INTERNAL_POLLFD,
757 (int)timeout);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800758
Jeremy Kerrd831f962016-01-29 17:18:01 +0800759 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800760 if (errno == EINTR) {
761 continue;
762 } else {
763 warn("poll error");
764 break;
765 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800766 }
767
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800768 /* process internal fd first */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800769 if (console->pollfds[console->n_pollers].revents) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800770 rc = read(console->tty_fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800771 if (rc <= 0) {
772 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800773 rc = -1;
774 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800775 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800776 rc = ringbuffer_queue(console->rb, buf, rc);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800777 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800778 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800779 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800780
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800781 if (console->pollfds[console->n_pollers + 1].revents) {
782 sd_bus_process(console->bus, NULL);
783 }
784
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800785 /* ... and then the pollers */
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800786 rc = call_pollers(console, &tv);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800787 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800788 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800789 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800790
791 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800792 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800793
794 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800795}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800796static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930797 { "config", required_argument, 0, 'c' },
798 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800799};
800
801int main(int argc, char **argv)
802{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800803 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500804 const char *config_tty_kname = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800805 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800806 struct config *config;
807 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800808
Jeremy Kerr957818b2016-03-08 14:35:15 +0800809 rc = -1;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800810
811 for (;;) {
812 int c, idx;
813
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800814 c = getopt_long(argc, argv, "c:", options, &idx);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800815 if (c == -1)
816 break;
817
818 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800819 case 'c':
820 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800821 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800822 case 'h':
823 case '?':
824 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800825 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800826 }
827 }
828
Andrew Jeffery91dde142020-02-12 22:46:27 +1030829 if (optind < argc)
830 config_tty_kname = argv[optind];
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500831
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800832 console = malloc(sizeof(struct console));
833 memset(console, 0, sizeof(*console));
Andrew Jefferya72711a2023-04-18 18:19:41 +0930834 console->pollfds =
835 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Jeremy Kerrf733c852017-02-07 18:40:10 +0800836 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800837
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800838 config = config_init(config_filename);
839 if (!config) {
840 warnx("Can't read configuration, exiting.");
841 goto out_free;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800842 }
843
Andrew Jeffery91dde142020-02-12 22:46:27 +1030844 if (!config_tty_kname)
845 config_tty_kname = config_get_value(config, "upstream-tty");
846
847 if (!config_tty_kname) {
848 warnx("No TTY device specified");
849 usage(argv[0]);
850 return EXIT_FAILURE;
851 }
852
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500853 console->tty_kname = config_tty_kname;
854
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800855 rc = tty_init(console, config);
Jeremy Kerr17217842016-01-29 18:44:21 +0800856 if (rc)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800857 goto out_config_fini;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800858
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800859 dbus_init(console, config);
860
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800861 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800862
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800863 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800864
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800865 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800866
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800867out_config_fini:
868 config_fini(config);
869
Jeremy Kerr957818b2016-03-08 14:35:15 +0800870out_free:
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800871 free(console->pollers);
872 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800873 free(console->tty_sysfs_devnode);
874 free(console->tty_dev);
875 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800876
877 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
878}