blob: 00ada7a5cb028fb494500421e558c50a4e3c96fd [file] [log] [blame]
Jeremy Kerrbc1e8932016-04-28 12:27:30 +08001/**
2 * Copyright © 2016 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define _GNU_SOURCE
18
19#include <assert.h>
20#include <err.h>
21#include <fcntl.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <unistd.h>
25
26#include "console-server.h"
27
28struct tty_handler {
29 struct handler handler;
30 struct console *console;
31 struct poller *poller;
32 int fd;
33};
34
35static struct tty_handler *to_tty_handler(struct handler *handler)
36{
37 return container_of(handler, struct tty_handler, handler);
38}
39
40static enum poller_ret tty_poll(struct handler *handler,
41 int events, void __attribute__((unused)) *data)
42{
43 struct tty_handler *th = to_tty_handler(handler);
44 uint8_t buf[4096];
45 ssize_t len;
46
47 if (!(events & POLLIN))
48 return POLLER_OK;
49
50 len = read(th->fd, buf, sizeof(buf));
51 if (len <= 0) {
52 th->poller = NULL;
53 close(th->fd);
54 return POLLER_REMOVE;
55 }
56
57 console_data_out(th->console, buf, len);
58
59 return POLLER_OK;
60}
61
62static int tty_init(struct handler *handler, struct console *console,
63 struct config *config __attribute__((unused)))
64{
65 struct tty_handler *th = to_tty_handler(handler);
66 const char *tty_name;
67 char *tty_path;
68 int rc, flags;
69
70 tty_name = config_get_value(config, "local-tty");
71 if (!tty_name)
72 return -1;
73
74 rc = asprintf(&tty_path, "/dev/%s", tty_name);
75 if (!rc)
76 return -1;
77
78 th->fd = open(tty_path, O_RDWR);
79 if (th->fd < 0) {
80 warn("Can't open %s; disabling local tty", tty_name);
81 free(tty_path);
82 return -1;
83 }
84
85 free(tty_path);
86
87 /* initial tty setup */
88 flags = fcntl(th->fd, F_GETFL, 0);
89 flags |= FNDELAY;
90 fcntl(th->fd, F_SETFL, flags);
91
92 th->poller = console_register_poller(console, handler, tty_poll,
93 th->fd, POLLIN, NULL);
94 th->console = console;
95
96 return 0;
97}
98
99static int tty_data(struct handler *handler, uint8_t *buf, size_t len)
100{
101 struct tty_handler *th = to_tty_handler(handler);
102 return write_buf_to_fd(th->fd, buf, len);
103}
104
105static void tty_fini(struct handler *handler)
106{
107 struct tty_handler *th = to_tty_handler(handler);
108 if (th->poller)
109 console_unregister_poller(th->console, th->poller);
110 close(th->fd);
111}
112
113static struct tty_handler tty_handler = {
114 .handler = {
115 .name = "tty",
116 .init = tty_init,
117 .data_in = tty_data,
118 .fini = tty_fini,
119 },
120};
121
122console_register_handler(&tty_handler.handler);
123