]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/ptyfwd.c
tree-wide: use ASSERT_PTR more
[thirdparty/systemd.git] / src / shared / ptyfwd.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
4ba93280 2
a8fbdf54 3#include <errno.h>
781fa474 4#include <fcntl.h>
cf0fbc49 5#include <limits.h>
a8fbdf54
TA
6#include <signal.h>
7#include <stddef.h>
8#include <stdint.h>
9#include <stdlib.h>
10#include <string.h>
4ba93280 11#include <sys/epoll.h>
4ba93280 12#include <sys/ioctl.h>
a8fbdf54 13#include <sys/time.h>
4ba93280 14#include <termios.h>
a8fbdf54
TA
15#include <unistd.h>
16
17#include "sd-event.h"
4ba93280 18
b5efdb8a 19#include "alloc-util.h"
fce93d7a 20#include "errno-util.h"
3ffd4af2 21#include "fd-util.h"
a8fbdf54
TA
22#include "log.h"
23#include "macro.h"
4ba93280 24#include "ptyfwd.h"
da22bdbc 25#include "terminal-util.h"
a8fbdf54 26#include "time-util.h"
4ba93280 27
023fb90b
LP
28struct PTYForward {
29 sd_event *event;
04d39279 30
781fa474
LP
31 int input_fd;
32 int output_fd;
023fb90b
LP
33 int master;
34
ae3dde80
LP
35 PTYForwardFlags flags;
36
023fb90b
LP
37 sd_event_source *stdin_event_source;
38 sd_event_source *stdout_event_source;
39 sd_event_source *master_event_source;
40
41 sd_event_source *sigwinch_event_source;
42
43 struct termios saved_stdin_attr;
44 struct termios saved_stdout_attr;
45
781fa474
LP
46 bool close_input_fd:1;
47 bool close_output_fd:1;
48
023fb90b
LP
49 bool saved_stdin:1;
50 bool saved_stdout:1;
51
52 bool stdin_readable:1;
53 bool stdin_hangup:1;
54 bool stdout_writable:1;
55 bool stdout_hangup:1;
56 bool master_readable:1;
57 bool master_writable:1;
58 bool master_hangup:1;
59
ae3dde80 60 bool read_from_master:1;
9b15b784 61
2a453c2e 62 bool done:1;
95f1d6bf 63 bool drain:1;
2a453c2e 64
9b15b784
LP
65 bool last_char_set:1;
66 char last_char;
67
023fb90b
LP
68 char in_buffer[LINE_MAX], out_buffer[LINE_MAX];
69 size_t in_buffer_full, out_buffer_full;
70
71 usec_t escape_timestamp;
72 unsigned escape_counter;
2a453c2e
LP
73
74 PTYForwardHandler handler;
75 void *userdata;
023fb90b
LP
76};
77
78#define ESCAPE_USEC (1*USEC_PER_SEC)
79
2a453c2e
LP
80static void pty_forward_disconnect(PTYForward *f) {
81
781fa474
LP
82 if (!f)
83 return;
84
85 f->stdin_event_source = sd_event_source_unref(f->stdin_event_source);
86 f->stdout_event_source = sd_event_source_unref(f->stdout_event_source);
2a453c2e 87
781fa474
LP
88 f->master_event_source = sd_event_source_unref(f->master_event_source);
89 f->sigwinch_event_source = sd_event_source_unref(f->sigwinch_event_source);
90 f->event = sd_event_unref(f->event);
2a453c2e 91
781fa474 92 if (f->output_fd >= 0) {
2a453c2e 93 if (f->saved_stdout)
781fa474
LP
94 (void) tcsetattr(f->output_fd, TCSANOW, &f->saved_stdout_attr);
95
96 /* STDIN/STDOUT should not be non-blocking normally, so let's reset it */
97 (void) fd_nonblock(f->output_fd, false);
98 if (f->close_output_fd)
99 f->output_fd = safe_close(f->output_fd);
100 }
101
102 if (f->input_fd >= 0) {
2a453c2e 103 if (f->saved_stdin)
781fa474 104 (void) tcsetattr(f->input_fd, TCSANOW, &f->saved_stdin_attr);
2a453c2e 105
781fa474
LP
106 (void) fd_nonblock(f->input_fd, false);
107 if (f->close_input_fd)
108 f->input_fd = safe_close(f->input_fd);
2a453c2e
LP
109 }
110
781fa474 111 f->saved_stdout = f->saved_stdin = false;
2a453c2e
LP
112}
113
114static int pty_forward_done(PTYForward *f, int rcode) {
115 _cleanup_(sd_event_unrefp) sd_event *e = NULL;
116 assert(f);
117
118 if (f->done)
119 return 0;
120
121 e = sd_event_ref(f->event);
122
123 f->done = true;
124 pty_forward_disconnect(f);
125
126 if (f->handler)
127 return f->handler(f, rcode, f->userdata);
128 else
129 return sd_event_exit(e, rcode < 0 ? EXIT_FAILURE : rcode);
130}
131
023fb90b 132static bool look_for_escape(PTYForward *f, const char *buffer, size_t n) {
04d39279
LP
133 const char *p;
134
023fb90b 135 assert(f);
04d39279
LP
136 assert(buffer);
137 assert(n > 0);
138
139 for (p = buffer; p < buffer + n; p++) {
140
141 /* Check for ^] */
142 if (*p == 0x1D) {
143 usec_t nw = now(CLOCK_MONOTONIC);
144
023fb90b
LP
145 if (f->escape_counter == 0 || nw > f->escape_timestamp + ESCAPE_USEC) {
146 f->escape_timestamp = nw;
147 f->escape_counter = 1;
04d39279 148 } else {
023fb90b 149 (f->escape_counter)++;
04d39279 150
023fb90b 151 if (f->escape_counter >= 3)
04d39279
LP
152 return true;
153 }
154 } else {
023fb90b
LP
155 f->escape_timestamp = 0;
156 f->escape_counter = 0;
04d39279
LP
157 }
158 }
159
160 return false;
161}
162
ae3dde80
LP
163static bool ignore_vhangup(PTYForward *f) {
164 assert(f);
165
166 if (f->flags & PTY_FORWARD_IGNORE_VHANGUP)
167 return true;
168
169 if ((f->flags & PTY_FORWARD_IGNORE_INITIAL_VHANGUP) && !f->read_from_master)
170 return true;
171
172 return false;
173}
174
e22e69a3
LP
175static bool drained(PTYForward *f) {
176 int q = 0;
177
178 assert(f);
179
180 if (f->out_buffer_full > 0)
181 return false;
182
183 if (f->master_readable)
184 return false;
185
186 if (ioctl(f->master, TIOCINQ, &q) < 0)
187 log_debug_errno(errno, "TIOCINQ failed on master: %m");
188 else if (q > 0)
189 return false;
190
191 if (ioctl(f->master, TIOCOUTQ, &q) < 0)
192 log_debug_errno(errno, "TIOCOUTQ failed on master: %m");
193 else if (q > 0)
194 return false;
195
196 return true;
197}
198
023fb90b
LP
199static int shovel(PTYForward *f) {
200 ssize_t k;
4ba93280 201
023fb90b 202 assert(f);
4ba93280 203
023fb90b
LP
204 while ((f->stdin_readable && f->in_buffer_full <= 0) ||
205 (f->master_writable && f->in_buffer_full > 0) ||
206 (f->master_readable && f->out_buffer_full <= 0) ||
207 (f->stdout_writable && f->out_buffer_full > 0)) {
4ba93280 208
023fb90b 209 if (f->stdin_readable && f->in_buffer_full < LINE_MAX) {
4ba93280 210
781fa474 211 k = read(f->input_fd, f->in_buffer + f->in_buffer_full, LINE_MAX - f->in_buffer_full);
023fb90b 212 if (k < 0) {
4ba93280 213
023fb90b
LP
214 if (errno == EAGAIN)
215 f->stdin_readable = false;
fce93d7a 216 else if (errno == EIO || ERRNO_IS_DISCONNECT(errno)) {
023fb90b
LP
217 f->stdin_readable = false;
218 f->stdin_hangup = true;
4ba93280 219
023fb90b
LP
220 f->stdin_event_source = sd_event_source_unref(f->stdin_event_source);
221 } else {
56f64d95 222 log_error_errno(errno, "read(): %m");
2a453c2e 223 return pty_forward_done(f, -errno);
023fb90b
LP
224 }
225 } else if (k == 0) {
226 /* EOF on stdin */
227 f->stdin_readable = false;
228 f->stdin_hangup = true;
229
230 f->stdin_event_source = sd_event_source_unref(f->stdin_event_source);
231 } else {
2a453c2e
LP
232 /* Check if ^] has been pressed three times within one second. If we get this we quite
233 * immediately. */
023fb90b 234 if (look_for_escape(f, f->in_buffer + f->in_buffer_full, k))
2a453c2e 235 return pty_forward_done(f, -ECANCELED);
023fb90b
LP
236
237 f->in_buffer_full += (size_t) k;
238 }
4ba93280
LP
239 }
240
023fb90b 241 if (f->master_writable && f->in_buffer_full > 0) {
4ba93280 242
023fb90b
LP
243 k = write(f->master, f->in_buffer, f->in_buffer_full);
244 if (k < 0) {
4ba93280 245
3742095b 246 if (IN_SET(errno, EAGAIN, EIO))
023fb90b 247 f->master_writable = false;
3742095b 248 else if (IN_SET(errno, EPIPE, ECONNRESET)) {
023fb90b
LP
249 f->master_writable = f->master_readable = false;
250 f->master_hangup = true;
4ba93280 251
023fb90b
LP
252 f->master_event_source = sd_event_source_unref(f->master_event_source);
253 } else {
56f64d95 254 log_error_errno(errno, "write(): %m");
2a453c2e 255 return pty_forward_done(f, -errno);
023fb90b
LP
256 }
257 } else {
258 assert(f->in_buffer_full >= (size_t) k);
259 memmove(f->in_buffer, f->in_buffer + k, f->in_buffer_full - k);
260 f->in_buffer_full -= k;
261 }
4ba93280
LP
262 }
263
023fb90b 264 if (f->master_readable && f->out_buffer_full < LINE_MAX) {
4ba93280 265
023fb90b
LP
266 k = read(f->master, f->out_buffer + f->out_buffer_full, LINE_MAX - f->out_buffer_full);
267 if (k < 0) {
4ba93280 268
da054c37
LP
269 /* Note that EIO on the master device
270 * might be caused by vhangup() or
271 * temporary closing of everything on
272 * the other side, we treat it like
273 * EAGAIN here and try again, unless
274 * ignore_vhangup is off. */
4ba93280 275
ae3dde80 276 if (errno == EAGAIN || (errno == EIO && ignore_vhangup(f)))
023fb90b 277 f->master_readable = false;
3742095b 278 else if (IN_SET(errno, EPIPE, ECONNRESET, EIO)) {
023fb90b
LP
279 f->master_readable = f->master_writable = false;
280 f->master_hangup = true;
4ba93280 281
023fb90b 282 f->master_event_source = sd_event_source_unref(f->master_event_source);
04d39279 283 } else {
56f64d95 284 log_error_errno(errno, "read(): %m");
2a453c2e 285 return pty_forward_done(f, -errno);
04d39279 286 }
ae3dde80
LP
287 } else {
288 f->read_from_master = true;
023fb90b 289 f->out_buffer_full += (size_t) k;
ae3dde80 290 }
023fb90b 291 }
4ba93280 292
023fb90b 293 if (f->stdout_writable && f->out_buffer_full > 0) {
4ba93280 294
781fa474 295 k = write(f->output_fd, f->out_buffer, f->out_buffer_full);
023fb90b 296 if (k < 0) {
4ba93280 297
023fb90b
LP
298 if (errno == EAGAIN)
299 f->stdout_writable = false;
fce93d7a 300 else if (errno == EIO || ERRNO_IS_DISCONNECT(errno)) {
023fb90b
LP
301 f->stdout_writable = false;
302 f->stdout_hangup = true;
303 f->stdout_event_source = sd_event_source_unref(f->stdout_event_source);
4ba93280 304 } else {
56f64d95 305 log_error_errno(errno, "write(): %m");
2a453c2e 306 return pty_forward_done(f, -errno);
4ba93280 307 }
4ba93280 308
023fb90b 309 } else {
9b15b784
LP
310
311 if (k > 0) {
312 f->last_char = f->out_buffer[k-1];
313 f->last_char_set = true;
314 }
315
023fb90b
LP
316 assert(f->out_buffer_full >= (size_t) k);
317 memmove(f->out_buffer, f->out_buffer + k, f->out_buffer_full - k);
318 f->out_buffer_full -= k;
4ba93280 319 }
023fb90b
LP
320 }
321 }
322
323 if (f->stdin_hangup || f->stdout_hangup || f->master_hangup) {
324 /* Exit the loop if any side hung up and if there's
325 * nothing more to write or nothing we could write. */
4ba93280 326
023fb90b
LP
327 if ((f->out_buffer_full <= 0 || f->stdout_hangup) &&
328 (f->in_buffer_full <= 0 || f->master_hangup))
2a453c2e 329 return pty_forward_done(f, 0);
023fb90b 330 }
4ba93280 331
95f1d6bf
LP
332 /* If we were asked to drain, and there's nothing more to handle from the master, then call the callback
333 * too. */
e22e69a3 334 if (f->drain && drained(f))
95f1d6bf
LP
335 return pty_forward_done(f, 0);
336
023fb90b
LP
337 return 0;
338}
4ba93280 339
023fb90b 340static int on_master_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
99534007 341 PTYForward *f = ASSERT_PTR(userdata);
4ba93280 342
023fb90b
LP
343 assert(e);
344 assert(e == f->master_event_source);
345 assert(fd >= 0);
346 assert(fd == f->master);
04d39279 347
023fb90b
LP
348 if (revents & (EPOLLIN|EPOLLHUP))
349 f->master_readable = true;
04d39279 350
023fb90b
LP
351 if (revents & (EPOLLOUT|EPOLLHUP))
352 f->master_writable = true;
04d39279 353
023fb90b
LP
354 return shovel(f);
355}
04d39279 356
023fb90b 357static int on_stdin_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
99534007 358 PTYForward *f = ASSERT_PTR(userdata);
04d39279 359
023fb90b
LP
360 assert(e);
361 assert(e == f->stdin_event_source);
362 assert(fd >= 0);
781fa474 363 assert(fd == f->input_fd);
04d39279 364
023fb90b
LP
365 if (revents & (EPOLLIN|EPOLLHUP))
366 f->stdin_readable = true;
04d39279 367
023fb90b
LP
368 return shovel(f);
369}
04d39279 370
023fb90b 371static int on_stdout_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
99534007 372 PTYForward *f = ASSERT_PTR(userdata);
04d39279 373
023fb90b
LP
374 assert(e);
375 assert(e == f->stdout_event_source);
376 assert(fd >= 0);
781fa474 377 assert(fd == f->output_fd);
04d39279 378
023fb90b
LP
379 if (revents & (EPOLLOUT|EPOLLHUP))
380 f->stdout_writable = true;
04d39279 381
023fb90b
LP
382 return shovel(f);
383}
04d39279 384
023fb90b 385static int on_sigwinch_event(sd_event_source *e, const struct signalfd_siginfo *si, void *userdata) {
99534007 386 PTYForward *f = ASSERT_PTR(userdata);
023fb90b 387 struct winsize ws;
04d39279 388
023fb90b
LP
389 assert(e);
390 assert(e == f->sigwinch_event_source);
391
392 /* The window size changed, let's forward that. */
781fa474 393 if (ioctl(f->output_fd, TIOCGWINSZ, &ws) >= 0)
679bc6cb 394 (void) ioctl(f->master, TIOCSWINSZ, &ws);
023fb90b
LP
395
396 return 0;
4ba93280 397}
04d39279 398
9c857b9d
LP
399int pty_forward_new(
400 sd_event *event,
401 int master,
ae3dde80 402 PTYForwardFlags flags,
9c857b9d
LP
403 PTYForward **ret) {
404
023fb90b 405 _cleanup_(pty_forward_freep) PTYForward *f = NULL;
04d39279
LP
406 struct winsize ws;
407 int r;
408
d435a182 409 f = new(PTYForward, 1);
023fb90b
LP
410 if (!f)
411 return -ENOMEM;
412
d435a182
LP
413 *f = (struct PTYForward) {
414 .flags = flags,
415 .master = -1,
781fa474
LP
416 .input_fd = -1,
417 .output_fd = -1,
d435a182 418 };
9b15b784 419
023fb90b
LP
420 if (event)
421 f->event = sd_event_ref(event);
422 else {
423 r = sd_event_default(&f->event);
424 if (r < 0)
425 return r;
426 }
427
781fa474
LP
428 if (FLAGS_SET(flags, PTY_FORWARD_READ_ONLY))
429 f->output_fd = STDOUT_FILENO;
430 else {
431 /* If we shall be invoked in interactive mode, let's switch on non-blocking mode, so that we
432 * never end up staving one direction while we block on the other. However, let's be careful
433 * here and not turn on O_NONBLOCK for stdin/stdout directly, but of re-opened copies of
434 * them. This has two advantages: when we are killed abruptly the stdin/stdout fds won't be
435 * left in O_NONBLOCK state for the next process using them. In addition, if some process
436 * running in the background wants to continue writing to our stdout it can do so without
437 * being confused by O_NONBLOCK. */
438
439 f->input_fd = fd_reopen(STDIN_FILENO, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
440 if (f->input_fd < 0) {
441 /* Handle failures gracefully, after all certain fd types cannot be reopened
442 * (sockets, …) */
443 log_debug_errno(f->input_fd, "Failed to reopen stdin, using original fd: %m");
444
445 r = fd_nonblock(STDIN_FILENO, true);
446 if (r < 0)
447 return r;
448
449 f->input_fd = STDIN_FILENO;
450 } else
451 f->close_input_fd = true;
452
453 f->output_fd = fd_reopen(STDOUT_FILENO, O_WRONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
454 if (f->output_fd < 0) {
455 log_debug_errno(f->output_fd, "Failed to reopen stdout, using original fd: %m");
456
457 r = fd_nonblock(STDOUT_FILENO, true);
458 if (r < 0)
459 return r;
460
461 f->output_fd = STDOUT_FILENO;
462 } else
463 f->close_output_fd = true;
9c857b9d 464 }
023fb90b
LP
465
466 r = fd_nonblock(master, true);
467 if (r < 0)
468 return r;
469
470 f->master = master;
471
d46b79bb 472 if (ioctl(f->output_fd, TIOCGWINSZ, &ws) < 0)
da22bdbc
LP
473 /* If we can't get the resolution from the output fd, then use our internal, regular width/height,
474 * i.e. something derived from $COLUMNS and $LINES if set. */
da22bdbc
LP
475 ws = (struct winsize) {
476 .ws_row = lines(),
477 .ws_col = columns(),
478 };
da22bdbc
LP
479
480 (void) ioctl(master, TIOCSWINSZ, &ws);
04d39279 481
ae3dde80 482 if (!(flags & PTY_FORWARD_READ_ONLY)) {
781fa474
LP
483 assert(f->input_fd >= 0);
484
485 if (tcgetattr(f->input_fd, &f->saved_stdin_attr) >= 0) {
9c857b9d 486 struct termios raw_stdin_attr;
023fb90b 487
9c857b9d 488 f->saved_stdin = true;
90d14d20 489
9c857b9d
LP
490 raw_stdin_attr = f->saved_stdin_attr;
491 cfmakeraw(&raw_stdin_attr);
492 raw_stdin_attr.c_oflag = f->saved_stdin_attr.c_oflag;
781fa474 493 tcsetattr(f->input_fd, TCSANOW, &raw_stdin_attr);
9c857b9d 494 }
023fb90b 495
781fa474 496 if (tcgetattr(f->output_fd, &f->saved_stdout_attr) >= 0) {
9c857b9d 497 struct termios raw_stdout_attr;
023fb90b 498
9c857b9d 499 f->saved_stdout = true;
04d39279 500
9c857b9d
LP
501 raw_stdout_attr = f->saved_stdout_attr;
502 cfmakeraw(&raw_stdout_attr);
503 raw_stdout_attr.c_iflag = f->saved_stdout_attr.c_iflag;
504 raw_stdout_attr.c_lflag = f->saved_stdout_attr.c_lflag;
781fa474 505 tcsetattr(f->output_fd, TCSANOW, &raw_stdout_attr);
9c857b9d 506 }
04d39279 507
781fa474 508 r = sd_event_add_io(f->event, &f->stdin_event_source, f->input_fd, EPOLLIN|EPOLLET, on_stdin_event, f);
9c857b9d
LP
509 if (r < 0 && r != -EPERM)
510 return r;
9a1c8f2d
LP
511
512 if (r >= 0)
513 (void) sd_event_source_set_description(f->stdin_event_source, "ptyfwd-stdin");
9c857b9d 514 }
023fb90b 515
781fa474 516 r = sd_event_add_io(f->event, &f->stdout_event_source, f->output_fd, EPOLLOUT|EPOLLET, on_stdout_event, f);
023fb90b
LP
517 if (r == -EPERM)
518 /* stdout without epoll support. Likely redirected to regular file. */
519 f->stdout_writable = true;
520 else if (r < 0)
521 return r;
9a1c8f2d
LP
522 else
523 (void) sd_event_source_set_description(f->stdout_event_source, "ptyfwd-stdout");
023fb90b 524
9c857b9d
LP
525 r = sd_event_add_io(f->event, &f->master_event_source, master, EPOLLIN|EPOLLOUT|EPOLLET, on_master_event, f);
526 if (r < 0)
527 return r;
528
9a1c8f2d
LP
529 (void) sd_event_source_set_description(f->master_event_source, "ptyfwd-master");
530
023fb90b 531 r = sd_event_add_signal(f->event, &f->sigwinch_event_source, SIGWINCH, on_sigwinch_event, f);
679bc6cb
LP
532 if (r < 0)
533 return r;
023fb90b 534
9a1c8f2d
LP
535 (void) sd_event_source_set_description(f->sigwinch_event_source, "ptyfwd-sigwinch");
536
1cc6c93a 537 *ret = TAKE_PTR(f);
023fb90b
LP
538
539 return 0;
540}
541
542PTYForward *pty_forward_free(PTYForward *f) {
2a453c2e 543 pty_forward_disconnect(f);
6b430fdb 544 return mfree(f);
04d39279 545}
9b15b784 546
0ec5543c 547int pty_forward_get_last_char(PTYForward *f, char *ch) {
9b15b784
LP
548 assert(f);
549 assert(ch);
550
551 if (!f->last_char_set)
552 return -ENXIO;
553
554 *ch = f->last_char;
555 return 0;
556}
0ec5543c 557
ae3dde80 558int pty_forward_set_ignore_vhangup(PTYForward *f, bool b) {
0ec5543c
LP
559 int r;
560
561 assert(f);
562
ae3dde80 563 if (!!(f->flags & PTY_FORWARD_IGNORE_VHANGUP) == b)
0ec5543c
LP
564 return 0;
565
5883ff60 566 SET_FLAG(f->flags, PTY_FORWARD_IGNORE_VHANGUP, b);
ae3dde80
LP
567
568 if (!ignore_vhangup(f)) {
0ec5543c 569
da054c37
LP
570 /* We shall now react to vhangup()s? Let's check
571 * immediately if we might be in one */
0ec5543c
LP
572
573 f->master_readable = true;
574 r = shovel(f);
575 if (r < 0)
576 return r;
577 }
578
579 return 0;
580}
581
2a453c2e 582bool pty_forward_get_ignore_vhangup(PTYForward *f) {
0ec5543c
LP
583 assert(f);
584
ae3dde80 585 return !!(f->flags & PTY_FORWARD_IGNORE_VHANGUP);
0ec5543c 586}
2a453c2e
LP
587
588bool pty_forward_is_done(PTYForward *f) {
589 assert(f);
590
591 return f->done;
592}
593
594void pty_forward_set_handler(PTYForward *f, PTYForwardHandler cb, void *userdata) {
595 assert(f);
596
597 f->handler = cb;
598 f->userdata = userdata;
599}
95f1d6bf
LP
600
601bool pty_forward_drain(PTYForward *f) {
602 assert(f);
603
604 /* Starts draining the forwarder. Specifically:
605 *
606 * - Returns true if there are no unprocessed bytes from the pty, false otherwise
607 *
608 * - Makes sure the handler function is called the next time the number of unprocessed bytes hits zero
609 */
610
611 f->drain = true;
e22e69a3 612 return drained(f);
95f1d6bf 613}
d147457c
LP
614
615int pty_forward_set_priority(PTYForward *f, int64_t priority) {
616 int r;
617 assert(f);
618
1ba37106
LP
619 if (f->stdin_event_source) {
620 r = sd_event_source_set_priority(f->stdin_event_source, priority);
621 if (r < 0)
622 return r;
623 }
d147457c
LP
624
625 r = sd_event_source_set_priority(f->stdout_event_source, priority);
626 if (r < 0)
627 return r;
628
629 r = sd_event_source_set_priority(f->master_event_source, priority);
630 if (r < 0)
631 return r;
632
633 r = sd_event_source_set_priority(f->sigwinch_event_source, priority);
634 if (r < 0)
635 return r;
636
637 return 0;
638}
d435a182
LP
639
640int pty_forward_set_width_height(PTYForward *f, unsigned width, unsigned height) {
641 struct winsize ws;
642
643 assert(f);
644
f5fbe71d 645 if (width == UINT_MAX && height == UINT_MAX)
d435a182
LP
646 return 0; /* noop */
647
f5fbe71d 648 if (width != UINT_MAX &&
d435a182
LP
649 (width == 0 || width > USHRT_MAX))
650 return -ERANGE;
651
f5fbe71d 652 if (height != UINT_MAX &&
d435a182
LP
653 (height == 0 || height > USHRT_MAX))
654 return -ERANGE;
655
f5fbe71d 656 if (width == UINT_MAX || height == UINT_MAX) {
d435a182
LP
657 if (ioctl(f->master, TIOCGWINSZ, &ws) < 0)
658 return -errno;
659
f5fbe71d 660 if (width != UINT_MAX)
d435a182 661 ws.ws_col = width;
f5fbe71d 662 if (height != UINT_MAX)
d435a182
LP
663 ws.ws_row = height;
664 } else
665 ws = (struct winsize) {
666 .ws_row = height,
667 .ws_col = width,
668 };
669
670 if (ioctl(f->master, TIOCSWINSZ, &ws) < 0)
671 return -errno;
672
673 /* Make sure we ignore SIGWINCH window size events from now on */
674 f->sigwinch_event_source = sd_event_source_unref(f->sigwinch_event_source);
675
676 return 0;
677}