spawn.xml   [plain text]


<refentry id="glib-Spawning-Processes">
<refmeta>
<refentrytitle>Spawning Processes</refentrytitle>
<manvolnum>3</manvolnum>
<refmiscinfo>GLIB Library</refmiscinfo>
</refmeta>

<refnamediv>
<refname>Spawning Processes</refname><refpurpose>process launching with <function><link linkend="fork"><function>fork()</function></link></function>/<function><link linkend="exec"><function>exec()</function></link></function>.</refpurpose>
</refnamediv>

<refsynopsisdiv><title>Synopsis</title>

<synopsis>

#include &lt;glib.h&gt;


enum        <link linkend="GSpawnError">GSpawnError</link>;
#define     <link linkend="G-SPAWN-ERROR-CAPS">G_SPAWN_ERROR</link>
enum        <link linkend="GSpawnFlags">GSpawnFlags</link>;
<link linkend="void">void</link>        (<link linkend="GSpawnChildSetupFunc">*GSpawnChildSetupFunc</link>)         (<link linkend="gpointer">gpointer</link> user_data);
<link linkend="gboolean">gboolean</link>    <link linkend="g-spawn-async-with-pipes">g_spawn_async_with_pipes</link>        (const <link linkend="gchar">gchar</link> *working_directory,
                                             <link linkend="gchar">gchar</link> **argv,
                                             <link linkend="gchar">gchar</link> **envp,
                                             <link linkend="GSpawnFlags">GSpawnFlags</link> flags,
                                             <link linkend="GSpawnChildSetupFunc">GSpawnChildSetupFunc</link> child_setup,
                                             <link linkend="gpointer">gpointer</link> user_data,
                                             <link linkend="GPid">GPid</link> *child_pid,
                                             <link linkend="gint">gint</link> *standard_input,
                                             <link linkend="gint">gint</link> *standard_output,
                                             <link linkend="gint">gint</link> *standard_error,
                                             <link linkend="GError">GError</link> **error);
<link linkend="gboolean">gboolean</link>    <link linkend="g-spawn-async">g_spawn_async</link>                   (const <link linkend="gchar">gchar</link> *working_directory,
                                             <link linkend="gchar">gchar</link> **argv,
                                             <link linkend="gchar">gchar</link> **envp,
                                             <link linkend="GSpawnFlags">GSpawnFlags</link> flags,
                                             <link linkend="GSpawnChildSetupFunc">GSpawnChildSetupFunc</link> child_setup,
                                             <link linkend="gpointer">gpointer</link> user_data,
                                             <link linkend="GPid">GPid</link> *child_pid,
                                             <link linkend="GError">GError</link> **error);
<link linkend="gboolean">gboolean</link>    <link linkend="g-spawn-sync">g_spawn_sync</link>                    (const <link linkend="gchar">gchar</link> *working_directory,
                                             <link linkend="gchar">gchar</link> **argv,
                                             <link linkend="gchar">gchar</link> **envp,
                                             <link linkend="GSpawnFlags">GSpawnFlags</link> flags,
                                             <link linkend="GSpawnChildSetupFunc">GSpawnChildSetupFunc</link> child_setup,
                                             <link linkend="gpointer">gpointer</link> user_data,
                                             <link linkend="gchar">gchar</link> **standard_output,
                                             <link linkend="gchar">gchar</link> **standard_error,
                                             <link linkend="gint">gint</link> *exit_status,
                                             <link linkend="GError">GError</link> **error);
<link linkend="gboolean">gboolean</link>    <link linkend="g-spawn-command-line-async">g_spawn_command_line_async</link>      (const <link linkend="gchar">gchar</link> *command_line,
                                             <link linkend="GError">GError</link> **error);
<link linkend="gboolean">gboolean</link>    <link linkend="g-spawn-command-line-sync">g_spawn_command_line_sync</link>       (const <link linkend="gchar">gchar</link> *command_line,
                                             <link linkend="gchar">gchar</link> **standard_output,
                                             <link linkend="gchar">gchar</link> **standard_error,
                                             <link linkend="gint">gint</link> *exit_status,
                                             <link linkend="GError">GError</link> **error);
<link linkend="void">void</link>        <link linkend="g-spawn-close-pid">g_spawn_close_pid</link>               (<link linkend="GPid">GPid</link> pid);

</synopsis>
</refsynopsisdiv>









<refsect1>
<title>Description</title>
<para>

</para>
</refsect1>

<refsect1>
<title>Details</title>
<refsect2>
<title><anchor id="GSpawnError"/>enum GSpawnError</title>
<indexterm><primary>GSpawnError</primary></indexterm><programlisting>typedef enum
{
  G_SPAWN_ERROR_FORK,   /* fork failed due to lack of memory */
  G_SPAWN_ERROR_READ,   /* read or select on pipes failed */
  G_SPAWN_ERROR_CHDIR,  /* changing to working dir failed */
  G_SPAWN_ERROR_ACCES,  /* execv() returned EACCES */
  G_SPAWN_ERROR_PERM,   /* execv() returned EPERM */
  G_SPAWN_ERROR_2BIG,   /* execv() returned E2BIG */
  G_SPAWN_ERROR_NOEXEC, /* execv() returned ENOEXEC */
  G_SPAWN_ERROR_NAMETOOLONG, /* ""  "" ENAMETOOLONG */
  G_SPAWN_ERROR_NOENT,       /* ""  "" ENOENT */
  G_SPAWN_ERROR_NOMEM,       /* ""  "" ENOMEM */
  G_SPAWN_ERROR_NOTDIR,      /* ""  "" ENOTDIR */
  G_SPAWN_ERROR_LOOP,        /* ""  "" ELOOP   */
  G_SPAWN_ERROR_TXTBUSY,     /* ""  "" ETXTBUSY */
  G_SPAWN_ERROR_IO,          /* ""  "" EIO */
  G_SPAWN_ERROR_NFILE,       /* ""  "" ENFILE */
  G_SPAWN_ERROR_MFILE,       /* ""  "" EMFLE */
  G_SPAWN_ERROR_INVAL,       /* ""  "" EINVAL */
  G_SPAWN_ERROR_ISDIR,       /* ""  "" EISDIR */
  G_SPAWN_ERROR_LIBBAD,      /* ""  "" ELIBBAD */
  G_SPAWN_ERROR_FAILED       /* other fatal failure, error-&gt;message
                              * should explain
                              */
} GSpawnError;
</programlisting>
<para>
Error codes returned by spawning processes.
</para><variablelist role="enum">
<varlistentry>
<term><literal>G_SPAWN_ERROR_FORK</literal></term>
<listitem><simpara>Fork failed due to lack of memory.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_READ</literal></term>
<listitem><simpara>Read or select on pipes failed.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_CHDIR</literal></term>
<listitem><simpara>Changing to working directory failed.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_ACCES</literal></term>
<listitem><simpara><function><link linkend="execv"><function>execv()</function></link></function> returned <literal>EACCES</literal>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_PERM</literal></term>
<listitem><simpara><function><link linkend="execv"><function>execv()</function></link></function> returned <literal>EPERM</literal>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_2BIG</literal></term>
<listitem><simpara><function><link linkend="execv"><function>execv()</function></link></function> returned <literal>E2BIG</literal>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_NOEXEC</literal></term>
<listitem><simpara><function><link linkend="execv"><function>execv()</function></link></function> returned <literal>ENOEXEC</literal>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_NAMETOOLONG</literal></term>
<listitem><simpara><function><link linkend="execv"><function>execv()</function></link></function> returned <literal>ENAMETOOLONG</literal>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_NOENT</literal></term>
<listitem><simpara><function><link linkend="execv"><function>execv()</function></link></function> returned <literal>ENOENT</literal>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_NOMEM</literal></term>
<listitem><simpara><function><link linkend="execv"><function>execv()</function></link></function> returned <literal>ENOMEM</literal>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_NOTDIR</literal></term>
<listitem><simpara><function><link linkend="execv"><function>execv()</function></link></function> returned <literal>ENOTDIR</literal>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_LOOP</literal></term>
<listitem><simpara><function><link linkend="execv"><function>execv()</function></link></function> returned <literal>ELOOP</literal>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_TXTBUSY</literal></term>
<listitem><simpara><function><link linkend="execv"><function>execv()</function></link></function> returned <literal>ETXTBUSY</literal>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_IO</literal></term>
<listitem><simpara><function><link linkend="execv"><function>execv()</function></link></function> returned <literal>EIO</literal>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_NFILE</literal></term>
<listitem><simpara><function><link linkend="execv"><function>execv()</function></link></function> returned <literal>ENFILE</literal>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_MFILE</literal></term>
<listitem><simpara><function><link linkend="execv"><function>execv()</function></link></function> returned <literal>EMFILE</literal>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_INVAL</literal></term>
<listitem><simpara><function><link linkend="execv"><function>execv()</function></link></function> returned <literal>EINVAL</literal>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_ISDIR</literal></term>
<listitem><simpara><function><link linkend="execv"><function>execv()</function></link></function> returned <literal>EISDIR</literal>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_LIBBAD</literal></term>
<listitem><simpara><function><link linkend="execv"><function>execv()</function></link></function> returned <literal>ELIBBAD</literal>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_ERROR_FAILED</literal></term>
<listitem><simpara>Some other fatal failure, <literal>error-&gt;message</literal> should explain.

</simpara></listitem>
</varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="G-SPAWN-ERROR-CAPS"/>G_SPAWN_ERROR</title>
<indexterm><primary>G_SPAWN_ERROR</primary></indexterm><programlisting>#define G_SPAWN_ERROR g_spawn_error_quark ()
</programlisting>
<para>
Error domain for spawning processes. Errors in this domain will
be from the <link linkend="GSpawnError"><type>GSpawnError</type></link> enumeration. See <link linkend="GError"><type>GError</type></link> for information on
error domains.
</para></refsect2>
<refsect2>
<title><anchor id="GSpawnFlags"/>enum GSpawnFlags</title>
<indexterm><primary>GSpawnFlags</primary></indexterm><programlisting>typedef enum
{
  G_SPAWN_LEAVE_DESCRIPTORS_OPEN = 1 &lt;&lt; 0,
  G_SPAWN_DO_NOT_REAP_CHILD      = 1 &lt;&lt; 1,
  /* look for argv[0] in the path i.e. use execvp() */
  G_SPAWN_SEARCH_PATH            = 1 &lt;&lt; 2,
  /* Dump output to /dev/null */
  G_SPAWN_STDOUT_TO_DEV_NULL     = 1 &lt;&lt; 3,
  G_SPAWN_STDERR_TO_DEV_NULL     = 1 &lt;&lt; 4,
  G_SPAWN_CHILD_INHERITS_STDIN   = 1 &lt;&lt; 5,
  G_SPAWN_FILE_AND_ARGV_ZERO     = 1 &lt;&lt; 6
} GSpawnFlags;
</programlisting>
<para>
Flags passed to <link linkend="g-spawn-sync"><function>g_spawn_sync()</function></link>, <link linkend="g-spawn-async"><function>g_spawn_async()</function></link> and <link linkend="g-spawn-async-with-pipes"><function>g_spawn_async_with_pipes()</function></link>.
</para><variablelist role="enum">
<varlistentry>
<term><literal>G_SPAWN_LEAVE_DESCRIPTORS_OPEN</literal></term>
<listitem><simpara>the parent's open file descriptors will be 
 inherited by the child; otherwise all descriptors except stdin/stdout/stderr 
 will be closed before calling <function><link linkend="exec"><function>exec()</function></link></function> in the child.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_DO_NOT_REAP_CHILD</literal></term>
<listitem><simpara>the child will not be automatically reaped; you 
  must call <function><link linkend="waitpid"><function>waitpid()</function></link></function> or handle <literal>SIGCHLD</literal> 
  yourself, or the child will become a zombie.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_SEARCH_PATH</literal></term>
<listitem><simpara><literal>argv[0]</literal> need not be an absolute path, 
  it will be looked for in the user's <envar>PATH</envar>.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_STDOUT_TO_DEV_NULL</literal></term>
<listitem><simpara>the child's standad output will be discarded, 
  instead of going to the same location as the parent's standard output.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_STDERR_TO_DEV_NULL</literal></term>
<listitem><simpara>the child's standard error will be discarded.
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_CHILD_INHERITS_STDIN</literal></term>
<listitem><simpara>the child will inherit the parent's standard 
  input (by default, the child's standard input is attached to 
  <filename>/dev/null</filename>).
</simpara></listitem>
</varlistentry>
<varlistentry>
<term><literal>G_SPAWN_FILE_AND_ARGV_ZERO</literal></term>
<listitem><simpara>the first element of <literal>argv</literal> is 
  the file to execute, while the remaining elements are the actual argument 
  vector to pass to the file. Normally <link linkend="g-spawn-async-with-pipes"><function>g_spawn_async_with_pipes()</function></link> uses 
  <literal>argv[0]</literal> as the file to execute, and passes all of 
  <literal>argv</literal> to the child.

</simpara></listitem>
</varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="GSpawnChildSetupFunc"/>GSpawnChildSetupFunc ()</title>
<indexterm><primary>GSpawnChildSetupFunc</primary></indexterm><programlisting><link linkend="void">void</link>        (*GSpawnChildSetupFunc)         (<link linkend="gpointer">gpointer</link> user_data);</programlisting>
<para>
Specifies the type of the setup function passed to <link linkend="g-spawn-async"><function>g_spawn_async()</function></link>,
<link linkend="g-spawn-sync"><function>g_spawn_sync()</function></link> and <link linkend="g-spawn-async-with-pipes"><function>g_spawn_async_with_pipes()</function></link>. It is called in the 
child after GLib has performed all the setup it plans to perform 
but before calling <function><link linkend="exec"><function>exec()</function></link></function>. Obviously, actions 
taken in this function will only affect the child, not the parent.
</para><variablelist role="params">
<varlistentry><term><parameter>user_data</parameter>&nbsp;:</term>
<listitem><simpara>user data to pass to the function.


</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-spawn-async-with-pipes"/>g_spawn_async_with_pipes ()</title>
<indexterm><primary>g_spawn_async_with_pipes</primary></indexterm><programlisting><link linkend="gboolean">gboolean</link>    g_spawn_async_with_pipes        (const <link linkend="gchar">gchar</link> *working_directory,
                                             <link linkend="gchar">gchar</link> **argv,
                                             <link linkend="gchar">gchar</link> **envp,
                                             <link linkend="GSpawnFlags">GSpawnFlags</link> flags,
                                             <link linkend="GSpawnChildSetupFunc">GSpawnChildSetupFunc</link> child_setup,
                                             <link linkend="gpointer">gpointer</link> user_data,
                                             <link linkend="GPid">GPid</link> *child_pid,
                                             <link linkend="gint">gint</link> *standard_input,
                                             <link linkend="gint">gint</link> *standard_output,
                                             <link linkend="gint">gint</link> *standard_error,
                                             <link linkend="GError">GError</link> **error);</programlisting>
<para>
Executes a child program asynchronously (your program will not
block waiting for the child to exit). The child program is
specified by the only argument that must be provided, <parameter>argv</parameter>. <parameter>argv</parameter>
should be a <literal>NULL</literal>-terminated array of strings, to be passed as the
argument vector for the child. The first string in <parameter>argv</parameter> is of
course the name of the program to execute. By default, the name of
the program must be a full path; the <envar>PATH</envar> shell variable 
will only be searched if you pass the <literal>G_SPAWN_SEARCH_PATH</literal> flag.
</para>
<para>
On Windows, the low-level child process creation API
(<link linkend="CreateProcess"><function>CreateProcess()</function></link>)doesn't use argument vectors,
but a command line. The C runtime library's
<function>spawn*()</function> family of functions (which
<link linkend="g-spawn-async-with-pipes"><function>g_spawn_async_with_pipes()</function></link> eventually calls) paste the argument
vector elements into a command line, and the C runtime startup code
does a corresponding recostruction of an argument vector from the
command line, to be passed to <link linkend="main"><function>main()</function></link>. Complications arise when you have
argument vector elements that contain spaces of double quotes. The
<function>spawn*()</function> functions don't do any quoting or
escaping, but on the other hand the startup code does do unquoting
and unescaping in order to enable receiving arguments with embedded
spaces or double quotes. To work around this asymmetry,
<link linkend="g-spawn-async-with-pipes"><function>g_spawn_async_with_pipes()</function></link> will do quoting and escaping on argument
vector elements that need it before calling the C runtime
<link linkend="spawn"><function>spawn()</function></link> function.
</para>
<para>
<parameter>envp</parameter> is a <literal>NULL</literal>-terminated array of strings, where each string
has the form <literal>KEY=VALUE</literal>. This will become
the child's environment. If <parameter>envp</parameter> is <literal>NULL</literal>, the child inherits its
parent's environment.
</para>
<para>
<parameter>flags</parameter> should be the bitwise OR of any flags you want to affect the
function's behavior. On Unix, the <literal>G_SPAWN_DO_NOT_REAP_CHILD</literal> means
that the child will not be automatically reaped; you must call
<link linkend="waitpid"><function>waitpid()</function></link> or handle <literal>SIGCHLD</literal> yourself, or the
child will become a zombie. On Windows, the flag means that a
handle to the child will be returned <parameter>child_pid</parameter>. You must call
<link linkend="CloseHandle"><function>CloseHandle()</function></link> on it eventually (or exit the
process), or the child processs will continue to take up some table
space even after its death. Quite similar to zombies on Unix,
actually.
</para>
<para>
<literal>G_SPAWN_LEAVE_DESCRIPTORS_OPEN</literal> means that the parent's open file
descriptors will be inherited by the child; otherwise all
descriptors except stdin/stdout/stderr will be closed before
calling <link linkend="exec"><function>exec()</function></link> in the child. <literal>G_SPAWN_SEARCH_PATH</literal> 
means that <literal>argv[0]</literal> need not be an absolute path, it
will be looked for in the user's <envar>PATH</envar>. 
<literal>G_SPAWN_STDOUT_TO_DEV_NULL</literal> means that the child's standard output will 
be discarded, instead of going to the same location as the parent's 
standard output. If you use this flag, <parameter>standard_output</parameter> must be <literal>NULL</literal>.
<literal>G_SPAWN_STDERR_TO_DEV_NULL</literal> means that the child's standard error
will be discarded, instead of going to the same location as the parent's
standard error. If you use this flag, <parameter>standard_error</parameter> must be <literal>NULL</literal>.
<literal>G_SPAWN_CHILD_INHERITS_STDIN</literal> means that the child will inherit the parent's
standard input (by default, the child's standard input is attached to
/dev/null). If you use this flag, <parameter>standard_input</parameter> must be <literal>NULL</literal>.
<literal>G_SPAWN_FILE_AND_ARGV_ZERO</literal> means that the first element of <parameter>argv</parameter> is
the file to execute, while the remaining elements are the
actual argument vector to pass to the file. Normally
<link linkend="g-spawn-async-with-pipes"><function>g_spawn_async_with_pipes()</function></link> uses <parameter>argv</parameter>[0] as the file to execute, and
passes all of <parameter>argv</parameter> to the child.
</para>
<para>
<parameter>child_setup</parameter> and <parameter>user_data</parameter> are a function and user data. On POSIX
platforms, the function is called in the child after GLib has
performed all the setup it plans to perform (including creating
pipes, closing file descriptors, etc.) but before calling
<link linkend="exec"><function>exec()</function></link>. That is, <parameter>child_setup</parameter> is called just
before calling <link linkend="exec"><function>exec()</function></link> in the child. Obviously
actions taken in this function will only affect the child, not the
parent. On Windows, there is no separate <link linkend="fork"><function>fork()</function></link> and <link linkend="exec"><function>exec()</function></link>
functionality. Child processes are created and run right away with
one API call, <link linkend="CreateProcess"><function>CreateProcess()</function></link>. <parameter>child_setup</parameter> is
called in the parent process just before creating the child
process. You should carefully consider what you do in <parameter>child_setup</parameter>
if you intend your software to be portable to Windows.
</para>
<para>
If non-<literal>NULL</literal>, <parameter>child_pid</parameter> will on Unix be filled with the child's
process ID. You can use the process ID to send signals to the
child, or to <link linkend="waitpid"><function>waitpid()</function></link> if you specified the
<literal>G_SPAWN_DO_NOT_REAP_CHILD</literal> flag. On Windows, <parameter>child_pid</parameter> will be
filled with a handle to the child process only if you specified the
<literal>G_SPAWN_DO_NOT_REAP_CHILD</literal> flag. You can then access the child
process using the Win32 API, for example wait for its termination
with the <function>WaitFor*()</function> functions, or examine its
exit code with <link linkend="GetExitCodeProcess"><function>GetExitCodeProcess()</function></link>. You should close the handle 
with <link linkend="CloseHandle"><function>CloseHandle()</function></link> when you no longer need it.
</para>
<para>
If non-<literal>NULL</literal>, the <parameter>standard_input</parameter>, <parameter>standard_output</parameter>, <parameter>standard_error</parameter>
locations will be filled with file descriptors for writing to the child's
standard input or reading from its standard output or standard error.
The caller of <link linkend="g-spawn-async-with-pipes"><function>g_spawn_async_with_pipes()</function></link> must close these file descriptors
when they are no longer in use. If these parameters are <literal>NULL</literal>, the corresponding
pipe won't be created.
</para>
<para>
If <parameter>standard_input</parameter> is NULL, the child's standard input is attached to /dev/null
unless <literal>G_SPAWN_CHILD_INHERITS_STDIN</literal> is set.
</para>
<para>
If <parameter>standard_error</parameter> is NULL, the child's standard error goes to the same location
as the parent's standard error unless <literal>G_SPAWN_STDERR_TO_DEV_NULL</literal> is set.
</para>
<para>
If <parameter>standard_output</parameter> is NULL, the child's standard output goes to the same location
as the parent's standard output unless <literal>G_SPAWN_STDOUT_TO_DEV_NULL</literal> is set.
</para>
<para>
<parameter>error</parameter> can be <literal>NULL</literal> to ignore errors, or non-<literal>NULL</literal> to report errors.
If an error is set, the function returns <literal>FALSE</literal>. Errors
are reported even if they occur in the child (for example if the
executable in <literal>argv[0]</literal> is not found). Typically
the <literal>message</literal> field of returned errors should be displayed
to users. Possible errors are those from the <link linkend="G-SPAWN-ERROR-CAPS"><type>G_SPAWN_ERROR</type></link> domain.
</para>
<para>
If an error occurs, <parameter>child_pid</parameter>, <parameter>standard_input</parameter>, <parameter>standard_output</parameter>,
and <parameter>standard_error</parameter> will not be filled with valid values.
</para>
<para>
If <parameter>child_pid</parameter> is not <literal>NULL</literal> and an error does not occur then the returned
pid must be closed using <link linkend="g-spawn-close-pid"><function>g_spawn_close_pid()</function></link>.</para>
<para>

</para><variablelist role="params">
<varlistentry><term><parameter>working_directory</parameter>&nbsp;:</term>
<listitem><simpara> child's current working directory, or <literal>NULL</literal> to inherit parent's
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>argv</parameter>&nbsp;:</term>
<listitem><simpara> child's argument vector
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>envp</parameter>&nbsp;:</term>
<listitem><simpara> child's environment, or <literal>NULL</literal> to inherit parent's
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>flags</parameter>&nbsp;:</term>
<listitem><simpara> flags from <link linkend="GSpawnFlags"><type>GSpawnFlags</type></link>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>child_setup</parameter>&nbsp;:</term>
<listitem><simpara> function to run in the child just before <link linkend="exec"><function>exec()</function></link>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>user_data</parameter>&nbsp;:</term>
<listitem><simpara> user data for <parameter>child_setup</parameter>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>child_pid</parameter>&nbsp;:</term>
<listitem><simpara> return location for child process ID, or <literal>NULL</literal>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>standard_input</parameter>&nbsp;:</term>
<listitem><simpara> return location for file descriptor to write to child's stdin, or <literal>NULL</literal>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>standard_output</parameter>&nbsp;:</term>
<listitem><simpara> return location for file descriptor to read child's stdout, or <literal>NULL</literal>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>standard_error</parameter>&nbsp;:</term>
<listitem><simpara> return location for file descriptor to read child's stderr, or <literal>NULL</literal>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>error</parameter>&nbsp;:</term>
<listitem><simpara> return location for error
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara> <literal>TRUE</literal> on success, <literal>FALSE</literal> if an error was set
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-spawn-async"/>g_spawn_async ()</title>
<indexterm><primary>g_spawn_async</primary></indexterm><programlisting><link linkend="gboolean">gboolean</link>    g_spawn_async                   (const <link linkend="gchar">gchar</link> *working_directory,
                                             <link linkend="gchar">gchar</link> **argv,
                                             <link linkend="gchar">gchar</link> **envp,
                                             <link linkend="GSpawnFlags">GSpawnFlags</link> flags,
                                             <link linkend="GSpawnChildSetupFunc">GSpawnChildSetupFunc</link> child_setup,
                                             <link linkend="gpointer">gpointer</link> user_data,
                                             <link linkend="GPid">GPid</link> *child_pid,
                                             <link linkend="GError">GError</link> **error);</programlisting>
<para>
See <link linkend="g-spawn-async-with-pipes"><function>g_spawn_async_with_pipes()</function></link> for a full description; this function
simply calls the <link linkend="g-spawn-async-with-pipes"><function>g_spawn_async_with_pipes()</function></link> without any pipes.</para>
<para>

</para><variablelist role="params">
<varlistentry><term><parameter>working_directory</parameter>&nbsp;:</term>
<listitem><simpara> child's current working directory, or <literal>NULL</literal> to inherit parent's
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>argv</parameter>&nbsp;:</term>
<listitem><simpara> child's argument vector
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>envp</parameter>&nbsp;:</term>
<listitem><simpara> child's environment, or <literal>NULL</literal> to inherit parent's
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>flags</parameter>&nbsp;:</term>
<listitem><simpara> flags from <link linkend="GSpawnFlags"><type>GSpawnFlags</type></link>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>child_setup</parameter>&nbsp;:</term>
<listitem><simpara> function to run in the child just before <link linkend="exec"><function>exec()</function></link>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>user_data</parameter>&nbsp;:</term>
<listitem><simpara> user data for <parameter>child_setup</parameter>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>child_pid</parameter>&nbsp;:</term>
<listitem><simpara> return location for child process ID, or <literal>NULL</literal>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>error</parameter>&nbsp;:</term>
<listitem><simpara> return location for error
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara> <literal>TRUE</literal> on success, <literal>FALSE</literal> if error is set
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-spawn-sync"/>g_spawn_sync ()</title>
<indexterm><primary>g_spawn_sync</primary></indexterm><programlisting><link linkend="gboolean">gboolean</link>    g_spawn_sync                    (const <link linkend="gchar">gchar</link> *working_directory,
                                             <link linkend="gchar">gchar</link> **argv,
                                             <link linkend="gchar">gchar</link> **envp,
                                             <link linkend="GSpawnFlags">GSpawnFlags</link> flags,
                                             <link linkend="GSpawnChildSetupFunc">GSpawnChildSetupFunc</link> child_setup,
                                             <link linkend="gpointer">gpointer</link> user_data,
                                             <link linkend="gchar">gchar</link> **standard_output,
                                             <link linkend="gchar">gchar</link> **standard_error,
                                             <link linkend="gint">gint</link> *exit_status,
                                             <link linkend="GError">GError</link> **error);</programlisting>
<para>
Executes a child synchronously (waits for the child to exit before returning).
All output from the child is stored in <parameter>standard_output</parameter> and <parameter>standard_error</parameter>,
if those parameters are non-<literal>NULL</literal>. If <parameter>exit_status</parameter> is non-<literal>NULL</literal>, the exit 
status of the child is stored there as it would be returned by 
<link linkend="waitpid"><function>waitpid()</function></link>; standard UNIX macros such as <link linkend="WIFEXITED-CAPS"><function>WIFEXITED()</function></link> and <link linkend="WEXITSTATUS-CAPS"><function>WEXITSTATUS()</function></link> 
must be used to evaluate the exit status. If an error occurs, no data is 
returned in <parameter>standard_output</parameter>, <parameter>standard_error</parameter>, or <parameter>exit_status</parameter>.
</para>
<para>
This function calls <link linkend="g-spawn-async-with-pipes"><function>g_spawn_async_with_pipes()</function></link> internally; see that function
for full details on the other parameters.</para>
<para>

</para><variablelist role="params">
<varlistentry><term><parameter>working_directory</parameter>&nbsp;:</term>
<listitem><simpara> child's current working directory, or <literal>NULL</literal> to inherit parent's
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>argv</parameter>&nbsp;:</term>
<listitem><simpara> child's argument vector
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>envp</parameter>&nbsp;:</term>
<listitem><simpara> child's environment, or <literal>NULL</literal> to inherit parent's
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>flags</parameter>&nbsp;:</term>
<listitem><simpara> flags from <link linkend="GSpawnFlags"><type>GSpawnFlags</type></link>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>child_setup</parameter>&nbsp;:</term>
<listitem><simpara> function to run in the child just before <link linkend="exec"><function>exec()</function></link>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>user_data</parameter>&nbsp;:</term>
<listitem><simpara> user data for <parameter>child_setup</parameter>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>standard_output</parameter>&nbsp;:</term>
<listitem><simpara> return location for child output 
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>standard_error</parameter>&nbsp;:</term>
<listitem><simpara> return location for child error messages
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>exit_status</parameter>&nbsp;:</term>
<listitem><simpara> child exit status, as returned by <link linkend="waitpid"><function>waitpid()</function></link>
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>error</parameter>&nbsp;:</term>
<listitem><simpara> return location for error
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara> <literal>TRUE</literal> on success, <literal>FALSE</literal> if an error was set.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-spawn-command-line-async"/>g_spawn_command_line_async ()</title>
<indexterm><primary>g_spawn_command_line_async</primary></indexterm><programlisting><link linkend="gboolean">gboolean</link>    g_spawn_command_line_async      (const <link linkend="gchar">gchar</link> *command_line,
                                             <link linkend="GError">GError</link> **error);</programlisting>
<para>
A simple version of <link linkend="g-spawn-async"><function>g_spawn_async()</function></link> that parses a command line with
<link linkend="g-shell-parse-argv"><function>g_shell_parse_argv()</function></link> and passes it to <link linkend="g-spawn-async"><function>g_spawn_async()</function></link>. Runs a
command line in the background. Unlike <link linkend="g-spawn-async"><function>g_spawn_async()</function></link>, the
<literal>G_SPAWN_SEARCH_PATH</literal> flag is enabled, other flags are not. Note
that <literal>G_SPAWN_SEARCH_PATH</literal> can have security implications, so
consider using <link linkend="g-spawn-async"><function>g_spawn_async()</function></link> directly if appropriate. Possible
errors are those from <link linkend="g-shell-parse-argv"><function>g_shell_parse_argv()</function></link> and <link linkend="g-spawn-async"><function>g_spawn_async()</function></link>.
</para>
<para>
The same concerns on Windows apply as for <link linkend="g-spawn-command-line-sync"><function>g_spawn_command_line_sync()</function></link>.</para>
<para>

</para><variablelist role="params">
<varlistentry><term><parameter>command_line</parameter>&nbsp;:</term>
<listitem><simpara> a command line
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>error</parameter>&nbsp;:</term>
<listitem><simpara> return location for errors
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara> <literal>TRUE</literal> on success, <literal>FALSE</literal> if error is set.
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-spawn-command-line-sync"/>g_spawn_command_line_sync ()</title>
<indexterm><primary>g_spawn_command_line_sync</primary></indexterm><programlisting><link linkend="gboolean">gboolean</link>    g_spawn_command_line_sync       (const <link linkend="gchar">gchar</link> *command_line,
                                             <link linkend="gchar">gchar</link> **standard_output,
                                             <link linkend="gchar">gchar</link> **standard_error,
                                             <link linkend="gint">gint</link> *exit_status,
                                             <link linkend="GError">GError</link> **error);</programlisting>
<para>
A simple version of <link linkend="g-spawn-sync"><function>g_spawn_sync()</function></link> with little-used parameters
removed, taking a command line instead of an argument vector.  See
<link linkend="g-spawn-sync"><function>g_spawn_sync()</function></link> for full details. <parameter>command_line</parameter> will be parsed by
<link linkend="g-shell-parse-argv"><function>g_shell_parse_argv()</function></link>. Unlike <link linkend="g-spawn-sync"><function>g_spawn_sync()</function></link>, the <literal>G_SPAWN_SEARCH_PATH</literal> flag
is enabled. Note that <literal>G_SPAWN_SEARCH_PATH</literal> can have security
implications, so consider using <link linkend="g-spawn-sync"><function>g_spawn_sync()</function></link> directly if
appropriate. Possible errors are those from <link linkend="g-spawn-sync"><function>g_spawn_sync()</function></link> and those
from <link linkend="g-shell-parse-argv"><function>g_shell_parse_argv()</function></link>.
</para>
<para>
On Windows, please note the implications of <link linkend="g-shell-parse-argv"><function>g_shell_parse_argv()</function></link>
parsing <parameter>command_line</parameter>. Space is a separator, and backslashes are
special. Thus you cannot simply pass a <parameter>command_line</parameter> containing
canonical Windows paths, like "c:\\program files\\app\\app.exe", as
the backslashes will be eaten, and the space will act as a
separator. You need to enclose such paths with single quotes, like
"'c:\\program files\\app\\app.exe' 'e:\\folder\\argument.txt'".</para>
<para>

</para><variablelist role="params">
<varlistentry><term><parameter>command_line</parameter>&nbsp;:</term>
<listitem><simpara> a command line 
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>standard_output</parameter>&nbsp;:</term>
<listitem><simpara> return location for child output
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>standard_error</parameter>&nbsp;:</term>
<listitem><simpara> return location for child errors
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>exit_status</parameter>&nbsp;:</term>
<listitem><simpara> return location for child exit status
</simpara></listitem></varlistentry>
<varlistentry><term><parameter>error</parameter>&nbsp;:</term>
<listitem><simpara> return location for errors
</simpara></listitem></varlistentry>
<varlistentry><term><emphasis>Returns</emphasis> :</term><listitem><simpara> <literal>TRUE</literal> on success, <literal>FALSE</literal> if an error was set
</simpara></listitem></varlistentry>
</variablelist></refsect2>
<refsect2>
<title><anchor id="g-spawn-close-pid"/>g_spawn_close_pid ()</title>
<indexterm><primary>g_spawn_close_pid</primary></indexterm><programlisting><link linkend="void">void</link>        g_spawn_close_pid               (<link linkend="GPid">GPid</link> pid);</programlisting>
<para>
On some platforms, notably WIN32, the <link linkend="GPid"><type>GPid</type></link> type represents a resource
which must be closed to prevent resource leaking. <link linkend="g-spawn-close-pid"><function>g_spawn_close_pid()</function></link>
is provided for this purpose. It should be used on all platforms, even
though it doesn't do anything under UNIX.</para>
<para>

</para><variablelist role="params">
<varlistentry><term><parameter>pid</parameter>&nbsp;:</term>
<listitem><simpara> The process identifier to close
</simpara></listitem></varlistentry>
</variablelist></refsect2>

</refsect1>




</refentry>