Blame glib/gspawn.c

Packit ae235b
/* gspawn.c - Process launching
Packit ae235b
 *
Packit ae235b
 *  Copyright 2000 Red Hat, Inc.
Packit ae235b
 *  g_execvpe implementation based on GNU libc execvp:
Packit ae235b
 *   Copyright 1991, 92, 95, 96, 97, 98, 99 Free Software Foundation, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public License
Packit ae235b
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <sys/time.h>
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include <sys/wait.h>
Packit ae235b
#include <unistd.h>
Packit ae235b
#include <errno.h>
Packit ae235b
#include <fcntl.h>
Packit ae235b
#include <signal.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <stdlib.h>   /* for fdwalk */
Packit ae235b
#include <dirent.h>
Packit ae235b
Packit ae235b
#ifdef HAVE_SYS_SELECT_H
Packit ae235b
#include <sys/select.h>
Packit ae235b
#endif /* HAVE_SYS_SELECT_H */
Packit ae235b
Packit ae235b
#ifdef HAVE_SYS_RESOURCE_H
Packit ae235b
#include <sys/resource.h>
Packit ae235b
#endif /* HAVE_SYS_RESOURCE_H */
Packit ae235b
Packit ae235b
#include "gspawn.h"
Packit ae235b
#include "gthread.h"
Packit ae235b
#include "glib/gstdio.h"
Packit ae235b
Packit ae235b
#include "genviron.h"
Packit ae235b
#include "gmem.h"
Packit ae235b
#include "gshell.h"
Packit ae235b
#include "gstring.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gtestutils.h"
Packit ae235b
#include "gutils.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
#include "glib-unix.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:spawn
Packit ae235b
 * @Short_description: process launching
Packit ae235b
 * @Title: Spawning Processes
Packit ae235b
 *
Packit ae235b
 * GLib supports spawning of processes with an API that is more
Packit ae235b
 * convenient than the bare UNIX fork() and exec().
Packit ae235b
 *
Packit ae235b
 * The g_spawn family of functions has synchronous (g_spawn_sync())
Packit ae235b
 * and asynchronous variants (g_spawn_async(), g_spawn_async_with_pipes()),
Packit ae235b
 * as well as convenience variants that take a complete shell-like
Packit ae235b
 * commandline (g_spawn_command_line_sync(), g_spawn_command_line_async()).
Packit ae235b
 *
Packit ae235b
 * See #GSubprocess in GIO for a higher-level API that provides
Packit ae235b
 * stream interfaces for communication with child processes.
Packit ae235b
 *
Packit ae235b
 * An example of using g_spawn_async_with_pipes():
Packit ae235b
 * |[
Packit ae235b
 * const gchar * const argv[] = { "my-favourite-program", "--args", NULL };
Packit ae235b
 * gint child_stdout, child_stderr;
Packit ae235b
 * GPid child_pid;
Packit ae235b
 * g_autoptr(GError) error = NULL;
Packit ae235b
 *
Packit ae235b
 * // Spawn child process.
Packit ae235b
 * g_spawn_async_with_pipes (NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL,
Packit ae235b
 *                           NULL, &child_pid, NULL, &child_stdout,
Packit ae235b
 *                           &child_stderr, &error);
Packit ae235b
 * if (error != NULL)
Packit ae235b
 *   {
Packit ae235b
 *     g_error ("Spawning child failed: %s", error->message);
Packit ae235b
 *     return;
Packit ae235b
 *   }
Packit ae235b
 *
Packit ae235b
 * // Add a child watch function which will be called when the child process
Packit ae235b
 * // exits.
Packit ae235b
 * g_child_watch_add (child_pid, child_watch_cb, NULL);
Packit ae235b
 *
Packit ae235b
 * // You could watch for output on @child_stdout and @child_stderr using
Packit ae235b
 * // #GUnixInputStream or #GIOChannel here.
Packit ae235b
 *
Packit ae235b
 * static void
Packit ae235b
 * child_watch_cb (GPid     pid,
Packit ae235b
 *                 gint     status,
Packit ae235b
 *                 gpointer user_data)
Packit ae235b
 * {
Packit ae235b
 *   g_message ("Child %" G_PID_FORMAT " exited %s", pid,
Packit ae235b
 *              g_spawn_check_exit_status (status, NULL) ? "normally" : "abnormally");
Packit ae235b
 *
Packit ae235b
 *   // Free any resources associated with the child here, such as I/O channels
Packit ae235b
 *   // on its stdout and stderr FDs. If you have no code to put in the
Packit ae235b
 *   // child_watch_cb() callback, you can remove it and the g_child_watch_add()
Packit ae235b
 *   // call, but you must also remove the G_SPAWN_DO_NOT_REAP_CHILD flag,
Packit ae235b
 *   // otherwise the child process will stay around as a zombie until this
Packit ae235b
 *   // process exits.
Packit ae235b
 *
Packit ae235b
 *   g_spawn_close_pid (pid);
Packit ae235b
 * }
Packit ae235b
 * ]|
Packit ae235b
 */
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
static gint g_execute (const gchar  *file,
Packit ae235b
                       gchar **argv,
Packit ae235b
                       gchar **envp,
Packit ae235b
                       gboolean search_path,
Packit ae235b
                       gboolean search_path_from_envp);
Packit ae235b
Packit ae235b
static gboolean fork_exec_with_pipes (gboolean              intermediate_child,
Packit ae235b
                                      const gchar          *working_directory,
Packit ae235b
                                      gchar               **argv,
Packit ae235b
                                      gchar               **envp,
Packit ae235b
                                      gboolean              close_descriptors,
Packit ae235b
                                      gboolean              search_path,
Packit ae235b
                                      gboolean              search_path_from_envp,
Packit ae235b
                                      gboolean              stdout_to_null,
Packit ae235b
                                      gboolean              stderr_to_null,
Packit ae235b
                                      gboolean              child_inherits_stdin,
Packit ae235b
                                      gboolean              file_and_argv_zero,
Packit ae235b
                                      gboolean              cloexec_pipes,
Packit ae235b
                                      GSpawnChildSetupFunc  child_setup,
Packit ae235b
                                      gpointer              user_data,
Packit ae235b
                                      GPid                 *child_pid,
Packit ae235b
                                      gint                 *standard_input,
Packit ae235b
                                      gint                 *standard_output,
Packit ae235b
                                      gint                 *standard_error,
Packit ae235b
                                      GError              **error);
Packit ae235b
Packit ae235b
G_DEFINE_QUARK (g-exec-error-quark, g_spawn_error)
Packit ae235b
G_DEFINE_QUARK (g-spawn-exit-error-quark, g_spawn_exit_error)
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_spawn_async:
Packit ae235b
 * @working_directory: (type filename) (nullable): child's current working
Packit ae235b
 *     directory, or %NULL to inherit parent's
Packit ae235b
 * @argv: (array zero-terminated=1) (element-type filename):
Packit ae235b
 *     child's argument vector
Packit ae235b
 * @envp: (array zero-terminated=1) (element-type filename) (nullable):
Packit ae235b
 *     child's environment, or %NULL to inherit parent's
Packit ae235b
 * @flags: flags from #GSpawnFlags
Packit ae235b
 * @child_setup: (scope async) (nullable): function to run in the child just before exec()
Packit ae235b
 * @user_data: (closure): user data for @child_setup
Packit ae235b
 * @child_pid: (out) (optional): return location for child process reference, or %NULL
Packit ae235b
 * @error: return location for error
Packit ae235b
 * 
Packit ae235b
 * See g_spawn_async_with_pipes() for a full description; this function
Packit ae235b
 * simply calls the g_spawn_async_with_pipes() without any pipes.
Packit ae235b
 *
Packit ae235b
 * You should call g_spawn_close_pid() on the returned child process
Packit ae235b
 * reference when you don't need it any more.
Packit ae235b
 * 
Packit ae235b
 * If you are writing a GTK+ application, and the program you are spawning is a
Packit ae235b
 * graphical application too, then to ensure that the spawned program opens its
Packit ae235b
 * windows on the right screen, you may want to use #GdkAppLaunchContext,
Packit ae235b
 * #GAppLaunchContext, or set the %DISPLAY environment variable.
Packit ae235b
 *
Packit ae235b
 * Note that the returned @child_pid on Windows is a handle to the child
Packit ae235b
 * process and not its identifier. Process handles and process identifiers
Packit ae235b
 * are different concepts on Windows.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE if error is set
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_spawn_async (const gchar          *working_directory,
Packit ae235b
               gchar               **argv,
Packit ae235b
               gchar               **envp,
Packit ae235b
               GSpawnFlags           flags,
Packit ae235b
               GSpawnChildSetupFunc  child_setup,
Packit ae235b
               gpointer              user_data,
Packit ae235b
               GPid                 *child_pid,
Packit ae235b
               GError              **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (argv != NULL, FALSE);
Packit ae235b
  
Packit ae235b
  return g_spawn_async_with_pipes (working_directory,
Packit ae235b
                                   argv, envp,
Packit ae235b
                                   flags,
Packit ae235b
                                   child_setup,
Packit ae235b
                                   user_data,
Packit ae235b
                                   child_pid,
Packit ae235b
                                   NULL, NULL, NULL,
Packit ae235b
                                   error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Avoids a danger in threaded situations (calling close()
Packit ae235b
 * on a file descriptor twice, and another thread has
Packit ae235b
 * re-opened it since the first close)
Packit ae235b
 */
Packit ae235b
static void
Packit ae235b
close_and_invalidate (gint *fd)
Packit ae235b
{
Packit ae235b
  if (*fd < 0)
Packit ae235b
    return;
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      (void) g_close (*fd, NULL);
Packit ae235b
      *fd = -1;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Some versions of OS X define READ_OK in public headers */
Packit ae235b
#undef READ_OK
Packit ae235b
Packit ae235b
typedef enum
Packit ae235b
{
Packit ae235b
  READ_FAILED = 0, /* FALSE */
Packit ae235b
  READ_OK,
Packit ae235b
  READ_EOF
Packit ae235b
} ReadResult;
Packit ae235b
Packit ae235b
static ReadResult
Packit ae235b
read_data (GString *str,
Packit ae235b
           gint     fd,
Packit ae235b
           GError **error)
Packit ae235b
{
Packit ae235b
  gssize bytes;
Packit ae235b
  gchar buf[4096];
Packit ae235b
Packit ae235b
 again:
Packit ae235b
  bytes = read (fd, buf, 4096);
Packit ae235b
Packit ae235b
  if (bytes == 0)
Packit ae235b
    return READ_EOF;
Packit ae235b
  else if (bytes > 0)
Packit ae235b
    {
Packit ae235b
      g_string_append_len (str, buf, bytes);
Packit ae235b
      return READ_OK;
Packit ae235b
    }
Packit ae235b
  else if (errno == EINTR)
Packit ae235b
    goto again;
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_SPAWN_ERROR,
Packit ae235b
                   G_SPAWN_ERROR_READ,
Packit ae235b
                   _("Failed to read data from child process (%s)"),
Packit ae235b
                   g_strerror (errsv));
Packit ae235b
Packit ae235b
      return READ_FAILED;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_spawn_sync:
Packit ae235b
 * @working_directory: (type filename) (nullable): child's current working
Packit ae235b
 *     directory, or %NULL to inherit parent's
Packit ae235b
 * @argv: (array zero-terminated=1) (element-type filename):
Packit ae235b
 *     child's argument vector
Packit ae235b
 * @envp: (array zero-terminated=1) (element-type filename) (nullable):
Packit ae235b
 *     child's environment, or %NULL to inherit parent's
Packit ae235b
 * @flags: flags from #GSpawnFlags
Packit ae235b
 * @child_setup: (scope async) (nullable): function to run in the child just before exec()
Packit ae235b
 * @user_data: (closure): user data for @child_setup
Packit ae235b
 * @standard_output: (out) (array zero-terminated=1) (element-type guint8) (optional): return location for child output, or %NULL
Packit ae235b
 * @standard_error: (out) (array zero-terminated=1) (element-type guint8) (optional): return location for child error messages, or %NULL
Packit ae235b
 * @exit_status: (out) (optional): return location for child exit status, as returned by waitpid(), or %NULL
Packit ae235b
 * @error: return location for error, or %NULL
Packit ae235b
 *
Packit ae235b
 * Executes a child synchronously (waits for the child to exit before returning).
Packit ae235b
 * All output from the child is stored in @standard_output and @standard_error,
Packit ae235b
 * if those parameters are non-%NULL. Note that you must set the  
Packit ae235b
 * %G_SPAWN_STDOUT_TO_DEV_NULL and %G_SPAWN_STDERR_TO_DEV_NULL flags when
Packit ae235b
 * passing %NULL for @standard_output and @standard_error.
Packit ae235b
 *
Packit ae235b
 * If @exit_status is non-%NULL, the platform-specific exit status of
Packit ae235b
 * the child is stored there; see the documentation of
Packit ae235b
 * g_spawn_check_exit_status() for how to use and interpret this.
Packit ae235b
 * Note that it is invalid to pass %G_SPAWN_DO_NOT_REAP_CHILD in
Packit ae235b
 * @flags, and on POSIX platforms, the same restrictions as for
Packit ae235b
 * g_child_watch_source_new() apply.
Packit ae235b
 *
Packit ae235b
 * If an error occurs, no data is returned in @standard_output,
Packit ae235b
 * @standard_error, or @exit_status.
Packit ae235b
 *
Packit ae235b
 * This function calls g_spawn_async_with_pipes() internally; see that
Packit ae235b
 * function for full details on the other parameters and details on
Packit ae235b
 * how these functions work on Windows.
Packit ae235b
 * 
Packit ae235b
 * Returns: %TRUE on success, %FALSE if an error was set
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_spawn_sync (const gchar          *working_directory,
Packit ae235b
              gchar               **argv,
Packit ae235b
              gchar               **envp,
Packit ae235b
              GSpawnFlags           flags,
Packit ae235b
              GSpawnChildSetupFunc  child_setup,
Packit ae235b
              gpointer              user_data,
Packit ae235b
              gchar               **standard_output,
Packit ae235b
              gchar               **standard_error,
Packit ae235b
              gint                 *exit_status,
Packit ae235b
              GError              **error)     
Packit ae235b
{
Packit ae235b
  gint outpipe = -1;
Packit ae235b
  gint errpipe = -1;
Packit ae235b
  GPid pid;
Packit ae235b
  fd_set fds;
Packit ae235b
  gint ret;
Packit ae235b
  GString *outstr = NULL;
Packit ae235b
  GString *errstr = NULL;
Packit ae235b
  gboolean failed;
Packit ae235b
  gint status;
Packit ae235b
  
Packit ae235b
  g_return_val_if_fail (argv != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
Packit ae235b
  g_return_val_if_fail (standard_output == NULL ||
Packit ae235b
                        !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
Packit ae235b
  g_return_val_if_fail (standard_error == NULL ||
Packit ae235b
                        !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
Packit ae235b
  
Packit ae235b
  /* Just to ensure segfaults if callers try to use
Packit ae235b
   * these when an error is reported.
Packit ae235b
   */
Packit ae235b
  if (standard_output)
Packit ae235b
    *standard_output = NULL;
Packit ae235b
Packit ae235b
  if (standard_error)
Packit ae235b
    *standard_error = NULL;
Packit ae235b
  
Packit ae235b
  if (!fork_exec_with_pipes (FALSE,
Packit ae235b
                             working_directory,
Packit ae235b
                             argv,
Packit ae235b
                             envp,
Packit ae235b
                             !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
Packit ae235b
                             (flags & G_SPAWN_SEARCH_PATH) != 0,
Packit ae235b
                             (flags & G_SPAWN_SEARCH_PATH_FROM_ENVP) != 0,
Packit ae235b
                             (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
Packit ae235b
                             (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
Packit ae235b
                             (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
Packit ae235b
                             (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
Packit ae235b
                             (flags & G_SPAWN_CLOEXEC_PIPES) != 0,
Packit ae235b
                             child_setup,
Packit ae235b
                             user_data,
Packit ae235b
                             &pid,
Packit ae235b
                             NULL,
Packit ae235b
                             standard_output ? &outpipe : NULL,
Packit ae235b
                             standard_error ? &errpipe : NULL,
Packit ae235b
                             error))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  /* Read data from child. */
Packit ae235b
  
Packit ae235b
  failed = FALSE;
Packit ae235b
Packit ae235b
  if (outpipe >= 0)
Packit ae235b
    {
Packit ae235b
      outstr = g_string_new (NULL);
Packit ae235b
    }
Packit ae235b
      
Packit ae235b
  if (errpipe >= 0)
Packit ae235b
    {
Packit ae235b
      errstr = g_string_new (NULL);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Read data until we get EOF on both pipes. */
Packit ae235b
  while (!failed &&
Packit ae235b
         (outpipe >= 0 ||
Packit ae235b
          errpipe >= 0))
Packit ae235b
    {
Packit ae235b
      ret = 0;
Packit ae235b
          
Packit ae235b
      FD_ZERO (&fds);
Packit ae235b
      if (outpipe >= 0)
Packit ae235b
        FD_SET (outpipe, &fds);
Packit ae235b
      if (errpipe >= 0)
Packit ae235b
        FD_SET (errpipe, &fds);
Packit ae235b
          
Packit ae235b
      ret = select (MAX (outpipe, errpipe) + 1,
Packit ae235b
                    &fds,
Packit ae235b
                    NULL, NULL,
Packit ae235b
                    NULL /* no timeout */);
Packit ae235b
Packit ae235b
      if (ret < 0)
Packit ae235b
        {
Packit ae235b
          int errsv = errno;
Packit ae235b
Packit ae235b
	  if (errno == EINTR)
Packit ae235b
	    continue;
Packit ae235b
Packit ae235b
          failed = TRUE;
Packit ae235b
Packit ae235b
          g_set_error (error,
Packit ae235b
                       G_SPAWN_ERROR,
Packit ae235b
                       G_SPAWN_ERROR_READ,
Packit ae235b
                       _("Unexpected error in select() reading data from a child process (%s)"),
Packit ae235b
                       g_strerror (errsv));
Packit ae235b
              
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (outpipe >= 0 && FD_ISSET (outpipe, &fds))
Packit ae235b
        {
Packit ae235b
          switch (read_data (outstr, outpipe, error))
Packit ae235b
            {
Packit ae235b
            case READ_FAILED:
Packit ae235b
              failed = TRUE;
Packit ae235b
              break;
Packit ae235b
            case READ_EOF:
Packit ae235b
              close_and_invalidate (&outpipe);
Packit ae235b
              outpipe = -1;
Packit ae235b
              break;
Packit ae235b
            default:
Packit ae235b
              break;
Packit ae235b
            }
Packit ae235b
Packit ae235b
          if (failed)
Packit ae235b
            break;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (errpipe >= 0 && FD_ISSET (errpipe, &fds))
Packit ae235b
        {
Packit ae235b
          switch (read_data (errstr, errpipe, error))
Packit ae235b
            {
Packit ae235b
            case READ_FAILED:
Packit ae235b
              failed = TRUE;
Packit ae235b
              break;
Packit ae235b
            case READ_EOF:
Packit ae235b
              close_and_invalidate (&errpipe);
Packit ae235b
              errpipe = -1;
Packit ae235b
              break;
Packit ae235b
            default:
Packit ae235b
              break;
Packit ae235b
            }
Packit ae235b
Packit ae235b
          if (failed)
Packit ae235b
            break;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* These should only be open still if we had an error.  */
Packit ae235b
  
Packit ae235b
  if (outpipe >= 0)
Packit ae235b
    close_and_invalidate (&outpipe);
Packit ae235b
  if (errpipe >= 0)
Packit ae235b
    close_and_invalidate (&errpipe);
Packit ae235b
  
Packit ae235b
  /* Wait for child to exit, even if we have
Packit ae235b
   * an error pending.
Packit ae235b
   */
Packit ae235b
 again:
Packit ae235b
      
Packit ae235b
  ret = waitpid (pid, &status, 0);
Packit ae235b
Packit ae235b
  if (ret < 0)
Packit ae235b
    {
Packit ae235b
      if (errno == EINTR)
Packit ae235b
        goto again;
Packit ae235b
      else if (errno == ECHILD)
Packit ae235b
        {
Packit ae235b
          if (exit_status)
Packit ae235b
            {
Packit ae235b
              g_warning ("In call to g_spawn_sync(), exit status of a child process was requested but ECHILD was received by waitpid(). See the documentation of g_child_watch_source_new() for possible causes.");
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              /* We don't need the exit status. */
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          if (!failed) /* avoid error pileups */
Packit ae235b
            {
Packit ae235b
              int errsv = errno;
Packit ae235b
Packit ae235b
              failed = TRUE;
Packit ae235b
                  
Packit ae235b
              g_set_error (error,
Packit ae235b
                           G_SPAWN_ERROR,
Packit ae235b
                           G_SPAWN_ERROR_READ,
Packit ae235b
                           _("Unexpected error in waitpid() (%s)"),
Packit ae235b
                           g_strerror (errsv));
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  if (failed)
Packit ae235b
    {
Packit ae235b
      if (outstr)
Packit ae235b
        g_string_free (outstr, TRUE);
Packit ae235b
      if (errstr)
Packit ae235b
        g_string_free (errstr, TRUE);
Packit ae235b
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      if (exit_status)
Packit ae235b
        *exit_status = status;
Packit ae235b
      
Packit ae235b
      if (standard_output)        
Packit ae235b
        *standard_output = g_string_free (outstr, FALSE);
Packit ae235b
Packit ae235b
      if (standard_error)
Packit ae235b
        *standard_error = g_string_free (errstr, FALSE);
Packit ae235b
Packit ae235b
      return TRUE;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_spawn_async_with_pipes:
Packit ae235b
 * @working_directory: (type filename) (nullable): child's current working
Packit ae235b
 *     directory, or %NULL to inherit parent's, in the GLib file name encoding
Packit ae235b
 * @argv: (array zero-terminated=1) (element-type filename): child's argument
Packit ae235b
 *     vector, in the GLib file name encoding
Packit ae235b
 * @envp: (array zero-terminated=1) (element-type filename) (nullable):
Packit ae235b
 *     child's environment, or %NULL to inherit parent's, in the GLib file
Packit ae235b
 *     name encoding
Packit ae235b
 * @flags: flags from #GSpawnFlags
Packit ae235b
 * @child_setup: (scope async) (nullable): function to run in the child just before exec()
Packit ae235b
 * @user_data: (closure): user data for @child_setup
Packit ae235b
 * @child_pid: (out) (optional): return location for child process ID, or %NULL
Packit ae235b
 * @standard_input: (out) (optional): return location for file descriptor to write to child's stdin, or %NULL
Packit ae235b
 * @standard_output: (out) (optional): return location for file descriptor to read child's stdout, or %NULL
Packit ae235b
 * @standard_error: (out) (optional): return location for file descriptor to read child's stderr, or %NULL
Packit ae235b
 * @error: return location for error
Packit ae235b
 *
Packit ae235b
 * Executes a child program asynchronously (your program will not
Packit ae235b
 * block waiting for the child to exit). The child program is
Packit ae235b
 * specified by the only argument that must be provided, @argv.
Packit ae235b
 * @argv should be a %NULL-terminated array of strings, to be passed
Packit ae235b
 * as the argument vector for the child. The first string in @argv
Packit ae235b
 * is of course the name of the program to execute. By default, the
Packit ae235b
 * name of the program must be a full path. If @flags contains the
Packit ae235b
 * %G_SPAWN_SEARCH_PATH flag, the `PATH` environment variable is
Packit ae235b
 * used to search for the executable. If @flags contains the
Packit ae235b
 * %G_SPAWN_SEARCH_PATH_FROM_ENVP flag, the `PATH` variable from 
Packit ae235b
 * @envp is used to search for the executable. If both the
Packit ae235b
 * %G_SPAWN_SEARCH_PATH and %G_SPAWN_SEARCH_PATH_FROM_ENVP flags
Packit ae235b
 * are set, the `PATH` variable from @envp takes precedence over
Packit ae235b
 * the environment variable.
Packit ae235b
 *
Packit ae235b
 * If the program name is not a full path and %G_SPAWN_SEARCH_PATH flag is not
Packit ae235b
 * used, then the program will be run from the current directory (or
Packit ae235b
 * @working_directory, if specified); this might be unexpected or even
Packit ae235b
 * dangerous in some cases when the current directory is world-writable.
Packit ae235b
 *
Packit ae235b
 * On Windows, note that all the string or string vector arguments to
Packit ae235b
 * this function and the other g_spawn*() functions are in UTF-8, the
Packit ae235b
 * GLib file name encoding. Unicode characters that are not part of
Packit ae235b
 * the system codepage passed in these arguments will be correctly
Packit ae235b
 * available in the spawned program only if it uses wide character API
Packit ae235b
 * to retrieve its command line. For C programs built with Microsoft's
Packit ae235b
 * tools it is enough to make the program have a wmain() instead of
Packit ae235b
 * main(). wmain() has a wide character argument vector as parameter.
Packit ae235b
 *
Packit ae235b
 * At least currently, mingw doesn't support wmain(), so if you use
Packit ae235b
 * mingw to develop the spawned program, it should call
Packit ae235b
 * g_win32_get_command_line() to get arguments in UTF-8.
Packit ae235b
 *
Packit ae235b
 * On Windows the low-level child process creation API CreateProcess()
Packit ae235b
 * doesn't use argument vectors, but a command line. The C runtime
Packit ae235b
 * library's spawn*() family of functions (which g_spawn_async_with_pipes()
Packit ae235b
 * eventually calls) paste the argument vector elements together into
Packit ae235b
 * a command line, and the C runtime startup code does a corresponding
Packit ae235b
 * reconstruction of an argument vector from the command line, to be
Packit ae235b
 * passed to main(). Complications arise when you have argument vector
Packit ae235b
 * elements that contain spaces of double quotes. The spawn*() functions
Packit ae235b
 * don't do any quoting or escaping, but on the other hand the startup
Packit ae235b
 * code does do unquoting and unescaping in order to enable receiving
Packit ae235b
 * arguments with embedded spaces or double quotes. To work around this
Packit ae235b
 * asymmetry, g_spawn_async_with_pipes() will do quoting and escaping on
Packit ae235b
 * argument vector elements that need it before calling the C runtime
Packit ae235b
 * spawn() function.
Packit ae235b
 *
Packit ae235b
 * The returned @child_pid on Windows is a handle to the child
Packit ae235b
 * process, not its identifier. Process handles and process
Packit ae235b
 * identifiers are different concepts on Windows.
Packit ae235b
 *
Packit ae235b
 * @envp is a %NULL-terminated array of strings, where each string
Packit ae235b
 * has the form `KEY=VALUE`. This will become the child's environment.
Packit ae235b
 * If @envp is %NULL, the child inherits its parent's environment.
Packit ae235b
 *
Packit ae235b
 * @flags should be the bitwise OR of any flags you want to affect the
Packit ae235b
 * function's behaviour. The %G_SPAWN_DO_NOT_REAP_CHILD means that the
Packit ae235b
 * child will not automatically be reaped; you must use a child watch
Packit ae235b
 * (g_child_watch_add()) to be notified about the death of the child process,
Packit ae235b
 * otherwise it will stay around as a zombie process until this process exits.
Packit ae235b
 * Eventually you must call g_spawn_close_pid() on the @child_pid, in order to
Packit ae235b
 * free resources which may be associated with the child process. (On Unix,
Packit ae235b
 * using a child watch is equivalent to calling waitpid() or handling
Packit ae235b
 * the %SIGCHLD signal manually. On Windows, calling g_spawn_close_pid()
Packit ae235b
 * is equivalent to calling CloseHandle() on the process handle returned
Packit ae235b
 * in @child_pid). See g_child_watch_add().
Packit ae235b
 *
Packit ae235b
 * %G_SPAWN_LEAVE_DESCRIPTORS_OPEN means that the parent's open file
Packit ae235b
 * descriptors will be inherited by the child; otherwise all descriptors
Packit ae235b
 * except stdin/stdout/stderr will be closed before calling exec() in
Packit ae235b
 * the child. %G_SPAWN_SEARCH_PATH means that @argv[0] need not be an
Packit ae235b
 * absolute path, it will be looked for in the `PATH` environment
Packit ae235b
 * variable. %G_SPAWN_SEARCH_PATH_FROM_ENVP means need not be an
Packit ae235b
 * absolute path, it will be looked for in the `PATH` variable from
Packit ae235b
 * @envp. If both %G_SPAWN_SEARCH_PATH and %G_SPAWN_SEARCH_PATH_FROM_ENVP
Packit ae235b
 * are used, the value from @envp takes precedence over the environment.
Packit ae235b
 * %G_SPAWN_STDOUT_TO_DEV_NULL means that the child's standard output
Packit ae235b
 * will be discarded, instead of going to the same location as the parent's 
Packit ae235b
 * standard output. If you use this flag, @standard_output must be %NULL.
Packit ae235b
 * %G_SPAWN_STDERR_TO_DEV_NULL means that the child's standard error
Packit ae235b
 * will be discarded, instead of going to the same location as the parent's
Packit ae235b
 * standard error. If you use this flag, @standard_error must be %NULL.
Packit ae235b
 * %G_SPAWN_CHILD_INHERITS_STDIN means that the child will inherit the parent's
Packit ae235b
 * standard input (by default, the child's standard input is attached to
Packit ae235b
 * `/dev/null`). If you use this flag, @standard_input must be %NULL.
Packit ae235b
 * %G_SPAWN_FILE_AND_ARGV_ZERO means that the first element of @argv is
Packit ae235b
 * the file to execute, while the remaining elements are the actual
Packit ae235b
 * argument vector to pass to the file. Normally g_spawn_async_with_pipes()
Packit ae235b
 * uses @argv[0] as the file to execute, and passes all of @argv to the child.
Packit ae235b
 *
Packit ae235b
 * @child_setup and @user_data are a function and user data. On POSIX
Packit ae235b
 * platforms, the function is called in the child after GLib has
Packit ae235b
 * performed all the setup it plans to perform (including creating
Packit ae235b
 * pipes, closing file descriptors, etc.) but before calling exec().
Packit ae235b
 * That is, @child_setup is called just before calling exec() in the
Packit ae235b
 * child. Obviously actions taken in this function will only affect
Packit ae235b
 * the child, not the parent.
Packit ae235b
 *
Packit ae235b
 * On Windows, there is no separate fork() and exec() functionality.
Packit ae235b
 * Child processes are created and run with a single API call,
Packit ae235b
 * CreateProcess(). There is no sensible thing @child_setup
Packit ae235b
 * could be used for on Windows so it is ignored and not called.
Packit ae235b
 *
Packit ae235b
 * If non-%NULL, @child_pid will on Unix be filled with the child's
Packit ae235b
 * process ID. You can use the process ID to send signals to the child,
Packit ae235b
 * or to use g_child_watch_add() (or waitpid()) if you specified the
Packit ae235b
 * %G_SPAWN_DO_NOT_REAP_CHILD flag. On Windows, @child_pid will be
Packit ae235b
 * filled with a handle to the child process only if you specified the
Packit ae235b
 * %G_SPAWN_DO_NOT_REAP_CHILD flag. You can then access the child
Packit ae235b
 * process using the Win32 API, for example wait for its termination
Packit ae235b
 * with the WaitFor*() functions, or examine its exit code with
Packit ae235b
 * GetExitCodeProcess(). You should close the handle with CloseHandle()
Packit ae235b
 * or g_spawn_close_pid() when you no longer need it.
Packit ae235b
 *
Packit ae235b
 * If non-%NULL, the @standard_input, @standard_output, @standard_error
Packit ae235b
 * locations will be filled with file descriptors for writing to the child's
Packit ae235b
 * standard input or reading from its standard output or standard error.
Packit ae235b
 * The caller of g_spawn_async_with_pipes() must close these file descriptors
Packit ae235b
 * when they are no longer in use. If these parameters are %NULL, the
Packit ae235b
 * corresponding pipe won't be created.
Packit ae235b
 *
Packit ae235b
 * If @standard_input is %NULL, the child's standard input is attached to
Packit ae235b
 * `/dev/null` unless %G_SPAWN_CHILD_INHERITS_STDIN is set.
Packit ae235b
 *
Packit ae235b
 * If @standard_error is NULL, the child's standard error goes to the same 
Packit ae235b
 * location as the parent's standard error unless %G_SPAWN_STDERR_TO_DEV_NULL 
Packit ae235b
 * is set.
Packit ae235b
 *
Packit ae235b
 * If @standard_output is NULL, the child's standard output goes to the same 
Packit ae235b
 * location as the parent's standard output unless %G_SPAWN_STDOUT_TO_DEV_NULL 
Packit ae235b
 * is set.
Packit ae235b
 *
Packit ae235b
 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
Packit ae235b
 * If an error is set, the function returns %FALSE. Errors are reported
Packit ae235b
 * even if they occur in the child (for example if the executable in
Packit ae235b
 * @argv[0] is not found). Typically the `message` field of returned
Packit ae235b
 * errors should be displayed to users. Possible errors are those from
Packit ae235b
 * the #G_SPAWN_ERROR domain.
Packit ae235b
 *
Packit ae235b
 * If an error occurs, @child_pid, @standard_input, @standard_output,
Packit ae235b
 * and @standard_error will not be filled with valid values.
Packit ae235b
 *
Packit ae235b
 * If @child_pid is not %NULL and an error does not occur then the returned
Packit ae235b
 * process reference must be closed using g_spawn_close_pid().
Packit ae235b
 *
Packit ae235b
 * If you are writing a GTK+ application, and the program you are spawning is a
Packit ae235b
 * graphical application too, then to ensure that the spawned program opens its
Packit ae235b
 * windows on the right screen, you may want to use #GdkAppLaunchContext,
Packit ae235b
 * #GAppLaunchContext, or set the %DISPLAY environment variable.
Packit ae235b
 * 
Packit ae235b
 * Returns: %TRUE on success, %FALSE if an error was set
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_spawn_async_with_pipes (const gchar          *working_directory,
Packit ae235b
                          gchar               **argv,
Packit ae235b
                          gchar               **envp,
Packit ae235b
                          GSpawnFlags           flags,
Packit ae235b
                          GSpawnChildSetupFunc  child_setup,
Packit ae235b
                          gpointer              user_data,
Packit ae235b
                          GPid                 *child_pid,
Packit ae235b
                          gint                 *standard_input,
Packit ae235b
                          gint                 *standard_output,
Packit ae235b
                          gint                 *standard_error,
Packit ae235b
                          GError              **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (argv != NULL, FALSE);
Packit ae235b
  g_return_val_if_fail (standard_output == NULL ||
Packit ae235b
                        !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
Packit ae235b
  g_return_val_if_fail (standard_error == NULL ||
Packit ae235b
                        !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
Packit ae235b
  /* can't inherit stdin if we have an input pipe. */
Packit ae235b
  g_return_val_if_fail (standard_input == NULL ||
Packit ae235b
                        !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
Packit ae235b
  
Packit ae235b
  return fork_exec_with_pipes (!(flags & G_SPAWN_DO_NOT_REAP_CHILD),
Packit ae235b
                               working_directory,
Packit ae235b
                               argv,
Packit ae235b
                               envp,
Packit ae235b
                               !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
Packit ae235b
                               (flags & G_SPAWN_SEARCH_PATH) != 0,
Packit ae235b
                               (flags & G_SPAWN_SEARCH_PATH_FROM_ENVP) != 0,
Packit ae235b
                               (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
Packit ae235b
                               (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
Packit ae235b
                               (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
Packit ae235b
                               (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
Packit ae235b
                               (flags & G_SPAWN_CLOEXEC_PIPES) != 0,
Packit ae235b
                               child_setup,
Packit ae235b
                               user_data,
Packit ae235b
                               child_pid,
Packit ae235b
                               standard_input,
Packit ae235b
                               standard_output,
Packit ae235b
                               standard_error,
Packit ae235b
                               error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_spawn_command_line_sync:
Packit ae235b
 * @command_line: (type filename): a command line
Packit ae235b
 * @standard_output: (out) (array zero-terminated=1) (element-type guint8) (optional): return location for child output
Packit ae235b
 * @standard_error: (out) (array zero-terminated=1) (element-type guint8) (optional): return location for child errors
Packit ae235b
 * @exit_status: (out) (optional): return location for child exit status, as returned by waitpid()
Packit ae235b
 * @error: return location for errors
Packit ae235b
 *
Packit ae235b
 * A simple version of g_spawn_sync() with little-used parameters
Packit ae235b
 * removed, taking a command line instead of an argument vector.  See
Packit ae235b
 * g_spawn_sync() for full details. @command_line will be parsed by
Packit ae235b
 * g_shell_parse_argv(). Unlike g_spawn_sync(), the %G_SPAWN_SEARCH_PATH flag
Packit ae235b
 * is enabled. Note that %G_SPAWN_SEARCH_PATH can have security
Packit ae235b
 * implications, so consider using g_spawn_sync() directly if
Packit ae235b
 * appropriate. Possible errors are those from g_spawn_sync() and those
Packit ae235b
 * from g_shell_parse_argv().
Packit ae235b
 *
Packit ae235b
 * If @exit_status is non-%NULL, the platform-specific exit status of
Packit ae235b
 * the child is stored there; see the documentation of
Packit ae235b
 * g_spawn_check_exit_status() for how to use and interpret this.
Packit ae235b
 * 
Packit ae235b
 * On Windows, please note the implications of g_shell_parse_argv()
Packit ae235b
 * parsing @command_line. Parsing is done according to Unix shell rules, not 
Packit ae235b
 * Windows command interpreter rules.
Packit ae235b
 * Space is a separator, and backslashes are
Packit ae235b
 * special. Thus you cannot simply pass a @command_line containing
Packit ae235b
 * canonical Windows paths, like "c:\\program files\\app\\app.exe", as
Packit ae235b
 * the backslashes will be eaten, and the space will act as a
Packit ae235b
 * separator. You need to enclose such paths with single quotes, like
Packit ae235b
 * "'c:\\program files\\app\\app.exe' 'e:\\folder\\argument.txt'".
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE if an error was set
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_spawn_command_line_sync (const gchar  *command_line,
Packit ae235b
                           gchar       **standard_output,
Packit ae235b
                           gchar       **standard_error,
Packit ae235b
                           gint         *exit_status,
Packit ae235b
                           GError      **error)
Packit ae235b
{
Packit ae235b
  gboolean retval;
Packit ae235b
  gchar **argv = NULL;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (command_line != NULL, FALSE);
Packit ae235b
  
Packit ae235b
  if (!g_shell_parse_argv (command_line,
Packit ae235b
                           NULL, &argv,
Packit ae235b
                           error))
Packit ae235b
    return FALSE;
Packit ae235b
  
Packit ae235b
  retval = g_spawn_sync (NULL,
Packit ae235b
                         argv,
Packit ae235b
                         NULL,
Packit ae235b
                         G_SPAWN_SEARCH_PATH,
Packit ae235b
                         NULL,
Packit ae235b
                         NULL,
Packit ae235b
                         standard_output,
Packit ae235b
                         standard_error,
Packit ae235b
                         exit_status,
Packit ae235b
                         error);
Packit ae235b
  g_strfreev (argv);
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_spawn_command_line_async:
Packit ae235b
 * @command_line: (type filename): a command line
Packit ae235b
 * @error: return location for errors
Packit ae235b
 * 
Packit ae235b
 * A simple version of g_spawn_async() that parses a command line with
Packit ae235b
 * g_shell_parse_argv() and passes it to g_spawn_async(). Runs a
Packit ae235b
 * command line in the background. Unlike g_spawn_async(), the
Packit ae235b
 * %G_SPAWN_SEARCH_PATH flag is enabled, other flags are not. Note
Packit ae235b
 * that %G_SPAWN_SEARCH_PATH can have security implications, so
Packit ae235b
 * consider using g_spawn_async() directly if appropriate. Possible
Packit ae235b
 * errors are those from g_shell_parse_argv() and g_spawn_async().
Packit ae235b
 * 
Packit ae235b
 * The same concerns on Windows apply as for g_spawn_command_line_sync().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE if error is set
Packit ae235b
 **/
Packit ae235b
gboolean
Packit ae235b
g_spawn_command_line_async (const gchar *command_line,
Packit ae235b
                            GError     **error)
Packit ae235b
{
Packit ae235b
  gboolean retval;
Packit ae235b
  gchar **argv = NULL;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (command_line != NULL, FALSE);
Packit ae235b
Packit ae235b
  if (!g_shell_parse_argv (command_line,
Packit ae235b
                           NULL, &argv,
Packit ae235b
                           error))
Packit ae235b
    return FALSE;
Packit ae235b
  
Packit ae235b
  retval = g_spawn_async (NULL,
Packit ae235b
                          argv,
Packit ae235b
                          NULL,
Packit ae235b
                          G_SPAWN_SEARCH_PATH,
Packit ae235b
                          NULL,
Packit ae235b
                          NULL,
Packit ae235b
                          NULL,
Packit ae235b
                          error);
Packit ae235b
  g_strfreev (argv);
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_spawn_check_exit_status:
Packit ae235b
 * @exit_status: An exit code as returned from g_spawn_sync()
Packit ae235b
 * @error: a #GError
Packit ae235b
 *
Packit ae235b
 * Set @error if @exit_status indicates the child exited abnormally
Packit ae235b
 * (e.g. with a nonzero exit code, or via a fatal signal).
Packit ae235b
 *
Packit ae235b
 * The g_spawn_sync() and g_child_watch_add() family of APIs return an
Packit ae235b
 * exit status for subprocesses encoded in a platform-specific way.
Packit ae235b
 * On Unix, this is guaranteed to be in the same format waitpid() returns,
Packit ae235b
 * and on Windows it is guaranteed to be the result of GetExitCodeProcess().
Packit ae235b
 *
Packit ae235b
 * Prior to the introduction of this function in GLib 2.34, interpreting
Packit ae235b
 * @exit_status required use of platform-specific APIs, which is problematic
Packit ae235b
 * for software using GLib as a cross-platform layer.
Packit ae235b
 *
Packit ae235b
 * Additionally, many programs simply want to determine whether or not
Packit ae235b
 * the child exited successfully, and either propagate a #GError or
Packit ae235b
 * print a message to standard error. In that common case, this function
Packit ae235b
 * can be used. Note that the error message in @error will contain
Packit ae235b
 * human-readable information about the exit status.
Packit ae235b
 *
Packit ae235b
 * The @domain and @code of @error have special semantics in the case
Packit ae235b
 * where the process has an "exit code", as opposed to being killed by
Packit ae235b
 * a signal. On Unix, this happens if WIFEXITED() would be true of
Packit ae235b
 * @exit_status. On Windows, it is always the case.
Packit ae235b
 *
Packit ae235b
 * The special semantics are that the actual exit code will be the
Packit ae235b
 * code set in @error, and the domain will be %G_SPAWN_EXIT_ERROR.
Packit ae235b
 * This allows you to differentiate between different exit codes.
Packit ae235b
 *
Packit ae235b
 * If the process was terminated by some means other than an exit
Packit ae235b
 * status, the domain will be %G_SPAWN_ERROR, and the code will be
Packit ae235b
 * %G_SPAWN_ERROR_FAILED.
Packit ae235b
 *
Packit ae235b
 * This function just offers convenience; you can of course also check
Packit ae235b
 * the available platform via a macro such as %G_OS_UNIX, and use
Packit ae235b
 * WIFEXITED() and WEXITSTATUS() on @exit_status directly. Do not attempt
Packit ae235b
 * to scan or parse the error message string; it may be translated and/or
Packit ae235b
 * change in future versions of GLib.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if child exited successfully, %FALSE otherwise (and
Packit ae235b
 *     @error will be set)
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_spawn_check_exit_status (gint      exit_status,
Packit ae235b
			   GError  **error)
Packit ae235b
{
Packit ae235b
  gboolean ret = FALSE;
Packit ae235b
Packit ae235b
  if (WIFEXITED (exit_status))
Packit ae235b
    {
Packit ae235b
      if (WEXITSTATUS (exit_status) != 0)
Packit ae235b
	{
Packit ae235b
	  g_set_error (error, G_SPAWN_EXIT_ERROR, WEXITSTATUS (exit_status),
Packit ae235b
		       _("Child process exited with code %ld"),
Packit ae235b
		       (long) WEXITSTATUS (exit_status));
Packit ae235b
	  goto out;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
  else if (WIFSIGNALED (exit_status))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
Packit ae235b
		   _("Child process killed by signal %ld"),
Packit ae235b
		   (long) WTERMSIG (exit_status));
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
  else if (WIFSTOPPED (exit_status))
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
Packit ae235b
		   _("Child process stopped by signal %ld"),
Packit ae235b
		   (long) WSTOPSIG (exit_status));
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
Packit ae235b
		   _("Child process exited abnormally"));
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  ret = TRUE;
Packit ae235b
 out:
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint
Packit ae235b
exec_err_to_g_error (gint en)
Packit ae235b
{
Packit ae235b
  switch (en)
Packit ae235b
    {
Packit ae235b
#ifdef EACCES
Packit ae235b
    case EACCES:
Packit ae235b
      return G_SPAWN_ERROR_ACCES;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EPERM
Packit ae235b
    case EPERM:
Packit ae235b
      return G_SPAWN_ERROR_PERM;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef E2BIG
Packit ae235b
    case E2BIG:
Packit ae235b
      return G_SPAWN_ERROR_TOO_BIG;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENOEXEC
Packit ae235b
    case ENOEXEC:
Packit ae235b
      return G_SPAWN_ERROR_NOEXEC;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENAMETOOLONG
Packit ae235b
    case ENAMETOOLONG:
Packit ae235b
      return G_SPAWN_ERROR_NAMETOOLONG;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENOENT
Packit ae235b
    case ENOENT:
Packit ae235b
      return G_SPAWN_ERROR_NOENT;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENOMEM
Packit ae235b
    case ENOMEM:
Packit ae235b
      return G_SPAWN_ERROR_NOMEM;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENOTDIR
Packit ae235b
    case ENOTDIR:
Packit ae235b
      return G_SPAWN_ERROR_NOTDIR;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ELOOP
Packit ae235b
    case ELOOP:
Packit ae235b
      return G_SPAWN_ERROR_LOOP;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
      
Packit ae235b
#ifdef ETXTBUSY
Packit ae235b
    case ETXTBUSY:
Packit ae235b
      return G_SPAWN_ERROR_TXTBUSY;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EIO
Packit ae235b
    case EIO:
Packit ae235b
      return G_SPAWN_ERROR_IO;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ENFILE
Packit ae235b
    case ENFILE:
Packit ae235b
      return G_SPAWN_ERROR_NFILE;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EMFILE
Packit ae235b
    case EMFILE:
Packit ae235b
      return G_SPAWN_ERROR_MFILE;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EINVAL
Packit ae235b
    case EINVAL:
Packit ae235b
      return G_SPAWN_ERROR_INVAL;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef EISDIR
Packit ae235b
    case EISDIR:
Packit ae235b
      return G_SPAWN_ERROR_ISDIR;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef ELIBBAD
Packit ae235b
    case ELIBBAD:
Packit ae235b
      return G_SPAWN_ERROR_LIBBAD;
Packit ae235b
      break;
Packit ae235b
#endif
Packit ae235b
      
Packit ae235b
    default:
Packit ae235b
      return G_SPAWN_ERROR_FAILED;
Packit ae235b
      break;
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gssize
Packit ae235b
write_all (gint fd, gconstpointer vbuf, gsize to_write)
Packit ae235b
{
Packit ae235b
  gchar *buf = (gchar *) vbuf;
Packit ae235b
  
Packit ae235b
  while (to_write > 0)
Packit ae235b
    {
Packit ae235b
      gssize count = write (fd, buf, to_write);
Packit ae235b
      if (count < 0)
Packit ae235b
        {
Packit ae235b
          if (errno != EINTR)
Packit ae235b
            return FALSE;
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          to_write -= count;
Packit ae235b
          buf += count;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
G_GNUC_NORETURN
Packit ae235b
static void
Packit ae235b
write_err_and_exit (gint fd, gint msg)
Packit ae235b
{
Packit ae235b
  gint en = errno;
Packit ae235b
  
Packit ae235b
  write_all (fd, &msg, sizeof(msg));
Packit ae235b
  write_all (fd, &en, sizeof(en));
Packit ae235b
  
Packit ae235b
  _exit (1);
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
set_cloexec (void *data, gint fd)
Packit ae235b
{
Packit ae235b
  if (fd >= GPOINTER_TO_INT (data))
Packit ae235b
    fcntl (fd, F_SETFD, FD_CLOEXEC);
Packit ae235b
Packit ae235b
  return 0;
Packit ae235b
}
Packit ae235b
Packit ae235b
#ifndef HAVE_FDWALK
Packit ae235b
static int
Packit ae235b
fdwalk (int (*cb)(void *data, int fd), void *data)
Packit ae235b
{
Packit ae235b
  gint open_max;
Packit ae235b
  gint fd;
Packit ae235b
  gint res = 0;
Packit ae235b
  
Packit ae235b
#ifdef HAVE_SYS_RESOURCE_H
Packit ae235b
  struct rlimit rl;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef __linux__  
Packit ae235b
  DIR *d;
Packit ae235b
Packit ae235b
  if ((d = opendir("/proc/self/fd"))) {
Packit ae235b
      struct dirent *de;
Packit ae235b
Packit ae235b
      while ((de = readdir(d))) {
Packit ae235b
          glong l;
Packit ae235b
          gchar *e = NULL;
Packit ae235b
Packit ae235b
          if (de->d_name[0] == '.')
Packit ae235b
              continue;
Packit ae235b
            
Packit ae235b
          errno = 0;
Packit ae235b
          l = strtol(de->d_name, &e, 10);
Packit ae235b
          if (errno != 0 || !e || *e)
Packit ae235b
              continue;
Packit ae235b
Packit ae235b
          fd = (gint) l;
Packit ae235b
Packit ae235b
          if ((glong) fd != l)
Packit ae235b
              continue;
Packit ae235b
Packit ae235b
          if (fd == dirfd(d))
Packit ae235b
              continue;
Packit ae235b
Packit ae235b
          if ((res = cb (data, fd)) != 0)
Packit ae235b
              break;
Packit ae235b
        }
Packit ae235b
      
Packit ae235b
      closedir(d);
Packit ae235b
      return res;
Packit ae235b
  }
Packit ae235b
Packit ae235b
  /* If /proc is not mounted or not accessible we fall back to the old
Packit ae235b
   * rlimit trick */
Packit ae235b
Packit ae235b
#endif
Packit ae235b
  
Packit ae235b
#ifdef HAVE_SYS_RESOURCE_H
Packit ae235b
      
Packit ae235b
  if (getrlimit(RLIMIT_NOFILE, &rl) == 0 && rl.rlim_max != RLIM_INFINITY)
Packit ae235b
      open_max = rl.rlim_max;
Packit ae235b
  else
Packit ae235b
#endif
Packit ae235b
      open_max = sysconf (_SC_OPEN_MAX);
Packit ae235b
Packit ae235b
  for (fd = 0; fd < open_max; fd++)
Packit ae235b
      if ((res = cb (data, fd)) != 0)
Packit ae235b
          break;
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}
Packit ae235b
#endif
Packit ae235b
Packit ae235b
static gint
Packit ae235b
sane_dup2 (gint fd1, gint fd2)
Packit ae235b
{
Packit ae235b
  gint ret;
Packit ae235b
Packit ae235b
 retry:
Packit ae235b
  ret = dup2 (fd1, fd2);
Packit ae235b
  if (ret < 0 && errno == EINTR)
Packit ae235b
    goto retry;
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint
Packit ae235b
sane_open (const char *path, gint mode)
Packit ae235b
{
Packit ae235b
  gint ret;
Packit ae235b
Packit ae235b
 retry:
Packit ae235b
  ret = open (path, mode);
Packit ae235b
  if (ret < 0 && errno == EINTR)
Packit ae235b
    goto retry;
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b
Packit ae235b
enum
Packit ae235b
{
Packit ae235b
  CHILD_CHDIR_FAILED,
Packit ae235b
  CHILD_EXEC_FAILED,
Packit ae235b
  CHILD_DUP2_FAILED,
Packit ae235b
  CHILD_FORK_FAILED
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
do_exec (gint                  child_err_report_fd,
Packit ae235b
         gint                  stdin_fd,
Packit ae235b
         gint                  stdout_fd,
Packit ae235b
         gint                  stderr_fd,
Packit ae235b
         const gchar          *working_directory,
Packit ae235b
         gchar               **argv,
Packit ae235b
         gchar               **envp,
Packit ae235b
         gboolean              close_descriptors,
Packit ae235b
         gboolean              search_path,
Packit ae235b
         gboolean              search_path_from_envp,
Packit ae235b
         gboolean              stdout_to_null,
Packit ae235b
         gboolean              stderr_to_null,
Packit ae235b
         gboolean              child_inherits_stdin,
Packit ae235b
         gboolean              file_and_argv_zero,
Packit ae235b
         GSpawnChildSetupFunc  child_setup,
Packit ae235b
         gpointer              user_data)
Packit ae235b
{
Packit ae235b
  if (working_directory && chdir (working_directory) < 0)
Packit ae235b
    write_err_and_exit (child_err_report_fd,
Packit ae235b
                        CHILD_CHDIR_FAILED);
Packit ae235b
Packit ae235b
  /* Close all file descriptors but stdin stdout and stderr as
Packit ae235b
   * soon as we exec. Note that this includes
Packit ae235b
   * child_err_report_fd, which keeps the parent from blocking
Packit ae235b
   * forever on the other end of that pipe.
Packit ae235b
   */
Packit ae235b
  if (close_descriptors)
Packit ae235b
    {
Packit ae235b
      fdwalk (set_cloexec, GINT_TO_POINTER(3));
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      /* We need to do child_err_report_fd anyway */
Packit ae235b
      set_cloexec (GINT_TO_POINTER(0), child_err_report_fd);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  /* Redirect pipes as required */
Packit ae235b
  
Packit ae235b
  if (stdin_fd >= 0)
Packit ae235b
    {
Packit ae235b
      /* dup2 can't actually fail here I don't think */
Packit ae235b
          
Packit ae235b
      if (sane_dup2 (stdin_fd, 0) < 0)
Packit ae235b
        write_err_and_exit (child_err_report_fd,
Packit ae235b
                            CHILD_DUP2_FAILED);
Packit ae235b
Packit ae235b
      /* ignore this if it doesn't work */
Packit ae235b
      close_and_invalidate (&stdin_fd);
Packit ae235b
    }
Packit ae235b
  else if (!child_inherits_stdin)
Packit ae235b
    {
Packit ae235b
      /* Keep process from blocking on a read of stdin */
Packit ae235b
      gint read_null = open ("/dev/null", O_RDONLY);
Packit ae235b
      g_assert (read_null != -1);
Packit ae235b
      sane_dup2 (read_null, 0);
Packit ae235b
      close_and_invalidate (&read_null);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (stdout_fd >= 0)
Packit ae235b
    {
Packit ae235b
      /* dup2 can't actually fail here I don't think */
Packit ae235b
          
Packit ae235b
      if (sane_dup2 (stdout_fd, 1) < 0)
Packit ae235b
        write_err_and_exit (child_err_report_fd,
Packit ae235b
                            CHILD_DUP2_FAILED);
Packit ae235b
Packit ae235b
      /* ignore this if it doesn't work */
Packit ae235b
      close_and_invalidate (&stdout_fd);
Packit ae235b
    }
Packit ae235b
  else if (stdout_to_null)
Packit ae235b
    {
Packit ae235b
      gint write_null = sane_open ("/dev/null", O_WRONLY);
Packit ae235b
      g_assert (write_null != -1);
Packit ae235b
      sane_dup2 (write_null, 1);
Packit ae235b
      close_and_invalidate (&write_null);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (stderr_fd >= 0)
Packit ae235b
    {
Packit ae235b
      /* dup2 can't actually fail here I don't think */
Packit ae235b
          
Packit ae235b
      if (sane_dup2 (stderr_fd, 2) < 0)
Packit ae235b
        write_err_and_exit (child_err_report_fd,
Packit ae235b
                            CHILD_DUP2_FAILED);
Packit ae235b
Packit ae235b
      /* ignore this if it doesn't work */
Packit ae235b
      close_and_invalidate (&stderr_fd);
Packit ae235b
    }
Packit ae235b
  else if (stderr_to_null)
Packit ae235b
    {
Packit ae235b
      gint write_null = sane_open ("/dev/null", O_WRONLY);
Packit ae235b
      sane_dup2 (write_null, 2);
Packit ae235b
      close_and_invalidate (&write_null);
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  /* Call user function just before we exec */
Packit ae235b
  if (child_setup)
Packit ae235b
    {
Packit ae235b
      (* child_setup) (user_data);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_execute (argv[0],
Packit ae235b
             file_and_argv_zero ? argv + 1 : argv,
Packit ae235b
             envp, search_path, search_path_from_envp);
Packit ae235b
Packit ae235b
  /* Exec failed */
Packit ae235b
  write_err_and_exit (child_err_report_fd,
Packit ae235b
                      CHILD_EXEC_FAILED);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
read_ints (int      fd,
Packit ae235b
           gint*    buf,
Packit ae235b
           gint     n_ints_in_buf,    
Packit ae235b
           gint    *n_ints_read,      
Packit ae235b
           GError **error)
Packit ae235b
{
Packit ae235b
  gsize bytes = 0;    
Packit ae235b
  
Packit ae235b
  while (TRUE)
Packit ae235b
    {
Packit ae235b
      gssize chunk;    
Packit ae235b
Packit ae235b
      if (bytes >= sizeof(gint)*2)
Packit ae235b
        break; /* give up, who knows what happened, should not be
Packit ae235b
                * possible.
Packit ae235b
                */
Packit ae235b
          
Packit ae235b
    again:
Packit ae235b
      chunk = read (fd,
Packit ae235b
                    ((gchar*)buf) + bytes,
Packit ae235b
                    sizeof(gint) * n_ints_in_buf - bytes);
Packit ae235b
      if (chunk < 0 && errno == EINTR)
Packit ae235b
        goto again;
Packit ae235b
          
Packit ae235b
      if (chunk < 0)
Packit ae235b
        {
Packit ae235b
          int errsv = errno;
Packit ae235b
Packit ae235b
          /* Some weird shit happened, bail out */
Packit ae235b
          g_set_error (error,
Packit ae235b
                       G_SPAWN_ERROR,
Packit ae235b
                       G_SPAWN_ERROR_FAILED,
Packit ae235b
                       _("Failed to read from child pipe (%s)"),
Packit ae235b
                       g_strerror (errsv));
Packit ae235b
Packit ae235b
          return FALSE;
Packit ae235b
        }
Packit ae235b
      else if (chunk == 0)
Packit ae235b
        break; /* EOF */
Packit ae235b
      else /* chunk > 0 */
Packit ae235b
	bytes += chunk;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  *n_ints_read = (gint)(bytes / sizeof(gint));
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
fork_exec_with_pipes (gboolean              intermediate_child,
Packit ae235b
                      const gchar          *working_directory,
Packit ae235b
                      gchar               **argv,
Packit ae235b
                      gchar               **envp,
Packit ae235b
                      gboolean              close_descriptors,
Packit ae235b
                      gboolean              search_path,
Packit ae235b
                      gboolean              search_path_from_envp,
Packit ae235b
                      gboolean              stdout_to_null,
Packit ae235b
                      gboolean              stderr_to_null,
Packit ae235b
                      gboolean              child_inherits_stdin,
Packit ae235b
                      gboolean              file_and_argv_zero,
Packit ae235b
                      gboolean              cloexec_pipes,
Packit ae235b
                      GSpawnChildSetupFunc  child_setup,
Packit ae235b
                      gpointer              user_data,
Packit ae235b
                      GPid                 *child_pid,
Packit ae235b
                      gint                 *standard_input,
Packit ae235b
                      gint                 *standard_output,
Packit ae235b
                      gint                 *standard_error,
Packit ae235b
                      GError              **error)     
Packit ae235b
{
Packit ae235b
  GPid pid = -1;
Packit ae235b
  gint stdin_pipe[2] = { -1, -1 };
Packit ae235b
  gint stdout_pipe[2] = { -1, -1 };
Packit ae235b
  gint stderr_pipe[2] = { -1, -1 };
Packit ae235b
  gint child_err_report_pipe[2] = { -1, -1 };
Packit ae235b
  gint child_pid_report_pipe[2] = { -1, -1 };
Packit ae235b
  guint pipe_flags = cloexec_pipes ? FD_CLOEXEC : 0;
Packit ae235b
  gint status;
Packit ae235b
  
Packit ae235b
  if (!g_unix_open_pipe (child_err_report_pipe, pipe_flags, error))
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (intermediate_child && !g_unix_open_pipe (child_pid_report_pipe, pipe_flags, error))
Packit ae235b
    goto cleanup_and_fail;
Packit ae235b
  
Packit ae235b
  if (standard_input && !g_unix_open_pipe (stdin_pipe, pipe_flags, error))
Packit ae235b
    goto cleanup_and_fail;
Packit ae235b
  
Packit ae235b
  if (standard_output && !g_unix_open_pipe (stdout_pipe, pipe_flags, error))
Packit ae235b
    goto cleanup_and_fail;
Packit ae235b
Packit ae235b
  if (standard_error && !g_unix_open_pipe (stderr_pipe, FD_CLOEXEC, error))
Packit ae235b
    goto cleanup_and_fail;
Packit ae235b
Packit ae235b
  pid = fork ();
Packit ae235b
Packit ae235b
  if (pid < 0)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_SPAWN_ERROR,
Packit ae235b
                   G_SPAWN_ERROR_FORK,
Packit ae235b
                   _("Failed to fork (%s)"),
Packit ae235b
                   g_strerror (errsv));
Packit ae235b
Packit ae235b
      goto cleanup_and_fail;
Packit ae235b
    }
Packit ae235b
  else if (pid == 0)
Packit ae235b
    {
Packit ae235b
      /* Immediate child. This may or may not be the child that
Packit ae235b
       * actually execs the new process.
Packit ae235b
       */
Packit ae235b
Packit ae235b
      /* Reset some signal handlers that we may use */
Packit ae235b
      signal (SIGCHLD, SIG_DFL);
Packit ae235b
      signal (SIGINT, SIG_DFL);
Packit ae235b
      signal (SIGTERM, SIG_DFL);
Packit ae235b
      signal (SIGHUP, SIG_DFL);
Packit ae235b
      
Packit ae235b
      /* Be sure we crash if the parent exits
Packit ae235b
       * and we write to the err_report_pipe
Packit ae235b
       */
Packit ae235b
      signal (SIGPIPE, SIG_DFL);
Packit ae235b
Packit ae235b
      /* Close the parent's end of the pipes;
Packit ae235b
       * not needed in the close_descriptors case,
Packit ae235b
       * though
Packit ae235b
       */
Packit ae235b
      close_and_invalidate (&child_err_report_pipe[0]);
Packit ae235b
      close_and_invalidate (&child_pid_report_pipe[0]);
Packit ae235b
      close_and_invalidate (&stdin_pipe[1]);
Packit ae235b
      close_and_invalidate (&stdout_pipe[0]);
Packit ae235b
      close_and_invalidate (&stderr_pipe[0]);
Packit ae235b
      
Packit ae235b
      if (intermediate_child)
Packit ae235b
        {
Packit ae235b
          /* We need to fork an intermediate child that launches the
Packit ae235b
           * final child. The purpose of the intermediate child
Packit ae235b
           * is to exit, so we can waitpid() it immediately.
Packit ae235b
           * Then the grandchild will not become a zombie.
Packit ae235b
           */
Packit ae235b
          GPid grandchild_pid;
Packit ae235b
Packit ae235b
          grandchild_pid = fork ();
Packit ae235b
Packit ae235b
          if (grandchild_pid < 0)
Packit ae235b
            {
Packit ae235b
              /* report -1 as child PID */
Packit ae235b
              write_all (child_pid_report_pipe[1], &grandchild_pid,
Packit ae235b
                         sizeof(grandchild_pid));
Packit ae235b
              
Packit ae235b
              write_err_and_exit (child_err_report_pipe[1],
Packit ae235b
                                  CHILD_FORK_FAILED);              
Packit ae235b
            }
Packit ae235b
          else if (grandchild_pid == 0)
Packit ae235b
            {
Packit ae235b
              close_and_invalidate (&child_pid_report_pipe[1]);
Packit ae235b
              do_exec (child_err_report_pipe[1],
Packit ae235b
                       stdin_pipe[0],
Packit ae235b
                       stdout_pipe[1],
Packit ae235b
                       stderr_pipe[1],
Packit ae235b
                       working_directory,
Packit ae235b
                       argv,
Packit ae235b
                       envp,
Packit ae235b
                       close_descriptors,
Packit ae235b
                       search_path,
Packit ae235b
                       search_path_from_envp,
Packit ae235b
                       stdout_to_null,
Packit ae235b
                       stderr_to_null,
Packit ae235b
                       child_inherits_stdin,
Packit ae235b
                       file_and_argv_zero,
Packit ae235b
                       child_setup,
Packit ae235b
                       user_data);
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              write_all (child_pid_report_pipe[1], &grandchild_pid, sizeof(grandchild_pid));
Packit ae235b
              close_and_invalidate (&child_pid_report_pipe[1]);
Packit ae235b
              
Packit ae235b
              _exit (0);
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          /* Just run the child.
Packit ae235b
           */
Packit ae235b
Packit ae235b
          do_exec (child_err_report_pipe[1],
Packit ae235b
                   stdin_pipe[0],
Packit ae235b
                   stdout_pipe[1],
Packit ae235b
                   stderr_pipe[1],
Packit ae235b
                   working_directory,
Packit ae235b
                   argv,
Packit ae235b
                   envp,
Packit ae235b
                   close_descriptors,
Packit ae235b
                   search_path,
Packit ae235b
                   search_path_from_envp,
Packit ae235b
                   stdout_to_null,
Packit ae235b
                   stderr_to_null,
Packit ae235b
                   child_inherits_stdin,
Packit ae235b
                   file_and_argv_zero,
Packit ae235b
                   child_setup,
Packit ae235b
                   user_data);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      /* Parent */
Packit ae235b
      
Packit ae235b
      gint buf[2];
Packit ae235b
      gint n_ints = 0;    
Packit ae235b
Packit ae235b
      /* Close the uncared-about ends of the pipes */
Packit ae235b
      close_and_invalidate (&child_err_report_pipe[1]);
Packit ae235b
      close_and_invalidate (&child_pid_report_pipe[1]);
Packit ae235b
      close_and_invalidate (&stdin_pipe[0]);
Packit ae235b
      close_and_invalidate (&stdout_pipe[1]);
Packit ae235b
      close_and_invalidate (&stderr_pipe[1]);
Packit ae235b
Packit ae235b
      /* If we had an intermediate child, reap it */
Packit ae235b
      if (intermediate_child)
Packit ae235b
        {
Packit ae235b
        wait_again:
Packit ae235b
          if (waitpid (pid, &status, 0) < 0)
Packit ae235b
            {
Packit ae235b
              if (errno == EINTR)
Packit ae235b
                goto wait_again;
Packit ae235b
              else if (errno == ECHILD)
Packit ae235b
                ; /* do nothing, child already reaped */
Packit ae235b
              else
Packit ae235b
                g_warning ("waitpid() should not fail in "
Packit ae235b
			   "'fork_exec_with_pipes'");
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      
Packit ae235b
Packit ae235b
      if (!read_ints (child_err_report_pipe[0],
Packit ae235b
                      buf, 2, &n_ints,
Packit ae235b
                      error))
Packit ae235b
        goto cleanup_and_fail;
Packit ae235b
        
Packit ae235b
      if (n_ints >= 2)
Packit ae235b
        {
Packit ae235b
          /* Error from the child. */
Packit ae235b
Packit ae235b
          switch (buf[0])
Packit ae235b
            {
Packit ae235b
            case CHILD_CHDIR_FAILED:
Packit ae235b
              g_set_error (error,
Packit ae235b
                           G_SPAWN_ERROR,
Packit ae235b
                           G_SPAWN_ERROR_CHDIR,
Packit ae235b
                           _("Failed to change to directory “%s” (%s)"),
Packit ae235b
                           working_directory,
Packit ae235b
                           g_strerror (buf[1]));
Packit ae235b
Packit ae235b
              break;
Packit ae235b
              
Packit ae235b
            case CHILD_EXEC_FAILED:
Packit ae235b
              g_set_error (error,
Packit ae235b
                           G_SPAWN_ERROR,
Packit ae235b
                           exec_err_to_g_error (buf[1]),
Packit ae235b
                           _("Failed to execute child process “%s” (%s)"),
Packit ae235b
                           argv[0],
Packit ae235b
                           g_strerror (buf[1]));
Packit ae235b
Packit ae235b
              break;
Packit ae235b
              
Packit ae235b
            case CHILD_DUP2_FAILED:
Packit ae235b
              g_set_error (error,
Packit ae235b
                           G_SPAWN_ERROR,
Packit ae235b
                           G_SPAWN_ERROR_FAILED,
Packit ae235b
                           _("Failed to redirect output or input of child process (%s)"),
Packit ae235b
                           g_strerror (buf[1]));
Packit ae235b
Packit ae235b
              break;
Packit ae235b
Packit ae235b
            case CHILD_FORK_FAILED:
Packit ae235b
              g_set_error (error,
Packit ae235b
                           G_SPAWN_ERROR,
Packit ae235b
                           G_SPAWN_ERROR_FORK,
Packit ae235b
                           _("Failed to fork child process (%s)"),
Packit ae235b
                           g_strerror (buf[1]));
Packit ae235b
              break;
Packit ae235b
              
Packit ae235b
            default:
Packit ae235b
              g_set_error (error,
Packit ae235b
                           G_SPAWN_ERROR,
Packit ae235b
                           G_SPAWN_ERROR_FAILED,
Packit ae235b
                           _("Unknown error executing child process “%s”"),
Packit ae235b
                           argv[0]);
Packit ae235b
              break;
Packit ae235b
            }
Packit ae235b
Packit ae235b
          goto cleanup_and_fail;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      /* Get child pid from intermediate child pipe. */
Packit ae235b
      if (intermediate_child)
Packit ae235b
        {
Packit ae235b
          n_ints = 0;
Packit ae235b
          
Packit ae235b
          if (!read_ints (child_pid_report_pipe[0],
Packit ae235b
                          buf, 1, &n_ints, error))
Packit ae235b
            goto cleanup_and_fail;
Packit ae235b
Packit ae235b
          if (n_ints < 1)
Packit ae235b
            {
Packit ae235b
              int errsv = errno;
Packit ae235b
Packit ae235b
              g_set_error (error,
Packit ae235b
                           G_SPAWN_ERROR,
Packit ae235b
                           G_SPAWN_ERROR_FAILED,
Packit ae235b
                           _("Failed to read enough data from child pid pipe (%s)"),
Packit ae235b
                           g_strerror (errsv));
Packit ae235b
              goto cleanup_and_fail;
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              /* we have the child pid */
Packit ae235b
              pid = buf[0];
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      
Packit ae235b
      /* Success against all odds! return the information */
Packit ae235b
      close_and_invalidate (&child_err_report_pipe[0]);
Packit ae235b
      close_and_invalidate (&child_pid_report_pipe[0]);
Packit ae235b
 
Packit ae235b
      if (child_pid)
Packit ae235b
        *child_pid = pid;
Packit ae235b
Packit ae235b
      if (standard_input)
Packit ae235b
        *standard_input = stdin_pipe[1];
Packit ae235b
      if (standard_output)
Packit ae235b
        *standard_output = stdout_pipe[0];
Packit ae235b
      if (standard_error)
Packit ae235b
        *standard_error = stderr_pipe[0];
Packit ae235b
      
Packit ae235b
      return TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
 cleanup_and_fail:
Packit ae235b
Packit ae235b
  /* There was an error from the Child, reap the child to avoid it being
Packit ae235b
     a zombie.
Packit ae235b
   */
Packit ae235b
Packit ae235b
  if (pid > 0)
Packit ae235b
  {
Packit ae235b
    wait_failed:
Packit ae235b
     if (waitpid (pid, NULL, 0) < 0)
Packit ae235b
       {
Packit ae235b
          if (errno == EINTR)
Packit ae235b
            goto wait_failed;
Packit ae235b
          else if (errno == ECHILD)
Packit ae235b
            ; /* do nothing, child already reaped */
Packit ae235b
          else
Packit ae235b
            g_warning ("waitpid() should not fail in "
Packit ae235b
                       "'fork_exec_with_pipes'");
Packit ae235b
       }
Packit ae235b
   }
Packit ae235b
Packit ae235b
  close_and_invalidate (&child_err_report_pipe[0]);
Packit ae235b
  close_and_invalidate (&child_err_report_pipe[1]);
Packit ae235b
  close_and_invalidate (&child_pid_report_pipe[0]);
Packit ae235b
  close_and_invalidate (&child_pid_report_pipe[1]);
Packit ae235b
  close_and_invalidate (&stdin_pipe[0]);
Packit ae235b
  close_and_invalidate (&stdin_pipe[1]);
Packit ae235b
  close_and_invalidate (&stdout_pipe[0]);
Packit ae235b
  close_and_invalidate (&stdout_pipe[1]);
Packit ae235b
  close_and_invalidate (&stderr_pipe[0]);
Packit ae235b
  close_and_invalidate (&stderr_pipe[1]);
Packit ae235b
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* Based on execvp from GNU C Library */
Packit ae235b
Packit ae235b
static void
Packit ae235b
script_execute (const gchar *file,
Packit ae235b
                gchar      **argv,
Packit ae235b
                gchar      **envp)
Packit ae235b
{
Packit ae235b
  /* Count the arguments.  */
Packit ae235b
  int argc = 0;
Packit ae235b
  while (argv[argc])
Packit ae235b
    ++argc;
Packit ae235b
  
Packit ae235b
  /* Construct an argument list for the shell.  */
Packit ae235b
  {
Packit ae235b
    gchar **new_argv;
Packit ae235b
Packit ae235b
    new_argv = g_new0 (gchar*, argc + 2); /* /bin/sh and NULL */
Packit ae235b
    
Packit ae235b
    new_argv[0] = (char *) "/bin/sh";
Packit ae235b
    new_argv[1] = (char *) file;
Packit ae235b
    while (argc > 0)
Packit ae235b
      {
Packit ae235b
	new_argv[argc + 1] = argv[argc];
Packit ae235b
	--argc;
Packit ae235b
      }
Packit ae235b
Packit ae235b
    /* Execute the shell. */
Packit ae235b
    if (envp)
Packit ae235b
      execve (new_argv[0], new_argv, envp);
Packit ae235b
    else
Packit ae235b
      execv (new_argv[0], new_argv);
Packit ae235b
    
Packit ae235b
    g_free (new_argv);
Packit ae235b
  }
Packit ae235b
}
Packit ae235b
Packit ae235b
static gchar*
Packit ae235b
my_strchrnul (const gchar *str, gchar c)
Packit ae235b
{
Packit ae235b
  gchar *p = (gchar*) str;
Packit ae235b
  while (*p && (*p != c))
Packit ae235b
    ++p;
Packit ae235b
Packit ae235b
  return p;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gint
Packit ae235b
g_execute (const gchar *file,
Packit ae235b
           gchar      **argv,
Packit ae235b
           gchar      **envp,
Packit ae235b
           gboolean     search_path,
Packit ae235b
           gboolean     search_path_from_envp)
Packit ae235b
{
Packit ae235b
  if (*file == '\0')
Packit ae235b
    {
Packit ae235b
      /* We check the simple case first. */
Packit ae235b
      errno = ENOENT;
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (!(search_path || search_path_from_envp) || strchr (file, '/') != NULL)
Packit ae235b
    {
Packit ae235b
      /* Don't search when it contains a slash. */
Packit ae235b
      if (envp)
Packit ae235b
        execve (file, argv, envp);
Packit ae235b
      else
Packit ae235b
        execv (file, argv);
Packit ae235b
      
Packit ae235b
      if (errno == ENOEXEC)
Packit ae235b
	script_execute (file, argv, envp);
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      gboolean got_eacces = 0;
Packit ae235b
      const gchar *path, *p;
Packit ae235b
      gchar *name, *freeme;
Packit ae235b
      gsize len;
Packit ae235b
      gsize pathlen;
Packit ae235b
Packit ae235b
      path = NULL;
Packit ae235b
      if (search_path_from_envp)
Packit ae235b
        path = g_environ_getenv (envp, "PATH");
Packit ae235b
      if (search_path && path == NULL)
Packit ae235b
        path = g_getenv ("PATH");
Packit ae235b
Packit ae235b
      if (path == NULL)
Packit ae235b
	{
Packit ae235b
	  /* There is no 'PATH' in the environment.  The default
Packit ae235b
	   * search path in libc is the current directory followed by
Packit ae235b
	   * the path 'confstr' returns for '_CS_PATH'.
Packit ae235b
           */
Packit ae235b
Packit ae235b
          /* In GLib we put . last, for security, and don't use the
Packit ae235b
           * unportable confstr(); UNIX98 does not actually specify
Packit ae235b
           * what to search if PATH is unset. POSIX may, dunno.
Packit ae235b
           */
Packit ae235b
          
Packit ae235b
          path = "/bin:/usr/bin:.";
Packit ae235b
	}
Packit ae235b
Packit ae235b
      len = strlen (file) + 1;
Packit ae235b
      pathlen = strlen (path);
Packit ae235b
      freeme = name = g_malloc (pathlen + len + 1);
Packit ae235b
      
Packit ae235b
      /* Copy the file name at the top, including '\0'  */
Packit ae235b
      memcpy (name + pathlen + 1, file, len);
Packit ae235b
      name = name + pathlen;
Packit ae235b
      /* And add the slash before the filename  */
Packit ae235b
      *name = '/';
Packit ae235b
Packit ae235b
      p = path;
Packit ae235b
      do
Packit ae235b
	{
Packit ae235b
	  char *startp;
Packit ae235b
Packit ae235b
	  path = p;
Packit ae235b
	  p = my_strchrnul (path, ':');
Packit ae235b
Packit ae235b
	  if (p == path)
Packit ae235b
	    /* Two adjacent colons, or a colon at the beginning or the end
Packit ae235b
             * of 'PATH' means to search the current directory.
Packit ae235b
             */
Packit ae235b
	    startp = name + 1;
Packit ae235b
	  else
Packit ae235b
	    startp = memcpy (name - (p - path), path, p - path);
Packit ae235b
Packit ae235b
	  /* Try to execute this name.  If it works, execv will not return.  */
Packit ae235b
          if (envp)
Packit ae235b
            execve (startp, argv, envp);
Packit ae235b
          else
Packit ae235b
            execv (startp, argv);
Packit ae235b
          
Packit ae235b
	  if (errno == ENOEXEC)
Packit ae235b
	    script_execute (startp, argv, envp);
Packit ae235b
Packit ae235b
	  switch (errno)
Packit ae235b
	    {
Packit ae235b
	    case EACCES:
Packit ae235b
	      /* Record the we got a 'Permission denied' error.  If we end
Packit ae235b
               * up finding no executable we can use, we want to diagnose
Packit ae235b
               * that we did find one but were denied access.
Packit ae235b
               */
Packit ae235b
	      got_eacces = TRUE;
Packit ae235b
Packit ae235b
              /* FALL THRU */
Packit ae235b
              
Packit ae235b
	    case ENOENT:
Packit ae235b
#ifdef ESTALE
Packit ae235b
	    case ESTALE:
Packit ae235b
#endif
Packit ae235b
#ifdef ENOTDIR
Packit ae235b
	    case ENOTDIR:
Packit ae235b
#endif
Packit ae235b
	      /* Those errors indicate the file is missing or not executable
Packit ae235b
               * by us, in which case we want to just try the next path
Packit ae235b
               * directory.
Packit ae235b
               */
Packit ae235b
	      break;
Packit ae235b
Packit ae235b
	    case ENODEV:
Packit ae235b
	    case ETIMEDOUT:
Packit ae235b
	      /* Some strange filesystems like AFS return even
Packit ae235b
	       * stranger error numbers.  They cannot reasonably mean anything
Packit ae235b
	       * else so ignore those, too.
Packit ae235b
	       */
Packit ae235b
	      break;
Packit ae235b
Packit ae235b
	    default:
Packit ae235b
	      /* Some other error means we found an executable file, but
Packit ae235b
               * something went wrong executing it; return the error to our
Packit ae235b
               * caller.
Packit ae235b
               */
Packit ae235b
              g_free (freeme);
Packit ae235b
	      return -1;
Packit ae235b
	    }
Packit ae235b
	}
Packit ae235b
      while (*p++ != '\0');
Packit ae235b
Packit ae235b
      /* We tried every element and none of them worked.  */
Packit ae235b
      if (got_eacces)
Packit ae235b
	/* At least one failure was due to permissions, so report that
Packit ae235b
         * error.
Packit ae235b
         */
Packit ae235b
        errno = EACCES;
Packit ae235b
Packit ae235b
      g_free (freeme);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* Return the error from the last attempt (probably ENOENT).  */
Packit ae235b
  return -1;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_spawn_close_pid:
Packit ae235b
 * @pid: The process reference to close
Packit ae235b
 *
Packit ae235b
 * On some platforms, notably Windows, the #GPid type represents a resource
Packit ae235b
 * which must be closed to prevent resource leaking. g_spawn_close_pid()
Packit ae235b
 * is provided for this purpose. It should be used on all platforms, even
Packit ae235b
 * though it doesn't do anything under UNIX.
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_spawn_close_pid (GPid pid)
Packit ae235b
{
Packit ae235b
}