blob: 3546aca341e9afe599abebb6f1123d04716a010b [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>
Jeremy Kerrf733c852017-02-07 18:40:10 +080021#include <errno.h>
Jeremy Kerrbc1e8932016-04-28 12:27:30 +080022#include <fcntl.h>
23#include <stdio.h>
24#include <stdlib.h>
Xo Wangc5ef8ea2017-04-17 16:20:43 -070025#include <string.h>
Jeremy Kerrbc1e8932016-04-28 12:27:30 +080026#include <unistd.h>
Xo Wangc5ef8ea2017-04-17 16:20:43 -070027#include <termios.h>
Jeremy Kerrbc1e8932016-04-28 12:27:30 +080028
29#include "console-server.h"
30
31struct tty_handler {
Jeremy Kerrf733c852017-02-07 18:40:10 +080032 struct handler handler;
33 struct console *console;
34 struct ringbuffer_consumer *rbc;
35 struct poller *poller;
36 int fd;
Jeremy Kerr6b1fed22017-02-07 21:40:38 +080037 int fd_flags;
38 bool blocked;
Jeremy Kerrbc1e8932016-04-28 12:27:30 +080039};
40
41static struct tty_handler *to_tty_handler(struct handler *handler)
42{
43 return container_of(handler, struct tty_handler, handler);
44}
45
Jeremy Kerr6b1fed22017-02-07 21:40:38 +080046static void tty_set_fd_blocking(struct tty_handler *th, bool fd_blocking)
47{
48 int flags;
49
50 flags = th->fd_flags & ~O_NONBLOCK;
51 if (!fd_blocking)
52 flags |= O_NONBLOCK;
53
54 if (flags != th->fd_flags) {
55 fcntl(th->fd, F_SETFL, flags);
56 th->fd_flags = flags;
57 }
58}
59
60/*
61 * A "blocked" handler indicates that the last write returned EAGAIN
62 * (==EWOULDBLOCK), so we know not to continue writing (for non-forced output),
63 * as it'll just return EAGAIN again.
64 *
65 * Once we detect this, we watch for POLLOUT in the poller events. A
66 * POLLOUT indicates that the fd is no longer blocking, so we clear
67 * blocked mode and can continue writing.
68 */
69static void tty_set_blocked(struct tty_handler *th, bool blocked)
70{
71 int events;
72
73 if (blocked == th->blocked)
74 return;
75
76 th->blocked = blocked;
77 events = POLLIN;
78
79 if (th->blocked)
80 events |= POLLOUT;
81
82 console_poller_set_events(th->console, th->poller, events);
83}
84
Jeremy Kerrf733c852017-02-07 18:40:10 +080085static int tty_drain_queue(struct tty_handler *th, size_t force_len)
86{
87 size_t len, total_len;
88 ssize_t wlen;
89 uint8_t *buf;
Jeremy Kerrf733c852017-02-07 18:40:10 +080090
91 /* if we're forcing data, we need to clear non-blocking mode */
Jeremy Kerr6b1fed22017-02-07 21:40:38 +080092 if (force_len)
93 tty_set_fd_blocking(th, true);
94
95 /* no point writing, we'll just see -EAGAIN */
96 else if (th->blocked)
97 return 0;
Jeremy Kerrf733c852017-02-07 18:40:10 +080098
99 total_len = 0;
100
101 for (;;) {
102 len = ringbuffer_dequeue_peek(th->rbc, total_len, &buf);
103 if (!len)
104 break;
105
106 /* write as little as possible while blocking */
107 if (force_len && force_len < total_len + len)
108 len = force_len - total_len;
109
110 wlen = write(th->fd, buf, len);
111 if (wlen < 0) {
112 if (errno == EINTR)
113 continue;
114 if ((errno == EAGAIN || errno == EWOULDBLOCK)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800115 && !force_len) {
116 tty_set_blocked(th, true);
Jeremy Kerrf733c852017-02-07 18:40:10 +0800117 break;
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800118 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800119 warn("failed writing to local tty; disabling");
120 return -1;
121 }
122
123 total_len += wlen;
124
125 if (force_len && total_len >= force_len)
126 break;
127 }
128
129 ringbuffer_dequeue_commit(th->rbc, total_len);
130
131 if (force_len)
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800132 tty_set_fd_blocking(th, false);
Jeremy Kerrf733c852017-02-07 18:40:10 +0800133
134 return 0;
135}
136
137static enum ringbuffer_poll_ret tty_ringbuffer_poll(void *arg, size_t force_len)
138{
139 struct tty_handler *th = arg;
140 int rc;
141
142 rc = tty_drain_queue(th, force_len);
143 if (rc) {
144 console_poller_unregister(th->console, th->poller);
145 return RINGBUFFER_POLL_REMOVE;
146 }
147
148 return RINGBUFFER_POLL_OK;
149}
150
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800151static enum poller_ret tty_poll(struct handler *handler,
152 int events, void __attribute__((unused)) *data)
153{
154 struct tty_handler *th = to_tty_handler(handler);
155 uint8_t buf[4096];
156 ssize_t len;
Jeremy Kerrf733c852017-02-07 18:40:10 +0800157 int rc;
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800158
Jeremy Kerrf733c852017-02-07 18:40:10 +0800159 if (events & POLLIN) {
160 len = read(th->fd, buf, sizeof(buf));
Jeremy Kerr67eab042017-07-12 12:08:35 +0800161 if (len <= 0)
162 goto err;
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800163
Jeremy Kerrf733c852017-02-07 18:40:10 +0800164 console_data_out(th->console, buf, len);
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800165 }
166
Jeremy Kerrf733c852017-02-07 18:40:10 +0800167 if (events & POLLOUT) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800168 tty_set_blocked(th, false);
Jeremy Kerrf733c852017-02-07 18:40:10 +0800169 rc = tty_drain_queue(th, 0);
Jeremy Kerr67eab042017-07-12 12:08:35 +0800170 if (rc)
171 goto err;
Jeremy Kerrf733c852017-02-07 18:40:10 +0800172 }
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800173
174 return POLLER_OK;
Jeremy Kerr67eab042017-07-12 12:08:35 +0800175
176err:
177 th->poller = NULL;
178 close(th->fd);
179 ringbuffer_consumer_unregister(th->rbc);
180 return POLLER_REMOVE;
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800181}
182
Xo Wangc5ef8ea2017-04-17 16:20:43 -0700183static int set_terminal_baud(struct tty_handler *th, const char *tty_name,
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800184 speed_t speed) {
Xo Wangc5ef8ea2017-04-17 16:20:43 -0700185 struct termios term_options;
Xo Wangc5ef8ea2017-04-17 16:20:43 -0700186
187 if (tcgetattr(th->fd, &term_options) < 0) {
188 warn("Can't get config for %s", tty_name);
189 return -1;
190 }
191
192 if (cfsetspeed(&term_options, speed) < 0) {
193 warn("Couldn't set speeds for %s", tty_name);
194 return -1;
195 }
196
197 if (tcsetattr(th->fd, TCSAFLUSH, &term_options) < 0) {
198 warn("Couldn't commit terminal options for %s", tty_name);
199 return -1;
200 }
Xo Wangc5ef8ea2017-04-17 16:20:43 -0700201
202 return 0;
203}
204
Xo Wang81408bd2017-04-18 16:43:07 -0700205static int make_terminal_raw(struct tty_handler *th, const char *tty_name) {
206 struct termios term_options;
207
208 if (tcgetattr(th->fd, &term_options) < 0) {
209 warn("Can't get config for %s", tty_name);
210 return -1;
211 }
212
213 /* Disable various input and output processing including character
214 * translation, line edit (canonical) mode, flow control, and special signal
215 * generating characters. */
216 cfmakeraw(&term_options);
217
218 if (tcsetattr(th->fd, TCSAFLUSH, &term_options) < 0) {
219 warn("Couldn't commit terminal options for %s", tty_name);
220 return -1;
221 }
222 printf("Set %s for raw byte handling\n", tty_name);
223
224 return 0;
225}
226
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800227static int tty_init(struct handler *handler, struct console *console,
228 struct config *config __attribute__((unused)))
229{
230 struct tty_handler *th = to_tty_handler(handler);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800231 speed_t desired_speed;
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800232 const char *tty_name;
Xo Wangc5ef8ea2017-04-17 16:20:43 -0700233 const char *tty_baud;
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800234 char *tty_path;
Jeremy Kerrbc506fd2017-02-07 09:24:48 +0800235 int rc;
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800236
237 tty_name = config_get_value(config, "local-tty");
238 if (!tty_name)
239 return -1;
240
241 rc = asprintf(&tty_path, "/dev/%s", tty_name);
242 if (!rc)
243 return -1;
244
Jeremy Kerrbc506fd2017-02-07 09:24:48 +0800245 th->fd = open(tty_path, O_RDWR | O_NONBLOCK);
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800246 if (th->fd < 0) {
247 warn("Can't open %s; disabling local tty", tty_name);
248 free(tty_path);
249 return -1;
250 }
251
252 free(tty_path);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800253 th->fd_flags = fcntl(th->fd, F_GETFL, 0);
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800254
Xo Wangc5ef8ea2017-04-17 16:20:43 -0700255 tty_baud = config_get_value(config, "local-tty-baud");
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800256 if (tty_baud != NULL) {
257 rc = config_parse_baud(&desired_speed, tty_baud);
258 if (rc) {
259 fprintf(stderr, "%s is not a valid baud rate\n",
260 tty_baud);
261 } else {
262 rc = set_terminal_baud(th, tty_name, desired_speed);
263 if (rc)
264 fprintf(stderr, "Couldn't set baud rate for %s to %s\n",
Xo Wangc5ef8ea2017-04-17 16:20:43 -0700265 tty_name, tty_baud);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800266 }
267 }
Xo Wangc5ef8ea2017-04-17 16:20:43 -0700268
Xo Wang81408bd2017-04-18 16:43:07 -0700269 if (make_terminal_raw(th, tty_name) != 0)
270 fprintf(stderr, "Couldn't make %s a raw terminal\n", tty_name);
271
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800272 th->poller = console_poller_register(console, handler, tty_poll, NULL,
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800273 th->fd, POLLIN, NULL);
274 th->console = console;
Jeremy Kerrf733c852017-02-07 18:40:10 +0800275 th->rbc = console_ringbuffer_consumer_register(console,
276 tty_ringbuffer_poll, th);
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800277
278 return 0;
279}
280
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800281static void tty_fini(struct handler *handler)
282{
283 struct tty_handler *th = to_tty_handler(handler);
284 if (th->poller)
Jeremy Kerr55c97122017-02-07 17:06:46 +0800285 console_poller_unregister(th->console, th->poller);
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800286 close(th->fd);
287}
288
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800289static int tty_baudrate(struct handler *handler, speed_t baudrate)
290{
291 const char *tty_name = "local-tty";
292 struct tty_handler *th = to_tty_handler(handler);
293
294 if (baudrate == 0) {
295 return -1;
296 }
297
298 if (set_terminal_baud(th, tty_name, baudrate) != 0) {
299 fprintf(stderr, "Couldn't set baud rate for %s to %d\n",
300 tty_name, baudrate);
301 return -1;
302 }
303 return 0;
304}
305
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800306static struct tty_handler tty_handler = {
307 .handler = {
308 .name = "tty",
309 .init = tty_init,
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800310 .fini = tty_fini,
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800311 .baudrate = tty_baudrate,
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800312 },
313};
314
Jeremy Kerr55c97122017-02-07 17:06:46 +0800315console_handler_register(&tty_handler.handler);
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800316