]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/ptyfwd.c
ptyfwd: trivial format cleanups
[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
7b3bbb10 145 if (f->escape_counter == 0 || nw > f->escape_timestamp + ESCAPE_USEC) {
023fb90b
LP
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);
7b3bbb10 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
7b3bbb10
YW
269 /* Note that EIO on the master device might be caused by vhangup() or
270 * temporary closing of everything on the other side, we treat it like EAGAIN
271 * here and try again, unless ignore_vhangup is off. */
4ba93280 272
ae3dde80 273 if (errno == EAGAIN || (errno == EIO && ignore_vhangup(f)))
023fb90b 274 f->master_readable = false;
3742095b 275 else if (IN_SET(errno, EPIPE, ECONNRESET, EIO)) {
023fb90b
LP
276 f->master_readable = f->master_writable = false;
277 f->master_hangup = true;
4ba93280 278
023fb90b 279 f->master_event_source = sd_event_source_unref(f->master_event_source);
04d39279 280 } else {
56f64d95 281 log_error_errno(errno, "read(): %m");
2a453c2e 282 return pty_forward_done(f, -errno);
04d39279 283 }
7b3bbb10 284 } else {
ae3dde80 285 f->read_from_master = true;
023fb90b 286 f->out_buffer_full += (size_t) k;
ae3dde80 287 }
023fb90b 288 }
4ba93280 289
023fb90b 290 if (f->stdout_writable && f->out_buffer_full > 0) {
4ba93280 291
781fa474 292 k = write(f->output_fd, f->out_buffer, f->out_buffer_full);
023fb90b 293 if (k < 0) {
4ba93280 294
023fb90b
LP
295 if (errno == EAGAIN)
296 f->stdout_writable = false;
fce93d7a 297 else if (errno == EIO || ERRNO_IS_DISCONNECT(errno)) {
023fb90b
LP
298 f->stdout_writable = false;
299 f->stdout_hangup = true;
300 f->stdout_event_source = sd_event_source_unref(f->stdout_event_source);
4ba93280 301 } else {
56f64d95 302 log_error_errno(errno, "write(): %m");
2a453c2e 303 return pty_forward_done(f, -errno);
4ba93280 304 }
4ba93280 305
023fb90b 306 } else {
9b15b784
LP
307
308 if (k > 0) {
309 f->last_char = f->out_buffer[k-1];
310 f->last_char_set = true;
311 }
312
023fb90b
LP
313 assert(f->out_buffer_full >= (size_t) k);
314 memmove(f->out_buffer, f->out_buffer + k, f->out_buffer_full - k);
315 f->out_buffer_full -= k;
4ba93280 316 }
023fb90b
LP
317 }
318 }
319
320 if (f->stdin_hangup || f->stdout_hangup || f->master_hangup) {
321 /* Exit the loop if any side hung up and if there's
322 * nothing more to write or nothing we could write. */
4ba93280 323
023fb90b
LP
324 if ((f->out_buffer_full <= 0 || f->stdout_hangup) &&
325 (f->in_buffer_full <= 0 || f->master_hangup))
2a453c2e 326 return pty_forward_done(f, 0);
023fb90b 327 }
4ba93280 328
95f1d6bf
LP
329 /* If we were asked to drain, and there's nothing more to handle from the master, then call the callback
330 * too. */
e22e69a3 331 if (f->drain && drained(f))
95f1d6bf
LP
332 return pty_forward_done(f, 0);
333
023fb90b
LP
334 return 0;
335}
4ba93280 336
023fb90b 337static int on_master_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
99534007 338 PTYForward *f = ASSERT_PTR(userdata);
4ba93280 339
023fb90b
LP
340 assert(e);
341 assert(e == f->master_event_source);
342 assert(fd >= 0);
343 assert(fd == f->master);
04d39279 344
023fb90b
LP
345 if (revents & (EPOLLIN|EPOLLHUP))
346 f->master_readable = true;
04d39279 347
023fb90b
LP
348 if (revents & (EPOLLOUT|EPOLLHUP))
349 f->master_writable = true;
04d39279 350
023fb90b
LP
351 return shovel(f);
352}
04d39279 353
023fb90b 354static int on_stdin_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
99534007 355 PTYForward *f = ASSERT_PTR(userdata);
04d39279 356
023fb90b
LP
357 assert(e);
358 assert(e == f->stdin_event_source);
359 assert(fd >= 0);
781fa474 360 assert(fd == f->input_fd);
04d39279 361
023fb90b
LP
362 if (revents & (EPOLLIN|EPOLLHUP))
363 f->stdin_readable = true;
04d39279 364
023fb90b
LP
365 return shovel(f);
366}
04d39279 367
023fb90b 368static int on_stdout_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
99534007 369 PTYForward *f = ASSERT_PTR(userdata);
04d39279 370
023fb90b
LP
371 assert(e);
372 assert(e == f->stdout_event_source);
373 assert(fd >= 0);
781fa474 374 assert(fd == f->output_fd);
04d39279 375
023fb90b
LP
376 if (revents & (EPOLLOUT|EPOLLHUP))
377 f->stdout_writable = true;
04d39279 378
023fb90b
LP
379 return shovel(f);
380}
04d39279 381
023fb90b 382static int on_sigwinch_event(sd_event_source *e, const struct signalfd_siginfo *si, void *userdata) {
99534007 383 PTYForward *f = ASSERT_PTR(userdata);
023fb90b 384 struct winsize ws;
04d39279 385
023fb90b
LP
386 assert(e);
387 assert(e == f->sigwinch_event_source);
388
389 /* The window size changed, let's forward that. */
781fa474 390 if (ioctl(f->output_fd, TIOCGWINSZ, &ws) >= 0)
679bc6cb 391 (void) ioctl(f->master, TIOCSWINSZ, &ws);
023fb90b
LP
392
393 return 0;
4ba93280 394}
04d39279 395
9c857b9d
LP
396int pty_forward_new(
397 sd_event *event,
398 int master,
ae3dde80 399 PTYForwardFlags flags,
9c857b9d
LP
400 PTYForward **ret) {
401
023fb90b 402 _cleanup_(pty_forward_freep) PTYForward *f = NULL;
04d39279
LP
403 struct winsize ws;
404 int r;
405
d435a182 406 f = new(PTYForward, 1);
023fb90b
LP
407 if (!f)
408 return -ENOMEM;
409
d435a182
LP
410 *f = (struct PTYForward) {
411 .flags = flags,
254d1313
ZJS
412 .master = -EBADF,
413 .input_fd = -EBADF,
414 .output_fd = -EBADF,
d435a182 415 };
9b15b784 416
023fb90b
LP
417 if (event)
418 f->event = sd_event_ref(event);
419 else {
420 r = sd_event_default(&f->event);
421 if (r < 0)
422 return r;
423 }
424
781fa474
LP
425 if (FLAGS_SET(flags, PTY_FORWARD_READ_ONLY))
426 f->output_fd = STDOUT_FILENO;
427 else {
428 /* If we shall be invoked in interactive mode, let's switch on non-blocking mode, so that we
429 * never end up staving one direction while we block on the other. However, let's be careful
430 * here and not turn on O_NONBLOCK for stdin/stdout directly, but of re-opened copies of
431 * them. This has two advantages: when we are killed abruptly the stdin/stdout fds won't be
432 * left in O_NONBLOCK state for the next process using them. In addition, if some process
433 * running in the background wants to continue writing to our stdout it can do so without
434 * being confused by O_NONBLOCK. */
435
436 f->input_fd = fd_reopen(STDIN_FILENO, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
437 if (f->input_fd < 0) {
438 /* Handle failures gracefully, after all certain fd types cannot be reopened
439 * (sockets, …) */
440 log_debug_errno(f->input_fd, "Failed to reopen stdin, using original fd: %m");
441
442 r = fd_nonblock(STDIN_FILENO, true);
443 if (r < 0)
444 return r;
445
446 f->input_fd = STDIN_FILENO;
447 } else
448 f->close_input_fd = true;
449
450 f->output_fd = fd_reopen(STDOUT_FILENO, O_WRONLY|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
451 if (f->output_fd < 0) {
452 log_debug_errno(f->output_fd, "Failed to reopen stdout, using original fd: %m");
453
454 r = fd_nonblock(STDOUT_FILENO, true);
455 if (r < 0)
456 return r;
457
458 f->output_fd = STDOUT_FILENO;
459 } else
460 f->close_output_fd = true;
9c857b9d 461 }
023fb90b
LP
462
463 r = fd_nonblock(master, true);
464 if (r < 0)
465 return r;
466
467 f->master = master;
468
d46b79bb 469 if (ioctl(f->output_fd, TIOCGWINSZ, &ws) < 0)
da22bdbc
LP
470 /* If we can't get the resolution from the output fd, then use our internal, regular width/height,
471 * i.e. something derived from $COLUMNS and $LINES if set. */
da22bdbc
LP
472 ws = (struct winsize) {
473 .ws_row = lines(),
474 .ws_col = columns(),
475 };
da22bdbc
LP
476
477 (void) ioctl(master, TIOCSWINSZ, &ws);
04d39279 478
ae3dde80 479 if (!(flags & PTY_FORWARD_READ_ONLY)) {
781fa474
LP
480 assert(f->input_fd >= 0);
481
482 if (tcgetattr(f->input_fd, &f->saved_stdin_attr) >= 0) {
9c857b9d 483 struct termios raw_stdin_attr;
023fb90b 484
9c857b9d 485 f->saved_stdin = true;
90d14d20 486
9c857b9d
LP
487 raw_stdin_attr = f->saved_stdin_attr;
488 cfmakeraw(&raw_stdin_attr);
489 raw_stdin_attr.c_oflag = f->saved_stdin_attr.c_oflag;
781fa474 490 tcsetattr(f->input_fd, TCSANOW, &raw_stdin_attr);
9c857b9d 491 }
023fb90b 492
781fa474 493 if (tcgetattr(f->output_fd, &f->saved_stdout_attr) >= 0) {
9c857b9d 494 struct termios raw_stdout_attr;
023fb90b 495
9c857b9d 496 f->saved_stdout = true;
04d39279 497
9c857b9d
LP
498 raw_stdout_attr = f->saved_stdout_attr;
499 cfmakeraw(&raw_stdout_attr);
500 raw_stdout_attr.c_iflag = f->saved_stdout_attr.c_iflag;
501 raw_stdout_attr.c_lflag = f->saved_stdout_attr.c_lflag;
781fa474 502 tcsetattr(f->output_fd, TCSANOW, &raw_stdout_attr);
9c857b9d 503 }
04d39279 504
781fa474 505 r = sd_event_add_io(f->event, &f->stdin_event_source, f->input_fd, EPOLLIN|EPOLLET, on_stdin_event, f);
9c857b9d
LP
506 if (r < 0 && r != -EPERM)
507 return r;
9a1c8f2d
LP
508
509 if (r >= 0)
510 (void) sd_event_source_set_description(f->stdin_event_source, "ptyfwd-stdin");
9c857b9d 511 }
023fb90b 512
781fa474 513 r = sd_event_add_io(f->event, &f->stdout_event_source, f->output_fd, EPOLLOUT|EPOLLET, on_stdout_event, f);
023fb90b
LP
514 if (r == -EPERM)
515 /* stdout without epoll support. Likely redirected to regular file. */
516 f->stdout_writable = true;
517 else if (r < 0)
518 return r;
9a1c8f2d
LP
519 else
520 (void) sd_event_source_set_description(f->stdout_event_source, "ptyfwd-stdout");
023fb90b 521
9c857b9d
LP
522 r = sd_event_add_io(f->event, &f->master_event_source, master, EPOLLIN|EPOLLOUT|EPOLLET, on_master_event, f);
523 if (r < 0)
524 return r;
525
9a1c8f2d
LP
526 (void) sd_event_source_set_description(f->master_event_source, "ptyfwd-master");
527
023fb90b 528 r = sd_event_add_signal(f->event, &f->sigwinch_event_source, SIGWINCH, on_sigwinch_event, f);
679bc6cb
LP
529 if (r < 0)
530 return r;
023fb90b 531
9a1c8f2d
LP
532 (void) sd_event_source_set_description(f->sigwinch_event_source, "ptyfwd-sigwinch");
533
1cc6c93a 534 *ret = TAKE_PTR(f);
023fb90b
LP
535
536 return 0;
537}
538
539PTYForward *pty_forward_free(PTYForward *f) {
2a453c2e 540 pty_forward_disconnect(f);
6b430fdb 541 return mfree(f);
04d39279 542}
9b15b784 543
0ec5543c 544int pty_forward_get_last_char(PTYForward *f, char *ch) {
9b15b784
LP
545 assert(f);
546 assert(ch);
547
548 if (!f->last_char_set)
549 return -ENXIO;
550
551 *ch = f->last_char;
552 return 0;
553}
0ec5543c 554
ae3dde80 555int pty_forward_set_ignore_vhangup(PTYForward *f, bool b) {
0ec5543c
LP
556 int r;
557
558 assert(f);
559
ae3dde80 560 if (!!(f->flags & PTY_FORWARD_IGNORE_VHANGUP) == b)
0ec5543c
LP
561 return 0;
562
5883ff60 563 SET_FLAG(f->flags, PTY_FORWARD_IGNORE_VHANGUP, b);
ae3dde80
LP
564
565 if (!ignore_vhangup(f)) {
0ec5543c 566
da054c37
LP
567 /* We shall now react to vhangup()s? Let's check
568 * immediately if we might be in one */
0ec5543c
LP
569
570 f->master_readable = true;
571 r = shovel(f);
572 if (r < 0)
573 return r;
574 }
575
576 return 0;
577}
578
2a453c2e 579bool pty_forward_get_ignore_vhangup(PTYForward *f) {
0ec5543c
LP
580 assert(f);
581
ae3dde80 582 return !!(f->flags & PTY_FORWARD_IGNORE_VHANGUP);
0ec5543c 583}
2a453c2e
LP
584
585bool pty_forward_is_done(PTYForward *f) {
586 assert(f);
587
588 return f->done;
589}
590
591void pty_forward_set_handler(PTYForward *f, PTYForwardHandler cb, void *userdata) {
592 assert(f);
593
594 f->handler = cb;
595 f->userdata = userdata;
596}
95f1d6bf
LP
597
598bool pty_forward_drain(PTYForward *f) {
599 assert(f);
600
601 /* Starts draining the forwarder. Specifically:
602 *
603 * - Returns true if there are no unprocessed bytes from the pty, false otherwise
604 *
605 * - Makes sure the handler function is called the next time the number of unprocessed bytes hits zero
606 */
607
608 f->drain = true;
e22e69a3 609 return drained(f);
95f1d6bf 610}
d147457c
LP
611
612int pty_forward_set_priority(PTYForward *f, int64_t priority) {
613 int r;
614 assert(f);
615
1ba37106
LP
616 if (f->stdin_event_source) {
617 r = sd_event_source_set_priority(f->stdin_event_source, priority);
618 if (r < 0)
619 return r;
620 }
d147457c
LP
621
622 r = sd_event_source_set_priority(f->stdout_event_source, priority);
623 if (r < 0)
624 return r;
625
626 r = sd_event_source_set_priority(f->master_event_source, priority);
627 if (r < 0)
628 return r;
629
630 r = sd_event_source_set_priority(f->sigwinch_event_source, priority);
631 if (r < 0)
632 return r;
633
634 return 0;
635}
d435a182
LP
636
637int pty_forward_set_width_height(PTYForward *f, unsigned width, unsigned height) {
638 struct winsize ws;
639
640 assert(f);
641
f5fbe71d 642 if (width == UINT_MAX && height == UINT_MAX)
d435a182
LP
643 return 0; /* noop */
644
f5fbe71d 645 if (width != UINT_MAX &&
d435a182
LP
646 (width == 0 || width > USHRT_MAX))
647 return -ERANGE;
648
f5fbe71d 649 if (height != UINT_MAX &&
d435a182
LP
650 (height == 0 || height > USHRT_MAX))
651 return -ERANGE;
652
f5fbe71d 653 if (width == UINT_MAX || height == UINT_MAX) {
d435a182
LP
654 if (ioctl(f->master, TIOCGWINSZ, &ws) < 0)
655 return -errno;
656
f5fbe71d 657 if (width != UINT_MAX)
d435a182 658 ws.ws_col = width;
f5fbe71d 659 if (height != UINT_MAX)
d435a182
LP
660 ws.ws_row = height;
661 } else
662 ws = (struct winsize) {
663 .ws_row = height,
664 .ws_col = width,
665 };
666
667 if (ioctl(f->master, TIOCSWINSZ, &ws) < 0)
668 return -errno;
669
670 /* Make sure we ignore SIGWINCH window size events from now on */
671 f->sigwinch_event_source = sd_event_source_unref(f->sigwinch_event_source);
672
673 return 0;
674}