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