blob: 275034958a47180b2a679a3394ea5c95c4bb0bdb [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;
243 int i, rc;
244
245 tty_init_termios(console);
246
247 for (i = 0; i < console->n_handlers; i++) {
248 handler = console->handlers[i];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930249 if (!handler->baudrate) {
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800250 continue;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930251 }
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800252
253 rc = handler->baudrate(handler, console->tty_baud);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930254 if (rc) {
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800255 warnx("Can't set terminal baudrate for handler %s",
Andrew Jefferya72711a2023-04-18 18:19:41 +0930256 handler->name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930257 }
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800258 }
259}
260
Jeremy Kerr54e95692016-03-24 17:07:56 +0800261/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800262 * Open and initialise the serial device
263 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800264static int tty_init_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800265{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930266 if (console->tty_sirq) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800267 tty_set_sysfs_attr(console, "sirq", console->tty_sirq);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930268 }
269 if (console->tty_lpc_addr) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800270 tty_set_sysfs_attr(console, "lpc_address",
Andrew Jefferya72711a2023-04-18 18:19:41 +0930271 console->tty_lpc_addr);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930272 }
Jeremy Kerr957818b2016-03-08 14:35:15 +0800273
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800274 console->tty_fd = open(console->tty_dev, O_RDWR);
275 if (console->tty_fd <= 0) {
276 warn("Can't open tty %s", console->tty_dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800277 return -1;
278 }
279
280 /* Disable character delay. We may want to later enable this when
281 * we detect larger amounts of data
282 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800283 fcntl(console->tty_fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800284
Jeremy Kerr54e95692016-03-24 17:07:56 +0800285 tty_init_termios(console);
286
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800287 console->pollfds[console->n_pollers].fd = console->tty_fd;
288 console->pollfds[console->n_pollers].events = POLLIN;
289
Jeremy Kerrd831f962016-01-29 17:18:01 +0800290 return 0;
291}
292
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800293static int tty_init(struct console *console, struct config *config)
294{
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930295 unsigned long parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800296 const char *val;
297 char *endp;
298 int rc;
299
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800300 val = config_get_value(config, "lpc-address");
301 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930302 errno = 0;
303 parsed = strtoul(val, &endp, 0);
304 if (parsed == ULONG_MAX && errno == ERANGE) {
305 warn("Cannot interpret 'lpc-address' value as an unsigned long: '%s'",
306 val);
307 return -1;
308 }
309
310 if (parsed > UINT16_MAX) {
311 warn("Invalid LPC address '%s'", val);
312 return -1;
313 }
314
315 console->tty_lpc_addr = (uint16_t)parsed;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800316 if (endp == optarg) {
317 warn("Invalid LPC address: '%s'", val);
318 return -1;
319 }
320 }
321
322 val = config_get_value(config, "sirq");
323 if (val) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930324 errno = 0;
325 parsed = strtoul(val, &endp, 0);
326 if (parsed == ULONG_MAX && errno == ERANGE) {
327 warn("Cannot interpret 'sirq' value as an unsigned long: '%s'",
328 val);
329 }
330
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930331 if (parsed > 16) {
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930332 warn("Invalid LPC SERIRQ: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930333 }
Andrew Jefferyfd883a82023-04-18 22:18:25 +0930334
335 console->tty_sirq = (int)parsed;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930336 if (endp == optarg) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800337 warn("Invalid sirq: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930338 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800339 }
340
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700341 val = config_get_value(config, "baud");
342 if (val) {
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930343 if (config_parse_baud(&console->tty_baud, val)) {
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700344 warnx("Invalid baud rate: '%s'", val);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930345 }
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700346 }
347
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800348 if (!console->tty_kname) {
349 warnx("Error: No TTY device specified");
350 return -1;
351 }
352
353 rc = tty_find_device(console);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930354 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800355 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930356 }
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800357
358 rc = tty_init_io(console);
359 return rc;
360}
361
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800362int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800363{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800364 return write_buf_to_fd(console->tty_fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800365}
366
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800367static int method_set_baud_rate(sd_bus_message *msg, void *userdata,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930368 sd_bus_error *err)
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800369{
370 struct console *console = userdata;
371 uint32_t baudrate;
372 speed_t speed;
373 int r;
374
375 if (!console) {
376 sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
377 return sd_bus_reply_method_return(msg, "x", 0);
378 }
379
380 r = sd_bus_message_read(msg, "u", &baudrate);
381 if (r < 0) {
382 sd_bus_error_set_const(err, DBUS_ERR, "Bad message");
383 return sd_bus_reply_method_return(msg, "x", -EINVAL);
384 }
385
386 speed = parse_int_to_baud(baudrate);
387 if (!speed) {
388 warnx("Invalid baud rate: '%u'", baudrate);
389 return sd_bus_reply_method_return(msg, "x", -EINVAL);
390 }
391
392 console->tty_baud = speed;
393 tty_change_baudrate(console);
394
395 return sd_bus_reply_method_return(msg, "x", r);
396}
397
Andrew Jefferyfd048322023-04-18 12:02:01 +0930398static int get_handler(sd_bus *bus __attribute__((unused)),
399 const char *path __attribute__((unused)),
400 const char *interface __attribute__((unused)),
401 const char *property __attribute__((unused)),
402 sd_bus_message *reply, void *userdata,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930403 sd_bus_error *error __attribute__((unused)))
404{
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800405 struct console *console = userdata;
406 uint32_t baudrate;
407 int r;
408
409 baudrate = parse_baud_to_int(console->tty_baud);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930410 if (!baudrate) {
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800411 warnx("Invalid baud rate: '%d'", console->tty_baud);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930412 }
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800413
414 r = sd_bus_message_append(reply, "u", baudrate);
415
416 return r;
417}
418
419static const sd_bus_vtable console_vtable[] = {
420 SD_BUS_VTABLE_START(0),
421 SD_BUS_METHOD("setBaudRate", "u", "x", method_set_baud_rate,
422 SD_BUS_VTABLE_UNPRIVILEGED),
423 SD_BUS_PROPERTY("baudrate", "u", get_handler, 0, 0),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930424 SD_BUS_VTABLE_END,
425};
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800426
Andrew Jefferya72711a2023-04-18 18:19:41 +0930427static void dbus_init(struct console *console,
428 struct config *config __attribute__((unused)))
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800429{
430 int dbus_poller = 0;
431 int fd, r;
432
433 if (!console) {
434 warnx("Couldn't get valid console");
435 return;
436 }
437
438 r = sd_bus_default_system(&console->bus);
439 if (r < 0) {
440 warnx("Failed to connect to system bus: %s", strerror(-r));
441 return;
442 }
443
444 r = sd_bus_add_object_vtable(console->bus, NULL, OBJ_NAME, DBUS_NAME,
445 console_vtable, console);
446 if (r < 0) {
447 warnx("Failed to issue method call: %s", strerror(-r));
448 return;
449 }
450
Andrew Jefferya72711a2023-04-18 18:19:41 +0930451 r = sd_bus_request_name(console->bus, DBUS_NAME,
452 SD_BUS_NAME_ALLOW_REPLACEMENT |
453 SD_BUS_NAME_REPLACE_EXISTING);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800454 if (r < 0) {
455 warnx("Failed to acquire service name: %s", strerror(-r));
456 return;
457 }
458
459 fd = sd_bus_get_fd(console->bus);
460 if (fd < 0) {
461 warnx("Couldn't get the bus file descriptor");
462 return;
463 }
464
465 dbus_poller = POLLFD_DBUS;
466
467 console->pollfds[dbus_poller].fd = fd;
468 console->pollfds[dbus_poller].events = POLLIN;
469}
470
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800471static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800472{
Andrew Jeffery750fb0c2023-04-19 11:48:50 +0930473 /* NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp) */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800474 extern struct handler *__start_handlers, *__stop_handlers;
475 struct handler *handler;
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800476 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800477
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800478 console->n_handlers = &__stop_handlers - &__start_handlers;
479 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800480
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930481 printf("%ld handler%s\n", console->n_handlers,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930482 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800483
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800484 for (i = 0; i < console->n_handlers; i++) {
485 handler = console->handlers[i];
486
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800487 rc = 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930488 if (handler->init) {
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800489 rc = handler->init(handler, console, config);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930490 }
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800491
492 handler->active = rc == 0;
493
494 printf(" %s [%sactive]\n", handler->name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930495 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800496 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800497}
498
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800499static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800500{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800501 struct handler *handler;
502 int i;
503
504 for (i = 0; i < console->n_handlers; i++) {
505 handler = console->handlers[i];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930506 if (handler->fini && handler->active) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800507 handler->fini(handler);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930508 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800509 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800510}
511
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800512static int get_current_time(struct timeval *tv)
513{
514 struct timespec t;
515 int rc;
516
517 /*
518 * We use clock_gettime(CLOCK_MONOTONIC) so we're immune to
519 * local time changes. However, a struct timeval is more
520 * convenient for calculations, so convert to that.
521 */
522 rc = clock_gettime(CLOCK_MONOTONIC, &t);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930523 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800524 return rc;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930525 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800526
527 tv->tv_sec = t.tv_sec;
528 tv->tv_usec = t.tv_nsec / 1000;
529
530 return 0;
531}
532
Andrew Jefferya72711a2023-04-18 18:19:41 +0930533struct ringbuffer_consumer *
534console_ringbuffer_consumer_register(struct console *console,
535 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800536{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800537 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800538}
539
Jeremy Kerr55c97122017-02-07 17:06:46 +0800540struct poller *console_poller_register(struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930541 struct handler *handler,
542 poller_event_fn_t poller_fn,
543 poller_timeout_fn_t timeout_fn, int fd,
544 int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800545{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800546 struct poller *poller;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930547 long n;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800548
549 poller = malloc(sizeof(*poller));
550 poller->remove = false;
551 poller->handler = handler;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800552 poller->event_fn = poller_fn;
553 poller->timeout_fn = timeout_fn;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800554 poller->data = data;
555
556 /* add one to our pollers array */
557 n = console->n_pollers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +0930558 /*
559 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
560 * pointer type.
561 */
562 /* NOLINTBEGIN(bugprone-sizeof-expression) */
563 console->pollers = reallocarray(console->pollers, console->n_pollers,
564 sizeof(*console->pollers));
565 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800566
567 console->pollers[n] = poller;
568
569 /* increase pollfds array too */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930570 console->pollfds =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930571 reallocarray(console->pollfds,
572 (MAX_INTERNAL_POLLFD + console->n_pollers),
573 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800574
575 /* shift the end pollfds up by one */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930576 memcpy(&console->pollfds[n + 1], &console->pollfds[n],
577 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800578
579 console->pollfds[n].fd = fd;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930580 console->pollfds[n].events = (short)(events & 0x7fff);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800581
582 return poller;
583}
584
Andrew Jefferya72711a2023-04-18 18:19:41 +0930585void console_poller_unregister(struct console *console, struct poller *poller)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800586{
587 int i;
588
589 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930590 for (i = 0; i < console->n_pollers; i++) {
591 if (console->pollers[i] == poller) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800592 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930593 }
594 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800595
596 assert(i < console->n_pollers);
597
598 console->n_pollers--;
599
Andrew Jeffery91b52172023-04-19 12:42:14 +0930600 /*
601 * Remove the item from the pollers array...
602 *
603 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
604 * pointer type.
605 */
606 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930607 memmove(&console->pollers[i], &console->pollers[i + 1],
608 sizeof(*console->pollers) * (console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800609
Andrew Jeffery91b52172023-04-19 12:42:14 +0930610 console->pollers = reallocarray(console->pollers, console->n_pollers,
611 sizeof(*console->pollers));
612 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800613
614 /* ... and the pollfds array */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930615 memmove(&console->pollfds[i], &console->pollfds[i + 1],
616 sizeof(*console->pollfds) *
617 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800618
Andrew Jefferya72711a2023-04-18 18:19:41 +0930619 console->pollfds =
Andrew Jeffery91b52172023-04-19 12:42:14 +0930620 reallocarray(console->pollfds,
621 (MAX_INTERNAL_POLLFD + console->n_pollers),
622 sizeof(*console->pollfds));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800623
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800624 free(poller);
625}
626
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800627void console_poller_set_events(struct console *console, struct poller *poller,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930628 int events)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800629{
630 int i;
631
632 /* find the entry in our pollers array */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930633 for (i = 0; i < console->n_pollers; i++) {
634 if (console->pollers[i] == poller) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800635 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930636 }
637 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800638
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930639 console->pollfds[i].events = (short)(events & 0x7fff);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800640}
641
Andrew Jefferyfd048322023-04-18 12:02:01 +0930642void console_poller_set_timeout(struct console *console __attribute__((unused)),
Andrew Jefferya72711a2023-04-18 18:19:41 +0930643 struct poller *poller, const struct timeval *tv)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800644{
645 struct timeval now;
646 int rc;
647
648 rc = get_current_time(&now);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930649 if (rc) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800650 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930651 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800652
653 timeradd(&now, tv, &poller->timeout);
654}
655
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930656static long get_poll_timeout(struct console *console, struct timeval *cur_time)
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800657{
658 struct timeval *earliest, interval;
659 struct poller *poller;
660 int i;
661
662 earliest = NULL;
663
664 for (i = 0; i < console->n_pollers; i++) {
665 poller = console->pollers[i];
666
667 if (poller->timeout_fn && timerisset(&poller->timeout) &&
668 (!earliest ||
Andrew Jefferya72711a2023-04-18 18:19:41 +0930669 (earliest && timercmp(&poller->timeout, earliest, <)))) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800670 // poller is buffering data and needs the poll
671 // function to timeout.
672 earliest = &poller->timeout;
673 }
674 }
675
676 if (earliest) {
677 if (timercmp(earliest, cur_time, >)) {
678 /* recalculate the timeout period, time period has
679 * not elapsed */
680 timersub(earliest, cur_time, &interval);
681 return ((interval.tv_sec * 1000) +
682 (interval.tv_usec / 1000));
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930683 } /* return from poll immediately */
684 return 0;
685
686 } /* poll indefinitely */
687 return -1;
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800688}
689
690static int call_pollers(struct console *console, struct timeval *cur_time)
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800691{
692 struct poller *poller;
693 struct pollfd *pollfd;
694 enum poller_ret prc;
695 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800696
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800697 rc = 0;
698
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800699 /*
700 * Process poll events by iterating through the pollers and pollfds
701 * in-step, calling any pollers that we've found revents for.
702 */
703 for (i = 0; i < console->n_pollers; i++) {
704 poller = console->pollers[i];
705 pollfd = &console->pollfds[i];
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800706 prc = POLLER_OK;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800707
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800708 /* process pending events... */
709 if (pollfd->revents) {
710 prc = poller->event_fn(poller->handler, pollfd->revents,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930711 poller->data);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930712 if (prc == POLLER_EXIT) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800713 rc = -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930714 } else if (prc == POLLER_REMOVE) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800715 poller->remove = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930716 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800717 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800718
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800719 if ((prc == POLLER_OK) && poller->timeout_fn &&
720 timerisset(&poller->timeout) &&
721 timercmp(&poller->timeout, cur_time, <=)) {
722 /* One of the ringbuffer consumers is buffering the
723 data stream. The amount of idle time the consumer
724 desired has expired. Process the buffered data for
725 transmission. */
726 timerclear(&poller->timeout);
727 prc = poller->timeout_fn(poller->handler, poller->data);
728 if (prc == POLLER_EXIT) {
729 rc = -1;
730 } else if (prc == POLLER_REMOVE) {
731 poller->remove = true;
732 }
733 }
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800734 }
735
736 /**
737 * Process deferred removals; restarting each time we unregister, as
738 * the array will have changed
739 */
740 for (;;) {
741 bool removed = false;
742
743 for (i = 0; i < console->n_pollers; i++) {
744 poller = console->pollers[i];
745 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800746 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800747 removed = true;
748 break;
749 }
750 }
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930751 if (!removed) {
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800752 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930753 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800754 }
755
756 return rc;
757}
758
Jeremy Kerr769cee12016-03-15 17:53:56 +0800759static void sighandler(int signal)
760{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930761 if (signal == SIGINT) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800762 sigint = true;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930763 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800764}
765
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800766int run_console(struct console *console)
767{
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930768 sighandler_t sighandler_save = signal(SIGINT, sighandler);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800769 struct timeval tv;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930770 long timeout;
771 ssize_t rc;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800772
773 rc = 0;
774
Jeremy Kerrd831f962016-01-29 17:18:01 +0800775 for (;;) {
776 uint8_t buf[4096];
777
Jeremy Kerr17641452017-02-07 22:09:13 +0800778 BUILD_ASSERT(sizeof(buf) <= buffer_size);
779
Jeremy Kerr769cee12016-03-15 17:53:56 +0800780 if (sigint) {
781 fprintf(stderr, "Received interrupt, exiting\n");
782 break;
783 }
784
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800785 rc = get_current_time(&tv);
786 if (rc) {
787 warn("Failed to read current time");
788 break;
789 }
790
791 timeout = get_poll_timeout(console, &tv);
792
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800793 rc = poll(console->pollfds,
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930794 console->n_pollers + MAX_INTERNAL_POLLFD,
795 (int)timeout);
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800796
Jeremy Kerrd831f962016-01-29 17:18:01 +0800797 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800798 if (errno == EINTR) {
799 continue;
Jeremy Kerr769cee12016-03-15 17:53:56 +0800800 }
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930801 warn("poll error");
802 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800803 }
804
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800805 /* process internal fd first */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800806 if (console->pollfds[console->n_pollers].revents) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800807 rc = read(console->tty_fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800808 if (rc <= 0) {
809 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800810 rc = -1;
811 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800812 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800813 rc = ringbuffer_queue(console->rb, buf, rc);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930814 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800815 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930816 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800817 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800818
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800819 if (console->pollfds[console->n_pollers + 1].revents) {
820 sd_bus_process(console->bus, NULL);
821 }
822
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800823 /* ... and then the pollers */
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800824 rc = call_pollers(console, &tv);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930825 if (rc) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800826 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930827 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800828 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800829
830 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800831 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800832
833 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800834}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800835static const struct option options[] = {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930836 { "config", required_argument, 0, 'c' },
837 { 0, 0, 0, 0 },
Jeremy Kerrd831f962016-01-29 17:18:01 +0800838};
839
840int main(int argc, char **argv)
841{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800842 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500843 const char *config_tty_kname = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800844 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800845 struct config *config;
846 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800847
Jeremy Kerr957818b2016-03-08 14:35:15 +0800848 rc = -1;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800849
850 for (;;) {
851 int c, idx;
852
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800853 c = getopt_long(argc, argv, "c:", options, &idx);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930854 if (c == -1) {
Jeremy Kerrd831f962016-01-29 17:18:01 +0800855 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930856 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800857
858 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800859 case 'c':
860 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800861 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800862 case 'h':
863 case '?':
864 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800865 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800866 }
867 }
868
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930869 if (optind < argc) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030870 config_tty_kname = argv[optind];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930871 }
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500872
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800873 console = malloc(sizeof(struct console));
874 memset(console, 0, sizeof(*console));
Andrew Jefferya72711a2023-04-18 18:19:41 +0930875 console->pollfds =
876 calloc(MAX_INTERNAL_POLLFD, sizeof(*console->pollfds));
Jeremy Kerrf733c852017-02-07 18:40:10 +0800877 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800878
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800879 config = config_init(config_filename);
880 if (!config) {
881 warnx("Can't read configuration, exiting.");
882 goto out_free;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800883 }
884
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930885 if (!config_tty_kname) {
Andrew Jeffery91dde142020-02-12 22:46:27 +1030886 config_tty_kname = config_get_value(config, "upstream-tty");
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930887 }
Andrew Jeffery91dde142020-02-12 22:46:27 +1030888
889 if (!config_tty_kname) {
890 warnx("No TTY device specified");
891 usage(argv[0]);
892 return EXIT_FAILURE;
893 }
894
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500895 console->tty_kname = config_tty_kname;
896
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800897 rc = tty_init(console, config);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930898 if (rc) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800899 goto out_config_fini;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930900 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800901
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800902 dbus_init(console, config);
903
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800904 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800905
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800906 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800907
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800908 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800909
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800910out_config_fini:
911 config_fini(config);
912
Jeremy Kerr957818b2016-03-08 14:35:15 +0800913out_free:
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800914 free(console->pollers);
915 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800916 free(console->tty_sysfs_devnode);
917 free(console->tty_dev);
918 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800919
920 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
921}