]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/ptyfwd.c
Merge pull request #6960 from keszybz/hwdb-update
[thirdparty/systemd.git] / src / shared / ptyfwd.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010-2013 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <limits.h>
22 #include <signal.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/epoll.h>
28 #include <sys/ioctl.h>
29 #include <sys/time.h>
30 #include <termios.h>
31 #include <unistd.h>
32
33 #include "sd-event.h"
34
35 #include "alloc-util.h"
36 #include "fd-util.h"
37 #include "log.h"
38 #include "macro.h"
39 #include "ptyfwd.h"
40 #include "time-util.h"
41
42 struct PTYForward {
43 sd_event *event;
44
45 int master;
46
47 PTYForwardFlags flags;
48
49 sd_event_source *stdin_event_source;
50 sd_event_source *stdout_event_source;
51 sd_event_source *master_event_source;
52
53 sd_event_source *sigwinch_event_source;
54
55 struct termios saved_stdin_attr;
56 struct termios saved_stdout_attr;
57
58 bool saved_stdin:1;
59 bool saved_stdout:1;
60
61 bool stdin_readable:1;
62 bool stdin_hangup:1;
63 bool stdout_writable:1;
64 bool stdout_hangup:1;
65 bool master_readable:1;
66 bool master_writable:1;
67 bool master_hangup:1;
68
69 bool read_from_master:1;
70
71 bool done:1;
72 bool drain:1;
73
74 bool last_char_set:1;
75 char last_char;
76
77 char in_buffer[LINE_MAX], out_buffer[LINE_MAX];
78 size_t in_buffer_full, out_buffer_full;
79
80 usec_t escape_timestamp;
81 unsigned escape_counter;
82
83 PTYForwardHandler handler;
84 void *userdata;
85 };
86
87 #define ESCAPE_USEC (1*USEC_PER_SEC)
88
89 static void pty_forward_disconnect(PTYForward *f) {
90
91 if (f) {
92 f->stdin_event_source = sd_event_source_unref(f->stdin_event_source);
93 f->stdout_event_source = sd_event_source_unref(f->stdout_event_source);
94
95 f->master_event_source = sd_event_source_unref(f->master_event_source);
96 f->sigwinch_event_source = sd_event_source_unref(f->sigwinch_event_source);
97 f->event = sd_event_unref(f->event);
98
99 if (f->saved_stdout)
100 tcsetattr(STDOUT_FILENO, TCSANOW, &f->saved_stdout_attr);
101 if (f->saved_stdin)
102 tcsetattr(STDIN_FILENO, TCSANOW, &f->saved_stdin_attr);
103
104 f->saved_stdout = f->saved_stdin = false;
105 }
106
107 /* STDIN/STDOUT should not be nonblocking normally, so let's unconditionally reset it */
108 fd_nonblock(STDIN_FILENO, false);
109 fd_nonblock(STDOUT_FILENO, false);
110 }
111
112 static int pty_forward_done(PTYForward *f, int rcode) {
113 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
114 assert(f);
115
116 if (f->done)
117 return 0;
118
119 e = sd_event_ref(f->event);
120
121 f->done = true;
122 pty_forward_disconnect(f);
123
124 if (f->handler)
125 return f->handler(f, rcode, f->userdata);
126 else
127 return sd_event_exit(e, rcode < 0 ? EXIT_FAILURE : rcode);
128 }
129
130 static bool look_for_escape(PTYForward *f, const char *buffer, size_t n) {
131 const char *p;
132
133 assert(f);
134 assert(buffer);
135 assert(n > 0);
136
137 for (p = buffer; p < buffer + n; p++) {
138
139 /* Check for ^] */
140 if (*p == 0x1D) {
141 usec_t nw = now(CLOCK_MONOTONIC);
142
143 if (f->escape_counter == 0 || nw > f->escape_timestamp + ESCAPE_USEC) {
144 f->escape_timestamp = nw;
145 f->escape_counter = 1;
146 } else {
147 (f->escape_counter)++;
148
149 if (f->escape_counter >= 3)
150 return true;
151 }
152 } else {
153 f->escape_timestamp = 0;
154 f->escape_counter = 0;
155 }
156 }
157
158 return false;
159 }
160
161 static bool ignore_vhangup(PTYForward *f) {
162 assert(f);
163
164 if (f->flags & PTY_FORWARD_IGNORE_VHANGUP)
165 return true;
166
167 if ((f->flags & PTY_FORWARD_IGNORE_INITIAL_VHANGUP) && !f->read_from_master)
168 return true;
169
170 return false;
171 }
172
173 static int shovel(PTYForward *f) {
174 ssize_t k;
175
176 assert(f);
177
178 while ((f->stdin_readable && f->in_buffer_full <= 0) ||
179 (f->master_writable && f->in_buffer_full > 0) ||
180 (f->master_readable && f->out_buffer_full <= 0) ||
181 (f->stdout_writable && f->out_buffer_full > 0)) {
182
183 if (f->stdin_readable && f->in_buffer_full < LINE_MAX) {
184
185 k = read(STDIN_FILENO, f->in_buffer + f->in_buffer_full, LINE_MAX - f->in_buffer_full);
186 if (k < 0) {
187
188 if (errno == EAGAIN)
189 f->stdin_readable = false;
190 else if (IN_SET(errno, EIO, EPIPE, ECONNRESET)) {
191 f->stdin_readable = false;
192 f->stdin_hangup = true;
193
194 f->stdin_event_source = sd_event_source_unref(f->stdin_event_source);
195 } else {
196 log_error_errno(errno, "read(): %m");
197 return pty_forward_done(f, -errno);
198 }
199 } else if (k == 0) {
200 /* EOF on stdin */
201 f->stdin_readable = false;
202 f->stdin_hangup = true;
203
204 f->stdin_event_source = sd_event_source_unref(f->stdin_event_source);
205 } else {
206 /* Check if ^] has been pressed three times within one second. If we get this we quite
207 * immediately. */
208 if (look_for_escape(f, f->in_buffer + f->in_buffer_full, k))
209 return pty_forward_done(f, -ECANCELED);
210
211 f->in_buffer_full += (size_t) k;
212 }
213 }
214
215 if (f->master_writable && f->in_buffer_full > 0) {
216
217 k = write(f->master, f->in_buffer, f->in_buffer_full);
218 if (k < 0) {
219
220 if (IN_SET(errno, EAGAIN, EIO))
221 f->master_writable = false;
222 else if (IN_SET(errno, EPIPE, ECONNRESET)) {
223 f->master_writable = f->master_readable = false;
224 f->master_hangup = true;
225
226 f->master_event_source = sd_event_source_unref(f->master_event_source);
227 } else {
228 log_error_errno(errno, "write(): %m");
229 return pty_forward_done(f, -errno);
230 }
231 } else {
232 assert(f->in_buffer_full >= (size_t) k);
233 memmove(f->in_buffer, f->in_buffer + k, f->in_buffer_full - k);
234 f->in_buffer_full -= k;
235 }
236 }
237
238 if (f->master_readable && f->out_buffer_full < LINE_MAX) {
239
240 k = read(f->master, f->out_buffer + f->out_buffer_full, LINE_MAX - f->out_buffer_full);
241 if (k < 0) {
242
243 /* Note that EIO on the master device
244 * might be caused by vhangup() or
245 * temporary closing of everything on
246 * the other side, we treat it like
247 * EAGAIN here and try again, unless
248 * ignore_vhangup is off. */
249
250 if (errno == EAGAIN || (errno == EIO && ignore_vhangup(f)))
251 f->master_readable = false;
252 else if (IN_SET(errno, EPIPE, ECONNRESET, EIO)) {
253 f->master_readable = f->master_writable = false;
254 f->master_hangup = true;
255
256 f->master_event_source = sd_event_source_unref(f->master_event_source);
257 } else {
258 log_error_errno(errno, "read(): %m");
259 return pty_forward_done(f, -errno);
260 }
261 } else {
262 f->read_from_master = true;
263 f->out_buffer_full += (size_t) k;
264 }
265 }
266
267 if (f->stdout_writable && f->out_buffer_full > 0) {
268
269 k = write(STDOUT_FILENO, f->out_buffer, f->out_buffer_full);
270 if (k < 0) {
271
272 if (errno == EAGAIN)
273 f->stdout_writable = false;
274 else if (IN_SET(errno, EIO, EPIPE, ECONNRESET)) {
275 f->stdout_writable = false;
276 f->stdout_hangup = true;
277 f->stdout_event_source = sd_event_source_unref(f->stdout_event_source);
278 } else {
279 log_error_errno(errno, "write(): %m");
280 return pty_forward_done(f, -errno);
281 }
282
283 } else {
284
285 if (k > 0) {
286 f->last_char = f->out_buffer[k-1];
287 f->last_char_set = true;
288 }
289
290 assert(f->out_buffer_full >= (size_t) k);
291 memmove(f->out_buffer, f->out_buffer + k, f->out_buffer_full - k);
292 f->out_buffer_full -= k;
293 }
294 }
295 }
296
297 if (f->stdin_hangup || f->stdout_hangup || f->master_hangup) {
298 /* Exit the loop if any side hung up and if there's
299 * nothing more to write or nothing we could write. */
300
301 if ((f->out_buffer_full <= 0 || f->stdout_hangup) &&
302 (f->in_buffer_full <= 0 || f->master_hangup))
303 return pty_forward_done(f, 0);
304 }
305
306 /* If we were asked to drain, and there's nothing more to handle from the master, then call the callback
307 * too. */
308 if (f->drain && f->out_buffer_full == 0 && !f->master_readable)
309 return pty_forward_done(f, 0);
310
311 return 0;
312 }
313
314 static int on_master_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
315 PTYForward *f = userdata;
316
317 assert(f);
318 assert(e);
319 assert(e == f->master_event_source);
320 assert(fd >= 0);
321 assert(fd == f->master);
322
323 if (revents & (EPOLLIN|EPOLLHUP))
324 f->master_readable = true;
325
326 if (revents & (EPOLLOUT|EPOLLHUP))
327 f->master_writable = true;
328
329 return shovel(f);
330 }
331
332 static int on_stdin_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
333 PTYForward *f = userdata;
334
335 assert(f);
336 assert(e);
337 assert(e == f->stdin_event_source);
338 assert(fd >= 0);
339 assert(fd == STDIN_FILENO);
340
341 if (revents & (EPOLLIN|EPOLLHUP))
342 f->stdin_readable = true;
343
344 return shovel(f);
345 }
346
347 static int on_stdout_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
348 PTYForward *f = userdata;
349
350 assert(f);
351 assert(e);
352 assert(e == f->stdout_event_source);
353 assert(fd >= 0);
354 assert(fd == STDOUT_FILENO);
355
356 if (revents & (EPOLLOUT|EPOLLHUP))
357 f->stdout_writable = true;
358
359 return shovel(f);
360 }
361
362 static int on_sigwinch_event(sd_event_source *e, const struct signalfd_siginfo *si, void *userdata) {
363 PTYForward *f = userdata;
364 struct winsize ws;
365
366 assert(f);
367 assert(e);
368 assert(e == f->sigwinch_event_source);
369
370 /* The window size changed, let's forward that. */
371 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0)
372 (void) ioctl(f->master, TIOCSWINSZ, &ws);
373
374 return 0;
375 }
376
377 int pty_forward_new(
378 sd_event *event,
379 int master,
380 PTYForwardFlags flags,
381 PTYForward **ret) {
382
383 _cleanup_(pty_forward_freep) PTYForward *f = NULL;
384 struct winsize ws;
385 int r;
386
387 f = new0(PTYForward, 1);
388 if (!f)
389 return -ENOMEM;
390
391 f->flags = flags;
392
393 if (event)
394 f->event = sd_event_ref(event);
395 else {
396 r = sd_event_default(&f->event);
397 if (r < 0)
398 return r;
399 }
400
401 if (!(flags & PTY_FORWARD_READ_ONLY)) {
402 r = fd_nonblock(STDIN_FILENO, true);
403 if (r < 0)
404 return r;
405
406 r = fd_nonblock(STDOUT_FILENO, true);
407 if (r < 0)
408 return r;
409 }
410
411 r = fd_nonblock(master, true);
412 if (r < 0)
413 return r;
414
415 f->master = master;
416
417 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0)
418 (void) ioctl(master, TIOCSWINSZ, &ws);
419
420 if (!(flags & PTY_FORWARD_READ_ONLY)) {
421 if (tcgetattr(STDIN_FILENO, &f->saved_stdin_attr) >= 0) {
422 struct termios raw_stdin_attr;
423
424 f->saved_stdin = true;
425
426 raw_stdin_attr = f->saved_stdin_attr;
427 cfmakeraw(&raw_stdin_attr);
428 raw_stdin_attr.c_oflag = f->saved_stdin_attr.c_oflag;
429 tcsetattr(STDIN_FILENO, TCSANOW, &raw_stdin_attr);
430 }
431
432 if (tcgetattr(STDOUT_FILENO, &f->saved_stdout_attr) >= 0) {
433 struct termios raw_stdout_attr;
434
435 f->saved_stdout = true;
436
437 raw_stdout_attr = f->saved_stdout_attr;
438 cfmakeraw(&raw_stdout_attr);
439 raw_stdout_attr.c_iflag = f->saved_stdout_attr.c_iflag;
440 raw_stdout_attr.c_lflag = f->saved_stdout_attr.c_lflag;
441 tcsetattr(STDOUT_FILENO, TCSANOW, &raw_stdout_attr);
442 }
443
444 r = sd_event_add_io(f->event, &f->stdin_event_source, STDIN_FILENO, EPOLLIN|EPOLLET, on_stdin_event, f);
445 if (r < 0 && r != -EPERM)
446 return r;
447
448 if (r >= 0)
449 (void) sd_event_source_set_description(f->stdin_event_source, "ptyfwd-stdin");
450 }
451
452 r = sd_event_add_io(f->event, &f->stdout_event_source, STDOUT_FILENO, EPOLLOUT|EPOLLET, on_stdout_event, f);
453 if (r == -EPERM)
454 /* stdout without epoll support. Likely redirected to regular file. */
455 f->stdout_writable = true;
456 else if (r < 0)
457 return r;
458 else
459 (void) sd_event_source_set_description(f->stdout_event_source, "ptyfwd-stdout");
460
461 r = sd_event_add_io(f->event, &f->master_event_source, master, EPOLLIN|EPOLLOUT|EPOLLET, on_master_event, f);
462 if (r < 0)
463 return r;
464
465 (void) sd_event_source_set_description(f->master_event_source, "ptyfwd-master");
466
467 r = sd_event_add_signal(f->event, &f->sigwinch_event_source, SIGWINCH, on_sigwinch_event, f);
468 if (r < 0)
469 return r;
470
471 (void) sd_event_source_set_description(f->sigwinch_event_source, "ptyfwd-sigwinch");
472
473 *ret = f;
474 f = NULL;
475
476 return 0;
477 }
478
479 PTYForward *pty_forward_free(PTYForward *f) {
480 pty_forward_disconnect(f);
481 return mfree(f);
482 }
483
484 int pty_forward_get_last_char(PTYForward *f, char *ch) {
485 assert(f);
486 assert(ch);
487
488 if (!f->last_char_set)
489 return -ENXIO;
490
491 *ch = f->last_char;
492 return 0;
493 }
494
495 int pty_forward_set_ignore_vhangup(PTYForward *f, bool b) {
496 int r;
497
498 assert(f);
499
500 if (!!(f->flags & PTY_FORWARD_IGNORE_VHANGUP) == b)
501 return 0;
502
503 SET_FLAG(f->flags, PTY_FORWARD_IGNORE_VHANGUP, b);
504
505 if (!ignore_vhangup(f)) {
506
507 /* We shall now react to vhangup()s? Let's check
508 * immediately if we might be in one */
509
510 f->master_readable = true;
511 r = shovel(f);
512 if (r < 0)
513 return r;
514 }
515
516 return 0;
517 }
518
519 bool pty_forward_get_ignore_vhangup(PTYForward *f) {
520 assert(f);
521
522 return !!(f->flags & PTY_FORWARD_IGNORE_VHANGUP);
523 }
524
525 bool pty_forward_is_done(PTYForward *f) {
526 assert(f);
527
528 return f->done;
529 }
530
531 void pty_forward_set_handler(PTYForward *f, PTYForwardHandler cb, void *userdata) {
532 assert(f);
533
534 f->handler = cb;
535 f->userdata = userdata;
536 }
537
538 bool pty_forward_drain(PTYForward *f) {
539 assert(f);
540
541 /* Starts draining the forwarder. Specifically:
542 *
543 * - Returns true if there are no unprocessed bytes from the pty, false otherwise
544 *
545 * - Makes sure the handler function is called the next time the number of unprocessed bytes hits zero
546 */
547
548 f->drain = true;
549
550 return f->out_buffer_full == 0 && !f->master_readable;
551 }