Merge branch 'concurrent-cancellable'
[platform/upstream/glib.git] / glib / gspawn.h
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 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
22 #error "Only <glib.h> can be included directly."
23 #endif
24
25 #ifndef __G_SPAWN_H__
26 #define __G_SPAWN_H__
27
28 #include <glib/gerror.h>
29
30 G_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_2BIG: execv() returned %E2BIG.
51  * @G_SPAWN_ERROR_NOEXEC: execv() returned %ENOEXEC.
52  * @G_SPAWN_ERROR_NAMETOOLONG: execv() returned %ENAMETOOLONG.
53  * @G_SPAWN_ERROR_NOENT: execv() returned %ENOENT.
54  * @G_SPAWN_ERROR_NOMEM: execv() returned %ENOMEM.
55  * @G_SPAWN_ERROR_NOTDIR: execv() returned %ENOTDIR.
56  * @G_SPAWN_ERROR_LOOP: execv() returned %ELOOP.
57  * @G_SPAWN_ERROR_TXTBUSY: execv() returned %ETXTBUSY.
58  * @G_SPAWN_ERROR_IO: execv() returned %EIO.
59  * @G_SPAWN_ERROR_NFILE: execv() returned %ENFILE.
60  * @G_SPAWN_ERROR_MFILE: execv() returned %EMFILE.
61  * @G_SPAWN_ERROR_INVAL: execv() returned %EINVAL.
62  * @G_SPAWN_ERROR_ISDIR: execv() returned %EISDIR.
63  * @G_SPAWN_ERROR_LIBBAD: execv() returned %ELIBBAD.
64  * @G_SPAWN_ERROR_FAILED: Some other fatal failure,
65  *   <literal>error-&gt;message</literal> should explain.
66  *
67  * Error codes returned by spawning processes.
68  */
69 typedef enum
70 {
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
91                               * should explain
92                               */
93 } GSpawnError;
94
95 /**
96  * GSpawnChildSetupFunc:
97  * @user_data: user data to pass to the function.
98  *
99  * Specifies the type of the setup function passed to g_spawn_async(),
100  * g_spawn_sync() and g_spawn_async_with_pipes(). On POSIX platforms it
101  * is called in the child after GLib has performed all the setup it plans
102  * to perform but before calling exec(). On POSIX actions taken in this
103  * function will thus only affect the child, not the parent.
104  *
105  * Note that POSIX allows only async-signal-safe functions (see signal(7))
106  * to be called in the child between fork() and exec(), which drastically
107  * limits the usefulness of child setup functions.
108  *
109  * Also note that modifying the environment from the child setup function
110  * may not have the intended effect, since it will get overridden by
111  * a non-%NULL @env argument to the <literal>g_spawn...</literal> functions.
112  *
113  * On Windows the function is called in the parent. Its usefulness on
114  * Windows is thus questionable. In many cases executing the child setup
115  * function in the parent can have ill effects, and you should be very
116  * careful when porting software to Windows that uses child setup
117  * functions.
118  */
119 typedef void (* GSpawnChildSetupFunc) (gpointer user_data);
120
121 /**
122  * GSpawnFlags:
123  * @G_SPAWN_LEAVE_DESCRIPTORS_OPEN: the parent's open file descriptors will be
124  *   inherited by the child; otherwise all descriptors except stdin/stdout/stderr
125  *   will be closed before calling exec() in the child.
126  * @G_SPAWN_DO_NOT_REAP_CHILD: the child will not be automatically reaped; you
127  *   must use g_child_watch_add() yourself (or call waitpid()
128  *   or handle <literal>SIGCHLD</literal> yourself), or the child will become a zombie.
129  * @G_SPAWN_SEARCH_PATH: <literal>argv[0]</literal> need not be an absolute path,
130  *   it will be looked for in the user's <envar>PATH</envar>.
131  * @G_SPAWN_STDOUT_TO_DEV_NULL: the child's standard output will be discarded,
132  *   instead of going to the same location as the parent's standard output.
133  * @G_SPAWN_STDERR_TO_DEV_NULL: the child's standard error will be discarded.
134  * @G_SPAWN_CHILD_INHERITS_STDIN: the child will inherit the parent's standard
135  *   input (by default, the child's standard input is attached to
136  *   <filename>/dev/null</filename>).
137  * @G_SPAWN_FILE_AND_ARGV_ZERO: the first element of <literal>argv</literal> is
138  *   the file to execute, while the remaining elements are the actual argument
139  *   vector to pass to the file. Normally g_spawn_async_with_pipes() uses
140  *   <literal>argv[0]</literal> as the file to execute, and passes all of
141  *   <literal>argv</literal> to the child.
142  *
143  * Flags passed to g_spawn_sync(), g_spawn_async() and g_spawn_async_with_pipes().
144  */
145 typedef enum
146 {
147   G_SPAWN_LEAVE_DESCRIPTORS_OPEN = 1 << 0,
148   G_SPAWN_DO_NOT_REAP_CHILD      = 1 << 1,
149   /* look for argv[0] in the path i.e. use execvp() */
150   G_SPAWN_SEARCH_PATH            = 1 << 2,
151   /* Dump output to /dev/null */
152   G_SPAWN_STDOUT_TO_DEV_NULL     = 1 << 3,
153   G_SPAWN_STDERR_TO_DEV_NULL     = 1 << 4,
154   G_SPAWN_CHILD_INHERITS_STDIN   = 1 << 5,
155   G_SPAWN_FILE_AND_ARGV_ZERO     = 1 << 6
156 } GSpawnFlags;
157
158 GQuark g_spawn_error_quark (void);
159
160 #ifndef __GTK_DOC_IGNORE__
161 #ifdef G_OS_WIN32
162 #define g_spawn_async g_spawn_async_utf8
163 #define g_spawn_async_with_pipes g_spawn_async_with_pipes_utf8
164 #define g_spawn_sync g_spawn_sync_utf8
165 #define g_spawn_command_line_sync g_spawn_command_line_sync_utf8
166 #define g_spawn_command_line_async g_spawn_command_line_async_utf8
167 #endif
168 #endif
169
170 gboolean g_spawn_async (const gchar           *working_directory,
171                         gchar                **argv,
172                         gchar                **envp,
173                         GSpawnFlags            flags,
174                         GSpawnChildSetupFunc   child_setup,
175                         gpointer               user_data,
176                         GPid                  *child_pid,
177                         GError               **error);
178
179
180 /* Opens pipes for non-NULL standard_output, standard_input, standard_error,
181  * and returns the parent's end of the pipes.
182  */
183 gboolean g_spawn_async_with_pipes (const gchar          *working_directory,
184                                    gchar               **argv,
185                                    gchar               **envp,
186                                    GSpawnFlags           flags,
187                                    GSpawnChildSetupFunc  child_setup,
188                                    gpointer              user_data,
189                                    GPid                 *child_pid,
190                                    gint                 *standard_input,
191                                    gint                 *standard_output,
192                                    gint                 *standard_error,
193                                    GError              **error);
194
195
196 /* If standard_output or standard_error are non-NULL, the full
197  * standard output or error of the command will be placed there.
198  */
199
200 gboolean g_spawn_sync         (const gchar          *working_directory,
201                                gchar               **argv,
202                                gchar               **envp,
203                                GSpawnFlags           flags,
204                                GSpawnChildSetupFunc  child_setup,
205                                gpointer              user_data,
206                                gchar               **standard_output,
207                                gchar               **standard_error,
208                                gint                 *exit_status,
209                                GError              **error);
210
211 gboolean g_spawn_command_line_sync  (const gchar          *command_line,
212                                      gchar               **standard_output,
213                                      gchar               **standard_error,
214                                      gint                 *exit_status,
215                                      GError              **error);
216 gboolean g_spawn_command_line_async (const gchar          *command_line,
217                                      GError              **error);
218
219 void g_spawn_close_pid (GPid pid);
220
221 G_END_DECLS
222
223 #endif /* __G_SPAWN_H__ */