blob: cde4bfc6f4131aa6c37bf1b7ddb362ad7d64c795 [file] [log] [blame]
Jeremy Kerrd831f962016-01-29 17:18:01 +08001/**
2 * Console server process for OpenBMC
3 *
Jeremy Kerr9326d772016-03-17 17:15:02 +08004 * Copyright © 2016 IBM Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
Jeremy Kerrd831f962016-01-29 17:18:01 +080017 */
18
Jeremy Kerr17217842016-01-29 18:44:21 +080019#define _GNU_SOURCE
20
Jeremy Kerr329a35f2016-03-10 15:36:01 +080021#include <assert.h>
Jeremy Kerr769cee12016-03-15 17:53:56 +080022#include <errno.h>
23#include <signal.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080024#include <stdint.h>
25#include <stdbool.h>
26#include <stdlib.h>
27#include <stdio.h>
28#include <fcntl.h>
29#include <unistd.h>
30#include <err.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080031#include <string.h>
32#include <getopt.h>
Jeremy Kerr17217842016-01-29 18:44:21 +080033#include <limits.h>
Jeremy Kerr54e95692016-03-24 17:07:56 +080034#include <termios.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080035
36#include <sys/types.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
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +080041#define DBUS_ERR "org.openbmc.error"
42#define DBUS_NAME "xyz.openbmc_project.console"
43#define OBJ_NAME "/xyz/openbmc_project/console"
44
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080045struct console {
Jeremy Kerr17217842016-01-29 18:44:21 +080046 const char *tty_kname;
47 char *tty_sysfs_devnode;
48 char *tty_dev;
Jeremy Kerr957818b2016-03-08 14:35:15 +080049 int tty_sirq;
50 int tty_lpc_addr;
Benjamin Fairc7fbcd42018-06-04 14:39:01 -070051 speed_t tty_baud;
Jeremy Kerrd831f962016-01-29 17:18:01 +080052 int tty_fd;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080053
Jeremy Kerrf733c852017-02-07 18:40:10 +080054 struct ringbuffer *rb;
55
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080056 struct handler **handlers;
57 int n_handlers;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080058
59 struct poller **pollers;
60 int n_pollers;
61
62 struct pollfd *pollfds;
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +080063 struct sd_bus *bus;
Jeremy Kerrd831f962016-01-29 17:18:01 +080064};
65
Jeremy Kerr329a35f2016-03-10 15:36:01 +080066struct poller {
67 struct handler *handler;
68 void *data;
69 poller_fn_t fn;
70 bool remove;
71};
72
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +080073/* we have two extra entry in the pollfds array for the VUART tty */
74enum internal_pollfds {
75 POLLFD_HOSTTTY = 0,
76 POLLFD_DBUS = 1,
77 MAX_INTERNAL_POLLFD = 2,
78};
Jeremy Kerr329a35f2016-03-10 15:36:01 +080079
Jeremy Kerrf733c852017-02-07 18:40:10 +080080/* size of the shared backlog ringbuffer */
81const size_t buffer_size = 128 * 1024;
82
Jeremy Kerr769cee12016-03-15 17:53:56 +080083/* state shared with the signal handler */
84static bool sigint;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080085
Jeremy Kerrd831f962016-01-29 17:18:01 +080086static void usage(const char *progname)
87{
88 fprintf(stderr,
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -050089"usage: %s [options] <DEVICE>\n"
Jeremy Kerrd831f962016-01-29 17:18:01 +080090"\n"
91"Options:\n"
Jeremy Kerrd66195c2016-03-16 17:24:51 +080092" --config <FILE> Use FILE for configuration\n"
Jeremy Kerrd831f962016-01-29 17:18:01 +080093"",
94 progname);
95}
96
Jeremy Kerr17217842016-01-29 18:44:21 +080097/* populates tty_dev and tty_sysfs_devnode, using the tty kernel name */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080098static int tty_find_device(struct console *console)
Jeremy Kerr17217842016-01-29 18:44:21 +080099{
100 char *tty_class_device_link;
101 char *tty_device_tty_dir;
102 char *tty_device_reldir;
Yi Li45ad7672016-10-18 23:21:19 +0800103 char *tty_path_input;
104 char *tty_path_input_real;
105 char *tty_kname_real;
Jeremy Kerr17217842016-01-29 18:44:21 +0800106 int rc;
107
Jeremy Kerr17217842016-01-29 18:44:21 +0800108 tty_class_device_link = NULL;
109 tty_device_tty_dir = NULL;
110 tty_device_reldir = NULL;
Yi Li45ad7672016-10-18 23:21:19 +0800111 tty_path_input = NULL;
112 tty_path_input_real = NULL;
113 tty_kname_real = NULL;
Jeremy Kerr17217842016-01-29 18:44:21 +0800114
Yi Li45ad7672016-10-18 23:21:19 +0800115 /* udev may rename the tty name with a symbol link, try to resolve */
116 rc = asprintf(&tty_path_input, "/dev/%s", console->tty_kname);
Jeremy Kerr17217842016-01-29 18:44:21 +0800117 if (rc < 0)
118 return -1;
119
Yi Li45ad7672016-10-18 23:21:19 +0800120 tty_path_input_real = realpath(tty_path_input, NULL);
121 if (!tty_path_input_real) {
122 warn("Can't find realpath for /dev/%s", console->tty_kname);
123 goto out_free;
124 }
125
126 tty_kname_real = basename(tty_path_input_real);
127 if (!tty_kname_real) {
128 warn("Can't find real name for /dev/%s", console->tty_kname);
129 goto out_free;
130 }
131
132 rc = asprintf(&tty_class_device_link,
133 "/sys/class/tty/%s", tty_kname_real);
134 if (rc < 0)
135 goto out_free;
136
Jeremy Kerr17217842016-01-29 18:44:21 +0800137 tty_device_tty_dir = realpath(tty_class_device_link, NULL);
Yi Li45ad7672016-10-18 23:21:19 +0800138 if (!tty_device_tty_dir) {
139 warn("Can't query sysfs for device %s", tty_kname_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800140 goto out_free;
141 }
142
143 rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
144 if (rc < 0)
145 goto out_free;
146
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800147 console->tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
148 if (!console->tty_sysfs_devnode)
Yi Li45ad7672016-10-18 23:21:19 +0800149 warn("Can't find parent device for %s", tty_kname_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800150
Yi Li45ad7672016-10-18 23:21:19 +0800151 rc = asprintf(&console->tty_dev, "/dev/%s", tty_kname_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800152 if (rc < 0)
153 goto out_free;
154
155 rc = 0;
156
157out_free:
158 free(tty_class_device_link);
159 free(tty_device_tty_dir);
160 free(tty_device_reldir);
Yi Li45ad7672016-10-18 23:21:19 +0800161 free(tty_path_input);
162 free(tty_path_input_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800163 return rc;
164}
165
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800166static int tty_set_sysfs_attr(struct console *console, const char *name,
Jeremy Kerr957818b2016-03-08 14:35:15 +0800167 int value)
168{
169 char *path;
170 FILE *fp;
171 int rc;
172
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800173 rc = asprintf(&path, "%s/%s", console->tty_sysfs_devnode, name);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800174 if (rc < 0)
175 return -1;
176
177 fp = fopen(path, "w");
178 if (!fp) {
179 warn("Can't access attribute %s on device %s",
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800180 name, console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800181 rc = -1;
182 goto out_free;
183 }
184 setvbuf(fp, NULL, _IONBF, 0);
185
186 rc = fprintf(fp, "0x%x", value);
187 if (rc < 0)
188 warn("Error writing to %s attribute of device %s",
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800189 name, console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800190 fclose(fp);
191
192
193
194out_free:
195 free(path);
196 return rc;
197}
198
Jeremy Kerrd831f962016-01-29 17:18:01 +0800199/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700200 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800201 */
202static void tty_init_termios(struct console *console)
203{
204 struct termios termios;
205 int rc;
206
207 rc = tcgetattr(console->tty_fd, &termios);
208 if (rc) {
209 warn("Can't read tty termios");
210 return;
211 }
212
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700213 if (console->tty_baud) {
214 if (cfsetspeed(&termios, console->tty_baud) < 0)
215 warn("Couldn't set speeds for %s", console->tty_kname);
216 }
217
218 /* Set console to raw mode: we don't want any processing to occur on
219 * the underlying terminal input/output.
220 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800221 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700222
Jeremy Kerr54e95692016-03-24 17:07:56 +0800223 rc = tcsetattr(console->tty_fd, TCSANOW, &termios);
224 if (rc)
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700225 warn("Can't set terminal options for %s", console->tty_kname);
Jeremy Kerr54e95692016-03-24 17:07:56 +0800226}
227
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800228
229static void tty_change_baudrate(struct console *console)
230{
231 struct handler *handler;
232 int i, rc;
233
234 tty_init_termios(console);
235
236 for (i = 0; i < console->n_handlers; i++) {
237 handler = console->handlers[i];
238 if (!handler->baudrate)
239 continue;
240
241 rc = handler->baudrate(handler, console->tty_baud);
242 if (rc)
243 warnx("Can't set terminal baudrate for handler %s",
244 handler->name);
245 }
246}
247
Jeremy Kerr54e95692016-03-24 17:07:56 +0800248/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800249 * Open and initialise the serial device
250 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800251static int tty_init_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800252{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800253 if (console->tty_sirq)
254 tty_set_sysfs_attr(console, "sirq", console->tty_sirq);
255 if (console->tty_lpc_addr)
256 tty_set_sysfs_attr(console, "lpc_address",
257 console->tty_lpc_addr);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800258
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800259 console->tty_fd = open(console->tty_dev, O_RDWR);
260 if (console->tty_fd <= 0) {
261 warn("Can't open tty %s", console->tty_dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800262 return -1;
263 }
264
265 /* Disable character delay. We may want to later enable this when
266 * we detect larger amounts of data
267 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800268 fcntl(console->tty_fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800269
Jeremy Kerr54e95692016-03-24 17:07:56 +0800270 tty_init_termios(console);
271
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800272 console->pollfds[console->n_pollers].fd = console->tty_fd;
273 console->pollfds[console->n_pollers].events = POLLIN;
274
Jeremy Kerrd831f962016-01-29 17:18:01 +0800275 return 0;
276}
277
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800278static int tty_init(struct console *console, struct config *config)
279{
280 const char *val;
281 char *endp;
282 int rc;
283
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800284 val = config_get_value(config, "lpc-address");
285 if (val) {
286 console->tty_lpc_addr = strtoul(val, &endp, 0);
287 if (endp == optarg) {
288 warn("Invalid LPC address: '%s'", val);
289 return -1;
290 }
291 }
292
293 val = config_get_value(config, "sirq");
294 if (val) {
295 console->tty_sirq = strtoul(val, &endp, 0);
296 if (endp == optarg)
297 warn("Invalid sirq: '%s'", val);
298 }
299
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700300 val = config_get_value(config, "baud");
301 if (val) {
302 if (config_parse_baud(&console->tty_baud, val))
303 warnx("Invalid baud rate: '%s'", val);
304 }
305
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800306 if (!console->tty_kname) {
307 warnx("Error: No TTY device specified");
308 return -1;
309 }
310
311 rc = tty_find_device(console);
312 if (rc)
313 return rc;
314
315 rc = tty_init_io(console);
316 return rc;
317}
318
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800319
320int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800321{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800322 return write_buf_to_fd(console->tty_fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800323}
324
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800325static int method_set_baud_rate(sd_bus_message *msg, void *userdata,
326 sd_bus_error *err)
327{
328 struct console *console = userdata;
329 uint32_t baudrate;
330 speed_t speed;
331 int r;
332
333 if (!console) {
334 sd_bus_error_set_const(err, DBUS_ERR, "Internal error");
335 return sd_bus_reply_method_return(msg, "x", 0);
336 }
337
338 r = sd_bus_message_read(msg, "u", &baudrate);
339 if (r < 0) {
340 sd_bus_error_set_const(err, DBUS_ERR, "Bad message");
341 return sd_bus_reply_method_return(msg, "x", -EINVAL);
342 }
343
344 speed = parse_int_to_baud(baudrate);
345 if (!speed) {
346 warnx("Invalid baud rate: '%u'", baudrate);
347 return sd_bus_reply_method_return(msg, "x", -EINVAL);
348 }
349
350 console->tty_baud = speed;
351 tty_change_baudrate(console);
352
353 return sd_bus_reply_method_return(msg, "x", r);
354}
355
356static int get_handler(sd_bus *bus, const char *path, const char *interface,
357 const char *property, sd_bus_message *reply, void *userdata,
358 sd_bus_error *error) {
359 struct console *console = userdata;
360 uint32_t baudrate;
361 int r;
362
363 baudrate = parse_baud_to_int(console->tty_baud);
364 if (!baudrate)
365 warnx("Invalid baud rate: '%d'", console->tty_baud);
366
367 r = sd_bus_message_append(reply, "u", baudrate);
368
369 return r;
370}
371
372static const sd_bus_vtable console_vtable[] = {
373 SD_BUS_VTABLE_START(0),
374 SD_BUS_METHOD("setBaudRate", "u", "x", method_set_baud_rate,
375 SD_BUS_VTABLE_UNPRIVILEGED),
376 SD_BUS_PROPERTY("baudrate", "u", get_handler, 0, 0),
377 SD_BUS_VTABLE_END,};
378
379static void dbus_init(struct console *console, struct config *config)
380{
381 int dbus_poller = 0;
382 int fd, r;
383
384 if (!console) {
385 warnx("Couldn't get valid console");
386 return;
387 }
388
389 r = sd_bus_default_system(&console->bus);
390 if (r < 0) {
391 warnx("Failed to connect to system bus: %s", strerror(-r));
392 return;
393 }
394
395 r = sd_bus_add_object_vtable(console->bus, NULL, OBJ_NAME, DBUS_NAME,
396 console_vtable, console);
397 if (r < 0) {
398 warnx("Failed to issue method call: %s", strerror(-r));
399 return;
400 }
401
402 r = sd_bus_request_name(console->bus, DBUS_NAME, SD_BUS_NAME_ALLOW_REPLACEMENT
403 |SD_BUS_NAME_REPLACE_EXISTING);
404 if (r < 0) {
405 warnx("Failed to acquire service name: %s", strerror(-r));
406 return;
407 }
408
409 fd = sd_bus_get_fd(console->bus);
410 if (fd < 0) {
411 warnx("Couldn't get the bus file descriptor");
412 return;
413 }
414
415 dbus_poller = POLLFD_DBUS;
416
417 console->pollfds[dbus_poller].fd = fd;
418 console->pollfds[dbus_poller].events = POLLIN;
419}
420
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800421static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800422{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800423 extern struct handler *__start_handlers, *__stop_handlers;
424 struct handler *handler;
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800425 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800426
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800427 console->n_handlers = &__stop_handlers - &__start_handlers;
428 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800429
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800430 printf("%d handler%s\n", console->n_handlers,
431 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800432
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800433 for (i = 0; i < console->n_handlers; i++) {
434 handler = console->handlers[i];
435
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800436 rc = 0;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800437 if (handler->init)
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800438 rc = handler->init(handler, console, config);
439
440 handler->active = rc == 0;
441
442 printf(" %s [%sactive]\n", handler->name,
443 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800444 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800445}
446
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800447static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800448{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800449 struct handler *handler;
450 int i;
451
452 for (i = 0; i < console->n_handlers; i++) {
453 handler = console->handlers[i];
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800454 if (handler->fini && handler->active)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800455 handler->fini(handler);
456 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800457}
458
Jeremy Kerrf733c852017-02-07 18:40:10 +0800459struct ringbuffer_consumer *console_ringbuffer_consumer_register(
460 struct console *console,
461 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800462{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800463 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800464}
465
Jeremy Kerr55c97122017-02-07 17:06:46 +0800466struct poller *console_poller_register(struct console *console,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800467 struct handler *handler, poller_fn_t poller_fn,
468 int fd, int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800469{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800470 struct poller *poller;
471 int n;
472
473 poller = malloc(sizeof(*poller));
474 poller->remove = false;
475 poller->handler = handler;
476 poller->fn = poller_fn;
477 poller->data = data;
478
479 /* add one to our pollers array */
480 n = console->n_pollers++;
481 console->pollers = realloc(console->pollers,
482 sizeof(*console->pollers) * console->n_pollers);
483
484 console->pollers[n] = poller;
485
486 /* increase pollfds array too */
487 console->pollfds = realloc(console->pollfds,
488 sizeof(*console->pollfds) *
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800489 (MAX_INTERNAL_POLLFD + console->n_pollers));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800490
491 /* shift the end pollfds up by one */
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800492 memcpy(&console->pollfds[n+1],
493 &console->pollfds[n],
494 sizeof(*console->pollfds) * MAX_INTERNAL_POLLFD);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800495
496 console->pollfds[n].fd = fd;
497 console->pollfds[n].events = events;
498
499 return poller;
500}
501
Jeremy Kerr55c97122017-02-07 17:06:46 +0800502void console_poller_unregister(struct console *console,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800503 struct poller *poller)
504{
505 int i;
506
507 /* find the entry in our pollers array */
508 for (i = 0; i < console->n_pollers; i++)
509 if (console->pollers[i] == poller)
510 break;
511
512 assert(i < console->n_pollers);
513
514 console->n_pollers--;
515
516 /* remove the item from the pollers array... */
517 memmove(&console->pollers[i], &console->pollers[i+1],
518 sizeof(*console->pollers)
519 * (console->n_pollers - i));
520
521 console->pollers = realloc(console->pollers,
522 sizeof(*console->pollers) * console->n_pollers);
523
524 /* ... and the pollfds array */
525 memmove(&console->pollfds[i], &console->pollfds[i+1],
526 sizeof(*console->pollfds) *
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800527 (MAX_INTERNAL_POLLFD + console->n_pollers - i));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800528
529 console->pollfds = realloc(console->pollfds,
530 sizeof(*console->pollfds) *
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800531 (MAX_INTERNAL_POLLFD + console->n_pollers));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800532
533
534 free(poller);
535}
536
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800537void console_poller_set_events(struct console *console, struct poller *poller,
538 int events)
539{
540 int i;
541
542 /* find the entry in our pollers array */
543 for (i = 0; i < console->n_pollers; i++)
544 if (console->pollers[i] == poller)
545 break;
546
547 console->pollfds[i].events = events;
548}
549
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800550static int call_pollers(struct console *console)
551{
552 struct poller *poller;
553 struct pollfd *pollfd;
554 enum poller_ret prc;
555 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800556
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800557 rc = 0;
558
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800559 /*
560 * Process poll events by iterating through the pollers and pollfds
561 * in-step, calling any pollers that we've found revents for.
562 */
563 for (i = 0; i < console->n_pollers; i++) {
564 poller = console->pollers[i];
565 pollfd = &console->pollfds[i];
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800566
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800567 if (!pollfd->revents)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800568 continue;
569
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800570 prc = poller->fn(poller->handler, pollfd->revents,
571 poller->data);
572 if (prc == POLLER_EXIT)
573 rc = -1;
574 else if (prc == POLLER_REMOVE)
575 poller->remove = true;
576 }
577
578 /**
579 * Process deferred removals; restarting each time we unregister, as
580 * the array will have changed
581 */
582 for (;;) {
583 bool removed = false;
584
585 for (i = 0; i < console->n_pollers; i++) {
586 poller = console->pollers[i];
587 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800588 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800589 removed = true;
590 break;
591 }
592 }
593 if (!removed)
594 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800595 }
596
597 return rc;
598}
599
Jeremy Kerr769cee12016-03-15 17:53:56 +0800600static void sighandler(int signal)
601{
602 if (signal == SIGINT)
603 sigint = true;
604}
605
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800606int run_console(struct console *console)
607{
Jeremy Kerr769cee12016-03-15 17:53:56 +0800608 sighandler_t sighandler_save;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800609 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800610
Jeremy Kerr769cee12016-03-15 17:53:56 +0800611 sighandler_save = signal(SIGINT, sighandler);
612
613 rc = 0;
614
Jeremy Kerrd831f962016-01-29 17:18:01 +0800615 for (;;) {
616 uint8_t buf[4096];
617
Jeremy Kerr17641452017-02-07 22:09:13 +0800618 BUILD_ASSERT(sizeof(buf) <= buffer_size);
619
Jeremy Kerr769cee12016-03-15 17:53:56 +0800620 if (sigint) {
621 fprintf(stderr, "Received interrupt, exiting\n");
622 break;
623 }
624
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800625 rc = poll(console->pollfds,
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800626 console->n_pollers + MAX_INTERNAL_POLLFD, -1);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800627 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800628 if (errno == EINTR) {
629 continue;
630 } else {
631 warn("poll error");
632 break;
633 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800634 }
635
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800636 /* process internal fd first */
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800637 if (console->pollfds[console->n_pollers].revents) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800638 rc = read(console->tty_fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800639 if (rc <= 0) {
640 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800641 rc = -1;
642 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800643 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800644 rc = ringbuffer_queue(console->rb, buf, rc);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800645 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800646 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800647 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800648
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800649 if (console->pollfds[console->n_pollers + 1].revents) {
650 sd_bus_process(console->bus, NULL);
651 }
652
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800653 /* ... and then the pollers */
654 rc = call_pollers(console);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800655 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800656 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800657 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800658
659 signal(SIGINT, sighandler_save);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800660 sd_bus_unref(console->bus);
Jeremy Kerr769cee12016-03-15 17:53:56 +0800661
662 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800663}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800664static const struct option options[] = {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800665 { "config", required_argument, 0, 'c'},
Joel Stanleyf5858b52016-03-18 16:33:03 +1030666 { 0, 0, 0, 0},
Jeremy Kerrd831f962016-01-29 17:18:01 +0800667};
668
669int main(int argc, char **argv)
670{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800671 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500672 const char *config_tty_kname = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800673 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800674 struct config *config;
675 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800676
Jeremy Kerr957818b2016-03-08 14:35:15 +0800677 rc = -1;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800678
679 for (;;) {
680 int c, idx;
681
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800682 c = getopt_long(argc, argv, "c:", options, &idx);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800683 if (c == -1)
684 break;
685
686 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800687 case 'c':
688 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800689 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800690 case 'h':
691 case '?':
692 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800693 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800694 }
695 }
696
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500697 if (optind >= argc) {
698 warnx("Required argument <DEVICE> missing");
699 usage(argv[0]);
700 return EXIT_FAILURE;
701 }
702
703 config_tty_kname = argv[optind];
704
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800705 console = malloc(sizeof(struct console));
706 memset(console, 0, sizeof(*console));
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800707 console->pollfds = calloc(MAX_INTERNAL_POLLFD,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800708 sizeof(*console->pollfds));
Jeremy Kerrf733c852017-02-07 18:40:10 +0800709 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800710
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800711 config = config_init(config_filename);
712 if (!config) {
713 warnx("Can't read configuration, exiting.");
714 goto out_free;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800715 }
716
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500717 console->tty_kname = config_tty_kname;
718
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800719 rc = tty_init(console, config);
Jeremy Kerr17217842016-01-29 18:44:21 +0800720 if (rc)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800721 goto out_config_fini;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800722
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800723 dbus_init(console, config);
724
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800725 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800726
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800727 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800728
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800729 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800730
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800731out_config_fini:
732 config_fini(config);
733
Jeremy Kerr957818b2016-03-08 14:35:15 +0800734out_free:
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800735 free(console->pollers);
736 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800737 free(console->tty_sysfs_devnode);
738 free(console->tty_dev);
739 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800740
741 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
742}