blob: e0b42497d69a654f51f3bfb19c1f346467dfc344 [file] [log] [blame]
Jeremy Kerrd831f962016-01-29 17:18:01 +08001/**
2 * Console server process for OpenBMC
3 *
4 * Copyright © 2016 IBM Corporation <jk@ozlabs.org>
5 */
6
Jeremy Kerr17217842016-01-29 18:44:21 +08007#define _GNU_SOURCE
8
Jeremy Kerr329a35f2016-03-10 15:36:01 +08009#include <assert.h>
Jeremy Kerr769cee12016-03-15 17:53:56 +080010#include <errno.h>
11#include <signal.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080012#include <stdint.h>
13#include <stdbool.h>
14#include <stdlib.h>
15#include <stdio.h>
16#include <fcntl.h>
17#include <unistd.h>
18#include <err.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080019#include <string.h>
20#include <getopt.h>
Jeremy Kerr17217842016-01-29 18:44:21 +080021#include <limits.h>
Jeremy Kerrd831f962016-01-29 17:18:01 +080022
23#include <sys/types.h>
24#include <sys/poll.h>
25
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080026#include "console-server.h"
Jeremy Kerrd831f962016-01-29 17:18:01 +080027
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080028struct console {
Jeremy Kerr17217842016-01-29 18:44:21 +080029 const char *tty_kname;
30 char *tty_sysfs_devnode;
31 char *tty_dev;
Jeremy Kerr957818b2016-03-08 14:35:15 +080032 int tty_sirq;
33 int tty_lpc_addr;
Jeremy Kerrd831f962016-01-29 17:18:01 +080034 int tty_fd;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080035
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080036 struct handler **handlers;
37 int n_handlers;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080038
39 struct poller **pollers;
40 int n_pollers;
41
42 struct pollfd *pollfds;
Jeremy Kerrd831f962016-01-29 17:18:01 +080043};
44
Jeremy Kerr329a35f2016-03-10 15:36:01 +080045struct poller {
46 struct handler *handler;
47 void *data;
48 poller_fn_t fn;
49 bool remove;
50};
51
52/* we have one extra entry in the pollfds array for the VUART tty */
53static const int n_internal_pollfds = 1;
54
Jeremy Kerr769cee12016-03-15 17:53:56 +080055/* state shared with the signal handler */
56static bool sigint;
Jeremy Kerr329a35f2016-03-10 15:36:01 +080057
Jeremy Kerrd831f962016-01-29 17:18:01 +080058static void usage(const char *progname)
59{
60 fprintf(stderr,
61"usage: %s [options]\n"
62"\n"
63"Options:\n"
Jeremy Kerr17217842016-01-29 18:44:21 +080064" --device <TTY> Use serial device TTY (eg, ttyS0)\n"
Jeremy Kerrd831f962016-01-29 17:18:01 +080065"",
66 progname);
67}
68
Jeremy Kerr17217842016-01-29 18:44:21 +080069/* populates tty_dev and tty_sysfs_devnode, using the tty kernel name */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080070static int tty_find_device(struct console *console)
Jeremy Kerr17217842016-01-29 18:44:21 +080071{
72 char *tty_class_device_link;
73 char *tty_device_tty_dir;
74 char *tty_device_reldir;
75 int rc;
76
77 rc = -1;
78 tty_class_device_link = NULL;
79 tty_device_tty_dir = NULL;
80 tty_device_reldir = NULL;
81
82 rc = asprintf(&tty_class_device_link,
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080083 "/sys/class/tty/%s", console->tty_kname);
Jeremy Kerr17217842016-01-29 18:44:21 +080084 if (rc < 0)
85 return -1;
86
87 tty_device_tty_dir = realpath(tty_class_device_link, NULL);
88 if (rc < 0) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080089 warn("Can't query sysfs for device %s", console->tty_kname);
Jeremy Kerr17217842016-01-29 18:44:21 +080090 goto out_free;
91 }
92
93 rc = asprintf(&tty_device_reldir, "%s/../../", tty_device_tty_dir);
94 if (rc < 0)
95 goto out_free;
96
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +080097 console->tty_sysfs_devnode = realpath(tty_device_reldir, NULL);
98 if (!console->tty_sysfs_devnode)
99 warn("Can't find parent device for %s", console->tty_kname);
Jeremy Kerr17217842016-01-29 18:44:21 +0800100
101
102 /* todo: lookup from major/minor info in sysfs, in case udev has
103 * renamed us */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800104 rc = asprintf(&console->tty_dev, "/dev/%s", console->tty_kname);
Jeremy Kerr17217842016-01-29 18:44:21 +0800105 if (rc < 0)
106 goto out_free;
107
108 rc = 0;
109
110out_free:
111 free(tty_class_device_link);
112 free(tty_device_tty_dir);
113 free(tty_device_reldir);
114 return rc;
115}
116
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800117static int tty_set_sysfs_attr(struct console *console, const char *name,
Jeremy Kerr957818b2016-03-08 14:35:15 +0800118 int value)
119{
120 char *path;
121 FILE *fp;
122 int rc;
123
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800124 rc = asprintf(&path, "%s/%s", console->tty_sysfs_devnode, name);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800125 if (rc < 0)
126 return -1;
127
128 fp = fopen(path, "w");
129 if (!fp) {
130 warn("Can't access attribute %s on device %s",
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800131 name, console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800132 rc = -1;
133 goto out_free;
134 }
135 setvbuf(fp, NULL, _IONBF, 0);
136
137 rc = fprintf(fp, "0x%x", value);
138 if (rc < 0)
139 warn("Error writing to %s attribute of device %s",
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800140 name, console->tty_kname);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800141 fclose(fp);
142
143
144
145out_free:
146 free(path);
147 return rc;
148}
149
Jeremy Kerrd831f962016-01-29 17:18:01 +0800150/**
151 * Open and initialise the serial device
152 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800153static int tty_init_io(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800154{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800155 if (console->tty_sirq)
156 tty_set_sysfs_attr(console, "sirq", console->tty_sirq);
157 if (console->tty_lpc_addr)
158 tty_set_sysfs_attr(console, "lpc_address",
159 console->tty_lpc_addr);
160 tty_set_sysfs_attr(console, "enabled", 1);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800161
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800162 console->tty_fd = open(console->tty_dev, O_RDWR);
163 if (console->tty_fd <= 0) {
164 warn("Can't open tty %s", console->tty_dev);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800165 return -1;
166 }
167
168 /* Disable character delay. We may want to later enable this when
169 * we detect larger amounts of data
170 */
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800171 fcntl(console->tty_fd, F_SETFL, FNDELAY);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800172
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800173 console->pollfds[console->n_pollers].fd = console->tty_fd;
174 console->pollfds[console->n_pollers].events = POLLIN;
175
Jeremy Kerrd831f962016-01-29 17:18:01 +0800176 return 0;
177}
178
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800179
180int console_data_out(struct console *console, const uint8_t *data, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800181{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800182 return write_buf_to_fd(console->tty_fd, data, len);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800183}
184
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800185static void handlers_init(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800186{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800187 extern struct handler *__start_handlers, *__stop_handlers;
188 struct handler *handler;
189 int i;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800190
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800191 console->n_handlers = &__stop_handlers - &__start_handlers;
192 console->handlers = &__start_handlers;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800193
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800194 printf("%d handler%s\n", console->n_handlers,
195 console->n_handlers == 1 ? "" : "s");
Jeremy Kerrd831f962016-01-29 17:18:01 +0800196
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800197 for (i = 0; i < console->n_handlers; i++) {
198 handler = console->handlers[i];
199
200 printf(" %s\n", handler->name);
201
202 if (handler->init)
203 handler->init(handler, console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800204 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800205}
206
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800207static void handlers_fini(struct console *console)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800208{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800209 struct handler *handler;
210 int i;
211
212 for (i = 0; i < console->n_handlers; i++) {
213 handler = console->handlers[i];
214 if (handler->fini)
215 handler->fini(handler);
216 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800217}
218
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800219static int handlers_data_in(struct console *console, uint8_t *buf, size_t len)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800220{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800221 struct handler *handler;
222 int i, rc, tmp;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800223
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800224 rc = 0;
225
226 for (i = 0; i < console->n_handlers; i++) {
227 handler = console->handlers[i];
228
229 if (!handler->data_in)
230 continue;
231
232 tmp = handler->data_in(handler, buf, len);
233 if (tmp == HANDLER_EXIT)
234 rc = 1;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800235 }
236
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800237 return rc;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800238
Jeremy Kerrd831f962016-01-29 17:18:01 +0800239}
240
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800241struct poller *console_register_poller(struct console *console,
242 struct handler *handler, poller_fn_t poller_fn,
243 int fd, int events, void *data)
Jeremy Kerrd831f962016-01-29 17:18:01 +0800244{
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800245 struct poller *poller;
246 int n;
247
248 poller = malloc(sizeof(*poller));
249 poller->remove = false;
250 poller->handler = handler;
251 poller->fn = poller_fn;
252 poller->data = data;
253
254 /* add one to our pollers array */
255 n = console->n_pollers++;
256 console->pollers = realloc(console->pollers,
257 sizeof(*console->pollers) * console->n_pollers);
258
259 console->pollers[n] = poller;
260
261 /* increase pollfds array too */
262 console->pollfds = realloc(console->pollfds,
263 sizeof(*console->pollfds) *
264 (n_internal_pollfds + console->n_pollers));
265
266 /* shift the end pollfds up by one */
267 memcpy(&console->pollfds[n+n_internal_pollfds],
268 &console->pollfds[n],
269 sizeof(*console->pollfds) * n_internal_pollfds);
270
271 console->pollfds[n].fd = fd;
272 console->pollfds[n].events = events;
273
274 return poller;
275}
276
277void console_unregister_poller(struct console *console,
278 struct poller *poller)
279{
280 int i;
281
282 /* find the entry in our pollers array */
283 for (i = 0; i < console->n_pollers; i++)
284 if (console->pollers[i] == poller)
285 break;
286
287 assert(i < console->n_pollers);
288
289 console->n_pollers--;
290
291 /* remove the item from the pollers array... */
292 memmove(&console->pollers[i], &console->pollers[i+1],
293 sizeof(*console->pollers)
294 * (console->n_pollers - i));
295
296 console->pollers = realloc(console->pollers,
297 sizeof(*console->pollers) * console->n_pollers);
298
299 /* ... and the pollfds array */
300 memmove(&console->pollfds[i], &console->pollfds[i+1],
301 sizeof(*console->pollfds) *
302 (n_internal_pollfds + console->n_pollers - i));
303
304 console->pollfds = realloc(console->pollfds,
305 sizeof(*console->pollfds) *
306 (n_internal_pollfds + console->n_pollers));
307
308
309 free(poller);
310}
311
312static int call_pollers(struct console *console)
313{
314 struct poller *poller;
315 struct pollfd *pollfd;
316 enum poller_ret prc;
317 int i, rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800318
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800319 rc = 0;
320
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800321 /*
322 * Process poll events by iterating through the pollers and pollfds
323 * in-step, calling any pollers that we've found revents for.
324 */
325 for (i = 0; i < console->n_pollers; i++) {
326 poller = console->pollers[i];
327 pollfd = &console->pollfds[i];
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800328
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800329 if (!pollfd->revents)
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800330 continue;
331
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800332 prc = poller->fn(poller->handler, pollfd->revents,
333 poller->data);
334 if (prc == POLLER_EXIT)
335 rc = -1;
336 else if (prc == POLLER_REMOVE)
337 poller->remove = true;
338 }
339
340 /**
341 * Process deferred removals; restarting each time we unregister, as
342 * the array will have changed
343 */
344 for (;;) {
345 bool removed = false;
346
347 for (i = 0; i < console->n_pollers; i++) {
348 poller = console->pollers[i];
349 if (poller->remove) {
350 console_unregister_poller(console, poller);
351 removed = true;
352 break;
353 }
354 }
355 if (!removed)
356 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800357 }
358
359 return rc;
360}
361
Jeremy Kerr769cee12016-03-15 17:53:56 +0800362static void sighandler(int signal)
363{
364 if (signal == SIGINT)
365 sigint = true;
366}
367
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800368int run_console(struct console *console)
369{
Jeremy Kerr769cee12016-03-15 17:53:56 +0800370 sighandler_t sighandler_save;
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800371 int rc;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800372
Jeremy Kerr769cee12016-03-15 17:53:56 +0800373 sighandler_save = signal(SIGINT, sighandler);
374
375 rc = 0;
376
Jeremy Kerrd831f962016-01-29 17:18:01 +0800377 for (;;) {
378 uint8_t buf[4096];
379
Jeremy Kerr769cee12016-03-15 17:53:56 +0800380 if (sigint) {
381 fprintf(stderr, "Received interrupt, exiting\n");
382 break;
383 }
384
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800385 rc = poll(console->pollfds,
386 console->n_pollers + n_internal_pollfds, -1);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800387 if (rc < 0) {
Jeremy Kerr769cee12016-03-15 17:53:56 +0800388 if (errno == EINTR) {
389 continue;
390 } else {
391 warn("poll error");
392 break;
393 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800394 }
395
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800396 /* process internal fd first */
397 BUILD_ASSERT(n_internal_pollfds == 1);
398
399 if (console->pollfds[console->n_pollers].revents) {
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800400 rc = read(console->tty_fd, buf, sizeof(buf));
Jeremy Kerrd831f962016-01-29 17:18:01 +0800401 if (rc <= 0) {
402 warn("Error reading from tty device");
Jeremy Kerr769cee12016-03-15 17:53:56 +0800403 rc = -1;
404 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800405 }
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800406 rc = handlers_data_in(console, buf, rc);
407 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800408 break;
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800409 }
Jeremy Kerrd831f962016-01-29 17:18:01 +0800410
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800411 /* ... and then the pollers */
412 rc = call_pollers(console);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800413 if (rc)
Jeremy Kerr769cee12016-03-15 17:53:56 +0800414 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800415 }
Jeremy Kerr769cee12016-03-15 17:53:56 +0800416
417 signal(SIGINT, sighandler_save);
418
419 return rc ? -1 : 0;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800420}
Jeremy Kerrd831f962016-01-29 17:18:01 +0800421static const struct option options[] = {
422 { "device", required_argument, 0, 'd'},
Jeremy Kerr957818b2016-03-08 14:35:15 +0800423 { "sirq", required_argument, 0, 's'},
424 { "lpc-addr", required_argument, 0, 'l'},
Jeremy Kerrd831f962016-01-29 17:18:01 +0800425 { },
426};
427
428int main(int argc, char **argv)
429{
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800430 struct console *console;
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800431 int rc, i;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800432
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800433 console = malloc(sizeof(struct console));
434 memset(console, 0, sizeof(*console));
Jeremy Kerr957818b2016-03-08 14:35:15 +0800435 rc = -1;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800436
437 for (;;) {
Jeremy Kerr957818b2016-03-08 14:35:15 +0800438 char *endp;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800439 int c, idx;
440
Jeremy Kerr957818b2016-03-08 14:35:15 +0800441 c = getopt_long(argc, argv, "d:s:l:", options, &idx);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800442 if (c == -1)
443 break;
444
445 switch (c) {
446 case 'd':
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800447 console->tty_kname = optarg;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800448 break;
Jeremy Kerr957818b2016-03-08 14:35:15 +0800449 case 'l':
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800450 console->tty_lpc_addr = strtoul(optarg, &endp, 0);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800451 if (endp == optarg) {
452 warnx("Invalid sirq: '%s'", optarg);
453 goto out_free;
454 }
455 break;
456
457 case 's':
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800458 console->tty_sirq = strtoul(optarg, &endp, 0);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800459 if (endp == optarg) {
460 warnx("Invalid sirq: '%s'", optarg);
461 goto out_free;
462 }
463 break;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800464
465 case 'h':
466 case '?':
467 usage(argv[0]);
Jeremy Kerr957818b2016-03-08 14:35:15 +0800468 rc = 0;
469 goto out_free;
Jeremy Kerrd831f962016-01-29 17:18:01 +0800470 }
471 }
472
Jeremy Kerr329a35f2016-03-10 15:36:01 +0800473 console->pollfds = calloc(n_internal_pollfds,
474 sizeof(*console->pollfds));
475
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800476 if (!console->tty_kname) {
Jeremy Kerrd831f962016-01-29 17:18:01 +0800477 fprintf(stderr,
478 "Error: No TTY device specified (use --device)\n");
479 return EXIT_FAILURE;
480 }
481
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800482 rc = tty_find_device(console);
Jeremy Kerr17217842016-01-29 18:44:21 +0800483 if (rc)
484 return EXIT_FAILURE;
485
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800486 rc = tty_init_io(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800487 if (rc)
488 return EXIT_FAILURE;
489
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800490 handlers_init(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800491
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800492 rc = run_console(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800493
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800494 handlers_fini(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800495
Jeremy Kerr957818b2016-03-08 14:35:15 +0800496out_free:
Jeremy Kerr89ea8192016-03-15 17:57:43 +0800497 free(console->pollers);
498 free(console->pollfds);
Jeremy Kerr1a0e03b2016-03-08 17:57:11 +0800499 free(console->tty_sysfs_devnode);
500 free(console->tty_dev);
501 free(console);
Jeremy Kerrd831f962016-01-29 17:18:01 +0800502
503 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
504}