]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ptyfwd: replace pty_forward_set_ignore_vhangup() with pty_forward_honor_vhangup()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 28 Jul 2025 15:59:46 +0000 (00:59 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 29 Jul 2025 16:14:57 +0000 (01:14 +0900)
Currently, pty_forward_set_ignore_vhangup() is only used for disabling
the flag. To make the function also disable PTY_FORWARD_IGNORE_INITIAL_VHANGUP
flag, this renames it to pty_forward_honor_vhangup().

Also, for consistency, pty_forward_get_ignore_vhangup() and
ignore_vhangup() are replaced with pty_forward_vhangup_honored().

src/machine/machinectl.c
src/shared/ptyfwd.c
src/shared/ptyfwd.h

index 9eddc53990d3d48ca9b81ce26f98f90d68e71fe2..4e3138b2ff152455dcc4ac26af072c2ac39187d6 100644 (file)
@@ -1236,10 +1236,10 @@ static int on_machine_removed(sd_bus_message *m, void *userdata, sd_bus_error *r
         /* Tell the forwarder to exit on the next vhangup(), so that we still flush out what might be queued
          * and exit then. */
 
-        r = pty_forward_set_ignore_vhangup(forward, false);
+        r = pty_forward_honor_vhangup(forward);
         if (r < 0) {
                 /* On error, quit immediately. */
-                log_error_errno(r, "Failed to set ignore_vhangup flag: %m");
+                log_error_errno(r, "Failed to make PTY forwarder honor vhangup(): %m");
                 (void) sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), EXIT_FAILURE);
         }
 
@@ -1285,8 +1285,8 @@ static int process_forward(sd_event *event, sd_bus_slot *machine_removed_slot, i
                 return log_error_errno(r, "Failed to run event loop: %m");
 
         bool machine_died =
-                (flags & PTY_FORWARD_IGNORE_VHANGUP) &&
-                pty_forward_get_ignore_vhangup(forward) == 0;
+                FLAGS_SET(flags, PTY_FORWARD_IGNORE_VHANGUP) &&
+                !pty_forward_vhangup_honored(forward);
 
         if (!arg_quiet) {
                 if (machine_died)
index 77bd624ec97d0af3e4539055cd2e5c78c4196d3f..543466ceb733378175df7b810853fec9b7dfd906 100644 (file)
@@ -249,18 +249,6 @@ static RequestOperation look_for_escape(PTYForward *f, const char *buffer, size_
         return REQUEST_NOP;
 }
 
-static bool ignore_vhangup(PTYForward *f) {
-        assert(f);
-
-        if (f->flags & PTY_FORWARD_IGNORE_VHANGUP)
-                return true;
-
-        if ((f->flags & PTY_FORWARD_IGNORE_INITIAL_VHANGUP) && !f->read_from_master)
-                return true;
-
-        return false;
-}
-
 static bool drained(PTYForward *f) {
         int q = 0;
 
@@ -720,9 +708,9 @@ static int do_shovel(PTYForward *f) {
 
                                 /* Note that EIO on the master device might be caused by vhangup() or
                                  * temporary closing of everything on the other side, we treat it like EAGAIN
-                                 * here and try again, unless ignore_vhangup is off. */
+                                 * here and try again, unless vhangup() is honored. */
 
-                                if (errno == EAGAIN || (errno == EIO && ignore_vhangup(f)))
+                                if (errno == EAGAIN || (errno == EIO && !pty_forward_vhangup_honored(f)))
                                         f->master_readable = false;
                                 else if (IN_SET(errno, EPIPE, ECONNRESET, EIO)) {
                                         f->master_readable = f->master_writable = false;
@@ -1087,33 +1075,29 @@ PTYForward* pty_forward_free(PTYForward *f) {
         return mfree(f);
 }
 
-int pty_forward_set_ignore_vhangup(PTYForward *f, bool b) {
-        int r;
-
+int pty_forward_honor_vhangup(PTYForward *f) {
         assert(f);
 
-        if (FLAGS_SET(f->flags, PTY_FORWARD_IGNORE_VHANGUP) == b)
-                return 0;
-
-        SET_FLAG(f->flags, PTY_FORWARD_IGNORE_VHANGUP, b);
-
-        if (!ignore_vhangup(f)) {
+        if ((f->flags & (PTY_FORWARD_IGNORE_VHANGUP | PTY_FORWARD_IGNORE_INITIAL_VHANGUP)) == 0)
+                return 0; /* nothing changed. */
 
-                /* We shall now react to vhangup()s? Let's check immediately if we might be in one. */
+        f->flags &= ~(PTY_FORWARD_IGNORE_VHANGUP | PTY_FORWARD_IGNORE_INITIAL_VHANGUP);
 
-                f->master_readable = true;
-                r = shovel(f);
-                if (r < 0)
-                        return r;
-        }
-
-        return 0;
+        /* We shall now react to vhangup()s? Let's check immediately if we might be in one. */
+        f->master_readable = true;
+        return shovel(f);
 }
 
-bool pty_forward_get_ignore_vhangup(PTYForward *f) {
+bool pty_forward_vhangup_honored(const PTYForward *f) {
         assert(f);
 
-        return FLAGS_SET(f->flags, PTY_FORWARD_IGNORE_VHANGUP);
+        if (FLAGS_SET(f->flags, PTY_FORWARD_IGNORE_VHANGUP))
+                return false;
+
+        if (FLAGS_SET(f->flags, PTY_FORWARD_IGNORE_INITIAL_VHANGUP) && !f->read_from_master)
+                return false;
+
+        return true;
 }
 
 void pty_forward_set_hangup_handler(PTYForward *f, PTYForwardHangupHandler cb, void *userdata) {
index 6f7ec0734c2698b52417daac9321cac3731788c6..eb6d2807c088fcd01c20c3e27d2eed58f3b64d24 100644 (file)
@@ -28,8 +28,8 @@ extern const int pty_forward_signals[N_PTY_FORWARD_SIGNALS];
 int pty_forward_new(sd_event *event, int master, PTYForwardFlags flags, PTYForward **ret);
 PTYForward* pty_forward_free(PTYForward *f);
 
-int pty_forward_set_ignore_vhangup(PTYForward *f, bool ignore_vhangup);
-bool pty_forward_get_ignore_vhangup(PTYForward *f);
+int pty_forward_honor_vhangup(PTYForward *f);
+bool pty_forward_vhangup_honored(const PTYForward *f);
 
 void pty_forward_set_hangup_handler(PTYForward *f, PTYForwardHangupHandler handler, void *userdata);
 void pty_forward_set_hotkey_handler(PTYForward *f, PTYForwardHotkeyHandler handler, void *userdata);