Blame glib/glib/gspawn.h

Packit db3073
/* gspawn.h - Process launching
Packit db3073
 *
Packit db3073
 *  Copyright 2000 Red Hat, Inc.
Packit db3073
 *
Packit db3073
 * GLib is free software; you can redistribute it and/or
Packit db3073
 * modify it under the terms of the GNU Lesser General Public License as
Packit db3073
 * published by the Free Software Foundation; either version 2 of the
Packit db3073
 * License, or (at your option) any later version.
Packit db3073
 *
Packit db3073
 * GLib is distributed in the hope that it will be useful,
Packit db3073
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit db3073
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit db3073
 * Lesser General Public License for more details.
Packit db3073
 *
Packit db3073
 * You should have received a copy of the GNU Lesser General Public
Packit db3073
 * License along with GLib; see the file COPYING.LIB.  If not, write
Packit db3073
 * to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit db3073
 * Boston, MA 02111-1307, USA.
Packit db3073
 */
Packit db3073
Packit db3073
#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
Packit db3073
#error "Only <glib.h> can be included directly."
Packit db3073
#endif
Packit db3073
Packit db3073
#ifndef __G_SPAWN_H__
Packit db3073
#define __G_SPAWN_H__
Packit db3073
Packit db3073
#include <glib/gerror.h>
Packit db3073
Packit db3073
G_BEGIN_DECLS
Packit db3073
Packit db3073
Packit db3073
/* I'm not sure I remember our proposed naming convention here. */
Packit db3073
/**
Packit db3073
 * G_SPAWN_ERROR:
Packit db3073
 *
Packit db3073
 * Error domain for spawning processes. Errors in this domain will
Packit db3073
 * be from the #GSpawnError enumeration. See #GError for information on
Packit db3073
 * error domains.
Packit db3073
 */
Packit db3073
#define G_SPAWN_ERROR g_spawn_error_quark ()
Packit db3073
Packit db3073
/**
Packit db3073
 * GSpawnError:
Packit db3073
 * @G_SPAWN_ERROR_FORK: Fork failed due to lack of memory.
Packit db3073
 * @G_SPAWN_ERROR_READ: Read or select on pipes failed.
Packit db3073
 * @G_SPAWN_ERROR_CHDIR: Changing to working directory failed.
Packit db3073
 * @G_SPAWN_ERROR_ACCES: execv() returned <literal>EACCES</literal>
Packit db3073
 * @G_SPAWN_ERROR_PERM: execv() returned <literal>EPERM</literal>
Packit db3073
 * @G_SPAWN_ERROR_TOO_BIG: execv() returned <literal>E2BIG</literal>
Packit db3073
 * @G_SPAWN_ERROR_2BIG: deprecated alias for %G_SPAWN_ERROR_TOO_BIG
Packit db3073
 * @G_SPAWN_ERROR_NOEXEC: execv() returned <literal>ENOEXEC</literal>
Packit db3073
 * @G_SPAWN_ERROR_NAMETOOLONG: execv() returned <literal>ENAMETOOLONG</literal>
Packit db3073
 * @G_SPAWN_ERROR_NOENT: execv() returned <literal>ENOENT</literal>
Packit db3073
 * @G_SPAWN_ERROR_NOMEM: execv() returned <literal>ENOMEM</literal>
Packit db3073
 * @G_SPAWN_ERROR_NOTDIR: execv() returned <literal>ENOTDIR</literal>
Packit db3073
 * @G_SPAWN_ERROR_LOOP: execv() returned <literal>ELOOP</literal>
Packit db3073
 * @G_SPAWN_ERROR_TXTBUSY: execv() returned <literal>ETXTBUSY</literal>
Packit db3073
 * @G_SPAWN_ERROR_IO: execv() returned <literal>EIO</literal>
Packit db3073
 * @G_SPAWN_ERROR_NFILE: execv() returned <literal>ENFILE</literal>
Packit db3073
 * @G_SPAWN_ERROR_MFILE: execv() returned <literal>EMFILE</literal>
Packit db3073
 * @G_SPAWN_ERROR_INVAL: execv() returned <literal>EINVAL</literal>
Packit db3073
 * @G_SPAWN_ERROR_ISDIR: execv() returned <literal>EISDIR</literal>
Packit db3073
 * @G_SPAWN_ERROR_LIBBAD: execv() returned <literal>ELIBBAD</literal>
Packit db3073
 * @G_SPAWN_ERROR_FAILED: Some other fatal failure,
Packit db3073
 *   <literal>error->message</literal> should explain.
Packit db3073
 *
Packit db3073
 * Error codes returned by spawning processes.
Packit db3073
 */
Packit db3073
typedef enum
Packit db3073
{
Packit db3073
  G_SPAWN_ERROR_FORK,   /* fork failed due to lack of memory */
Packit db3073
  G_SPAWN_ERROR_READ,   /* read or select on pipes failed */
Packit db3073
  G_SPAWN_ERROR_CHDIR,  /* changing to working dir failed */
Packit db3073
  G_SPAWN_ERROR_ACCES,  /* execv() returned EACCES */
Packit db3073
  G_SPAWN_ERROR_PERM,   /* execv() returned EPERM */
Packit db3073
  G_SPAWN_ERROR_TOO_BIG,/* execv() returned E2BIG */
Packit db3073
#ifndef G_DISABLE_DEPRECATED
Packit db3073
  G_SPAWN_ERROR_2BIG = G_SPAWN_ERROR_TOO_BIG,
Packit db3073
#endif
Packit db3073
  G_SPAWN_ERROR_NOEXEC, /* execv() returned ENOEXEC */
Packit db3073
  G_SPAWN_ERROR_NAMETOOLONG, /* ""  "" ENAMETOOLONG */
Packit db3073
  G_SPAWN_ERROR_NOENT,       /* ""  "" ENOENT */
Packit db3073
  G_SPAWN_ERROR_NOMEM,       /* ""  "" ENOMEM */
Packit db3073
  G_SPAWN_ERROR_NOTDIR,      /* ""  "" ENOTDIR */
Packit db3073
  G_SPAWN_ERROR_LOOP,        /* ""  "" ELOOP   */
Packit db3073
  G_SPAWN_ERROR_TXTBUSY,     /* ""  "" ETXTBUSY */
Packit db3073
  G_SPAWN_ERROR_IO,          /* ""  "" EIO */
Packit db3073
  G_SPAWN_ERROR_NFILE,       /* ""  "" ENFILE */
Packit db3073
  G_SPAWN_ERROR_MFILE,       /* ""  "" EMFLE */
Packit db3073
  G_SPAWN_ERROR_INVAL,       /* ""  "" EINVAL */
Packit db3073
  G_SPAWN_ERROR_ISDIR,       /* ""  "" EISDIR */
Packit db3073
  G_SPAWN_ERROR_LIBBAD,      /* ""  "" ELIBBAD */
Packit db3073
  G_SPAWN_ERROR_FAILED       /* other fatal failure, error->message
Packit db3073
                              * should explain
Packit db3073
                              */
Packit db3073
} GSpawnError;
Packit db3073
Packit db3073
/**
Packit db3073
 * GSpawnChildSetupFunc:
Packit db3073
 * @user_data: user data to pass to the function.
Packit db3073
 *
Packit db3073
 * Specifies the type of the setup function passed to g_spawn_async(),
Packit db3073
 * g_spawn_sync() and g_spawn_async_with_pipes(), which can, in very
Packit db3073
 * limited ways, be used to affect the child's execution.
Packit db3073
 *
Packit db3073
 * On POSIX platforms, the function is called in the child after GLib
Packit db3073
 * has performed all the setup it plans to perform, but before calling
Packit db3073
 * exec(). Actions taken in this function will only affect the child,
Packit db3073
 * not the parent.
Packit db3073
 *
Packit db3073
 * On Windows, the function is called in the parent. Its usefulness on
Packit db3073
 * Windows is thus questionable. In many cases executing the child setup
Packit db3073
 * function in the parent can have ill effects, and you should be very
Packit db3073
 * careful when porting software to Windows that uses child setup
Packit db3073
 * functions.
Packit db3073
 *
Packit db3073
 * However, even on POSIX, you are extremely limited in what you can
Packit db3073
 * safely do from a #GSpawnChildSetupFunc, because any mutexes that
Packit db3073
 * were held by other threads in the parent process at the time of the
Packit db3073
 * fork() will still be locked in the child process, and they will
Packit db3073
 * never be unlocked (since the threads that held them don't exist in
Packit db3073
 * the child). POSIX allows only async-signal-safe functions (see
Packit db3073
 * <citerefentry><refentrytitle>signal</refentrytitle><manvolnum>7</manvolnum></citerefentry>)
Packit db3073
 * to be called in the child between fork() and exec(), which
Packit db3073
 * drastically limits the usefulness of child setup functions.
Packit db3073
 *
Packit db3073
 * In particular, it is not safe to call any function which may
Packit db3073
 * call malloc(), which includes POSIX functions such as setenv().
Packit db3073
 * If you need to set up the child environment differently from
Packit db3073
 * the parent, you should use g_get_environ(), g_environ_setenv(),
Packit db3073
 * and g_environ_unsetenv(), and then pass the complete environment
Packit db3073
 * list to the <literal>g_spawn...</literal> function.
Packit db3073
 */
Packit db3073
typedef void (* GSpawnChildSetupFunc) (gpointer user_data);
Packit db3073
Packit db3073
/**
Packit db3073
 * GSpawnFlags:
Packit db3073
 * @G_SPAWN_LEAVE_DESCRIPTORS_OPEN: the parent's open file descriptors will be
Packit db3073
 *   inherited by the child; otherwise all descriptors except stdin/stdout/stderr
Packit db3073
 *   will be closed before calling exec() in the child.
Packit db3073
 * @G_SPAWN_DO_NOT_REAP_CHILD: the child will not be automatically reaped; you
Packit db3073
 *   must use g_child_watch_add() yourself (or call waitpid()
Packit db3073
 *   or handle <literal>SIGCHLD</literal> yourself), or the child will become a zombie.
Packit db3073
 * @G_SPAWN_SEARCH_PATH: <literal>argv[0]</literal> need not be an absolute path,
Packit db3073
 *   it will be looked for in the user's <envar>PATH</envar>.
Packit db3073
 * @G_SPAWN_STDOUT_TO_DEV_NULL: the child's standard output will be discarded,
Packit db3073
 *   instead of going to the same location as the parent's standard output.
Packit db3073
 * @G_SPAWN_STDERR_TO_DEV_NULL: the child's standard error will be discarded.
Packit db3073
 * @G_SPAWN_CHILD_INHERITS_STDIN: the child will inherit the parent's standard
Packit db3073
 *   input (by default, the child's standard input is attached to
Packit db3073
 *   <filename>/dev/null</filename>).
Packit db3073
 * @G_SPAWN_FILE_AND_ARGV_ZERO: the first element of <literal>argv</literal> is
Packit db3073
 *   the file to execute, while the remaining elements are the actual argument
Packit db3073
 *   vector to pass to the file. Normally g_spawn_async_with_pipes() uses
Packit db3073
 *   <literal>argv[0]</literal> as the file to execute, and passes all of
Packit db3073
 *   <literal>argv</literal> to the child.
Packit db3073
 *
Packit db3073
 * Flags passed to g_spawn_sync(), g_spawn_async() and g_spawn_async_with_pipes().
Packit db3073
 */
Packit db3073
typedef enum
Packit db3073
{
Packit db3073
  G_SPAWN_LEAVE_DESCRIPTORS_OPEN = 1 << 0,
Packit db3073
  G_SPAWN_DO_NOT_REAP_CHILD      = 1 << 1,
Packit db3073
  /* look for argv[0] in the path i.e. use execvp() */
Packit db3073
  G_SPAWN_SEARCH_PATH            = 1 << 2,
Packit db3073
  /* Dump output to /dev/null */
Packit db3073
  G_SPAWN_STDOUT_TO_DEV_NULL     = 1 << 3,
Packit db3073
  G_SPAWN_STDERR_TO_DEV_NULL     = 1 << 4,
Packit db3073
  G_SPAWN_CHILD_INHERITS_STDIN   = 1 << 5,
Packit db3073
  G_SPAWN_FILE_AND_ARGV_ZERO     = 1 << 6
Packit db3073
} GSpawnFlags;
Packit db3073
Packit db3073
GQuark g_spawn_error_quark (void);
Packit db3073
Packit db3073
#ifndef __GTK_DOC_IGNORE__
Packit db3073
#ifdef G_OS_WIN32
Packit db3073
#define g_spawn_async g_spawn_async_utf8
Packit db3073
#define g_spawn_async_with_pipes g_spawn_async_with_pipes_utf8
Packit db3073
#define g_spawn_sync g_spawn_sync_utf8
Packit db3073
#define g_spawn_command_line_sync g_spawn_command_line_sync_utf8
Packit db3073
#define g_spawn_command_line_async g_spawn_command_line_async_utf8
Packit db3073
#endif
Packit db3073
#endif
Packit db3073
Packit db3073
gboolean g_spawn_async (const gchar           *working_directory,
Packit db3073
                        gchar                **argv,
Packit db3073
                        gchar                **envp,
Packit db3073
                        GSpawnFlags            flags,
Packit db3073
                        GSpawnChildSetupFunc   child_setup,
Packit db3073
                        gpointer               user_data,
Packit db3073
                        GPid                  *child_pid,
Packit db3073
                        GError               **error);
Packit db3073
Packit db3073
Packit db3073
/* Opens pipes for non-NULL standard_output, standard_input, standard_error,
Packit db3073
 * and returns the parent's end of the pipes.
Packit db3073
 */
Packit db3073
gboolean g_spawn_async_with_pipes (const gchar          *working_directory,
Packit db3073
                                   gchar               **argv,
Packit db3073
                                   gchar               **envp,
Packit db3073
                                   GSpawnFlags           flags,
Packit db3073
                                   GSpawnChildSetupFunc  child_setup,
Packit db3073
                                   gpointer              user_data,
Packit db3073
                                   GPid                 *child_pid,
Packit db3073
                                   gint                 *standard_input,
Packit db3073
                                   gint                 *standard_output,
Packit db3073
                                   gint                 *standard_error,
Packit db3073
                                   GError              **error);
Packit db3073
Packit db3073
Packit db3073
/* If standard_output or standard_error are non-NULL, the full
Packit db3073
 * standard output or error of the command will be placed there.
Packit db3073
 */
Packit db3073
Packit db3073
gboolean g_spawn_sync         (const gchar          *working_directory,
Packit db3073
                               gchar               **argv,
Packit db3073
                               gchar               **envp,
Packit db3073
                               GSpawnFlags           flags,
Packit db3073
                               GSpawnChildSetupFunc  child_setup,
Packit db3073
                               gpointer              user_data,
Packit db3073
                               gchar               **standard_output,
Packit db3073
                               gchar               **standard_error,
Packit db3073
                               gint                 *exit_status,
Packit db3073
                               GError              **error);
Packit db3073
Packit db3073
gboolean g_spawn_command_line_sync  (const gchar          *command_line,
Packit db3073
                                     gchar               **standard_output,
Packit db3073
                                     gchar               **standard_error,
Packit db3073
                                     gint                 *exit_status,
Packit db3073
                                     GError              **error);
Packit db3073
gboolean g_spawn_command_line_async (const gchar          *command_line,
Packit db3073
                                     GError              **error);
Packit db3073
Packit db3073
void g_spawn_close_pid (GPid pid);
Packit db3073
Packit db3073
G_END_DECLS
Packit db3073
Packit db3073
#endif /* __G_SPAWN_H__ */