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