mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-28 00:16:15 +01:00
GSubprocess: New class for spawning child processes
There are a number of nice things this class brings: 0) Has a race-free termination API on all platforms (on UNIX, calls to kill() and waitpid() are coordinated as not to cause problems). 1) Operates in terms of G{Input,Output}Stream, not file descriptors 2) Standard GIO-style async API for wait() with cancellation 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
This commit is contained in:
parent
e30bbca667
commit
5b48dc40cc
@ -108,6 +108,10 @@
|
|||||||
<xi:include href="xml/ginitable.xml"/>
|
<xi:include href="xml/ginitable.xml"/>
|
||||||
<xi:include href="xml/gasyncinitable.xml"/>
|
<xi:include href="xml/gasyncinitable.xml"/>
|
||||||
</chapter>
|
</chapter>
|
||||||
|
<chapter id="subprocesses">
|
||||||
|
<title>Subprocesses</title>
|
||||||
|
<xi:include href="xml/gsubprocess.xml"/>
|
||||||
|
</chapter>
|
||||||
<chapter id="networking">
|
<chapter id="networking">
|
||||||
<title>Low-level network support</title>
|
<title>Low-level network support</title>
|
||||||
<xi:include href="xml/gsocket.xml"/>
|
<xi:include href="xml/gsocket.xml"/>
|
||||||
|
@ -4077,4 +4077,37 @@ G_SIMPLE_PROXY_RESOLVER_CLASS
|
|||||||
G_IS_SIMPLE_PROXY_RESOLVER_CLASS
|
G_IS_SIMPLE_PROXY_RESOLVER_CLASS
|
||||||
G_SIMPLE_PROXY_RESOLVER_GET_CLASS
|
G_SIMPLE_PROXY_RESOLVER_GET_CLASS
|
||||||
g_simple_proxy_resolver_get_type
|
g_simple_proxy_resolver_get_type
|
||||||
|
|
||||||
|
<FILE>gsubprocess</FILE>
|
||||||
|
<TITLE>GSubprocess</TITLE>
|
||||||
|
GSubprocess
|
||||||
|
g_subprocess_new
|
||||||
|
g_subprocess_newv
|
||||||
|
<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_sync
|
||||||
|
g_subprocess_wait_finish
|
||||||
|
g_subprocess_wait_check
|
||||||
|
g_subprocess_wait_check_sync
|
||||||
|
g_subprocess_wait_check_finish
|
||||||
|
<SUBSECTION Status>
|
||||||
|
g_subprocess_get_successful
|
||||||
|
g_subprocess_get_if_exited
|
||||||
|
g_subprocess_get_exit_status
|
||||||
|
g_subprocess_get_if_signaled
|
||||||
|
g_subprocess_get_term_sig
|
||||||
|
g_subprocess_get_status
|
||||||
|
<SUBSECTION Control>
|
||||||
|
g_subprocess_send_signal
|
||||||
|
g_subprocess_force_exit
|
||||||
|
<SUBSECTION Standard>
|
||||||
|
G_IS_SUBPROCESS
|
||||||
|
G_TYPE_SUBPROCESS
|
||||||
|
G_SUBPROCESS
|
||||||
|
<SUBSECTION Private>
|
||||||
|
g_subprocess_get_type
|
||||||
</SECTION>
|
</SECTION>
|
||||||
|
@ -137,3 +137,4 @@ g_test_dbus_get_type
|
|||||||
g_test_dbus_flags_get_type
|
g_test_dbus_flags_get_type
|
||||||
g_task_get_type
|
g_task_get_type
|
||||||
g_simple_proxy_resolver_get_type
|
g_simple_proxy_resolver_get_type
|
||||||
|
g_subprocess_get_type
|
||||||
|
@ -428,6 +428,9 @@ libgio_2_0_la_SOURCES = \
|
|||||||
gsocketlistener.c \
|
gsocketlistener.c \
|
||||||
gsocketoutputstream.c \
|
gsocketoutputstream.c \
|
||||||
gsocketoutputstream.h \
|
gsocketoutputstream.h \
|
||||||
|
gsubprocesslauncher.c \
|
||||||
|
gsubprocess.c \
|
||||||
|
gsubprocesslauncher-private.h \
|
||||||
gsocketservice.c \
|
gsocketservice.c \
|
||||||
gsrvtarget.c \
|
gsrvtarget.c \
|
||||||
gsimpleproxyresolver.c \
|
gsimpleproxyresolver.c \
|
||||||
@ -591,6 +594,8 @@ gio_headers = \
|
|||||||
gsrvtarget.h \
|
gsrvtarget.h \
|
||||||
gsimpleproxyresolver.h \
|
gsimpleproxyresolver.h \
|
||||||
gtask.h \
|
gtask.h \
|
||||||
|
gsubprocess.h \
|
||||||
|
gsubprocesslauncher.h \
|
||||||
gtcpconnection.h \
|
gtcpconnection.h \
|
||||||
gtcpwrapperconnection.h \
|
gtcpwrapperconnection.h \
|
||||||
gthreadedsocketservice.h\
|
gthreadedsocketservice.h\
|
||||||
|
@ -126,6 +126,8 @@
|
|||||||
#include <gio/gsrvtarget.h>
|
#include <gio/gsrvtarget.h>
|
||||||
#include <gio/gsimpleproxyresolver.h>
|
#include <gio/gsimpleproxyresolver.h>
|
||||||
#include <gio/gtask.h>
|
#include <gio/gtask.h>
|
||||||
|
#include <gio/gsubprocess.h>
|
||||||
|
#include <gio/gsubprocesslauncher.h>
|
||||||
#include <gio/gtcpconnection.h>
|
#include <gio/gtcpconnection.h>
|
||||||
#include <gio/gtcpwrapperconnection.h>
|
#include <gio/gtcpwrapperconnection.h>
|
||||||
#include <gio/gtestdbus.h>
|
#include <gio/gtestdbus.h>
|
||||||
|
@ -1696,6 +1696,56 @@ typedef enum /*< flags >*/ {
|
|||||||
G_TEST_DBUS_NONE = 0
|
G_TEST_DBUS_NONE = 0
|
||||||
} GTestDBusFlags;
|
} GTestDBusFlags;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GSubprocessFlags:
|
||||||
|
* @G_SUBPROCESS_FLAGS_NONE: No flags.
|
||||||
|
* @G_SUBPROCESS_FLAGS_STDIN_PIPE: create a pipe for the stdin of the
|
||||||
|
* spawned process that can be accessed with
|
||||||
|
* g_subprocess_get_stdin_pipe().
|
||||||
|
* @G_SUBPROCESS_FLAGS_STDIN_INHERIT: stdin is inherited from the
|
||||||
|
* calling process.
|
||||||
|
* @G_SUBPROCESS_FLAGS_STDOUT_PIPE: create a pipe for the stdout of the
|
||||||
|
* spawned process that can be accessed with
|
||||||
|
* g_subprocess_get_stdout_pipe().
|
||||||
|
* @G_SUBPROCESS_FLAGS_STDOUT_SILENCE: silence the stdout of the spawned
|
||||||
|
* process (ie: redirect to /dev/null).
|
||||||
|
* @G_SUBPROCESS_FLAGS_STDERR_PIPE: create a pipe for the stderr of the
|
||||||
|
* spawned process that can be accessed with
|
||||||
|
* g_subprocess_get_stderr_pipe().
|
||||||
|
* @G_SUBPROCESS_FLAGS_STDERR_SILENCE: silence the stderr of the spawned
|
||||||
|
* process (ie: redirect to /dev/null).
|
||||||
|
* @G_SUBPROCESS_FLAGS_STDERR_MERGE: merge the stderr of the spawned
|
||||||
|
* process with whatever the stdout happens to be. This is a good way
|
||||||
|
* of directing both streams to a common log file, for example.
|
||||||
|
* @G_SUBPROCESS_FLAGS_INHERIT_FDS: spawned processes will inherit the
|
||||||
|
* file descriptors of their parent, unless those descriptors have
|
||||||
|
* been explicitly marked as close-on-exec. This flag has no effect
|
||||||
|
* over the "standard" file descriptors (stdin, stdout, stderr).
|
||||||
|
*
|
||||||
|
* Flags to define the behaviour of a #GSubprocess.
|
||||||
|
*
|
||||||
|
* Note that the default for stdin is to redirect from /dev/null. For
|
||||||
|
* stdout and stderr the default are for them to inherit the
|
||||||
|
* corresponding descriptor from the calling process.
|
||||||
|
*
|
||||||
|
* Note that it is a programmer error to mix 'incompatible' flags. For
|
||||||
|
* example, you may not request both %G_SUBPROCESS_FLAGS_STDOUT_PIPE and
|
||||||
|
* %G_SUBPROCESS_FLAGS_STDOUT_SILENCE.
|
||||||
|
*
|
||||||
|
* Since: 2.36
|
||||||
|
**/
|
||||||
|
typedef enum {
|
||||||
|
G_SUBPROCESS_FLAGS_NONE = 0,
|
||||||
|
G_SUBPROCESS_FLAGS_STDIN_PIPE = (1u << 0),
|
||||||
|
G_SUBPROCESS_FLAGS_STDIN_INHERIT = (1u << 1),
|
||||||
|
G_SUBPROCESS_FLAGS_STDOUT_PIPE = (1u << 2),
|
||||||
|
G_SUBPROCESS_FLAGS_STDOUT_SILENCE = (1u << 3),
|
||||||
|
G_SUBPROCESS_FLAGS_STDERR_PIPE = (1u << 4),
|
||||||
|
G_SUBPROCESS_FLAGS_STDERR_SILENCE = (1u << 5),
|
||||||
|
G_SUBPROCESS_FLAGS_STDERR_MERGE = (1u << 6),
|
||||||
|
G_SUBPROCESS_FLAGS_INHERIT_FDS = (1u << 7)
|
||||||
|
} GSubprocessFlags;
|
||||||
|
|
||||||
G_END_DECLS
|
G_END_DECLS
|
||||||
|
|
||||||
#endif /* __GIO_ENUMS_H__ */
|
#endif /* __GIO_ENUMS_H__ */
|
||||||
|
@ -137,6 +137,7 @@ typedef struct _GIOStream GIOStream;
|
|||||||
typedef struct _GPollableInputStream GPollableInputStream; /* Dummy typedef */
|
typedef struct _GPollableInputStream GPollableInputStream; /* Dummy typedef */
|
||||||
typedef struct _GPollableOutputStream GPollableOutputStream; /* Dummy typedef */
|
typedef struct _GPollableOutputStream GPollableOutputStream; /* Dummy typedef */
|
||||||
typedef struct _GResolver GResolver;
|
typedef struct _GResolver GResolver;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GResource:
|
* GResource:
|
||||||
*
|
*
|
||||||
@ -513,6 +514,23 @@ typedef GType (*GDBusProxyTypeFunc) (GDBusObjectManagerClient *manager,
|
|||||||
|
|
||||||
typedef struct _GTestDBus GTestDBus;
|
typedef struct _GTestDBus GTestDBus;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GSubprocess:
|
||||||
|
*
|
||||||
|
* A child process.
|
||||||
|
*
|
||||||
|
* Since: 2.36
|
||||||
|
*/
|
||||||
|
typedef struct _GSubprocess GSubprocess;
|
||||||
|
/**
|
||||||
|
* GSubprocessLauncher:
|
||||||
|
*
|
||||||
|
* Options for launching a child process.
|
||||||
|
*
|
||||||
|
* Since: 2.36
|
||||||
|
*/
|
||||||
|
typedef struct _GSubprocessLauncher GSubprocessLauncher;
|
||||||
|
|
||||||
G_END_DECLS
|
G_END_DECLS
|
||||||
|
|
||||||
#endif /* __GIO_TYPES_H__ */
|
#endif /* __GIO_TYPES_H__ */
|
||||||
|
@ -297,9 +297,8 @@ end_element (GMarkupParseContext *context,
|
|||||||
|
|
||||||
if (xml_stripblanks && xmllint != NULL)
|
if (xml_stripblanks && xmllint != NULL)
|
||||||
{
|
{
|
||||||
gchar *argv[8];
|
int fd;
|
||||||
int status, fd, argc;
|
GSubprocess *proc;
|
||||||
gchar *stderr_child = NULL;
|
|
||||||
|
|
||||||
tmp_file = g_strdup ("resource-XXXXXXXX");
|
tmp_file = g_strdup ("resource-XXXXXXXX");
|
||||||
if ((fd = g_mkstemp (tmp_file)) == -1)
|
if ((fd = g_mkstemp (tmp_file)) == -1)
|
||||||
@ -315,43 +314,29 @@ end_element (GMarkupParseContext *context,
|
|||||||
}
|
}
|
||||||
close (fd);
|
close (fd);
|
||||||
|
|
||||||
argc = 0;
|
proc = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_SILENCE, error,
|
||||||
argv[argc++] = (gchar *) xmllint;
|
xmllint, "--nonet", "--noblanks", "--output", tmp_file, real_file, NULL);
|
||||||
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);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
g_free (stderr_child);
|
|
||||||
g_free (real_file);
|
g_free (real_file);
|
||||||
|
real_file = NULL;
|
||||||
|
|
||||||
|
if (!proc)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
if (!g_subprocess_wait_check (proc, NULL, error))
|
||||||
|
{
|
||||||
|
g_object_unref (proc);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_object_unref (proc);
|
||||||
|
|
||||||
real_file = g_strdup (tmp_file);
|
real_file = g_strdup (tmp_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (to_pixdata)
|
if (to_pixdata)
|
||||||
{
|
{
|
||||||
gchar *argv[4];
|
int fd;
|
||||||
gchar *stderr_child = NULL;
|
GSubprocess *proc;
|
||||||
int status, fd, argc;
|
|
||||||
|
|
||||||
if (gdk_pixbuf_pixdata == NULL)
|
if (gdk_pixbuf_pixdata == NULL)
|
||||||
{
|
{
|
||||||
@ -375,31 +360,19 @@ end_element (GMarkupParseContext *context,
|
|||||||
}
|
}
|
||||||
close (fd);
|
close (fd);
|
||||||
|
|
||||||
argc = 0;
|
proc = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_SILENCE, error,
|
||||||
argv[argc++] = (gchar *) gdk_pixbuf_pixdata;
|
gdk_pixbuf_pixdata, real_file, tmp_file2, NULL);
|
||||||
argv[argc++] = real_file;
|
|
||||||
argv[argc++] = tmp_file2;
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
g_free (stderr_child);
|
|
||||||
g_free (real_file);
|
g_free (real_file);
|
||||||
|
real_file = NULL;
|
||||||
|
|
||||||
|
if (!g_subprocess_wait_check (proc, NULL, error))
|
||||||
|
{
|
||||||
|
g_object_unref (proc);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_object_unref (proc);
|
||||||
|
|
||||||
real_file = g_strdup (tmp_file2);
|
real_file = g_strdup (tmp_file2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
1689
gio/gsubprocess.c
Normal file
1689
gio/gsubprocess.c
Normal file
File diff suppressed because it is too large
Load Diff
174
gio/gsubprocess.h
Normal file
174
gio/gsubprocess.h
Normal file
@ -0,0 +1,174 @@
|
|||||||
|
/* 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_40
|
||||||
|
GType g_subprocess_get_type (void) G_GNUC_CONST;
|
||||||
|
|
||||||
|
/**** Core API ****/
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
GSubprocess * g_subprocess_new (GSubprocessFlags flags,
|
||||||
|
GError **error,
|
||||||
|
const gchar *argv0,
|
||||||
|
...) G_GNUC_NULL_TERMINATED;
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
GSubprocess * g_subprocess_newv (const gchar * const *argv,
|
||||||
|
GSubprocessFlags flags,
|
||||||
|
GError **error);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
GOutputStream * g_subprocess_get_stdin_pipe (GSubprocess *self);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
GInputStream * g_subprocess_get_stdout_pipe (GSubprocess *self);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
GInputStream * g_subprocess_get_stderr_pipe (GSubprocess *self);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
const gchar * g_subprocess_get_identifier (GSubprocess *self);
|
||||||
|
|
||||||
|
#ifdef G_OS_UNIX
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_send_signal (GSubprocess *self,
|
||||||
|
gint signal_num);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_force_exit (GSubprocess *self);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
gboolean g_subprocess_wait (GSubprocess *self,
|
||||||
|
GCancellable *cancellable,
|
||||||
|
GError **error);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_wait_async (GSubprocess *self,
|
||||||
|
GCancellable *cancellable,
|
||||||
|
GAsyncReadyCallback callback,
|
||||||
|
gpointer user_data);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
gboolean g_subprocess_wait_finish (GSubprocess *self,
|
||||||
|
GAsyncResult *result,
|
||||||
|
GError **error);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
gboolean g_subprocess_wait_check (GSubprocess *self,
|
||||||
|
GCancellable *cancellable,
|
||||||
|
GError **error);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_wait_check_async (GSubprocess *self,
|
||||||
|
GCancellable *cancellable,
|
||||||
|
GAsyncReadyCallback callback,
|
||||||
|
gpointer user_data);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
gboolean g_subprocess_wait_check_finish (GSubprocess *self,
|
||||||
|
GAsyncResult *result,
|
||||||
|
GError **error);
|
||||||
|
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
gint g_subprocess_get_status (GSubprocess *self);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
gboolean g_subprocess_get_successful (GSubprocess *self);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
gboolean g_subprocess_get_if_exited (GSubprocess *self);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
gint g_subprocess_get_exit_status (GSubprocess *self);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
gboolean g_subprocess_get_if_signaled (GSubprocess *self);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
gint g_subprocess_get_term_sig (GSubprocess *self);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
gboolean g_subprocess_communicate (GSubprocess *subprocess,
|
||||||
|
const gchar *stdin_data,
|
||||||
|
gssize stdin_length,
|
||||||
|
GCancellable *cancellable,
|
||||||
|
gchar **stdout_data,
|
||||||
|
gsize *stdout_length,
|
||||||
|
gchar **stderr_data,
|
||||||
|
gsize *stderr_length,
|
||||||
|
GError **error);
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_communicate_async (GSubprocess *subprocess,
|
||||||
|
const gchar *stdin_data,
|
||||||
|
gssize stdin_length,
|
||||||
|
GCancellable *cancellable,
|
||||||
|
GAsyncReadyCallback callback,
|
||||||
|
gpointer user_data);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
gboolean g_subprocess_communicate_finish (GSubprocess *subprocess,
|
||||||
|
GAsyncResult *result,
|
||||||
|
gchar **stdout_data,
|
||||||
|
gsize *stdout_length,
|
||||||
|
gchar **stderr_data,
|
||||||
|
gsize *stderr_length,
|
||||||
|
GError **error);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
gboolean g_subprocess_communicate_bytes (GSubprocess *subprocess,
|
||||||
|
GBytes *stdin_bytes,
|
||||||
|
GCancellable *cancellable,
|
||||||
|
GBytes **stdout_bytes,
|
||||||
|
GBytes **stderr_bytes,
|
||||||
|
GError **error);
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_communicate_bytes_async (GSubprocess *subprocess,
|
||||||
|
GBytes *stdin_bytes,
|
||||||
|
GCancellable *cancellable,
|
||||||
|
GAsyncReadyCallback callback,
|
||||||
|
gpointer user_data);
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
gboolean g_subprocess_communicate_bytes_finish (GSubprocess *subprocess,
|
||||||
|
GAsyncResult *result,
|
||||||
|
GBytes **stdout_bytes,
|
||||||
|
GBytes **stderr_bytes,
|
||||||
|
GError **error);
|
||||||
|
|
||||||
|
G_END_DECLS
|
||||||
|
|
||||||
|
#endif /* __G_SUBPROCESS_H__ */
|
61
gio/gsubprocesslauncher-private.h
Normal file
61
gio/gsubprocesslauncher-private.h
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
/* 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 "gsubprocesslauncher.h"
|
||||||
|
|
||||||
|
G_BEGIN_DECLS
|
||||||
|
|
||||||
|
struct _GSubprocessLauncher
|
||||||
|
{
|
||||||
|
GObject parent;
|
||||||
|
|
||||||
|
GSubprocessFlags flags;
|
||||||
|
gboolean path_from_envp;
|
||||||
|
char **envp;
|
||||||
|
char *cwd;
|
||||||
|
|
||||||
|
#ifdef G_OS_UNIX
|
||||||
|
gint stdin_fd;
|
||||||
|
gchar *stdin_path;
|
||||||
|
|
||||||
|
gint stdout_fd;
|
||||||
|
gchar *stdout_path;
|
||||||
|
|
||||||
|
gint stderr_fd;
|
||||||
|
gchar *stderr_path;
|
||||||
|
|
||||||
|
GArray *basic_fd_assignments;
|
||||||
|
GArray *needdup_fd_assignments;
|
||||||
|
|
||||||
|
GSpawnChildSetupFunc child_setup_func;
|
||||||
|
gpointer child_setup_user_data;
|
||||||
|
GDestroyNotify child_setup_destroy_notify;
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
|
||||||
|
void g_subprocess_set_launcher (GSubprocess *subprocess,
|
||||||
|
GSubprocessLauncher *launcher);
|
||||||
|
|
||||||
|
G_END_DECLS
|
||||||
|
|
||||||
|
#endif
|
718
gio/gsubprocesslauncher.c
Normal file
718
gio/gsubprocesslauncher.c
Normal file
@ -0,0 +1,718 @@
|
|||||||
|
/* GIO - GLib Input, Output and Streaming Library
|
||||||
|
*
|
||||||
|
* Copyright © 2012 Red Hat, Inc.
|
||||||
|
* Copyright © 2012-2013 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 Launcher
|
||||||
|
* @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.40
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define ALL_STDIN_FLAGS (G_SUBPROCESS_FLAGS_STDIN_PIPE | \
|
||||||
|
G_SUBPROCESS_FLAGS_STDIN_INHERIT)
|
||||||
|
#define ALL_STDOUT_FLAGS (G_SUBPROCESS_FLAGS_STDOUT_PIPE | \
|
||||||
|
G_SUBPROCESS_FLAGS_STDOUT_SILENCE)
|
||||||
|
#define ALL_STDERR_FLAGS (G_SUBPROCESS_FLAGS_STDERR_PIPE | \
|
||||||
|
G_SUBPROCESS_FLAGS_STDERR_SILENCE | \
|
||||||
|
G_SUBPROCESS_FLAGS_STDERR_MERGE)
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#include "gsubprocesslauncher-private.h"
|
||||||
|
#include "gioenumtypes.h"
|
||||||
|
#include "gsubprocess.h"
|
||||||
|
#include "ginitable.h"
|
||||||
|
|
||||||
|
#ifdef G_OS_UNIX
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef GObjectClass GSubprocessLauncherClass;
|
||||||
|
|
||||||
|
G_DEFINE_TYPE (GSubprocessLauncher, g_subprocess_launcher, G_TYPE_OBJECT);
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
verify_disposition (const gchar *stream_name,
|
||||||
|
GSubprocessFlags filtered_flags,
|
||||||
|
gint fd,
|
||||||
|
const gchar *filename)
|
||||||
|
{
|
||||||
|
guint n_bits;
|
||||||
|
|
||||||
|
if (!filtered_flags)
|
||||||
|
n_bits = 0;
|
||||||
|
else if (((filtered_flags - 1) & filtered_flags) == 0)
|
||||||
|
n_bits = 1;
|
||||||
|
else
|
||||||
|
n_bits = 2; /* ...or more */
|
||||||
|
|
||||||
|
if (n_bits + (fd >= 0) + (filename != NULL) > 1)
|
||||||
|
{
|
||||||
|
GString *err;
|
||||||
|
|
||||||
|
err = g_string_new (NULL);
|
||||||
|
if (n_bits)
|
||||||
|
{
|
||||||
|
GFlagsClass *class;
|
||||||
|
GFlagsValue *value;
|
||||||
|
|
||||||
|
class = g_type_class_peek (G_TYPE_SUBPROCESS_FLAGS);
|
||||||
|
while ((value = g_flags_get_first_value (class, filtered_flags)))
|
||||||
|
{
|
||||||
|
g_string_append_printf (err, " %s", value->value_name);
|
||||||
|
filtered_flags &= value->value;
|
||||||
|
}
|
||||||
|
|
||||||
|
g_type_class_unref (class);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fd >= 0)
|
||||||
|
g_string_append_printf (err, " g_subprocess_launcher_take_%s_fd()", stream_name);
|
||||||
|
|
||||||
|
if (filename)
|
||||||
|
g_string_append_printf (err, " g_subprocess_launcher_set_%s_file_path()", stream_name);
|
||||||
|
|
||||||
|
g_critical ("You may specify at most one disposition for the %s stream, but you specified:%s.",
|
||||||
|
stream_name, err->str);
|
||||||
|
g_string_free (err, TRUE);
|
||||||
|
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static gboolean
|
||||||
|
verify_flags (GSubprocessFlags flags)
|
||||||
|
{
|
||||||
|
return verify_disposition ("stdin", flags & ALL_STDIN_FLAGS, -1, NULL) &&
|
||||||
|
verify_disposition ("stdout", flags & ALL_STDOUT_FLAGS, -1, NULL) &&
|
||||||
|
verify_disposition ("stderr", flags & ALL_STDERR_FLAGS, -1, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
g_subprocess_launcher_set_property (GObject *object, guint prop_id,
|
||||||
|
const GValue *value, GParamSpec *pspec)
|
||||||
|
{
|
||||||
|
GSubprocessLauncher *launcher = G_SUBPROCESS_LAUNCHER (object);
|
||||||
|
|
||||||
|
g_assert (prop_id == 1);
|
||||||
|
|
||||||
|
if (verify_flags (g_value_get_flags (value)))
|
||||||
|
launcher->flags = g_value_get_flags (value);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
g_subprocess_launcher_finalize (GObject *object)
|
||||||
|
{
|
||||||
|
GSubprocessLauncher *self = G_SUBPROCESS_LAUNCHER (object);
|
||||||
|
|
||||||
|
g_strfreev (self->envp);
|
||||||
|
g_free (self->cwd);
|
||||||
|
|
||||||
|
#ifdef G_OS_UNIX
|
||||||
|
g_free (self->stdin_path);
|
||||||
|
g_free (self->stdout_path);
|
||||||
|
g_free (self->stderr_path);
|
||||||
|
|
||||||
|
if (self->stdin_fd != -1)
|
||||||
|
close (self->stdin_fd);
|
||||||
|
|
||||||
|
if (self->stdout_fd != -1)
|
||||||
|
close (self->stdout_fd);
|
||||||
|
|
||||||
|
if (self->stderr_fd != -1)
|
||||||
|
close (self->stderr_fd);
|
||||||
|
|
||||||
|
g_clear_pointer (&self->basic_fd_assignments, g_array_unref);
|
||||||
|
g_clear_pointer (&self->needdup_fd_assignments, g_array_unref);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (self->child_setup_destroy_notify)
|
||||||
|
(* self->child_setup_destroy_notify) (self->child_setup_user_data);
|
||||||
|
|
||||||
|
G_OBJECT_CLASS (g_subprocess_launcher_parent_class)->finalize (object);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
g_subprocess_launcher_init (GSubprocessLauncher *self)
|
||||||
|
{
|
||||||
|
self->envp = g_listenv ();
|
||||||
|
|
||||||
|
self->stdin_fd = -1;
|
||||||
|
self->stdout_fd = -1;
|
||||||
|
self->stderr_fd = -1;
|
||||||
|
#ifdef G_OS_UNIX
|
||||||
|
self->basic_fd_assignments = g_array_new (FALSE, 0, sizeof (int));
|
||||||
|
self->needdup_fd_assignments = g_array_new (FALSE, 0, sizeof (int));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
g_subprocess_launcher_class_init (GSubprocessLauncherClass *class)
|
||||||
|
{
|
||||||
|
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
|
||||||
|
|
||||||
|
gobject_class->set_property = g_subprocess_launcher_set_property;
|
||||||
|
gobject_class->finalize = g_subprocess_launcher_finalize;
|
||||||
|
|
||||||
|
g_object_class_install_property (gobject_class, 1,
|
||||||
|
g_param_spec_flags ("flags", "Flags", "GSubprocessFlags for launched processes",
|
||||||
|
G_TYPE_SUBPROCESS_FLAGS, 0, G_PARAM_WRITABLE |
|
||||||
|
G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_new:
|
||||||
|
*
|
||||||
|
* Creates a new #GSubprocessLauncher.
|
||||||
|
*
|
||||||
|
* The launcher is created with the default options. A copy of the
|
||||||
|
* environment of the calling process is made at the time of this call
|
||||||
|
* and will be used as the environment that the process is launched in.
|
||||||
|
*
|
||||||
|
* Since: 2.40
|
||||||
|
**/
|
||||||
|
GSubprocessLauncher *
|
||||||
|
g_subprocess_launcher_new (GSubprocessFlags flags)
|
||||||
|
{
|
||||||
|
if (!verify_flags (flags))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
return g_object_new (G_TYPE_SUBPROCESS_LAUNCHER,
|
||||||
|
"flags", flags,
|
||||||
|
NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_set_environ:
|
||||||
|
* @self: a #GSubprocess
|
||||||
|
* @environ: the replacement environment
|
||||||
|
*
|
||||||
|
* Replace the entire environment of processes launched from this
|
||||||
|
* launcher with the given 'environ' variable.
|
||||||
|
*
|
||||||
|
* Typically you will build this variable by using g_listenv() to copy
|
||||||
|
* the process 'environ' and using the functions g_environ_setenv(),
|
||||||
|
* g_environ_unsetenv(), etc.
|
||||||
|
*
|
||||||
|
* As an alternative, you can use g_subprocess_launcher_setenv(),
|
||||||
|
* g_subprocess_launcher_unsetenv(), etc.
|
||||||
|
*
|
||||||
|
* All strings in this array are expected to be in the GLib file name
|
||||||
|
* encoding. On UNIX, this means that they can be arbitrary byte
|
||||||
|
* strings. On Windows, they should be in UTF-8.
|
||||||
|
*
|
||||||
|
* Since: 2.40
|
||||||
|
**/
|
||||||
|
void
|
||||||
|
g_subprocess_launcher_set_environ (GSubprocessLauncher *self,
|
||||||
|
gchar **environ)
|
||||||
|
{
|
||||||
|
g_strfreev (self->envp);
|
||||||
|
self->envp = g_strdupv (environ);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_setenv:
|
||||||
|
* @self: a #GSubprocess
|
||||||
|
* @variable: the environment variable to set, must not contain '='
|
||||||
|
* @value: the new value for the variable
|
||||||
|
* @overwrite: whether to change the variable if it already exists
|
||||||
|
*
|
||||||
|
* Sets the environment variable @variable in the environment of
|
||||||
|
* processes launched from this launcher.
|
||||||
|
*
|
||||||
|
* Both the variable's name and value should be in the GLib file name
|
||||||
|
* encoding. On UNIX, this means that they can be arbitrary byte
|
||||||
|
* strings. On Windows, they should be in UTF-8.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* Since: 2.40
|
||||||
|
**/
|
||||||
|
void
|
||||||
|
g_subprocess_launcher_setenv (GSubprocessLauncher *self,
|
||||||
|
const gchar *variable,
|
||||||
|
const gchar *value,
|
||||||
|
gboolean overwrite)
|
||||||
|
{
|
||||||
|
self->envp = g_environ_setenv (self->envp, variable, value, overwrite);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_unsetsenv:
|
||||||
|
* @self: a #GSubprocess
|
||||||
|
* @variable: the environment variable to unset, must not contain '='
|
||||||
|
*
|
||||||
|
* Removes the environment variable @variable from the environment of
|
||||||
|
* processes launched from this launcher.
|
||||||
|
*
|
||||||
|
* The variable name should be in the GLib file name encoding. On UNIX,
|
||||||
|
* this means that they can be arbitrary byte strings. On Windows, they
|
||||||
|
* should be in UTF-8.
|
||||||
|
*
|
||||||
|
* Since: 2.40
|
||||||
|
**/
|
||||||
|
void
|
||||||
|
g_subprocess_launcher_unsetenv (GSubprocessLauncher *self,
|
||||||
|
const gchar *variable)
|
||||||
|
{
|
||||||
|
self->envp = g_environ_unsetenv (self->envp, variable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_getenv:
|
||||||
|
* @self: a #GSubprocess
|
||||||
|
* @variable: the environment variable to get
|
||||||
|
*
|
||||||
|
* Returns the value of the environment variable @variable in the
|
||||||
|
* environment of processes launched from this launcher.
|
||||||
|
*
|
||||||
|
* The returned string is in the GLib file name encoding. On UNIX, this
|
||||||
|
* means that it can be an arbitrary byte string. On Windows, it will
|
||||||
|
* be UTF-8.
|
||||||
|
*
|
||||||
|
* Returns: the value of the environment variable, %NULL if unset
|
||||||
|
*
|
||||||
|
* Since: 2.40
|
||||||
|
**/
|
||||||
|
const gchar *
|
||||||
|
g_subprocess_launcher_getenv (GSubprocessLauncher *self,
|
||||||
|
const gchar *variable)
|
||||||
|
{
|
||||||
|
return g_environ_getenv (self->envp, variable);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_set_cwd:
|
||||||
|
* @self: a #GSubprocess
|
||||||
|
* @cwd: the cwd for launched processes
|
||||||
|
*
|
||||||
|
* Sets the current working directory that processes will be launched
|
||||||
|
* with.
|
||||||
|
*
|
||||||
|
* By default processes are launched with the current working directory
|
||||||
|
* of the launching process at the time of launch.
|
||||||
|
*
|
||||||
|
* Since: 2.40
|
||||||
|
**/
|
||||||
|
void
|
||||||
|
g_subprocess_launcher_set_cwd (GSubprocessLauncher *self,
|
||||||
|
const gchar *cwd)
|
||||||
|
{
|
||||||
|
g_free (self->cwd);
|
||||||
|
self->cwd = g_strdup (cwd);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_set_flags:
|
||||||
|
* @self: a #GSubprocessLauncher
|
||||||
|
* @flags: #GSubprocessFlags
|
||||||
|
*
|
||||||
|
* Sets the flags on the launcher.
|
||||||
|
*
|
||||||
|
* The default flags are %G_SUBPROCESS_FLAGS_NONE.
|
||||||
|
*
|
||||||
|
* You may not set flags that specify conflicting options for how to
|
||||||
|
* handle a particular stdio stream (eg: specifying both
|
||||||
|
* %G_SUBPROCESS_FLAGS_STDIN_PIPE and
|
||||||
|
* %G_SUBPROCESS_FLAGS_STDIN_INHERIT).
|
||||||
|
*
|
||||||
|
* You may also not set a flag that conflicts with a previous call to a
|
||||||
|
* function like g_subprocess_launcher_set_stdin_file_path() or
|
||||||
|
* g_subprocess_launcher_take_stdout_fd().
|
||||||
|
*
|
||||||
|
* Since: 2.40
|
||||||
|
**/
|
||||||
|
void
|
||||||
|
g_subprocess_launcher_set_flags (GSubprocessLauncher *self,
|
||||||
|
GSubprocessFlags flags)
|
||||||
|
{
|
||||||
|
if (verify_disposition ("stdin", flags & ALL_STDIN_FLAGS, self->stdin_fd, self->stdin_path) &&
|
||||||
|
verify_disposition ("stdout", flags & ALL_STDOUT_FLAGS, self->stdout_fd, self->stdout_path) &&
|
||||||
|
verify_disposition ("stderr", flags & ALL_STDERR_FLAGS, self->stderr_fd, self->stderr_path))
|
||||||
|
self->flags = flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef G_OS_UNIX
|
||||||
|
static void
|
||||||
|
assign_fd (gint *fd_ptr, gint fd)
|
||||||
|
{
|
||||||
|
gint flags;
|
||||||
|
|
||||||
|
if (*fd_ptr != -1)
|
||||||
|
close (*fd_ptr);
|
||||||
|
|
||||||
|
*fd_ptr = fd;
|
||||||
|
|
||||||
|
if (fd != -1)
|
||||||
|
{
|
||||||
|
/* best effort */
|
||||||
|
flags = fcntl (fd, F_GETFD);
|
||||||
|
if (~flags & FD_CLOEXEC)
|
||||||
|
fcntl (fd, F_SETFD, flags | FD_CLOEXEC);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_set_stdin_file_path:
|
||||||
|
* @self: a #GSubprocessLauncher
|
||||||
|
* @path: a filename or %NULL
|
||||||
|
*
|
||||||
|
* Sets the file path to use as the stdin for spawned processes.
|
||||||
|
*
|
||||||
|
* If @path is %NULL then any previously given path is unset.
|
||||||
|
*
|
||||||
|
* The file must exist or spawning the process will fail.
|
||||||
|
*
|
||||||
|
* You may not set a stdin file path if a stdin fd is already set or if
|
||||||
|
* the launcher flags contain any flags directing stdin elsewhere.
|
||||||
|
*
|
||||||
|
* This feature is only available on UNIX.
|
||||||
|
*
|
||||||
|
* Since: 2.40
|
||||||
|
**/
|
||||||
|
void
|
||||||
|
g_subprocess_launcher_set_stdin_file_path (GSubprocessLauncher *self,
|
||||||
|
const gchar *path)
|
||||||
|
{
|
||||||
|
if (verify_disposition ("stdin", self->flags & ALL_STDIN_FLAGS, self->stdin_fd, path))
|
||||||
|
{
|
||||||
|
g_free (self->stdin_path);
|
||||||
|
self->stdin_path = g_strdup (path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_take_stdin_fd:
|
||||||
|
* @self: a #GSubprocessLauncher
|
||||||
|
* @fd: a file descriptor, or -1
|
||||||
|
*
|
||||||
|
* Sets the file descriptor to use as the stdin for spawned processes.
|
||||||
|
*
|
||||||
|
* If @fd is -1 then any previously given fd is unset.
|
||||||
|
*
|
||||||
|
* Note that if your intention is to have the stdin of the calling
|
||||||
|
* process inherited by the child then %G_SUBPROCESS_FLAGS_STDIN_INHERIT
|
||||||
|
* is a better way to go about doing that.
|
||||||
|
*
|
||||||
|
* The passed @fd is noted but will not be touched in the current
|
||||||
|
* process. It is therefore necessary that it be kept open by the
|
||||||
|
* caller until the subprocess is spawned. The file descriptor will
|
||||||
|
* also not be explicitly closed on the child side, so it must be marked
|
||||||
|
* O_CLOEXEC if that's what you want.
|
||||||
|
*
|
||||||
|
* You may not set a stdin fd if a stdin file path is already set or if
|
||||||
|
* the launcher flags contain any flags directing stdin elsewhere.
|
||||||
|
*
|
||||||
|
* This feature is only available on UNIX.
|
||||||
|
*
|
||||||
|
* Since: 2.40
|
||||||
|
**/
|
||||||
|
void
|
||||||
|
g_subprocess_launcher_take_stdin_fd (GSubprocessLauncher *self,
|
||||||
|
gint fd)
|
||||||
|
{
|
||||||
|
if (verify_disposition ("stdin", self->flags & ALL_STDIN_FLAGS, fd, self->stdin_path))
|
||||||
|
assign_fd (&self->stdin_fd, fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_set_stdout_file_path:
|
||||||
|
* @self: a #GSubprocessLauncher
|
||||||
|
* @path: a filename or %NULL
|
||||||
|
*
|
||||||
|
* Sets the file path to use as the stdout for spawned processes.
|
||||||
|
*
|
||||||
|
* If @path is %NULL then any previously given path is unset.
|
||||||
|
*
|
||||||
|
* The file will be created or truncated when the process is spawned, as
|
||||||
|
* would be the case if using '>' at the shell.
|
||||||
|
*
|
||||||
|
* You may not set a stdout file path if a stdout fd is already set or
|
||||||
|
* if the launcher flags contain any flags directing stdout elsewhere.
|
||||||
|
*
|
||||||
|
* This feature is only available on UNIX.
|
||||||
|
*
|
||||||
|
* Since: 2.40
|
||||||
|
**/
|
||||||
|
void
|
||||||
|
g_subprocess_launcher_set_stdout_file_path (GSubprocessLauncher *self,
|
||||||
|
const gchar *path)
|
||||||
|
{
|
||||||
|
if (verify_disposition ("stdout", self->flags & ALL_STDOUT_FLAGS, self->stdout_fd, path))
|
||||||
|
{
|
||||||
|
g_free (self->stdout_path);
|
||||||
|
self->stdout_path = g_strdup (path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_take_stdout_fd:
|
||||||
|
* @self: a #GSubprocessLauncher
|
||||||
|
* @fd: a file descriptor, or -1
|
||||||
|
*
|
||||||
|
* Sets the file descriptor to use as the stdout for spawned processes.
|
||||||
|
*
|
||||||
|
* If @fd is -1 then any previously given fd is unset.
|
||||||
|
*
|
||||||
|
* Note that the default behaviour is to pass stdout through to the
|
||||||
|
* stdout of the parent process.
|
||||||
|
*
|
||||||
|
* The passed @fd is noted but will not be touched in the current
|
||||||
|
* process. It is therefore necessary that it be kept open by the
|
||||||
|
* caller until the subprocess is spawned. The file descriptor will
|
||||||
|
* also not be explicitly closed on the child side, so it must be marked
|
||||||
|
* O_CLOEXEC if that's what you want.
|
||||||
|
*
|
||||||
|
* You may not set a stdout fd if a stdout file path is already set or
|
||||||
|
* if the launcher flags contain any flags directing stdout elsewhere.
|
||||||
|
*
|
||||||
|
* This feature is only available on UNIX.
|
||||||
|
*
|
||||||
|
* Since: 2.40
|
||||||
|
**/
|
||||||
|
void
|
||||||
|
g_subprocess_launcher_take_stdout_fd (GSubprocessLauncher *self,
|
||||||
|
gint fd)
|
||||||
|
{
|
||||||
|
if (verify_disposition ("stdout", self->flags & ALL_STDOUT_FLAGS, fd, self->stdout_path))
|
||||||
|
assign_fd (&self->stdout_fd, fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_set_stderr_file_path:
|
||||||
|
* @self: a #GSubprocessLauncher
|
||||||
|
* @path: a filename or %NULL
|
||||||
|
*
|
||||||
|
* Sets the file path to use as the stderr for spawned processes.
|
||||||
|
*
|
||||||
|
* If @path is %NULL then any previously given path is unset.
|
||||||
|
*
|
||||||
|
* The file will be created or truncated when the process is spawned, as
|
||||||
|
* would be the case if using '2>' at the shell.
|
||||||
|
*
|
||||||
|
* If you want to send both stdout and stderr to the same file then use
|
||||||
|
* %G_SUBPROCESS_FLAGS_STDERR_MERGE.
|
||||||
|
*
|
||||||
|
* You may not set a stderr file path if a stderr fd is already set or
|
||||||
|
* if the launcher flags contain any flags directing stderr elsewhere.
|
||||||
|
*
|
||||||
|
* This feature is only available on UNIX.
|
||||||
|
*
|
||||||
|
* Since: 2.40
|
||||||
|
**/
|
||||||
|
void
|
||||||
|
g_subprocess_launcher_set_stderr_file_path (GSubprocessLauncher *self,
|
||||||
|
const gchar *path)
|
||||||
|
{
|
||||||
|
if (verify_disposition ("stderr", self->flags & ALL_STDERR_FLAGS, self->stderr_fd, path))
|
||||||
|
{
|
||||||
|
g_free (self->stderr_path);
|
||||||
|
self->stderr_path = g_strdup (path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_take_stderr_fd:
|
||||||
|
* @self: a #GSubprocessLauncher
|
||||||
|
* @fd: a file descriptor, or -1
|
||||||
|
*
|
||||||
|
* Sets the file descriptor to use as the stderr for spawned processes.
|
||||||
|
*
|
||||||
|
* If @fd is -1 then any previously given fd is unset.
|
||||||
|
*
|
||||||
|
* Note that the default behaviour is to pass stderr through to the
|
||||||
|
* stderr of the parent process.
|
||||||
|
*
|
||||||
|
* The passed @fd belongs to the #GSubprocessLauncher. It will be
|
||||||
|
* automatically closed when the launcher is finalized. The file
|
||||||
|
* descriptor will also be closed on the child side when executing the
|
||||||
|
* spawned process.
|
||||||
|
*
|
||||||
|
* You may not set a stderr fd if a stderr file path is already set or
|
||||||
|
* if the launcher flags contain any flags directing stderr elsewhere.
|
||||||
|
*
|
||||||
|
* This feature is only available on UNIX.
|
||||||
|
*
|
||||||
|
* Since: 2.40
|
||||||
|
**/
|
||||||
|
void
|
||||||
|
g_subprocess_launcher_take_stderr_fd (GSubprocessLauncher *self,
|
||||||
|
gint fd)
|
||||||
|
{
|
||||||
|
if (verify_disposition ("stderr", self->flags & ALL_STDERR_FLAGS, fd, self->stderr_path))
|
||||||
|
assign_fd (&self->stderr_fd, fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_pass_fd:
|
||||||
|
* @self: a #GSubprocessLauncher
|
||||||
|
* @source_fd: File descriptor in parent process
|
||||||
|
* @target_fd: Target descriptor for child process
|
||||||
|
*
|
||||||
|
* Pass an arbitrary file descriptor from parent process to
|
||||||
|
* the child. By default, all file descriptors from the parent
|
||||||
|
* will be closed. This function allows you to create (for example)
|
||||||
|
* a custom pipe() or socketpair() before launching the process, and
|
||||||
|
* choose the target descriptor in the child.
|
||||||
|
*
|
||||||
|
* An example use case is GNUPG, which has a command line argument
|
||||||
|
* --passphrase-fd providing a file descriptor number where it expects
|
||||||
|
* the passphrase to be written.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
g_subprocess_launcher_pass_fd (GSubprocessLauncher *self,
|
||||||
|
gint source_fd,
|
||||||
|
gint target_fd)
|
||||||
|
{
|
||||||
|
if (source_fd == target_fd)
|
||||||
|
{
|
||||||
|
g_array_append_val (self->basic_fd_assignments, source_fd);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
g_array_append_val (self->needdup_fd_assignments, source_fd);
|
||||||
|
g_array_append_val (self->needdup_fd_assignments, target_fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_set_child_setup:
|
||||||
|
* @self: a #GSubprocessLauncher
|
||||||
|
* @child_setup: a #GSpawnChildSetupFunc to use as the child setup function
|
||||||
|
* @user_data: user data for @child_setup
|
||||||
|
* @destroy_notify: a #GDestroyNotify for @user_data
|
||||||
|
*
|
||||||
|
* Sets up a child setup function.
|
||||||
|
*
|
||||||
|
* The child setup function will be called after fork() but before
|
||||||
|
* exec() on the child's side.
|
||||||
|
*
|
||||||
|
* @destroy_notify will not be automatically called on the child's side
|
||||||
|
* of the fork(). It will only be called when the last reference on the
|
||||||
|
* #GSubprocessLauncher is dropped or when a new child setup function is
|
||||||
|
* given.
|
||||||
|
*
|
||||||
|
* %NULL can be given as @child_setup to disable the functionality.
|
||||||
|
*
|
||||||
|
* Child setup functions are only available on UNIX.
|
||||||
|
*
|
||||||
|
* Since: 2.40
|
||||||
|
**/
|
||||||
|
void
|
||||||
|
g_subprocess_launcher_set_child_setup (GSubprocessLauncher *self,
|
||||||
|
GSpawnChildSetupFunc child_setup,
|
||||||
|
gpointer user_data,
|
||||||
|
GDestroyNotify destroy_notify)
|
||||||
|
{
|
||||||
|
if (self->child_setup_destroy_notify)
|
||||||
|
(* self->child_setup_destroy_notify) (self->child_setup_user_data);
|
||||||
|
|
||||||
|
self->child_setup_func = child_setup;
|
||||||
|
self->child_setup_user_data = user_data;
|
||||||
|
self->child_setup_destroy_notify = destroy_notify;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_spawn:
|
||||||
|
* @self: a #GSubprocessLauncher
|
||||||
|
* @error: Error
|
||||||
|
* @argv0: Command line arguments
|
||||||
|
* @...: Continued arguments, %NULL terminated
|
||||||
|
*
|
||||||
|
* A convenience helper for creating a #GSubprocess given a provided
|
||||||
|
* varargs list of arguments.
|
||||||
|
*
|
||||||
|
* Since: 2.40
|
||||||
|
* Returns: (transfer full): A new #GSubprocess, or %NULL on error (and @error will be set)
|
||||||
|
**/
|
||||||
|
GSubprocess *
|
||||||
|
g_subprocess_launcher_spawn (GSubprocessLauncher *launcher,
|
||||||
|
GError **error,
|
||||||
|
const gchar *argv0,
|
||||||
|
...)
|
||||||
|
{
|
||||||
|
GSubprocess *result;
|
||||||
|
GPtrArray *args;
|
||||||
|
const gchar *arg;
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
|
g_return_val_if_fail (argv0 != NULL && argv0[0] != '\0', NULL);
|
||||||
|
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
|
||||||
|
|
||||||
|
args = g_ptr_array_new ();
|
||||||
|
|
||||||
|
va_start (ap, argv0);
|
||||||
|
g_ptr_array_add (args, (gchar *) argv0);
|
||||||
|
while ((arg = va_arg (ap, const gchar *)))
|
||||||
|
g_ptr_array_add (args, (gchar *) arg);
|
||||||
|
|
||||||
|
result = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, error);
|
||||||
|
|
||||||
|
g_ptr_array_free (args, TRUE);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_subprocess_launcher_spawnv:
|
||||||
|
* @self: a #GSubprocessLauncher
|
||||||
|
* @argv: Command line arguments
|
||||||
|
* @error: Error
|
||||||
|
*
|
||||||
|
* A convenience helper for creating a #GSubprocess given a provided
|
||||||
|
* array of arguments.
|
||||||
|
*
|
||||||
|
* Since: 2.40
|
||||||
|
* Returns: (transfer full): A new #GSubprocess, or %NULL on error (and @error will be set)
|
||||||
|
**/
|
||||||
|
GSubprocess *
|
||||||
|
g_subprocess_launcher_spawnv (GSubprocessLauncher *launcher,
|
||||||
|
const gchar * const *argv,
|
||||||
|
GError **error)
|
||||||
|
{
|
||||||
|
GSubprocess *subprocess;
|
||||||
|
|
||||||
|
g_return_val_if_fail (argv != NULL && argv[0] != NULL && argv[0][0] != '\0', NULL);
|
||||||
|
|
||||||
|
subprocess = g_object_new (G_TYPE_SUBPROCESS,
|
||||||
|
"argv", argv,
|
||||||
|
"flags", launcher->flags,
|
||||||
|
NULL);
|
||||||
|
g_subprocess_set_launcher (subprocess, launcher);
|
||||||
|
|
||||||
|
if (!g_initable_init (G_INITABLE (subprocess), NULL, error))
|
||||||
|
{
|
||||||
|
g_object_unref (subprocess);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return subprocess;
|
||||||
|
}
|
118
gio/gsubprocesslauncher.h
Normal file
118
gio/gsubprocesslauncher.h
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
/* GIO - GLib Input, Output and Streaming Library
|
||||||
|
*
|
||||||
|
* Copyright © 2012,2013 Colin Walters <walters@verbum.org>
|
||||||
|
* Copyright © 2012,2013 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_LAUNCHER_H__
|
||||||
|
#define __G_SUBPROCESS_LAUNCHER_H__
|
||||||
|
|
||||||
|
#include <gio/giotypes.h>
|
||||||
|
|
||||||
|
G_BEGIN_DECLS
|
||||||
|
|
||||||
|
#define G_TYPE_SUBPROCESS_LAUNCHER (g_subprocess_launcher_get_type ())
|
||||||
|
#define G_SUBPROCESS_LAUNCHER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_SUBPROCESS_LAUNCHER, GSubprocessLauncher))
|
||||||
|
#define G_IS_SUBPROCESS_LAUNCHER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_SUBPROCESS_LAUNCHER))
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
GType g_subprocess_launcher_get_type (void) G_GNUC_CONST;
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
GSubprocessLauncher * g_subprocess_launcher_new (GSubprocessFlags flags);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
GSubprocess * g_subprocess_launcher_spawn (GSubprocessLauncher *self,
|
||||||
|
GError **error,
|
||||||
|
const gchar *argv0,
|
||||||
|
...);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
GSubprocess * g_subprocess_launcher_spawnv (GSubprocessLauncher *self,
|
||||||
|
const gchar * const *argv,
|
||||||
|
GError **error);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_launcher_set_environ (GSubprocessLauncher *self,
|
||||||
|
gchar **environ);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_launcher_setenv (GSubprocessLauncher *self,
|
||||||
|
const gchar *variable,
|
||||||
|
const gchar *value,
|
||||||
|
gboolean overwrite);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_launcher_unsetenv (GSubprocessLauncher *self,
|
||||||
|
const gchar *variable);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
const gchar * g_subprocess_launcher_getenv (GSubprocessLauncher *self,
|
||||||
|
const gchar *variable);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_launcher_set_cwd (GSubprocessLauncher *self,
|
||||||
|
const gchar *cwd);
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_launcher_set_flags (GSubprocessLauncher *self,
|
||||||
|
GSubprocessFlags flags);
|
||||||
|
|
||||||
|
/* Extended I/O control, only available on UNIX */
|
||||||
|
#ifdef G_OS_UNIX
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_launcher_set_stdin_file_path (GSubprocessLauncher *self,
|
||||||
|
const gchar *path);
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_launcher_take_stdin_fd (GSubprocessLauncher *self,
|
||||||
|
gint fd);
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_launcher_set_stdout_file_path (GSubprocessLauncher *self,
|
||||||
|
const gchar *path);
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_launcher_take_stdout_fd (GSubprocessLauncher *self,
|
||||||
|
gint fd);
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_launcher_set_stderr_file_path (GSubprocessLauncher *self,
|
||||||
|
const gchar *path);
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_launcher_take_stderr_fd (GSubprocessLauncher *self,
|
||||||
|
gint fd);
|
||||||
|
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_launcher_pass_fd (GSubprocessLauncher *self,
|
||||||
|
gint source_fd,
|
||||||
|
gint target_fd);
|
||||||
|
|
||||||
|
/* Child setup, only available on UNIX */
|
||||||
|
GLIB_AVAILABLE_IN_2_40
|
||||||
|
void g_subprocess_launcher_set_child_setup (GSubprocessLauncher *self,
|
||||||
|
GSpawnChildSetupFunc child_setup,
|
||||||
|
gpointer user_data,
|
||||||
|
GDestroyNotify destroy_notify);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
G_END_DECLS
|
||||||
|
|
||||||
|
#endif /* __G_SUBPROCESS_H__ */
|
2
gio/tests/.gitignore
vendored
2
gio/tests/.gitignore
vendored
@ -71,6 +71,8 @@ gdbus-test-fixture
|
|||||||
gdbus-testserver
|
gdbus-testserver
|
||||||
gdbus-threading
|
gdbus-threading
|
||||||
gio-du
|
gio-du
|
||||||
|
gsubprocess
|
||||||
|
gsubprocess-testprog
|
||||||
g-file
|
g-file
|
||||||
g-file-info
|
g-file-info
|
||||||
g-icon
|
g-icon
|
||||||
|
@ -30,6 +30,7 @@ test_programs = \
|
|||||||
data-output-stream \
|
data-output-stream \
|
||||||
fileattributematcher \
|
fileattributematcher \
|
||||||
filter-streams \
|
filter-streams \
|
||||||
|
gsubprocess \
|
||||||
g-file \
|
g-file \
|
||||||
g-file-info \
|
g-file-info \
|
||||||
g-icon \
|
g-icon \
|
||||||
@ -87,6 +88,7 @@ uninstalled_test_extra_programs = \
|
|||||||
gdbus-example-subtree \
|
gdbus-example-subtree \
|
||||||
gdbus-example-watch-name \
|
gdbus-example-watch-name \
|
||||||
gdbus-example-watch-proxy \
|
gdbus-example-watch-proxy \
|
||||||
|
gsubprocess-testprog \
|
||||||
httpd \
|
httpd \
|
||||||
proxy \
|
proxy \
|
||||||
resolver \
|
resolver \
|
||||||
@ -294,8 +296,7 @@ gdbus_non_socket_SOURCES = \
|
|||||||
uninstalled_test_extra_programs += gdbus-example-objectmanager-client
|
uninstalled_test_extra_programs += gdbus-example-objectmanager-client
|
||||||
gdbus_example_objectmanager_client_LDADD = gdbus-object-manager-example/libgdbus-example-objectmanager.la $(LDADD)
|
gdbus_example_objectmanager_client_LDADD = gdbus-object-manager-example/libgdbus-example-objectmanager.la $(LDADD)
|
||||||
|
|
||||||
uninstalled_test_extra_programs += gdbus-example-objectmanager-server
|
test_extra_programs += gsubprocess-testprog
|
||||||
gdbus_example_objectmanager_server_LDADD = gdbus-object-manager-example/libgdbus-example-objectmanager.la $(LDADD)
|
|
||||||
|
|
||||||
uninstalled_test_extra_programs += gdbus-test-fixture
|
uninstalled_test_extra_programs += gdbus-test-fixture
|
||||||
gdbus_test_fixture_LDADD = gdbus-object-manager-example/libgdbus-example-objectmanager.la $(LDADD)
|
gdbus_test_fixture_LDADD = gdbus-object-manager-example/libgdbus-example-objectmanager.la $(LDADD)
|
||||||
|
199
gio/tests/gsubprocess-testprog.c
Normal file
199
gio/tests/gsubprocess-testprog.c
Normal file
@ -0,0 +1,199 @@
|
|||||||
|
#include <gio/gio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
write_to_fds (int argc, char **argv)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 2; i < argc; i++)
|
||||||
|
{
|
||||||
|
int fd = atoi (argv[i]);
|
||||||
|
FILE *f = fdopen (fd, "w");
|
||||||
|
const char buf[] = "hello world\n";
|
||||||
|
size_t bytes_written;
|
||||||
|
|
||||||
|
g_assert (f != NULL);
|
||||||
|
|
||||||
|
bytes_written = fwrite (buf, 1, sizeof (buf), f);
|
||||||
|
g_assert (bytes_written == sizeof (buf));
|
||||||
|
|
||||||
|
if (fclose (f) == -1)
|
||||||
|
g_assert_not_reached ();
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int argc, char **argv)
|
||||||
|
{
|
||||||
|
GOptionContext *context;
|
||||||
|
GError *error = NULL;
|
||||||
|
const char *mode;
|
||||||
|
|
||||||
|
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 if (strcmp (mode, "write-to-fds") == 0)
|
||||||
|
return write_to_fds (argc, argv);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
g_printerr ("Unknown MODE %s\n", argv[1]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
855
gio/tests/gsubprocess.c
Normal file
855
gio/tests/gsubprocess.c
Normal file
@ -0,0 +1,855 @@
|
|||||||
|
#include <gio/gio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#ifdef G_OS_UNIX
|
||||||
|
#include <sys/wait.h>
|
||||||
|
#include <glib-unix.h>
|
||||||
|
#include <gio/gunixinputstream.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_newv ((const gchar * const *) args->pdata, G_SUBPROCESS_FLAGS_NONE, error);
|
||||||
|
g_ptr_array_free (args, TRUE);
|
||||||
|
g_assert_no_error (local_error);
|
||||||
|
|
||||||
|
g_subprocess_wait_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_newv ((const gchar * const *) args->pdata,
|
||||||
|
G_SUBPROCESS_FLAGS_STDOUT_SILENCE | G_SUBPROCESS_FLAGS_STDERR_SILENCE,
|
||||||
|
error);
|
||||||
|
g_ptr_array_free (args, TRUE);
|
||||||
|
g_assert_no_error (local_error);
|
||||||
|
|
||||||
|
g_subprocess_wait_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_newv ((const gchar * const *) args->pdata, G_SUBPROCESS_FLAGS_NONE, 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_newv ((const gchar * const *) args->pdata, G_SUBPROCESS_FLAGS_STDIN_INHERIT, error);
|
||||||
|
g_ptr_array_free (args, TRUE);
|
||||||
|
g_assert_no_error (local_error);
|
||||||
|
|
||||||
|
g_subprocess_wait_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;
|
||||||
|
GSubprocess *proc;
|
||||||
|
|
||||||
|
proc = g_subprocess_new (G_SUBPROCESS_FLAGS_NONE, error, "true", NULL);
|
||||||
|
g_assert_no_error (local_error);
|
||||||
|
|
||||||
|
g_subprocess_wait_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_newv ((const gchar * const *) args->pdata, G_SUBPROCESS_FLAGS_NONE, error);
|
||||||
|
g_ptr_array_free (args, TRUE);
|
||||||
|
g_assert_no_error (local_error);
|
||||||
|
|
||||||
|
g_subprocess_wait_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_newv ((const gchar * const *) args->pdata, G_SUBPROCESS_FLAGS_STDOUT_PIPE, 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_newv ((const gchar * const *) args->pdata,
|
||||||
|
G_SUBPROCESS_FLAGS_STDOUT_PIPE | G_SUBPROCESS_FLAGS_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;
|
||||||
|
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_newv ((const gchar * const *) args->pdata,
|
||||||
|
G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE,
|
||||||
|
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_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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
GCancellable *cancellable;
|
||||||
|
GError *error = NULL;
|
||||||
|
GSubprocess *cat;
|
||||||
|
gboolean result;
|
||||||
|
gchar buffer;
|
||||||
|
gssize s;
|
||||||
|
|
||||||
|
/* Spawn 'cat' */
|
||||||
|
cat = g_subprocess_new (G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE, &error, "cat", NULL);
|
||||||
|
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 (cat, NULL, &error);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
g_assert (g_subprocess_get_if_exited (cat));
|
||||||
|
g_assert_cmpint (g_subprocess_get_exit_status (cat), ==, 0);
|
||||||
|
g_assert (result);
|
||||||
|
|
||||||
|
g_object_unref (cat);
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
GSubprocess *subprocess = G_SUBPROCESS (object);
|
||||||
|
TestMultiSpliceData *data = user_data;
|
||||||
|
GError *error = NULL;
|
||||||
|
|
||||||
|
if (!g_subprocess_wait_finish (subprocess, result, &error))
|
||||||
|
{
|
||||||
|
if (!data->caught_error)
|
||||||
|
{
|
||||||
|
data->caught_error = TRUE;
|
||||||
|
g_propagate_error (&data->error, error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
g_spawn_check_exit_status (g_subprocess_get_exit_status (subprocess), &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;
|
||||||
|
GSubprocessLauncher *launcher;
|
||||||
|
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);
|
||||||
|
launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDIN_PIPE | G_SUBPROCESS_FLAGS_STDOUT_PIPE);
|
||||||
|
first = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, error);
|
||||||
|
g_assert_no_error (local_error);
|
||||||
|
second = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, error);
|
||||||
|
g_assert_no_error (local_error);
|
||||||
|
third = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, 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_async (first, NULL, on_subprocess_exited, &data);
|
||||||
|
data.events_pending++;
|
||||||
|
g_subprocess_wait_async (second, NULL, on_subprocess_exited, &data);
|
||||||
|
data.events_pending++;
|
||||||
|
g_subprocess_wait_async (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 (launcher);
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
GSubprocess *subprocess = G_SUBPROCESS (object);
|
||||||
|
GError *error = NULL;
|
||||||
|
|
||||||
|
g_subprocess_wait_finish (subprocess, result, &error);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
#ifdef G_OS_UNIX
|
||||||
|
g_assert (g_subprocess_get_if_signaled (subprocess));
|
||||||
|
g_assert (g_subprocess_get_term_sig (subprocess) == 9);
|
||||||
|
#endif
|
||||||
|
g_spawn_check_exit_status (g_subprocess_get_status (subprocess), &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_newv ((const gchar * const *) args->pdata, G_SUBPROCESS_FLAGS_NONE, error);
|
||||||
|
g_ptr_array_free (args, TRUE);
|
||||||
|
g_assert_no_error (local_error);
|
||||||
|
|
||||||
|
loop = g_main_loop_new (NULL, TRUE);
|
||||||
|
|
||||||
|
g_subprocess_wait_async (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;
|
||||||
|
GSubprocessLauncher *launcher;
|
||||||
|
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);
|
||||||
|
launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDIN_PIPE);
|
||||||
|
g_subprocess_launcher_set_stdout_file_path (launcher, tmp_file_path);
|
||||||
|
proc = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, 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_check (proc, NULL, error);
|
||||||
|
|
||||||
|
g_object_unref (launcher);
|
||||||
|
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;
|
||||||
|
GSubprocessLauncher *launcher;
|
||||||
|
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);
|
||||||
|
launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDIN_PIPE);
|
||||||
|
descriptor_stream = G_FILE_DESCRIPTOR_BASED (g_io_stream_get_output_stream (G_IO_STREAM (iostream)));
|
||||||
|
g_subprocess_launcher_take_stdout_fd (launcher, dup (g_file_descriptor_based_get_fd (descriptor_stream)));
|
||||||
|
proc = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, 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_check (proc, NULL, error);
|
||||||
|
|
||||||
|
g_object_unref (launcher);
|
||||||
|
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;
|
||||||
|
GSubprocessLauncher *launcher;
|
||||||
|
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);
|
||||||
|
launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDIN_PIPE);
|
||||||
|
g_subprocess_launcher_set_child_setup (launcher, child_setup, GINT_TO_POINTER (fd), NULL);
|
||||||
|
proc = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, 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_check (proc, NULL, error);
|
||||||
|
|
||||||
|
g_object_unref (launcher);
|
||||||
|
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
|
||||||
|
test_pass_fd (void)
|
||||||
|
{
|
||||||
|
GError *local_error = NULL;
|
||||||
|
GError **error = &local_error;
|
||||||
|
GInputStream *child_input;
|
||||||
|
GDataInputStream *child_datainput;
|
||||||
|
GSubprocessLauncher *launcher;
|
||||||
|
GSubprocess *proc;
|
||||||
|
GPtrArray *args;
|
||||||
|
int basic_pipefds[2];
|
||||||
|
int needdup_pipefds[2];
|
||||||
|
char *buf;
|
||||||
|
gsize len;
|
||||||
|
char *basic_fd_str;
|
||||||
|
char *needdup_fd_str;
|
||||||
|
|
||||||
|
g_unix_open_pipe (basic_pipefds, FD_CLOEXEC, error);
|
||||||
|
g_assert_no_error (local_error);
|
||||||
|
g_unix_open_pipe (needdup_pipefds, FD_CLOEXEC, error);
|
||||||
|
g_assert_no_error (local_error);
|
||||||
|
|
||||||
|
basic_fd_str = g_strdup_printf ("%d", basic_pipefds[1]);
|
||||||
|
needdup_fd_str = g_strdup_printf ("%d", needdup_pipefds[1] + 1);
|
||||||
|
|
||||||
|
args = get_test_subprocess_args ("write-to-fds", basic_fd_str, needdup_fd_str, NULL);
|
||||||
|
launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_NONE);
|
||||||
|
g_subprocess_launcher_pass_fd (launcher, basic_pipefds[1], basic_pipefds[1]);
|
||||||
|
g_subprocess_launcher_pass_fd (launcher, needdup_pipefds[1], needdup_pipefds[1] + 1);
|
||||||
|
proc = g_subprocess_launcher_spawnv (launcher, (const gchar * const *) args->pdata, error);
|
||||||
|
g_ptr_array_free (args, TRUE);
|
||||||
|
g_assert_no_error (local_error);
|
||||||
|
|
||||||
|
(void) close (basic_pipefds[1]);
|
||||||
|
(void) close (needdup_pipefds[1]);
|
||||||
|
g_free (basic_fd_str);
|
||||||
|
g_free (needdup_fd_str);
|
||||||
|
|
||||||
|
child_input = g_unix_input_stream_new (basic_pipefds[0], TRUE);
|
||||||
|
child_datainput = g_data_input_stream_new (child_input);
|
||||||
|
buf = g_data_input_stream_read_line_utf8 (child_datainput, &len, NULL, error);
|
||||||
|
g_assert_no_error (local_error);
|
||||||
|
g_assert_cmpstr (buf, ==, "hello world");
|
||||||
|
g_object_unref (child_datainput);
|
||||||
|
g_object_unref (child_input);
|
||||||
|
g_free (buf);
|
||||||
|
|
||||||
|
child_input = g_unix_input_stream_new (needdup_pipefds[0], TRUE);
|
||||||
|
child_datainput = g_data_input_stream_new (child_input);
|
||||||
|
buf = g_data_input_stream_read_line_utf8 (child_datainput, &len, NULL, error);
|
||||||
|
g_assert_no_error (local_error);
|
||||||
|
g_assert_cmpstr (buf, ==, "hello world");
|
||||||
|
g_free (buf);
|
||||||
|
g_object_unref (child_datainput);
|
||||||
|
g_object_unref (child_input);
|
||||||
|
|
||||||
|
g_object_unref (launcher);
|
||||||
|
g_object_unref (proc);
|
||||||
|
}
|
||||||
|
|
||||||
|
#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);
|
||||||
|
g_test_add_func ("/gsubprocess/pass-fd", test_pass_fd);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return g_test_run ();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user