blob: a02d899d4d183bbac6c9a8fc1708af58701f554b [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
Jeremy Kerrbc1e8932016-04-28 12:27:30 +080017#include <assert.h>
18#include <err.h>
Jeremy Kerrf733c852017-02-07 18:40:10 +080019#include <errno.h>
Jeremy Kerrbc1e8932016-04-28 12:27:30 +080020#include <fcntl.h>
21#include <stdio.h>
22#include <stdlib.h>
Xo Wangc5ef8ea2017-04-17 16:20:43 -070023#include <string.h>
Jeremy Kerrbc1e8932016-04-28 12:27:30 +080024#include <unistd.h>
Xo Wangc5ef8ea2017-04-17 16:20:43 -070025#include <termios.h>
Jeremy Kerrbc1e8932016-04-28 12:27:30 +080026
27#include "console-server.h"
28
29struct tty_handler {
Andrew Jefferya72711a2023-04-18 18:19:41 +093030 struct handler handler;
31 struct console *console;
32 struct ringbuffer_consumer *rbc;
33 struct poller *poller;
34 int fd;
35 int fd_flags;
36 bool blocked;
Jeremy Kerrbc1e8932016-04-28 12:27:30 +080037};
38
39static struct tty_handler *to_tty_handler(struct handler *handler)
40{
41 return container_of(handler, struct tty_handler, handler);
42}
43
Jeremy Kerr6b1fed22017-02-07 21:40:38 +080044static void tty_set_fd_blocking(struct tty_handler *th, bool fd_blocking)
45{
46 int flags;
47
48 flags = th->fd_flags & ~O_NONBLOCK;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093049 if (!fd_blocking) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +080050 flags |= O_NONBLOCK;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093051 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +080052
53 if (flags != th->fd_flags) {
54 fcntl(th->fd, F_SETFL, flags);
55 th->fd_flags = flags;
56 }
57}
58
59/*
60 * A "blocked" handler indicates that the last write returned EAGAIN
61 * (==EWOULDBLOCK), so we know not to continue writing (for non-forced output),
62 * as it'll just return EAGAIN again.
63 *
64 * Once we detect this, we watch for POLLOUT in the poller events. A
65 * POLLOUT indicates that the fd is no longer blocking, so we clear
66 * blocked mode and can continue writing.
67 */
68static void tty_set_blocked(struct tty_handler *th, bool blocked)
69{
70 int events;
71
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093072 if (blocked == th->blocked) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +080073 return;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093074 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +080075
76 th->blocked = blocked;
77 events = POLLIN;
78
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093079 if (th->blocked) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +080080 events |= POLLOUT;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093081 }
Jeremy Kerr6b1fed22017-02-07 21:40:38 +080082
83 console_poller_set_events(th->console, th->poller, events);
84}
85
Jeremy Kerrf733c852017-02-07 18:40:10 +080086static int tty_drain_queue(struct tty_handler *th, size_t force_len)
87{
88 size_t len, total_len;
89 ssize_t wlen;
90 uint8_t *buf;
Jeremy Kerrf733c852017-02-07 18:40:10 +080091
92 /* if we're forcing data, we need to clear non-blocking mode */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093093 if (force_len) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +080094 tty_set_fd_blocking(th, true);
95
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093096 /* no point writing, we'll just see -EAGAIN */
97 } else if (th->blocked) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +080098 return 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093099 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800100
101 total_len = 0;
102
103 for (;;) {
104 len = ringbuffer_dequeue_peek(th->rbc, total_len, &buf);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930105 if (!len) {
Jeremy Kerrf733c852017-02-07 18:40:10 +0800106 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930107 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800108
109 /* write as little as possible while blocking */
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930110 if (force_len && force_len < total_len + len) {
Jeremy Kerrf733c852017-02-07 18:40:10 +0800111 len = force_len - total_len;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930112 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800113
114 wlen = write(th->fd, buf, len);
115 if (wlen < 0) {
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930116 if (errno == EINTR) {
Jeremy Kerrf733c852017-02-07 18:40:10 +0800117 continue;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930118 }
Andrew Jefferya72711a2023-04-18 18:19:41 +0930119 if ((errno == EAGAIN || errno == EWOULDBLOCK) &&
120 !force_len) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800121 tty_set_blocked(th, true);
Jeremy Kerrf733c852017-02-07 18:40:10 +0800122 break;
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800123 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800124 warn("failed writing to local tty; disabling");
125 return -1;
126 }
127
128 total_len += wlen;
129
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930130 if (force_len && total_len >= force_len) {
Jeremy Kerrf733c852017-02-07 18:40:10 +0800131 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930132 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800133 }
134
135 ringbuffer_dequeue_commit(th->rbc, total_len);
136
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930137 if (force_len) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800138 tty_set_fd_blocking(th, false);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930139 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800140
141 return 0;
142}
143
144static enum ringbuffer_poll_ret tty_ringbuffer_poll(void *arg, size_t force_len)
145{
146 struct tty_handler *th = arg;
147 int rc;
148
149 rc = tty_drain_queue(th, force_len);
150 if (rc) {
151 console_poller_unregister(th->console, th->poller);
152 return RINGBUFFER_POLL_REMOVE;
153 }
154
155 return RINGBUFFER_POLL_OK;
156}
157
Andrew Jefferya72711a2023-04-18 18:19:41 +0930158static enum poller_ret tty_poll(struct handler *handler, int events,
159 void __attribute__((unused)) * data)
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800160{
161 struct tty_handler *th = to_tty_handler(handler);
162 uint8_t buf[4096];
163 ssize_t len;
Jeremy Kerrf733c852017-02-07 18:40:10 +0800164 int rc;
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800165
Jeremy Kerrf733c852017-02-07 18:40:10 +0800166 if (events & POLLIN) {
167 len = read(th->fd, buf, sizeof(buf));
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930168 if (len <= 0) {
Jeremy Kerr67eab042017-07-12 12:08:35 +0800169 goto err;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930170 }
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800171
Jeremy Kerrf733c852017-02-07 18:40:10 +0800172 console_data_out(th->console, buf, len);
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800173 }
174
Jeremy Kerrf733c852017-02-07 18:40:10 +0800175 if (events & POLLOUT) {
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800176 tty_set_blocked(th, false);
Jeremy Kerrf733c852017-02-07 18:40:10 +0800177 rc = tty_drain_queue(th, 0);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930178 if (rc) {
Jeremy Kerr67eab042017-07-12 12:08:35 +0800179 goto err;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930180 }
Jeremy Kerrf733c852017-02-07 18:40:10 +0800181 }
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800182
183 return POLLER_OK;
Jeremy Kerr67eab042017-07-12 12:08:35 +0800184
185err:
186 th->poller = NULL;
187 close(th->fd);
188 ringbuffer_consumer_unregister(th->rbc);
189 return POLLER_REMOVE;
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800190}
191
Xo Wangc5ef8ea2017-04-17 16:20:43 -0700192static int set_terminal_baud(struct tty_handler *th, const char *tty_name,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930193 speed_t speed)
194{
Xo Wangc5ef8ea2017-04-17 16:20:43 -0700195 struct termios term_options;
Xo Wangc5ef8ea2017-04-17 16:20:43 -0700196
197 if (tcgetattr(th->fd, &term_options) < 0) {
198 warn("Can't get config for %s", tty_name);
199 return -1;
200 }
201
202 if (cfsetspeed(&term_options, speed) < 0) {
203 warn("Couldn't set speeds for %s", tty_name);
204 return -1;
205 }
206
207 if (tcsetattr(th->fd, TCSAFLUSH, &term_options) < 0) {
208 warn("Couldn't commit terminal options for %s", tty_name);
209 return -1;
210 }
Xo Wangc5ef8ea2017-04-17 16:20:43 -0700211
212 return 0;
213}
214
Andrew Jefferya72711a2023-04-18 18:19:41 +0930215static int make_terminal_raw(struct tty_handler *th, const char *tty_name)
216{
Xo Wang81408bd2017-04-18 16:43:07 -0700217 struct termios term_options;
218
219 if (tcgetattr(th->fd, &term_options) < 0) {
220 warn("Can't get config for %s", tty_name);
221 return -1;
222 }
223
224 /* Disable various input and output processing including character
225 * translation, line edit (canonical) mode, flow control, and special signal
226 * generating characters. */
227 cfmakeraw(&term_options);
228
229 if (tcsetattr(th->fd, TCSAFLUSH, &term_options) < 0) {
230 warn("Couldn't commit terminal options for %s", tty_name);
231 return -1;
232 }
233 printf("Set %s for raw byte handling\n", tty_name);
234
235 return 0;
236}
237
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800238static int tty_init(struct handler *handler, struct console *console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930239 struct config *config __attribute__((unused)))
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800240{
241 struct tty_handler *th = to_tty_handler(handler);
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800242 speed_t desired_speed;
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800243 const char *tty_name;
Xo Wangc5ef8ea2017-04-17 16:20:43 -0700244 const char *tty_baud;
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800245 char *tty_path;
Jeremy Kerrbc506fd2017-02-07 09:24:48 +0800246 int rc;
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800247
248 tty_name = config_get_value(config, "local-tty");
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930249 if (!tty_name) {
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800250 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930251 }
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800252
253 rc = asprintf(&tty_path, "/dev/%s", tty_name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930254 if (!rc) {
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800255 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930256 }
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800257
Jeremy Kerrbc506fd2017-02-07 09:24:48 +0800258 th->fd = open(tty_path, O_RDWR | O_NONBLOCK);
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800259 if (th->fd < 0) {
260 warn("Can't open %s; disabling local tty", tty_name);
261 free(tty_path);
262 return -1;
263 }
264
265 free(tty_path);
Jeremy Kerr6b1fed22017-02-07 21:40:38 +0800266 th->fd_flags = fcntl(th->fd, F_GETFL, 0);
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800267
Xo Wangc5ef8ea2017-04-17 16:20:43 -0700268 tty_baud = config_get_value(config, "local-tty-baud");
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800269 if (tty_baud != NULL) {
270 rc = config_parse_baud(&desired_speed, tty_baud);
271 if (rc) {
272 fprintf(stderr, "%s is not a valid baud rate\n",
273 tty_baud);
274 } else {
275 rc = set_terminal_baud(th, tty_name, desired_speed);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930276 if (rc) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930277 fprintf(stderr,
278 "Couldn't set baud rate for %s to %s\n",
Xo Wangc5ef8ea2017-04-17 16:20:43 -0700279 tty_name, tty_baud);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930280 }
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800281 }
282 }
Xo Wangc5ef8ea2017-04-17 16:20:43 -0700283
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930284 if (make_terminal_raw(th, tty_name) != 0) {
Xo Wang81408bd2017-04-18 16:43:07 -0700285 fprintf(stderr, "Couldn't make %s a raw terminal\n", tty_name);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930286 }
Xo Wang81408bd2017-04-18 16:43:07 -0700287
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800288 th->poller = console_poller_register(console, handler, tty_poll, NULL,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930289 th->fd, POLLIN, NULL);
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800290 th->console = console;
Jeremy Kerrf733c852017-02-07 18:40:10 +0800291 th->rbc = console_ringbuffer_consumer_register(console,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930292 tty_ringbuffer_poll, th);
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800293
294 return 0;
295}
296
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800297static void tty_fini(struct handler *handler)
298{
299 struct tty_handler *th = to_tty_handler(handler);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930300 if (th->poller) {
Jeremy Kerr55c97122017-02-07 17:06:46 +0800301 console_poller_unregister(th->console, th->poller);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930302 }
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800303 close(th->fd);
304}
305
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800306static int tty_baudrate(struct handler *handler, speed_t baudrate)
307{
308 const char *tty_name = "local-tty";
309 struct tty_handler *th = to_tty_handler(handler);
310
311 if (baudrate == 0) {
312 return -1;
313 }
314
315 if (set_terminal_baud(th, tty_name, baudrate) != 0) {
316 fprintf(stderr, "Couldn't set baud rate for %s to %d\n",
317 tty_name, baudrate);
318 return -1;
319 }
320 return 0;
321}
322
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800323static struct tty_handler tty_handler = {
324 .handler = {
325 .name = "tty",
326 .init = tty_init,
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800327 .fini = tty_fini,
Cheng C Yangf9c8f6c2019-03-04 18:39:52 +0800328 .baudrate = tty_baudrate,
Jeremy Kerrbc1e8932016-04-28 12:27:30 +0800329 },
330};
331
Jeremy Kerr55c97122017-02-07 17:06:46 +0800332console_handler_register(&tty_handler.handler);