blob: 9da26b63bc71a70d03267a36f78b7abee8512d62 [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);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930119 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800120 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930121 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800122
Yi Li45ad7672016-10-18 23:21:19 +0800123 tty_path_input_real = realpath(tty_path_input, NULL);
124 if (!tty_path_input_real) {
125 warn("Can't find realpath for /dev/%s", console->tty_kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930126 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800127 goto out_free;
128 }
129
130 tty_kname_real = basename(tty_path_input_real);
131 if (!tty_kname_real) {
132 warn("Can't find real name for /dev/%s", console->tty_kname);
Andrew Jeffery15792aa2023-04-18 16:47:33 +0930133 rc = -1;
Yi Li45ad7672016-10-18 23:21:19 +0800134 goto out_free;
135 }
136
Andrew Jefferya72711a2023-04-18 18:19:41 +0930137 rc = asprintf(&tty_class_device_link, "/sys/class/tty/%s",
138 tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930139 if (rc < 0) {
Yi Li45ad7672016-10-18 23:21:19 +0800140 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930141 }
Yi Li45ad7672016-10-18 23:21:19 +0800142
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);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930151 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800152 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930153 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800154
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800155 console->tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930156 if (!console->tty_sysfs_devnode) {
Yi Li45ad7672016-10-18 23:21:19 +0800157 warn("Can't find parent device for %s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930158 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800159
Yi Li45ad7672016-10-18 23:21:19 +0800160 rc = asprintf(&console->tty_dev, "/dev/%s", tty_kname_real);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930161 if (rc < 0) {
Jeremy Kerr17217842016-01-29 18:44:21 +0800162 goto out_free;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930163 }
Jeremy Kerr17217842016-01-29 18:44:21 +0800164
165 rc = 0;
166
167out_free:
168 free(tty_class_device_link);
169 free(tty_device_tty_dir);
170 free(tty_device_reldir);
Yi Li45ad7672016-10-18 23:21:19 +0800171 free(tty_path_input);
172 free(tty_path_input_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800173 return rc;
174}
175
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800176static int tty_set_sysfs_attr(struct console *console, const char *name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930177 int value)
Jeremy Kerr957818b2016-03-08 14:35:15 +0800178{
179 char *path;
180 FILE *fp;
181 int rc;
182
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800183 rc = asprintf(&path, "%s/%s", console->tty_sysfs_devnode, name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930184 if (rc < 0) {
Jeremy Kerr957818b2016-03-08 14:35:15 +0800185 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930186 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800187
188 fp = fopen(path, "w");
189 if (!fp) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930190 warn("Can't access attribute %s on device %s", name,
191 console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800192 rc = -1;
193 goto out_free;
194 }
195 setvbuf(fp, NULL, _IONBF, 0);
196
197 rc = fprintf(fp, "0x%x", value);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930198 if (rc < 0) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930199 warn("Error writing to %s attribute of device %s", name,
200 console->tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930201 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800202 fclose(fp);
203
Jeremy Kerr957818b2016-03-08 14:35:15 +0800204out_free:
205 free(path);
206 return rc;
207}
208
Jeremy Kerrd831f962016-01-29 17:18:01 +0800209/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700210 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800211 */
212static void tty_init_termios(struct console *console)
213{
214 struct termios termios;
215 int rc;
216
217 rc = tcgetattr(console->tty_fd, &termios);
218 if (rc) {
219 warn("Can't read tty termios");
220 return;
221 }
222
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700223 if (console->tty_baud) {
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930224 if (cfsetspeed(&termios, console->tty_baud) < 0) {
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700225 warn("Couldn't set speeds for %s", console->tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930226 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700227 }
228
229 /* Set console to raw mode: we don't want any processing to occur on
230 * the underlying terminal input/output.
231 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800232 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700233
Jeremy Kerr54e95692016-03-24 17:07:56 +0800234 rc = tcsetattr(console->tty_fd, TCSANOW, &termios);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930235 if (rc) {
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700236 warn("Can't set terminal options for %s", console->tty_kname);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930237 }
Jeremy Kerr54e95692016-03-24 17:07:56 +0800238}
239
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800240static void tty_change_baudrate(struct console *console)
241{
242 struct handler *handler;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930243 int i;
244 int rc;
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800245
246 tty_init_termios(console);
247
248 for (i = 0; i < console->n_handlers; i++) {
249 handler = console->handlers[i];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930250 if (!handler->baudrate) {
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800251 continue;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930252 }
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800253
254 rc = handler->baudrate(handler, console->tty_baud);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930255 if (rc) {
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800256 warnx("Can't set terminal baudrate for handler %s",
Andrew Jefferya72711a2023-04-18 18:19:41 +0930257 handler->name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930258 }
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800259 }
260}
261
Jeremy Kerr54e95692016-03-24 17:07:56 +0800262/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800263 * Open and initialise the serial device
264 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800265static int tty_init_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800266{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930267 if (console->tty_sirq) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800268 tty_set_sysfs_attr(console, "sirq", console->tty_sirq);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930269 }
270 if (console->tty_lpc_addr) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800271 tty_set_sysfs_attr(console, "lpc_address",
Andrew Jefferya72711a2023-04-18 18:19:41 +0930272 console->tty_lpc_addr);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930273 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800274
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800275 console->tty_fd = open(console->tty_dev, O_RDWR);
276 if (console->tty_fd <= 0) {
277 warn("Can't open tty %s", console->tty_dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800278 return -1;
279 }
280
281 /* Disable character delay. We may want to later enable this when
282 * we detect larger amounts of data
283 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800284 fcntl(console->tty_fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800285
Jeremy Kerr54e95692016-03-24 17:07:56 +0800286 tty_init_termios(console);
287
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800288 console->pollfds[console->n_pollers].fd = console->tty_fd;
289 console->pollfds[console->n_pollers].events = POLLIN;
290
Jeremy Kerrd831f962016-01-29 17:18:01 +0800291 return 0;
292}
293
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800294static int tty_init(struct console *console, struct config *config)
295{
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930296 unsigned long parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800297 const char *val;
298 char *endp;
299 int rc;
300
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800301 val = config_get_value(config, "lpc-address");
302 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930303 errno = 0;
304 parsed = strtoul(val, &endp, 0);
305 if (parsed == ULONG_MAX && errno == ERANGE) {
306 warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'",
307 val);
308 return -1;
309 }
310
311 if (parsed > UINT16_MAX) {
312 warn("Invalid LPC address '%s'", val);
313 return -1;
314 }
315
316 console->tty_lpc_addr = (uint16_t)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800317 if (endp == optarg) {
318 warn("Invalid LPC address: '%s'", val);
319 return -1;
320 }
321 }
322
323 val = config_get_value(config, "sirq");
324 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930325 errno = 0;
326 parsed = strtoul(val, &endp, 0);
327 if (parsed == ULONG_MAX && errno == ERANGE) {
328 warn("Cannot interpret 'sirq' value as an unsigned long: '%s'",
329 val);
330 }
331
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930332 if (parsed > 16) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930333 warn("Invalid LPC SERIRQ: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930334 }
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930335
336 console->tty_sirq = (int)parsed;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930337 if (endp == optarg) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800338 warn("Invalid sirq: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930339 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800340 }
341
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700342 val = config_get_value(config, "baud");
343 if (val) {
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930344 if (config_parse_baud(&console->tty_baud, val)) {
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700345 warnx("Invalid baud rate: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930346 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700347 }
348
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800349 if (!console->tty_kname) {
350 warnx("Error: No TTY device specified");
351 return -1;
352 }
353
354 rc = tty_find_device(console);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930355 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800356 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930357 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800358
359 rc = tty_init_io(console);
360 return rc;
361}
362
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800363int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800364{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800365 return write_buf_to_fd(console->tty_fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800366}
367
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800368static int method_set_baud_rate(sd_bus_message *msg, void *userdata,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930369 sd_bus_error *err)
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800370{
371 struct console *console = userdata;
372 uint32_t baudrate;
373 speed_t speed;
374 int r;
375
376 if (!console) {
377 sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
378 return sd_bus_reply_method_return(msg, "x", 0);
379 }
380
381 r = sd_bus_message_read(msg, "u", &baudrate);
382 if (r < 0) {
383 sd_bus_error_set_const(err, DBUS_ERR, "Bad message");
384 return sd_bus_reply_method_return(msg, "x", -EINVAL);
385 }
386
387 speed = parse_int_to_baud(baudrate);
388 if (!speed) {
389 warnx("Invalid baud rate: '%u'", baudrate);
390 return sd_bus_reply_method_return(msg, "x", -EINVAL);
391 }
392
393 console->tty_baud = speed;
394 tty_change_baudrate(console);
395
396 return sd_bus_reply_method_return(msg, "x", r);
397}
398
Andrew Jefferyfd048322023-04-18 12:02:01 +0930399static int get_handler(sd_bus *bus __attribute__((unused)),
400 const char *path __attribute__((unused)),
401 const char *interface __attribute__((unused)),
402 const char *property __attribute__((unused)),
403 sd_bus_message *reply, void *userdata,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930404 sd_bus_error *error __attribute__((unused)))
405{
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800406 struct console *console = userdata;
407 uint32_t baudrate;
408 int r;
409
410 baudrate = parse_baud_to_int(console->tty_baud);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930411 if (!baudrate) {
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800412 warnx("Invalid baud rate: '%d'", console->tty_baud);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930413 }
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800414
415 r = sd_bus_message_append(reply, "u", baudrate);
416
417 return r;
418}
419
420static const sd_bus_vtable console_vtable[] = {
421 SD_BUS_VTABLE_START(0),
422 SD_BUS_METHOD("setBaudRate", "u", "x", method_set_baud_rate,
423 SD_BUS_VTABLE_UNPRIVILEGED),
424 SD_BUS_PROPERTY("baudrate", "u", get_handler, 0, 0),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930425 SD_BUS_VTABLE_END,
426};
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800427
Andrew Jefferya72711a2023-04-18 18:19:41 +0930428static void dbus_init(struct console *console,
429 struct config *config __attribute__((unused)))
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800430{
431 int dbus_poller = 0;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930432 int fd;
433 int r;
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800434
435 if (!console) {
436 warnx("Couldn't get valid console");
437 return;
438 }
439
440 r = sd_bus_default_system(&console->bus);
441 if (r < 0) {
442 warnx("Failed to connect to system bus: %s", strerror(-r));
443 return;
444 }
445
446 r = sd_bus_add_object_vtable(console->bus, NULL, OBJ_NAME, DBUS_NAME,
447 console_vtable, console);
448 if (r < 0) {
449 warnx("Failed to issue method call: %s", strerror(-r));
450 return;
451 }
452
Andrew Jefferya72711a2023-04-18 18:19:41 +0930453 r = sd_bus_request_name(console->bus, DBUS_NAME,
454 SD_BUS_NAME_ALLOW_REPLACEMENT |
455 SD_BUS_NAME_REPLACE_EXISTING);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800456 if (r < 0) {
457 warnx("Failed to acquire service name: %s", strerror(-r));
458 return;
459 }
460
461 fd = sd_bus_get_fd(console->bus);
462 if (fd < 0) {
463 warnx("Couldn't get the bus file descriptor");
464 return;
465 }
466
467 dbus_poller = POLLFD_DBUS;
468
469 console->pollfds[dbus_poller].fd = fd;
470 console->pollfds[dbus_poller].events = POLLIN;
471}
472
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800473static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800474{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930475 /* NOLINTBEGIN(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
476 extern struct handler *__start_handlers;
477 extern struct handler *__stop_handlers;
478 /* NOLINTEND(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800479 struct handler *handler;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930480 int i;
481 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800482
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800483 console->n_handlers = &__stop_handlers - &__start_handlers;
484 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800485
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930486 printf("%ld handler%s\n", console->n_handlers,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930487 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800488
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800489 for (i = 0; i < console->n_handlers; i++) {
490 handler = console->handlers[i];
491
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800492 rc = 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930493 if (handler->init) {
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800494 rc = handler->init(handler, console, config);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930495 }
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800496
497 handler->active = rc == 0;
498
499 printf(" %s [%sactive]\n", handler->name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930500 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800501 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800502}
503
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800504static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800505{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800506 struct handler *handler;
507 int i;
508
509 for (i = 0; i < console->n_handlers; i++) {
510 handler = console->handlers[i];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930511 if (handler->fini && handler->active) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800512 handler->fini(handler);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930513 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800514 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800515}
516
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800517static int get_current_time(struct timeval *tv)
518{
519 struct timespec t;
520 int rc;
521
522 /*
523 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
524 * local time changes. However, a struct timeval is more
525 * convenient for calculations, so convert to that.
526 */
527 rc = clock_gettime(CLOCK_MONOTONIC, &t);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930528 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800529 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930530 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800531
532 tv->tv_sec = t.tv_sec;
533 tv->tv_usec = t.tv_nsec / 1000;
534
535 return 0;
536}
537
Andrew Jefferya72711a2023-04-18 18:19:41 +0930538struct ringbuffer_consumer *
539console_ringbuffer_consumer_register(struct console *console,
540 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800541{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800542 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800543}
544
Jeremy Kerr55c97122017-02-07 17:06:46 +0800545struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930546 struct handler *handler,
547 poller_event_fn_t poller_fn,
548 poller_timeout_fn_t timeout_fn, int fd,
549 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800550{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800551 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930552 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800553
554 poller = malloc(sizeof(*poller));
555 poller->remove = false;
556 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800557 poller->event_fn = poller_fn;
558 poller->timeout_fn = timeout_fn;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800559 poller->data = data;
560
561 /* add one to our pollers array */
562 n = console->n_pollers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930563 /*
564 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
565 * pointer type.
566 */
567 /* NOLINTBEGIN(bugprone-sizeof-expression) */
568 console->pollers = reallocarray(console->pollers, console->n_pollers,
569 sizeof(*console->pollers));
570 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800571
572 console->pollers[n] = poller;
573
574 /* increase pollfds array too */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930575 console->pollfds =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930576 reallocarray(console->pollfds,
577 (MAX_INTERNAL_POLLFD + console->n_pollers),
578 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800579
580 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930581 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
582 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800583
584 console->pollfds[n].fd = fd;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930585 console->pollfds[n].events = (short)(events & 0x7fff);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800586
587 return poller;
588}
589
Andrew Jefferya72711a2023-04-18 18:19:41 +0930590void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800591{
592 int i;
593
594 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930595 for (i = 0; i < console->n_pollers; i++) {
596 if (console->pollers[i] == poller) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800597 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930598 }
599 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800600
601 assert(i < console->n_pollers);
602
603 console->n_pollers--;
604
Andrew Jeffery91b52172023-04-19 12:42:14 +0930605 /*
606 * Remove the item from the pollers array...
607 *
608 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
609 * pointer type.
610 */
611 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930612 memmove(&console->pollers[i], &console->pollers[i + 1],
613 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800614
Andrew Jeffery91b52172023-04-19 12:42:14 +0930615 console->pollers = reallocarray(console->pollers, console->n_pollers,
616 sizeof(*console->pollers));
617 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800618
619 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930620 memmove(&console->pollfds[i], &console->pollfds[i + 1],
621 sizeof(*console->pollfds) *
622 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800623
Andrew Jefferya72711a2023-04-18 18:19:41 +0930624 console->pollfds =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930625 reallocarray(console->pollfds,
626 (MAX_INTERNAL_POLLFD + console->n_pollers),
627 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800628
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800629 free(poller);
630}
631
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800632void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930633 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800634{
635 int i;
636
637 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930638 for (i = 0; i < console->n_pollers; i++) {
639 if (console->pollers[i] == poller) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800640 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930641 }
642 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800643
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930644 console->pollfds[i].events = (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800645}
646
Andrew Jefferyfd048322023-04-18 12:02:01 +0930647void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930648 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800649{
650 struct timeval now;
651 int rc;
652
653 rc = get_current_time(&now);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930654 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800655 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930656 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800657
658 timeradd(&now, tv, &poller->timeout);
659}
660
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930661static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800662{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930663 struct timeval *earliest;
664 struct timeval interval;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800665 struct poller *poller;
666 int i;
667
668 earliest = NULL;
669
670 for (i = 0; i < console->n_pollers; i++) {
671 poller = console->pollers[i];
672
673 if (poller->timeout_fn && timerisset(&poller->timeout) &&
674 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930675 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800676 // poller is buffering data and needs the poll
677 // function to timeout.
678 earliest = &poller->timeout;
679 }
680 }
681
682 if (earliest) {
683 if (timercmp(earliest, cur_time, >)) {
684 /* recalculate the timeout period, time period has
685 * not elapsed */
686 timersub(earliest, cur_time, &interval);
687 return ((interval.tv_sec * 1000) +
688 (interval.tv_usec / 1000));
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930689 } /* return from poll immediately */
690 return 0;
691
692 } /* poll indefinitely */
693 return -1;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800694}
695
696static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800697{
698 struct poller *poller;
699 struct pollfd *pollfd;
700 enum poller_ret prc;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930701 int i;
702 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800703
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800704 rc = 0;
705
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800706 /*
707 * Process poll events by iterating through the pollers and pollfds
708 * in-step, calling any pollers that we've found revents for.
709 */
710 for (i = 0; i < console->n_pollers; i++) {
711 poller = console->pollers[i];
712 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800713 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800714
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800715 /* process pending events... */
716 if (pollfd->revents) {
717 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930718 poller->data);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930719 if (prc == POLLER_EXIT) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800720 rc = -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930721 } else if (prc == POLLER_REMOVE) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800722 poller->remove = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930723 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800724 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800725
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800726 if ((prc == POLLER_OK) && poller->timeout_fn &&
727 timerisset(&poller->timeout) &&
728 timercmp(&poller->timeout, cur_time, <=)) {
729 /* One of the ringbuffer consumers is buffering the
730 data stream. The amount of idle time the consumer
731 desired has expired. Process the buffered data for
732 transmission. */
733 timerclear(&poller->timeout);
734 prc = poller->timeout_fn(poller->handler, poller->data);
735 if (prc == POLLER_EXIT) {
736 rc = -1;
737 } else if (prc == POLLER_REMOVE) {
738 poller->remove = true;
739 }
740 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800741 }
742
743 /**
744 * Process deferred removals; restarting each time we unregister, as
745 * the array will have changed
746 */
747 for (;;) {
748 bool removed = false;
749
750 for (i = 0; i < console->n_pollers; i++) {
751 poller = console->pollers[i];
752 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800753 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800754 removed = true;
755 break;
756 }
757 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930758 if (!removed) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800759 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930760 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800761 }
762
763 return rc;
764}
765
Jeremy Kerr769cee12016-03-15 17:53:56 +0800766static void sighandler(int signal)
767{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930768 if (signal == SIGINT) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800769 sigint = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930770 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800771}
772
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800773int run_console(struct console *console)
774{
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930775 sighandler_t sighandler_save = signal(SIGINT, sighandler);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800776 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930777 long timeout;
778 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800779
780 rc = 0;
781
Jeremy Kerrd831f962016-01-29 17:18:01 +0800782 for (;;) {
783 uint8_t buf[4096];
784
Jeremy Kerr17641452017-02-07 22:09:13 +0800785 BUILD_ASSERT(sizeof(buf) <= buffer_size);
786
Jeremy Kerr769cee12016-03-15 17:53:56 +0800787 if (sigint) {
788 fprintf(stderr, "Received interrupt, exiting\n");
789 break;
790 }
791
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800792 rc = get_current_time(&tv);
793 if (rc) {
794 warn("Failed to read current time");
795 break;
796 }
797
798 timeout = get_poll_timeout(console, &tv);
799
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800800 rc = poll(console->pollfds,
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930801 console->n_pollers + MAX_INTERNAL_POLLFD,
802 (int)timeout);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800803
Jeremy Kerrd831f962016-01-29 17:18:01 +0800804 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800805 if (errno == EINTR) {
806 continue;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800807 }
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930808 warn("poll error");
809 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800810 }
811
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800812 /* process internal fd first */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800813 if (console->pollfds[console->n_pollers].revents) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800814 rc = read(console->tty_fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800815 if (rc <= 0) {
816 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800817 rc = -1;
818 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800819 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800820 rc = ringbuffer_queue(console->rb, buf, rc);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930821 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800822 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930823 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800824 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800825
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800826 if (console->pollfds[console->n_pollers + 1].revents) {
827 sd_bus_process(console->bus, NULL);
828 }
829
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800830 /* ... and then the pollers */
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800831 rc = call_pollers(console, &tv);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930832 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800833 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930834 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800835 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800836
837 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800838 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800839
840 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800841}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800842static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930843 { "config", required_argument, 0, 'c' },
844 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800845};
846
847int main(int argc, char **argv)
848{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800849 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500850 const char *config_tty_kname = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800851 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800852 struct config *config;
853 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800854
Jeremy Kerr957818b2016-03-08 14:35:15 +0800855 rc = -1;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800856
857 for (;;) {
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930858 int c;
859 int idx;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800860
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800861 c = getopt_long(argc, argv, "c:", options, &idx);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930862 if (c == -1) {
Jeremy Kerrd831f962016-01-29 17:18:01 +0800863 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930864 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800865
866 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800867 case 'c':
868 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800869 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800870 case 'h':
871 case '?':
872 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800873 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800874 }
875 }
876
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930877 if (optind < argc) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030878 config_tty_kname = argv[optind];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930879 }
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500880
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800881 console = malloc(sizeof(struct console));
882 memset(console, 0, sizeof(*console));
Andrew Jefferya72711a2023-04-18 18:19:41 +0930883 console->pollfds =
884 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Jeremy Kerrf733c852017-02-07 18:40:10 +0800885 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800886
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800887 config = config_init(config_filename);
888 if (!config) {
889 warnx("Can't read configuration, exiting.");
890 goto out_free;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800891 }
892
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930893 if (!config_tty_kname) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030894 config_tty_kname = config_get_value(config, "upstream-tty");
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930895 }
Andrew Jeffery91dde142020-02-12 22:46:27 +1030896
897 if (!config_tty_kname) {
898 warnx("No TTY device specified");
899 usage(argv[0]);
900 return EXIT_FAILURE;
901 }
902
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500903 console->tty_kname = config_tty_kname;
904
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800905 rc = tty_init(console, config);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930906 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800907 goto out_config_fini;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930908 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800909
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800910 dbus_init(console, config);
911
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800912 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800913
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800914 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800915
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800916 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800917
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800918out_config_fini:
919 config_fini(config);
920
Jeremy Kerr957818b2016-03-08 14:35:15 +0800921out_free:
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800922 free(console->pollers);
923 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800924 free(console->tty_sysfs_devnode);
925 free(console->tty_dev);
926 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800927
928 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
929}