chiark / gitweb /
machinectl: make sure that "machinectl login" exits immediately when the machine...
[elogind.git] / src / shared / ptyfwd.c
index 085d374ed873494105473adfffc6225455eccb46..db2445ceab20a3780aac2d18f627c4ad9d4538f1 100644 (file)
@@ -52,6 +52,12 @@ struct PTYForward {
         bool master_readable:1;
         bool master_writable:1;
         bool master_hangup:1;
+        bool master_suppressed_hangup:1;
+
+        bool repeat:1;
+
+        bool last_char_set:1;
+        char last_char;
 
         char in_buffer[LINE_MAX], out_buffer[LINE_MAX];
         size_t in_buffer_full, out_buffer_full;
@@ -165,15 +171,22 @@ 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) {
 
-                                /* 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. */
+                                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. */
 
-                                if (errno == EAGAIN || errno == EIO)
                                         f->master_readable = false;
-                                else if (errno == EPIPE || errno == ECONNRESET) {
+                                        f->master_suppressed_hangup = true;
+
+                                } else if (errno == EPIPE || errno == ECONNRESET || errno == EIO) {
                                         f->master_readable = f->master_writable = false;
                                         f->master_hangup = true;
 
@@ -203,6 +216,12 @@ static int shovel(PTYForward *f) {
                                 }
 
                         } else {
+
+                                if (k > 0) {
+                                        f->last_char = f->out_buffer[k-1];
+                                        f->last_char_set = true;
+                                }
+
                                 assert(f->out_buffer_full >= (size_t) k);
                                 memmove(f->out_buffer, f->out_buffer + k, f->out_buffer_full - k);
                                 f->out_buffer_full -= k;
@@ -231,6 +250,8 @@ 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;
 
@@ -285,7 +306,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, PTYForward **ret) {
+int pty_forward_new(sd_event *event, int master, bool repeat, PTYForward **ret) {
         _cleanup_(pty_forward_freep) PTYForward *f = NULL;
         struct winsize ws;
         int r;
@@ -294,6 +315,8 @@ int pty_forward_new(sd_event *event, int master, PTYForward **ret) {
         if (!f)
                 return -ENOMEM;
 
+        f->repeat = repeat;
+
         if (event)
                 f->event = sd_event_ref(event);
         else {
@@ -388,3 +411,47 @@ PTYForward *pty_forward_free(PTYForward *f) {
 
         return NULL;
 }
+
+int pty_forward_get_last_char(PTYForward *f, char *ch) {
+        assert(f);
+        assert(ch);
+
+        if (!f->last_char_set)
+                return -ENXIO;
+
+        *ch = f->last_char;
+        return 0;
+}
+
+int pty_forward_set_repeat(PTYForward *f, bool repeat) {
+        int r;
+
+        assert(f);
+
+        if (f->repeat == repeat)
+                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) {
+
+                /* Let's try to read again from the master fd, and if
+                 * it is this will now cause termination of the
+                 * session. */
+
+                f->master_readable = true;
+                r = shovel(f);
+                if (r < 0)
+                        return r;
+        }
+
+        return 0;
+}
+
+int pty_forward_get_repeat(PTYForward *f) {
+        assert(f);
+
+        return f->repeat;
+}