1/* gspawn.h - Process launching
2 *
3 * Copyright 2000 Red Hat, Inc.
4 *
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.
9 *
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.
14 *
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.
19 */
20
21#ifndef __G_SPAWN_H__
22#define __G_SPAWN_H__
23
24#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
25#error "Only <glib.h> can be included directly."
26#endif
27
28#include <glib/gerror.h>
29
30G_BEGIN_DECLS
31
32
33/* I'm not sure I remember our proposed naming convention here. */
34/**
35 * G_SPAWN_ERROR:
36 *
37 * Error domain for spawning processes. Errors in this domain will
38 * be from the #GSpawnError enumeration. See #GError for information on
39 * error domains.
40 */
41#define G_SPAWN_ERROR g_spawn_error_quark ()
42
43/**
44 * GSpawnError:
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 `EACCES`
49 * @G_SPAWN_ERROR_PERM: execv() returned `EPERM`
50 * @G_SPAWN_ERROR_TOO_BIG: execv() returned `E2BIG`
51 * @G_SPAWN_ERROR_2BIG: deprecated alias for %G_SPAWN_ERROR_TOO_BIG
52 * @G_SPAWN_ERROR_NOEXEC: execv() returned `ENOEXEC`
53 * @G_SPAWN_ERROR_NAMETOOLONG: execv() returned `ENAMETOOLONG`
54 * @G_SPAWN_ERROR_NOENT: execv() returned `ENOENT`
55 * @G_SPAWN_ERROR_NOMEM: execv() returned `ENOMEM`
56 * @G_SPAWN_ERROR_NOTDIR: execv() returned `ENOTDIR`
57 * @G_SPAWN_ERROR_LOOP: execv() returned `ELOOP`
58 * @G_SPAWN_ERROR_TXTBUSY: execv() returned `ETXTBUSY`
59 * @G_SPAWN_ERROR_IO: execv() returned `EIO`
60 * @G_SPAWN_ERROR_NFILE: execv() returned `ENFILE`
61 * @G_SPAWN_ERROR_MFILE: execv() returned `EMFILE`
62 * @G_SPAWN_ERROR_INVAL: execv() returned `EINVAL`
63 * @G_SPAWN_ERROR_ISDIR: execv() returned `EISDIR`
64 * @G_SPAWN_ERROR_LIBBAD: execv() returned `ELIBBAD`
65 * @G_SPAWN_ERROR_FAILED: Some other fatal failure,
66 * `error->message` should explain.
67 *
68 * Error codes returned by spawning processes.
69 */
70typedef enum
71{
72 G_SPAWN_ERROR_FORK, /* fork failed due to lack of memory */
73 G_SPAWN_ERROR_READ, /* read or select on pipes failed */
74 G_SPAWN_ERROR_CHDIR, /* changing to working dir failed */
75 G_SPAWN_ERROR_ACCES, /* execv() returned EACCES */
76 G_SPAWN_ERROR_PERM, /* execv() returned EPERM */
77 G_SPAWN_ERROR_TOO_BIG,/* execv() returned E2BIG */
78#ifndef G_DISABLE_DEPRECATED
79 G_SPAWN_ERROR_2BIG = G_SPAWN_ERROR_TOO_BIG,
80#endif
81 G_SPAWN_ERROR_NOEXEC, /* execv() returned ENOEXEC */
82 G_SPAWN_ERROR_NAMETOOLONG, /* "" "" ENAMETOOLONG */
83 G_SPAWN_ERROR_NOENT, /* "" "" ENOENT */
84 G_SPAWN_ERROR_NOMEM, /* "" "" ENOMEM */
85 G_SPAWN_ERROR_NOTDIR, /* "" "" ENOTDIR */
86 G_SPAWN_ERROR_LOOP, /* "" "" ELOOP */
87 G_SPAWN_ERROR_TXTBUSY, /* "" "" ETXTBUSY */
88 G_SPAWN_ERROR_IO, /* "" "" EIO */
89 G_SPAWN_ERROR_NFILE, /* "" "" ENFILE */
90 G_SPAWN_ERROR_MFILE, /* "" "" EMFLE */
91 G_SPAWN_ERROR_INVAL, /* "" "" EINVAL */
92 G_SPAWN_ERROR_ISDIR, /* "" "" EISDIR */
93 G_SPAWN_ERROR_LIBBAD, /* "" "" ELIBBAD */
94 G_SPAWN_ERROR_FAILED /* other fatal failure, error->message
95 * should explain
96 */
97} GSpawnError;
98
99/**
100 * G_SPAWN_EXIT_ERROR:
101 *
102 * Error domain used by g_spawn_check_exit_status(). The code
103 * will be the program exit code.
104 */
105#define G_SPAWN_EXIT_ERROR g_spawn_exit_error_quark ()
106
107/**
108 * GSpawnChildSetupFunc:
109 * @user_data: user data to pass to the function.
110 *
111 * Specifies the type of the setup function passed to g_spawn_async(),
112 * g_spawn_sync() and g_spawn_async_with_pipes(), which can, in very
113 * limited ways, be used to affect the child's execution.
114 *
115 * On POSIX platforms, the function is called in the child after GLib
116 * has performed all the setup it plans to perform, but before calling
117 * exec(). Actions taken in this function will only affect the child,
118 * not the parent.
119 *
120 * On Windows, the function is called in the parent. Its usefulness on
121 * Windows is thus questionable. In many cases executing the child setup
122 * function in the parent can have ill effects, and you should be very
123 * careful when porting software to Windows that uses child setup
124 * functions.
125 *
126 * However, even on POSIX, you are extremely limited in what you can
127 * safely do from a #GSpawnChildSetupFunc, because any mutexes that were
128 * held by other threads in the parent process at the time of the fork()
129 * will still be locked in the child process, and they will never be
130 * unlocked (since the threads that held them don't exist in the child).
131 * POSIX allows only async-signal-safe functions (see signal(7)) to be
132 * called in the child between fork() and exec(), which drastically limits
133 * the usefulness of child setup functions.
134 *
135 * In particular, it is not safe to call any function which may
136 * call malloc(), which includes POSIX functions such as setenv().
137 * If you need to set up the child environment differently from
138 * the parent, you should use g_get_environ(), g_environ_setenv(),
139 * and g_environ_unsetenv(), and then pass the complete environment
140 * list to the `g_spawn...` function.
141 */
142typedef void (* GSpawnChildSetupFunc) (gpointer user_data);
143
144/**
145 * GSpawnFlags:
146 * @G_SPAWN_DEFAULT: no flags, default behaviour
147 * @G_SPAWN_LEAVE_DESCRIPTORS_OPEN: the parent's open file descriptors will
148 * be inherited by the child; otherwise all descriptors except stdin,
149 * stdout and stderr will be closed before calling exec() in the child.
150 * @G_SPAWN_DO_NOT_REAP_CHILD: the child will not be automatically reaped;
151 * you must use g_child_watch_add() yourself (or call waitpid() or handle
152 * `SIGCHLD` yourself), or the child will become a zombie.
153 * @G_SPAWN_SEARCH_PATH: `argv[0]` need not be an absolute path, it will be
154 * looked for in the user's `PATH`.
155 * @G_SPAWN_STDOUT_TO_DEV_NULL: the child's standard output will be discarded,
156 * instead of going to the same location as the parent's standard output.
157 * @G_SPAWN_STDERR_TO_DEV_NULL: the child's standard error will be discarded.
158 * @G_SPAWN_CHILD_INHERITS_STDIN: the child will inherit the parent's standard
159 * input (by default, the child's standard input is attached to `/dev/null`).
160 * @G_SPAWN_FILE_AND_ARGV_ZERO: the first element of `argv` is the file to
161 * execute, while the remaining elements are the actual argument vector
162 * to pass to the file. Normally g_spawn_async_with_pipes() uses `argv[0]`
163 * as the file to execute, and passes all of `argv` to the child.
164 * @G_SPAWN_SEARCH_PATH_FROM_ENVP: if `argv[0]` is not an abolute path,
165 * it will be looked for in the `PATH` from the passed child environment.
166 * Since: 2.34
167 * @G_SPAWN_CLOEXEC_PIPES: create all pipes with the `O_CLOEXEC` flag set.
168 * Since: 2.40
169 *
170 * Flags passed to g_spawn_sync(), g_spawn_async() and g_spawn_async_with_pipes().
171 */
172typedef enum
173{
174 G_SPAWN_DEFAULT = 0,
175 G_SPAWN_LEAVE_DESCRIPTORS_OPEN = 1 << 0,
176 G_SPAWN_DO_NOT_REAP_CHILD = 1 << 1,
177 /* look for argv[0] in the path i.e. use execvp() */
178 G_SPAWN_SEARCH_PATH = 1 << 2,
179 /* Dump output to /dev/null */
180 G_SPAWN_STDOUT_TO_DEV_NULL = 1 << 3,
181 G_SPAWN_STDERR_TO_DEV_NULL = 1 << 4,
182 G_SPAWN_CHILD_INHERITS_STDIN = 1 << 5,
183 G_SPAWN_FILE_AND_ARGV_ZERO = 1 << 6,
184 G_SPAWN_SEARCH_PATH_FROM_ENVP = 1 << 7,
185 G_SPAWN_CLOEXEC_PIPES = 1 << 8
186} GSpawnFlags;
187
188GLIB_AVAILABLE_IN_ALL
189GQuark g_spawn_error_quark (void);
190GLIB_AVAILABLE_IN_ALL
191GQuark g_spawn_exit_error_quark (void);
192
193GLIB_AVAILABLE_IN_ALL
194gboolean g_spawn_async (const gchar *working_directory,
195 gchar **argv,
196 gchar **envp,
197 GSpawnFlags flags,
198 GSpawnChildSetupFunc child_setup,
199 gpointer user_data,
200 GPid *child_pid,
201 GError **error);
202
203
204/* Opens pipes for non-NULL standard_output, standard_input, standard_error,
205 * and returns the parent's end of the pipes.
206 */
207GLIB_AVAILABLE_IN_ALL
208gboolean g_spawn_async_with_pipes (const gchar *working_directory,
209 gchar **argv,
210 gchar **envp,
211 GSpawnFlags flags,
212 GSpawnChildSetupFunc child_setup,
213 gpointer user_data,
214 GPid *child_pid,
215 gint *standard_input,
216 gint *standard_output,
217 gint *standard_error,
218 GError **error);
219
220
221/* If standard_output or standard_error are non-NULL, the full
222 * standard output or error of the command will be placed there.
223 */
224
225GLIB_AVAILABLE_IN_ALL
226gboolean g_spawn_sync (const gchar *working_directory,
227 gchar **argv,
228 gchar **envp,
229 GSpawnFlags flags,
230 GSpawnChildSetupFunc child_setup,
231 gpointer user_data,
232 gchar **standard_output,
233 gchar **standard_error,
234 gint *exit_status,
235 GError **error);
236
237GLIB_AVAILABLE_IN_ALL
238gboolean g_spawn_command_line_sync (const gchar *command_line,
239 gchar **standard_output,
240 gchar **standard_error,
241 gint *exit_status,
242 GError **error);
243GLIB_AVAILABLE_IN_ALL
244gboolean g_spawn_command_line_async (const gchar *command_line,
245 GError **error);
246
247GLIB_AVAILABLE_IN_2_34
248gboolean g_spawn_check_exit_status (gint exit_status,
249 GError **error);
250
251GLIB_AVAILABLE_IN_ALL
252void g_spawn_close_pid (GPid pid);
253
254#ifndef __GTK_DOC_IGNORE__
255#ifdef G_OS_WIN32
256#define g_spawn_async g_spawn_async_utf8
257#define g_spawn_async_with_pipes g_spawn_async_with_pipes_utf8
258#define g_spawn_sync g_spawn_sync_utf8
259#define g_spawn_command_line_sync g_spawn_command_line_sync_utf8
260#define g_spawn_command_line_async g_spawn_command_line_async_utf8
261
262GLIB_AVAILABLE_IN_ALL
263gboolean g_spawn_async_utf8 (const gchar *working_directory,
264 gchar **argv,
265 gchar **envp,
266 GSpawnFlags flags,
267 GSpawnChildSetupFunc child_setup,
268 gpointer user_data,
269 GPid *child_pid,
270 GError **error);
271GLIB_AVAILABLE_IN_ALL
272gboolean g_spawn_async_with_pipes_utf8 (const gchar *working_directory,
273 gchar **argv,
274 gchar **envp,
275 GSpawnFlags flags,
276 GSpawnChildSetupFunc child_setup,
277 gpointer user_data,
278 GPid *child_pid,
279 gint *standard_input,
280 gint *standard_output,
281 gint *standard_error,
282 GError **error);
283GLIB_AVAILABLE_IN_ALL
284gboolean g_spawn_sync_utf8 (const gchar *working_directory,
285 gchar **argv,
286 gchar **envp,
287 GSpawnFlags flags,
288 GSpawnChildSetupFunc child_setup,
289 gpointer user_data,
290 gchar **standard_output,
291 gchar **standard_error,
292 gint *exit_status,
293 GError **error);
294
295GLIB_AVAILABLE_IN_ALL
296gboolean g_spawn_command_line_sync_utf8 (const gchar *command_line,
297 gchar **standard_output,
298 gchar **standard_error,
299 gint *exit_status,
300 GError **error);
301GLIB_AVAILABLE_IN_ALL
302gboolean g_spawn_command_line_async_utf8 (const gchar *command_line,
303 GError **error);
304#endif
305#endif
306
307G_END_DECLS
308
309#endif /* __G_SPAWN_H__ */
310