]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/ptyfwd.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / shared / ptyfwd.c
index 3a02ae98dcb69e4fbb7776d3a1d534befddfd007..487a013148b9cd606e3ba46f879af25b34121e19 100644 (file)
@@ -1,3 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
@@ -69,6 +70,7 @@ struct PTYForward {
         bool read_from_master:1;
 
         bool done:1;
+        bool drain:1;
 
         bool last_char_set:1;
         char last_char;
@@ -186,7 +188,7 @@ static int shovel(PTYForward *f) {
 
                                 if (errno == EAGAIN)
                                         f->stdin_readable = false;
-                                else if (errno == EIO || errno == EPIPE || errno == ECONNRESET) {
+                                else if (IN_SET(errno, EIO, EPIPE, ECONNRESET)) {
                                         f->stdin_readable = false;
                                         f->stdin_hangup = true;
 
@@ -216,9 +218,9 @@ static int shovel(PTYForward *f) {
                         k = write(f->master, f->in_buffer, f->in_buffer_full);
                         if (k < 0) {
 
-                                if (errno == EAGAIN || errno == EIO)
+                                if (IN_SET(errno, EAGAIN, EIO))
                                         f->master_writable = false;
-                                else if (errno == EPIPE || errno == ECONNRESET) {
+                                else if (IN_SET(errno, EPIPE, ECONNRESET)) {
                                         f->master_writable = f->master_readable = false;
                                         f->master_hangup = true;
 
@@ -248,7 +250,7 @@ static int shovel(PTYForward *f) {
 
                                 if (errno == EAGAIN || (errno == EIO && ignore_vhangup(f)))
                                         f->master_readable = false;
-                                else if (errno == EPIPE || errno == ECONNRESET || errno == EIO) {
+                                else if (IN_SET(errno, EPIPE, ECONNRESET, EIO)) {
                                         f->master_readable = f->master_writable = false;
                                         f->master_hangup = true;
 
@@ -270,7 +272,7 @@ static int shovel(PTYForward *f) {
 
                                 if (errno == EAGAIN)
                                         f->stdout_writable = false;
-                                else if (errno == EIO || errno == EPIPE || errno == ECONNRESET) {
+                                else if (IN_SET(errno, EIO, EPIPE, ECONNRESET)) {
                                         f->stdout_writable = false;
                                         f->stdout_hangup = true;
                                         f->stdout_event_source = sd_event_source_unref(f->stdout_event_source);
@@ -302,6 +304,11 @@ static int shovel(PTYForward *f) {
                         return pty_forward_done(f, 0);
         }
 
+        /* If we were asked to drain, and there's nothing more to handle from the master, then call the callback
+         * too. */
+        if (f->drain && f->out_buffer_full == 0 && !f->master_readable)
+                return pty_forward_done(f, 0);
+
         return 0;
 }
 
@@ -528,3 +535,18 @@ void pty_forward_set_handler(PTYForward *f, PTYForwardHandler cb, void *userdata
         f->handler = cb;
         f->userdata = userdata;
 }
+
+bool pty_forward_drain(PTYForward *f) {
+        assert(f);
+
+        /* Starts draining the forwarder. Specifically:
+         *
+         * - Returns true if there are no unprocessed bytes from the pty, false otherwise
+         *
+         * - Makes sure the handler function is called the next time the number of unprocessed bytes hits zero
+         */
+
+        f->drain = true;
+
+        return f->out_buffer_full == 0 && !f->master_readable;
+}