Make PLT-reduction work with gcc4, and don't include everything in
[platform/upstream/glib.git] / glib / gspawn.c
1 /* gspawn.c - Process launching
2  *
3  *  Copyright 2000 Red Hat, Inc.
4  *  g_execvpe implementation based on GNU libc execvp:
5  *   Copyright 1991, 92, 95, 96, 97, 98, 99 Free Software Foundation, Inc.
6  *
7  * GLib is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * GLib is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with GLib; see the file COPYING.LIB.  If not, write
19  * to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include "config.h"
24
25 #include <sys/time.h>
26 #include <sys/types.h>
27 #include <sys/wait.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <signal.h>
32 #include <string.h>
33
34 #ifdef HAVE_SYS_SELECT_H
35 #include <sys/select.h>
36 #endif /* HAVE_SYS_SELECT_H */
37
38 #include "glib.h"
39 #include "galias.h"
40
41 #include "glibintl.h"
42
43 /* With solaris threads, fork() duplicates all threads, which
44  * a) could cause unexpected side-effects, and b) is expensive.
45  * Once we remove support for solaris threads, the FORK1 #define
46  * should be removedl
47  */
48 #ifdef G_THREADS_IMPL_SOLARIS
49 #define FORK1() fork1()
50 #else
51 #define FORK1() fork()
52 #endif
53
54 static gint g_execute (const gchar  *file,
55                        gchar **argv,
56                        gchar **envp,
57                        gboolean search_path);
58
59 static gboolean make_pipe            (gint                  p[2],
60                                       GError              **error);
61 static gboolean fork_exec_with_pipes (gboolean              intermediate_child,
62                                       const gchar          *working_directory,
63                                       gchar               **argv,
64                                       gchar               **envp,
65                                       gboolean              close_descriptors,
66                                       gboolean              search_path,
67                                       gboolean              stdout_to_null,
68                                       gboolean              stderr_to_null,
69                                       gboolean              child_inherits_stdin,
70                                       gboolean              file_and_argv_zero,
71                                       GSpawnChildSetupFunc  child_setup,
72                                       gpointer              user_data,
73                                       GPid                 *child_pid,
74                                       gint                 *standard_input,
75                                       gint                 *standard_output,
76                                       gint                 *standard_error,
77                                       GError              **error);
78
79 GQuark
80 g_spawn_error_quark (void)
81 {
82   static GQuark quark = 0;
83   if (quark == 0)
84     quark = g_quark_from_static_string ("g-exec-error-quark");
85   return quark;
86 }
87
88 /**
89  * g_spawn_async:
90  * @working_directory: child's current working directory, or %NULL to inherit parent's
91  * @argv: child's argument vector
92  * @envp: child's environment, or %NULL to inherit parent's
93  * @flags: flags from #GSpawnFlags
94  * @child_setup: function to run in the child just before exec()
95  * @user_data: user data for @child_setup
96  * @child_pid: return location for child process ID, or %NULL
97  * @error: return location for error
98  * 
99  * See g_spawn_async_with_pipes() for a full description; this function
100  * simply calls the g_spawn_async_with_pipes() without any pipes.
101  * 
102  * Return value: %TRUE on success, %FALSE if error is set
103  **/
104 gboolean
105 g_spawn_async (const gchar          *working_directory,
106                gchar               **argv,
107                gchar               **envp,
108                GSpawnFlags           flags,
109                GSpawnChildSetupFunc  child_setup,
110                gpointer              user_data,
111                GPid                 *child_pid,
112                GError              **error)
113 {
114   g_return_val_if_fail (argv != NULL, FALSE);
115   
116   return g_spawn_async_with_pipes (working_directory,
117                                    argv, envp,
118                                    flags,
119                                    child_setup,
120                                    user_data,
121                                    child_pid,
122                                    NULL, NULL, NULL,
123                                    error);
124 }
125
126 /* Avoids a danger in threaded situations (calling close()
127  * on a file descriptor twice, and another thread has
128  * re-opened it since the first close)
129  */
130 static gint
131 close_and_invalidate (gint *fd)
132 {
133   gint ret;
134
135   if (*fd < 0)
136     return -1;
137   else
138     {
139       ret = close (*fd);
140       *fd = -1;
141     }
142
143   return ret;
144 }
145
146 typedef enum
147 {
148   READ_FAILED = 0, /* FALSE */
149   READ_OK,
150   READ_EOF
151 } ReadResult;
152
153 static ReadResult
154 read_data (GString *str,
155            gint     fd,
156            GError **error)
157 {
158   gssize bytes;        
159   gchar buf[4096];    
160
161  again:
162   
163   bytes = read (fd, buf, 4096);
164
165   if (bytes == 0)
166     return READ_EOF;
167   else if (bytes > 0)
168     {
169       g_string_append_len (str, buf, bytes);
170       return READ_OK;
171     }
172   else if (bytes < 0 && errno == EINTR)
173     goto again;
174   else if (bytes < 0)
175     {
176       g_set_error (error,
177                    G_SPAWN_ERROR,
178                    G_SPAWN_ERROR_READ,
179                    _("Failed to read data from child process (%s)"),
180                    g_strerror (errno));
181       
182       return READ_FAILED;
183     }
184   else
185     return READ_OK;
186 }
187
188 /**
189  * g_spawn_sync:
190  * @working_directory: child's current working directory, or %NULL to inherit parent's
191  * @argv: child's argument vector
192  * @envp: child's environment, or %NULL to inherit parent's
193  * @flags: flags from #GSpawnFlags
194  * @child_setup: function to run in the child just before exec()
195  * @user_data: user data for @child_setup
196  * @standard_output: return location for child output 
197  * @standard_error: return location for child error messages
198  * @exit_status: child exit status, as returned by waitpid()
199  * @error: return location for error
200  *
201  * Executes a child synchronously (waits for the child to exit before returning).
202  * All output from the child is stored in @standard_output and @standard_error,
203  * if those parameters are non-%NULL. If @exit_status is non-%NULL, the exit 
204  * status of the child is stored there as it would be returned by 
205  * waitpid(); standard UNIX macros such as WIFEXITED() and WEXITSTATUS() 
206  * must be used to evaluate the exit status. If an error occurs, no data is 
207  * returned in @standard_output, @standard_error, or @exit_status.
208  * 
209  * This function calls g_spawn_async_with_pipes() internally; see that function
210  * for full details on the other parameters.
211  * 
212  * Return value: %TRUE on success, %FALSE if an error was set.
213  **/
214 gboolean
215 g_spawn_sync (const gchar          *working_directory,
216               gchar               **argv,
217               gchar               **envp,
218               GSpawnFlags           flags,
219               GSpawnChildSetupFunc  child_setup,
220               gpointer              user_data,
221               gchar               **standard_output,
222               gchar               **standard_error,
223               gint                 *exit_status,
224               GError              **error)     
225 {
226   gint outpipe = -1;
227   gint errpipe = -1;
228   GPid pid;
229   fd_set fds;
230   gint ret;
231   GString *outstr = NULL;
232   GString *errstr = NULL;
233   gboolean failed;
234   gint status;
235   
236   g_return_val_if_fail (argv != NULL, FALSE);
237   g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
238   g_return_val_if_fail (standard_output == NULL ||
239                         !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
240   g_return_val_if_fail (standard_error == NULL ||
241                         !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
242   
243   /* Just to ensure segfaults if callers try to use
244    * these when an error is reported.
245    */
246   if (standard_output)
247     *standard_output = NULL;
248
249   if (standard_error)
250     *standard_error = NULL;
251   
252   if (!fork_exec_with_pipes (FALSE,
253                              working_directory,
254                              argv,
255                              envp,
256                              !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
257                              (flags & G_SPAWN_SEARCH_PATH) != 0,
258                              (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
259                              (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
260                              (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
261                              (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
262                              child_setup,
263                              user_data,
264                              &pid,
265                              NULL,
266                              standard_output ? &outpipe : NULL,
267                              standard_error ? &errpipe : NULL,
268                              error))
269     return FALSE;
270
271   /* Read data from child. */
272   
273   failed = FALSE;
274
275   if (outpipe >= 0)
276     {
277       outstr = g_string_new (NULL);
278     }
279       
280   if (errpipe >= 0)
281     {
282       errstr = g_string_new (NULL);
283     }
284
285   /* Read data until we get EOF on both pipes. */
286   while (!failed &&
287          (outpipe >= 0 ||
288           errpipe >= 0))
289     {
290       ret = 0;
291           
292       FD_ZERO (&fds);
293       if (outpipe >= 0)
294         FD_SET (outpipe, &fds);
295       if (errpipe >= 0)
296         FD_SET (errpipe, &fds);
297           
298       ret = select (MAX (outpipe, errpipe) + 1,
299                     &fds,
300                     NULL, NULL,
301                     NULL /* no timeout */);
302
303       if (ret < 0 && errno != EINTR)
304         {
305           failed = TRUE;
306
307           g_set_error (error,
308                        G_SPAWN_ERROR,
309                        G_SPAWN_ERROR_READ,
310                        _("Unexpected error in select() reading data from a child process (%s)"),
311                        g_strerror (errno));
312               
313           break;
314         }
315
316       if (outpipe >= 0 && FD_ISSET (outpipe, &fds))
317         {
318           switch (read_data (outstr, outpipe, error))
319             {
320             case READ_FAILED:
321               failed = TRUE;
322               break;
323             case READ_EOF:
324               close_and_invalidate (&outpipe);
325               outpipe = -1;
326               break;
327             default:
328               break;
329             }
330
331           if (failed)
332             break;
333         }
334
335       if (errpipe >= 0 && FD_ISSET (errpipe, &fds))
336         {
337           switch (read_data (errstr, errpipe, error))
338             {
339             case READ_FAILED:
340               failed = TRUE;
341               break;
342             case READ_EOF:
343               close_and_invalidate (&errpipe);
344               errpipe = -1;
345               break;
346             default:
347               break;
348             }
349
350           if (failed)
351             break;
352         }
353     }
354
355   /* These should only be open still if we had an error.  */
356   
357   if (outpipe >= 0)
358     close_and_invalidate (&outpipe);
359   if (errpipe >= 0)
360     close_and_invalidate (&errpipe);
361   
362   /* Wait for child to exit, even if we have
363    * an error pending.
364    */
365  again:
366       
367   ret = waitpid (pid, &status, 0);
368
369   if (ret < 0)
370     {
371       if (errno == EINTR)
372         goto again;
373       else if (errno == ECHILD)
374         {
375           if (exit_status)
376             {
377               g_warning ("In call to g_spawn_sync(), exit status of a child process was requested but SIGCHLD action was set to SIG_IGN and ECHILD was received by waitpid(), so exit status can't be returned. This is a bug in the program calling g_spawn_sync(); either don't request the exit status, or don't set the SIGCHLD action.");
378             }
379           else
380             {
381               /* We don't need the exit status. */
382             }
383         }
384       else
385         {
386           if (!failed) /* avoid error pileups */
387             {
388               failed = TRUE;
389                   
390               g_set_error (error,
391                            G_SPAWN_ERROR,
392                            G_SPAWN_ERROR_READ,
393                            _("Unexpected error in waitpid() (%s)"),
394                            g_strerror (errno));
395             }
396         }
397     }
398   
399   if (failed)
400     {
401       if (outstr)
402         g_string_free (outstr, TRUE);
403       if (errstr)
404         g_string_free (errstr, TRUE);
405
406       return FALSE;
407     }
408   else
409     {
410       if (exit_status)
411         *exit_status = status;
412       
413       if (standard_output)        
414         *standard_output = g_string_free (outstr, FALSE);
415
416       if (standard_error)
417         *standard_error = g_string_free (errstr, FALSE);
418
419       return TRUE;
420     }
421 }
422
423 /**
424  * g_spawn_async_with_pipes:
425  * @working_directory: child's current working directory, or %NULL to inherit parent's
426  * @argv: child's argument vector
427  * @envp: child's environment, or %NULL to inherit parent's
428  * @flags: flags from #GSpawnFlags
429  * @child_setup: function to run in the child just before exec()
430  * @user_data: user data for @child_setup
431  * @child_pid: return location for child process ID, or %NULL
432  * @standard_input: return location for file descriptor to write to child's stdin, or %NULL
433  * @standard_output: return location for file descriptor to read child's stdout, or %NULL
434  * @standard_error: return location for file descriptor to read child's stderr, or %NULL
435  * @error: return location for error
436  *
437  * Executes a child program asynchronously (your program will not
438  * block waiting for the child to exit). The child program is
439  * specified by the only argument that must be provided, @argv. @argv
440  * should be a %NULL-terminated array of strings, to be passed as the
441  * argument vector for the child. The first string in @argv is of
442  * course the name of the program to execute. By default, the name of
443  * the program must be a full path; the <envar>PATH</envar> shell variable 
444  * will only be searched if you pass the %G_SPAWN_SEARCH_PATH flag.
445  *
446  * On Windows, the low-level child process creation API
447  * (CreateProcess())doesn't use argument vectors,
448  * but a command line. The C runtime library's
449  * <function>spawn*()</function> family of functions (which
450  * g_spawn_async_with_pipes() eventually calls) paste the argument
451  * vector elements into a command line, and the C runtime startup code
452  * does a corresponding reconstruction of an argument vector from the
453  * command line, to be passed to main(). Complications arise when you have
454  * argument vector elements that contain spaces of double quotes. The
455  * <function>spawn*()</function> functions don't do any quoting or
456  * escaping, but on the other hand the startup code does do unquoting
457  * and unescaping in order to enable receiving arguments with embedded
458  * spaces or double quotes. To work around this asymmetry,
459  * g_spawn_async_with_pipes() will do quoting and escaping on argument
460  * vector elements that need it before calling the C runtime
461  * spawn() function.
462  *
463  * @envp is a %NULL-terminated array of strings, where each string
464  * has the form <literal>KEY=VALUE</literal>. This will become
465  * the child's environment. If @envp is %NULL, the child inherits its
466  * parent's environment.
467  *
468  * @flags should be the bitwise OR of any flags you want to affect the
469  * function's behaviour. The %G_SPAWN_DO_NOT_REAP_CHILD means that 
470  * the child will not automatically be reaped; you must use a
471  * #GChildWatch source to be notified about the death of the child 
472  * process. Eventually you must call g_spawn_close_pid() on the
473  * @child_pid, in order to free resources which may be associated
474  * with the child process. (On Unix, using a #GChildWatch source is
475  * equivalent to calling waitpid() or handling the %SIGCHLD signal 
476  * manually. On Windows, calling g_spawn_close_pid() is equivalent
477  * to calling CloseHandle() on the process handle returned in 
478  * @child_pid).
479  *
480  * %G_SPAWN_LEAVE_DESCRIPTORS_OPEN means that the parent's open file
481  * descriptors will be inherited by the child; otherwise all
482  * descriptors except stdin/stdout/stderr will be closed before
483  * calling exec() in the child. %G_SPAWN_SEARCH_PATH 
484  * means that <literal>argv[0]</literal> need not be an absolute path, it
485  * will be looked for in the user's <envar>PATH</envar>. 
486  * %G_SPAWN_STDOUT_TO_DEV_NULL means that the child's standard output will 
487  * be discarded, instead of going to the same location as the parent's 
488  * standard output. If you use this flag, @standard_output must be %NULL.
489  * %G_SPAWN_STDERR_TO_DEV_NULL means that the child's standard error
490  * will be discarded, instead of going to the same location as the parent's
491  * standard error. If you use this flag, @standard_error must be %NULL.
492  * %G_SPAWN_CHILD_INHERITS_STDIN means that the child will inherit the parent's
493  * standard input (by default, the child's standard input is attached to
494  * /dev/null). If you use this flag, @standard_input must be %NULL.
495  * %G_SPAWN_FILE_AND_ARGV_ZERO means that the first element of @argv is
496  * the file to execute, while the remaining elements are the
497  * actual argument vector to pass to the file. Normally
498  * g_spawn_async_with_pipes() uses @argv[0] as the file to execute, and
499  * passes all of @argv to the child.
500  *
501  * @child_setup and @user_data are a function and user data. On POSIX
502  * platforms, the function is called in the child after GLib has
503  * performed all the setup it plans to perform (including creating
504  * pipes, closing file descriptors, etc.) but before calling
505  * exec(). That is, @child_setup is called just
506  * before calling exec() in the child. Obviously
507  * actions taken in this function will only affect the child, not the
508  * parent. On Windows, there is no separate fork() and exec()
509  * functionality. Child processes are created and run right away with
510  * one API call, CreateProcess(). @child_setup is
511  * called in the parent process just before creating the child
512  * process. You should carefully consider what you do in @child_setup
513  * if you intend your software to be portable to Windows.
514  *
515  * If non-%NULL, @child_pid will on Unix be filled with the child's
516  * process ID. You can use the process ID to send signals to the
517  * child, or to waitpid() if you specified the
518  * %G_SPAWN_DO_NOT_REAP_CHILD flag. On Windows, @child_pid will be
519  * filled with a handle to the child process only if you specified the
520  * %G_SPAWN_DO_NOT_REAP_CHILD flag. You can then access the child
521  * process using the Win32 API, for example wait for its termination
522  * with the <function>WaitFor*()</function> functions, or examine its
523  * exit code with GetExitCodeProcess(). You should close the handle 
524  * with CloseHandle() when you no longer need it.
525  *
526  * If non-%NULL, the @standard_input, @standard_output, @standard_error
527  * locations will be filled with file descriptors for writing to the child's
528  * standard input or reading from its standard output or standard error.
529  * The caller of g_spawn_async_with_pipes() must close these file descriptors
530  * when they are no longer in use. If these parameters are %NULL, the corresponding
531  * pipe won't be created.
532  *
533  * If @standard_input is NULL, the child's standard input is attached to /dev/null
534  * unless %G_SPAWN_CHILD_INHERITS_STDIN is set.
535  *
536  * If @standard_error is NULL, the child's standard error goes to the same location
537  * as the parent's standard error unless %G_SPAWN_STDERR_TO_DEV_NULL is set.
538  *
539  * If @standard_output is NULL, the child's standard output goes to the same location
540  * as the parent's standard output unless %G_SPAWN_STDOUT_TO_DEV_NULL is set.
541  *
542  * @error can be %NULL to ignore errors, or non-%NULL to report errors.
543  * If an error is set, the function returns %FALSE. Errors
544  * are reported even if they occur in the child (for example if the
545  * executable in <literal>argv[0]</literal> is not found). Typically
546  * the <literal>message</literal> field of returned errors should be displayed
547  * to users. Possible errors are those from the #G_SPAWN_ERROR domain.
548  *
549  * If an error occurs, @child_pid, @standard_input, @standard_output,
550  * and @standard_error will not be filled with valid values.
551  *
552  * If @child_pid is not %NULL and an error does not occur then the returned
553  * pid must be closed using g_spawn_close_pid().
554  * 
555  * Return value: %TRUE on success, %FALSE if an error was set
556  **/
557 gboolean
558 g_spawn_async_with_pipes (const gchar          *working_directory,
559                           gchar               **argv,
560                           gchar               **envp,
561                           GSpawnFlags           flags,
562                           GSpawnChildSetupFunc  child_setup,
563                           gpointer              user_data,
564                           GPid                 *child_pid,
565                           gint                 *standard_input,
566                           gint                 *standard_output,
567                           gint                 *standard_error,
568                           GError              **error)
569 {
570   g_return_val_if_fail (argv != NULL, FALSE);
571   g_return_val_if_fail (standard_output == NULL ||
572                         !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
573   g_return_val_if_fail (standard_error == NULL ||
574                         !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
575   /* can't inherit stdin if we have an input pipe. */
576   g_return_val_if_fail (standard_input == NULL ||
577                         !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
578   
579   return fork_exec_with_pipes (!(flags & G_SPAWN_DO_NOT_REAP_CHILD),
580                                working_directory,
581                                argv,
582                                envp,
583                                !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
584                                (flags & G_SPAWN_SEARCH_PATH) != 0,
585                                (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
586                                (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
587                                (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
588                                (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
589                                child_setup,
590                                user_data,
591                                child_pid,
592                                standard_input,
593                                standard_output,
594                                standard_error,
595                                error);
596 }
597
598 /**
599  * g_spawn_command_line_sync:
600  * @command_line: a command line 
601  * @standard_output: return location for child output
602  * @standard_error: return location for child errors
603  * @exit_status: return location for child exit status
604  * @error: return location for errors
605  *
606  * A simple version of g_spawn_sync() with little-used parameters
607  * removed, taking a command line instead of an argument vector.  See
608  * g_spawn_sync() for full details. @command_line will be parsed by
609  * g_shell_parse_argv(). Unlike g_spawn_sync(), the %G_SPAWN_SEARCH_PATH flag
610  * is enabled. Note that %G_SPAWN_SEARCH_PATH can have security
611  * implications, so consider using g_spawn_sync() directly if
612  * appropriate. Possible errors are those from g_spawn_sync() and those
613  * from g_shell_parse_argv().
614  * 
615  * On Windows, please note the implications of g_shell_parse_argv()
616  * parsing @command_line. Space is a separator, and backslashes are
617  * special. Thus you cannot simply pass a @command_line containing
618  * canonical Windows paths, like "c:\\program files\\app\\app.exe", as
619  * the backslashes will be eaten, and the space will act as a
620  * separator. You need to enclose such paths with single quotes, like
621  * "'c:\\program files\\app\\app.exe' 'e:\\folder\\argument.txt'".
622  *
623  * Return value: %TRUE on success, %FALSE if an error was set
624  **/
625 gboolean
626 g_spawn_command_line_sync (const gchar  *command_line,
627                            gchar       **standard_output,
628                            gchar       **standard_error,
629                            gint         *exit_status,
630                            GError      **error)
631 {
632   gboolean retval;
633   gchar **argv = NULL;
634
635   g_return_val_if_fail (command_line != NULL, FALSE);
636   
637   if (!g_shell_parse_argv (command_line,
638                            NULL, &argv,
639                            error))
640     return FALSE;
641   
642   retval = g_spawn_sync (NULL,
643                          argv,
644                          NULL,
645                          G_SPAWN_SEARCH_PATH,
646                          NULL,
647                          NULL,
648                          standard_output,
649                          standard_error,
650                          exit_status,
651                          error);
652   g_strfreev (argv);
653
654   return retval;
655 }
656
657 /**
658  * g_spawn_command_line_async:
659  * @command_line: a command line
660  * @error: return location for errors
661  * 
662  * A simple version of g_spawn_async() that parses a command line with
663  * g_shell_parse_argv() and passes it to g_spawn_async(). Runs a
664  * command line in the background. Unlike g_spawn_async(), the
665  * %G_SPAWN_SEARCH_PATH flag is enabled, other flags are not. Note
666  * that %G_SPAWN_SEARCH_PATH can have security implications, so
667  * consider using g_spawn_async() directly if appropriate. Possible
668  * errors are those from g_shell_parse_argv() and g_spawn_async().
669  * 
670  * The same concerns on Windows apply as for g_spawn_command_line_sync().
671  *
672  * Return value: %TRUE on success, %FALSE if error is set.
673  **/
674 gboolean
675 g_spawn_command_line_async (const gchar *command_line,
676                             GError     **error)
677 {
678   gboolean retval;
679   gchar **argv = NULL;
680
681   g_return_val_if_fail (command_line != NULL, FALSE);
682
683   if (!g_shell_parse_argv (command_line,
684                            NULL, &argv,
685                            error))
686     return FALSE;
687   
688   retval = g_spawn_async (NULL,
689                           argv,
690                           NULL,
691                           G_SPAWN_SEARCH_PATH,
692                           NULL,
693                           NULL,
694                           NULL,
695                           error);
696   g_strfreev (argv);
697
698   return retval;
699 }
700
701 static gint
702 exec_err_to_g_error (gint en)
703 {
704   switch (en)
705     {
706 #ifdef EACCES
707     case EACCES:
708       return G_SPAWN_ERROR_ACCES;
709       break;
710 #endif
711
712 #ifdef EPERM
713     case EPERM:
714       return G_SPAWN_ERROR_PERM;
715       break;
716 #endif
717
718 #ifdef E2BIG
719     case E2BIG:
720       return G_SPAWN_ERROR_2BIG;
721       break;
722 #endif
723
724 #ifdef ENOEXEC
725     case ENOEXEC:
726       return G_SPAWN_ERROR_NOEXEC;
727       break;
728 #endif
729
730 #ifdef ENAMETOOLONG
731     case ENAMETOOLONG:
732       return G_SPAWN_ERROR_NAMETOOLONG;
733       break;
734 #endif
735
736 #ifdef ENOENT
737     case ENOENT:
738       return G_SPAWN_ERROR_NOENT;
739       break;
740 #endif
741
742 #ifdef ENOMEM
743     case ENOMEM:
744       return G_SPAWN_ERROR_NOMEM;
745       break;
746 #endif
747
748 #ifdef ENOTDIR
749     case ENOTDIR:
750       return G_SPAWN_ERROR_NOTDIR;
751       break;
752 #endif
753
754 #ifdef ELOOP
755     case ELOOP:
756       return G_SPAWN_ERROR_LOOP;
757       break;
758 #endif
759       
760 #ifdef ETXTBUSY
761     case ETXTBUSY:
762       return G_SPAWN_ERROR_TXTBUSY;
763       break;
764 #endif
765
766 #ifdef EIO
767     case EIO:
768       return G_SPAWN_ERROR_IO;
769       break;
770 #endif
771
772 #ifdef ENFILE
773     case ENFILE:
774       return G_SPAWN_ERROR_NFILE;
775       break;
776 #endif
777
778 #ifdef EMFILE
779     case EMFILE:
780       return G_SPAWN_ERROR_MFILE;
781       break;
782 #endif
783
784 #ifdef EINVAL
785     case EINVAL:
786       return G_SPAWN_ERROR_INVAL;
787       break;
788 #endif
789
790 #ifdef EISDIR
791     case EISDIR:
792       return G_SPAWN_ERROR_ISDIR;
793       break;
794 #endif
795
796 #ifdef ELIBBAD
797     case ELIBBAD:
798       return G_SPAWN_ERROR_LIBBAD;
799       break;
800 #endif
801       
802     default:
803       return G_SPAWN_ERROR_FAILED;
804       break;
805     }
806 }
807
808 static gssize
809 write_all (gint fd, gconstpointer vbuf, gsize to_write)
810 {
811   gchar *buf = (gchar *) vbuf;
812   
813   while (to_write > 0)
814     {
815       gssize count = write (fd, buf, to_write);
816       if (count < 0)
817         {
818           if (errno != EINTR)
819             return FALSE;
820         }
821       else
822         {
823           to_write -= count;
824           buf += count;
825         }
826     }
827   
828   return TRUE;
829 }
830
831 static void
832 write_err_and_exit (gint fd, gint msg)
833 {
834   gint en = errno;
835   
836   write_all (fd, &msg, sizeof(msg));
837   write_all (fd, &en, sizeof(en));
838   
839   _exit (1);
840 }
841
842 static void
843 set_cloexec (gint fd)
844 {
845   fcntl (fd, F_SETFD, FD_CLOEXEC);
846 }
847
848 static gint
849 sane_dup2 (gint fd1, gint fd2)
850 {
851   gint ret;
852
853  retry:
854   ret = dup2 (fd1, fd2);
855   if (ret < 0 && errno == EINTR)
856     goto retry;
857
858   return ret;
859 }
860
861 enum
862 {
863   CHILD_CHDIR_FAILED,
864   CHILD_EXEC_FAILED,
865   CHILD_DUP2_FAILED,
866   CHILD_FORK_FAILED
867 };
868
869 static void
870 do_exec (gint                  child_err_report_fd,
871          gint                  stdin_fd,
872          gint                  stdout_fd,
873          gint                  stderr_fd,
874          const gchar          *working_directory,
875          gchar               **argv,
876          gchar               **envp,
877          gboolean              close_descriptors,
878          gboolean              search_path,
879          gboolean              stdout_to_null,
880          gboolean              stderr_to_null,
881          gboolean              child_inherits_stdin,
882          gboolean              file_and_argv_zero,
883          GSpawnChildSetupFunc  child_setup,
884          gpointer              user_data)
885 {
886   if (working_directory && chdir (working_directory) < 0)
887     write_err_and_exit (child_err_report_fd,
888                         CHILD_CHDIR_FAILED);
889
890   /* Close all file descriptors but stdin stdout and stderr as
891    * soon as we exec. Note that this includes
892    * child_err_report_fd, which keeps the parent from blocking
893    * forever on the other end of that pipe.
894    */
895   if (close_descriptors)
896     {
897       gint open_max;
898       gint i;
899       
900       open_max = sysconf (_SC_OPEN_MAX);
901       for (i = 3; i < open_max; i++)
902         set_cloexec (i);
903     }
904   else
905     {
906       /* We need to do child_err_report_fd anyway */
907       set_cloexec (child_err_report_fd);
908     }
909   
910   /* Redirect pipes as required */
911   
912   if (stdin_fd >= 0)
913     {
914       /* dup2 can't actually fail here I don't think */
915           
916       if (sane_dup2 (stdin_fd, 0) < 0)
917         write_err_and_exit (child_err_report_fd,
918                             CHILD_DUP2_FAILED);
919
920       /* ignore this if it doesn't work */
921       close_and_invalidate (&stdin_fd);
922     }
923   else if (!child_inherits_stdin)
924     {
925       /* Keep process from blocking on a read of stdin */
926       gint read_null = open ("/dev/null", O_RDONLY);
927       sane_dup2 (read_null, 0);
928       close_and_invalidate (&read_null);
929     }
930
931   if (stdout_fd >= 0)
932     {
933       /* dup2 can't actually fail here I don't think */
934           
935       if (sane_dup2 (stdout_fd, 1) < 0)
936         write_err_and_exit (child_err_report_fd,
937                             CHILD_DUP2_FAILED);
938
939       /* ignore this if it doesn't work */
940       close_and_invalidate (&stdout_fd);
941     }
942   else if (stdout_to_null)
943     {
944       gint write_null = open ("/dev/null", O_WRONLY);
945       sane_dup2 (write_null, 1);
946       close_and_invalidate (&write_null);
947     }
948
949   if (stderr_fd >= 0)
950     {
951       /* dup2 can't actually fail here I don't think */
952           
953       if (sane_dup2 (stderr_fd, 2) < 0)
954         write_err_and_exit (child_err_report_fd,
955                             CHILD_DUP2_FAILED);
956
957       /* ignore this if it doesn't work */
958       close_and_invalidate (&stderr_fd);
959     }
960   else if (stderr_to_null)
961     {
962       gint write_null = open ("/dev/null", O_WRONLY);
963       sane_dup2 (write_null, 2);
964       close_and_invalidate (&write_null);
965     }
966   
967   /* Call user function just before we exec */
968   if (child_setup)
969     {
970       (* child_setup) (user_data);
971     }
972
973   g_execute (argv[0],
974              file_and_argv_zero ? argv + 1 : argv,
975              envp, search_path);
976
977   /* Exec failed */
978   write_err_and_exit (child_err_report_fd,
979                       CHILD_EXEC_FAILED);
980 }
981
982 static gboolean
983 read_ints (int      fd,
984            gint*    buf,
985            gint     n_ints_in_buf,    
986            gint    *n_ints_read,      
987            GError **error)
988 {
989   gsize bytes = 0;    
990   
991   while (TRUE)
992     {
993       gssize chunk;    
994
995       if (bytes >= sizeof(gint)*2)
996         break; /* give up, who knows what happened, should not be
997                 * possible.
998                 */
999           
1000     again:
1001       chunk = read (fd,
1002                     ((gchar*)buf) + bytes,
1003                     sizeof(gint) * n_ints_in_buf - bytes);
1004       if (chunk < 0 && errno == EINTR)
1005         goto again;
1006           
1007       if (chunk < 0)
1008         {
1009           /* Some weird shit happened, bail out */
1010               
1011           g_set_error (error,
1012                        G_SPAWN_ERROR,
1013                        G_SPAWN_ERROR_FAILED,
1014                        _("Failed to read from child pipe (%s)"),
1015                        g_strerror (errno));
1016
1017           return FALSE;
1018         }
1019       else if (chunk == 0)
1020         break; /* EOF */
1021       else /* chunk > 0 */
1022         bytes += chunk;
1023     }
1024
1025   *n_ints_read = (gint)(bytes / sizeof(gint));
1026
1027   return TRUE;
1028 }
1029
1030 static gboolean
1031 fork_exec_with_pipes (gboolean              intermediate_child,
1032                       const gchar          *working_directory,
1033                       gchar               **argv,
1034                       gchar               **envp,
1035                       gboolean              close_descriptors,
1036                       gboolean              search_path,
1037                       gboolean              stdout_to_null,
1038                       gboolean              stderr_to_null,
1039                       gboolean              child_inherits_stdin,
1040                       gboolean              file_and_argv_zero,
1041                       GSpawnChildSetupFunc  child_setup,
1042                       gpointer              user_data,
1043                       GPid                 *child_pid,
1044                       gint                 *standard_input,
1045                       gint                 *standard_output,
1046                       gint                 *standard_error,
1047                       GError              **error)     
1048 {
1049   GPid pid = -1;
1050   gint stdin_pipe[2] = { -1, -1 };
1051   gint stdout_pipe[2] = { -1, -1 };
1052   gint stderr_pipe[2] = { -1, -1 };
1053   gint child_err_report_pipe[2] = { -1, -1 };
1054   gint child_pid_report_pipe[2] = { -1, -1 };
1055   gint status;
1056   
1057   if (!make_pipe (child_err_report_pipe, error))
1058     return FALSE;
1059
1060   if (intermediate_child && !make_pipe (child_pid_report_pipe, error))
1061     goto cleanup_and_fail;
1062   
1063   if (standard_input && !make_pipe (stdin_pipe, error))
1064     goto cleanup_and_fail;
1065   
1066   if (standard_output && !make_pipe (stdout_pipe, error))
1067     goto cleanup_and_fail;
1068
1069   if (standard_error && !make_pipe (stderr_pipe, error))
1070     goto cleanup_and_fail;
1071
1072   pid = FORK1 ();
1073
1074   if (pid < 0)
1075     {      
1076       g_set_error (error,
1077                    G_SPAWN_ERROR,
1078                    G_SPAWN_ERROR_FORK,
1079                    _("Failed to fork (%s)"),
1080                    g_strerror (errno));
1081
1082       goto cleanup_and_fail;
1083     }
1084   else if (pid == 0)
1085     {
1086       /* Immediate child. This may or may not be the child that
1087        * actually execs the new process.
1088        */
1089       
1090       /* Be sure we crash if the parent exits
1091        * and we write to the err_report_pipe
1092        */
1093       signal (SIGPIPE, SIG_DFL);
1094
1095       /* Close the parent's end of the pipes;
1096        * not needed in the close_descriptors case,
1097        * though
1098        */
1099       close_and_invalidate (&child_err_report_pipe[0]);
1100       close_and_invalidate (&child_pid_report_pipe[0]);
1101       close_and_invalidate (&stdin_pipe[1]);
1102       close_and_invalidate (&stdout_pipe[0]);
1103       close_and_invalidate (&stderr_pipe[0]);
1104       
1105       if (intermediate_child)
1106         {
1107           /* We need to fork an intermediate child that launches the
1108            * final child. The purpose of the intermediate child
1109            * is to exit, so we can waitpid() it immediately.
1110            * Then the grandchild will not become a zombie.
1111            */
1112           GPid grandchild_pid;
1113
1114           grandchild_pid = FORK1 ();
1115
1116           if (grandchild_pid < 0)
1117             {
1118               /* report -1 as child PID */
1119               write_all (child_pid_report_pipe[1], &grandchild_pid,
1120                          sizeof(grandchild_pid));
1121               
1122               write_err_and_exit (child_err_report_pipe[1],
1123                                   CHILD_FORK_FAILED);              
1124             }
1125           else if (grandchild_pid == 0)
1126             {
1127               do_exec (child_err_report_pipe[1],
1128                        stdin_pipe[0],
1129                        stdout_pipe[1],
1130                        stderr_pipe[1],
1131                        working_directory,
1132                        argv,
1133                        envp,
1134                        close_descriptors,
1135                        search_path,
1136                        stdout_to_null,
1137                        stderr_to_null,
1138                        child_inherits_stdin,
1139                        file_and_argv_zero,
1140                        child_setup,
1141                        user_data);
1142             }
1143           else
1144             {
1145               write_all (child_pid_report_pipe[1], &grandchild_pid, sizeof(grandchild_pid));
1146               close_and_invalidate (&child_pid_report_pipe[1]);
1147               
1148               _exit (0);
1149             }
1150         }
1151       else
1152         {
1153           /* Just run the child.
1154            */
1155
1156           do_exec (child_err_report_pipe[1],
1157                    stdin_pipe[0],
1158                    stdout_pipe[1],
1159                    stderr_pipe[1],
1160                    working_directory,
1161                    argv,
1162                    envp,
1163                    close_descriptors,
1164                    search_path,
1165                    stdout_to_null,
1166                    stderr_to_null,
1167                    child_inherits_stdin,
1168                    file_and_argv_zero,
1169                    child_setup,
1170                    user_data);
1171         }
1172     }
1173   else
1174     {
1175       /* Parent */
1176       
1177       gint buf[2];
1178       gint n_ints = 0;    
1179
1180       /* Close the uncared-about ends of the pipes */
1181       close_and_invalidate (&child_err_report_pipe[1]);
1182       close_and_invalidate (&child_pid_report_pipe[1]);
1183       close_and_invalidate (&stdin_pipe[0]);
1184       close_and_invalidate (&stdout_pipe[1]);
1185       close_and_invalidate (&stderr_pipe[1]);
1186
1187       /* If we had an intermediate child, reap it */
1188       if (intermediate_child)
1189         {
1190         wait_again:
1191           if (waitpid (pid, &status, 0) < 0)
1192             {
1193               if (errno == EINTR)
1194                 goto wait_again;
1195               else if (errno == ECHILD)
1196                 ; /* do nothing, child already reaped */
1197               else
1198                 g_warning ("waitpid() should not fail in "
1199                            "'fork_exec_with_pipes'");
1200             }
1201         }
1202       
1203
1204       if (!read_ints (child_err_report_pipe[0],
1205                       buf, 2, &n_ints,
1206                       error))
1207         goto cleanup_and_fail;
1208         
1209       if (n_ints >= 2)
1210         {
1211           /* Error from the child. */
1212
1213           switch (buf[0])
1214             {
1215             case CHILD_CHDIR_FAILED:
1216               g_set_error (error,
1217                            G_SPAWN_ERROR,
1218                            G_SPAWN_ERROR_CHDIR,
1219                            _("Failed to change to directory '%s' (%s)"),
1220                            working_directory,
1221                            g_strerror (buf[1]));
1222
1223               break;
1224               
1225             case CHILD_EXEC_FAILED:
1226               g_set_error (error,
1227                            G_SPAWN_ERROR,
1228                            exec_err_to_g_error (buf[1]),
1229                            _("Failed to execute child process \"%s\" (%s)"),
1230                            argv[0],
1231                            g_strerror (buf[1]));
1232
1233               break;
1234               
1235             case CHILD_DUP2_FAILED:
1236               g_set_error (error,
1237                            G_SPAWN_ERROR,
1238                            G_SPAWN_ERROR_FAILED,
1239                            _("Failed to redirect output or input of child process (%s)"),
1240                            g_strerror (buf[1]));
1241
1242               break;
1243
1244             case CHILD_FORK_FAILED:
1245               g_set_error (error,
1246                            G_SPAWN_ERROR,
1247                            G_SPAWN_ERROR_FORK,
1248                            _("Failed to fork child process (%s)"),
1249                            g_strerror (buf[1]));
1250               break;
1251               
1252             default:
1253               g_set_error (error,
1254                            G_SPAWN_ERROR,
1255                            G_SPAWN_ERROR_FAILED,
1256                            _("Unknown error executing child process \"%s\""),
1257                            argv[0]);
1258               break;
1259             }
1260
1261           goto cleanup_and_fail;
1262         }
1263
1264       /* Get child pid from intermediate child pipe. */
1265       if (intermediate_child)
1266         {
1267           n_ints = 0;
1268           
1269           if (!read_ints (child_pid_report_pipe[0],
1270                           buf, 1, &n_ints, error))
1271             goto cleanup_and_fail;
1272
1273           if (n_ints < 1)
1274             {
1275               g_set_error (error,
1276                            G_SPAWN_ERROR,
1277                            G_SPAWN_ERROR_FAILED,
1278                            _("Failed to read enough data from child pid pipe (%s)"),
1279                            g_strerror (errno));
1280               goto cleanup_and_fail;
1281             }
1282           else
1283             {
1284               /* we have the child pid */
1285               pid = buf[0];
1286             }
1287         }
1288       
1289       /* Success against all odds! return the information */
1290       close_and_invalidate (&child_err_report_pipe[0]);
1291       close_and_invalidate (&child_pid_report_pipe[0]);
1292  
1293       if (child_pid)
1294         *child_pid = pid;
1295
1296       if (standard_input)
1297         *standard_input = stdin_pipe[1];
1298       if (standard_output)
1299         *standard_output = stdout_pipe[0];
1300       if (standard_error)
1301         *standard_error = stderr_pipe[0];
1302       
1303       return TRUE;
1304     }
1305
1306  cleanup_and_fail:
1307
1308   /* There was an error from the Child, reap the child to avoid it being
1309      a zombie.
1310    */
1311
1312   if (pid > 0)
1313   {
1314     wait_failed:
1315      if (waitpid (pid, NULL, 0) < 0)
1316        {
1317           if (errno == EINTR)
1318             goto wait_failed;
1319           else if (errno == ECHILD)
1320             ; /* do nothing, child already reaped */
1321           else
1322             g_warning ("waitpid() should not fail in "
1323                        "'fork_exec_with_pipes'");
1324        }
1325    }
1326
1327   close_and_invalidate (&child_err_report_pipe[0]);
1328   close_and_invalidate (&child_err_report_pipe[1]);
1329   close_and_invalidate (&child_pid_report_pipe[0]);
1330   close_and_invalidate (&child_pid_report_pipe[1]);
1331   close_and_invalidate (&stdin_pipe[0]);
1332   close_and_invalidate (&stdin_pipe[1]);
1333   close_and_invalidate (&stdout_pipe[0]);
1334   close_and_invalidate (&stdout_pipe[1]);
1335   close_and_invalidate (&stderr_pipe[0]);
1336   close_and_invalidate (&stderr_pipe[1]);
1337
1338   return FALSE;
1339 }
1340
1341 static gboolean
1342 make_pipe (gint     p[2],
1343            GError **error)
1344 {
1345   if (pipe (p) < 0)
1346     {
1347       g_set_error (error,
1348                    G_SPAWN_ERROR,
1349                    G_SPAWN_ERROR_FAILED,
1350                    _("Failed to create pipe for communicating with child process (%s)"),
1351                    g_strerror (errno));
1352       return FALSE;
1353     }
1354   else
1355     return TRUE;
1356 }
1357
1358 /* Based on execvp from GNU C Library */
1359
1360 static void
1361 script_execute (const gchar *file,
1362                 gchar      **argv,
1363                 gchar      **envp,
1364                 gboolean     search_path)
1365 {
1366   /* Count the arguments.  */
1367   int argc = 0;
1368   while (argv[argc])
1369     ++argc;
1370   
1371   /* Construct an argument list for the shell.  */
1372   {
1373     gchar **new_argv;
1374
1375     new_argv = g_new0 (gchar*, argc + 2); /* /bin/sh and NULL */
1376     
1377     new_argv[0] = (char *) "/bin/sh";
1378     new_argv[1] = (char *) file;
1379     while (argc > 0)
1380       {
1381         new_argv[argc + 1] = argv[argc];
1382         --argc;
1383       }
1384
1385     /* Execute the shell. */
1386     if (envp)
1387       execve (new_argv[0], new_argv, envp);
1388     else
1389       execv (new_argv[0], new_argv);
1390     
1391     g_free (new_argv);
1392   }
1393 }
1394
1395 static gchar*
1396 my_strchrnul (const gchar *str, gchar c)
1397 {
1398   gchar *p = (gchar*) str;
1399   while (*p && (*p != c))
1400     ++p;
1401
1402   return p;
1403 }
1404
1405 static gint
1406 g_execute (const gchar *file,
1407            gchar      **argv,
1408            gchar      **envp,
1409            gboolean     search_path)
1410 {
1411   if (*file == '\0')
1412     {
1413       /* We check the simple case first. */
1414       errno = ENOENT;
1415       return -1;
1416     }
1417
1418   if (!search_path || strchr (file, '/') != NULL)
1419     {
1420       /* Don't search when it contains a slash. */
1421       if (envp)
1422         execve (file, argv, envp);
1423       else
1424         execv (file, argv);
1425       
1426       if (errno == ENOEXEC)
1427         script_execute (file, argv, envp, FALSE);
1428     }
1429   else
1430     {
1431       gboolean got_eacces = 0;
1432       const gchar *path, *p;
1433       gchar *name, *freeme;
1434       size_t len;
1435       size_t pathlen;
1436
1437       path = g_getenv ("PATH");
1438       if (path == NULL)
1439         {
1440           /* There is no `PATH' in the environment.  The default
1441            * search path in libc is the current directory followed by
1442            * the path `confstr' returns for `_CS_PATH'.
1443            */
1444
1445           /* In GLib we put . last, for security, and don't use the
1446            * unportable confstr(); UNIX98 does not actually specify
1447            * what to search if PATH is unset. POSIX may, dunno.
1448            */
1449           
1450           path = "/bin:/usr/bin:.";
1451         }
1452
1453       len = strlen (file) + 1;
1454       pathlen = strlen (path);
1455       freeme = name = g_malloc (pathlen + len + 1);
1456       
1457       /* Copy the file name at the top, including '\0'  */
1458       memcpy (name + pathlen + 1, file, len);
1459       name = name + pathlen;
1460       /* And add the slash before the filename  */
1461       *name = '/';
1462
1463       p = path;
1464       do
1465         {
1466           char *startp;
1467
1468           path = p;
1469           p = my_strchrnul (path, ':');
1470
1471           if (p == path)
1472             /* Two adjacent colons, or a colon at the beginning or the end
1473              * of `PATH' means to search the current directory.
1474              */
1475             startp = name + 1;
1476           else
1477             startp = memcpy (name - (p - path), path, p - path);
1478
1479           /* Try to execute this name.  If it works, execv will not return.  */
1480           if (envp)
1481             execve (startp, argv, envp);
1482           else
1483             execv (startp, argv);
1484           
1485           if (errno == ENOEXEC)
1486             script_execute (startp, argv, envp, search_path);
1487
1488           switch (errno)
1489             {
1490             case EACCES:
1491               /* Record the we got a `Permission denied' error.  If we end
1492                * up finding no executable we can use, we want to diagnose
1493                * that we did find one but were denied access.
1494                */
1495               got_eacces = TRUE;
1496
1497               /* FALL THRU */
1498               
1499             case ENOENT:
1500 #ifdef ESTALE
1501             case ESTALE:
1502 #endif
1503 #ifdef ENOTDIR
1504             case ENOTDIR:
1505 #endif
1506               /* Those errors indicate the file is missing or not executable
1507                * by us, in which case we want to just try the next path
1508                * directory.
1509                */
1510               break;
1511
1512             default:
1513               /* Some other error means we found an executable file, but
1514                * something went wrong executing it; return the error to our
1515                * caller.
1516                */
1517               g_free (freeme);
1518               return -1;
1519             }
1520         }
1521       while (*p++ != '\0');
1522
1523       /* We tried every element and none of them worked.  */
1524       if (got_eacces)
1525         /* At least one failure was due to permissions, so report that
1526          * error.
1527          */
1528         errno = EACCES;
1529
1530       g_free (freeme);
1531     }
1532
1533   /* Return the error from the last attempt (probably ENOENT).  */
1534   return -1;
1535 }
1536
1537 /**
1538  * g_spawn_close_pid:
1539  * @pid: The process identifier to close
1540  *
1541  * On some platforms, notably WIN32, the #GPid type represents a resource
1542  * which must be closed to prevent resource leaking. g_spawn_close_pid()
1543  * is provided for this purpose. It should be used on all platforms, even
1544  * though it doesn't do anything under UNIX.
1545  **/
1546 void
1547 g_spawn_close_pid (GPid pid)
1548 {
1549 }
1550
1551 #define __G_SPAWN_C__
1552 #include "galiasdef.c"