]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/ptyfwd.c
Merge pull request #2794 from jhol/dont-unmount-initramfs-mounts
[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 last_char_set:1;
72 char last_char;
73
74 char in_buffer[LINE_MAX], out_buffer[LINE_MAX];
75 size_t in_buffer_full, out_buffer_full;
76
77 usec_t escape_timestamp;
78 unsigned escape_counter;
79 };
80
81 #define ESCAPE_USEC (1*USEC_PER_SEC)
82
83 static bool look_for_escape(PTYForward *f, const char *buffer, size_t n) {
84 const char *p;
85
86 assert(f);
87 assert(buffer);
88 assert(n > 0);
89
90 for (p = buffer; p < buffer + n; p++) {
91
92 /* Check for ^] */
93 if (*p == 0x1D) {
94 usec_t nw = now(CLOCK_MONOTONIC);
95
96 if (f->escape_counter == 0 || nw > f->escape_timestamp + ESCAPE_USEC) {
97 f->escape_timestamp = nw;
98 f->escape_counter = 1;
99 } else {
100 (f->escape_counter)++;
101
102 if (f->escape_counter >= 3)
103 return true;
104 }
105 } else {
106 f->escape_timestamp = 0;
107 f->escape_counter = 0;
108 }
109 }
110
111 return false;
112 }
113
114 static bool ignore_vhangup(PTYForward *f) {
115 assert(f);
116
117 if (f->flags & PTY_FORWARD_IGNORE_VHANGUP)
118 return true;
119
120 if ((f->flags & PTY_FORWARD_IGNORE_INITIAL_VHANGUP) && !f->read_from_master)
121 return true;
122
123 return false;
124 }
125
126 static int shovel(PTYForward *f) {
127 ssize_t k;
128
129 assert(f);
130
131 while ((f->stdin_readable && f->in_buffer_full <= 0) ||
132 (f->master_writable && f->in_buffer_full > 0) ||
133 (f->master_readable && f->out_buffer_full <= 0) ||
134 (f->stdout_writable && f->out_buffer_full > 0)) {
135
136 if (f->stdin_readable && f->in_buffer_full < LINE_MAX) {
137
138 k = read(STDIN_FILENO, f->in_buffer + f->in_buffer_full, LINE_MAX - f->in_buffer_full);
139 if (k < 0) {
140
141 if (errno == EAGAIN)
142 f->stdin_readable = false;
143 else if (errno == EIO || errno == EPIPE || errno == ECONNRESET) {
144 f->stdin_readable = false;
145 f->stdin_hangup = true;
146
147 f->stdin_event_source = sd_event_source_unref(f->stdin_event_source);
148 } else {
149 log_error_errno(errno, "read(): %m");
150 return sd_event_exit(f->event, EXIT_FAILURE);
151 }
152 } else if (k == 0) {
153 /* EOF on stdin */
154 f->stdin_readable = false;
155 f->stdin_hangup = true;
156
157 f->stdin_event_source = sd_event_source_unref(f->stdin_event_source);
158 } else {
159 /* Check if ^] has been
160 * pressed three times within
161 * one second. If we get this
162 * we quite immediately. */
163 if (look_for_escape(f, f->in_buffer + f->in_buffer_full, k))
164 return sd_event_exit(f->event, EXIT_FAILURE);
165
166 f->in_buffer_full += (size_t) k;
167 }
168 }
169
170 if (f->master_writable && f->in_buffer_full > 0) {
171
172 k = write(f->master, f->in_buffer, f->in_buffer_full);
173 if (k < 0) {
174
175 if (errno == EAGAIN || errno == EIO)
176 f->master_writable = false;
177 else if (errno == EPIPE || errno == ECONNRESET) {
178 f->master_writable = f->master_readable = false;
179 f->master_hangup = true;
180
181 f->master_event_source = sd_event_source_unref(f->master_event_source);
182 } else {
183 log_error_errno(errno, "write(): %m");
184 return sd_event_exit(f->event, EXIT_FAILURE);
185 }
186 } else {
187 assert(f->in_buffer_full >= (size_t) k);
188 memmove(f->in_buffer, f->in_buffer + k, f->in_buffer_full - k);
189 f->in_buffer_full -= k;
190 }
191 }
192
193 if (f->master_readable && f->out_buffer_full < LINE_MAX) {
194
195 k = read(f->master, f->out_buffer + f->out_buffer_full, LINE_MAX - f->out_buffer_full);
196 if (k < 0) {
197
198 /* Note that EIO on the master device
199 * might be caused by vhangup() or
200 * temporary closing of everything on
201 * the other side, we treat it like
202 * EAGAIN here and try again, unless
203 * ignore_vhangup is off. */
204
205 if (errno == EAGAIN || (errno == EIO && ignore_vhangup(f)))
206 f->master_readable = false;
207 else if (errno == EPIPE || errno == ECONNRESET || errno == EIO) {
208 f->master_readable = f->master_writable = false;
209 f->master_hangup = true;
210
211 f->master_event_source = sd_event_source_unref(f->master_event_source);
212 } else {
213 log_error_errno(errno, "read(): %m");
214 return sd_event_exit(f->event, EXIT_FAILURE);
215 }
216 } else {
217 f->read_from_master = true;
218 f->out_buffer_full += (size_t) k;
219 }
220 }
221
222 if (f->stdout_writable && f->out_buffer_full > 0) {
223
224 k = write(STDOUT_FILENO, f->out_buffer, f->out_buffer_full);
225 if (k < 0) {
226
227 if (errno == EAGAIN)
228 f->stdout_writable = false;
229 else if (errno == EIO || errno == EPIPE || errno == ECONNRESET) {
230 f->stdout_writable = false;
231 f->stdout_hangup = true;
232 f->stdout_event_source = sd_event_source_unref(f->stdout_event_source);
233 } else {
234 log_error_errno(errno, "write(): %m");
235 return sd_event_exit(f->event, EXIT_FAILURE);
236 }
237
238 } else {
239
240 if (k > 0) {
241 f->last_char = f->out_buffer[k-1];
242 f->last_char_set = true;
243 }
244
245 assert(f->out_buffer_full >= (size_t) k);
246 memmove(f->out_buffer, f->out_buffer + k, f->out_buffer_full - k);
247 f->out_buffer_full -= k;
248 }
249 }
250 }
251
252 if (f->stdin_hangup || f->stdout_hangup || f->master_hangup) {
253 /* Exit the loop if any side hung up and if there's
254 * nothing more to write or nothing we could write. */
255
256 if ((f->out_buffer_full <= 0 || f->stdout_hangup) &&
257 (f->in_buffer_full <= 0 || f->master_hangup))
258 return sd_event_exit(f->event, EXIT_SUCCESS);
259 }
260
261 return 0;
262 }
263
264 static int on_master_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
265 PTYForward *f = userdata;
266
267 assert(f);
268 assert(e);
269 assert(e == f->master_event_source);
270 assert(fd >= 0);
271 assert(fd == f->master);
272
273 if (revents & (EPOLLIN|EPOLLHUP))
274 f->master_readable = true;
275
276 if (revents & (EPOLLOUT|EPOLLHUP))
277 f->master_writable = true;
278
279 return shovel(f);
280 }
281
282 static int on_stdin_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
283 PTYForward *f = userdata;
284
285 assert(f);
286 assert(e);
287 assert(e == f->stdin_event_source);
288 assert(fd >= 0);
289 assert(fd == STDIN_FILENO);
290
291 if (revents & (EPOLLIN|EPOLLHUP))
292 f->stdin_readable = true;
293
294 return shovel(f);
295 }
296
297 static int on_stdout_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
298 PTYForward *f = userdata;
299
300 assert(f);
301 assert(e);
302 assert(e == f->stdout_event_source);
303 assert(fd >= 0);
304 assert(fd == STDOUT_FILENO);
305
306 if (revents & (EPOLLOUT|EPOLLHUP))
307 f->stdout_writable = true;
308
309 return shovel(f);
310 }
311
312 static int on_sigwinch_event(sd_event_source *e, const struct signalfd_siginfo *si, void *userdata) {
313 PTYForward *f = userdata;
314 struct winsize ws;
315
316 assert(f);
317 assert(e);
318 assert(e == f->sigwinch_event_source);
319
320 /* The window size changed, let's forward that. */
321 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0)
322 (void) ioctl(f->master, TIOCSWINSZ, &ws);
323
324 return 0;
325 }
326
327 int pty_forward_new(
328 sd_event *event,
329 int master,
330 PTYForwardFlags flags,
331 PTYForward **ret) {
332
333 _cleanup_(pty_forward_freep) PTYForward *f = NULL;
334 struct winsize ws;
335 int r;
336
337 f = new0(PTYForward, 1);
338 if (!f)
339 return -ENOMEM;
340
341 f->flags = flags;
342
343 if (event)
344 f->event = sd_event_ref(event);
345 else {
346 r = sd_event_default(&f->event);
347 if (r < 0)
348 return r;
349 }
350
351 if (!(flags & PTY_FORWARD_READ_ONLY)) {
352 r = fd_nonblock(STDIN_FILENO, true);
353 if (r < 0)
354 return r;
355
356 r = fd_nonblock(STDOUT_FILENO, true);
357 if (r < 0)
358 return r;
359 }
360
361 r = fd_nonblock(master, true);
362 if (r < 0)
363 return r;
364
365 f->master = master;
366
367 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) >= 0)
368 (void) ioctl(master, TIOCSWINSZ, &ws);
369
370 if (!(flags & PTY_FORWARD_READ_ONLY)) {
371 if (tcgetattr(STDIN_FILENO, &f->saved_stdin_attr) >= 0) {
372 struct termios raw_stdin_attr;
373
374 f->saved_stdin = true;
375
376 raw_stdin_attr = f->saved_stdin_attr;
377 cfmakeraw(&raw_stdin_attr);
378 raw_stdin_attr.c_oflag = f->saved_stdin_attr.c_oflag;
379 tcsetattr(STDIN_FILENO, TCSANOW, &raw_stdin_attr);
380 }
381
382 if (tcgetattr(STDOUT_FILENO, &f->saved_stdout_attr) >= 0) {
383 struct termios raw_stdout_attr;
384
385 f->saved_stdout = true;
386
387 raw_stdout_attr = f->saved_stdout_attr;
388 cfmakeraw(&raw_stdout_attr);
389 raw_stdout_attr.c_iflag = f->saved_stdout_attr.c_iflag;
390 raw_stdout_attr.c_lflag = f->saved_stdout_attr.c_lflag;
391 tcsetattr(STDOUT_FILENO, TCSANOW, &raw_stdout_attr);
392 }
393
394 r = sd_event_add_io(f->event, &f->stdin_event_source, STDIN_FILENO, EPOLLIN|EPOLLET, on_stdin_event, f);
395 if (r < 0 && r != -EPERM)
396 return r;
397 }
398
399 r = sd_event_add_io(f->event, &f->stdout_event_source, STDOUT_FILENO, EPOLLOUT|EPOLLET, on_stdout_event, f);
400 if (r == -EPERM)
401 /* stdout without epoll support. Likely redirected to regular file. */
402 f->stdout_writable = true;
403 else if (r < 0)
404 return r;
405
406 r = sd_event_add_io(f->event, &f->master_event_source, master, EPOLLIN|EPOLLOUT|EPOLLET, on_master_event, f);
407 if (r < 0)
408 return r;
409
410 r = sd_event_add_signal(f->event, &f->sigwinch_event_source, SIGWINCH, on_sigwinch_event, f);
411 if (r < 0)
412 return r;
413
414 *ret = f;
415 f = NULL;
416
417 return 0;
418 }
419
420 PTYForward *pty_forward_free(PTYForward *f) {
421
422 if (f) {
423 sd_event_source_unref(f->stdin_event_source);
424 sd_event_source_unref(f->stdout_event_source);
425 sd_event_source_unref(f->master_event_source);
426 sd_event_source_unref(f->sigwinch_event_source);
427 sd_event_unref(f->event);
428
429 if (f->saved_stdout)
430 tcsetattr(STDOUT_FILENO, TCSANOW, &f->saved_stdout_attr);
431 if (f->saved_stdin)
432 tcsetattr(STDIN_FILENO, TCSANOW, &f->saved_stdin_attr);
433
434 free(f);
435 }
436
437 /* STDIN/STDOUT should not be nonblocking normally, so let's
438 * unconditionally reset it */
439 fd_nonblock(STDIN_FILENO, false);
440 fd_nonblock(STDOUT_FILENO, false);
441
442 return NULL;
443 }
444
445 int pty_forward_get_last_char(PTYForward *f, char *ch) {
446 assert(f);
447 assert(ch);
448
449 if (!f->last_char_set)
450 return -ENXIO;
451
452 *ch = f->last_char;
453 return 0;
454 }
455
456 int pty_forward_set_ignore_vhangup(PTYForward *f, bool b) {
457 int r;
458
459 assert(f);
460
461 if (!!(f->flags & PTY_FORWARD_IGNORE_VHANGUP) == b)
462 return 0;
463
464 SET_FLAG(f->flags, PTY_FORWARD_IGNORE_VHANGUP, b);
465
466 if (!ignore_vhangup(f)) {
467
468 /* We shall now react to vhangup()s? Let's check
469 * immediately if we might be in one */
470
471 f->master_readable = true;
472 r = shovel(f);
473 if (r < 0)
474 return r;
475 }
476
477 return 0;
478 }
479
480 int pty_forward_get_ignore_vhangup(PTYForward *f) {
481 assert(f);
482
483 return !!(f->flags & PTY_FORWARD_IGNORE_VHANGUP);
484 }