[glib/wip/gsubprocess-2.36: 4/4] GSubprocess: New class for spawning child processes
- From: Colin Walters <walters src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/wip/gsubprocess-2.36: 4/4] GSubprocess: New class for spawning child processes
- Date: Sat, 10 Nov 2012 18:22:04 +0000 (UTC)
commit 10a851b591eaccecdcf84a53562d78bdd1cb3541
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, if WNOWAIT is available, 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
3) Makes some simple cases easy, like synchronously spawning a
process with an argument list
4) Makes hard cases possible, like asynchronously running a process
with stdout/stderr merged, output directly to a file path
Much rewriting and code review from Ryan Lortie <desrt desrt ca>
https://bugzilla.gnome.org/show_bug.cgi?id=672102
docs/reference/gio/gio-docs.xml | 4 +
docs/reference/gio/gio-sections.txt | 26 +
docs/reference/gio/gio.types | 1 +
gio/Makefile.am | 5 +
gio/gio.h | 2 +
gio/gio.symbols | 47 ++
gio/gioenums.h | 19 +
gio/giotypes.h | 18 +
gio/glib-compile-resources.c | 89 ++---
gio/gsubprocess.c | 864 +++++++++++++++++++++++++++++++++++
gio/gsubprocess.h | 104 +++++
gio/gsubprocesscontext-private.h | 62 +++
gio/gsubprocesscontext.c | 307 +++++++++++++
gio/gsubprocesscontext.h | 118 +++++
gio/tests/.gitignore | 2 +
gio/tests/Makefile.am | 3 +
gio/tests/gsubprocess-testprog.c | 174 +++++++
gio/tests/gsubprocess.c | 829 +++++++++++++++++++++++++++++++++
18 files changed, 2620 insertions(+), 54 deletions(-)
---
diff --git a/docs/reference/gio/gio-docs.xml b/docs/reference/gio/gio-docs.xml
index 03dd28d..7c9ba32 100644
--- a/docs/reference/gio/gio-docs.xml
+++ b/docs/reference/gio/gio-docs.xml
@@ -107,6 +107,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 a3a0fb0..d073a88 100644
--- a/docs/reference/gio/gio-sections.txt
+++ b/docs/reference/gio/gio-sections.txt
@@ -3974,3 +3974,29 @@ G_IS_TASK_CLASS
G_TASK_GET_CLASS
g_task_get_type
</SECTION>
+
+<SECTION>
+<FILE>gsubprocess</FILE>
+<TITLE>GSubprocess</TITLE>
+GSubprocess
+g_subprocess_new
+<SUBSECTION IO>
+g_subprocess_get_stdin_pipe
+g_subprocess_get_stdout_pipe
+g_subprocess_get_stderr_pipe
+<SUBSECTION Waiting>
+g_subprocess_wait
+g_subprocess_wait_finish
+g_subprocess_wait_sync
+g_subprocess_wait_sync_check
+<SUBSECTION Control>
+g_subprocess_get_pid
+g_subprocess_request_exit
+g_subprocess_force_exit
+<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 5cd996c..98ba8bf 100644
--- a/docs/reference/gio/gio.types
+++ b/docs/reference/gio/gio.types
@@ -136,3 +136,4 @@ g_menu_item_get_type
g_test_dbus_get_type
g_test_dbus_flags_get_type
g_task_get_type
+g_subprocess_get_type
diff --git a/gio/Makefile.am b/gio/Makefile.am
index 9732ab7..eeaa379 100644
--- a/gio/Makefile.am
+++ b/gio/Makefile.am
@@ -427,6 +427,9 @@ libgio_2_0_la_SOURCES = \
gsocketlistener.c \
gsocketoutputstream.c \
gsocketoutputstream.h \
+ gsubprocess.c \
+ gsubprocesscontext.c \
+ gsubprocesscontext-private.h \
gproxy.c \
gproxyaddress.c \
gproxyaddressenumerator.c \
@@ -603,6 +606,8 @@ gio_headers = \
gsocketservice.h \
gsrvtarget.h \
gtask.h \
+ gsubprocess.h \
+ gsubprocesscontext.h \
gtcpconnection.h \
gtcpwrapperconnection.h \
gthreadedsocketservice.h\
diff --git a/gio/gio.h b/gio/gio.h
index 1998a64..f52e01b 100644
--- a/gio/gio.h
+++ b/gio/gio.h
@@ -123,6 +123,8 @@
#include <gio/gsocketservice.h>
#include <gio/gsrvtarget.h>
#include <gio/gtask.h>
+#include <gio/gsubprocess.h>
+#include <gio/gsubprocesscontext.h>
#include <gio/gtcpconnection.h>
#include <gio/gtcpwrapperconnection.h>
#include <gio/gtestdbus.h>
diff --git a/gio/gio.symbols b/gio/gio.symbols
index b2a95b2..9d53ddc 100644
--- a/gio/gio.symbols
+++ b/gio/gio.symbols
@@ -1097,6 +1097,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/gioenums.h b/gio/gioenums.h
index b370623..c9ccef7 100644
--- a/gio/gioenums.h
+++ b/gio/gioenums.h
@@ -1653,6 +1653,25 @@ typedef enum /*< flags >*/ {
G_TEST_DBUS_NONE = 0
} GTestDBusFlags;
+/**
+ * GSubprocessStreamDisposition:
+ * @G_SUBPROCESS_STREAM_DISPOSITION_NULL: Redirect to operating system's null output stream
+ * @G_SUBPROCESS_STREAM_DISPOSITION_INHERIT: Keep the stream from the parent process
+ * @G_SUBPROCESS_STREAM_DISPOSITION_PIPE: Open a private unidirectional channel between the processes
+ * @G_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 #GSubprocess.
+ *
+ * Since: 2.36
+ **/
+typedef enum {
+ G_SUBPROCESS_STREAM_DISPOSITION_NULL,
+ G_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
+ G_SUBPROCESS_STREAM_DISPOSITION_PIPE,
+ G_SUBPROCESS_STREAM_DISPOSITION_STDERR_MERGE
+} GSubprocessStreamDisposition;
+
G_END_DECLS
#endif /* __GIO_ENUMS_H__ */
diff --git a/gio/giotypes.h b/gio/giotypes.h
index 82845c5..3c5153d 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:
*
@@ -468,6 +469,23 @@ typedef GType (*GDBusProxyTypeFunc) (GDBusObjectManagerClient *manager,
typedef struct _GTestDBus GTestDBus;
+/**
+ * GSubprocess:
+ *
+ * A child process.
+ *
+ * Since: 2.36
+ */
+typedef struct _GSubprocess GSubprocess;
+/**
+ * GSubprocessContext:
+ *
+ * Options for launching a child process.
+ *
+ * Since: 2.36
+ */
+typedef struct _GSubprocessContext GSubprocessContext;
+
G_END_DECLS
#endif /* __GIO_TYPES_H__ */
diff --git a/gio/glib-compile-resources.c b/gio/glib-compile-resources.c
index f8ac652..ade4ee8 100644
--- a/gio/glib-compile-resources.c
+++ b/gio/glib-compile-resources.c
@@ -293,9 +293,8 @@ end_element (GMarkupParseContext *context,
if (xml_stripblanks && xmllint != NULL)
{
- gchar *argv[8];
- int status, fd, argc;
- gchar *stderr_child = NULL;
+ int fd;
+ GSubprocess *proc;
tmp_file = g_strdup ("resource-XXXXXXXX");
if ((fd = g_mkstemp (tmp_file)) == -1)
@@ -311,43 +310,34 @@ end_element (GMarkupParseContext *context,
}
close (fd);
- argc = 0;
- argv[argc++] = (gchar *) xmllint;
- argv[argc++] = "--nonet";
- argv[argc++] = "--noblanks";
- argv[argc++] = "--output";
- argv[argc++] = tmp_file;
- argv[argc++] = real_file;
- argv[argc++] = NULL;
- g_assert (argc <= G_N_ELEMENTS (argv));
-
- if (!g_spawn_sync (NULL /* cwd */, argv, NULL /* envv */,
- G_SPAWN_STDOUT_TO_DEV_NULL,
- NULL, NULL, NULL, &stderr_child, &status, &my_error))
- {
- g_propagate_error (error, my_error);
- goto cleanup;
- }
-
- /* Ugly...we shoud probably just let stderr be inherited */
- if (!g_spawn_check_exit_status (status, NULL))
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
- _("Error processing input file with xmllint:\n%s"), stderr_child);
- g_free (stderr_child);
+ proc = g_subprocess_new_simple_argl (G_SUBPROCESS_STREAM_DISPOSITION_NULL,
+ G_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
+ error,
+ xmllint,
+ "--nonet", "--noblanks",
+ "--output", tmp_file,
+ real_file, NULL);
+ g_free (real_file);
+ real_file = NULL;
+
+ if (!proc)
+ goto cleanup;
+
+ if (!g_subprocess_wait_sync_check (proc, NULL, error))
+ {
+ g_object_unref (proc);
goto cleanup;
}
- g_free (stderr_child);
- g_free (real_file);
+ g_object_unref (proc);
+
real_file = g_strdup (tmp_file);
}
if (to_pixdata)
{
- gchar *argv[4];
- gchar *stderr_child = NULL;
- int status, fd, argc;
+ int fd;
+ GSubprocess *proc;
if (gdk_pixbuf_pixdata == NULL)
{
@@ -371,31 +361,22 @@ end_element (GMarkupParseContext *context,
}
close (fd);
- argc = 0;
- argv[argc++] = (gchar *) gdk_pixbuf_pixdata;
- argv[argc++] = real_file;
- argv[argc++] = tmp_file2;
- argv[argc++] = NULL;
- g_assert (argc <= G_N_ELEMENTS (argv));
+ proc = g_subprocess_new_simple_argl (G_SUBPROCESS_STREAM_DISPOSITION_NULL,
+ G_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
+ error,
+ gdk_pixbuf_pixdata, real_file, tmp_file2,
+ NULL);
+ g_free (real_file);
+ real_file = NULL;
- if (!g_spawn_sync (NULL /* cwd */, argv, NULL /* envv */,
- G_SPAWN_STDOUT_TO_DEV_NULL,
- NULL, NULL, NULL, &stderr_child, &status, &my_error))
- {
- g_propagate_error (error, my_error);
- goto cleanup;
- }
-
- if (!g_spawn_check_exit_status (status, NULL))
- {
- g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
- _("Error processing input file with to-pixdata:\n%s"), stderr_child);
- g_free (stderr_child);
+ if (!g_subprocess_wait_sync_check (proc, NULL, error))
+ {
+ g_object_unref (proc);
goto cleanup;
- }
+ }
+
+ g_object_unref (proc);
- g_free (stderr_child);
- g_free (real_file);
real_file = g_strdup (tmp_file2);
}
}
diff --git a/gio/gsubprocess.c b/gio/gsubprocess.c
new file mode 100644
index 0000000..8e70aaa
--- /dev/null
+++ b/gio/gsubprocess.c
@@ -0,0 +1,864 @@
+/* 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>
+ */
+
+/**
+ * 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.
+ *
+ * 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 "gsubprocess.h"
+#include "gsubprocesscontext-private.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
+
+static void initable_iface_init (GInitableIface *initable_iface);
+
+typedef GObjectClass GSubprocessClass;
+
+#ifdef G_OS_UNIX
+static void
+g_subprocess_unix_queue_waitpid (GSubprocess *self);
+#endif
+
+struct _GSubprocess
+{
+ GObject parent;
+
+ GSubprocessContext *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 (GSubprocess, g_subprocess, G_TYPE_OBJECT,
+ G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, initable_iface_init));
+
+enum
+{
+ PROP_0,
+ PROP_CONTEXT,
+ N_PROPS
+};
+
+static GParamSpec *g_subprocess_pspecs[N_PROPS];
+
+static void
+g_subprocess_init (GSubprocess *self)
+{
+}
+
+static void
+g_subprocess_finalize (GObject *object)
+{
+ GSubprocess *self = G_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)
+ g_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 (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_CONTEXT:
+ self->context = 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_CONTEXT:
+ g_value_set_object (value, self->context);
+ 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:context:
+ *
+ *
+ * Since: 2.36
+ */
+ g_subprocess_pspecs[PROP_CONTEXT] = g_param_spec_object ("context", P_("Context"), P_("Subprocess options"), G_TYPE_SUBPROCESS_CONTEXT,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_STATIC_STRINGS);
+
+ g_object_class_install_properties (gobject_class, N_PROPS, g_subprocess_pspecs);
+}
+
+#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);
+}
+
+#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;
+
+ my_fd = g_open (filename, mode | O_BINARY | O_CLOEXEC, 0666);
+
+ /* 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)
+{
+ GSubprocess *self = G_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 == G_SUBPROCESS_STREAM_DISPOSITION_NULL)
+ ; /* nothing */
+ else if (self->context->stdin_disposition == G_SUBPROCESS_STREAM_DISPOSITION_INHERIT)
+ spawn_flags |= G_SPAWN_CHILD_INHERITS_STDIN;
+ else if (self->context->stdin_disposition == G_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 == G_SUBPROCESS_STREAM_DISPOSITION_NULL)
+ spawn_flags |= G_SPAWN_STDOUT_TO_DEV_NULL;
+ else if (self->context->stdout_disposition == G_SUBPROCESS_STREAM_DISPOSITION_INHERIT)
+ ; /* Nothing */
+ else if (self->context->stdout_disposition == G_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 == G_SUBPROCESS_STREAM_DISPOSITION_NULL)
+ spawn_flags |= G_SPAWN_STDERR_TO_DEV_NULL;
+ else if (self->context->stderr_disposition == G_SUBPROCESS_STREAM_DISPOSITION_INHERIT)
+ ; /* Nothing */
+ else if (self->context->stderr_disposition == G_SUBPROCESS_STREAM_DISPOSITION_PIPE)
+ pipe_ptrs[2] = &pipe_fds[2];
+ else if (self->context->stderr_disposition == G_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->pdata)[0]))
+ spawn_flags |= G_SPAWN_SEARCH_PATH;
+
+ spawn_flags |= G_SPAWN_DO_NOT_REAP_CHILD;
+ spawn_flags |= G_SPAWN_CLOEXEC_PIPES;
+
+ 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->pdata,
+ 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;
+}
+
+/**
+ * g_subprocess_new:
+ *
+ * Create a new process, using the parameters specified by
+ * GSubprocessContext.
+ *
+ * Returns: (transfer full): A newly created %GSubprocess, or %NULL on error (and @error will be set)
+ *
+ * Since: 2.36
+ */
+GLIB_AVAILABLE_IN_2_36
+GSubprocess *
+g_subprocess_new (GSubprocessContext *context,
+ GError **error)
+{
+ return g_initable_new (G_TYPE_SUBPROCESS,
+ NULL, error,
+ "context", context,
+ 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.36
+ */
+GPid
+g_subprocess_get_pid (GSubprocess *self)
+{
+ g_return_val_if_fail (G_IS_SUBPROCESS (self), 0);
+
+ return self->pid;
+}
+
+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_pipe, NULL);
+
+ return self->stdin_pipe;
+}
+
+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_pipe, NULL);
+
+ return self->stdout_pipe;
+}
+
+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_pipe, NULL);
+
+ return self->stderr_pipe;
+}
+
+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.36
+ */
+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.36
+ */
+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.36
+ */
+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.36
+ */
+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.36
+ */
+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
+}
+
+GSubprocess *
+g_subprocess_new_simple_argl (GSubprocessStreamDisposition stdout_disposition,
+ GSubprocessStreamDisposition stderr_disposition,
+ GError **error,
+ const gchar *first_arg,
+ ...)
+{
+ GPtrArray *argv;
+ va_list args;
+ GSubprocess *result;
+
+ argv = g_ptr_array_new ();
+ va_start (args, first_arg);
+ do
+ g_ptr_array_add (argv, (gchar*)first_arg);
+ while ((first_arg = va_arg (args, const char *)) != NULL);
+
+ result = g_subprocess_new_simple_argv ((char**)argv->pdata,
+ stdout_disposition,
+ stderr_disposition,
+ error);
+ g_ptr_array_free (argv, TRUE);
+
+ return result;
+}
+
+GSubprocess *
+g_subprocess_new_simple_argv (gchar **argv,
+ GSubprocessStreamDisposition stdout_disposition,
+ GSubprocessStreamDisposition stderr_disposition,
+ GError **error)
+{
+ GSubprocessContext *context;
+ GSubprocess *result;
+
+ context = g_subprocess_context_new (argv);
+ g_subprocess_context_set_stdout_disposition (context, stdout_disposition);
+ g_subprocess_context_set_stderr_disposition (context, stderr_disposition);
+
+ result = g_subprocess_new (context, error);
+ g_object_unref (context);
+
+ return result;
+}
diff --git a/gio/gsubprocess.h b/gio/gsubprocess.h
new file mode 100644
index 0000000..2af5363
--- /dev/null
+++ b/gio/gsubprocess.h
@@ -0,0 +1,104 @@
+/* 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_36
+GType g_subprocess_get_type (void) G_GNUC_CONST;
+
+/**** Core API ****/
+
+GLIB_AVAILABLE_IN_2_36
+GSubprocess * g_subprocess_new (GSubprocessContext *context,
+ GError **error);
+
+GLIB_AVAILABLE_IN_2_36
+GOutputStream * g_subprocess_get_stdin_pipe (GSubprocess *self);
+
+GLIB_AVAILABLE_IN_2_36
+GInputStream * g_subprocess_get_stdout_pipe (GSubprocess *self);
+
+GLIB_AVAILABLE_IN_2_36
+GInputStream * g_subprocess_get_stderr_pipe (GSubprocess *self);
+
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_wait (GSubprocess *self,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+
+GLIB_AVAILABLE_IN_2_36
+gboolean g_subprocess_wait_finish (GSubprocess *self,
+ GAsyncResult *result,
+ int *out_exit_status,
+ GError **error);
+
+GLIB_AVAILABLE_IN_2_36
+gboolean g_subprocess_wait_sync (GSubprocess *self,
+ int *out_exit_status,
+ GCancellable *cancellable,
+ GError **error);
+
+GLIB_AVAILABLE_IN_2_36
+gboolean g_subprocess_wait_sync_check (GSubprocess *self,
+ GCancellable *cancellable,
+ GError **error);
+
+GLIB_AVAILABLE_IN_2_36
+GPid g_subprocess_get_pid (GSubprocess *self);
+
+GLIB_AVAILABLE_IN_2_36
+gboolean g_subprocess_request_exit (GSubprocess *self);
+
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_force_exit (GSubprocess *self);
+
+/** High level helpers **/
+
+GLIB_AVAILABLE_IN_2_36
+GSubprocess * g_subprocess_new_simple_argl (GSubprocessStreamDisposition stdout_disposition,
+ GSubprocessStreamDisposition stderr_disposition,
+ GError **error,
+ const char *first_arg,
+ ...) G_GNUC_NULL_TERMINATED;
+GLIB_AVAILABLE_IN_2_36
+GSubprocess * g_subprocess_new_simple_argv (char **argv,
+ GSubprocessStreamDisposition stdout_disposition,
+ GSubprocessStreamDisposition stderr_disposition,
+ GError **error);
+
+G_END_DECLS
+
+#endif /* __G_SUBPROCESS_H__ */
diff --git a/gio/gsubprocesscontext-private.h b/gio/gsubprocesscontext-private.h
new file mode 100644
index 0000000..39ceafa
--- /dev/null
+++ b/gio/gsubprocesscontext-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 __G_SUBPROCESS_CONTEXT_PRIVATE_H__
+#define __G_SUBPROCESS_CONTEXT_PRIVATE_H__
+
+#include "gsubprocesscontext.h"
+
+G_BEGIN_DECLS
+
+struct _GSubprocessContext
+{
+ GObject parent;
+
+ GSpawnFlags flags;
+ GPtrArray *argv;
+ gboolean has_argv0;
+ char **envp;
+ char *cwd;
+
+ GSubprocessStreamDisposition stdin_disposition;
+ GSubprocessStreamDisposition stdout_disposition;
+ GSubprocessStreamDisposition 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/gio/gsubprocesscontext.c b/gio/gsubprocesscontext.c
new file mode 100644
index 0000000..c2a0d69
--- /dev/null
+++ b/gio/gsubprocesscontext.c
@@ -0,0 +1,307 @@
+/* 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>
+ */
+
+/**
+ * SECTION:gsubprocess
+ * @title: GSubprocess 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 #GSubprocess 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 "gsubprocesscontext-private.h"
+#include "gsubprocess.h"
+#include "gasyncresult.h"
+#include "glibintl.h"
+#include "glib-private.h"
+
+#include <string.h>
+
+typedef GObjectClass GSubprocessContextClass;
+
+G_DEFINE_TYPE (GSubprocessContext, g_subprocess_context, G_TYPE_OBJECT);
+
+enum
+{
+ PROP_0,
+ PROP_ARGV,
+ N_PROPS
+};
+
+static GParamSpec *g_subprocess_context_pspecs[N_PROPS];
+
+GSubprocessContext *
+g_subprocess_context_new (gchar **argv)
+{
+ return g_object_new (G_TYPE_SUBPROCESS_CONTEXT,
+ "argv", argv,
+ NULL);
+}
+
+static void
+g_subprocess_context_init (GSubprocessContext *self)
+{
+ self->argv = g_ptr_array_new_with_free_func (g_free);
+ self->stdin_fd = -1;
+ self->stdout_fd = -1;
+ self->stderr_fd = -1;
+}
+
+static void
+g_subprocess_context_finalize (GObject *object)
+{
+ GSubprocessContext *self = G_SUBPROCESS_CONTEXT (object);
+
+ g_ptr_array_unref (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 (g_subprocess_context_parent_class)->finalize != NULL)
+ G_OBJECT_CLASS (g_subprocess_context_parent_class)->finalize (object);
+}
+
+static void
+g_subprocess_context_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ GSubprocessContext *self = G_SUBPROCESS_CONTEXT (object);
+
+ switch (prop_id)
+ {
+ case PROP_ARGV:
+ g_subprocess_context_set_args (self, (char**)g_value_get_boxed (value));
+ break;
+
+ default:
+ g_assert_not_reached ();
+ }
+}
+
+static void
+g_subprocess_context_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GSubprocessContext *self = G_SUBPROCESS_CONTEXT (object);
+
+ switch (prop_id)
+ {
+ case PROP_ARGV:
+ g_value_set_boxed (value, self->argv->pdata);
+ break;
+
+ default:
+ g_assert_not_reached ();
+ }
+}
+
+static void
+g_subprocess_context_class_init (GSubprocessContextClass *class)
+{
+ GObjectClass *gobject_class = G_OBJECT_CLASS (class);
+
+ gobject_class->finalize = g_subprocess_context_finalize;
+ gobject_class->get_property = g_subprocess_context_get_property;
+ gobject_class->set_property = g_subprocess_context_set_property;
+
+ /**
+ * GSubprocessContext: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
+ * g_subprocess_context_set_args_and_argv0(). For more information
+ * about this, see %G_SPAWN_FILE_AND_ARGV_ZERO.
+ *
+ * Since: 2.36
+ */
+ g_subprocess_context_pspecs[PROP_ARGV] = g_param_spec_boxed ("argv", P_("Arguments"), P_("Arguments for child process"), G_TYPE_STRV,
+ G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+
+ g_object_class_install_properties (gobject_class, N_PROPS, g_subprocess_context_pspecs);
+}
+
+/* Only exported on Unix */
+#ifndef G_OS_UNIX
+static
+#endif
+void
+g_subprocess_context_set_args_and_argv0 (GSubprocessContext *self,
+ const gchar *argv0,
+ gchar **args)
+{
+ gchar **iter;
+
+ g_ptr_array_set_size (self->argv, 0);
+
+ if (argv0)
+ g_ptr_array_add (self->argv, g_strdup (argv0));
+
+ for (iter = args; *iter; iter++)
+ g_ptr_array_add (self->argv, g_strdup (*iter));
+ g_ptr_array_add (self->argv, NULL);
+}
+
+void
+g_subprocess_context_set_args (GSubprocessContext *self,
+ gchar **args)
+{
+ g_subprocess_context_set_args_and_argv0 (self, NULL, args);
+}
+
+/* Environment */
+
+void
+g_subprocess_context_set_environment (GSubprocessContext *self,
+ gchar **environ)
+{
+ g_strfreev (self->envp);
+ self->envp = g_strdupv (environ);
+}
+
+void
+g_subprocess_context_set_cwd (GSubprocessContext *self,
+ const gchar *cwd)
+{
+ g_free (self->cwd);
+ self->cwd = g_strdup (cwd);
+}
+
+void
+g_subprocess_context_set_keep_descriptors (GSubprocessContext *self,
+ gboolean keep_descriptors)
+
+{
+ self->keep_descriptors = keep_descriptors ? 1 : 0;
+}
+
+void
+g_subprocess_context_set_search_path (GSubprocessContext *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
+g_subprocess_context_set_stdin_disposition (GSubprocessContext *self,
+ GSubprocessStreamDisposition disposition)
+{
+ g_return_if_fail (disposition != G_SUBPROCESS_STREAM_DISPOSITION_STDERR_MERGE);
+ self->stdin_disposition = disposition;
+}
+
+void
+g_subprocess_context_set_stdout_disposition (GSubprocessContext *self,
+ GSubprocessStreamDisposition disposition)
+{
+ g_return_if_fail (disposition != G_SUBPROCESS_STREAM_DISPOSITION_STDERR_MERGE);
+ self->stdout_disposition = disposition;
+}
+
+void
+g_subprocess_context_set_stderr_disposition (GSubprocessContext *self,
+ GSubprocessStreamDisposition disposition)
+{
+ self->stderr_disposition = disposition;
+}
+
+#ifdef G_OS_UNIX
+void
+g_subprocess_context_set_stdin_file_path (GSubprocessContext *self,
+ const gchar *path)
+{
+ self->stdin_disposition = G_SUBPROCESS_STREAM_DISPOSITION_NULL;
+ g_free (self->stdin_path);
+ self->stdin_path = g_strdup (path);
+}
+
+void
+g_subprocess_context_set_stdin_fd (GSubprocessContext *self,
+ gint fd)
+{
+ self->stdin_disposition = G_SUBPROCESS_STREAM_DISPOSITION_NULL;
+ self->stdin_fd = fd;
+}
+
+void
+g_subprocess_context_set_stdout_file_path (GSubprocessContext *self,
+ const gchar *path)
+{
+ self->stdout_disposition = G_SUBPROCESS_STREAM_DISPOSITION_NULL;
+ g_free (self->stdout_path);
+ self->stdout_path = g_strdup (path);
+}
+
+void
+g_subprocess_context_set_stdout_fd (GSubprocessContext *self,
+ gint fd)
+{
+ self->stdout_disposition = G_SUBPROCESS_STREAM_DISPOSITION_NULL;
+ self->stdout_fd = fd;
+}
+
+void
+g_subprocess_context_set_stderr_file_path (GSubprocessContext *self,
+ const gchar *path)
+{
+ self->stderr_disposition = G_SUBPROCESS_STREAM_DISPOSITION_NULL;
+ g_free (self->stderr_path);
+ self->stderr_path = g_strdup (path);
+}
+
+void
+g_subprocess_context_set_stderr_fd (GSubprocessContext *self,
+ gint fd)
+{
+ self->stderr_disposition = G_SUBPROCESS_STREAM_DISPOSITION_NULL;
+ self->stderr_fd = fd;
+}
+#endif
+
+#ifdef G_OS_UNIX
+void
+g_subprocess_context_set_child_setup (GSubprocessContext *self,
+ GSpawnChildSetupFunc child_setup,
+ gpointer user_data)
+{
+ self->child_setup_func = child_setup;
+ self->child_setup_data = user_data;
+}
+#endif
diff --git a/gio/gsubprocesscontext.h b/gio/gsubprocesscontext.h
new file mode 100644
index 0000000..118aef9
--- /dev/null
+++ b/gio/gsubprocesscontext.h
@@ -0,0 +1,118 @@
+/* GIO - GLib Input, Output and Streaming Library
+ *
+ * Copyright  2012 Colin Walters <walters verbum org>
+ * Copyright  2012 Canonical Limited
+ *
+ * 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: Ryan Lortie <desrt desrt ca>
+ * 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_CONTEXT_H__
+#define __G_SUBPROCESS_CONTEXT_H__
+
+#include <gio/giotypes.h>
+
+G_BEGIN_DECLS
+
+#define G_TYPE_SUBPROCESS_CONTEXT (g_subprocess_context_get_type ())
+#define G_SUBPROCESS_CONTEXT(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_SUBPROCESS_CONTEXT, GSubprocessContext))
+#define G_IS_SUBPROCESS_CONTEXT(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_SUBPROCESS_CONTEXT))
+
+GLIB_AVAILABLE_IN_2_36
+GType g_subprocess_context_get_type (void) G_GNUC_CONST;
+
+GLIB_AVAILABLE_IN_2_36
+GSubprocessContext * g_subprocess_context_new (gchar **argv);
+
+/* Argument control */
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_context_set_args (GSubprocessContext *self,
+ gchar **args);
+#ifdef G_OS_UNIX
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_context_set_args_and_argv0 (GSubprocessContext *self,
+ const gchar *argv0,
+ gchar **args);
+#endif
+
+/* Environment */
+
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_context_set_environment (GSubprocessContext *self,
+ gchar **environ);
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_context_set_cwd (GSubprocessContext *self,
+ const gchar *cwd);
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_context_set_keep_descriptors (GSubprocessContext *self,
+ gboolean keep_descriptors);
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_context_set_search_path (GSubprocessContext *self,
+ gboolean search_path,
+ gboolean search_path_from_envp);
+
+/* Basic I/O control */
+
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_context_set_stdin_disposition (GSubprocessContext *self,
+ GSubprocessStreamDisposition disposition);
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_context_set_stdout_disposition (GSubprocessContext *self,
+ GSubprocessStreamDisposition disposition);
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_context_set_stderr_disposition (GSubprocessContext *self,
+ GSubprocessStreamDisposition disposition);
+
+/* Extended I/O control, only available on UNIX */
+
+#ifdef G_OS_UNIX
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_context_set_stdin_file_path (GSubprocessContext *self,
+ const gchar *path);
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_context_set_stdin_fd (GSubprocessContext *self,
+ gint fd);
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_context_set_stdout_file_path (GSubprocessContext *self,
+ const gchar *path);
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_context_set_stdout_fd (GSubprocessContext *self,
+ gint fd);
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_context_set_stderr_file_path (GSubprocessContext *self,
+ const gchar *path);
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_context_set_stderr_fd (GSubprocessContext *self,
+ gint fd);
+#endif
+
+/* Child setup, only available on UNIX */
+#ifdef G_OS_UNIX
+GLIB_AVAILABLE_IN_2_36
+void g_subprocess_context_set_child_setup (GSubprocessContext *self,
+ GSpawnChildSetupFunc child_setup,
+ gpointer user_data);
+#endif
+
+G_END_DECLS
+
+#endif /* __G_SUBPROCESS_H__ */
diff --git a/gio/tests/.gitignore b/gio/tests/.gitignore
index 1ef962b..1da39d6 100644
--- a/gio/tests/.gitignore
+++ b/gio/tests/.gitignore
@@ -64,6 +64,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 fb419a0..c367be4 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 \
@@ -181,6 +182,8 @@ send_data_LDADD = $(LDADD) \
contexts_LDADD = $(LDADD) \
$(top_builddir)/gthread/libgthread-2.0.la
+noinst_PROGRAMS += gsubprocess-testprog
+
gdbus_daemon_SOURCES = gdbus-daemon.c $(top_srcdir)/gio/gdbusdaemon.c $(top_builddir)/gio/gdbus-daemon-generated.c
if HAVE_DBUS1
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..0c67591
--- /dev/null
+++ b/gio/tests/gsubprocess.c
@@ -0,0 +1,829 @@
+#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_add (ret, cwd_path);
+ g_ptr_array_add (ret, g_strdup (mode));
+
+ 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_simple_argv ((gchar**) args->pdata,
+ G_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
+ G_SUBPROCESS_STREAM_DISPOSITION_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_simple_argv ((gchar**) args->pdata,
+ G_SUBPROCESS_STREAM_DISPOSITION_NULL,
+ G_SUBPROCESS_STREAM_DISPOSITION_NULL,
+ 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_simple_argv ((gchar **) args->pdata,
+ G_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
+ G_SUBPROCESS_STREAM_DISPOSITION_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;
+ GSubprocessContext *context;
+
+ args = get_test_subprocess_args ("noop", NULL);
+ context = g_subprocess_context_new ((gchar**) args->pdata);
+ g_subprocess_context_set_stdin_disposition (context, G_SUBPROCESS_STREAM_DISPOSITION_INHERIT);
+ proc = g_subprocess_new (context, 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);
+ g_object_unref (context);
+}
+
+
+#ifdef G_OS_UNIX
+static void
+test_search_path (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GSubprocess *proc;
+
+ proc = g_subprocess_new_simple_argl (G_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
+ G_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
+ error,
+ "true", NULL);
+ 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_simple_argv ((gchar **) args->pdata,
+ G_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
+ G_SUBPROCESS_STREAM_DISPOSITION_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_simple_argv ((gchar **) args->pdata,
+ G_SUBPROCESS_STREAM_DISPOSITION_PIPE,
+ G_SUBPROCESS_STREAM_DISPOSITION_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_simple_argv ((gchar **) args->pdata,
+ G_SUBPROCESS_STREAM_DISPOSITION_PIPE,
+ G_SUBPROCESS_STREAM_DISPOSITION_STDERR_MERGE,
+ 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;
+ GSubprocessContext *context;
+ 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);
+ context = g_subprocess_context_new ((gchar**)args->pdata);
+ g_subprocess_context_set_stdin_disposition (context, G_SUBPROCESS_STREAM_DISPOSITION_PIPE);
+ g_subprocess_context_set_stdout_disposition (context, G_SUBPROCESS_STREAM_DISPOSITION_PIPE);
+ proc = g_subprocess_new (context, 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);
+ g_object_unref (context);
+}
+
+static gpointer
+cancel_soon (gpointer user_data)
+{
+ GCancellable *cancellable = user_data;
+
+ g_usleep (G_TIME_SPAN_SECOND);
+ g_cancellable_cancel (cancellable);
+ g_object_unref (cancellable);
+
+ return NULL;
+}
+
+static void
+test_cat_eof (void)
+{
+ const gchar *args[] = { "cat", NULL };
+ GCancellable *cancellable;
+ GError *error = NULL;
+ GSubprocessContext *context;
+ GSubprocess *cat;
+ gint exit_status;
+ gboolean result;
+ gchar buffer;
+ gssize s;
+
+ /* Spawn 'cat' */
+ context = g_subprocess_context_new ((gchar**)args);
+ g_subprocess_context_set_stdin_disposition (context, G_SUBPROCESS_STREAM_DISPOSITION_PIPE);
+ g_subprocess_context_set_stdout_disposition (context, G_SUBPROCESS_STREAM_DISPOSITION_PIPE);
+ cat = g_subprocess_new (context, &error);
+ g_assert_no_error (error);
+ g_assert (cat);
+
+ /* Make sure that reading stdout blocks (until we cancel) */
+ cancellable = g_cancellable_new ();
+ g_thread_unref (g_thread_new ("cancel thread", cancel_soon, g_object_ref (cancellable)));
+ s = g_input_stream_read (g_subprocess_get_stdout_pipe (cat), &buffer, sizeof buffer, cancellable, &error);
+ g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
+ g_assert_cmpint (s, ==, -1);
+ g_object_unref (cancellable);
+ g_clear_error (&error);
+
+ /* Close the stream (EOF on cat's stdin) */
+ result = g_output_stream_close (g_subprocess_get_stdin_pipe (cat), NULL, &error);
+ g_assert_no_error (error);
+ g_assert (result);
+
+ /* Now check that reading cat's stdout gets us an EOF (since it quit) */
+ s = g_input_stream_read (g_subprocess_get_stdout_pipe (cat), &buffer, sizeof buffer, NULL, &error);
+ g_assert_no_error (error);
+ g_assert (!s);
+
+ /* Check that the process has exited as a result of the EOF */
+ result = g_subprocess_wait_sync (cat, &exit_status, NULL, &error);
+ g_assert_no_error (error);
+ g_assert_cmpint (exit_status, ==, 0);
+ g_assert (result);
+
+ g_object_unref (cat);
+ g_object_unref (context);
+}
+
+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;
+ GSubprocessContext *context;
+ 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);
+ context = g_subprocess_context_new ((gchar**)args->pdata);
+ g_subprocess_context_set_stdin_disposition (context, G_SUBPROCESS_STREAM_DISPOSITION_PIPE);
+ g_subprocess_context_set_stdout_disposition (context, G_SUBPROCESS_STREAM_DISPOSITION_PIPE);
+ first = g_subprocess_new (context, error);
+ g_assert_no_error (local_error);
+ second = g_subprocess_new (context, error);
+ g_assert_no_error (local_error);
+ third = g_subprocess_new (context, 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 (context);
+ g_object_unref (first);
+ g_object_unref (second);
+ g_object_unref (third);
+}
+
+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_simple_argv ((gchar **) args->pdata,
+ G_SUBPROCESS_STREAM_DISPOSITION_INHERIT,
+ G_SUBPROCESS_STREAM_DISPOSITION_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);
+}
+
+#ifdef G_OS_UNIX
+static void
+test_stdout_file (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GSubprocessContext *context;
+ GSubprocess *proc;
+ GPtrArray *args;
+ GFile *tmpfile;
+ GFileIOStream *iostream;
+ GOutputStream *stdin;
+ const char *test_data = "this is some test data\n";
+ char *tmp_contents;
+ char *tmp_file_path;
+
+ tmpfile = g_file_new_tmp ("gsubprocessXXXXXX", &iostream, error);
+ g_assert_no_error (local_error);
+ g_clear_object (&iostream);
+
+ tmp_file_path = g_file_get_path (tmpfile);
+
+ args = get_test_subprocess_args ("cat", NULL);
+ context = g_subprocess_context_new ((gchar**)args->pdata);
+ g_subprocess_context_set_stdin_disposition (context, G_SUBPROCESS_STREAM_DISPOSITION_PIPE);
+ g_subprocess_context_set_stdout_file_path (context, tmp_file_path);
+ proc = g_subprocess_new (context, error);
+ g_ptr_array_free (args, TRUE);
+ g_assert_no_error (local_error);
+
+ stdin = g_subprocess_get_stdin_pipe (proc);
+
+ g_output_stream_write_all (stdin, test_data, strlen (test_data), NULL, 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_object_unref (context);
+ g_object_unref (proc);
+
+ g_file_load_contents (tmpfile, NULL, &tmp_contents, NULL, NULL, error);
+ g_assert_no_error (local_error);
+
+ g_assert_cmpstr (test_data, ==, tmp_contents);
+ g_free (tmp_contents);
+
+ (void) g_file_delete (tmpfile, NULL, NULL);
+ g_free (tmp_file_path);
+}
+
+static void
+test_stdout_fd (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GSubprocessContext *context;
+ GSubprocess *proc;
+ GPtrArray *args;
+ GFile *tmpfile;
+ GFileIOStream *iostream;
+ GFileDescriptorBased *descriptor_stream;
+ GOutputStream *stdin;
+ const char *test_data = "this is some test data\n";
+ char *tmp_contents;
+
+ tmpfile = g_file_new_tmp ("gsubprocessXXXXXX", &iostream, error);
+ g_assert_no_error (local_error);
+
+ args = get_test_subprocess_args ("cat", NULL);
+ context = g_subprocess_context_new ((gchar**)args->pdata);
+ g_subprocess_context_set_stdin_disposition (context, G_SUBPROCESS_STREAM_DISPOSITION_PIPE);
+ descriptor_stream = G_FILE_DESCRIPTOR_BASED (g_io_stream_get_output_stream (G_IO_STREAM (iostream)));
+ g_subprocess_context_set_stdout_fd (context, g_file_descriptor_based_get_fd (descriptor_stream));
+ proc = g_subprocess_new (context, error);
+ g_ptr_array_free (args, TRUE);
+ g_assert_no_error (local_error);
+
+ g_clear_object (&iostream);
+
+ stdin = g_subprocess_get_stdin_pipe (proc);
+
+ g_output_stream_write_all (stdin, test_data, strlen (test_data), NULL, 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_object_unref (context);
+ g_object_unref (proc);
+
+ g_file_load_contents (tmpfile, NULL, &tmp_contents, NULL, NULL, error);
+ g_assert_no_error (local_error);
+
+ g_assert_cmpstr (test_data, ==, tmp_contents);
+ g_free (tmp_contents);
+
+ (void) g_file_delete (tmpfile, NULL, NULL);
+}
+
+static void
+child_setup (gpointer user_data)
+{
+ dup2 (GPOINTER_TO_INT (user_data), 1);
+}
+
+static void
+test_child_setup (void)
+{
+ GError *local_error = NULL;
+ GError **error = &local_error;
+ GSubprocessContext *context;
+ GSubprocess *proc;
+ GPtrArray *args;
+ GFile *tmpfile;
+ GFileIOStream *iostream;
+ GOutputStream *stdin;
+ const char *test_data = "this is some test data\n";
+ char *tmp_contents;
+ int fd;
+
+ tmpfile = g_file_new_tmp ("gsubprocessXXXXXX", &iostream, error);
+ g_assert_no_error (local_error);
+
+ fd = g_file_descriptor_based_get_fd (G_FILE_DESCRIPTOR_BASED (g_io_stream_get_output_stream (G_IO_STREAM (iostream))));
+
+ args = get_test_subprocess_args ("cat", NULL);
+ context = g_subprocess_context_new ((gchar**)args->pdata);
+ g_subprocess_context_set_stdin_disposition (context, G_SUBPROCESS_STREAM_DISPOSITION_PIPE);
+ g_subprocess_context_set_child_setup (context, child_setup, GINT_TO_POINTER (fd));
+ proc = g_subprocess_new (context, error);
+ g_ptr_array_free (args, TRUE);
+ g_assert_no_error (local_error);
+
+ g_clear_object (&iostream);
+
+ stdin = g_subprocess_get_stdin_pipe (proc);
+
+ g_output_stream_write_all (stdin, test_data, strlen (test_data), NULL, 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_object_unref (context);
+ g_object_unref (proc);
+
+ g_file_load_contents (tmpfile, NULL, &tmp_contents, NULL, NULL, error);
+ g_assert_no_error (local_error);
+
+ g_assert_cmpstr (test_data, ==, tmp_contents);
+ g_free (tmp_contents);
+
+ (void) g_file_delete (tmpfile, NULL, NULL);
+}
+#endif
+
+int
+main (int argc, char **argv)
+{
+ 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);
+ g_test_add_func ("/gsubprocess/cat-eof", test_cat_eof);
+ g_test_add_func ("/gsubprocess/multi1", test_multi_1);
+ g_test_add_func ("/gsubprocess/terminate", test_terminate);
+#ifdef G_OS_UNIX
+ g_test_add_func ("/gsubprocess/stdout-file", test_stdout_file);
+ g_test_add_func ("/gsubprocess/stdout-fd", test_stdout_fd);
+ g_test_add_func ("/gsubprocess/child-setup", test_child_setup);
+#endif
+
+ return g_test_run ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]