chiark / gitweb /
ptyfwd: simplify how we handle vhangups a bit
authorLennart Poettering <lennart@poettering.net>
Wed, 7 Jan 2015 13:46:45 +0000 (14:46 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 7 Jan 2015 13:47:10 +0000 (14:47 +0100)
src/machine/machinectl.c
src/shared/ptyfwd.c
src/shared/ptyfwd.h

index 7e995cab68d22e6d092d703c7fd7d0fbd33cc602..be3896a19bbe60e3fae460326b66ca1d4ee3f00f 100644 (file)
@@ -1240,13 +1240,14 @@ static int on_machine_removed(sd_bus *bus, sd_bus_message *m, void *userdata, sd
 
         if (*forward) {
                 /* If the forwarder is already initialized, tell it to
-                 * exit on the next hangup */
+                 * exit on the next vhangup(), so that we still flush
+                 * out what might be queued and exit then. */
 
-                r = pty_forward_set_repeat(*forward, false);
+                r = pty_forward_set_ignore_vhangup(*forward, false);
                 if (r >= 0)
                         return 0;
 
-                log_error_errno(r, "Failed to set repeat flag: %m");
+                log_error_errno(r, "Failed to set ignore_vhangup flag: %m");
         }
 
         /* On error, or when the forwarder is not initialized yet, quit immediately */
@@ -1341,7 +1342,7 @@ static int login_machine(int argc, char *argv[], void *userdata) {
                 return log_error_errno(r, "Failed to run event loop: %m");
 
         pty_forward_get_last_char(forward, &last_char);
-        machine_died = pty_forward_get_repeat(forward) == 0;
+        machine_died = pty_forward_get_ignore_vhangup(forward) == 0;
 
         forward = pty_forward_free(forward);
 
index db2445ceab20a3780aac2d18f627c4ad9d4538f1..a780f7de9af99f68c774021f2ee2ddfa3a4fc3b9 100644 (file)
@@ -52,9 +52,9 @@ struct PTYForward {
         bool master_readable:1;
         bool master_writable:1;
         bool master_hangup:1;
-        bool master_suppressed_hangup:1;
 
-        bool repeat:1;
+        /* Continue reading after hangup? */
+        bool ignore_vhangup:1;
 
         bool last_char_set:1;
         char last_char;
@@ -171,22 +171,16 @@ static int shovel(PTYForward *f) {
                         k = read(f->master, f->out_buffer + f->out_buffer_full, LINE_MAX - f->out_buffer_full);
                         if (k < 0) {
 
-                                if (errno == EAGAIN)
-                                        f->master_readable = false;
-
-                                else if (errno == EIO && f->repeat) {
-
-                                        /* Note that EIO on the master device
-                                         * might be cause by vhangup() or
-                                         * temporary closing of everything on
-                                         * the other side, we treat it like
-                                         * EAGAIN here and try again, unless
-                                         * repeat is off. */
+                                /* 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. */
 
+                                if (errno == EAGAIN || (errno == EIO && f->ignore_vhangup))
                                         f->master_readable = false;
-                                        f->master_suppressed_hangup = true;
-
-                                } else if (errno == EPIPE || errno == ECONNRESET || errno == EIO) {
+                                else if (errno == EPIPE || errno == ECONNRESET || errno == EIO) {
                                         f->master_readable = f->master_writable = false;
                                         f->master_hangup = true;
 
@@ -250,8 +244,6 @@ static int on_master_event(sd_event_source *e, int fd, uint32_t revents, void *u
         assert(fd >= 0);
         assert(fd == f->master);
 
-        f->master_suppressed_hangup = false;
-
         if (revents & (EPOLLIN|EPOLLHUP))
                 f->master_readable = true;
 
@@ -306,7 +298,7 @@ static int on_sigwinch_event(sd_event_source *e, const struct signalfd_siginfo *
         return 0;
 }
 
-int pty_forward_new(sd_event *event, int master, bool repeat, PTYForward **ret) {
+int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, PTYForward **ret) {
         _cleanup_(pty_forward_freep) PTYForward *f = NULL;
         struct winsize ws;
         int r;
@@ -315,7 +307,7 @@ int pty_forward_new(sd_event *event, int master, bool repeat, PTYForward **ret)
         if (!f)
                 return -ENOMEM;
 
-        f->repeat = repeat;
+        f->ignore_vhangup = ignore_vhangup;
 
         if (event)
                 f->event = sd_event_ref(event);
@@ -423,23 +415,19 @@ int pty_forward_get_last_char(PTYForward *f, char *ch) {
         return 0;
 }
 
-int pty_forward_set_repeat(PTYForward *f, bool repeat) {
+int pty_forward_set_ignore_vhangup(PTYForward *f, bool ignore_vhangup) {
         int r;
 
         assert(f);
 
-        if (f->repeat == repeat)
+        if (f->ignore_vhangup == ignore_vhangup)
                 return 0;
 
-        f->repeat = repeat;
-
-        /* Are we currently in a suppress hangup phase? If so, let's
-         * immediately terminate things */
-        if (!f->repeat && f->master_suppressed_hangup) {
+        f->ignore_vhangup = ignore_vhangup;
+        if (!f->ignore_vhangup) {
 
-                /* Let's try to read again from the master fd, and if
-                 * it is this will now cause termination of the
-                 * session. */
+                /* We shall now react to vhangup()s? Let's check
+                 * immediately if we might be in one */
 
                 f->master_readable = true;
                 r = shovel(f);
@@ -450,8 +438,8 @@ int pty_forward_set_repeat(PTYForward *f, bool repeat) {
         return 0;
 }
 
-int pty_forward_get_repeat(PTYForward *f) {
+int pty_forward_get_ignore_vhangup(PTYForward *f) {
         assert(f);
 
-        return f->repeat;
+        return f->ignore_vhangup;
 }
index d557dee9db2f8be4d8c2243e61b1f945cd5d20b5..d65b66a3d081786b7ff14a9ab022df0c71ccf18d 100644 (file)
 
 typedef struct PTYForward PTYForward;
 
-int pty_forward_new(sd_event *event, int master, bool repeat, PTYForward **f);
+int pty_forward_new(sd_event *event, int master, bool ignore_vhangup, PTYForward **f);
 PTYForward *pty_forward_free(PTYForward *f);
 
 int pty_forward_get_last_char(PTYForward *f, char *ch);
 
-int pty_forward_set_repeat(PTYForward *f, bool repeat);
-int pty_forward_get_repeat(PTYForward *f);
+int pty_forward_set_ignore_vhangup(PTYForward *f, bool ignore_vhangup);
+int pty_forward_get_ignore_vhangup(PTYForward *f);
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(PTYForward*, pty_forward_free);