blob: 22e60948cf2e45d2b5499a34b90e0a48ced10f3b [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
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080041struct console {
Jeremy Kerr17217842016-01-29 18:44:21 +080042 const char *tty_kname;
43 char *tty_sysfs_devnode;
44 char *tty_dev;
Jeremy Kerr957818b2016-03-08 14:35:15 +080045 int tty_sirq;
46 int tty_lpc_addr;
Benjamin Fairc7fbcd42018-06-04 14:39:01 -070047 speed_t tty_baud;
Jeremy Kerrd831f962016-01-29 17:18:01 +080048 int tty_fd;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080049
Jeremy Kerrf733c852017-02-07 18:40:10 +080050 struct ringbuffer *rb;
51
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080052 struct handler **handlers;
53 int n_handlers;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080054
55 struct poller **pollers;
56 int n_pollers;
57
58 struct pollfd *pollfds;
Jeremy Kerrd831f962016-01-29 17:18:01 +080059};
60
Jeremy Kerr329a35f2016-03-10 15:36:01 +080061struct poller {
62 struct handler *handler;
63 void *data;
64 poller_fn_t fn;
65 bool remove;
66};
67
68/* we have one extra entry in the pollfds array for the VUART tty */
69static const int n_internal_pollfds = 1;
70
Jeremy Kerrf733c852017-02-07 18:40:10 +080071/* size of the shared backlog ringbuffer */
72const size_t buffer_size = 128 * 1024;
73
Jeremy Kerr769cee12016-03-15 17:53:56 +080074/* state shared with the signal handler */
75static bool sigint;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080076
Jeremy Kerrd831f962016-01-29 17:18:01 +080077static void usage(const char *progname)
78{
79 fprintf(stderr,
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -050080"usage: %s [options] <DEVICE>\n"
Jeremy Kerrd831f962016-01-29 17:18:01 +080081"\n"
82"Options:\n"
Jeremy Kerrd66195c2016-03-16 17:24:51 +080083" --config <FILE> Use FILE for configuration\n"
Jeremy Kerrd831f962016-01-29 17:18:01 +080084"",
85 progname);
86}
87
Jeremy Kerr17217842016-01-29 18:44:21 +080088/* populates tty_dev and tty_sysfs_devnode, using the tty kernel name */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080089static int tty_find_device(struct console *console)
Jeremy Kerr17217842016-01-29 18:44:21 +080090{
91 char *tty_class_device_link;
92 char *tty_device_tty_dir;
93 char *tty_device_reldir;
Yi Li45ad7672016-10-18 23:21:19 +080094 char *tty_path_input;
95 char *tty_path_input_real;
96 char *tty_kname_real;
Jeremy Kerr17217842016-01-29 18:44:21 +080097 int rc;
98
Jeremy Kerr17217842016-01-29 18:44:21 +080099 tty_class_device_link = NULL;
100 tty_device_tty_dir = NULL;
101 tty_device_reldir = NULL;
Yi Li45ad7672016-10-18 23:21:19 +0800102 tty_path_input = NULL;
103 tty_path_input_real = NULL;
104 tty_kname_real = NULL;
Jeremy Kerr17217842016-01-29 18:44:21 +0800105
Yi Li45ad7672016-10-18 23:21:19 +0800106 /* udev may rename the tty name with a symbol link, try to resolve */
107 rc = asprintf(&tty_path_input, "/dev/%s", console->tty_kname);
Jeremy Kerr17217842016-01-29 18:44:21 +0800108 if (rc < 0)
109 return -1;
110
Yi Li45ad7672016-10-18 23:21:19 +0800111 tty_path_input_real = realpath(tty_path_input, NULL);
112 if (!tty_path_input_real) {
113 warn("Can't find realpath for /dev/%s", console->tty_kname);
114 goto out_free;
115 }
116
117 tty_kname_real = basename(tty_path_input_real);
118 if (!tty_kname_real) {
119 warn("Can't find real name for /dev/%s", console->tty_kname);
120 goto out_free;
121 }
122
123 rc = asprintf(&tty_class_device_link,
124 "/sys/class/tty/%s", tty_kname_real);
125 if (rc < 0)
126 goto out_free;
127
Jeremy Kerr17217842016-01-29 18:44:21 +0800128 tty_device_tty_dir = realpath(tty_class_device_link, NULL);
Yi Li45ad7672016-10-18 23:21:19 +0800129 if (!tty_device_tty_dir) {
130 warn("Can't query sysfs for device %s", tty_kname_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800131 goto out_free;
132 }
133
134 rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
135 if (rc < 0)
136 goto out_free;
137
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800138 console->tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
139 if (!console->tty_sysfs_devnode)
Yi Li45ad7672016-10-18 23:21:19 +0800140 warn("Can't find parent device for %s", tty_kname_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800141
Yi Li45ad7672016-10-18 23:21:19 +0800142 rc = asprintf(&console->tty_dev, "/dev/%s", tty_kname_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800143 if (rc < 0)
144 goto out_free;
145
146 rc = 0;
147
148out_free:
149 free(tty_class_device_link);
150 free(tty_device_tty_dir);
151 free(tty_device_reldir);
Yi Li45ad7672016-10-18 23:21:19 +0800152 free(tty_path_input);
153 free(tty_path_input_real);
Jeremy Kerr17217842016-01-29 18:44:21 +0800154 return rc;
155}
156
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800157static int tty_set_sysfs_attr(struct console *console, const char *name,
Jeremy Kerr957818b2016-03-08 14:35:15 +0800158 int value)
159{
160 char *path;
161 FILE *fp;
162 int rc;
163
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800164 rc = asprintf(&path, "%s/%s", console->tty_sysfs_devnode, name);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800165 if (rc < 0)
166 return -1;
167
168 fp = fopen(path, "w");
169 if (!fp) {
170 warn("Can't access attribute %s on device %s",
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800171 name, console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800172 rc = -1;
173 goto out_free;
174 }
175 setvbuf(fp, NULL, _IONBF, 0);
176
177 rc = fprintf(fp, "0x%x", value);
178 if (rc < 0)
179 warn("Error writing to %s attribute of device %s",
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800180 name, console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800181 fclose(fp);
182
183
184
185out_free:
186 free(path);
187 return rc;
188}
189
Jeremy Kerrd831f962016-01-29 17:18:01 +0800190/**
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700191 * Set termios attributes on the console tty.
Jeremy Kerr54e95692016-03-24 17:07:56 +0800192 */
193static void tty_init_termios(struct console *console)
194{
195 struct termios termios;
196 int rc;
197
198 rc = tcgetattr(console->tty_fd, &termios);
199 if (rc) {
200 warn("Can't read tty termios");
201 return;
202 }
203
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700204 if (console->tty_baud) {
205 if (cfsetspeed(&termios, console->tty_baud) < 0)
206 warn("Couldn't set speeds for %s", console->tty_kname);
207 }
208
209 /* Set console to raw mode: we don't want any processing to occur on
210 * the underlying terminal input/output.
211 */
Jeremy Kerr54e95692016-03-24 17:07:56 +0800212 cfmakeraw(&termios);
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700213
Jeremy Kerr54e95692016-03-24 17:07:56 +0800214 rc = tcsetattr(console->tty_fd, TCSANOW, &termios);
215 if (rc)
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700216 warn("Can't set terminal options for %s", console->tty_kname);
Jeremy Kerr54e95692016-03-24 17:07:56 +0800217}
218
219/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800220 * Open and initialise the serial device
221 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800222static int tty_init_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800223{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800224 if (console->tty_sirq)
225 tty_set_sysfs_attr(console, "sirq", console->tty_sirq);
226 if (console->tty_lpc_addr)
227 tty_set_sysfs_attr(console, "lpc_address",
228 console->tty_lpc_addr);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800229
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800230 console->tty_fd = open(console->tty_dev, O_RDWR);
231 if (console->tty_fd <= 0) {
232 warn("Can't open tty %s", console->tty_dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800233 return -1;
234 }
235
236 /* Disable character delay. We may want to later enable this when
237 * we detect larger amounts of data
238 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800239 fcntl(console->tty_fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800240
Jeremy Kerr54e95692016-03-24 17:07:56 +0800241 tty_init_termios(console);
242
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800243 console->pollfds[console->n_pollers].fd = console->tty_fd;
244 console->pollfds[console->n_pollers].events = POLLIN;
245
Jeremy Kerrd831f962016-01-29 17:18:01 +0800246 return 0;
247}
248
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800249static int tty_init(struct console *console, struct config *config)
250{
251 const char *val;
252 char *endp;
253 int rc;
254
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800255 val = config_get_value(config, "lpc-address");
256 if (val) {
257 console->tty_lpc_addr = strtoul(val, &endp, 0);
258 if (endp == optarg) {
259 warn("Invalid LPC address: '%s'", val);
260 return -1;
261 }
262 }
263
264 val = config_get_value(config, "sirq");
265 if (val) {
266 console->tty_sirq = strtoul(val, &endp, 0);
267 if (endp == optarg)
268 warn("Invalid sirq: '%s'", val);
269 }
270
Benjamin Fairc7fbcd42018-06-04 14:39:01 -0700271 val = config_get_value(config, "baud");
272 if (val) {
273 if (config_parse_baud(&console->tty_baud, val))
274 warnx("Invalid baud rate: '%s'", val);
275 }
276
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800277 if (!console->tty_kname) {
278 warnx("Error: No TTY device specified");
279 return -1;
280 }
281
282 rc = tty_find_device(console);
283 if (rc)
284 return rc;
285
286 rc = tty_init_io(console);
287 return rc;
288}
289
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800290
291int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800292{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800293 return write_buf_to_fd(console->tty_fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800294}
295
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800296static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800297{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800298 extern struct handler *__start_handlers, *__stop_handlers;
299 struct handler *handler;
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800300 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800301
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800302 console->n_handlers = &__stop_handlers - &__start_handlers;
303 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800304
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800305 printf("%d handler%s\n", console->n_handlers,
306 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800307
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800308 for (i = 0; i < console->n_handlers; i++) {
309 handler = console->handlers[i];
310
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800311 rc = 0;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800312 if (handler->init)
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800313 rc = handler->init(handler, console, config);
314
315 handler->active = rc == 0;
316
317 printf(" %s [%sactive]\n", handler->name,
318 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800319 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800320}
321
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800322static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800323{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800324 struct handler *handler;
325 int i;
326
327 for (i = 0; i < console->n_handlers; i++) {
328 handler = console->handlers[i];
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800329 if (handler->fini && handler->active)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800330 handler->fini(handler);
331 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800332}
333
Jeremy Kerrf733c852017-02-07 18:40:10 +0800334struct ringbuffer_consumer *console_ringbuffer_consumer_register(
335 struct console *console,
336 ringbuffer_poll_fn_t poll_fn, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800337{
Jeremy Kerrf733c852017-02-07 18:40:10 +0800338 return ringbuffer_consumer_register(console->rb, poll_fn, data);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800339}
340
Jeremy Kerr55c97122017-02-07 17:06:46 +0800341struct poller *console_poller_register(struct console *console,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800342 struct handler *handler, poller_fn_t poller_fn,
343 int fd, int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800344{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800345 struct poller *poller;
346 int n;
347
348 poller = malloc(sizeof(*poller));
349 poller->remove = false;
350 poller->handler = handler;
351 poller->fn = poller_fn;
352 poller->data = data;
353
354 /* add one to our pollers array */
355 n = console->n_pollers++;
356 console->pollers = realloc(console->pollers,
357 sizeof(*console->pollers) * console->n_pollers);
358
359 console->pollers[n] = poller;
360
361 /* increase pollfds array too */
362 console->pollfds = realloc(console->pollfds,
363 sizeof(*console->pollfds) *
364 (n_internal_pollfds + console->n_pollers));
365
366 /* shift the end pollfds up by one */
367 memcpy(&console->pollfds[n+n_internal_pollfds],
368 &console->pollfds[n],
369 sizeof(*console->pollfds) * n_internal_pollfds);
370
371 console->pollfds[n].fd = fd;
372 console->pollfds[n].events = events;
373
374 return poller;
375}
376
Jeremy Kerr55c97122017-02-07 17:06:46 +0800377void console_poller_unregister(struct console *console,
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800378 struct poller *poller)
379{
380 int i;
381
382 /* find the entry in our pollers array */
383 for (i = 0; i < console->n_pollers; i++)
384 if (console->pollers[i] == poller)
385 break;
386
387 assert(i < console->n_pollers);
388
389 console->n_pollers--;
390
391 /* remove the item from the pollers array... */
392 memmove(&console->pollers[i], &console->pollers[i+1],
393 sizeof(*console->pollers)
394 * (console->n_pollers - i));
395
396 console->pollers = realloc(console->pollers,
397 sizeof(*console->pollers) * console->n_pollers);
398
399 /* ... and the pollfds array */
400 memmove(&console->pollfds[i], &console->pollfds[i+1],
401 sizeof(*console->pollfds) *
402 (n_internal_pollfds + console->n_pollers - i));
403
404 console->pollfds = realloc(console->pollfds,
405 sizeof(*console->pollfds) *
406 (n_internal_pollfds + console->n_pollers));
407
408
409 free(poller);
410}
411
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800412void console_poller_set_events(struct console *console, struct poller *poller,
413 int events)
414{
415 int i;
416
417 /* find the entry in our pollers array */
418 for (i = 0; i < console->n_pollers; i++)
419 if (console->pollers[i] == poller)
420 break;
421
422 console->pollfds[i].events = events;
423}
424
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800425static int call_pollers(struct console *console)
426{
427 struct poller *poller;
428 struct pollfd *pollfd;
429 enum poller_ret prc;
430 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800431
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800432 rc = 0;
433
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800434 /*
435 * Process poll events by iterating through the pollers and pollfds
436 * in-step, calling any pollers that we've found revents for.
437 */
438 for (i = 0; i < console->n_pollers; i++) {
439 poller = console->pollers[i];
440 pollfd = &console->pollfds[i];
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800441
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800442 if (!pollfd->revents)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800443 continue;
444
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800445 prc = poller->fn(poller->handler, pollfd->revents,
446 poller->data);
447 if (prc == POLLER_EXIT)
448 rc = -1;
449 else if (prc == POLLER_REMOVE)
450 poller->remove = true;
451 }
452
453 /**
454 * Process deferred removals; restarting each time we unregister, as
455 * the array will have changed
456 */
457 for (;;) {
458 bool removed = false;
459
460 for (i = 0; i < console->n_pollers; i++) {
461 poller = console->pollers[i];
462 if (poller->remove) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800463 console_poller_unregister(console, poller);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800464 removed = true;
465 break;
466 }
467 }
468 if (!removed)
469 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800470 }
471
472 return rc;
473}
474
Jeremy Kerr769cee12016-03-15 17:53:56 +0800475static void sighandler(int signal)
476{
477 if (signal == SIGINT)
478 sigint = true;
479}
480
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800481int run_console(struct console *console)
482{
Jeremy Kerr769cee12016-03-15 17:53:56 +0800483 sighandler_t sighandler_save;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800484 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800485
Jeremy Kerr769cee12016-03-15 17:53:56 +0800486 sighandler_save = signal(SIGINT, sighandler);
487
488 rc = 0;
489
Jeremy Kerrd831f962016-01-29 17:18:01 +0800490 for (;;) {
491 uint8_t buf[4096];
492
Jeremy Kerr17641452017-02-07 22:09:13 +0800493 BUILD_ASSERT(sizeof(buf) <= buffer_size);
494
Jeremy Kerr769cee12016-03-15 17:53:56 +0800495 if (sigint) {
496 fprintf(stderr, "Received interrupt, exiting\n");
497 break;
498 }
499
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800500 rc = poll(console->pollfds,
501 console->n_pollers + n_internal_pollfds, -1);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800502 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800503 if (errno == EINTR) {
504 continue;
505 } else {
506 warn("poll error");
507 break;
508 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800509 }
510
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800511 /* process internal fd first */
512 BUILD_ASSERT(n_internal_pollfds == 1);
513
514 if (console->pollfds[console->n_pollers].revents) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800515 rc = read(console->tty_fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800516 if (rc <= 0) {
517 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800518 rc = -1;
519 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800520 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800521 rc = ringbuffer_queue(console->rb, buf, rc);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800522 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800523 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800524 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800525
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800526 /* ... and then the pollers */
527 rc = call_pollers(console);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800528 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800529 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800530 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800531
532 signal(SIGINT, sighandler_save);
533
534 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800535}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800536static const struct option options[] = {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800537 { "config", required_argument, 0, 'c'},
Joel Stanleyf5858b52016-03-18 16:33:03 +1030538 { 0, 0, 0, 0},
Jeremy Kerrd831f962016-01-29 17:18:01 +0800539};
540
541int main(int argc, char **argv)
542{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800543 const char *config_filename = NULL;
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500544 const char *config_tty_kname = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800545 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800546 struct config *config;
547 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800548
Jeremy Kerr957818b2016-03-08 14:35:15 +0800549 rc = -1;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800550
551 for (;;) {
552 int c, idx;
553
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800554 c = getopt_long(argc, argv, "c:", options, &idx);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800555 if (c == -1)
556 break;
557
558 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800559 case 'c':
560 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800561 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800562 case 'h':
563 case '?':
564 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800565 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800566 }
567 }
568
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500569 if (optind >= argc) {
570 warnx("Required argument <DEVICE> missing");
571 usage(argv[0]);
572 return EXIT_FAILURE;
573 }
574
575 config_tty_kname = argv[optind];
576
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800577 console = malloc(sizeof(struct console));
578 memset(console, 0, sizeof(*console));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800579 console->pollfds = calloc(n_internal_pollfds,
580 sizeof(*console->pollfds));
Jeremy Kerrf733c852017-02-07 18:40:10 +0800581 console->rb = ringbuffer_init(buffer_size);
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800582
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800583 config = config_init(config_filename);
584 if (!config) {
585 warnx("Can't read configuration, exiting.");
586 goto out_free;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800587 }
588
Vishwanatha Subbanna6221ce92016-07-20 05:35:45 -0500589 console->tty_kname = config_tty_kname;
590
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800591 rc = tty_init(console, config);
Jeremy Kerr17217842016-01-29 18:44:21 +0800592 if (rc)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800593 goto out_config_fini;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800594
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800595 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800596
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800597 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800598
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800599 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800600
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800601out_config_fini:
602 config_fini(config);
603
Jeremy Kerr957818b2016-03-08 14:35:15 +0800604out_free:
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800605 free(console->pollers);
606 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800607 free(console->tty_sysfs_devnode);
608 free(console->tty_dev);
609 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800610
611 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
612}