Apply a patch to fix sparse warnings. (#154696, Kjartan Maraas)
[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 "galias.h"
39 #include "glib.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 recostruction 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 behavior. On Unix, the %G_SPAWN_DO_NOT_REAP_CHILD means
470  * that the child will not be automatically reaped; you must call
471  * waitpid() or handle %SIGCHLD yourself, or the
472  * child will become a zombie. On Windows, the flag means that a
473  * handle to the child will be returned @child_pid. You must call
474  * CloseHandle() on it eventually (or exit the
475  * process), or the child processs will continue to take up some table
476  * space even after its death. Quite similar to zombies on Unix,
477  * actually.
478  *
479  * %G_SPAWN_LEAVE_DESCRIPTORS_OPEN means that the parent's open file
480  * descriptors will be inherited by the child; otherwise all
481  * descriptors except stdin/stdout/stderr will be closed before
482  * calling exec() in the child. %G_SPAWN_SEARCH_PATH 
483  * means that <literal>argv[0]</literal> need not be an absolute path, it
484  * will be looked for in the user's <envar>PATH</envar>. 
485  * %G_SPAWN_STDOUT_TO_DEV_NULL means that the child's standard output will 
486  * be discarded, instead of going to the same location as the parent's 
487  * standard output. If you use this flag, @standard_output must be %NULL.
488  * %G_SPAWN_STDERR_TO_DEV_NULL means that the child's standard error
489  * will be discarded, instead of going to the same location as the parent's
490  * standard error. If you use this flag, @standard_error must be %NULL.
491  * %G_SPAWN_CHILD_INHERITS_STDIN means that the child will inherit the parent's
492  * standard input (by default, the child's standard input is attached to
493  * /dev/null). If you use this flag, @standard_input must be %NULL.
494  * %G_SPAWN_FILE_AND_ARGV_ZERO means that the first element of @argv is
495  * the file to execute, while the remaining elements are the
496  * actual argument vector to pass to the file. Normally
497  * g_spawn_async_with_pipes() uses @argv[0] as the file to execute, and
498  * passes all of @argv to the child.
499  *
500  * @child_setup and @user_data are a function and user data. On POSIX
501  * platforms, the function is called in the child after GLib has
502  * performed all the setup it plans to perform (including creating
503  * pipes, closing file descriptors, etc.) but before calling
504  * exec(). That is, @child_setup is called just
505  * before calling exec() in the child. Obviously
506  * actions taken in this function will only affect the child, not the
507  * parent. On Windows, there is no separate fork() and exec()
508  * functionality. Child processes are created and run right away with
509  * one API call, CreateProcess(). @child_setup is
510  * called in the parent process just before creating the child
511  * process. You should carefully consider what you do in @child_setup
512  * if you intend your software to be portable to Windows.
513  *
514  * If non-%NULL, @child_pid will on Unix be filled with the child's
515  * process ID. You can use the process ID to send signals to the
516  * child, or to waitpid() if you specified the
517  * %G_SPAWN_DO_NOT_REAP_CHILD flag. On Windows, @child_pid will be
518  * filled with a handle to the child process only if you specified the
519  * %G_SPAWN_DO_NOT_REAP_CHILD flag. You can then access the child
520  * process using the Win32 API, for example wait for its termination
521  * with the <function>WaitFor*()</function> functions, or examine its
522  * exit code with GetExitCodeProcess(). You should close the handle 
523  * with CloseHandle() when you no longer need it.
524  *
525  * If non-%NULL, the @standard_input, @standard_output, @standard_error
526  * locations will be filled with file descriptors for writing to the child's
527  * standard input or reading from its standard output or standard error.
528  * The caller of g_spawn_async_with_pipes() must close these file descriptors
529  * when they are no longer in use. If these parameters are %NULL, the corresponding
530  * pipe won't be created.
531  *
532  * If @standard_input is NULL, the child's standard input is attached to /dev/null
533  * unless %G_SPAWN_CHILD_INHERITS_STDIN is set.
534  *
535  * If @standard_error is NULL, the child's standard error goes to the same location
536  * as the parent's standard error unless %G_SPAWN_STDERR_TO_DEV_NULL is set.
537  *
538  * If @standard_output is NULL, the child's standard output goes to the same location
539  * as the parent's standard output unless %G_SPAWN_STDOUT_TO_DEV_NULL is set.
540  *
541  * @error can be %NULL to ignore errors, or non-%NULL to report errors.
542  * If an error is set, the function returns %FALSE. Errors
543  * are reported even if they occur in the child (for example if the
544  * executable in <literal>argv[0]</literal> is not found). Typically
545  * the <literal>message</literal> field of returned errors should be displayed
546  * to users. Possible errors are those from the #G_SPAWN_ERROR domain.
547  *
548  * If an error occurs, @child_pid, @standard_input, @standard_output,
549  * and @standard_error will not be filled with valid values.
550  *
551  * If @child_pid is not %NULL and an error does not occur then the returned
552  * pid must be closed using g_spawn_close_pid().
553  * 
554  * Return value: %TRUE on success, %FALSE if an error was set
555  **/
556 gboolean
557 g_spawn_async_with_pipes (const gchar          *working_directory,
558                           gchar               **argv,
559                           gchar               **envp,
560                           GSpawnFlags           flags,
561                           GSpawnChildSetupFunc  child_setup,
562                           gpointer              user_data,
563                           GPid                 *child_pid,
564                           gint                 *standard_input,
565                           gint                 *standard_output,
566                           gint                 *standard_error,
567                           GError              **error)
568 {
569   g_return_val_if_fail (argv != NULL, FALSE);
570   g_return_val_if_fail (standard_output == NULL ||
571                         !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
572   g_return_val_if_fail (standard_error == NULL ||
573                         !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
574   /* can't inherit stdin if we have an input pipe. */
575   g_return_val_if_fail (standard_input == NULL ||
576                         !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
577   
578   return fork_exec_with_pipes (!(flags & G_SPAWN_DO_NOT_REAP_CHILD),
579                                working_directory,
580                                argv,
581                                envp,
582                                !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
583                                (flags & G_SPAWN_SEARCH_PATH) != 0,
584                                (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
585                                (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
586                                (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
587                                (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
588                                child_setup,
589                                user_data,
590                                child_pid,
591                                standard_input,
592                                standard_output,
593                                standard_error,
594                                error);
595 }
596
597 /**
598  * g_spawn_command_line_sync:
599  * @command_line: a command line 
600  * @standard_output: return location for child output
601  * @standard_error: return location for child errors
602  * @exit_status: return location for child exit status
603  * @error: return location for errors
604  *
605  * A simple version of g_spawn_sync() with little-used parameters
606  * removed, taking a command line instead of an argument vector.  See
607  * g_spawn_sync() for full details. @command_line will be parsed by
608  * g_shell_parse_argv(). Unlike g_spawn_sync(), the %G_SPAWN_SEARCH_PATH flag
609  * is enabled. Note that %G_SPAWN_SEARCH_PATH can have security
610  * implications, so consider using g_spawn_sync() directly if
611  * appropriate. Possible errors are those from g_spawn_sync() and those
612  * from g_shell_parse_argv().
613  * 
614  * On Windows, please note the implications of g_shell_parse_argv()
615  * parsing @command_line. Space is a separator, and backslashes are
616  * special. Thus you cannot simply pass a @command_line containing
617  * canonical Windows paths, like "c:\\program files\\app\\app.exe", as
618  * the backslashes will be eaten, and the space will act as a
619  * separator. You need to enclose such paths with single quotes, like
620  * "'c:\\program files\\app\\app.exe' 'e:\\folder\\argument.txt'".
621  *
622  * Return value: %TRUE on success, %FALSE if an error was set
623  **/
624 gboolean
625 g_spawn_command_line_sync (const gchar  *command_line,
626                            gchar       **standard_output,
627                            gchar       **standard_error,
628                            gint         *exit_status,
629                            GError      **error)
630 {
631   gboolean retval;
632   gchar **argv = NULL;
633
634   g_return_val_if_fail (command_line != NULL, FALSE);
635   
636   if (!g_shell_parse_argv (command_line,
637                            NULL, &argv,
638                            error))
639     return FALSE;
640   
641   retval = g_spawn_sync (NULL,
642                          argv,
643                          NULL,
644                          G_SPAWN_SEARCH_PATH,
645                          NULL,
646                          NULL,
647                          standard_output,
648                          standard_error,
649                          exit_status,
650                          error);
651   g_strfreev (argv);
652
653   return retval;
654 }
655
656 /**
657  * g_spawn_command_line_async:
658  * @command_line: a command line
659  * @error: return location for errors
660  * 
661  * A simple version of g_spawn_async() that parses a command line with
662  * g_shell_parse_argv() and passes it to g_spawn_async(). Runs a
663  * command line in the background. Unlike g_spawn_async(), the
664  * %G_SPAWN_SEARCH_PATH flag is enabled, other flags are not. Note
665  * that %G_SPAWN_SEARCH_PATH can have security implications, so
666  * consider using g_spawn_async() directly if appropriate. Possible
667  * errors are those from g_shell_parse_argv() and g_spawn_async().
668  * 
669  * The same concerns on Windows apply as for g_spawn_command_line_sync().
670  *
671  * Return value: %TRUE on success, %FALSE if error is set.
672  **/
673 gboolean
674 g_spawn_command_line_async (const gchar *command_line,
675                             GError     **error)
676 {
677   gboolean retval;
678   gchar **argv = NULL;
679
680   g_return_val_if_fail (command_line != NULL, FALSE);
681
682   if (!g_shell_parse_argv (command_line,
683                            NULL, &argv,
684                            error))
685     return FALSE;
686   
687   retval = g_spawn_async (NULL,
688                           argv,
689                           NULL,
690                           G_SPAWN_SEARCH_PATH,
691                           NULL,
692                           NULL,
693                           NULL,
694                           error);
695   g_strfreev (argv);
696
697   return retval;
698 }
699
700 static gint
701 exec_err_to_g_error (gint en)
702 {
703   switch (en)
704     {
705 #ifdef EACCES
706     case EACCES:
707       return G_SPAWN_ERROR_ACCES;
708       break;
709 #endif
710
711 #ifdef EPERM
712     case EPERM:
713       return G_SPAWN_ERROR_PERM;
714       break;
715 #endif
716
717 #ifdef E2BIG
718     case E2BIG:
719       return G_SPAWN_ERROR_2BIG;
720       break;
721 #endif
722
723 #ifdef ENOEXEC
724     case ENOEXEC:
725       return G_SPAWN_ERROR_NOEXEC;
726       break;
727 #endif
728
729 #ifdef ENAMETOOLONG
730     case ENAMETOOLONG:
731       return G_SPAWN_ERROR_NAMETOOLONG;
732       break;
733 #endif
734
735 #ifdef ENOENT
736     case ENOENT:
737       return G_SPAWN_ERROR_NOENT;
738       break;
739 #endif
740
741 #ifdef ENOMEM
742     case ENOMEM:
743       return G_SPAWN_ERROR_NOMEM;
744       break;
745 #endif
746
747 #ifdef ENOTDIR
748     case ENOTDIR:
749       return G_SPAWN_ERROR_NOTDIR;
750       break;
751 #endif
752
753 #ifdef ELOOP
754     case ELOOP:
755       return G_SPAWN_ERROR_LOOP;
756       break;
757 #endif
758       
759 #ifdef ETXTBUSY
760     case ETXTBUSY:
761       return G_SPAWN_ERROR_TXTBUSY;
762       break;
763 #endif
764
765 #ifdef EIO
766     case EIO:
767       return G_SPAWN_ERROR_IO;
768       break;
769 #endif
770
771 #ifdef ENFILE
772     case ENFILE:
773       return G_SPAWN_ERROR_NFILE;
774       break;
775 #endif
776
777 #ifdef EMFILE
778     case EMFILE:
779       return G_SPAWN_ERROR_MFILE;
780       break;
781 #endif
782
783 #ifdef EINVAL
784     case EINVAL:
785       return G_SPAWN_ERROR_INVAL;
786       break;
787 #endif
788
789 #ifdef EISDIR
790     case EISDIR:
791       return G_SPAWN_ERROR_ISDIR;
792       break;
793 #endif
794
795 #ifdef ELIBBAD
796     case ELIBBAD:
797       return G_SPAWN_ERROR_LIBBAD;
798       break;
799 #endif
800       
801     default:
802       return G_SPAWN_ERROR_FAILED;
803       break;
804     }
805 }
806
807 static gssize
808 write_all (gint fd, gconstpointer vbuf, gsize to_write)
809 {
810   gchar *buf = (gchar *) vbuf;
811   
812   while (to_write > 0)
813     {
814       gssize count = write (fd, buf, to_write);
815       if (count < 0)
816         {
817           if (errno != EINTR)
818             return FALSE;
819         }
820       else
821         {
822           to_write -= count;
823           buf += count;
824         }
825     }
826   
827   return TRUE;
828 }
829
830 static void
831 write_err_and_exit (gint fd, gint msg)
832 {
833   gint en = errno;
834   
835   write_all (fd, &msg, sizeof(msg));
836   write_all (fd, &en, sizeof(en));
837   
838   _exit (1);
839 }
840
841 static void
842 set_cloexec (gint fd)
843 {
844   fcntl (fd, F_SETFD, FD_CLOEXEC);
845 }
846
847 static gint
848 sane_dup2 (gint fd1, gint fd2)
849 {
850   gint ret;
851
852  retry:
853   ret = dup2 (fd1, fd2);
854   if (ret < 0 && errno == EINTR)
855     goto retry;
856
857   return ret;
858 }
859
860 enum
861 {
862   CHILD_CHDIR_FAILED,
863   CHILD_EXEC_FAILED,
864   CHILD_DUP2_FAILED,
865   CHILD_FORK_FAILED
866 };
867
868 static void
869 do_exec (gint                  child_err_report_fd,
870          gint                  stdin_fd,
871          gint                  stdout_fd,
872          gint                  stderr_fd,
873          const gchar          *working_directory,
874          gchar               **argv,
875          gchar               **envp,
876          gboolean              close_descriptors,
877          gboolean              search_path,
878          gboolean              stdout_to_null,
879          gboolean              stderr_to_null,
880          gboolean              child_inherits_stdin,
881          gboolean              file_and_argv_zero,
882          GSpawnChildSetupFunc  child_setup,
883          gpointer              user_data)
884 {
885   if (working_directory && chdir (working_directory) < 0)
886     write_err_and_exit (child_err_report_fd,
887                         CHILD_CHDIR_FAILED);
888
889   /* Close all file descriptors but stdin stdout and stderr as
890    * soon as we exec. Note that this includes
891    * child_err_report_fd, which keeps the parent from blocking
892    * forever on the other end of that pipe.
893    */
894   if (close_descriptors)
895     {
896       gint open_max;
897       gint i;
898       
899       open_max = sysconf (_SC_OPEN_MAX);
900       for (i = 3; i < open_max; i++)
901         set_cloexec (i);
902     }
903   else
904     {
905       /* We need to do child_err_report_fd anyway */
906       set_cloexec (child_err_report_fd);
907     }
908   
909   /* Redirect pipes as required */
910   
911   if (stdin_fd >= 0)
912     {
913       /* dup2 can't actually fail here I don't think */
914           
915       if (sane_dup2 (stdin_fd, 0) < 0)
916         write_err_and_exit (child_err_report_fd,
917                             CHILD_DUP2_FAILED);
918
919       /* ignore this if it doesn't work */
920       close_and_invalidate (&stdin_fd);
921     }
922   else if (!child_inherits_stdin)
923     {
924       /* Keep process from blocking on a read of stdin */
925       gint read_null = open ("/dev/null", O_RDONLY);
926       sane_dup2 (read_null, 0);
927       close_and_invalidate (&read_null);
928     }
929
930   if (stdout_fd >= 0)
931     {
932       /* dup2 can't actually fail here I don't think */
933           
934       if (sane_dup2 (stdout_fd, 1) < 0)
935         write_err_and_exit (child_err_report_fd,
936                             CHILD_DUP2_FAILED);
937
938       /* ignore this if it doesn't work */
939       close_and_invalidate (&stdout_fd);
940     }
941   else if (stdout_to_null)
942     {
943       gint write_null = open ("/dev/null", O_WRONLY);
944       sane_dup2 (write_null, 1);
945       close_and_invalidate (&write_null);
946     }
947
948   if (stderr_fd >= 0)
949     {
950       /* dup2 can't actually fail here I don't think */
951           
952       if (sane_dup2 (stderr_fd, 2) < 0)
953         write_err_and_exit (child_err_report_fd,
954                             CHILD_DUP2_FAILED);
955
956       /* ignore this if it doesn't work */
957       close_and_invalidate (&stderr_fd);
958     }
959   else if (stderr_to_null)
960     {
961       gint write_null = open ("/dev/null", O_WRONLY);
962       sane_dup2 (write_null, 2);
963       close_and_invalidate (&write_null);
964     }
965   
966   /* Call user function just before we exec */
967   if (child_setup)
968     {
969       (* child_setup) (user_data);
970     }
971
972   g_execute (argv[0],
973              file_and_argv_zero ? argv + 1 : argv,
974              envp, search_path);
975
976   /* Exec failed */
977   write_err_and_exit (child_err_report_fd,
978                       CHILD_EXEC_FAILED);
979 }
980
981 static gboolean
982 read_ints (int      fd,
983            gint*    buf,
984            gint     n_ints_in_buf,    
985            gint    *n_ints_read,      
986            GError **error)
987 {
988   gsize bytes = 0;    
989   
990   while (TRUE)
991     {
992       gssize chunk;    
993
994       if (bytes >= sizeof(gint)*2)
995         break; /* give up, who knows what happened, should not be
996                 * possible.
997                 */
998           
999     again:
1000       chunk = read (fd,
1001                     ((gchar*)buf) + bytes,
1002                     sizeof(gint) * n_ints_in_buf - bytes);
1003       if (chunk < 0 && errno == EINTR)
1004         goto again;
1005           
1006       if (chunk < 0)
1007         {
1008           /* Some weird shit happened, bail out */
1009               
1010           g_set_error (error,
1011                        G_SPAWN_ERROR,
1012                        G_SPAWN_ERROR_FAILED,
1013                        _("Failed to read from child pipe (%s)"),
1014                        g_strerror (errno));
1015
1016           return FALSE;
1017         }
1018       else if (chunk == 0)
1019         break; /* EOF */
1020       else /* chunk > 0 */
1021         bytes += chunk;
1022     }
1023
1024   *n_ints_read = (gint)(bytes / sizeof(gint));
1025
1026   return TRUE;
1027 }
1028
1029 static gboolean
1030 fork_exec_with_pipes (gboolean              intermediate_child,
1031                       const gchar          *working_directory,
1032                       gchar               **argv,
1033                       gchar               **envp,
1034                       gboolean              close_descriptors,
1035                       gboolean              search_path,
1036                       gboolean              stdout_to_null,
1037                       gboolean              stderr_to_null,
1038                       gboolean              child_inherits_stdin,
1039                       gboolean              file_and_argv_zero,
1040                       GSpawnChildSetupFunc  child_setup,
1041                       gpointer              user_data,
1042                       GPid                 *child_pid,
1043                       gint                 *standard_input,
1044                       gint                 *standard_output,
1045                       gint                 *standard_error,
1046                       GError              **error)     
1047 {
1048   GPid pid = -1;
1049   gint stdin_pipe[2] = { -1, -1 };
1050   gint stdout_pipe[2] = { -1, -1 };
1051   gint stderr_pipe[2] = { -1, -1 };
1052   gint child_err_report_pipe[2] = { -1, -1 };
1053   gint child_pid_report_pipe[2] = { -1, -1 };
1054   gint status;
1055   
1056   if (!make_pipe (child_err_report_pipe, error))
1057     return FALSE;
1058
1059   if (intermediate_child && !make_pipe (child_pid_report_pipe, error))
1060     goto cleanup_and_fail;
1061   
1062   if (standard_input && !make_pipe (stdin_pipe, error))
1063     goto cleanup_and_fail;
1064   
1065   if (standard_output && !make_pipe (stdout_pipe, error))
1066     goto cleanup_and_fail;
1067
1068   if (standard_error && !make_pipe (stderr_pipe, error))
1069     goto cleanup_and_fail;
1070
1071   pid = FORK1 ();
1072
1073   if (pid < 0)
1074     {      
1075       g_set_error (error,
1076                    G_SPAWN_ERROR,
1077                    G_SPAWN_ERROR_FORK,
1078                    _("Failed to fork (%s)"),
1079                    g_strerror (errno));
1080
1081       goto cleanup_and_fail;
1082     }
1083   else if (pid == 0)
1084     {
1085       /* Immediate child. This may or may not be the child that
1086        * actually execs the new process.
1087        */
1088       
1089       /* Be sure we crash if the parent exits
1090        * and we write to the err_report_pipe
1091        */
1092       signal (SIGPIPE, SIG_DFL);
1093
1094       /* Close the parent's end of the pipes;
1095        * not needed in the close_descriptors case,
1096        * though
1097        */
1098       close_and_invalidate (&child_err_report_pipe[0]);
1099       close_and_invalidate (&child_pid_report_pipe[0]);
1100       close_and_invalidate (&stdin_pipe[1]);
1101       close_and_invalidate (&stdout_pipe[0]);
1102       close_and_invalidate (&stderr_pipe[0]);
1103       
1104       if (intermediate_child)
1105         {
1106           /* We need to fork an intermediate child that launches the
1107            * final child. The purpose of the intermediate child
1108            * is to exit, so we can waitpid() it immediately.
1109            * Then the grandchild will not become a zombie.
1110            */
1111           GPid grandchild_pid;
1112
1113           grandchild_pid = FORK1 ();
1114
1115           if (grandchild_pid < 0)
1116             {
1117               /* report -1 as child PID */
1118               write_all (child_pid_report_pipe[1], &grandchild_pid,
1119                          sizeof(grandchild_pid));
1120               
1121               write_err_and_exit (child_err_report_pipe[1],
1122                                   CHILD_FORK_FAILED);              
1123             }
1124           else if (grandchild_pid == 0)
1125             {
1126               do_exec (child_err_report_pipe[1],
1127                        stdin_pipe[0],
1128                        stdout_pipe[1],
1129                        stderr_pipe[1],
1130                        working_directory,
1131                        argv,
1132                        envp,
1133                        close_descriptors,
1134                        search_path,
1135                        stdout_to_null,
1136                        stderr_to_null,
1137                        child_inherits_stdin,
1138                        file_and_argv_zero,
1139                        child_setup,
1140                        user_data);
1141             }
1142           else
1143             {
1144               write_all (child_pid_report_pipe[1], &grandchild_pid, sizeof(grandchild_pid));
1145               close_and_invalidate (&child_pid_report_pipe[1]);
1146               
1147               _exit (0);
1148             }
1149         }
1150       else
1151         {
1152           /* Just run the child.
1153            */
1154
1155           do_exec (child_err_report_pipe[1],
1156                    stdin_pipe[0],
1157                    stdout_pipe[1],
1158                    stderr_pipe[1],
1159                    working_directory,
1160                    argv,
1161                    envp,
1162                    close_descriptors,
1163                    search_path,
1164                    stdout_to_null,
1165                    stderr_to_null,
1166                    child_inherits_stdin,
1167                    file_and_argv_zero,
1168                    child_setup,
1169                    user_data);
1170         }
1171     }
1172   else
1173     {
1174       /* Parent */
1175       
1176       gint buf[2];
1177       gint n_ints = 0;    
1178
1179       /* Close the uncared-about ends of the pipes */
1180       close_and_invalidate (&child_err_report_pipe[1]);
1181       close_and_invalidate (&child_pid_report_pipe[1]);
1182       close_and_invalidate (&stdin_pipe[0]);
1183       close_and_invalidate (&stdout_pipe[1]);
1184       close_and_invalidate (&stderr_pipe[1]);
1185
1186       /* If we had an intermediate child, reap it */
1187       if (intermediate_child)
1188         {
1189         wait_again:
1190           if (waitpid (pid, &status, 0) < 0)
1191             {
1192               if (errno == EINTR)
1193                 goto wait_again;
1194               else if (errno == ECHILD)
1195                 ; /* do nothing, child already reaped */
1196               else
1197                 g_warning ("waitpid() should not fail in "
1198                            "'fork_exec_with_pipes'");
1199             }
1200         }
1201       
1202
1203       if (!read_ints (child_err_report_pipe[0],
1204                       buf, 2, &n_ints,
1205                       error))
1206         goto cleanup_and_fail;
1207         
1208       if (n_ints >= 2)
1209         {
1210           /* Error from the child. */
1211
1212           switch (buf[0])
1213             {
1214             case CHILD_CHDIR_FAILED:
1215               g_set_error (error,
1216                            G_SPAWN_ERROR,
1217                            G_SPAWN_ERROR_CHDIR,
1218                            _("Failed to change to directory '%s' (%s)"),
1219                            working_directory,
1220                            g_strerror (buf[1]));
1221
1222               break;
1223               
1224             case CHILD_EXEC_FAILED:
1225               g_set_error (error,
1226                            G_SPAWN_ERROR,
1227                            exec_err_to_g_error (buf[1]),
1228                            _("Failed to execute child process \"%s\" (%s)"),
1229                            argv[0],
1230                            g_strerror (buf[1]));
1231
1232               break;
1233               
1234             case CHILD_DUP2_FAILED:
1235               g_set_error (error,
1236                            G_SPAWN_ERROR,
1237                            G_SPAWN_ERROR_FAILED,
1238                            _("Failed to redirect output or input of child process (%s)"),
1239                            g_strerror (buf[1]));
1240
1241               break;
1242
1243             case CHILD_FORK_FAILED:
1244               g_set_error (error,
1245                            G_SPAWN_ERROR,
1246                            G_SPAWN_ERROR_FORK,
1247                            _("Failed to fork child process (%s)"),
1248                            g_strerror (buf[1]));
1249               break;
1250               
1251             default:
1252               g_set_error (error,
1253                            G_SPAWN_ERROR,
1254                            G_SPAWN_ERROR_FAILED,
1255                            _("Unknown error executing child process \"%s\""),
1256                            argv[0]);
1257               break;
1258             }
1259
1260           goto cleanup_and_fail;
1261         }
1262
1263       /* Get child pid from intermediate child pipe. */
1264       if (intermediate_child)
1265         {
1266           n_ints = 0;
1267           
1268           if (!read_ints (child_pid_report_pipe[0],
1269                           buf, 1, &n_ints, error))
1270             goto cleanup_and_fail;
1271
1272           if (n_ints < 1)
1273             {
1274               g_set_error (error,
1275                            G_SPAWN_ERROR,
1276                            G_SPAWN_ERROR_FAILED,
1277                            _("Failed to read enough data from child pid pipe (%s)"),
1278                            g_strerror (errno));
1279               goto cleanup_and_fail;
1280             }
1281           else
1282             {
1283               /* we have the child pid */
1284               pid = buf[0];
1285             }
1286         }
1287       
1288       /* Success against all odds! return the information */
1289       close_and_invalidate (&child_err_report_pipe[0]);
1290       close_and_invalidate (&child_pid_report_pipe[0]);
1291  
1292       if (child_pid)
1293         *child_pid = pid;
1294
1295       if (standard_input)
1296         *standard_input = stdin_pipe[1];
1297       if (standard_output)
1298         *standard_output = stdout_pipe[0];
1299       if (standard_error)
1300         *standard_error = stderr_pipe[0];
1301       
1302       return TRUE;
1303     }
1304
1305  cleanup_and_fail:
1306
1307   /* There was an error from the Child, reap the child to avoid it being
1308      a zombie.
1309    */
1310
1311   if (pid > 0)
1312   {
1313     wait_failed:
1314      if (waitpid (pid, NULL, 0) < 0)
1315        {
1316           if (errno == EINTR)
1317             goto wait_failed;
1318           else if (errno == ECHILD)
1319             ; /* do nothing, child already reaped */
1320           else
1321             g_warning ("waitpid() should not fail in "
1322                        "'fork_exec_with_pipes'");
1323        }
1324    }
1325
1326   close_and_invalidate (&child_err_report_pipe[0]);
1327   close_and_invalidate (&child_err_report_pipe[1]);
1328   close_and_invalidate (&child_pid_report_pipe[0]);
1329   close_and_invalidate (&child_pid_report_pipe[1]);
1330   close_and_invalidate (&stdin_pipe[0]);
1331   close_and_invalidate (&stdin_pipe[1]);
1332   close_and_invalidate (&stdout_pipe[0]);
1333   close_and_invalidate (&stdout_pipe[1]);
1334   close_and_invalidate (&stderr_pipe[0]);
1335   close_and_invalidate (&stderr_pipe[1]);
1336
1337   return FALSE;
1338 }
1339
1340 static gboolean
1341 make_pipe (gint     p[2],
1342            GError **error)
1343 {
1344   if (pipe (p) < 0)
1345     {
1346       g_set_error (error,
1347                    G_SPAWN_ERROR,
1348                    G_SPAWN_ERROR_FAILED,
1349                    _("Failed to create pipe for communicating with child process (%s)"),
1350                    g_strerror (errno));
1351       return FALSE;
1352     }
1353   else
1354     return TRUE;
1355 }
1356
1357 /* Based on execvp from GNU C Library */
1358
1359 static void
1360 script_execute (const gchar *file,
1361                 gchar      **argv,
1362                 gchar      **envp,
1363                 gboolean     search_path)
1364 {
1365   /* Count the arguments.  */
1366   int argc = 0;
1367   while (argv[argc])
1368     ++argc;
1369   
1370   /* Construct an argument list for the shell.  */
1371   {
1372     gchar **new_argv;
1373
1374     new_argv = g_new0 (gchar*, argc + 2); /* /bin/sh and NULL */
1375     
1376     new_argv[0] = (char *) "/bin/sh";
1377     new_argv[1] = (char *) file;
1378     while (argc > 0)
1379       {
1380         new_argv[argc + 1] = argv[argc];
1381         --argc;
1382       }
1383
1384     /* Execute the shell. */
1385     if (envp)
1386       execve (new_argv[0], new_argv, envp);
1387     else
1388       execv (new_argv[0], new_argv);
1389     
1390     g_free (new_argv);
1391   }
1392 }
1393
1394 static gchar*
1395 my_strchrnul (const gchar *str, gchar c)
1396 {
1397   gchar *p = (gchar*) str;
1398   while (*p && (*p != c))
1399     ++p;
1400
1401   return p;
1402 }
1403
1404 static gint
1405 g_execute (const gchar *file,
1406            gchar      **argv,
1407            gchar      **envp,
1408            gboolean     search_path)
1409 {
1410   if (*file == '\0')
1411     {
1412       /* We check the simple case first. */
1413       errno = ENOENT;
1414       return -1;
1415     }
1416
1417   if (!search_path || strchr (file, '/') != NULL)
1418     {
1419       /* Don't search when it contains a slash. */
1420       if (envp)
1421         execve (file, argv, envp);
1422       else
1423         execv (file, argv);
1424       
1425       if (errno == ENOEXEC)
1426         script_execute (file, argv, envp, FALSE);
1427     }
1428   else
1429     {
1430       gboolean got_eacces = 0;
1431       const gchar *path, *p;
1432       gchar *name, *freeme;
1433       size_t len;
1434       size_t pathlen;
1435
1436       path = g_getenv ("PATH");
1437       if (path == NULL)
1438         {
1439           /* There is no `PATH' in the environment.  The default
1440            * search path in libc is the current directory followed by
1441            * the path `confstr' returns for `_CS_PATH'.
1442            */
1443
1444           /* In GLib we put . last, for security, and don't use the
1445            * unportable confstr(); UNIX98 does not actually specify
1446            * what to search if PATH is unset. POSIX may, dunno.
1447            */
1448           
1449           path = "/bin:/usr/bin:.";
1450         }
1451
1452       len = strlen (file) + 1;
1453       pathlen = strlen (path);
1454       freeme = name = g_malloc (pathlen + len + 1);
1455       
1456       /* Copy the file name at the top, including '\0'  */
1457       memcpy (name + pathlen + 1, file, len);
1458       name = name + pathlen;
1459       /* And add the slash before the filename  */
1460       *name = '/';
1461
1462       p = path;
1463       do
1464         {
1465           char *startp;
1466
1467           path = p;
1468           p = my_strchrnul (path, ':');
1469
1470           if (p == path)
1471             /* Two adjacent colons, or a colon at the beginning or the end
1472              * of `PATH' means to search the current directory.
1473              */
1474             startp = name + 1;
1475           else
1476             startp = memcpy (name - (p - path), path, p - path);
1477
1478           /* Try to execute this name.  If it works, execv will not return.  */
1479           if (envp)
1480             execve (startp, argv, envp);
1481           else
1482             execv (startp, argv);
1483           
1484           if (errno == ENOEXEC)
1485             script_execute (startp, argv, envp, search_path);
1486
1487           switch (errno)
1488             {
1489             case EACCES:
1490               /* Record the we got a `Permission denied' error.  If we end
1491                * up finding no executable we can use, we want to diagnose
1492                * that we did find one but were denied access.
1493                */
1494               got_eacces = TRUE;
1495
1496               /* FALL THRU */
1497               
1498             case ENOENT:
1499 #ifdef ESTALE
1500             case ESTALE:
1501 #endif
1502 #ifdef ENOTDIR
1503             case ENOTDIR:
1504 #endif
1505               /* Those errors indicate the file is missing or not executable
1506                * by us, in which case we want to just try the next path
1507                * directory.
1508                */
1509               break;
1510
1511             default:
1512               /* Some other error means we found an executable file, but
1513                * something went wrong executing it; return the error to our
1514                * caller.
1515                */
1516               g_free (freeme);
1517               return -1;
1518             }
1519         }
1520       while (*p++ != '\0');
1521
1522       /* We tried every element and none of them worked.  */
1523       if (got_eacces)
1524         /* At least one failure was due to permissions, so report that
1525          * error.
1526          */
1527         errno = EACCES;
1528
1529       g_free (freeme);
1530     }
1531
1532   /* Return the error from the last attempt (probably ENOENT).  */
1533   return -1;
1534 }
1535
1536 /**
1537  * g_spawn_close_pid:
1538  * @pid: The process identifier to close
1539  *
1540  * On some platforms, notably WIN32, the #GPid type represents a resource
1541  * which must be closed to prevent resource leaking. g_spawn_close_pid()
1542  * is provided for this purpose. It should be used on all platforms, even
1543  * though it doesn't do anything under UNIX.
1544  **/
1545 void
1546 g_spawn_close_pid (GPid pid)
1547 {
1548 }