blob: f8feb527da542d89f10ac6b04a508add4d594962 [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;
260 int i;
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
271 printf(" %s\n", handler->name);
272
273 if (handler->init)
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800274 handler->init(handler, console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800275 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800276}
277
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800278static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800279{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800280 struct handler *handler;
281 int i;
282
283 for (i = 0; i < console->n_handlers; i++) {
284 handler = console->handlers[i];
285 if (handler->fini)
286 handler->fini(handler);
287 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800288}
289
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800290static int handlers_data_in(struct console *console, uint8_t *buf, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800291{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800292 struct handler *handler;
293 int i, rc, tmp;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800294
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800295 rc = 0;
296
297 for (i = 0; i < console->n_handlers; i++) {
298 handler = console->handlers[i];
299
300 if (!handler->data_in)
301 continue;
302
303 tmp = handler->data_in(handler, buf, len);
304 if (tmp == HANDLER_EXIT)
305 rc = 1;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800306 }
307
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800308 return rc;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800309
Jeremy Kerrd831f962016-01-29 17:18:01 +0800310}
311
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800312struct poller *console_register_poller(struct console *console,
313 struct handler *handler, poller_fn_t poller_fn,
314 int fd, int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800315{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800316 struct poller *poller;
317 int n;
318
319 poller = malloc(sizeof(*poller));
320 poller->remove = false;
321 poller->handler = handler;
322 poller->fn = poller_fn;
323 poller->data = data;
324
325 /* add one to our pollers array */
326 n = console->n_pollers++;
327 console->pollers = realloc(console->pollers,
328 sizeof(*console->pollers) * console->n_pollers);
329
330 console->pollers[n] = poller;
331
332 /* increase pollfds array too */
333 console->pollfds = realloc(console->pollfds,
334 sizeof(*console->pollfds) *
335 (n_internal_pollfds + console->n_pollers));
336
337 /* shift the end pollfds up by one */
338 memcpy(&console->pollfds[n+n_internal_pollfds],
339 &console->pollfds[n],
340 sizeof(*console->pollfds) * n_internal_pollfds);
341
342 console->pollfds[n].fd = fd;
343 console->pollfds[n].events = events;
344
345 return poller;
346}
347
348void console_unregister_poller(struct console *console,
349 struct poller *poller)
350{
351 int i;
352
353 /* find the entry in our pollers array */
354 for (i = 0; i < console->n_pollers; i++)
355 if (console->pollers[i] == poller)
356 break;
357
358 assert(i < console->n_pollers);
359
360 console->n_pollers--;
361
362 /* remove the item from the pollers array... */
363 memmove(&console->pollers[i], &console->pollers[i+1],
364 sizeof(*console->pollers)
365 * (console->n_pollers - i));
366
367 console->pollers = realloc(console->pollers,
368 sizeof(*console->pollers) * console->n_pollers);
369
370 /* ... and the pollfds array */
371 memmove(&console->pollfds[i], &console->pollfds[i+1],
372 sizeof(*console->pollfds) *
373 (n_internal_pollfds + console->n_pollers - i));
374
375 console->pollfds = realloc(console->pollfds,
376 sizeof(*console->pollfds) *
377 (n_internal_pollfds + console->n_pollers));
378
379
380 free(poller);
381}
382
383static int call_pollers(struct console *console)
384{
385 struct poller *poller;
386 struct pollfd *pollfd;
387 enum poller_ret prc;
388 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800389
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800390 rc = 0;
391
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800392 /*
393 * Process poll events by iterating through the pollers and pollfds
394 * in-step, calling any pollers that we've found revents for.
395 */
396 for (i = 0; i < console->n_pollers; i++) {
397 poller = console->pollers[i];
398 pollfd = &console->pollfds[i];
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800399
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800400 if (!pollfd->revents)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800401 continue;
402
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800403 prc = poller->fn(poller->handler, pollfd->revents,
404 poller->data);
405 if (prc == POLLER_EXIT)
406 rc = -1;
407 else if (prc == POLLER_REMOVE)
408 poller->remove = true;
409 }
410
411 /**
412 * Process deferred removals; restarting each time we unregister, as
413 * the array will have changed
414 */
415 for (;;) {
416 bool removed = false;
417
418 for (i = 0; i < console->n_pollers; i++) {
419 poller = console->pollers[i];
420 if (poller->remove) {
421 console_unregister_poller(console, poller);
422 removed = true;
423 break;
424 }
425 }
426 if (!removed)
427 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800428 }
429
430 return rc;
431}
432
Jeremy Kerr769cee12016-03-15 17:53:56 +0800433static void sighandler(int signal)
434{
435 if (signal == SIGINT)
436 sigint = true;
437}
438
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800439int run_console(struct console *console)
440{
Jeremy Kerr769cee12016-03-15 17:53:56 +0800441 sighandler_t sighandler_save;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800442 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800443
Jeremy Kerr769cee12016-03-15 17:53:56 +0800444 sighandler_save = signal(SIGINT, sighandler);
445
446 rc = 0;
447
Jeremy Kerrd831f962016-01-29 17:18:01 +0800448 for (;;) {
449 uint8_t buf[4096];
450
Jeremy Kerr769cee12016-03-15 17:53:56 +0800451 if (sigint) {
452 fprintf(stderr, "Received interrupt, exiting\n");
453 break;
454 }
455
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800456 rc = poll(console->pollfds,
457 console->n_pollers + n_internal_pollfds, -1);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800458 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800459 if (errno == EINTR) {
460 continue;
461 } else {
462 warn("poll error");
463 break;
464 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800465 }
466
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800467 /* process internal fd first */
468 BUILD_ASSERT(n_internal_pollfds == 1);
469
470 if (console->pollfds[console->n_pollers].revents) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800471 rc = read(console->tty_fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800472 if (rc <= 0) {
473 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800474 rc = -1;
475 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800476 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800477 rc = handlers_data_in(console, buf, rc);
478 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800479 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800480 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800481
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800482 /* ... and then the pollers */
483 rc = call_pollers(console);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800484 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800485 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800486 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800487
488 signal(SIGINT, sighandler_save);
489
490 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800491}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800492static const struct option options[] = {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800493 { "config", required_argument, 0, 'c'},
Joel Stanleyf5858b52016-03-18 16:33:03 +1030494 { 0, 0, 0, 0},
Jeremy Kerrd831f962016-01-29 17:18:01 +0800495};
496
497int main(int argc, char **argv)
498{
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800499 const char *config_filename = NULL;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800500 struct console *console;
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800501 struct config *config;
502 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800503
Jeremy Kerr957818b2016-03-08 14:35:15 +0800504 rc = -1;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800505
506 for (;;) {
507 int c, idx;
508
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800509 c = getopt_long(argc, argv, "c:", options, &idx);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800510 if (c == -1)
511 break;
512
513 switch (c) {
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800514 case 'c':
515 config_filename = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800516 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800517 case 'h':
518 case '?':
519 usage(argv[0]);
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800520 return EXIT_SUCCESS;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800521 }
522 }
523
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800524 console = malloc(sizeof(struct console));
525 memset(console, 0, sizeof(*console));
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800526 console->pollfds = calloc(n_internal_pollfds,
527 sizeof(*console->pollfds));
528
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800529 config = config_init(config_filename);
530 if (!config) {
531 warnx("Can't read configuration, exiting.");
532 goto out_free;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800533 }
534
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800535 rc = tty_init(console, config);
Jeremy Kerr17217842016-01-29 18:44:21 +0800536 if (rc)
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800537 goto out_config_fini;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800538
Jeremy Kerrd47963e2016-03-16 17:29:55 +0800539 handlers_init(console, config);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800540
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800541 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800542
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800543 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800544
Jeremy Kerrd66195c2016-03-16 17:24:51 +0800545out_config_fini:
546 config_fini(config);
547
Jeremy Kerr957818b2016-03-08 14:35:15 +0800548out_free:
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800549 free(console->pollers);
550 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800551 free(console->tty_sysfs_devnode);
552 free(console->tty_dev);
553 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800554
555 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
556}