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