[glib/wip/gsubprocess-ostreams: 8/9] GSubprocess: New class for spawning child processes
- From: Colin Walters <walters src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/wip/gsubprocess-ostreams: 8/9] GSubprocess: New class for spawning child processes
- Date: Wed, 25 Jul 2012 23:24:23 +0000 (UTC)
commit 090cf3cee2e4f29293529f8f9c48c14053a405ec
Author: Colin Walters <walters verbum org>
Date: Thu May 17 14:37:17 2012 -0400
GSubprocess: New class for spawning child processes
There are a number of nice things this class brings:
0) On Unix, has race-free termination API (we don't reap the child
until the GSubprocess is finalized, so the GPid is always valid)
1) Operates in terms of G{Input,Output}Stream, not file descriptors
2) Async API instead of GSource
https://bugzilla.gnome.org/show_bug.cgi?id=672102
docs/reference/gio/gio-docs.xml | 4 +
docs/reference/gio/gio-sections.txt | 52 ++
docs/reference/gio/gio.types | 1 +
gio/Makefile.am | 2 +
gio/gio.h | 1 +
gio/gio.symbols | 47 ++
gio/giotypes.h | 10 +
gio/gsubprocess.c | 1253 +++++++++++++++++++++++++++++++++++
gio/gsubprocess.h | 110 +++
gio/tests/.gitignore | 2 +
gio/tests/Makefile.am | 8 +
gio/tests/gsubprocess-testprog.c | 174 +++++
gio/tests/gsubprocess.c | 806 ++++++++++++++++++++++
glib/gspawn.h | 1 +
gobject/genums.c | 26 +
gobject/genums.h | 3 +
gobject/glib-types.h | 2 +
17 files changed, 2502 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gio/gio-docs.xml b/docs/reference/gio/gio-docs.xml
index f5c599d..c6e25d3 100644
--- a/docs/reference/gio/gio-docs.xml
+++ b/docs/reference/gio/gio-docs.xml
@@ -106,6 +106,10 @@
<xi:include href="xml/ginitable.xml"/>
<xi:include href="xml/gasyncinitable.xml"/>
</chapter>
+ <chapter id="subprocesses">
+ <title>Subprocesses</title>
+ <xi:include href="xml/gsubprocess.xml"/>
+ </chapter>
<chapter id="networking">
<title>Low-level network support</title>
<xi:include href="xml/gsocket.xml"/>
diff --git a/docs/reference/gio/gio-sections.txt b/docs/reference/gio/gio-sections.txt
index e709e83..cb0f2d9 100644
--- a/docs/reference/gio/gio-sections.txt
+++ b/docs/reference/gio/gio-sections.txt
@@ -3904,3 +3904,55 @@ g_test_dbus_unset
g_test_dbus_get_type
g_test_dbus_flags_get_type
</SECTION>
+
+<SECTION>
+<FILE>gsubprocess</FILE>
+<TITLE>GSubprocess</TITLE>
+GSubprocess
+g_subprocess_new
+g_subprocess_new_with_args
+<SUBSECTION Environment>
+g_subprocess_append_arg
+g_subprocess_append_args
+g_subprocess_append_args_va
+g_subprocess_set_argv
+g_subprocess_set_argv0
+g_subprocess_set_child_setup
+g_subprocess_set_detached
+g_subprocess_setenv
+g_subprocess_unsetenv
+g_subprocess_set_environment
+g_subprocess_set_leave_descriptors_open
+g_subprocess_set_use_search_path
+g_subprocess_set_use_search_path_from_envp
+g_subprocess_set_standard_error_to_devnull
+g_subprocess_set_standard_error_to_stdout
+g_subprocess_set_standard_error_unix_fd
+g_subprocess_set_standard_input_inherit
+g_subprocess_set_standard_input_unix_fd
+g_subprocess_set_standard_output_to_devnull
+g_subprocess_set_standard_output_unix_fd
+g_subprocess_set_working_directory
+<SUBSECTION Running>
+g_subprocess_start_with_pipes
+g_subprocess_start
+g_subprocess_add_watch
+g_subprocess_create_source
+g_subprocess_run_sync
+g_subprocess_wait_sync
+g_subprocess_request_exit
+g_subprocess_force_exit
+<SUBSECTION Status>
+g_subprocess_get_exit_status
+g_subprocess_get_pid
+g_subprocess_check_exit_status
+<SUBSECTION Utilities>
+g_subprocess_run_sync_get_output_bytes
+g_subprocess_run_sync_get_stdout_utf8
+<SUBSECTION Standard>
+G_IS_SUBPROCESS
+G_TYPE_SUBPROCESS
+G_SUBPROCESS
+<SUBSECTION Private>
+g_subprocess_get_type
+</SECTION>
diff --git a/docs/reference/gio/gio.types b/docs/reference/gio/gio.types
index 9eec8a9..8d8ed78 100644
--- a/docs/reference/gio/gio.types
+++ b/docs/reference/gio/gio.types
@@ -135,3 +135,4 @@ g_menu_get_type
g_menu_item_get_type
g_test_dbus_get_type
g_test_dbus_flags_get_type
+g_subprocess_get_type
diff --git a/gio/Makefile.am b/gio/Makefile.am
index df1cc67..525ae6f 100644
--- a/gio/Makefile.am
+++ b/gio/Makefile.am
@@ -426,6 +426,7 @@ libgio_2_0_la_SOURCES = \
gsocketlistener.c \
gsocketoutputstream.c \
gsocketoutputstream.h \
+ gsubprocess.c \
gproxy.c \
gproxyaddress.c \
gproxyaddressenumerator.c \
@@ -600,6 +601,7 @@ gio_headers = \
gsocketlistener.h \
gsocketservice.h \
gsrvtarget.h \
+ gsubprocess.h \
gtcpconnection.h \
gtcpwrapperconnection.h \
gthreadedsocketservice.h\
diff --git a/gio/gio.h b/gio/gio.h
index 3fb914d..93ed5e4 100644
--- a/gio/gio.h
+++ b/gio/gio.h
@@ -122,6 +122,7 @@
#include <gio/gsocketlistener.h>
#include <gio/gsocketservice.h>
#include <gio/gsrvtarget.h>
+#include <gio/gsubprocess.h>
#include <gio/gtcpconnection.h>
#include <gio/gtcpwrapperconnection.h>
#include <gio/gtestdbus.h>
diff --git a/gio/gio.symbols b/gio/gio.symbols
index 6db9bbd..2d17c88 100644
--- a/gio/gio.symbols
+++ b/gio/gio.symbols
@@ -1084,6 +1084,53 @@ g_threaded_socket_service_new
g_tcp_connection_get_type
g_tcp_connection_set_graceful_disconnect
g_tcp_connection_get_graceful_disconnect
+g_subprocess_add_watch
+g_subprocess_append_arg
+g_subprocess_append_args
+g_subprocess_append_args_va
+g_subprocess_create_source
+g_subprocess_force_exit
+g_subprocess_get_exit_status
+g_subprocess_get_pid
+g_subprocess_get_type
+g_subprocess_new
+g_subprocess_new_with_args
+g_subprocess_check_exit_status
+g_subprocess_query_success
+g_subprocess_request_exit
+g_subprocess_run_sync
+g_subprocess_run_sync_get_output_bytes
+g_subprocess_run_sync_get_stdout_utf8
+g_subprocess_set_argv
+g_subprocess_set_argv0
+g_subprocess_set_child_setup
+g_subprocess_set_detached
+g_subprocess_setenv
+g_subprocess_set_leave_descriptors_open
+g_subprocess_set_use_search_path
+g_subprocess_set_use_search_path_from_envp
+g_subprocess_set_standard_error_to_devnull
+g_subprocess_set_standard_error_to_stdout
+#ifdef G_OS_UNIX
+g_subprocess_set_standard_error_unix_fd
+#endif
+g_subprocess_set_standard_input_bytes
+g_subprocess_set_standard_input_file_path
+g_subprocess_set_standard_input_str
+g_subprocess_set_standard_input_stream
+g_subprocess_set_standard_input_inherit
+#ifdef G_OS_UNIX
+g_subprocess_set_standard_input_unix_fd
+#endif
+g_subprocess_set_standard_output_to_devnull
+#ifdef G_OS_UNIX
+g_subprocess_set_standard_output_unix_fd
+#endif
+g_subprocess_set_working_directory
+g_subprocess_start
+g_subprocess_start_with_pipes
+g_subprocess_unsetenv
+g_subprocess_wait_sync
#ifndef G_OS_WIN32
g_unix_connection_get_type
g_unix_connection_receive_fd
diff --git a/gio/giotypes.h b/gio/giotypes.h
index 876d856..5c841b8 100644
--- a/gio/giotypes.h
+++ b/gio/giotypes.h
@@ -135,6 +135,7 @@ typedef struct _GIOStream GIOStream;
typedef struct _GPollableInputStream GPollableInputStream; /* Dummy typedef */
typedef struct _GPollableOutputStream GPollableOutputStream; /* Dummy typedef */
typedef struct _GResolver GResolver;
+
/**
* GResource:
*
@@ -467,6 +468,15 @@ typedef GType (*GDBusProxyTypeFunc) (GDBusObjectManagerClient *manager,
typedef struct _GTestDBus GTestDBus;
+/**
+ * GSubprocess:
+ *
+ * A child process.
+ *
+ * Since: 2.34
+ */
+typedef struct _GSubprocess GSubprocess;
+
G_END_DECLS
#endif /* __GIO_TYPES_H__ */
diff --git a/gio/gsubprocess.c b/gio/gsubprocess.c
new file mode 100644
index 0000000..93923ff
--- /dev/null
+++ b/gio/gsubprocess.c
@@ -0,0 +1,1253 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright  2012 Red Hat, Inc.
+ *
+ * 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.
+ */
+
+/**
+ * SECTION:gsubprocess
+ * @title: GSubprocess
+ * @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.
+ *
+ * There are quite a variety of ways to spawn processes for different
+ * use cases; the primary intent of this class is to act as a
+ * fundamental API, upon which a variety of higher-level helpers can
+ * be written.
+ *
+ * One major advantage that GIO brings 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.34
+ */
+
+#include "config.h"
+#include "gsubprocess.h"
+#include "gasyncresult.h"
+#include "giostream.h"
+#include "gmemoryinputstream.h"
+#include "glibintl.h"
+#include "glib-private.h"
+
+#include <string.h>
+#ifdef G_OS_UNIX
+#include <gio/gunixoutputstream.h>
+#include <gio/gfiledescriptorbased.h>
+#include <gio/gunixinputstream.h>
+#include <gstdio.h>
+#include <glib-unix.h>
+#include <fcntl.h>
+#endif
+#ifdef G_OS_WIN32
+#define _WIN32_WINNT 0x0500
+#include <windows.h>
+#include "giowin32-priv.h"
+#endif
+
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
+typedef enum {
+ G_SUBPROCESS_STREAM_DISPOSITION_DEVNULL = 0,
+ G_SUBPROCESS_STREAM_DISPOSITION_INHERIT = 1,
+ G_SUBPROCESS_STREAM_DISPOSITION_PIPE = 2,
+ G_SUBPROCESS_STREAM_DISPOSITION_MERGE_STDOUT
+#define G_SUBPROCESS_STREAM_DISPOSITION_LAST (G_SUBPROCESS_STREAM_DISPOSITION_MERGE_STDOUT)
+} GSubprocessStreamDispositionKind;
+
+static GObject * get_disposition (GSubprocessStreamDispositionKind kind);
+
+static void initable_iface_init (GInitableIface *initable_iface);
+
+typedef struct _GSubprocessClass GSubprocessClass;
+
+#ifdef G_OS_UNIX
+static void
+g_subprocess_unix_queue_waitpid (GSubprocess *self);
+#endif
+
+typedef struct _GSubprocessStreamDisposition GSubprocessStreamDisposition;
+typedef struct _GSubprocessStreamDispositionClass GSubprocessStreamDispositionClass;
+struct _GSubprocessStreamDispositionClass {
+ GObjectClass parent_class;
+};
+
+struct _GSubprocessClass {
+ GObjectClass parent_class;
+};
+
+struct _GSubprocess
+{
+ GObject parent;
+
+ char **argv;
+ GSpawnFlags spawn_flags;
+ char **envp;
+ char *cwd;
+ GSpawnChildSetupFunc child_setup;
+ gpointer child_setup_user_data;
+ GObject *stdin_disposition;
+ GObject *stdout_disposition;
+ GObject *stderr_disposition;
+
+ GPid pid;
+ guint reaped_child : 1;
+ guint merge_stderr_to_stdout : 1;
+ guint reserved : 30;
+
+ int internal_stdin_fd;
+ int internal_stdout_fd;
+ int internal_stderr_fd;
+
+ GOutputStream *stdin_stream;
+ GInputStream *stdout_stream;
+ GInputStream *stderr_stream;
+};
+
+struct _GSubprocessStreamDisposition
+{
+ GObject parent;
+ GSubprocessStreamDispositionKind kind;
+};
+
+G_DEFINE_TYPE_WITH_CODE (GSubprocess, g_subprocess, G_TYPE_OBJECT,
+ G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init));
+
+G_DEFINE_TYPE (GSubprocessStreamDisposition, g_subprocess_stream_disposition, G_TYPE_OBJECT);
+
+enum
+{
+ PROP_0,
+ PROP_ARGV,
+ PROP_WORKING_DIRECTORY,
+ PROP_ENVIRONMENT,
+ PROP_SPAWN_FLAGS,
+ PROP_CHILD_SETUP_FUNC,
+ PROP_CHILD_SETUP_USER_DATA,
+ PROP_STDIN_DISPOSITION,
+ PROP_STDOUT_DISPOSITION,
+ PROP_STDERR_DISPOSITION
+};
+
+static void
+g_subprocess_init (GSubprocess *self)
+{
+ self->stdin_disposition = get_disposition (G_SUBPROCESS_STREAM_DISPOSITION_DEVNULL);
+ self->stdout_disposition = get_disposition (G_SUBPROCESS_STREAM_DISPOSITION_INHERIT);
+ self->stderr_disposition = get_disposition (G_SUBPROCESS_STREAM_DISPOSITION_INHERIT);
+ self->internal_stdin_fd = -1;
+ self->internal_stdout_fd = -1;
+ self->internal_stderr_fd = -1;
+}
+
+static void
+g_subprocess_stream_disposition_init (GSubprocessStreamDisposition *self)
+{
+ self->kind = G_SUBPROCESS_STREAM_DISPOSITION_INHERIT;
+}
+
+static void
+g_subprocess_finalize (GObject *object)
+{
+ GSubprocess *self = G_SUBPROCESS (object);
+
+#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)
+ g_subprocess_unix_queue_waitpid (self);
+#endif
+ g_spawn_close_pid (self->pid);
+
+ g_clear_object (&self->stdin_disposition);
+ g_clear_object (&self->stdout_disposition);
+ g_clear_object (&self->stderr_disposition);
+
+ g_clear_object (&self->stdin_stream);
+ g_clear_object (&self->stdout_stream);
+ g_clear_object (&self->stderr_stream);
+
+ g_strfreev (self->argv);
+ g_strfreev (self->envp);
+ g_free (self->cwd);
+
+ if (G_OBJECT_CLASS (g_subprocess_parent_class)->finalize != NULL)
+ G_OBJECT_CLASS (g_subprocess_parent_class)->finalize (object);
+}
+
+static void
+g_subprocess_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ GSubprocess *self = G_SUBPROCESS (object);
+
+ switch (prop_id)
+ {
+ case PROP_ARGV:
+ self->argv = g_value_dup_boxed (value);
+ break;
+
+ case PROP_WORKING_DIRECTORY:
+ self->cwd = g_value_dup_string (value);
+ break;
+
+ case PROP_ENVIRONMENT:
+ self->envp = g_value_dup_boxed (value);
+ break;
+
+ case PROP_SPAWN_FLAGS:
+ self->spawn_flags = g_value_get_flags (value);
+ break;
+
+ case PROP_CHILD_SETUP_FUNC:
+ self->child_setup = g_value_get_pointer (value);
+ break;
+
+ case PROP_CHILD_SETUP_USER_DATA:
+ self->child_setup_user_data = g_value_get_pointer (value);
+ break;
+
+ case PROP_STDIN_DISPOSITION:
+ self->stdin_disposition = g_value_dup_object (value);
+ break;
+
+ case PROP_STDOUT_DISPOSITION:
+ self->stdout_disposition = g_value_dup_object (value);
+ break;
+
+ case PROP_STDERR_DISPOSITION:
+ self->stderr_disposition = g_value_dup_object (value);
+ break;
+
+ default:
+ g_assert_not_reached ();
+ }
+}
+
+static void
+g_subprocess_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GSubprocess *self = G_SUBPROCESS (object);
+
+ switch (prop_id)
+ {
+ case PROP_ARGV:
+ g_value_set_boxed (value, (gpointer)self->argv);
+ break;
+
+ case PROP_WORKING_DIRECTORY:
+ g_value_set_string (value, self->cwd);
+ break;
+
+ case PROP_ENVIRONMENT:
+ g_value_set_boxed (value, (gpointer)self->envp);
+ break;
+
+ case PROP_SPAWN_FLAGS:
+ g_value_set_flags (value, self->spawn_flags);
+ break;
+
+ case PROP_CHILD_SETUP_FUNC:
+ g_value_set_pointer (value, self->child_setup);
+ break;
+
+ case PROP_CHILD_SETUP_USER_DATA:
+ g_value_set_pointer (value, self->child_setup_user_data);
+ break;
+
+ case PROP_STDIN_DISPOSITION:
+ g_value_set_object (value, self->stdin_disposition);
+ break;
+
+ case PROP_STDOUT_DISPOSITION:
+ g_value_set_object (value, self->stdout_disposition);
+ break;
+
+ case PROP_STDERR_DISPOSITION:
+ g_value_set_object (value, self->stderr_disposition);
+ break;
+
+ default:
+ g_assert_not_reached ();
+ }
+}
+
+static void
+g_subprocess_class_init (GSubprocessClass *class)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (class);
+
+ gobject_class->finalize = g_subprocess_finalize;
+ gobject_class->get_property = g_subprocess_get_property;
+ gobject_class->set_property = g_subprocess_set_property;
+
+ /**
+ * GSubprocess:argv:
+ *
+ * Arguments to for child process; if the given #GSpawnFlags do not
+ * include %G_SPAWN_SEARCH_PATH has not been used, then this must be
+ * a full path.
+ *
+ * Since: 2.34
+ */
+ g_object_class_install_property (gobject_class, PROP_ARGV,
+ g_param_spec_boxed ("argv",
+ P_("Arguments"),
+ P_("Argument list"),
+ G_TYPE_STRV,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
+
+ /**
+ * GSubprocess:working-directory:
+ *
+ * Used for child's initial current working directory. %NULL will
+ * cause the child to inherit the parent's working directory; this
+ * is the default.
+ *
+ * Since: 2.34
+ */
+ g_object_class_install_property (gobject_class, PROP_WORKING_DIRECTORY,
+ g_param_spec_string ("working-directory",
+ P_("Working Directory"),
+ P_("Path to working directory"),
+ NULL,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
+
+ /**
+ * GSubprocess:environment:
+ *
+ * Used for child's environment. %NULL will cause the child to
+ * inherit the parent's environment; this is the default.
+ *
+ * Since: 2.34
+ */
+ g_object_class_install_property (gobject_class, PROP_ENVIRONMENT,
+ g_param_spec_boxed ("environment",
+ P_("Environment"),
+ P_("Environment for child process"),
+ G_TYPE_STRV,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
+
+ /**
+ * GSubprocess:spawn-flags:
+ *
+ * Additional flags controlling the subprocess. See %GSpawnFlags.
+ *
+ * Since: 2.34
+ */
+ g_object_class_install_property (gobject_class, PROP_SPAWN_FLAGS,
+ g_param_spec_flags ("spawn-flags",
+ P_("Spawn Flags"),
+ P_("Additional flags conrolling the subprocess"),
+ G_TYPE_SPAWN_FLAGS, 0,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
+
+
+ /**
+ * GSubprocess:child-setup-func:
+ *
+ * Only available on Unix; this function will be invoked between
+ * <literal>fork()</literal> and <literal>exec()</literal>. See the
+ * documentation of g_spawn_async_with_pipes() for more details.
+ *
+ * Since: 2.34
+ */
+ g_object_class_install_property (gobject_class, PROP_CHILD_SETUP_FUNC,
+ g_param_spec_pointer ("child-setup-func",
+ P_("Setup"),
+ P_("Child setup function"),
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
+
+ /**
+ * GSubprocess:child-setup-user-data:
+ *
+ * User data passed to the %GSubprocess:child-setup-func.
+ *
+ * Since: 2.34
+ */
+ g_object_class_install_property (gobject_class, PROP_CHILD_SETUP_USER_DATA,
+ g_param_spec_pointer ("child-setup-user-data",
+ P_("Data"),
+ P_("Child setup user data"),
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
+
+ /**
+ * GSubprocess:stdin-disposition:
+ *
+ * Controls the direction of standard input.
+ *
+ * Since: 2.34
+ */
+ g_object_class_install_property (gobject_class, PROP_STDIN_DISPOSITION,
+ g_param_spec_object ("stdin-disposition",
+ P_("Stdin"),
+ P_("Disposition of standard input"),
+ G_TYPE_OBJECT,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
+
+ /**
+ * GSubprocess:stdout-disposition:
+ *
+ * Controls the direction of standard output.
+ *
+ * Since: 2.34
+ */
+ g_object_class_install_property (gobject_class, PROP_STDOUT_DISPOSITION,
+ g_param_spec_object ("stdout-disposition",
+ P_("Stdout"),
+ P_("Disposition of standard output"),
+ G_TYPE_OBJECT,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
+
+ /**
+ * GSubprocess:stderr-disposition:
+ *
+ * Controls the direction of standard error.
+ *
+ * Since: 2.34
+ */
+ g_object_class_install_property (gobject_class, PROP_STDERR_DISPOSITION,
+ g_param_spec_object ("stderr-disposition",
+ P_("Stderr"),
+ P_("Disposition of standard error"),
+ G_TYPE_OBJECT,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
+
+}
+
+static void
+g_subprocess_stream_disposition_class_init (GSubprocessStreamDispositionClass *klass)
+{
+}
+
+#ifdef G_OS_UNIX
+
+static gboolean
+g_subprocess_unix_waitpid_dummy (gpointer data)
+{
+ return FALSE;
+}
+
+static void
+g_subprocess_unix_queue_waitpid (GSubprocess *self)
+{
+ GMainContext *worker_context;
+ GSource *waitpid_source;
+
+ worker_context = GLIB_PRIVATE_CALL (g_get_worker_context) ();
+ waitpid_source = g_child_watch_source_new (self->pid);
+ g_source_set_callback (waitpid_source, g_subprocess_unix_waitpid_dummy, NULL, NULL);
+ g_source_attach (waitpid_source, worker_context);
+ g_source_unref (waitpid_source);
+}
+
+static void
+safe_dup2 (gint a,
+ gint b)
+{
+ gint ecode;
+
+ if (a == b)
+ return;
+
+ do
+ ecode = dup2 (a, b);
+ while (ecode == -1 && errno == EINTR);
+}
+
+static void
+g_subprocess_internal_child_setup (gpointer user_data)
+{
+ GSubprocess *self = user_data;
+
+ if (self->internal_stdin_fd >= 0)
+ safe_dup2 (self->internal_stdin_fd, 0);
+
+ if (self->internal_stdout_fd >= 0)
+ safe_dup2 (self->internal_stdout_fd, 1);
+
+ if (self->internal_stderr_fd >= 0)
+ safe_dup2 (self->internal_stderr_fd, 2);
+
+ if (self->merge_stderr_to_stdout)
+ safe_dup2 (1, 2);
+
+ if (self->child_setup)
+ self->child_setup (self->child_setup_user_data);
+}
+
+#endif
+
+static GInputStream *
+platform_input_stream_from_spawn_fd (gint fd)
+{
+#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)
+{
+#ifdef G_OS_UNIX
+ return g_unix_output_stream_new (fd, TRUE);
+#else
+ return g_win32_output_stream_new_from_fd (fd, TRUE);
+#endif
+}
+
+static void
+set_open_error (const char *filename,
+ GError **error)
+{
+ int errsv = errno;
+ char *display_name = g_filename_display_name (filename);
+ g_set_error (error, G_IO_ERROR,
+ g_io_error_from_errno (errsv),
+ _("Error opening file '%s': %s"),
+ display_name, g_strerror (errsv));
+ g_free (display_name);
+
+}
+
+static int
+open_file_write_append (const char *filename,
+ GError **error)
+{
+ int fd;
+
+ fd = g_open (filename, O_WRONLY | O_CREAT | O_BINARY, 0666);
+ if (fd == -1)
+ {
+ set_open_error (filename, error);
+ return -1;
+ }
+ return fd;
+}
+
+static gboolean
+get_file_path_or_fail (GFile *f,
+ char **out_path,
+ GError **error)
+{
+ *out_path = g_file_get_path (f);
+ if (!*out_path)
+ {
+ g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
+ "File does not have a local path");
+ return FALSE;
+ }
+ return TRUE;
+}
+
+static void
+set_unsupported_stream_error (GObject *object,
+ GError **error)
+{
+ g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
+ "Unsupported stream type %s", g_type_name (G_OBJECT_TYPE (object)));
+}
+
+static gboolean
+unix_handle_open_fd_for_write (GObject *disposition,
+ gboolean *out_supported,
+ gint *out_fd,
+ gboolean *out_close_fd,
+ GError **error)
+{
+ gboolean ret = FALSE;
+ char *temp_path = NULL;
+
+#ifdef G_OS_UNIX
+ if (G_IS_FILE_DESCRIPTOR_BASED (disposition))
+ {
+ *out_fd = g_file_descriptor_based_get_fd ((GFileDescriptorBased*) disposition);
+ *out_close_fd = FALSE;
+ *out_supported = TRUE;
+ }
+ else if (G_IS_FILE (disposition))
+ {
+ if (!get_file_path_or_fail ((GFile*)disposition, &temp_path, error))
+ goto out;
+
+ *out_fd = open_file_write_append (temp_path, error);
+ if (*out_fd == -1)
+ goto out;
+ *out_close_fd = TRUE;
+ *out_supported = TRUE;
+ }
+ else
+ *out_supported = FALSE;
+
+ ret = TRUE;
+ out:
+ g_clear_pointer (&temp_path, g_free);
+ return ret;
+#else
+ *out_supported = FALSE;
+ return TRUE;
+#endif
+}
+
+static gboolean
+initable_init (GInitable *initable,
+ GCancellable *cancellable,
+ GError **error)
+{
+ GSubprocess *self = G_SUBPROCESS (initable);
+ gboolean ret = FALSE;
+ char *temp_path = NULL;
+ gint stdin_pipe_fd = -1;
+ gint *stdin_arg = NULL;
+ gint stdout_pipe_fd = -1;
+ gint *stdout_arg = NULL;
+ gint stderr_pipe_fd = -1;
+ gint *stderr_arg = NULL;
+ gboolean disposition_supported;
+ GSpawnChildSetupFunc child_setup;
+ gpointer child_setup_user_data;
+ gboolean close_stdin_fd = FALSE;
+ gboolean close_stdout_fd = FALSE;
+ gboolean close_stderr_fd = FALSE;
+
+ g_return_val_if_fail (self->argv && self->argv[0], FALSE);
+
+ if (self->stdin_disposition == NULL)
+ self->stdin_disposition = g_object_ref (g_subprocess_stream_devnull ());
+ if (self->stdout_disposition == NULL)
+ self->stdout_disposition = g_object_ref (g_subprocess_stream_inherit ());
+ if (self->stderr_disposition == NULL)
+ self->stderr_disposition = g_object_ref (g_subprocess_stream_inherit ());
+
+ if (self->spawn_flags & G_SPAWN_CHILD_INHERITS_STDIN)
+ {
+ g_clear_object (&self->stdin_disposition);
+ self->stdin_disposition = g_subprocess_stream_inherit ();
+ }
+ if (self->spawn_flags & G_SPAWN_STDOUT_TO_DEV_NULL)
+ {
+ g_clear_object (&self->stdout_disposition);
+ self->stdout_disposition = g_subprocess_stream_devnull ();
+ }
+ if (self->spawn_flags & G_SPAWN_STDERR_TO_DEV_NULL)
+ {
+ g_clear_object (&self->stderr_disposition);
+ self->stderr_disposition = g_subprocess_stream_devnull ();
+ }
+
+ /* We always use this one */
+ self->spawn_flags |= G_SPAWN_DO_NOT_REAP_CHILD;
+
+#ifdef G_OS_UNIX
+ child_setup = g_subprocess_internal_child_setup;
+ child_setup_user_data = self;
+#else
+ child_setup = NULL;
+ child_setup_user_data = NULL;
+#endif
+
+#ifdef G_OS_UNIX
+ if (G_IS_FILE_DESCRIPTOR_BASED (self->stdin_disposition))
+ {
+ self->internal_stdin_fd = g_file_descriptor_based_get_fd ((GFileDescriptorBased*) self->stdin_disposition);
+ }
+ else if (G_IS_FILE (self->stdin_disposition))
+ {
+ if (!get_file_path_or_fail ((GFile*)self->stdin_disposition, &temp_path, error))
+ goto out;
+
+ self->internal_stdin_fd = g_open (temp_path, O_RDONLY | O_BINARY, 0);
+ if (self->internal_stdin_fd == -1)
+ {
+ set_open_error (temp_path, error);
+ goto out;
+ }
+ close_stdin_fd = TRUE;
+
+ g_clear_pointer (&temp_path, g_free);
+ }
+ else
+#endif
+ if (self->stdin_disposition == g_subprocess_stream_devnull ())
+ {
+ /* Default */
+ }
+ else if (self->stdin_disposition == g_subprocess_stream_inherit ())
+ {
+ self->spawn_flags |= G_SPAWN_CHILD_INHERITS_STDIN;
+ }
+ else if (self->stdin_disposition == g_subprocess_stream_pipe ())
+ {
+ stdin_arg = &stdin_pipe_fd;
+ }
+ else
+ {
+ set_unsupported_stream_error (self->stdin_disposition, error);
+ goto out;
+ }
+
+ /* Handle stdout */
+
+ if (!unix_handle_open_fd_for_write (self->stdout_disposition,
+ &disposition_supported,
+ &self->internal_stdout_fd,
+ &close_stdout_fd,
+ error))
+ goto out;
+ else if (!disposition_supported)
+ {
+ if (self->stdout_disposition == g_subprocess_stream_devnull ())
+ {
+ self->spawn_flags |= G_SPAWN_STDOUT_TO_DEV_NULL;
+ }
+ else if (self->stdout_disposition == g_subprocess_stream_inherit ())
+ {
+ /* Default */
+ }
+ else if (self->stdout_disposition == g_subprocess_stream_pipe ())
+ {
+ stdout_arg = &stdout_pipe_fd;
+ }
+ else
+ {
+ set_unsupported_stream_error (self->stdout_disposition, error);
+ goto out;
+ }
+ }
+
+ /* Handle stderr */
+
+ if (!unix_handle_open_fd_for_write (self->stderr_disposition,
+ &disposition_supported,
+ &self->internal_stderr_fd,
+ &close_stderr_fd,
+ error))
+ goto out;
+ else if (!disposition_supported)
+ {
+ if (self->stderr_disposition == g_subprocess_stream_devnull ())
+ {
+ self->spawn_flags |= G_SPAWN_STDERR_TO_DEV_NULL;
+ }
+ else if (self->stderr_disposition == g_subprocess_stream_inherit ())
+ {
+ /* Default */
+ }
+ else if (self->stderr_disposition == g_subprocess_stream_pipe ())
+ {
+ stderr_arg = &stderr_pipe_fd;
+ }
+ else if (self->stderr_disposition == g_subprocess_stream_merge_stdout ())
+ {
+ self->merge_stderr_to_stdout = TRUE;
+ }
+ else
+ {
+ set_unsupported_stream_error (self->stderr_disposition, error);
+ goto out;
+ }
+ }
+
+ if (!g_spawn_async_with_pipes (self->cwd,
+ self->argv,
+ self->envp,
+ self->spawn_flags,
+ child_setup, child_setup_user_data,
+ &self->pid,
+ stdin_arg, stdout_arg, stderr_arg,
+ error))
+ goto out;
+
+ ret = TRUE;
+
+ if (stdin_pipe_fd != -1)
+ self->stdin_stream = platform_output_stream_from_spawn_fd (stdin_pipe_fd);
+ if (stdout_pipe_fd != -1)
+ self->stdout_stream = platform_input_stream_from_spawn_fd (stdout_pipe_fd);
+ if (stderr_pipe_fd != -1)
+ self->stderr_stream = platform_input_stream_from_spawn_fd (stderr_pipe_fd);
+
+ out:
+ g_free (temp_path);
+#ifdef G_OS_UNIX
+ if (close_stdin_fd)
+ (void) close (self->internal_stdin_fd);
+ if (close_stdout_fd)
+ (void) close (self->internal_stdout_fd);
+ if (close_stderr_fd)
+ (void) close (self->internal_stderr_fd);
+#endif
+ return ret;
+}
+
+static void
+initable_iface_init (GInitableIface *initable_iface)
+{
+ initable_iface->init = initable_init;
+}
+
+/**
+ * g_subprocess_new:
+ *
+ * Create a new process with the given parameters.
+ *
+ * Returns: (transfer full): A newly created %GSubprocess, or %NULL on error (and @error will be set)
+ *
+ * Since: 2.34
+ */
+GLIB_AVAILABLE_IN_2_34
+GSubprocess * g_subprocess_new (gchar **argv,
+ const gchar *cwd,
+ gchar **env,
+ GSpawnFlags spawn_flags,
+ GSpawnChildSetupFunc child_setup,
+ gpointer user_data,
+ GObject *stdin_disposition,
+ GObject *stdout_disposition,
+ GObject *stderr_disposition,
+ GError **error)
+{
+ return g_initable_new (G_TYPE_SUBPROCESS,
+ NULL,
+ error,
+ "argv", argv,
+ "working-directory", cwd,
+ "environment", env,
+ "spawn-flags", spawn_flags,
+ "child-setup-func", child_setup,
+ "child-setup-user-data", user_data,
+ "stdin-disposition", stdin_disposition,
+ "stdout-disposition", stdout_disposition,
+ "stderr-disposition", stderr_disposition,
+ NULL);
+}
+
+/**
+ * g_subprocess_get_pid:
+ * @self: a #GSubprocess
+ *
+ * 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
+ * g_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
+ * g_subprocess_request_exit() and g_subprocess_force_exit() API is
+ * recommended.
+ *
+ * Returns: Operating-system specific identifier for child process
+ *
+ * Since: 2.34
+ */
+GPid
+g_subprocess_get_pid (GSubprocess *self)
+{
+ g_return_val_if_fail (G_IS_SUBPROCESS (self), 0);
+
+ return self->pid;
+}
+
+static GObject *
+get_disposition (GSubprocessStreamDispositionKind kind)
+{
+ static gsize initialized = 0;
+ static GPtrArray* dispositions = NULL;
+
+ if (g_once_init_enter (&initialized))
+ {
+ int i;
+ dispositions = g_ptr_array_new_with_free_func ((GDestroyNotify)g_object_unref);
+ for (i = 0; i < G_SUBPROCESS_STREAM_DISPOSITION_LAST+1; i++)
+ {
+ GSubprocessStreamDisposition *disposition = g_object_new (g_subprocess_stream_disposition_get_type (), NULL);
+ disposition->kind = (GSubprocessStreamDispositionKind)i;
+ g_ptr_array_add (dispositions, disposition);
+ }
+ g_once_init_leave (&initialized, TRUE);
+ }
+ return dispositions->pdata[(guint)kind];
+}
+
+GObject *
+g_subprocess_stream_devnull (void)
+{
+ return get_disposition (G_SUBPROCESS_STREAM_DISPOSITION_DEVNULL);
+}
+
+GObject *
+g_subprocess_stream_inherit (void)
+{
+ return get_disposition (G_SUBPROCESS_STREAM_DISPOSITION_INHERIT);
+}
+
+GObject *
+g_subprocess_stream_pipe (void)
+{
+ return get_disposition (G_SUBPROCESS_STREAM_DISPOSITION_PIPE);
+}
+
+GObject *
+g_subprocess_stream_merge_stdout (void)
+{
+ return get_disposition (G_SUBPROCESS_STREAM_DISPOSITION_MERGE_STDOUT);
+}
+
+GOutputStream *
+g_subprocess_get_stdin_pipe (GSubprocess *self)
+{
+ g_return_val_if_fail (G_IS_SUBPROCESS (self), NULL);
+ g_return_val_if_fail (self->stdin_disposition == g_subprocess_stream_pipe (), NULL);
+
+ return self->stdin_stream;
+}
+
+GInputStream *
+g_subprocess_get_stdout_pipe (GSubprocess *self)
+{
+ g_return_val_if_fail (G_IS_SUBPROCESS (self), NULL);
+ g_return_val_if_fail (self->stdout_disposition == g_subprocess_stream_pipe (), NULL);
+
+ return self->stdout_stream;
+}
+
+GInputStream *
+g_subprocess_get_stderr_pipe (GSubprocess *self)
+{
+ g_return_val_if_fail (G_IS_SUBPROCESS (self), NULL);
+ g_return_val_if_fail (self->stderr_disposition == g_subprocess_stream_pipe (), NULL);
+
+ return self->stderr_stream;
+}
+
+typedef struct {
+ GSubprocess *self;
+ gboolean have_wnowait;
+ GCancellable *cancellable;
+ GSimpleAsyncResult *result;
+} GSubprocessWatchData;
+
+static gboolean
+g_subprocess_on_child_exited (GPid pid,
+ gint status_code,
+ gpointer user_data)
+{
+ GSubprocessWatchData *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
+ {
+ if (!data->have_wnowait)
+ 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;
+}
+
+/**
+ * g_subprocess_wait:
+ * @self: a #GSubprocess
+ * @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.34
+ */
+void
+g_subprocess_wait (GSubprocess *self,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GSource *source;
+ GSubprocessWatchData *data;
+
+ data = g_new0 (GSubprocessWatchData, 1);
+
+ data->self = g_object_ref (self);
+ data->result = g_simple_async_result_new ((GObject*)self, callback, user_data,
+ g_subprocess_wait);
+
+ source = GLIB_PRIVATE_CALL (g_child_watch_source_new_with_flags) (self->pid, _G_CHILD_WATCH_FLAGS_WNOWAIT);
+ if (source == NULL)
+ {
+ source = g_child_watch_source_new (self->pid);
+ data->have_wnowait = FALSE;
+ }
+ else
+ {
+ data->have_wnowait = TRUE;
+ }
+
+ g_source_set_callback (source, (GSourceFunc)g_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);
+}
+
+/**
+ * g_subprocess_wait_finish:
+ * @self: a #GSubprocess
+ * @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.34
+ */
+gboolean
+g_subprocess_wait_finish (GSubprocess *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;
+} GSubprocessSyncWaitData;
+
+static void
+g_subprocess_on_sync_wait_complete (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GSubprocessSyncWaitData *data = user_data;
+
+ if (!g_subprocess_wait_finish ((GSubprocess*)object, result,
+ data->exit_status_ptr, data->error))
+ data->caught_error = TRUE;
+
+ g_main_loop_quit (data->loop);
+}
+
+/**
+ * g_subprocess_wait_sync:
+ * @self: a #GSubprocess
+ * @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.34
+ */
+gboolean
+g_subprocess_wait_sync (GSubprocess *self,
+ int *out_exit_status,
+ GCancellable *cancellable,
+ GError **error)
+{
+ gboolean ret = FALSE;
+ gboolean pushed_thread_default = FALSE;
+ GMainContext *context = NULL;
+ GSubprocessSyncWaitData data;
+
+ memset (&data, 0, sizeof (data));
+
+ g_return_val_if_fail (G_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;
+
+ g_subprocess_wait (self, cancellable,
+ g_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;
+}
+
+/**
+ * g_subprocess_wait_sync_check:
+ * @self: a #GSubprocess
+ * @cancellable: a #GCancellable
+ * @error: a #GError
+ *
+ * Combines g_subprocess_wait_sync() with g_spawn_check_exit_status().
+ *
+ * Returns: %TRUE on success, %FALSE if process exited abnormally, or @cancellable was cancelled
+ *
+ * Since: 2.34
+ */
+gboolean
+g_subprocess_wait_sync_check (GSubprocess *self,
+ GCancellable *cancellable,
+ GError **error)
+{
+ gboolean ret = FALSE;
+ int exit_status;
+
+ if (!g_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;
+}
+
+/**
+ * g_subprocess_request_exit:
+ * @self: a #GSubprocess
+ *
+ * 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 g_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.34
+ */
+gboolean
+g_subprocess_request_exit (GSubprocess *self)
+{
+ g_return_val_if_fail (G_IS_SUBPROCESS (self), FALSE);
+
+#ifdef G_OS_UNIX
+ (void) kill (self->pid, SIGTERM);
+ return TRUE;
+#else
+ return FALSE;
+#endif
+}
+
+/**
+ * g_subprocess_force_exit:
+ * @self: a #GSubprocess
+ *
+ * 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 g_subprocess_wait() to monitor the status of
+ * the process after calling this function.
+ *
+ * On Unix, this function sends %SIGKILL.
+ */
+void
+g_subprocess_force_exit (GSubprocess *self)
+{
+ g_return_if_fail (G_IS_SUBPROCESS (self));
+
+#ifdef G_OS_UNIX
+ (void) kill (self->pid, SIGKILL);
+#else
+ TerminateProcess (self->pid, 1);
+#endif
+}
diff --git a/gio/gsubprocess.h b/gio/gsubprocess.h
new file mode 100644
index 0000000..6a57066
--- /dev/null
+++ b/gio/gsubprocess.h
@@ -0,0 +1,110 @@
+/* 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.
+ *
+ * Author: Colin Walters <walters verbum org>
+ */
+
+#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
+#error "Only <gio/gio.h> can be included directly."
+#endif
+
+#ifndef __G_SUBPROCESS_H__
+#define __G_SUBPROCESS_H__
+
+#include <gio/giotypes.h>
+
+G_BEGIN_DECLS
+
+#define G_TYPE_SUBPROCESS (g_subprocess_get_type ())
+#define G_SUBPROCESS(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_SUBPROCESS, GSubprocess))
+#define G_IS_SUBPROCESS(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_SUBPROCESS))
+
+GLIB_AVAILABLE_IN_2_34
+GType g_subprocess_get_type (void) G_GNUC_CONST;
+
+/**** Creation ****/
+
+GLIB_AVAILABLE_IN_2_34
+GSubprocess * g_subprocess_new (gchar **argv,
+ const gchar *cwd,
+ gchar **env,
+ GSpawnFlags spawn_flags,
+ GSpawnChildSetupFunc child_setup,
+ gpointer user_data,
+ GObject *stdin_disposition,
+ GObject *stdout_disposition,
+ GObject *stderr_disposition,
+ GError **error);
+
+GLIB_AVAILABLE_IN_2_34
+GObject * g_subprocess_stream_devnull (void);
+
+GLIB_AVAILABLE_IN_2_34
+GObject * g_subprocess_stream_inherit (void);
+
+GLIB_AVAILABLE_IN_2_34
+GObject * g_subprocess_stream_pipe (void);
+
+GLIB_AVAILABLE_IN_2_34
+GObject * g_subprocess_stream_merge_stdout (void);
+
+GLIB_AVAILABLE_IN_2_34
+GOutputStream * g_subprocess_get_stdin_pipe (GSubprocess *self);
+
+GLIB_AVAILABLE_IN_2_34
+GInputStream * g_subprocess_get_stdout_pipe (GSubprocess *self);
+
+GLIB_AVAILABLE_IN_2_34
+GInputStream * g_subprocess_get_stderr_pipe (GSubprocess *self);
+
+GLIB_AVAILABLE_IN_2_34
+void g_subprocess_wait (GSubprocess *self,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+
+GLIB_AVAILABLE_IN_2_34
+gboolean g_subprocess_wait_finish (GSubprocess *self,
+ GAsyncResult *result,
+ int *out_exit_status,
+ GError **error);
+
+GLIB_AVAILABLE_IN_2_34
+gboolean g_subprocess_wait_sync (GSubprocess *self,
+ int *out_exit_status,
+ GCancellable *cancellable,
+ GError **error);
+
+GLIB_AVAILABLE_IN_2_34
+gboolean g_subprocess_wait_sync_check (GSubprocess *self,
+ GCancellable *cancellable,
+ GError **error);
+
+GLIB_AVAILABLE_IN_2_34
+GPid g_subprocess_get_pid (GSubprocess *self);
+
+GLIB_AVAILABLE_IN_2_34
+gboolean g_subprocess_request_exit (GSubprocess *self);
+
+GLIB_AVAILABLE_IN_2_34
+void g_subprocess_force_exit (GSubprocess *self);
+
+G_END_DECLS
+
+#endif /* __G_SUBPROCESS_H__ */
diff --git a/gio/tests/.gitignore b/gio/tests/.gitignore
index 7492902..4f63e4f 100644
--- a/gio/tests/.gitignore
+++ b/gio/tests/.gitignore
@@ -63,6 +63,8 @@ gdbus-serialization
gdbus-test-codegen
gdbus-test-codegen-generated*
gdbus-threading
+gsubprocess
+gsubprocess-testprog
g-file
g-file-info
g-icon
diff --git a/gio/tests/Makefile.am b/gio/tests/Makefile.am
index 14411e2..c2061e1 100644
--- a/gio/tests/Makefile.am
+++ b/gio/tests/Makefile.am
@@ -45,6 +45,7 @@ TEST_PROGS += \
srvtarget \
contexts \
gsettings \
+ gsubprocess \
gschema-compile \
async-close-output-stream \
gdbus-addresses \
@@ -264,6 +265,13 @@ contexts_LDADD = $(progs_ldadd) \
gsettings_SOURCES = gsettings.c
gsettings_LDADD = $(progs_ldadd)
+gsubprocess_SOURCES = gsubprocess.c
+gsubprocess_LDADD = $(progs_ldadd)
+
+noinst_PROGRAMS += gsubprocess-testprog
+gsubprocess_testprog_SOURCES = gsubprocess-testprog.c
+gsubprocess_testprog_LDADD = $(progs_ldadd)
+
gschema_compile_SOURCES = gschema-compile.c
gschema_compile_LDADD = $(progs_ldadd)
diff --git a/gio/tests/gsubprocess-testprog.c b/gio/tests/gsubprocess-testprog.c
new file mode 100644
index 0000000..14fa405
--- /dev/null
+++ b/gio/tests/gsubprocess-testprog.c
@@ -0,0 +1,174 @@
+#include <gio/gio.h>
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+#ifdef G_OS_UNIX
+#include <gio/gunixinputstream.h>
+#include <gio/gunixoutputstream.h>
+#endif
+
+static GOptionEntry options[] = {
+ {NULL}
+};
+
+static void
+write_all (int fd,
+ const guint8* buf,
+ gsize len)
+{
+ while (len > 0)
+ {
+ ssize_t bytes_written = write (fd, buf, len);
+ if (bytes_written < 0)
+ g_error ("Failed to write to fd %d: %s",
+ fd, strerror (errno));
+ buf += bytes_written;
+ len -= bytes_written;
+ }
+}
+
+static int
+echo_mode (int argc,
+ char **argv)
+{
+ int i;
+
+ for (i = 2; i < argc; i++)
+ {
+ write_all (1, (guint8*)argv[i], strlen (argv[i]));
+ write_all (1, (guint8*)"\n", 1);
+ }
+
+ return 0;
+}
+
+static int
+echo_stdout_and_stderr_mode (int argc,
+ char **argv)
+{
+ int i;
+
+ for (i = 2; i < argc; i++)
+ {
+ write_all (1, (guint8*)argv[i], strlen (argv[i]));
+ write_all (1, (guint8*)"\n", 1);
+ write_all (2, (guint8*)argv[i], strlen (argv[i]));
+ write_all (2, (guint8*)"\n", 1);
+ }
+
+ return 0;
+}
+
+static int
+cat_mode (int argc,
+ char **argv)
+{
+ GIOChannel *chan_stdin;
+ GIOChannel *chan_stdout;
+ GIOStatus status;
+ char buf[1024];
+ gsize bytes_read, bytes_written;
+ GError *local_error = NULL;
+ GError **error = &local_error;
+
+ chan_stdin = g_io_channel_unix_new (0);
+ g_io_channel_set_encoding (chan_stdin, NULL, error);
+ g_assert_no_error (local_error);
+ chan_stdout = g_io_channel_unix_new (1);
+ g_io_channel_set_encoding (chan_stdout, NULL, error);
+ g_assert_no_error (local_error);
+
+ while (TRUE)
+ {
+ do
+ status = g_io_channel_read_chars (chan_stdin, buf, sizeof (buf),
+ &bytes_read, error);
+ while (status == G_IO_STATUS_AGAIN);
+
+ if (status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
+ break;
+
+ do
+ status = g_io_channel_write_chars (chan_stdout, buf, bytes_read,
+ &bytes_written, error);
+ while (status == G_IO_STATUS_AGAIN);
+
+ if (status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
+ break;
+ }
+
+ g_io_channel_unref (chan_stdin);
+ g_io_channel_unref (chan_stdout);
+
+ if (local_error)
+ {
+ g_printerr ("I/O error: %s\n", local_error->message);
+ g_clear_error (&local_error);
+ return 1;
+ }
+ return 0;
+}
+
+static gint
+sleep_forever_mode (int argc,
+ char **argv)
+{
+ GMainLoop *loop;
+
+ loop = g_main_loop_new (NULL, TRUE);
+ g_main_loop_run (loop);
+
+ return 0;
+}
+
+int
+main (int argc, char **argv)
+{
+ GOptionContext *context;
+ GError *error = NULL;
+ const char *mode;
+
+ g_type_init ();
+
+ context = g_option_context_new ("MODE - Test GSubprocess stuff");
+ g_option_context_add_main_entries (context, options, NULL);
+ if (!g_option_context_parse (context, &argc, &argv, &error))
+ {
+ g_printerr ("%s: %s\n", argv[0], error->message);
+ return 1;
+ }
+
+ if (argc < 2)
+ {
+ g_printerr ("MODE argument required\n");
+ return 1;
+ }
+
+ mode = argv[1];
+ if (strcmp (mode, "noop") == 0)
+ return 0;
+ else if (strcmp (mode, "exit1") == 0)
+ return 1;
+ else if (strcmp (mode, "assert-argv0") == 0)
+ {
+ if (strcmp (argv[0], "moocow") == 0)
+ return 0;
+ g_printerr ("argv0=%s != moocow\n", argv[0]);
+ return 1;
+ }
+ else if (strcmp (mode, "echo") == 0)
+ return echo_mode (argc, argv);
+ else if (strcmp (mode, "echo-stdout-and-stderr") == 0)
+ return echo_stdout_and_stderr_mode (argc, argv);
+ else if (strcmp (mode, "cat") == 0)
+ return cat_mode (argc, argv);
+ else if (strcmp (mode, "sleep-forever") == 0)
+ return sleep_forever_mode (argc, argv);
+ else
+ {
+ g_printerr ("Unknown MODE %s\n", argv[1]);
+ return 1;
+ }
+
+ return TRUE;
+}
diff --git a/gio/tests/gsubprocess.c b/gio/tests/gsubprocess.c
new file mode 100644
index 0000000..135fc9e
--- /dev/null
+++ b/gio/tests/gsubprocess.c
@@ -0,0 +1,806 @@
+#include <gio/gio.h>
+#include <string.h>
+
+#ifdef G_OS_UNIX
+#include <sys/wait.h>
+#include <gio/gfiledescriptorbased.h>
+#endif
+
+static GPtrArray *
+get_test_subprocess_args (const char *mode,
+ ...) G_GNUC_NULL_TERMINATED;
+
+static GPtrArray *
+get_test_subprocess_args (const char *mode,
+ ...)
+{
+ GPtrArray *ret;
+ char *cwd;
+ char *cwd_path;
+ const char *binname;
+ va_list args;
+ gpointer arg;
+
+ ret = g_ptr_array_new_with_free_func (g_free);
+
+ cwd = g_get_current_dir ();
+
+#ifdef G_OS_WIN32
+ binname = "gsubprocess-testprog.exe";
+#else
+ binname = "gsubprocess-testprog";
+#endif
+
+ cwd_path = g_build_filename (cwd, binname, NULL);
+ g_free (cwd);
+ g_ptr_array_addv (ret, cwd_path, g_strdup (mode), NULL);
+
+ va_start (args, mode);
+ while ((arg = va_arg (args, gpointer)) != NULL)
+ g_ptr_array_add (ret, g_strdup (arg));
+ va_end (args);
+
+ g_ptr_array_add (ret, NULL);
+ return ret;
+}
+
+static void
+test_noop (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GPtrArray *args;
+ GSubprocess *proc;
+
+ args = get_test_subprocess_args ("noop", NULL);
+ proc = g_subprocess_new ((char**) args->pdata, NULL, NULL, 0, NULL, NULL,
+ g_subprocess_stream_devnull (),
+ g_subprocess_stream_inherit (),
+ g_subprocess_stream_inherit (),
+ error);
+ g_ptr_array_free (args, TRUE);
+ g_assert_no_error (local_error);
+
+ (void)g_subprocess_wait_sync_check (proc, NULL, error);
+ g_assert_no_error (local_error);
+
+ g_object_unref (proc);
+}
+
+static void
+test_noop_all_to_null (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GPtrArray *args;
+ GSubprocess *proc;
+
+ args = get_test_subprocess_args ("noop", NULL);
+ proc = g_subprocess_new ((char**) args->pdata, NULL, NULL, 0, NULL, NULL,
+ g_subprocess_stream_devnull (),
+ g_subprocess_stream_devnull (),
+ g_subprocess_stream_devnull (),
+ error);
+ g_ptr_array_free (args, TRUE);
+ g_assert_no_error (local_error);
+
+ (void)g_subprocess_wait_sync_check (proc, NULL, error);
+ g_assert_no_error (local_error);
+
+ g_object_unref (proc);
+}
+
+static void
+test_noop_no_wait (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GPtrArray *args;
+ GSubprocess *proc;
+
+ args = get_test_subprocess_args ("noop", NULL);
+ proc = g_subprocess_new ((char**) args->pdata, NULL, NULL, 0, NULL, NULL,
+ g_subprocess_stream_devnull (),
+ g_subprocess_stream_inherit (),
+ g_subprocess_stream_inherit (),
+ error);
+ g_ptr_array_free (args, TRUE);
+ g_assert_no_error (local_error);
+
+ g_object_unref (proc);
+}
+
+static void
+test_noop_stdin_inherit (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GPtrArray *args;
+ GSubprocess *proc;
+
+ args = get_test_subprocess_args ("noop", NULL);
+ proc = g_subprocess_new ((char**) args->pdata, NULL, NULL, 0, NULL, NULL,
+ g_subprocess_stream_inherit (),
+ g_subprocess_stream_inherit (),
+ g_subprocess_stream_inherit (),
+ error);
+ g_ptr_array_free (args, TRUE);
+ g_assert_no_error (local_error);
+
+ (void)g_subprocess_wait_sync_check (proc, NULL, error);
+ g_assert_no_error (local_error);
+
+ g_object_unref (proc);
+}
+
+
+#ifdef G_OS_UNIX
+static void
+test_search_path (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GPtrArray *args;
+ GSubprocess *proc;
+
+ args = g_ptr_array_new ();
+ g_ptr_array_add (args, "true");
+ g_ptr_array_add (args, NULL);
+ proc = g_subprocess_new ((char**) args->pdata, NULL, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
+ g_subprocess_stream_inherit (),
+ g_subprocess_stream_inherit (),
+ g_subprocess_stream_inherit (),
+ error);
+ g_ptr_array_free (args, TRUE);
+ g_assert_no_error (local_error);
+
+ (void)g_subprocess_wait_sync_check (proc, NULL, error);
+ g_assert_no_error (local_error);
+
+ g_object_unref (proc);
+}
+#endif
+
+static void
+test_exit1 (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GPtrArray *args;
+ GSubprocess *proc;
+
+ args = get_test_subprocess_args ("exit1", NULL);
+ proc = g_subprocess_new ((char**) args->pdata, NULL, NULL, 0, NULL, NULL,
+ g_subprocess_stream_devnull (),
+ g_subprocess_stream_inherit (),
+ g_subprocess_stream_inherit (),
+ error);
+ g_ptr_array_free (args, TRUE);
+ g_assert_no_error (local_error);
+
+ (void)g_subprocess_wait_sync_check (proc, NULL, error);
+ g_assert_error (local_error, G_SPAWN_EXIT_ERROR, 1);
+ g_clear_error (error);
+
+ g_object_unref (proc);
+}
+
+static gchar *
+splice_to_string (GInputStream *stream,
+ GError **error)
+{
+ GMemoryOutputStream *buffer = NULL;
+ char *ret = NULL;
+
+ buffer = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
+ if (g_output_stream_splice ((GOutputStream*)buffer, stream, 0, NULL, error) < 0)
+ goto out;
+
+ if (!g_output_stream_write ((GOutputStream*)buffer, "\0", 1, NULL, error))
+ goto out;
+
+ if (!g_output_stream_close ((GOutputStream*)buffer, NULL, error))
+ goto out;
+
+ ret = g_memory_output_stream_steal_data (buffer);
+ out:
+ g_clear_object (&buffer);
+ return ret;
+}
+
+static void
+test_echo1 (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GSubprocess *proc;
+ GPtrArray *args;
+ GInputStream *stdout;
+ gchar *result;
+
+ args = get_test_subprocess_args ("echo", "hello", "world!", NULL);
+ proc = g_subprocess_new ((char**) args->pdata, NULL, NULL, 0, NULL, NULL,
+ g_subprocess_stream_devnull (),
+ g_subprocess_stream_pipe (),
+ g_subprocess_stream_inherit (),
+ error);
+ g_ptr_array_free (args, TRUE);
+ g_assert_no_error (local_error);
+
+ stdout = g_subprocess_get_stdout_pipe (proc);
+
+ result = splice_to_string (stdout, error);
+ g_assert_no_error (local_error);
+
+ g_assert_cmpstr (result, ==, "hello\nworld!\n");
+
+ g_free (result);
+ g_object_unref (proc);
+}
+
+#ifdef G_OS_UNIX
+static void
+test_echo_merged (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GSubprocess *proc;
+ GPtrArray *args;
+ GInputStream *stdout;
+ gchar *result;
+
+ args = get_test_subprocess_args ("echo-stdout-and-stderr", "merge", "this", NULL);
+ proc = g_subprocess_new ((char**) args->pdata, NULL, NULL, 0, NULL, NULL,
+ g_subprocess_stream_devnull (),
+ g_subprocess_stream_pipe (),
+ g_subprocess_stream_merge_stdout (),
+ error);
+ g_ptr_array_free (args, TRUE);
+ g_assert_no_error (local_error);
+
+ stdout = g_subprocess_get_stdout_pipe (proc);
+ result = splice_to_string (stdout, error);
+ g_assert_no_error (local_error);
+
+ g_assert_cmpstr (result, ==, "merge\nmerge\nthis\nthis\n");
+
+ g_free (result);
+ g_object_unref (proc);
+}
+#endif
+
+typedef struct {
+ guint events_pending;
+ GMainLoop *loop;
+} TestCatData;
+
+static void
+test_cat_on_input_splice_complete (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ TestCatData *data = user_data;
+ GError *error = NULL;
+
+ (void)g_output_stream_splice_finish ((GOutputStream*)object, result, &error);
+ g_assert_no_error (error);
+
+ data->events_pending--;
+ if (data->events_pending == 0)
+ g_main_loop_quit (data->loop);
+}
+
+static void
+test_cat_utf8 (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GSubprocess *proc;
+ GPtrArray *args;
+ GBytes *input_buf;
+ GBytes *output_buf;
+ GInputStream *input_buf_stream = NULL;
+ GOutputStream *output_buf_stream = NULL;
+ GOutputStream *stdin_stream = NULL;
+ GInputStream *stdout_stream = NULL;
+ TestCatData data;
+
+ memset (&data, 0, sizeof (data));
+ data.loop = g_main_loop_new (NULL, TRUE);
+
+ args = get_test_subprocess_args ("cat", NULL);
+ proc = g_subprocess_new ((char**) args->pdata, NULL, NULL, 0, NULL, NULL,
+ g_subprocess_stream_pipe (),
+ g_subprocess_stream_pipe (),
+ g_subprocess_stream_inherit (),
+ error);
+ g_ptr_array_free (args, TRUE);
+ g_assert_no_error (local_error);
+
+ stdin_stream = g_subprocess_get_stdin_pipe (proc);
+ stdout_stream = g_subprocess_get_stdout_pipe (proc);
+
+ input_buf = g_bytes_new_static ("hello, world!", strlen ("hello, world!"));
+ input_buf_stream = g_memory_input_stream_new_from_bytes (input_buf);
+ g_bytes_unref (input_buf);
+
+ output_buf_stream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
+
+ g_output_stream_splice_async (stdin_stream, input_buf_stream, G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
+ G_PRIORITY_DEFAULT, NULL, test_cat_on_input_splice_complete,
+ &data);
+ data.events_pending++;
+ g_output_stream_splice_async (output_buf_stream, stdout_stream, G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
+ G_PRIORITY_DEFAULT, NULL, test_cat_on_input_splice_complete,
+ &data);
+ data.events_pending++;
+
+ g_main_loop_run (data.loop);
+
+ g_subprocess_wait_sync_check (proc, NULL, error);
+ g_assert_no_error (local_error);
+
+ output_buf = g_memory_output_stream_steal_as_bytes ((GMemoryOutputStream*)output_buf_stream);
+
+ g_assert_cmpint (g_bytes_get_size (output_buf), ==, 13);
+ g_assert_cmpint (memcmp (g_bytes_get_data (output_buf, NULL), "hello, world!", 13), ==, 0);
+
+ g_bytes_unref (output_buf);
+ g_main_loop_unref (data.loop);
+ g_object_unref (input_buf_stream);
+ g_object_unref (output_buf_stream);
+ g_object_unref (proc);
+}
+
+#ifdef G_OS_UNIX
+static void
+test_file_redirection (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ char *expected_buf = NULL;
+ GSubprocess *proc;
+ GPtrArray *args;
+ GBytes *input_buf;
+ GBytes *output_buf;
+ GInputStream *input_buf_stream = NULL;
+ GOutputStream *output_buf_stream = NULL;
+ GOutputStream *stdin_stream = NULL;
+ GInputStream *stdout_stream = NULL;
+ GFile *temp_file = NULL;
+ GFileIOStream *temp_file_io = NULL;
+ TestCatData data;
+
+ memset (&data, 0, sizeof (data));
+ data.loop = g_main_loop_new (NULL, TRUE);
+
+ expected_buf = g_strdup_printf ("this is a test file, written by pid:%lu at monotonic time:%" G_GUINT64_FORMAT,
+ (gulong) getpid (), g_get_monotonic_time ());
+
+ temp_file = g_file_new_tmp ("gsubprocess-tmpXXXXXX",
+ &temp_file_io, error);
+ g_assert_no_error (local_error);
+
+ g_io_stream_close ((GIOStream*)temp_file_io, NULL, error);
+ g_assert_no_error (local_error);
+
+ args = get_test_subprocess_args ("cat", NULL);
+ proc = g_subprocess_new ((char**) args->pdata, NULL, NULL, 0, NULL, NULL,
+ g_subprocess_stream_pipe (),
+ (GObject*) temp_file,
+ g_subprocess_stream_inherit (),
+ error);
+ g_ptr_array_free (args, TRUE);
+ g_assert_no_error (local_error);
+
+ stdin_stream = g_subprocess_get_stdin_pipe (proc);
+
+ input_buf = g_bytes_new_take (expected_buf, strlen (expected_buf));
+ expected_buf = NULL;
+ input_buf_stream = g_memory_input_stream_new_from_bytes (input_buf);
+
+ g_output_stream_splice_async (stdin_stream, input_buf_stream, G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
+ G_PRIORITY_DEFAULT, NULL, test_cat_on_input_splice_complete,
+ &data);
+ data.events_pending++;
+
+ g_main_loop_run (data.loop);
+
+ g_subprocess_wait_sync_check (proc, NULL, error);
+ g_assert_no_error (local_error);
+
+ g_object_unref (proc);
+ g_object_unref (input_buf_stream);
+
+ args = get_test_subprocess_args ("cat", NULL);
+ proc = g_subprocess_new ((char**) args->pdata, NULL, NULL, 0, NULL, NULL,
+ (GObject*)temp_file,
+ g_subprocess_stream_pipe (),
+ g_subprocess_stream_inherit (),
+ error);
+ g_ptr_array_free (args, TRUE);
+ g_assert_no_error (local_error);
+
+ stdout_stream = g_subprocess_get_stdout_pipe (proc);
+ output_buf_stream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
+
+ g_output_stream_splice_async (output_buf_stream, stdout_stream, G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
+ G_PRIORITY_DEFAULT, NULL, test_cat_on_input_splice_complete,
+ &data);
+ data.events_pending++;
+
+ g_main_loop_run (data.loop);
+
+ output_buf = g_memory_output_stream_steal_as_bytes ((GMemoryOutputStream*)output_buf_stream);
+
+ g_assert_cmpint (g_bytes_get_size (output_buf), ==, g_bytes_get_size (input_buf));
+ g_assert_cmpint (memcmp (g_bytes_get_data (output_buf, NULL),
+ g_bytes_get_data (input_buf, NULL), g_bytes_get_size (input_buf)), ==, 0);
+
+ g_bytes_unref (input_buf);
+ g_bytes_unref (output_buf);
+ g_main_loop_unref (data.loop);
+ g_object_unref (output_buf_stream);
+ g_object_unref (proc);
+
+ g_file_delete (temp_file, NULL, error);
+ g_assert_no_error (local_error);
+ g_object_unref (temp_file);
+ g_object_unref (temp_file_io);
+}
+
+static void
+test_unix_fd_passing (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GSubprocess *proc;
+ GPtrArray *args;
+ GOutputStream *stdin;
+ GFile *temp_file = NULL;
+ GFileIOStream *temp_file_io = NULL;
+ GOutputStream *temp_file_out = NULL;
+ gsize bytes_written;
+ gchar *write_buf;
+ gchar *read_buf;
+
+ temp_file = g_file_new_tmp ("gsubprocess-tmpXXXXXX",
+ &temp_file_io, error);
+
+ temp_file_out = g_io_stream_get_output_stream ((GIOStream*)temp_file_io);
+ g_assert (G_IS_FILE_DESCRIPTOR_BASED (temp_file_out));
+
+ args = get_test_subprocess_args ("cat", NULL);
+ proc = g_subprocess_new ((char**) args->pdata, NULL, NULL, 0, NULL, NULL,
+ g_subprocess_stream_pipe (),
+ (GObject*)temp_file_out,
+ g_subprocess_stream_inherit (),
+ error);
+ g_ptr_array_free (args, TRUE);
+ g_assert_no_error (local_error);
+
+ g_io_stream_close ((GIOStream*)temp_file_io, NULL, error);
+ g_assert_no_error (local_error);
+
+ stdin = g_subprocess_get_stdin_pipe (proc);
+
+ write_buf = g_strdup_printf ("fd-passing timestamp:%" G_GUINT64_FORMAT,
+ g_get_monotonic_time ());
+ g_output_stream_write_all (stdin, write_buf, strlen (write_buf), &bytes_written,
+ NULL, error);
+ g_assert_no_error (local_error);
+
+ g_output_stream_close (stdin, NULL, error);
+ g_assert_no_error (local_error);
+
+ g_subprocess_wait_sync_check (proc, NULL, error);
+ g_assert_no_error (local_error);
+
+ g_file_load_contents (temp_file, NULL, &read_buf, &bytes_written, NULL, error);
+ g_assert_no_error (local_error);
+
+ g_assert_cmpstr (read_buf, ==, write_buf);
+
+ g_free (read_buf);
+ g_free (write_buf);
+ g_object_unref (proc);
+
+ g_file_delete (temp_file, NULL, error);
+ g_assert_no_error (local_error);
+ g_object_unref (temp_file);
+ g_object_unref (temp_file_io);
+}
+#endif
+
+typedef struct {
+ guint events_pending;
+ gboolean caught_error;
+ GError *error;
+ GMainLoop *loop;
+
+ gint counter;
+ GOutputStream *first_stdin;
+} TestMultiSpliceData;
+
+static void
+on_one_multi_splice_done (GObject *obj,
+ GAsyncResult *res,
+ gpointer user_data)
+{
+ TestMultiSpliceData *data = user_data;
+
+ if (!data->caught_error)
+ {
+ if (g_output_stream_splice_finish ((GOutputStream*)obj, res, &data->error) < 0)
+ data->caught_error = TRUE;
+ }
+
+ data->events_pending--;
+ if (data->events_pending == 0)
+ g_main_loop_quit (data->loop);
+}
+
+static gboolean
+on_idle_multisplice (gpointer user_data)
+{
+ TestMultiSpliceData *data = user_data;
+
+ /* We write 2^1 + 2^2 ... + 2^10 or 2047 copies of "Hello World!\n"
+ * ultimately
+ */
+ if (data->counter >= 2047 || data->caught_error)
+ {
+ if (!g_output_stream_close (data->first_stdin, NULL, &data->error))
+ data->caught_error = TRUE;
+ data->events_pending--;
+ if (data->events_pending == 0)
+ {
+ g_main_loop_quit (data->loop);
+ }
+ return FALSE;
+ }
+ else
+ {
+ int i;
+ for (i = 0; i < data->counter; i++)
+ {
+ gsize bytes_written;
+ if (!g_output_stream_write_all (data->first_stdin, "hello world!\n",
+ strlen ("hello world!\n"), &bytes_written,
+ NULL, &data->error))
+ {
+ data->caught_error = TRUE;
+ return FALSE;
+ }
+ }
+ data->counter *= 2;
+ return TRUE;
+ }
+}
+
+static void
+on_subprocess_exited (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ TestMultiSpliceData *data = user_data;
+ GError *error = NULL;
+ int exit_status;
+
+ if (!g_subprocess_wait_finish ((GSubprocess*)object, result, &exit_status, &error))
+ {
+ if (!data->caught_error)
+ {
+ data->caught_error = TRUE;
+ g_propagate_error (&data->error, error);
+ }
+ }
+ g_spawn_check_exit_status (exit_status, &error);
+ g_assert_no_error (error);
+ data->events_pending--;
+ if (data->events_pending == 0)
+ g_main_loop_quit (data->loop);
+}
+
+static void
+test_multi_1 (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GPtrArray *args;
+ GSubprocess *first;
+ GSubprocess *second;
+ GSubprocess *third;
+ GOutputStream *first_stdin;
+ GInputStream *first_stdout;
+ GOutputStream *second_stdin;
+ GInputStream *second_stdout;
+ GOutputStream *third_stdin;
+ GInputStream *third_stdout;
+ GOutputStream *membuf;
+ TestMultiSpliceData data;
+ int splice_flags = G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET;
+
+ args = get_test_subprocess_args ("cat", NULL);
+ first = g_subprocess_new ((char**) args->pdata, NULL, NULL, 0, NULL, NULL,
+ g_subprocess_stream_pipe (),
+ g_subprocess_stream_pipe (),
+ g_subprocess_stream_inherit (),
+ error);
+ g_assert_no_error (local_error);
+ second = g_subprocess_new ((char**) args->pdata, NULL, NULL, 0, NULL, NULL,
+ g_subprocess_stream_pipe (),
+ g_subprocess_stream_pipe (),
+ g_subprocess_stream_inherit (),
+ error);
+ g_assert_no_error (local_error);
+ third = g_subprocess_new ((char**) args->pdata, NULL, NULL, 0, NULL, NULL,
+ g_subprocess_stream_pipe (),
+ g_subprocess_stream_pipe (),
+ g_subprocess_stream_inherit (),
+ error);
+ g_assert_no_error (local_error);
+
+ g_ptr_array_free (args, TRUE);
+
+ membuf = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
+
+ first_stdin = g_subprocess_get_stdin_pipe (first);
+ first_stdout = g_subprocess_get_stdout_pipe (first);
+ second_stdin = g_subprocess_get_stdin_pipe (second);
+ second_stdout = g_subprocess_get_stdout_pipe (second);
+ third_stdin = g_subprocess_get_stdin_pipe (third);
+ third_stdout = g_subprocess_get_stdout_pipe (third);
+
+ memset (&data, 0, sizeof (data));
+ data.loop = g_main_loop_new (NULL, TRUE);
+ data.counter = 1;
+ data.first_stdin = first_stdin;
+
+ data.events_pending++;
+ g_output_stream_splice_async (second_stdin, first_stdout, splice_flags, G_PRIORITY_DEFAULT,
+ NULL, on_one_multi_splice_done, &data);
+ data.events_pending++;
+ g_output_stream_splice_async (third_stdin, second_stdout, splice_flags, G_PRIORITY_DEFAULT,
+ NULL, on_one_multi_splice_done, &data);
+ data.events_pending++;
+ g_output_stream_splice_async (membuf, third_stdout, splice_flags, G_PRIORITY_DEFAULT,
+ NULL, on_one_multi_splice_done, &data);
+
+ data.events_pending++;
+ g_timeout_add (250, on_idle_multisplice, &data);
+
+ data.events_pending++;
+ g_subprocess_wait (first, NULL, on_subprocess_exited, &data);
+ data.events_pending++;
+ g_subprocess_wait (second, NULL, on_subprocess_exited, &data);
+ data.events_pending++;
+ g_subprocess_wait (third, NULL, on_subprocess_exited, &data);
+
+ g_main_loop_run (data.loop);
+
+ g_assert (!data.caught_error);
+ g_assert_no_error (data.error);
+
+ g_assert_cmpint (g_memory_output_stream_get_data_size ((GMemoryOutputStream*)membuf), ==, 26611);
+
+ g_main_loop_unref (data.loop);
+ g_object_unref (membuf);
+ g_object_unref (first);
+ g_object_unref (second);
+ g_object_unref (third);
+}
+
+/*
+ TODO -
+ This doesn't work because libtool eats our argv0 trick =/
+*/
+#if 0
+static void
+test_argv0 (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GSubprocess *proc;
+
+ proc = get_test_subprocess ("assert-argv0");
+
+ g_subprocess_set_argv0 (proc, "moocow");
+
+ (void)g_subprocess_run_sync_check (proc, NULL, error);
+ g_assert_no_error (local_error);
+
+ g_object_unref (proc);
+}
+#endif
+
+static gboolean
+send_terminate (gpointer user_data)
+{
+ GSubprocess *proc = user_data;
+
+ g_subprocess_force_exit (proc);
+
+ return FALSE;
+}
+
+static void
+on_request_quit_exited (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GError *error = NULL;
+ int exit_status;
+
+ (void)g_subprocess_wait_finish ((GSubprocess*)object, result, &exit_status, &error);
+ g_assert_no_error (error);
+#ifdef G_OS_UNIX
+ g_assert (WIFSIGNALED (exit_status) && WTERMSIG (exit_status) == 9);
+#endif
+ g_spawn_check_exit_status (exit_status, &error);
+ g_assert (error != NULL);
+ g_clear_error (&error);
+
+ g_main_loop_quit ((GMainLoop*)user_data);
+}
+
+static void
+test_terminate (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GSubprocess *proc;
+ GPtrArray *args;
+ GMainLoop *loop;
+
+ args = get_test_subprocess_args ("sleep-forever", NULL);
+ proc = g_subprocess_new ((char**) args->pdata, NULL, NULL, 0, NULL, NULL,
+ g_subprocess_stream_devnull (),
+ g_subprocess_stream_inherit (),
+ g_subprocess_stream_inherit (),
+ error);
+ g_ptr_array_free (args, TRUE);
+ g_assert_no_error (local_error);
+
+ loop = g_main_loop_new (NULL, TRUE);
+
+ g_subprocess_wait (proc, NULL, on_request_quit_exited, loop);
+
+ g_timeout_add_seconds (3, send_terminate, proc);
+
+ g_main_loop_run (loop);
+
+ g_main_loop_unref (loop);
+ g_object_unref (proc);
+}
+
+int
+main (int argc, char **argv)
+{
+ g_type_init ();
+
+ g_test_init (&argc, &argv, NULL);
+
+ g_test_add_func ("/gsubprocess/noop", test_noop);
+ g_test_add_func ("/gsubprocess/noop-all-to-null", test_noop_all_to_null);
+ g_test_add_func ("/gsubprocess/noop-no-wait", test_noop_no_wait);
+ g_test_add_func ("/gsubprocess/noop-stdin-inherit", test_noop_stdin_inherit);
+#ifdef G_OS_UNIX
+ g_test_add_func ("/gsubprocess/search-path", test_search_path);
+#endif
+ g_test_add_func ("/gsubprocess/exit1", test_exit1);
+ g_test_add_func ("/gsubprocess/echo1", test_echo1);
+#ifdef G_OS_UNIX
+ g_test_add_func ("/gsubprocess/echo-merged", test_echo_merged);
+#endif
+ g_test_add_func ("/gsubprocess/cat-utf8", test_cat_utf8);
+#ifdef G_OS_UNIX
+ g_test_add_func ("/gsubprocess/file-redirection", test_file_redirection);
+ g_test_add_func ("/gsubprocess/unix-fd-passing", test_unix_fd_passing);
+#endif
+ g_test_add_func ("/gsubprocess/multi1", test_multi_1);
+ g_test_add_func ("/gsubprocess/terminate", test_terminate);
+ /* g_test_add_func ("/gsubprocess/argv0", test_argv0); */
+
+ return g_test_run ();
+}
diff --git a/glib/gspawn.h b/glib/gspawn.h
index 4036fa6..0d1277f 100644
--- a/glib/gspawn.h
+++ b/glib/gspawn.h
@@ -181,6 +181,7 @@ typedef enum
G_SPAWN_CHILD_INHERITS_STDIN = 1 << 5,
G_SPAWN_FILE_AND_ARGV_ZERO = 1 << 6,
G_SPAWN_SEARCH_PATH_FROM_ENVP = 1 << 7
+ /* Note if you add a value here, update gobject/genums.c too */
} GSpawnFlags;
GQuark g_spawn_error_quark (void);
diff --git a/gobject/genums.c b/gobject/genums.c
index 1b3411f..96332af 100644
--- a/gobject/genums.c
+++ b/gobject/genums.c
@@ -619,3 +619,29 @@ g_value_get_flags (const GValue *value)
return value->data[0].v_ulong;
}
+
+GType
+g_spawn_flags_get_type (void)
+{
+ static volatile gsize g_define_type_id__volatile = 0;
+
+ if (g_once_init_enter (&g_define_type_id__volatile))
+ {
+ static const GFlagsValue values[] = {
+ { G_SPAWN_LEAVE_DESCRIPTORS_OPEN, "G_SPAWN_LEAVE_DESCRIPTORS_OPEN", "leave-descriptors-open" },
+ { G_SPAWN_DO_NOT_REAP_CHILD, "G_SPAWN_DO_NOT_REAP_CHILD", "do-not-reap-child" },
+ { G_SPAWN_SEARCH_PATH, "G_SPAWN_SEARCH_PATH", "search-path" },
+ { G_SPAWN_STDOUT_TO_DEV_NULL, "G_SPAWN_STDOUT_TO_DEV_NULL", "stdout-to-dev-null" },
+ { G_SPAWN_STDERR_TO_DEV_NULL, "G_SPAWN_STDERR_TO_DEV_NULL", "stderr-to-dev-null" },
+ { G_SPAWN_CHILD_INHERITS_STDIN, "G_SPAWN_CHILD_INHERITS_STDIN", "child-inherits-stdin" },
+ { G_SPAWN_FILE_AND_ARGV_ZERO, "G_SPAWN_FILE_AND_ARGV_ZERO", "file-and-argv-zero" },
+ { G_SPAWN_SEARCH_PATH_FROM_ENVP, "G_SPAWN_SEARCH_PATH_FROM_ENVP", "search-path-from-envp" },
+ { 0, NULL, NULL }
+ };
+ GType g_define_type_id =
+ g_flags_register_static (g_intern_static_string ("GSpawnFlags"), values);
+ g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
+ }
+
+ return g_define_type_id__volatile;
+}
diff --git a/gobject/genums.h b/gobject/genums.h
index bf8a419..38938b3 100644
--- a/gobject/genums.h
+++ b/gobject/genums.h
@@ -256,6 +256,9 @@ void g_flags_complete_type_info (GType g_flags_type,
GTypeInfo *info,
const GFlagsValue *const_values);
+#define G_TYPE_SPAWN_FLAGS (g_spawn_flags_get_type ())
+GType g_spawn_flags_get_type (void) G_GNUC_CONST;
+
G_END_DECLS
#endif /* __G_ENUMS_H__ */
diff --git a/gobject/glib-types.h b/gobject/glib-types.h
index e262534..4bdad3a 100644
--- a/gobject/glib-types.h
+++ b/gobject/glib-types.h
@@ -255,6 +255,8 @@ GType g_source_get_type (void) G_GNUC_CONST;
GLIB_DEPRECATED_FOR('G_TYPE_VARIANT')
GType g_variant_get_gtype (void) G_GNUC_CONST;
+GType g_spawn_flags_get_type (void) G_GNUC_CONST;
+
/**
* GStrv:
*
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]