[libgsystem] Import GSubprocess as GSSubprocess



commit c17376d4acbddfa1909d24167d3ce24531b3db1a
Author: Colin Walters <walters verbum org>
Date:   Sat Dec 1 16:39:07 2012 -0500

    Import GSubprocess as GSSubprocess
    
    Since it's stalled getting into GLib, let's import it here.

 Makefile-libgsystem.am               |    5 +
 gsystem-subprocess-context-private.h |   62 +++
 gsystem-subprocess-context.c         |  351 ++++++++++++++
 gsystem-subprocess-context.h         |  113 +++++
 gsystem-subprocess.c                 |  865 ++++++++++++++++++++++++++++++++++
 gsystem-subprocess.h                 |   86 ++++
 libgsystem.h                         |    1 +
 7 files changed, 1483 insertions(+), 0 deletions(-)
---
diff --git a/Makefile-libgsystem.am b/Makefile-libgsystem.am
index 3e38f78..adc997f 100644
--- a/Makefile-libgsystem.am
+++ b/Makefile-libgsystem.am
@@ -24,6 +24,11 @@ libgsystem_la_SOURCES = \
 	$(libgsystem_srcpath)/gsystem-file-utils.c \
 	$(libgsystem_srcpath)/gsystem-shutil.h \
 	$(libgsystem_srcpath)/gsystem-shutil.c \
+	$(libgsystem_srcpath)/gsystem-subprocess-context.h \
+	$(libgsystem_srcpath)/gsystem-subprocess-context-private.h \
+	$(libgsystem_srcpath)/gsystem-subprocess-context.c \
+	$(libgsystem_srcpath)/gsystem-subprocess.h \
+	$(libgsystem_srcpath)/gsystem-subprocess.c \
 	$(libgsystem_srcpath)/libgsystem.h \
 	$(NULL)
 libgsystem_la_CFLAGS = $(AM_CFLAGS) $(libgsystem_cflags)
diff --git a/gsystem-subprocess-context-private.h b/gsystem-subprocess-context-private.h
new file mode 100644
index 0000000..18cb0ca
--- /dev/null
+++ b/gsystem-subprocess-context-private.h
@@ -0,0 +1,62 @@
+/* GIO - GLib Input, Output and Streaming Library
+ * 
+ * Copyright (C) 2012 Colin Walters <walters verbum org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GS_SUBPROCESS_CONTEXT_PRIVATE_H__
+#define __GS_SUBPROCESS_CONTEXT_PRIVATE_H__
+
+#include "gsystem-subprocess-context.h"
+
+G_BEGIN_DECLS
+
+struct _GSSubprocessContext
+{
+  GObject parent;
+
+  GSpawnFlags flags;
+  gchar **argv;
+  gboolean has_argv0;
+  char **envp;
+  char *cwd;
+
+  GSSubprocessStreamDisposition stdin_disposition;
+  GSSubprocessStreamDisposition stdout_disposition;
+  GSSubprocessStreamDisposition stderr_disposition;
+
+  guint keep_descriptors : 1;
+  guint search_path : 1;
+  guint search_path_from_envp : 1;
+  guint unused_flags : 29;
+
+  gint stdin_fd;
+  gchar *stdin_path;
+
+  gint stdout_fd;
+  gchar *stdout_path;
+
+  gint stderr_fd;
+  gchar *stderr_path;
+
+  GSpawnChildSetupFunc child_setup_func;
+  gpointer child_setup_data;
+};
+
+G_END_DECLS
+
+#endif
diff --git a/gsystem-subprocess-context.c b/gsystem-subprocess-context.c
new file mode 100644
index 0000000..67bd3db
--- /dev/null
+++ b/gsystem-subprocess-context.c
@@ -0,0 +1,351 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2012 Colin Walters <walters verbum org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+
+#include "libgsystem.h"
+
+/**
+ * SECTION:gssubprocess
+ * @title: GSSubprocess Context
+ * @short_description: Environment options for launching a child process
+ *
+ * This class contains a set of options for launching child processes,
+ * such as where its standard input and output will be directed, the
+ * argument list, the environment, and more.
+ *
+ * While the #GSSubprocess class has high level functions covering
+ * popular cases, use of this class allows access to more advanced
+ * options.  It can also be used to launch multiple subprocesses with
+ * a similar configuration.
+ *
+ * Since: 2.36
+ */
+
+#include "config.h"
+
+#include "gsystem-subprocess-context-private.h"
+#include "gsystem-subprocess.h"
+
+#include <string.h>
+
+typedef GObjectClass GSSubprocessContextClass;
+
+G_DEFINE_TYPE (GSSubprocessContext, gs_subprocess_context, G_TYPE_OBJECT);
+
+enum
+{
+  PROP_0,
+  PROP_ARGV,
+  N_PROPS
+};
+
+static GParamSpec *gs_subprocess_context_pspecs[N_PROPS];
+
+GSSubprocessContext *
+gs_subprocess_context_new (gchar           **argv)
+{
+  g_return_val_if_fail (argv != NULL && argv[0] != NULL, NULL);
+
+  return g_object_new (GS_TYPE_SUBPROCESS_CONTEXT,
+		       "argv", argv,
+		       NULL);
+}
+
+GSSubprocessContext *
+gs_subprocess_context_newv (const gchar  *first_arg,
+                           ...)
+{
+  GSSubprocessContext *result;
+  va_list args;
+
+  g_return_val_if_fail (first_arg != NULL, NULL);
+
+  va_start (args, first_arg);
+  result = gs_subprocess_context_newa (first_arg, args);
+  va_end (args);
+  
+  return result;
+}
+
+GSSubprocessContext *
+gs_subprocess_context_newa (const gchar *first_arg,
+                           va_list      args)
+{
+  GSSubprocessContext *result;
+  GPtrArray *argv;
+
+  g_return_val_if_fail (first_arg != NULL, NULL);
+
+  argv = g_ptr_array_new ();
+  do
+    g_ptr_array_add (argv, (gchar*)first_arg);
+  while ((first_arg = va_arg (args, const gchar *)) != NULL);
+  g_ptr_array_add (argv, NULL);
+
+  result = gs_subprocess_context_new ((gchar**)argv->pdata);
+  
+  return result;
+}
+
+#ifdef G_OS_UNIX
+GSSubprocessContext *
+gs_subprocess_context_new_argv0 (const gchar      *argv0,
+                                gchar           **argv)
+{
+  GSSubprocessContext *result;
+  GPtrArray *real_argv;
+  gchar **iter;
+  
+  g_return_val_if_fail (argv0 != NULL, NULL);
+  g_return_val_if_fail (argv != NULL && argv[0] != NULL, NULL);
+  
+  real_argv = g_ptr_array_new ();
+  g_ptr_array_add (real_argv, (gchar*)argv0);
+  for (iter = argv; *iter; iter++)
+    g_ptr_array_add (real_argv, (gchar*) *iter);
+  g_ptr_array_add (real_argv, NULL);
+
+  result = g_object_new (GS_TYPE_SUBPROCESS_CONTEXT,
+                         "argv", real_argv->pdata,
+                         NULL);
+  result->has_argv0 = TRUE;
+
+  return result;
+}
+#endif
+
+static void
+gs_subprocess_context_init (GSSubprocessContext  *self)
+{
+  self->stdin_fd = -1;
+  self->stdout_fd = -1;
+  self->stderr_fd = -1;
+  self->stdout_disposition = GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT;
+  self->stderr_disposition = GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT;
+}
+
+static void
+gs_subprocess_context_finalize (GObject *object)
+{
+  GSSubprocessContext *self = GS_SUBPROCESS_CONTEXT (object);
+
+  g_strfreev (self->argv);
+  g_strfreev (self->envp);
+  g_free (self->cwd);
+
+  g_free (self->stdin_path);
+  g_free (self->stdout_path);
+  g_free (self->stderr_path);
+
+  if (G_OBJECT_CLASS (gs_subprocess_context_parent_class)->finalize != NULL)
+    G_OBJECT_CLASS (gs_subprocess_context_parent_class)->finalize (object);
+}
+
+static void
+gs_subprocess_context_set_property (GObject      *object,
+				   guint         prop_id,
+				   const GValue *value,
+				   GParamSpec   *pspec)
+{
+  GSSubprocessContext *self = GS_SUBPROCESS_CONTEXT (object);
+
+  switch (prop_id)
+    {
+    case PROP_ARGV:
+      self->argv = (gchar**) g_value_dup_boxed (value);
+      break;
+
+    default:
+      g_assert_not_reached ();
+    }
+}
+
+static void
+gs_subprocess_context_get_property (GObject    *object,
+				   guint       prop_id,
+				   GValue     *value,
+				   GParamSpec *pspec)
+{
+  GSSubprocessContext *self = GS_SUBPROCESS_CONTEXT (object);
+
+  switch (prop_id)
+    {
+    case PROP_ARGV:
+      g_value_set_boxed (value, self->argv);
+      break;
+
+    default:
+      g_assert_not_reached ();
+    }
+}
+
+static void
+gs_subprocess_context_class_init (GSSubprocessContextClass *class)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
+
+  gobject_class->finalize = gs_subprocess_context_finalize;
+  gobject_class->get_property = gs_subprocess_context_get_property;
+  gobject_class->set_property = gs_subprocess_context_set_property;
+
+  /**
+   * GSSubprocessContext:argv:
+   *
+   * Array of arguments passed to child process; must have at least
+   * one element.  The first element has special handling - if it is
+   * an not absolute path ( as determined by g_path_is_absolute() ),
+   * then the system search path will be used.  See
+   * %G_SPAWN_SEARCH_PATH.
+   * 
+   * Note that in order to use the Unix-specific argv0 functionality,
+   * you must use the setter function
+   * gs_subprocess_context_set_args_and_argv0().  For more information
+   * about this, see %G_SPAWN_FILE_AND_ARGV_ZERO.
+   *
+   * Since: 2.36
+   */
+  gs_subprocess_context_pspecs[PROP_ARGV] = g_param_spec_boxed ("argv", "Arguments", "Arguments for child process", G_TYPE_STRV,
+							       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+
+  g_object_class_install_properties (gobject_class, N_PROPS, gs_subprocess_context_pspecs);
+}
+
+/* Environment */
+
+void
+gs_subprocess_context_set_environment (GSSubprocessContext           *self,
+				      gchar                       **environ)
+{
+  g_strfreev (self->envp);
+  self->envp = g_strdupv (environ);
+}
+
+void
+gs_subprocess_context_set_cwd (GSSubprocessContext           *self,
+			      const gchar                  *cwd)
+{
+  g_free (self->cwd);
+  self->cwd = g_strdup (cwd);
+}
+
+void
+gs_subprocess_context_set_keep_descriptors (GSSubprocessContext           *self,
+					   gboolean                      keep_descriptors)
+
+{
+  self->keep_descriptors = keep_descriptors ? 1 : 0;
+}
+
+void
+gs_subprocess_context_set_search_path (GSSubprocessContext           *self,
+				      gboolean                      search_path,
+				      gboolean                      search_path_from_envp)
+{
+  self->search_path = search_path ? 1 : 0;
+  self->search_path_from_envp = search_path_from_envp ? 1 : 0;
+}
+
+void
+gs_subprocess_context_set_stdin_disposition (GSSubprocessContext           *self,
+					    GSSubprocessStreamDisposition  disposition)
+{
+  g_return_if_fail (disposition != GS_SUBPROCESS_STREAM_DISPOSITION_STDERR_MERGE);
+  self->stdin_disposition = disposition;
+}
+
+void
+gs_subprocess_context_set_stdout_disposition (GSSubprocessContext           *self,
+					     GSSubprocessStreamDisposition  disposition)
+{
+  g_return_if_fail (disposition != GS_SUBPROCESS_STREAM_DISPOSITION_STDERR_MERGE);
+  self->stdout_disposition = disposition;
+}
+
+void
+gs_subprocess_context_set_stderr_disposition (GSSubprocessContext           *self,
+					     GSSubprocessStreamDisposition  disposition)
+{
+  self->stderr_disposition = disposition;
+}
+
+#ifdef G_OS_UNIX
+void
+gs_subprocess_context_set_stdin_file_path (GSSubprocessContext           *self,
+					  const gchar                  *path)
+{
+  self->stdin_disposition = GS_SUBPROCESS_STREAM_DISPOSITION_NULL;
+  g_free (self->stdin_path);
+  self->stdin_path = g_strdup (path);
+}
+
+void
+gs_subprocess_context_set_stdin_fd        (GSSubprocessContext           *self,
+					  gint                          fd)
+{
+  self->stdin_disposition = GS_SUBPROCESS_STREAM_DISPOSITION_NULL;
+  self->stdin_fd = fd;
+}
+
+void
+gs_subprocess_context_set_stdout_file_path (GSSubprocessContext           *self,
+					   const gchar                  *path)
+{
+  self->stdout_disposition = GS_SUBPROCESS_STREAM_DISPOSITION_NULL;
+  g_free (self->stdout_path);
+  self->stdout_path = g_strdup (path);
+}
+
+void
+gs_subprocess_context_set_stdout_fd (GSSubprocessContext           *self,
+				    gint                          fd)
+{
+  self->stdout_disposition = GS_SUBPROCESS_STREAM_DISPOSITION_NULL;
+  self->stdout_fd = fd;
+}
+
+void
+gs_subprocess_context_set_stderr_file_path (GSSubprocessContext           *self,
+					   const gchar                  *path)
+{
+  self->stderr_disposition = GS_SUBPROCESS_STREAM_DISPOSITION_NULL;
+  g_free (self->stderr_path);
+  self->stderr_path = g_strdup (path);
+}
+
+void
+gs_subprocess_context_set_stderr_fd        (GSSubprocessContext           *self,
+					   gint                          fd)
+{
+  self->stderr_disposition = GS_SUBPROCESS_STREAM_DISPOSITION_NULL;
+  self->stderr_fd = fd;
+}
+#endif
+
+#ifdef G_OS_UNIX
+void
+gs_subprocess_context_set_child_setup (GSSubprocessContext           *self,
+				      GSpawnChildSetupFunc          child_setup,
+				      gpointer                      user_data)
+{
+  self->child_setup_func = child_setup;
+  self->child_setup_data = user_data;
+}
+#endif
diff --git a/gsystem-subprocess-context.h b/gsystem-subprocess-context.h
new file mode 100644
index 0000000..1f57e03
--- /dev/null
+++ b/gsystem-subprocess-context.h
@@ -0,0 +1,113 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2012 Colin Walters <walters verbum org>.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GSYSTEM_SUBPROCESS_CONTEXT_H__
+#define __GSYSTEM_SUBPROCESS_CONTEXT_H__
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define GS_TYPE_SUBPROCESS_CONTEXT         (gs_subprocess_context_get_type ())
+#define GS_SUBPROCESS_CONTEXT(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GS_TYPE_SUBPROCESS_CONTEXT, GSSubprocessContext))
+#define GS_IS_SUBPROCESS_CONTEXT(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GS_TYPE_SUBPROCESS_CONTEXT))
+
+typedef struct _GSSubprocessContext GSSubprocessContext;
+
+/**
+ * GSSubprocessStreamDisposition:
+ * @GS_SUBPROCESS_STREAM_DISPOSITION_NULL: Redirect to operating system's null output stream
+ * @GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT: Keep the stream from the parent process
+ * @GS_SUBPROCESS_STREAM_DISPOSITION_PIPE: Open a private unidirectional channel between the processes
+ * @GS_SUBPROCESS_STREAM_DISPOSITION_STDERR_MERGE: Only applicable to standard error; causes it to be merged with standard output
+ *
+ * Flags to define the behaviour of the standard input/output/error of
+ * a #GSSubprocess.
+ *
+ * Since: 2.36
+ **/
+typedef enum {
+  GS_SUBPROCESS_STREAM_DISPOSITION_NULL,
+  GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
+  GS_SUBPROCESS_STREAM_DISPOSITION_PIPE,
+  GS_SUBPROCESS_STREAM_DISPOSITION_STDERR_MERGE
+} GSSubprocessStreamDisposition;
+
+GType            gs_subprocess_context_get_type (void) G_GNUC_CONST;
+
+GSSubprocessContext * gs_subprocess_context_new (gchar           **argv);
+GSSubprocessContext * gs_subprocess_context_newv (const gchar  *first_arg,
+                                                ...);
+GSSubprocessContext * gs_subprocess_context_newa (const gchar  *first_arg,
+                                                va_list       args);
+
+#ifdef G_OS_UNIX
+GSSubprocessContext * gs_subprocess_context_new_argv0 (const gchar   *argv0,
+                                                     gchar        **argv);
+#endif
+
+/* Environment */
+
+void             gs_subprocess_context_set_environment (GSSubprocessContext           *self,
+						       gchar                       **environ);
+void             gs_subprocess_context_set_cwd (GSSubprocessContext           *self,
+					       const gchar                  *cwd);
+void             gs_subprocess_context_set_keep_descriptors (GSSubprocessContext           *self,
+							    gboolean                      keep_descriptors);
+void             gs_subprocess_context_set_search_path (GSSubprocessContext           *self,
+						       gboolean                      search_path,
+						       gboolean                      search_path_from_envp);
+
+/* Basic I/O control */
+
+void             gs_subprocess_context_set_stdin_disposition (GSSubprocessContext           *self,
+							     GSSubprocessStreamDisposition  disposition);
+void             gs_subprocess_context_set_stdout_disposition (GSSubprocessContext           *self,
+							      GSSubprocessStreamDisposition  disposition);
+void             gs_subprocess_context_set_stderr_disposition (GSSubprocessContext           *self,
+							      GSSubprocessStreamDisposition  disposition);
+
+/* Extended I/O control, only available on UNIX */
+
+#ifdef G_OS_UNIX
+void             gs_subprocess_context_set_stdin_file_path (GSSubprocessContext           *self,
+							   const gchar                  *path);
+void             gs_subprocess_context_set_stdin_fd        (GSSubprocessContext           *self,
+							   gint                          fd);
+void             gs_subprocess_context_set_stdout_file_path (GSSubprocessContext           *self,
+							    const gchar                  *path);
+void             gs_subprocess_context_set_stdout_fd        (GSSubprocessContext           *self,
+							    gint                          fd);
+void             gs_subprocess_context_set_stderr_file_path (GSSubprocessContext           *self,
+							    const gchar                  *path);
+void             gs_subprocess_context_set_stderr_fd        (GSSubprocessContext           *self,
+							    gint                          fd);
+#endif
+
+/* Child setup, only available on UNIX */
+#ifdef G_OS_UNIX
+void             gs_subprocess_context_set_child_setup        (GSSubprocessContext           *self,
+							      GSpawnChildSetupFunc          child_setup,
+							      gpointer                      user_data);
+#endif
+
+G_END_DECLS
+
+#endif
diff --git a/gsystem-subprocess.c b/gsystem-subprocess.c
new file mode 100644
index 0000000..6de395b
--- /dev/null
+++ b/gsystem-subprocess.c
@@ -0,0 +1,865 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright  2012 Red Hat, Inc.
+ * Copyright  2012 Canonical Limited
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; either version 2 of the licence or (at
+ * your option) any later version.
+ *
+ * See the included COPYING file for more information.
+ *
+ * Authors: Colin Walters <walters verbum org>
+ *          Ryan Lortie <desrt desrt ca>
+ */
+
+#include "config.h"
+
+#include "libgsystem.h"
+
+/**
+ * SECTION:gssubprocess
+ * @title: GSSubprocess
+ * @short_description: Create child processes and monitor their status
+ *
+ * This class wraps the lower-level g_spawn_async_with_pipes() API,
+ * providing a more modern GIO-style API, such as returning
+ * #GInputStream objects for child output pipes.
+ *
+ * One major advantage that GIO brings over the core GLib library is
+ * comprehensive API for asynchronous I/O, such
+ * g_output_stream_splice_async().  This makes GSubprocess
+ * significantly more powerful and flexible than equivalent APIs in
+ * some other languages such as the <literal>subprocess.py</literal>
+ * included with Python.  For example, using #GSubprocess one could
+ * create two child processes, reading standard output from the first,
+ * processing it, and writing to the input stream of the second, all
+ * without blocking the main loop.
+ *
+ * Since: 2.36
+ */
+
+#include "config.h"
+
+#include "gsystem-subprocess.h"
+#include "gsystem-subprocess-context-private.h"
+
+#include <string.h>
+#ifdef G_OS_UNIX
+#include <gio/gunixoutputstream.h>
+#include <gio/gfiledescriptorbased.h>
+#include <gio/gunixinputstream.h>
+#include <glib-unix.h>
+#endif
+#include <fcntl.h>
+#ifdef G_OS_WIN32
+#define _WIN32_WINNT 0x0500
+#include <windows.h>
+#include "giowin32-priv.h"
+#endif
+
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+static void initable_iface_init (GInitableIface         *initable_iface);
+
+typedef GObjectClass GSSubprocessClass;
+
+#ifdef G_OS_UNIX
+static void
+gs_subprocess_unix_queue_waitpid (GSSubprocess  *self);
+#endif
+
+struct _GSSubprocess
+{
+  GObject parent;
+
+  GSSubprocessContext *context;
+  GPid pid;
+
+  guint pid_valid : 1;
+  guint reaped_child : 1;
+  guint unused : 30;
+
+  /* These are the streams created if a pipe is requested via flags. */
+  GOutputStream *stdin_pipe;
+  GInputStream  *stdout_pipe;
+  GInputStream  *stderr_pipe;
+};
+
+G_DEFINE_TYPE_WITH_CODE (GSSubprocess, gs_subprocess, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init));
+
+enum
+{
+  PROP_0,
+  PROP_CONTEXT,
+  N_PROPS
+};
+
+static GParamSpec *gs_subprocess_pspecs[N_PROPS];
+
+static void
+gs_subprocess_init (GSSubprocess  *self)
+{
+}
+
+static void
+gs_subprocess_finalize (GObject *object)
+{
+  GSSubprocess *self = GS_SUBPROCESS (object);
+
+  if (self->pid_valid)
+    {
+#ifdef G_OS_UNIX
+      /* Here we need to actually call waitpid() to clean up the
+       * zombie.  In case the child hasn't actually exited, defer this
+       * cleanup to the worker thread.
+       */
+      if (!self->reaped_child)
+        gs_subprocess_unix_queue_waitpid (self);
+#endif
+      g_spawn_close_pid (self->pid);
+    }
+
+  g_clear_object (&self->stdin_pipe);
+  g_clear_object (&self->stdout_pipe);
+  g_clear_object (&self->stderr_pipe);
+
+  if (G_OBJECT_CLASS (gs_subprocess_parent_class)->finalize != NULL)
+    G_OBJECT_CLASS (gs_subprocess_parent_class)->finalize (object);
+}
+
+static void
+gs_subprocess_set_property (GObject      *object,
+                           guint         prop_id,
+                           const GValue *value,
+                           GParamSpec   *pspec)
+{
+  GSSubprocess *self = GS_SUBPROCESS (object);
+
+  switch (prop_id)
+    {
+    case PROP_CONTEXT:
+      self->context = g_value_dup_object (value);
+      break;
+
+    default:
+      g_assert_not_reached ();
+    }
+}
+
+static void
+gs_subprocess_get_property (GObject    *object,
+                           guint       prop_id,
+                           GValue     *value,
+                           GParamSpec *pspec)
+{
+  GSSubprocess *self = GS_SUBPROCESS (object);
+
+  switch (prop_id)
+    {
+    case PROP_CONTEXT:
+      g_value_set_object (value, self->context);
+      break;
+
+    default:
+      g_assert_not_reached ();
+    }
+}
+
+static void
+gs_subprocess_class_init (GSSubprocessClass *class)
+{
+  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
+
+  gobject_class->finalize = gs_subprocess_finalize;
+  gobject_class->get_property = gs_subprocess_get_property;
+  gobject_class->set_property = gs_subprocess_set_property;
+
+  /**
+   * GSSubprocess:context:
+   *
+   *
+   * Since: 2.36
+   */
+  gs_subprocess_pspecs[PROP_CONTEXT] = g_param_spec_object ("context", "Context", "Subprocess options", GS_TYPE_SUBPROCESS_CONTEXT,
+							   G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
+							   G_PARAM_STATIC_STRINGS);
+
+  g_object_class_install_properties (gobject_class, N_PROPS, gs_subprocess_pspecs);
+}
+
+#ifdef G_OS_UNIX
+
+static gboolean
+gs_subprocess_unix_waitpid_dummy (gpointer data)
+{
+  return FALSE;
+}
+
+static void
+gs_subprocess_unix_queue_waitpid (GSSubprocess  *self)
+{
+  GMainContext *worker_context;
+  GSource *waitpid_source;
+
+#if GLIB_COMPILATION
+  worker_context = GLIB_PRIVATE_CALL (g_get_worker_context) ();
+#else
+  worker_context = g_main_context_get_thread_default ();
+#endif
+  waitpid_source = g_child_watch_source_new (self->pid); 
+  g_source_set_callback (waitpid_source, gs_subprocess_unix_waitpid_dummy, NULL, NULL);
+  g_source_attach (waitpid_source, worker_context);
+  g_source_unref (waitpid_source);
+}
+
+#endif
+
+static GInputStream *
+platform_input_stream_from_spawn_fd (gint fd)
+{
+  if (fd < 0)
+    return NULL;
+
+#ifdef G_OS_UNIX
+  return g_unix_input_stream_new (fd, TRUE);
+#else
+  return g_win32_input_stream_new_from_fd (fd, TRUE);
+#endif
+}
+
+static GOutputStream *
+platform_output_stream_from_spawn_fd (gint fd)
+{
+  if (fd < 0)
+    return NULL;
+
+#ifdef G_OS_UNIX
+  return g_unix_output_stream_new (fd, TRUE);
+#else
+  return g_win32_output_stream_new_from_fd (fd, TRUE);
+#endif
+}
+
+#ifdef G_OS_UNIX
+static gint
+unix_open_file (const char  *filename,
+                gint         mode,
+                GError     **error)
+{
+  gint my_fd;
+
+  do
+    my_fd = open (filename, mode | O_BINARY | O_CLOEXEC, 0666);
+  while (my_fd == -1 && errno == EINTR);
+
+  /* If we return -1 we should also set the error */
+  if (my_fd < 0)
+    {
+      gint saved_errno = errno;
+      char *display_name;
+
+      display_name = g_filename_display_name (filename);
+      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (saved_errno),
+                   "Error opening file '%s': %s", display_name,
+                   g_strerror (saved_errno));
+      g_free (display_name);
+      /* fall through... */
+    }
+
+  return my_fd;
+}
+#endif
+
+typedef struct
+{
+  gint             fds[3];
+  GSpawnChildSetupFunc child_setup_func;
+  gpointer             child_setup_data;
+} ChildData;
+
+static void
+child_setup (gpointer user_data)
+{
+  ChildData *child_data = user_data;
+  gint i;
+
+  /* We're on the child side now.  "Rename" the file descriptors in
+   * child_data.fds[] to stdin/stdout/stderr.
+   *
+   * We don't close the originals.  It's possible that the originals
+   * should not be closed and if they should be closed then they should
+   * have been created O_CLOEXEC.
+   */
+  for (i = 0; i < 3; i++)
+    if (child_data->fds[i] != -1 && child_data->fds[i] != i)
+      {
+        gint result;
+
+        do
+          result = dup2 (child_data->fds[i], i);
+        while (result == -1 && errno == EINTR);
+      }
+
+  if (child_data->child_setup_func)
+    child_data->child_setup_func (child_data->child_setup_data);
+}
+
+static gboolean
+initable_init (GInitable     *initable,
+               GCancellable  *cancellable,
+               GError       **error)
+{
+  GSSubprocess *self = GS_SUBPROCESS (initable);
+  ChildData child_data = { { -1, -1, -1 } };
+  gint *pipe_ptrs[3] = { NULL, NULL, NULL };
+  gint pipe_fds[3] = { -1, -1, -1 };
+  gint close_fds[3] = { -1, -1, -1 };
+  GSpawnFlags spawn_flags = 0;
+  gboolean success = FALSE;
+  gint i;
+
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    return FALSE;
+
+  /* We must setup the three fds that will end up in the child as stdin,
+   * stdout and stderr.
+   *
+   * First, stdin.
+   */
+#ifdef G_OS_UNIX
+  if (self->context->stdin_fd != -1)
+    child_data.fds[0] = self->context->stdin_fd;
+  else if (self->context->stdin_path != NULL)
+    {
+      child_data.fds[0] = close_fds[0] = unix_open_file (self->context->stdin_path, 
+							 O_RDONLY, error);
+      if (child_data.fds[0] == -1)
+	goto out;
+    }
+  else
+#endif
+  if (self->context->stdin_disposition == GS_SUBPROCESS_STREAM_DISPOSITION_NULL)
+    ; /* nothing */
+  else if (self->context->stdin_disposition == GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT)
+    spawn_flags |= G_SPAWN_CHILD_INHERITS_STDIN;
+  else if (self->context->stdin_disposition == GS_SUBPROCESS_STREAM_DISPOSITION_PIPE)
+    pipe_ptrs[0] = &pipe_fds[0];
+  else
+    g_assert_not_reached ();
+
+  /* Next, stdout. */
+#ifdef G_OS_UNIX
+  if (self->context->stdout_fd != -1)
+    child_data.fds[1] = self->context->stdout_fd;
+  else if (self->context->stdout_path != NULL)
+    {
+      child_data.fds[1] = close_fds[1] = unix_open_file (self->context->stdout_path, 
+							 O_CREAT | O_WRONLY, error);
+      if (child_data.fds[1] == -1)
+	goto out;
+    }
+  else
+#endif
+  if (self->context->stdout_disposition == GS_SUBPROCESS_STREAM_DISPOSITION_NULL)
+    spawn_flags |= G_SPAWN_STDOUT_TO_DEV_NULL;
+  else if (self->context->stdout_disposition == GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT)
+    ; /* Nothing */
+  else if (self->context->stdout_disposition == GS_SUBPROCESS_STREAM_DISPOSITION_PIPE)
+    pipe_ptrs[1] = &pipe_fds[1];
+  else
+    g_assert_not_reached ();
+
+  /* Finally, stderr. */
+#ifdef G_OS_UNIX
+  if (self->context->stderr_fd != -1)
+    child_data.fds[2] = self->context->stderr_fd;
+  else if (self->context->stderr_path != NULL)
+    {
+      child_data.fds[2] = close_fds[2] = unix_open_file (self->context->stderr_path, 
+							 O_CREAT | O_WRONLY, error);
+      if (child_data.fds[2] == -1)
+	goto out;
+    }
+  else
+#endif
+  if (self->context->stderr_disposition == GS_SUBPROCESS_STREAM_DISPOSITION_NULL)
+    spawn_flags |= G_SPAWN_STDERR_TO_DEV_NULL;
+  else if (self->context->stderr_disposition == GS_SUBPROCESS_STREAM_DISPOSITION_INHERIT)
+    ; /* Nothing */
+  else if (self->context->stderr_disposition == GS_SUBPROCESS_STREAM_DISPOSITION_PIPE)
+    pipe_ptrs[2] = &pipe_fds[2];
+  else if (self->context->stderr_disposition == GS_SUBPROCESS_STREAM_DISPOSITION_STDERR_MERGE)
+    /* This will work because stderr gets setup after stdout. */
+    child_data.fds[2] = 1;
+  else
+    g_assert_not_reached ();
+
+  if (self->context->keep_descriptors)
+    spawn_flags |= G_SPAWN_LEAVE_DESCRIPTORS_OPEN;
+
+  if (self->context->search_path)
+    spawn_flags |= G_SPAWN_SEARCH_PATH;
+  else if (self->context->search_path_from_envp)
+    spawn_flags |= G_SPAWN_SEARCH_PATH_FROM_ENVP;
+  else if (!g_path_is_absolute (((gchar**)self->context->argv)[0]))
+    spawn_flags |= G_SPAWN_SEARCH_PATH;
+
+  if (self->context->has_argv0)
+    spawn_flags |= G_SPAWN_FILE_AND_ARGV_ZERO;
+
+  spawn_flags |= G_SPAWN_DO_NOT_REAP_CHILD;
+#ifdef GLIB_COMPILATION
+  spawn_flags |= G_SPAWN_CLOEXEC_PIPES;
+#endif
+
+  child_data.child_setup_func = self->context->child_setup_func;
+  child_data.child_setup_data = self->context->child_setup_data;
+  success = g_spawn_async_with_pipes (self->context->cwd,
+				      (char**)self->context->argv,
+				      self->context->envp,
+                                      spawn_flags,
+                                      child_setup, &child_data,
+                                      &self->pid,
+                                      pipe_ptrs[0], pipe_ptrs[1], pipe_ptrs[2],
+                                      error);
+  if (success)
+    self->pid_valid = TRUE;
+
+out:
+  for (i = 0; i < 3; i++)
+    if (close_fds[i] != -1)
+      close (close_fds[i]);
+
+  self->stdin_pipe = platform_output_stream_from_spawn_fd (pipe_fds[0]);
+  self->stdout_pipe = platform_input_stream_from_spawn_fd (pipe_fds[1]);
+  self->stderr_pipe = platform_input_stream_from_spawn_fd (pipe_fds[2]);
+
+  return success;
+}
+
+static void
+initable_iface_init (GInitableIface *initable_iface)
+{
+  initable_iface->init = initable_init;
+}
+
+/**
+ * gs_subprocess_new:
+ *
+ * Create a new process, using the parameters specified by
+ * GSSubprocessContext.
+ *
+ * Returns: (transfer full): A newly created %GSSubprocess, or %NULL on error (and @error will be set)
+ *
+ * Since: 2.36
+ */
+GSSubprocess *
+gs_subprocess_new (GSSubprocessContext   *context,
+		  GError              **error)
+{
+  return g_initable_new (GS_TYPE_SUBPROCESS,
+                         NULL, error,
+                         "context", context,
+                         NULL);
+}
+
+/**
+ * gs_subprocess_get_pid:
+ * @self: a #GSSubprocess
+ *
+ * The identifier for this child process; it is valid as long as the
+ * process @self is referenced.  In particular, do
+ * <emphasis>not</emphasis> call g_spawn_close_pid() on this value;
+ * that is handled internally.
+ *
+ * On some Unix versions, it is possible for there to be a race
+ * condition where waitpid() may have been called to collect the child
+ * before any watches (such as that installed by
+ * gs_subprocess_add_watch()) have fired.  If you are planning to use
+ * native functions such as kill() on the pid, your program should
+ * gracefully handle an %ESRCH result to mitigate this.
+ *
+ * If you want to request process termination, using the high level
+ * gs_subprocess_request_exit() and gs_subprocess_force_exit() API is
+ * recommended.
+ *
+ * Returns: Operating-system specific identifier for child process
+ *
+ * Since: 2.36
+ */
+GPid
+gs_subprocess_get_pid (GSSubprocess     *self)
+{
+  g_return_val_if_fail (GS_IS_SUBPROCESS (self), 0);
+
+  return self->pid;
+}
+
+GOutputStream *
+gs_subprocess_get_stdin_pipe (GSSubprocess       *self)
+{
+  g_return_val_if_fail (GS_IS_SUBPROCESS (self), NULL);
+  g_return_val_if_fail (self->stdin_pipe, NULL);
+
+  return self->stdin_pipe;
+}
+
+GInputStream *
+gs_subprocess_get_stdout_pipe (GSSubprocess      *self)
+{
+  g_return_val_if_fail (GS_IS_SUBPROCESS (self), NULL);
+  g_return_val_if_fail (self->stdout_pipe, NULL);
+
+  return self->stdout_pipe;
+}
+
+GInputStream *
+gs_subprocess_get_stderr_pipe (GSSubprocess      *self)
+{
+  g_return_val_if_fail (GS_IS_SUBPROCESS (self), NULL);
+  g_return_val_if_fail (self->stderr_pipe, NULL);
+
+  return self->stderr_pipe;
+}
+
+typedef struct {
+  GSSubprocess *self;
+  GCancellable *cancellable;
+  GSimpleAsyncResult *result;
+} GSSubprocessWatchData;
+
+static gboolean
+gs_subprocess_on_child_exited (GPid       pid,
+			      gint       status_code,
+			      gpointer   user_data)
+{
+  GSSubprocessWatchData *data = user_data;
+  GError *error = NULL;
+  
+  if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
+    {
+      g_simple_async_result_take_error (data->result, error);
+    }
+  else
+    {
+      data->self->reaped_child = TRUE;
+
+      g_simple_async_result_set_op_res_gssize (data->result, status_code);
+    }
+
+  g_simple_async_result_complete (data->result);
+
+  g_object_unref (data->result);
+  g_object_unref (data->self);
+  g_free (data);
+
+  return FALSE;
+}
+
+/**
+ * gs_subprocess_wait:
+ * @self: a #GSSubprocess
+ * @cancellable: a #GCancellable
+ * @callback: Invoked when process exits, or @cancellable is cancelled
+ * @user_data: Data for @callback
+ *
+ * Start an asynchronous wait for the subprocess @self to exit.
+ *
+ * Since: 2.36
+ */
+void
+gs_subprocess_wait (GSSubprocess                *self,
+		   GCancellable               *cancellable,
+		   GAsyncReadyCallback         callback,
+		   gpointer                    user_data)
+{
+  GSource *source;
+  GSSubprocessWatchData *data;
+
+  data = g_new0 (GSSubprocessWatchData, 1);
+
+  data->self = g_object_ref (self);
+  data->result = g_simple_async_result_new ((GObject*)self, callback, user_data,
+					    gs_subprocess_wait);
+
+  source = g_child_watch_source_new (self->pid);
+
+  g_source_set_callback (source, (GSourceFunc)gs_subprocess_on_child_exited,
+			 data, NULL);
+  if (cancellable)
+    {
+      GSource *cancellable_source;
+
+      data->cancellable = g_object_ref (cancellable);
+
+      cancellable_source = g_cancellable_source_new (cancellable);
+      g_source_add_child_source (source, cancellable_source);
+      g_source_unref (cancellable_source);
+    }
+
+  g_source_attach (source, g_main_context_get_thread_default ());
+  g_source_unref (source);
+}
+
+/**
+ * gs_subprocess_wait_finish:
+ * @self: a #GSSubprocess
+ * @result: a #GAsyncResult
+ * @out_exit_status: (out): Exit status of the process encoded in platform-specific way
+ * @error: a #GError
+ *
+ * The exit status of the process will be stored in @out_exit_status.
+ * See the documentation of g_spawn_check_exit_status() for more
+ * details.
+ *
+ * Note that @error is not set if the process exits abnormally; you
+ * must use g_spawn_check_exit_status() for that.
+ *
+ * Since: 2.36
+ */
+gboolean
+gs_subprocess_wait_finish (GSSubprocess                *self,
+			  GAsyncResult               *result,
+			  int                        *out_exit_status,
+			  GError                    **error)
+{
+  GSimpleAsyncResult *simple;
+
+  simple = G_SIMPLE_ASYNC_RESULT (result);
+
+  if (g_simple_async_result_propagate_error (simple, error))
+    return FALSE;
+
+  *out_exit_status = g_simple_async_result_get_op_res_gssize (simple);
+  
+  return TRUE;
+}
+
+typedef struct {
+  GMainLoop *loop;
+  gint *exit_status_ptr;
+  gboolean caught_error;
+  GError **error;
+} GSSubprocessSyncWaitData;
+
+static void
+gs_subprocess_on_sync_wait_complete (GObject       *object,
+				    GAsyncResult  *result,
+				    gpointer       user_data)
+{
+  GSSubprocessSyncWaitData *data = user_data;
+
+  if (!gs_subprocess_wait_finish ((GSSubprocess*)object, result, 
+				 data->exit_status_ptr, data->error))
+    data->caught_error = TRUE;
+
+  g_main_loop_quit (data->loop);
+}
+
+/**
+ * gs_subprocess_wait_sync:
+ * @self: a #GSSubprocess
+ * @out_exit_status: (out): Platform-specific exit code
+ * @cancellable: a #GCancellable
+ * @error: a #GError
+ *
+ * Synchronously wait for the subprocess to terminate, returning the
+ * status code in @out_exit_status.  See the documentation of
+ * g_spawn_check_exit_status() for how to interpret it.  Note that if
+ * @error is set, then @out_exit_status will be left uninitialized.
+ * 
+ * Returns: %TRUE on success, %FALSE if @cancellable was cancelled
+ *
+ * Since: 2.36
+ */
+gboolean
+gs_subprocess_wait_sync (GSSubprocess        *self,
+			int                *out_exit_status,
+			GCancellable       *cancellable,
+			GError            **error)
+{
+  gboolean ret = FALSE;
+  gboolean pushed_thread_default = FALSE;
+  GMainContext *context = NULL;
+  GSSubprocessSyncWaitData data;
+
+  memset (&data, 0, sizeof (data));
+
+  g_return_val_if_fail (GS_IS_SUBPROCESS (self), FALSE);
+
+  if (g_cancellable_set_error_if_cancelled (cancellable, error))
+    return FALSE;
+
+  context = g_main_context_new ();
+  g_main_context_push_thread_default (context);
+  pushed_thread_default = TRUE;
+
+  data.exit_status_ptr = out_exit_status;
+  data.loop = g_main_loop_new (context, TRUE);
+  data.error = error;
+
+  gs_subprocess_wait (self, cancellable,
+		     gs_subprocess_on_sync_wait_complete, &data);
+
+  g_main_loop_run (data.loop);
+
+  if (data.caught_error)
+    goto out;
+
+  ret = TRUE;
+ out:
+  if (pushed_thread_default)
+    g_main_context_pop_thread_default (context);
+  if (context)
+    g_main_context_unref (context);
+  if (data.loop)
+    g_main_loop_unref (data.loop);
+
+  return ret;
+}
+
+/**
+ * gs_subprocess_wait_sync_check:
+ * @self: a #GSSubprocess
+ * @cancellable: a #GCancellable
+ * @error: a #GError
+ *
+ * Combines gs_subprocess_wait_sync() with g_spawn_check_exit_status().
+ * 
+ * Returns: %TRUE on success, %FALSE if process exited abnormally, or @cancellable was cancelled
+ *
+ * Since: 2.36
+ */
+gboolean
+gs_subprocess_wait_sync_check (GSSubprocess        *self,
+			      GCancellable       *cancellable,
+			      GError            **error)
+{
+  gboolean ret = FALSE;
+  int exit_status;
+
+  if (!gs_subprocess_wait_sync (self, &exit_status, cancellable, error))
+    goto out;
+
+  if (!g_spawn_check_exit_status (exit_status, error))
+    goto out;
+
+  ret = TRUE;
+ out:
+  return ret;
+}
+
+/**
+ * gs_subprocess_request_exit:
+ * @self: a #GSSubprocess
+ *
+ * This API uses an operating-system specific mechanism to request
+ * that the subprocess gracefully exit.  This API is not available on
+ * all operating systems; for those not supported, it will do nothing
+ * and return %FALSE.  Portable code should handle this situation
+ * gracefully.  For example, if you are communicating via input or
+ * output pipe with the child, many programs will automatically exit
+ * when one of their standard input or output are closed.
+ *
+ * On Unix, this API sends %SIGTERM.
+ *
+ * A %TRUE return value does <emphasis>not</emphasis> mean the
+ * subprocess has exited, merely that an exit request was initiated.
+ * You can use gs_subprocess_add_watch() to monitor the status of the
+ * process after calling this function.
+ *
+ * This function returns %TRUE if the process has already exited.
+ *
+ * Returns: %TRUE if the operation is supported, %FALSE otherwise.
+ *
+ * Since: 2.36
+ */
+gboolean
+gs_subprocess_request_exit (GSSubprocess *self)
+{
+  g_return_val_if_fail (GS_IS_SUBPROCESS (self), FALSE);
+
+#ifdef G_OS_UNIX
+  (void) kill (self->pid, SIGTERM);
+  return TRUE;
+#else
+  return FALSE;
+#endif
+}
+
+/**
+ * gs_subprocess_force_exit:
+ * @self: a #GSSubprocess
+ *
+ * Use an operating-system specific method to attempt an immediate,
+ * forceful termination of the process.  There is no mechanism to
+ * determine whether or not the request itself was successful;
+ * however, you can use gs_subprocess_wait() to monitor the status of
+ * the process after calling this function.
+ *
+ * On Unix, this function sends %SIGKILL.
+ */
+void
+gs_subprocess_force_exit (GSSubprocess *self)
+{
+  g_return_if_fail (GS_IS_SUBPROCESS (self));
+
+#if !defined(GLIB_COMPIATION)
+  {
+    int ret;
+    do
+      ret = kill (self->pid, SIGKILL);
+    while (ret == -1 && errno == EINTR);
+  }
+#elif defined(G_OS_UNIX)
+  GLIB_PRIVATE_CALL (g_main_send_signal) (self->pid, SIGKILL);
+#else
+  TerminateProcess (self->pid, 1);
+#endif
+}
+
+GSSubprocess *
+gs_subprocess_new_simple_argl (GSSubprocessStreamDisposition stdout_disposition,
+			      GSSubprocessStreamDisposition stderr_disposition,
+			      GError                     **error,
+			      const gchar                 *first_arg,
+			      ...)
+{
+  va_list args;
+  GSSubprocess *result;
+  GSSubprocessContext *context;
+
+  va_start (args, first_arg);
+  context = gs_subprocess_context_newa (first_arg, args);
+  va_end (args);
+  result = gs_subprocess_new (context, error);
+  g_object_unref (context);
+  
+  return result;
+}
+
+GSSubprocess *
+gs_subprocess_new_simple_argv (gchar                      **argv,
+			      GSSubprocessStreamDisposition stdout_disposition,
+			      GSSubprocessStreamDisposition stderr_disposition,
+			      GError                     **error)
+{
+  GSSubprocessContext *context;
+  GSSubprocess *result;
+
+  context = gs_subprocess_context_new (argv);
+  gs_subprocess_context_set_stdout_disposition (context, stdout_disposition);
+  gs_subprocess_context_set_stderr_disposition (context, stderr_disposition);
+
+  result = gs_subprocess_new (context, error);
+  g_object_unref (context);
+
+  return result;
+}
diff --git a/gsystem-subprocess.h b/gsystem-subprocess.h
new file mode 100644
index 0000000..218f34a
--- /dev/null
+++ b/gsystem-subprocess.h
@@ -0,0 +1,86 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
+ *
+ * Copyright (C) 2012 Colin Walters <walters verbum org>.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __GSYSTEM_SUBPROCESS_H__
+#define __GSYSTEM_SUBPROCESS_H__
+
+#include "gsystem-subprocess-context.h"
+
+G_BEGIN_DECLS
+
+#define GS_TYPE_SUBPROCESS         (gs_subprocess_get_type ())
+#define GS_SUBPROCESS(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GS_TYPE_SUBPROCESS, GSSubprocess))
+#define GS_IS_SUBPROCESS(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GS_TYPE_SUBPROCESS))
+
+typedef struct _GSSubprocess GSSubprocess;
+
+GType            gs_subprocess_get_type (void) G_GNUC_CONST;
+
+/**** Core API ****/
+
+GSSubprocess *    gs_subprocess_new (GSSubprocessContext   *context,
+                                     GError               **error);
+
+GOutputStream *  gs_subprocess_get_stdin_pipe (GSSubprocess       *self);
+
+GInputStream *   gs_subprocess_get_stdout_pipe (GSSubprocess      *self);
+
+GInputStream *   gs_subprocess_get_stderr_pipe (GSSubprocess      *self);
+
+void             gs_subprocess_wait (GSSubprocess                *self,
+				    GCancellable               *cancellable,
+				    GAsyncReadyCallback         callback,
+				    gpointer                    user_data);
+
+gboolean         gs_subprocess_wait_finish (GSSubprocess                *self,
+					   GAsyncResult               *result,
+					   int                        *out_exit_status,
+					   GError                    **error);
+
+gboolean         gs_subprocess_wait_sync (GSSubprocess   *self,
+					 int           *out_exit_status,
+					 GCancellable  *cancellable,
+					 GError       **error);
+
+gboolean         gs_subprocess_wait_sync_check (GSSubprocess   *self,
+					       GCancellable  *cancellable,
+					       GError       **error);
+
+GPid             gs_subprocess_get_pid (GSSubprocess     *self);
+
+gboolean         gs_subprocess_request_exit (GSSubprocess       *self);
+
+void             gs_subprocess_force_exit (GSSubprocess       *self);
+
+/** High level helpers **/
+
+GSSubprocess *    gs_subprocess_new_simple_argl (GSSubprocessStreamDisposition stdout_disposition,
+					       GSSubprocessStreamDisposition stderr_disposition,
+					       GError                     **error,
+					       const char                  *first_arg,
+					       ...) G_GNUC_NULL_TERMINATED;
+GSSubprocess *    gs_subprocess_new_simple_argv (char                       **argv,
+					       GSSubprocessStreamDisposition stdout_disposition,
+					       GSSubprocessStreamDisposition stderr_disposition,
+					       GError                     **error);
+
+G_END_DECLS
+
+#endif
diff --git a/libgsystem.h b/libgsystem.h
index cde7d61..1102b63 100644
--- a/libgsystem.h
+++ b/libgsystem.h
@@ -28,6 +28,7 @@ G_BEGIN_DECLS
 #include <gsystem-local-alloc.h>
 #include <gsystem-file-utils.h>
 #include <gsystem-shutil.h>
+#include <gsystem-subprocess.h>
 
 G_END_DECLS
 



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