1 /* gspawn.h - Process launching
3 * Copyright 2000 Red Hat, Inc.
5 * GLib is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * GLib is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with GLib; see the file COPYING.LIB. If not, write
17 * to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
22 #error "Only <glib.h> can be included directly."
28 #include <glib/gerror.h>
33 /* I'm not sure I remember our proposed naming convention here. */
37 * Error domain for spawning processes. Errors in this domain will
38 * be from the #GSpawnError enumeration. See #GError for information on
41 #define G_SPAWN_ERROR g_spawn_error_quark ()
45 * @G_SPAWN_ERROR_FORK: Fork failed due to lack of memory.
46 * @G_SPAWN_ERROR_READ: Read or select on pipes failed.
47 * @G_SPAWN_ERROR_CHDIR: Changing to working directory failed.
48 * @G_SPAWN_ERROR_ACCES: execv() returned <literal>EACCES</literal>
49 * @G_SPAWN_ERROR_PERM: execv() returned <literal>EPERM</literal>
50 * @G_SPAWN_ERROR_2BIG: execv() returned <literal>E2BIG</literal>
51 * @G_SPAWN_ERROR_NOEXEC: execv() returned <literal>ENOEXEC</literal>
52 * @G_SPAWN_ERROR_NAMETOOLONG: execv() returned <literal>ENAMETOOLONG</literal>
53 * @G_SPAWN_ERROR_NOENT: execv() returned <literal>ENOENT</literal>
54 * @G_SPAWN_ERROR_NOMEM: execv() returned <literal>ENOMEM</literal>
55 * @G_SPAWN_ERROR_NOTDIR: execv() returned <literal>ENOTDIR</literal>
56 * @G_SPAWN_ERROR_LOOP: execv() returned <literal>ELOOP</literal>
57 * @G_SPAWN_ERROR_TXTBUSY: execv() returned <literal>ETXTBUSY</literal>
58 * @G_SPAWN_ERROR_IO: execv() returned <literal>EIO</literal>
59 * @G_SPAWN_ERROR_NFILE: execv() returned <literal>ENFILE</literal>
60 * @G_SPAWN_ERROR_MFILE: execv() returned <literal>EMFILE</literal>
61 * @G_SPAWN_ERROR_INVAL: execv() returned <literal>EINVAL</literal>
62 * @G_SPAWN_ERROR_ISDIR: execv() returned <literal>EISDIR</literal>
63 * @G_SPAWN_ERROR_LIBBAD: execv() returned <literal>ELIBBAD</literal>
64 * @G_SPAWN_ERROR_FAILED: Some other fatal failure,
65 * <literal>error->message</literal> should explain.
67 * Error codes returned by spawning processes.
71 G_SPAWN_ERROR_FORK
, /* fork failed due to lack of memory */
72 G_SPAWN_ERROR_READ
, /* read or select on pipes failed */
73 G_SPAWN_ERROR_CHDIR
, /* changing to working dir failed */
74 G_SPAWN_ERROR_ACCES
, /* execv() returned EACCES */
75 G_SPAWN_ERROR_PERM
, /* execv() returned EPERM */
76 G_SPAWN_ERROR_2BIG
, /* execv() returned E2BIG */
77 G_SPAWN_ERROR_NOEXEC
, /* execv() returned ENOEXEC */
78 G_SPAWN_ERROR_NAMETOOLONG
, /* "" "" ENAMETOOLONG */
79 G_SPAWN_ERROR_NOENT
, /* "" "" ENOENT */
80 G_SPAWN_ERROR_NOMEM
, /* "" "" ENOMEM */
81 G_SPAWN_ERROR_NOTDIR
, /* "" "" ENOTDIR */
82 G_SPAWN_ERROR_LOOP
, /* "" "" ELOOP */
83 G_SPAWN_ERROR_TXTBUSY
, /* "" "" ETXTBUSY */
84 G_SPAWN_ERROR_IO
, /* "" "" EIO */
85 G_SPAWN_ERROR_NFILE
, /* "" "" ENFILE */
86 G_SPAWN_ERROR_MFILE
, /* "" "" EMFLE */
87 G_SPAWN_ERROR_INVAL
, /* "" "" EINVAL */
88 G_SPAWN_ERROR_ISDIR
, /* "" "" EISDIR */
89 G_SPAWN_ERROR_LIBBAD
, /* "" "" ELIBBAD */
90 G_SPAWN_ERROR_FAILED
/* other fatal failure, error->message
96 * GSpawnChildSetupFunc:
97 * @user_data: user data to pass to the function.
99 * Specifies the type of the setup function passed to g_spawn_async(),
100 * g_spawn_sync() and g_spawn_async_with_pipes(), which can, in very
101 * limited ways, be used to affect the child's execution.
103 * On POSIX platforms, the function is called in the child after GLib
104 * has performed all the setup it plans to perform, but before calling
105 * exec(). Actions taken in this function will only affect the child,
108 * On Windows, the function is called in the parent. Its usefulness on
109 * Windows is thus questionable. In many cases executing the child setup
110 * function in the parent can have ill effects, and you should be very
111 * careful when porting software to Windows that uses child setup
114 * However, even on POSIX, you are extremely limited in what you can
115 * safely do from a #GSpawnChildSetupFunc, because any mutexes that
116 * were held by other threads in the parent process at the time of the
117 * fork() will still be locked in the child process, and they will
118 * never be unlocked (since the threads that held them don't exist in
119 * the child). POSIX allows only async-signal-safe functions (see
120 * <citerefentry><refentrytitle>signal</refentrytitle><manvolnum>7</manvolnum></citerefentry>)
121 * to be called in the child between fork() and exec(), which
122 * drastically limits the usefulness of child setup functions.
124 * In particular, it is not safe to call any function which may
125 * call malloc(), which includes POSIX functions such as setenv().
126 * If you need to set up the child environment differently from
127 * the parent, you should use g_get_environ(), g_environ_setenv(),
128 * and g_environ_unsetenv(), and then pass the complete environment
129 * list to the <literal>g_spawn...</literal> function.
131 typedef void (* GSpawnChildSetupFunc
) (gpointer user_data
);
135 * @G_SPAWN_LEAVE_DESCRIPTORS_OPEN: the parent's open file descriptors will be
136 * inherited by the child; otherwise all descriptors except stdin/stdout/stderr
137 * will be closed before calling exec() in the child.
138 * @G_SPAWN_DO_NOT_REAP_CHILD: the child will not be automatically reaped; you
139 * must use g_child_watch_add() yourself (or call waitpid()
140 * or handle <literal>SIGCHLD</literal> yourself), or the child will become a zombie.
141 * @G_SPAWN_SEARCH_PATH: <literal>argv[0]</literal> need not be an absolute path,
142 * it will be looked for in the user's <envar>PATH</envar>.
143 * @G_SPAWN_STDOUT_TO_DEV_NULL: the child's standard output will be discarded,
144 * instead of going to the same location as the parent's standard output.
145 * @G_SPAWN_STDERR_TO_DEV_NULL: the child's standard error will be discarded.
146 * @G_SPAWN_CHILD_INHERITS_STDIN: the child will inherit the parent's standard
147 * input (by default, the child's standard input is attached to
148 * <filename>/dev/null</filename>).
149 * @G_SPAWN_FILE_AND_ARGV_ZERO: the first element of <literal>argv</literal> is
150 * the file to execute, while the remaining elements are the actual argument
151 * vector to pass to the file. Normally g_spawn_async_with_pipes() uses
152 * <literal>argv[0]</literal> as the file to execute, and passes all of
153 * <literal>argv</literal> to the child.
155 * Flags passed to g_spawn_sync(), g_spawn_async() and g_spawn_async_with_pipes().
159 G_SPAWN_LEAVE_DESCRIPTORS_OPEN
= 1 << 0,
160 G_SPAWN_DO_NOT_REAP_CHILD
= 1 << 1,
161 /* look for argv[0] in the path i.e. use execvp() */
162 G_SPAWN_SEARCH_PATH
= 1 << 2,
163 /* Dump output to /dev/null */
164 G_SPAWN_STDOUT_TO_DEV_NULL
= 1 << 3,
165 G_SPAWN_STDERR_TO_DEV_NULL
= 1 << 4,
166 G_SPAWN_CHILD_INHERITS_STDIN
= 1 << 5,
167 G_SPAWN_FILE_AND_ARGV_ZERO
= 1 << 6
170 GQuark
g_spawn_error_quark (void);
172 #ifndef __GTK_DOC_IGNORE__
174 #define g_spawn_async g_spawn_async_utf8
175 #define g_spawn_async_with_pipes g_spawn_async_with_pipes_utf8
176 #define g_spawn_sync g_spawn_sync_utf8
177 #define g_spawn_command_line_sync g_spawn_command_line_sync_utf8
178 #define g_spawn_command_line_async g_spawn_command_line_async_utf8
182 gboolean
g_spawn_async (const gchar
*working_directory
,
186 GSpawnChildSetupFunc child_setup
,
192 /* Opens pipes for non-NULL standard_output, standard_input, standard_error,
193 * and returns the parent's end of the pipes.
195 gboolean
g_spawn_async_with_pipes (const gchar
*working_directory
,
199 GSpawnChildSetupFunc child_setup
,
202 gint
*standard_input
,
203 gint
*standard_output
,
204 gint
*standard_error
,
208 /* If standard_output or standard_error are non-NULL, the full
209 * standard output or error of the command will be placed there.
212 gboolean
g_spawn_sync (const gchar
*working_directory
,
216 GSpawnChildSetupFunc child_setup
,
218 gchar
**standard_output
,
219 gchar
**standard_error
,
223 gboolean
g_spawn_command_line_sync (const gchar
*command_line
,
224 gchar
**standard_output
,
225 gchar
**standard_error
,
228 gboolean
g_spawn_command_line_async (const gchar
*command_line
,
231 void g_spawn_close_pid (GPid pid
);
235 #endif /* __G_SPAWN_H__ */