[gnome-builder] subprocess: add take_stdin, take_stdout, and take_stderr



commit b8595040fd8182763dbdb5ade2e9ca65c60df25d
Author: Christian Hergert <chergert redhat com>
Date:   Thu Sep 15 17:46:12 2016 -0700

    subprocess: add take_stdin, take_stdout, and take_stderr
    
    These map to the same functions as GSubprocessLauncher and can be used
    to set the FDs for the given process.

 libide/subprocess/ide-subprocess-launcher.c |   83 +++++++++++++++++++++++++++
 libide/subprocess/ide-subprocess-launcher.h |    6 ++
 2 files changed, 89 insertions(+), 0 deletions(-)
---
diff --git a/libide/subprocess/ide-subprocess-launcher.c b/libide/subprocess/ide-subprocess-launcher.c
index be8eab0..2ab4941 100644
--- a/libide/subprocess/ide-subprocess-launcher.c
+++ b/libide/subprocess/ide-subprocess-launcher.c
@@ -41,6 +41,10 @@ typedef struct
   gchar            *cwd;
   GPtrArray        *environ;
 
+  gint              stdin_fd;
+  gint              stdout_fd;
+  gint              stderr_fd;
+
   guint             run_on_host : 1;
   guint             clear_env : 1;
 } IdeSubprocessLauncherPrivate;
@@ -243,6 +247,24 @@ ide_subprocess_launcher_spawn_worker (GTask        *task,
   g_subprocess_launcher_set_child_setup (launcher, child_setup_func, NULL, NULL);
   g_subprocess_launcher_set_cwd (launcher, priv->cwd);
 
+  if (priv->stdin_fd)
+    {
+      g_subprocess_launcher_take_stdin_fd (launcher, priv->stdin_fd);
+      priv->stdin_fd = -1;
+    }
+
+  if (priv->stdout_fd)
+    {
+      g_subprocess_launcher_take_stdout_fd (launcher, priv->stdout_fd);
+      priv->stdout_fd = -1;
+    }
+
+  if (priv->stderr_fd)
+    {
+      g_subprocess_launcher_take_stderr_fd (launcher, priv->stderr_fd);
+      priv->stderr_fd = -1;
+    }
+
   if (priv->environ->len > 1)
     {
       g_auto(GStrv) env = NULL;
@@ -359,6 +381,15 @@ ide_subprocess_launcher_finalize (GObject *object)
   g_clear_pointer (&priv->cwd, g_free);
   g_clear_pointer (&priv->environ, g_ptr_array_unref);
 
+  if (priv->stdin_fd != -1)
+    close (priv->stdin_fd);
+
+  if (priv->stdout_fd != -1)
+    close (priv->stdout_fd);
+
+  if (priv->stderr_fd != -1)
+    close (priv->stderr_fd);
+
   G_OBJECT_CLASS (ide_subprocess_launcher_parent_class)->finalize (object);
 }
 
@@ -491,6 +522,10 @@ ide_subprocess_launcher_init (IdeSubprocessLauncher *self)
 
   priv->clear_env = TRUE;
 
+  priv->stdin_fd = -1;
+  priv->stdout_fd = -1;
+  priv->stderr_fd = -1;
+
   priv->environ = g_ptr_array_new_with_free_func (g_free);
   g_ptr_array_add (priv->environ, NULL);
 
@@ -817,3 +852,51 @@ ide_subprocess_launcher_set_clear_env (IdeSubprocessLauncher *self,
       g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_CLEAR_ENV]);
     }
 }
+
+void
+ide_subprocess_launcher_take_stdin_fd (IdeSubprocessLauncher *self,
+                                       gint                   stdin_fd)
+{
+  IdeSubprocessLauncherPrivate *priv = ide_subprocess_launcher_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_SUBPROCESS_LAUNCHER (self));
+
+  if (priv->stdin_fd != stdin_fd)
+    {
+      if (priv->stdin_fd != -1)
+        close (priv->stdin_fd);
+      priv->stdin_fd = stdin_fd;
+    }
+}
+
+void
+ide_subprocess_launcher_take_stdout_fd (IdeSubprocessLauncher *self,
+                                        gint                   stdout_fd)
+{
+  IdeSubprocessLauncherPrivate *priv = ide_subprocess_launcher_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_SUBPROCESS_LAUNCHER (self));
+
+  if (priv->stdout_fd != stdout_fd)
+    {
+      if (priv->stdout_fd != -1)
+        close (priv->stdout_fd);
+      priv->stdout_fd = stdout_fd;
+    }
+}
+
+void
+ide_subprocess_launcher_take_stderr_fd (IdeSubprocessLauncher *self,
+                                        gint                   stderr_fd)
+{
+  IdeSubprocessLauncherPrivate *priv = ide_subprocess_launcher_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_SUBPROCESS_LAUNCHER (self));
+
+  if (priv->stderr_fd != stderr_fd)
+    {
+      if (priv->stderr_fd != -1)
+        close (priv->stderr_fd);
+      priv->stderr_fd = stderr_fd;
+    }
+}
diff --git a/libide/subprocess/ide-subprocess-launcher.h b/libide/subprocess/ide-subprocess-launcher.h
index 4b2e6af..27ed5db 100644
--- a/libide/subprocess/ide-subprocess-launcher.h
+++ b/libide/subprocess/ide-subprocess-launcher.h
@@ -91,6 +91,12 @@ void                   ide_subprocess_launcher_spawn_async         (IdeSubproces
 IdeSubprocess         *ide_subprocess_launcher_spawn_finish        (IdeSubprocessLauncher  *self,
                                                                     GAsyncResult           *result,
                                                                     GError                **error);
+void                   ide_subprocess_launcher_take_stdin_fd       (IdeSubprocessLauncher  *self,
+                                                                    gint                    stdin_fd);
+void                   ide_subprocess_launcher_take_stdout_fd      (IdeSubprocessLauncher  *self,
+                                                                    gint                    stdout_fd);
+void                   ide_subprocess_launcher_take_stderr_fd      (IdeSubprocessLauncher  *self,
+                                                                    gint                    stderr_fd);
 
 G_END_DECLS
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]