Merge branch 'non-atomicity-of-g_file_set_contents' into 'master'
[glib.git] / glib / gspawn.h
blob055743ea205f0266f639ab4def0a5406d91db6f8
1 /* gspawn.h - Process launching
3 * Copyright 2000 Red Hat, Inc.
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.
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.
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/>.
19 #ifndef __G_SPAWN_H__
20 #define __G_SPAWN_H__
22 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
23 #error "Only <glib.h> can be included directly."
24 #endif
26 #include <glib/gerror.h>
28 G_BEGIN_DECLS
31 /* I'm not sure I remember our proposed naming convention here. */
32 /**
33 * G_SPAWN_ERROR:
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.
39 #define G_SPAWN_ERROR g_spawn_error_quark ()
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.
66 * Error codes returned by spawning processes.
68 typedef enum
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
95 } GSpawnError;
97 /**
98 * G_SPAWN_EXIT_ERROR:
100 * Error domain used by g_spawn_check_exit_status(). The code
101 * will be the program exit code.
103 #define G_SPAWN_EXIT_ERROR g_spawn_exit_error_quark ()
106 * GSpawnChildSetupFunc:
107 * @user_data: (closure): user data to pass to the function.
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.
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.
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.
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.
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.
140 typedef void (* GSpawnChildSetupFunc) (gpointer user_data);
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
168 * Flags passed to g_spawn_sync(), g_spawn_async() and g_spawn_async_with_pipes().
170 typedef enum
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;
186 GLIB_AVAILABLE_IN_ALL
187 GQuark g_spawn_error_quark (void);
188 GLIB_AVAILABLE_IN_ALL
189 GQuark g_spawn_exit_error_quark (void);
191 GLIB_AVAILABLE_IN_ALL
192 gboolean 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);
202 /* Opens pipes for non-NULL standard_output, standard_input, standard_error,
203 * and returns the parent's end of the pipes.
205 GLIB_AVAILABLE_IN_ALL
206 gboolean 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);
219 /* If standard_output or standard_error are non-NULL, the full
220 * standard output or error of the command will be placed there.
223 GLIB_AVAILABLE_IN_ALL
224 gboolean g_spawn_sync (const gchar *working_directory,
225 gchar **argv,
226 gchar **envp,
227 GSpawnFlags flags,
228 GSpawnChildSetupFunc child_setup,
229 gpointer user_data,
230 gchar **standard_output,
231 gchar **standard_error,
232 gint *exit_status,
233 GError **error);
235 GLIB_AVAILABLE_IN_ALL
236 gboolean g_spawn_command_line_sync (const gchar *command_line,
237 gchar **standard_output,
238 gchar **standard_error,
239 gint *exit_status,
240 GError **error);
241 GLIB_AVAILABLE_IN_ALL
242 gboolean g_spawn_command_line_async (const gchar *command_line,
243 GError **error);
245 GLIB_AVAILABLE_IN_2_34
246 gboolean g_spawn_check_exit_status (gint exit_status,
247 GError **error);
249 GLIB_AVAILABLE_IN_ALL
250 void g_spawn_close_pid (GPid pid);
252 G_END_DECLS
254 #endif /* __G_SPAWN_H__ */