]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/shared/ptyfwd.c
tree-wide: use mfree more
[thirdparty/systemd.git] / src / shared / ptyfwd.c
index 63e81f489490ed3af4bb8902ed8a754799815c31..293c6673fc43980768ad1daf2b79fb2dbc8ef1f2 100644 (file)
@@ -1,5 +1,3 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
 /***
   This file is part of systemd.
 
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <errno.h>
+#include <limits.h>
+#include <signal.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
 #include <sys/epoll.h>
 #include <sys/ioctl.h>
-#include <limits.h>
+#include <sys/time.h>
 #include <termios.h>
+#include <unistd.h>
+
+#include "sd-event.h"
 
 #include "alloc-util.h"
 #include "fd-util.h"
+#include "log.h"
+#include "macro.h"
 #include "ptyfwd.h"
-#include "util.h"
+#include "time-util.h"
 
 struct PTYForward {
         sd_event *event;
@@ -58,6 +68,8 @@ struct PTYForward {
 
         bool read_from_master:1;
 
+        bool done:1;
+
         bool last_char_set:1;
         char last_char;
 
@@ -66,10 +78,54 @@ struct PTYForward {
 
         usec_t escape_timestamp;
         unsigned escape_counter;
+
+        PTYForwardHandler handler;
+        void *userdata;
 };
 
 #define ESCAPE_USEC (1*USEC_PER_SEC)
 
+static void pty_forward_disconnect(PTYForward *f) {
+
+        if (f) {
+                f->stdin_event_source = sd_event_source_unref(f->stdin_event_source);
+                f->stdout_event_source = sd_event_source_unref(f->stdout_event_source);
+
+                f->master_event_source = sd_event_source_unref(f->master_event_source);
+                f->sigwinch_event_source = sd_event_source_unref(f->sigwinch_event_source);
+                f->event = sd_event_unref(f->event);
+
+                if (f->saved_stdout)
+                        tcsetattr(STDOUT_FILENO, TCSANOW, &f->saved_stdout_attr);
+                if (f->saved_stdin)
+                        tcsetattr(STDIN_FILENO, TCSANOW, &f->saved_stdin_attr);
+
+                f->saved_stdout = f->saved_stdin = false;
+        }
+
+        /* STDIN/STDOUT should not be nonblocking normally, so let's unconditionally reset it */
+        fd_nonblock(STDIN_FILENO, false);
+        fd_nonblock(STDOUT_FILENO, false);
+}
+
+static int pty_forward_done(PTYForward *f, int rcode) {
+        _cleanup_(sd_event_unrefp) sd_event *e = NULL;
+        assert(f);
+
+        if (f->done)
+                return 0;
+
+        e = sd_event_ref(f->event);
+
+        f->done = true;
+        pty_forward_disconnect(f);
+
+        if (f->handler)
+                return f->handler(f, rcode, f->userdata);
+        else
+                return sd_event_exit(e, rcode < 0 ? EXIT_FAILURE : rcode);
+}
+
 static bool look_for_escape(PTYForward *f, const char *buffer, size_t n) {
         const char *p;
 
@@ -137,7 +193,7 @@ static int shovel(PTYForward *f) {
                                         f->stdin_event_source = sd_event_source_unref(f->stdin_event_source);
                                 } else {
                                         log_error_errno(errno, "read(): %m");
-                                        return sd_event_exit(f->event, EXIT_FAILURE);
+                                        return pty_forward_done(f, -errno);
                                 }
                         } else if (k == 0) {
                                 /* EOF on stdin */
@@ -146,12 +202,10 @@ static int shovel(PTYForward *f) {
 
                                 f->stdin_event_source = sd_event_source_unref(f->stdin_event_source);
                         } else  {
-                                /* Check if ^] has been
-                                 * pressed three times within
-                                 * one second. If we get this
-                                 * we quite immediately. */
+                                /* Check if ^] has been pressed three times within one second. If we get this we quite
+                                 * immediately. */
                                 if (look_for_escape(f, f->in_buffer + f->in_buffer_full, k))
-                                        return sd_event_exit(f->event, EXIT_FAILURE);
+                                        return pty_forward_done(f, -ECANCELED);
 
                                 f->in_buffer_full += (size_t) k;
                         }
@@ -171,7 +225,7 @@ static int shovel(PTYForward *f) {
                                         f->master_event_source = sd_event_source_unref(f->master_event_source);
                                 } else {
                                         log_error_errno(errno, "write(): %m");
-                                        return sd_event_exit(f->event, EXIT_FAILURE);
+                                        return pty_forward_done(f, -errno);
                                 }
                         } else {
                                 assert(f->in_buffer_full >= (size_t) k);
@@ -201,7 +255,7 @@ static int shovel(PTYForward *f) {
                                         f->master_event_source = sd_event_source_unref(f->master_event_source);
                                 } else {
                                         log_error_errno(errno, "read(): %m");
-                                        return sd_event_exit(f->event, EXIT_FAILURE);
+                                        return pty_forward_done(f, -errno);
                                 }
                         }  else {
                                 f->read_from_master = true;
@@ -222,7 +276,7 @@ static int shovel(PTYForward *f) {
                                         f->stdout_event_source = sd_event_source_unref(f->stdout_event_source);
                                 } else {
                                         log_error_errno(errno, "write(): %m");
-                                        return sd_event_exit(f->event, EXIT_FAILURE);
+                                        return pty_forward_done(f, -errno);
                                 }
 
                         } else {
@@ -245,7 +299,7 @@ static int shovel(PTYForward *f) {
 
                 if ((f->out_buffer_full <= 0 || f->stdout_hangup) &&
                     (f->in_buffer_full <= 0 || f->master_hangup))
-                        return sd_event_exit(f->event, EXIT_SUCCESS);
+                        return pty_forward_done(f, 0);
         }
 
         return 0;
@@ -408,28 +462,8 @@ int pty_forward_new(
 }
 
 PTYForward *pty_forward_free(PTYForward *f) {
-
-        if (f) {
-                sd_event_source_unref(f->stdin_event_source);
-                sd_event_source_unref(f->stdout_event_source);
-                sd_event_source_unref(f->master_event_source);
-                sd_event_source_unref(f->sigwinch_event_source);
-                sd_event_unref(f->event);
-
-                if (f->saved_stdout)
-                        tcsetattr(STDOUT_FILENO, TCSANOW, &f->saved_stdout_attr);
-                if (f->saved_stdin)
-                        tcsetattr(STDIN_FILENO, TCSANOW, &f->saved_stdin_attr);
-
-                free(f);
-        }
-
-        /* STDIN/STDOUT should not be nonblocking normally, so let's
-         * unconditionally reset it */
-        fd_nonblock(STDIN_FILENO, false);
-        fd_nonblock(STDOUT_FILENO, false);
-
-        return NULL;
+        pty_forward_disconnect(f);
+        return mfree(f);
 }
 
 int pty_forward_get_last_char(PTYForward *f, char *ch) {
@@ -451,10 +485,7 @@ int pty_forward_set_ignore_vhangup(PTYForward *f, bool b) {
         if (!!(f->flags & PTY_FORWARD_IGNORE_VHANGUP) == b)
                 return 0;
 
-        if (b)
-                f->flags |= PTY_FORWARD_IGNORE_VHANGUP;
-        else
-                f->flags &= ~PTY_FORWARD_IGNORE_VHANGUP;
+        SET_FLAG(f->flags, PTY_FORWARD_IGNORE_VHANGUP, b);
 
         if (!ignore_vhangup(f)) {
 
@@ -470,8 +501,21 @@ int pty_forward_set_ignore_vhangup(PTYForward *f, bool b) {
         return 0;
 }
 
-int pty_forward_get_ignore_vhangup(PTYForward *f) {
+bool pty_forward_get_ignore_vhangup(PTYForward *f) {
         assert(f);
 
         return !!(f->flags & PTY_FORWARD_IGNORE_VHANGUP);
 }
+
+bool pty_forward_is_done(PTYForward *f) {
+        assert(f);
+
+        return f->done;
+}
+
+void pty_forward_set_handler(PTYForward *f, PTYForwardHandler cb, void *userdata) {
+        assert(f);
+
+        f->handler = cb;
+        f->userdata = userdata;
+}