blob: 45dd07a422ce23bd34cb82329324f9fe5905579a [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>
37#include <sys/poll.h>
38
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;
Jeremy Kerrd831f962016-01-29 17:18:01 +080047 int tty_fd;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080048
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080049 struct handler **handlers;
50 int n_handlers;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080051
52 struct poller **pollers;
53 int n_pollers;
54
55 struct pollfd *pollfds;
Jeremy Kerrd831f962016-01-29 17:18:01 +080056};
57
Jeremy Kerr329a35f2016-03-10 15:36:01 +080058struct poller {
59 struct handler *handler;
60 void *data;
61 poller_fn_t fn;
62 bool remove;
63};
64
65/* we have one extra entry in the pollfds array for the VUART tty */
66static const int n_internal_pollfds = 1;
67
Jeremy Kerr769cee12016-03-15 17:53:56 +080068/* state shared with the signal handler */
69static bool sigint;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080070
Jeremy Kerrd831f962016-01-29 17:18:01 +080071static void usage(const char *progname)
72{
73 fprintf(stderr,
74"usage: %s [options]\n"
75"\n"
76"Options:\n"
Jeremy Kerrd66195c2016-03-16 17:24:51 +080077" --config <FILE> Use FILE for configuration\n"
Jeremy Kerrd831f962016-01-29 17:18:01 +080078"",
79 progname);
80}
81
Jeremy Kerr17217842016-01-29 18:44:21 +080082/* populates tty_dev and tty_sysfs_devnode, using the tty kernel name */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080083static int tty_find_device(struct console *console)
Jeremy Kerr17217842016-01-29 18:44:21 +080084{
85 char *tty_class_device_link;
86 char *tty_device_tty_dir;
87 char *tty_device_reldir;
88 int rc;
89
Jeremy Kerr17217842016-01-29 18:44:21 +080090 tty_class_device_link = NULL;
91 tty_device_tty_dir = NULL;
92 tty_device_reldir = NULL;
93
94 rc = asprintf(&tty_class_device_link,
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080095 "/sys/class/tty/%s", console->tty_kname);
Jeremy Kerr17217842016-01-29 18:44:21 +080096 if (rc < 0)
97 return -1;
98
99 tty_device_tty_dir = realpath(tty_class_device_link, NULL);
100 if (rc < 0) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800101 warn("Can't query sysfs for device %s", console->tty_kname);
Jeremy Kerr17217842016-01-29 18:44:21 +0800102 goto out_free;
103 }
104
105 rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
106 if (rc < 0)
107 goto out_free;
108
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800109 console->tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
110 if (!console->tty_sysfs_devnode)
111 warn("Can't find parent device for %s", console->tty_kname);
Jeremy Kerr17217842016-01-29 18:44:21 +0800112
113
114 /* todo: lookup from major/minor info in sysfs, in case udev has
115 * renamed us */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800116 rc = asprintf(&console->tty_dev, "/dev/%s", console->tty_kname);
Jeremy Kerr17217842016-01-29 18:44:21 +0800117 if (rc < 0)
118 goto out_free;
119
120 rc = 0;
121
122out_free:
123 free(tty_class_device_link);
124 free(tty_device_tty_dir);
125 free(tty_device_reldir);
126 return rc;
127}
128
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800129static int tty_set_sysfs_attr(struct console *console, const char *name,
Jeremy Kerr957818b2016-03-08 14:35:15 +0800130 int value)
131{
132 char *path;
133 FILE *fp;
134 int rc;
135
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800136 rc = asprintf(&path, "%s/%s", console->tty_sysfs_devnode, name);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800137 if (rc < 0)
138 return -1;
139
140 fp = fopen(path, "w");
141 if (!fp) {
142 warn("Can't access attribute %s on device %s",
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800143 name, console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800144 rc = -1;
145 goto out_free;
146 }
147 setvbuf(fp, NULL, _IONBF, 0);
148
149 rc = fprintf(fp, "0x%x", value);
150 if (rc < 0)
151 warn("Error writing to %s attribute of device %s",
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800152 name, console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800153 fclose(fp);
154
155
156
157out_free:
158 free(path);
159 return rc;
160}
161
Jeremy Kerrd831f962016-01-29 17:18:01 +0800162/**
Jeremy Kerr54e95692016-03-24 17:07:56 +0800163 * Set console to raw mode: we don't want any processing to occur on
164 * the underlying terminal input/output.
165 */
166static void tty_init_termios(struct console *console)
167{
168 struct termios termios;
169 int rc;
170
171 rc = tcgetattr(console->tty_fd, &termios);
172 if (rc) {
173 warn("Can't read tty termios");
174 return;
175 }
176
177 cfmakeraw(&termios);
178 rc = tcsetattr(console->tty_fd, TCSANOW, &termios);
179 if (rc)
180 warn("Can't set terminal raw mode for tty");
181}
182
183/**
Jeremy Kerrd831f962016-01-29 17:18:01 +0800184 * Open and initialise the serial device
185 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800186static int tty_init_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800187{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800188 if (console->tty_sirq)
189 tty_set_sysfs_attr(console, "sirq", console->tty_sirq);
190 if (console->tty_lpc_addr)
191 tty_set_sysfs_attr(console, "lpc_address",
192 console->tty_lpc_addr);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800193
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800194 console->tty_fd = open(console->tty_dev, O_RDWR);
195 if (console->tty_fd <= 0) {
196 warn("Can't open tty %s", console->tty_dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800197 return -1;
198 }
199
200 /* Disable character delay. We may want to later enable this when
201 * we detect larger amounts of data
202 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800203 fcntl(console->tty_fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800204
Jeremy Kerr54e95692016-03-24 17:07:56 +0800205 tty_init_termios(console);
206
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800207 console->pollfds[console->n_pollers].fd = console->tty_fd;
208 console->pollfds[console->n_pollers].events = POLLIN;
209
Jeremy Kerrd831f962016-01-29 17:18:01 +0800210 return 0;
211}
212
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800213static int tty_init(struct console *console, struct config *config)
214{
215 const char *val;
216 char *endp;
217 int rc;
218
219 console->tty_kname = config_get_value(config, "device");
220
221 val = config_get_value(config, "lpc-address");
222 if (val) {
223 console->tty_lpc_addr = strtoul(val, &endp, 0);
224 if (endp == optarg) {
225 warn("Invalid LPC address: '%s'", val);
226 return -1;
227 }
228 }
229
230 val = config_get_value(config, "sirq");
231 if (val) {
232 console->tty_sirq = strtoul(val, &endp, 0);
233 if (endp == optarg)
234 warn("Invalid sirq: '%s'", val);
235 }
236
237 if (!console->tty_kname) {
238 warnx("Error: No TTY device specified");
239 return -1;
240 }
241
242 rc = tty_find_device(console);
243 if (rc)
244 return rc;
245
246 rc = tty_init_io(console);
247 return rc;
248}
249
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800250
251int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800252{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800253 return write_buf_to_fd(console->tty_fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800254}
255
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800256static void handlers_init(struct console *console, struct config *config)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800257{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800258 extern struct handler *__start_handlers, *__stop_handlers;
259 struct handler *handler;
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800260 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800261
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800262 console->n_handlers = &__stop_handlers - &__start_handlers;
263 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800264
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800265 printf("%d handler%s\n", console->n_handlers,
266 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800267
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800268 for (i = 0; i < console->n_handlers; i++) {
269 handler = console->handlers[i];
270
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800271 rc = 0;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800272 if (handler->init)
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800273 rc = handler->init(handler, console, config);
274
275 handler->active = rc == 0;
276
277 printf(" %s [%sactive]\n", handler->name,
278 handler->active ? "" : "in");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800279 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800280}
281
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800282static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800283{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800284 struct handler *handler;
285 int i;
286
287 for (i = 0; i < console->n_handlers; i++) {
288 handler = console->handlers[i];
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800289 if (handler->fini && handler->active)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800290 handler->fini(handler);
291 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800292}
293
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800294static int handlers_data_in(struct console *console, uint8_t *buf, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800295{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800296 struct handler *handler;
297 int i, rc, tmp;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800298
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800299 rc = 0;
300
301 for (i = 0; i < console->n_handlers; i++) {
302 handler = console->handlers[i];
303
Jeremy Kerr021b91f2016-04-28 11:51:52 +0800304 if (!handler->active)
305 continue;
306
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800307 if (!handler->data_in)
308 continue;
309
310 tmp = handler->data_in(handler, buf, len);
311 if (tmp == HANDLER_EXIT)
312 rc = 1;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800313 }
314
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800315 return rc;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800316
Jeremy Kerrd831f962016-01-29 17:18:01 +0800317}
318
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800319struct poller *console_register_poller(struct console *console,
320 struct handler *handler, poller_fn_t poller_fn,
321 int fd, int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800322{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800323 struct poller *poller;
324 int n;
325
326 poller = malloc(sizeof(*poller));
327 poller->remove = false;
328 poller->handler = handler;
329 poller->fn = poller_fn;
330 poller->data = data;
331
332 /* add one to our pollers array */
333 n = console->n_pollers++;
334 console->pollers = realloc(console->pollers,
335 sizeof(*console->pollers) * console->n_pollers);
336
337 console->pollers[n] = poller;
338
339 /* increase pollfds array too */
340 console->pollfds = realloc(console->pollfds,
341 sizeof(*console->pollfds) *
342 (n_internal_pollfds + console->n_pollers));
343
344 /* shift the end pollfds up by one */
345 memcpy(&console->pollfds[n+n_internal_pollfds],
346 &console->pollfds[n],
347 sizeof(*console->pollfds) * n_internal_pollfds);
348
349 console->pollfds[n].fd = fd;
350 console->pollfds[n].events = events;
351
352 return poller;
353}
354
355void console_unregister_poller(struct console *console,
356 struct poller *poller)
357{
358 int i;
359
360 /* find the entry in our pollers array */
361 for (i = 0; i < console->n_pollers; i++)
362 if (console->pollers[i] == poller)
363 break;
364
365 assert(i < console->n_pollers);
366
367 console->n_pollers--;
368
369 /* remove the item from the pollers array... */
370 memmove(&console->pollers[i], &console->pollers[i+1],
371 sizeof(*console->pollers)
372 * (console->n_pollers - i));
373
374 console->pollers = realloc(console->pollers,
375 sizeof(*console->pollers) * console->n_pollers);
376
377 /* ... and the pollfds array */
378 memmove(&console->pollfds[i], &console->pollfds[i+1],
379 sizeof(*console->pollfds) *
380 (n_internal_pollfds + console->n_pollers - i));
381
382 console->pollfds = realloc(console->pollfds,
383 sizeof(*console->pollfds) *
384 (n_internal_pollfds + console->n_pollers));
385
386
387 free(poller);
388}
389
390static int call_pollers(struct console *console)
391{
392 struct poller *poller;
393 struct pollfd *pollfd;
394 enum poller_ret prc;
395 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800396
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800397 rc = 0;
398
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800399 /*
400 * Process poll events by iterating through the pollers and pollfds
401 * in-step, calling any pollers that we've found revents for.
402 */
403 for (i = 0; i < console->n_pollers; i++) {
404 poller = console->pollers[i];
405 pollfd = &console->pollfds[i];
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800406
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800407 if (!pollfd->revents)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800408 continue;
409
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800410 prc = poller->fn(poller->handler, pollfd->revents,
411 poller->data);
412 if (prc == POLLER_EXIT)
413 rc = -1;
414 else if (prc == POLLER_REMOVE)
415 poller->remove = true;
416 }
417
418 /**
419 * Process deferred removals; restarting each time we unregister, as
420 * the array will have changed
421 */
422 for (;;) {
423 bool removed = false;
424
425 for (i = 0; i < console->n_pollers; i++) {
426 poller = console->pollers[i];
427 if (poller->remove) {
428 console_unregister_poller(console, poller);
429 removed = true;
430 break;
431 }
432 }
433 if (!removed)
434 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800435 }
436
437 return rc;
438}
439
Jeremy Kerr769cee12016-03-15 17:53:56 +0800440static void sighandler(int signal)
441{
442 if (signal == SIGINT)
443 sigint = true;
444}
445
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800446int run_console(struct console *console)
447{
Jeremy Kerr769cee12016-03-15 17:53:56 +0800448 sighandler_t sighandler_save;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800449 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800450
Jeremy Kerr769cee12016-03-15 17:53:56 +0800451 sighandler_save = signal(SIGINT, sighandler);
452
453 rc = 0;
454
Jeremy Kerrd831f962016-01-29 17:18:01 +0800455 for (;;) {
456 uint8_t buf[4096];
457
Jeremy Kerr769cee12016-03-15 17:53:56 +0800458 if (sigint) {
459 fprintf(stderr, "Received interrupt, exiting\n");
460 break;
461 }
462
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800463 rc = poll(console->pollfds,
464 console->n_pollers + n_internal_pollfds, -1);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800465 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800466 if (errno == EINTR) {
467 continue;
468 } else {
469 warn("poll error");
470 break;
471 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800472 }
473
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800474 /* process internal fd first */
475 BUILD_ASSERT(n_internal_pollfds == 1);
476
477 if (console->pollfds[console->n_pollers].revents) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800478 rc = read(console->tty_fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800479 if (rc <= 0) {
480 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800481 rc = -1;
482 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800483 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800484 rc = handlers_data_in(console, buf, rc);
485 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800486 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800487 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800488
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800489 /* ... and then the pollers */
490 rc = call_pollers(console);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800491 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800492 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800493 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800494
495 signal(SIGINT, sighandler_save);
496
497 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800498}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800499static const struct option options[] = {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800500 { "config", required_argument, 0, 'c'},
Joel Stanleyf5858b52016-03-18 16:33:03 +1030501 { 0, 0, 0, 0},
Jeremy Kerrd831f962016-01-29 17:18:01 +0800502};
503
504int main(int argc, char **argv)
505{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800506 const char *config_filename = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800507 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800508 struct config *config;
509 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800510
Jeremy Kerr957818b2016-03-08 14:35:15 +0800511 rc = -1;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800512
513 for (;;) {
514 int c, idx;
515
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800516 c = getopt_long(argc, argv, "c:", options, &idx);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800517 if (c == -1)
518 break;
519
520 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800521 case 'c':
522 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800523 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800524 case 'h':
525 case '?':
526 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800527 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800528 }
529 }
530
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800531 console = malloc(sizeof(struct console));
532 memset(console, 0, sizeof(*console));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800533 console->pollfds = calloc(n_internal_pollfds,
534 sizeof(*console->pollfds));
535
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800536 config = config_init(config_filename);
537 if (!config) {
538 warnx("Can't read configuration, exiting.");
539 goto out_free;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800540 }
541
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800542 rc = tty_init(console, config);
Jeremy Kerr17217842016-01-29 18:44:21 +0800543 if (rc)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800544 goto out_config_fini;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800545
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800546 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800547
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800548 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800549
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800550 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800551
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800552out_config_fini:
553 config_fini(config);
554
Jeremy Kerr957818b2016-03-08 14:35:15 +0800555out_free:
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800556 free(console->pollers);
557 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800558 free(console->tty_sysfs_devnode);
559 free(console->tty_dev);
560 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800561
562 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
563}