• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* gspawn-win32.c - Process launching on Win32
2  *
3  *  Copyright 2000 Red Hat, Inc.
4  *  Copyright 2003 Tor Lillqvist
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 /*
21  * Implementation details on Win32.
22  *
23  * - There is no way to set the no-inherit flag for
24  *   a "file descriptor" in the MS C runtime. The flag is there,
25  *   and the dospawn() function uses it, but unfortunately
26  *   this flag can only be set when opening the file.
27  * - As there is no fork(), we cannot reliably change directory
28  *   before starting the child process. (There might be several threads
29  *   running, and the current directory is common for all threads.)
30  *
31  * Thus, we must in many cases use a helper program to handle closing
32  * of (inherited) file descriptors and changing of directory. The
33  * helper process is also needed if the standard input, standard
34  * output, or standard error of the process to be run are supposed to
35  * be redirected somewhere.
36  *
37  * The structure of the source code in this file is a mess, I know.
38  */
39 
40 /* Define this to get some logging all the time */
41 /* #define G_SPAWN_WIN32_DEBUG */
42 
43 #include "config.h"
44 
45 #include "glib.h"
46 #include "glib-private.h"
47 #include "gprintfint.h"
48 #include "glibintl.h"
49 #include "gspawn-private.h"
50 #include "gthread.h"
51 
52 #include <string.h>
53 #include <stdlib.h>
54 #include <stdio.h>
55 
56 #include <windows.h>
57 #include <errno.h>
58 #include <fcntl.h>
59 #include <io.h>
60 #include <process.h>
61 #include <direct.h>
62 #include <wchar.h>
63 
64 #ifndef GSPAWN_HELPER
65 #ifdef G_SPAWN_WIN32_DEBUG
66   static int debug = 1;
67   #define SETUP_DEBUG() /* empty */
68 #else
69   static int debug = -1;
70   #define SETUP_DEBUG()					\
71     G_STMT_START					\
72       {							\
73 	if (debug == -1)				\
74 	  {						\
75 	    if (g_getenv ("G_SPAWN_WIN32_DEBUG") != NULL)	\
76 	      debug = 1;				\
77 	    else					\
78 	      debug = 0;				\
79 	  }						\
80       }							\
81     G_STMT_END
82 #endif
83 #endif
84 
85 enum
86 {
87   CHILD_NO_ERROR,
88   CHILD_CHDIR_FAILED,
89   CHILD_SPAWN_FAILED,
90   CHILD_SPAWN_NOENT,
91 };
92 
93 enum {
94   ARG_CHILD_ERR_REPORT = 1,
95   ARG_HELPER_SYNC,
96   ARG_STDIN,
97   ARG_STDOUT,
98   ARG_STDERR,
99   ARG_WORKING_DIRECTORY,
100   ARG_CLOSE_DESCRIPTORS,
101   ARG_USE_PATH,
102   ARG_WAIT,
103   ARG_PROGRAM,
104   ARG_COUNT = ARG_PROGRAM
105 };
106 
107 static int
reopen_noninherited(int fd,int mode)108 reopen_noninherited (int fd,
109 		     int mode)
110 {
111   HANDLE filehandle;
112 
113   DuplicateHandle (GetCurrentProcess (), (LPHANDLE) _get_osfhandle (fd),
114 		   GetCurrentProcess (), &filehandle,
115 		   0, FALSE, DUPLICATE_SAME_ACCESS);
116   close (fd);
117   return _open_osfhandle ((gintptr) filehandle, mode | _O_NOINHERIT);
118 }
119 
120 #ifndef GSPAWN_HELPER
121 
122 #ifdef _WIN64
123 #define HELPER_PROCESS "gspawn-win64-helper"
124 #else
125 #define HELPER_PROCESS "gspawn-win32-helper"
126 #endif
127 
128 /* This logic has a copy for wchar_t in gspawn-win32-helper.c, protect_wargv() */
129 static gchar *
protect_argv_string(const gchar * string)130 protect_argv_string (const gchar *string)
131 {
132   const gchar *p = string;
133   gchar *retval, *q;
134   gint len = 0;
135   gint pre_bslash = 0;
136   gboolean need_dblquotes = FALSE;
137   while (*p)
138     {
139       if (*p == ' ' || *p == '\t')
140 	need_dblquotes = TRUE;
141       /* estimate max len, assuming that all escapable characters will be escaped */
142       if (*p == '"' || *p == '\\')
143 	len += 2;
144       else
145 	len += 1;
146       p++;
147     }
148 
149   q = retval = g_malloc (len + need_dblquotes*2 + 1);
150   p = string;
151 
152   if (need_dblquotes)
153     *q++ = '"';
154   /* Only quotes and backslashes preceding quotes are escaped:
155    * see "Parsing C Command-Line Arguments" at
156    * https://docs.microsoft.com/en-us/cpp/c-language/parsing-c-command-line-arguments
157    */
158   while (*p)
159     {
160       if (*p == '"')
161 	{
162 	  /* Add backslash for escaping quote itself */
163 	  *q++ = '\\';
164 	  /* Add backslash for every preceding backslash for escaping it */
165 	  for (;pre_bslash > 0; --pre_bslash)
166 	    *q++ = '\\';
167 	}
168 
169       /* Count length of continuous sequence of preceding backslashes. */
170       if (*p == '\\')
171 	++pre_bslash;
172       else
173 	pre_bslash = 0;
174 
175       *q++ = *p;
176       p++;
177     }
178 
179   if (need_dblquotes)
180     {
181       /* Add backslash for every preceding backslash for escaping it,
182        * do NOT escape quote itself.
183        */
184       for (;pre_bslash > 0; --pre_bslash)
185 	*q++ = '\\';
186       *q++ = '"';
187     }
188   *q++ = '\0';
189 
190   return retval;
191 }
192 
193 static gint
protect_argv(const gchar * const * argv,gchar *** new_argv)194 protect_argv (const gchar * const   *argv,
195               gchar               ***new_argv)
196 {
197   gint i;
198   gint argc = 0;
199 
200   while (argv[argc])
201     ++argc;
202   *new_argv = g_new (gchar *, argc+1);
203 
204   /* Quote each argv element if necessary, so that it will get
205    * reconstructed correctly in the C runtime startup code.  Note that
206    * the unquoting algorithm in the C runtime is really weird, and
207    * rather different than what Unix shells do. See stdargv.c in the C
208    * runtime sources (in the Platform SDK, in src/crt).
209    *
210    * Note that a new_argv[0] constructed by this function should
211    * *not* be passed as the filename argument to a spawn* or exec*
212    * family function. That argument should be the real file name
213    * without any quoting.
214    */
215   for (i = 0; i < argc; i++)
216     (*new_argv)[i] = protect_argv_string (argv[i]);
217 
218   (*new_argv)[argc] = NULL;
219 
220   return argc;
221 }
222 
223 G_DEFINE_QUARK (g-exec-error-quark, g_spawn_error)
224 G_DEFINE_QUARK (g-spawn-exit-error-quark, g_spawn_exit_error)
225 
226 gboolean
g_spawn_async(const gchar * working_directory,gchar ** argv,gchar ** envp,GSpawnFlags flags,GSpawnChildSetupFunc child_setup,gpointer user_data,GPid * child_pid,GError ** error)227 g_spawn_async (const gchar          *working_directory,
228                gchar               **argv,
229                gchar               **envp,
230                GSpawnFlags           flags,
231                GSpawnChildSetupFunc  child_setup,
232                gpointer              user_data,
233                GPid                 *child_pid,
234                GError              **error)
235 {
236   g_return_val_if_fail (argv != NULL, FALSE);
237 
238   return g_spawn_async_with_pipes (working_directory,
239                                    argv, envp,
240                                    flags,
241                                    child_setup,
242                                    user_data,
243                                    child_pid,
244                                    NULL, NULL, NULL,
245                                    error);
246 }
247 
248 /* Avoids a danger in threaded situations (calling close()
249  * on a file descriptor twice, and another thread has
250  * re-opened it since the first close)
251  */
252 static void
close_and_invalidate(gint * fd)253 close_and_invalidate (gint *fd)
254 {
255   if (*fd < 0)
256     return;
257 
258   close (*fd);
259   *fd = -1;
260 }
261 
262 typedef enum
263 {
264   READ_FAILED = 0, /* FALSE */
265   READ_OK,
266   READ_EOF
267 } ReadResult;
268 
269 static ReadResult
read_data(GString * str,GIOChannel * iochannel,GError ** error)270 read_data (GString     *str,
271            GIOChannel  *iochannel,
272            GError     **error)
273 {
274   GIOStatus giostatus;
275   gsize bytes;
276   gchar buf[4096];
277 
278  again:
279 
280   giostatus = g_io_channel_read_chars (iochannel, buf, sizeof (buf), &bytes, NULL);
281 
282   if (bytes == 0)
283     return READ_EOF;
284   else if (bytes > 0)
285     {
286       g_string_append_len (str, buf, bytes);
287       return READ_OK;
288     }
289   else if (giostatus == G_IO_STATUS_AGAIN)
290     goto again;
291   else if (giostatus == G_IO_STATUS_ERROR)
292     {
293       g_set_error_literal (error, G_SPAWN_ERROR, G_SPAWN_ERROR_READ,
294                            _("Failed to read data from child process"));
295 
296       return READ_FAILED;
297     }
298   else
299     return READ_OK;
300 }
301 
302 static gboolean
make_pipe(gint p[2],GError ** error)303 make_pipe (gint     p[2],
304            GError **error)
305 {
306   if (_pipe (p, 4096, _O_BINARY) < 0)
307     {
308       int errsv = errno;
309 
310       g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
311                    _("Failed to create pipe for communicating with child process (%s)"),
312                    g_strerror (errsv));
313       return FALSE;
314     }
315   else
316     return TRUE;
317 }
318 
319 /* The helper process writes a status report back to us, through a
320  * pipe, consisting of two ints.
321  */
322 static gboolean
read_helper_report(int fd,gintptr report[2],GError ** error)323 read_helper_report (int      fd,
324 		    gintptr  report[2],
325 		    GError **error)
326 {
327   gint bytes = 0;
328 
329   while (bytes < sizeof(gintptr)*2)
330     {
331       gint chunk;
332       int errsv;
333 
334       if (debug)
335 	g_print ("%s:read_helper_report: read %" G_GSIZE_FORMAT "...\n",
336 		 __FILE__,
337 		 sizeof(gintptr)*2 - bytes);
338 
339       chunk = read (fd, ((gchar*)report) + bytes,
340 		    sizeof(gintptr)*2 - bytes);
341       errsv = errno;
342 
343       if (debug)
344 	g_print ("...got %d bytes\n", chunk);
345 
346       if (chunk < 0)
347         {
348           /* Some weird shit happened, bail out */
349           g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
350                        _("Failed to read from child pipe (%s)"),
351                        g_strerror (errsv));
352 
353           return FALSE;
354         }
355       else if (chunk == 0)
356 	{
357 	  g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
358 		       _("Failed to read from child pipe (%s)"),
359 		       "EOF");
360 	  break; /* EOF */
361 	}
362       else
363 	bytes += chunk;
364     }
365 
366   if (bytes < sizeof(gintptr)*2)
367     return FALSE;
368 
369   return TRUE;
370 }
371 
372 static void
set_child_error(gintptr report[2],const gchar * working_directory,GError ** error)373 set_child_error (gintptr      report[2],
374 		 const gchar *working_directory,
375 		 GError     **error)
376 {
377   switch (report[0])
378     {
379     case CHILD_CHDIR_FAILED:
380       g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
381 		   _("Failed to change to directory “%s” (%s)"),
382 		   working_directory,
383 		   g_strerror (report[1]));
384       break;
385     case CHILD_SPAWN_FAILED:
386       g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
387 		   _("Failed to execute child process (%s)"),
388 		   g_strerror (report[1]));
389       break;
390     case CHILD_SPAWN_NOENT:
391       g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_NOENT,
392                    _("Failed to execute child process (%s)"),
393                    g_strerror (report[1]));
394       break;
395     default:
396       g_assert_not_reached ();
397     }
398 }
399 
400 static gboolean
utf8_charv_to_wcharv(const gchar * const * utf8_charv,wchar_t *** wcharv,int * error_index,GError ** error)401 utf8_charv_to_wcharv (const gchar * const   *utf8_charv,
402                       wchar_t             ***wcharv,
403                       int                   *error_index,
404                       GError               **error)
405 {
406   wchar_t **retval = NULL;
407 
408   *wcharv = NULL;
409   if (utf8_charv != NULL)
410     {
411       int n = 0, i;
412 
413       while (utf8_charv[n])
414 	n++;
415       retval = g_new (wchar_t *, n + 1);
416 
417       for (i = 0; i < n; i++)
418 	{
419 	  retval[i] = g_utf8_to_utf16 (utf8_charv[i], -1, NULL, NULL, error);
420 	  if (retval[i] == NULL)
421 	    {
422 	      if (error_index)
423 		*error_index = i;
424 	      while (i)
425 		g_free (retval[--i]);
426 	      g_free (retval);
427 	      return FALSE;
428 	    }
429 	}
430 
431       retval[n] = NULL;
432     }
433   *wcharv = retval;
434   return TRUE;
435 }
436 
437 static gboolean
do_spawn_directly(gint * exit_status,gboolean do_return_handle,GSpawnFlags flags,const gchar * const * argv,const gchar * const * envp,const gchar * const * protected_argv,GPid * child_pid,GError ** error)438 do_spawn_directly (gint                 *exit_status,
439                    gboolean              do_return_handle,
440                    GSpawnFlags           flags,
441                    const gchar * const  *argv,
442                    const gchar * const  *envp,
443                    const gchar * const  *protected_argv,
444                    GPid                 *child_pid,
445                    GError              **error)
446 {
447   const int mode = (exit_status == NULL) ? P_NOWAIT : P_WAIT;
448   const gchar * const *new_argv;
449   gintptr rc = -1;
450   int errsv;
451   GError *conv_error = NULL;
452   gint conv_error_index;
453   wchar_t *wargv0, **wargv, **wenvp;
454 
455   new_argv = (flags & G_SPAWN_FILE_AND_ARGV_ZERO) ? protected_argv + 1 : protected_argv;
456 
457   wargv0 = g_utf8_to_utf16 (argv[0], -1, NULL, NULL, &conv_error);
458   if (wargv0 == NULL)
459     {
460       g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
461 		   _("Invalid program name: %s"),
462 		   conv_error->message);
463       g_error_free (conv_error);
464 
465       return FALSE;
466     }
467 
468   if (!utf8_charv_to_wcharv (new_argv, &wargv, &conv_error_index, &conv_error))
469     {
470       g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
471 		   _("Invalid string in argument vector at %d: %s"),
472 		   conv_error_index, conv_error->message);
473       g_error_free (conv_error);
474       g_free (wargv0);
475 
476       return FALSE;
477     }
478 
479   if (!utf8_charv_to_wcharv (envp, &wenvp, NULL, &conv_error))
480     {
481       g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
482 		   _("Invalid string in environment: %s"),
483 		   conv_error->message);
484       g_error_free (conv_error);
485       g_free (wargv0);
486       g_strfreev ((gchar **) wargv);
487 
488       return FALSE;
489     }
490 
491   if (flags & G_SPAWN_SEARCH_PATH)
492     if (wenvp != NULL)
493       rc = _wspawnvpe (mode, wargv0, (const wchar_t **) wargv, (const wchar_t **) wenvp);
494     else
495       rc = _wspawnvp (mode, wargv0, (const wchar_t **) wargv);
496   else
497     if (wenvp != NULL)
498       rc = _wspawnve (mode, wargv0, (const wchar_t **) wargv, (const wchar_t **) wenvp);
499     else
500       rc = _wspawnv (mode, wargv0, (const wchar_t **) wargv);
501 
502   errsv = errno;
503 
504   g_free (wargv0);
505   g_strfreev ((gchar **) wargv);
506   g_strfreev ((gchar **) wenvp);
507 
508   if (rc == -1 && errsv != 0)
509     {
510       g_set_error (error, G_SPAWN_ERROR, _g_spawn_exec_err_to_g_error (errsv),
511 		   _("Failed to execute child process (%s)"),
512 		   g_strerror (errsv));
513       return FALSE;
514     }
515 
516   if (exit_status == NULL)
517     {
518       if (child_pid && do_return_handle)
519 	*child_pid = (GPid) rc;
520       else
521 	{
522 	  CloseHandle ((HANDLE) rc);
523 	  if (child_pid)
524 	    *child_pid = 0;
525 	}
526     }
527   else
528     *exit_status = rc;
529 
530   return TRUE;
531 }
532 
533 static gboolean
fork_exec(gint * exit_status,gboolean do_return_handle,const gchar * working_directory,const gchar * const * argv,const gchar * const * envp,GSpawnFlags flags,GSpawnChildSetupFunc child_setup,gpointer user_data,GPid * child_pid,gint * stdin_pipe_out,gint * stdout_pipe_out,gint * stderr_pipe_out,gint stdin_fd,gint stdout_fd,gint stderr_fd,gint * err_report,GError ** error)534 fork_exec (gint                  *exit_status,
535            gboolean               do_return_handle,
536            const gchar           *working_directory,
537            const gchar * const   *argv,
538            const gchar * const   *envp,
539            GSpawnFlags            flags,
540            GSpawnChildSetupFunc   child_setup,
541            gpointer               user_data,
542            GPid                  *child_pid,
543            gint                  *stdin_pipe_out,
544            gint                  *stdout_pipe_out,
545            gint                  *stderr_pipe_out,
546            gint                   stdin_fd,
547            gint                   stdout_fd,
548            gint                   stderr_fd,
549            gint                  *err_report,
550            GError               **error)
551 {
552   char **protected_argv;
553   char args[ARG_COUNT][10];
554   char **new_argv;
555   int i;
556   gintptr rc = -1;
557   int errsv;
558   int argc;
559   int child_err_report_pipe[2] = { -1, -1 };
560   int helper_sync_pipe[2] = { -1, -1 };
561   gintptr helper_report[2];
562   static gboolean warned_about_child_setup = FALSE;
563   GError *conv_error = NULL;
564   gint conv_error_index;
565   gchar *helper_process;
566   wchar_t *whelper, **wargv, **wenvp;
567   gchar *glib_dll_directory;
568   int stdin_pipe[2] = { -1, -1 };
569   int stdout_pipe[2] = { -1, -1 };
570   int stderr_pipe[2] = { -1, -1 };
571 
572   g_assert (stdin_pipe_out == NULL || stdin_fd < 0);
573   g_assert (stdout_pipe_out == NULL || stdout_fd < 0);
574   g_assert (stderr_pipe_out == NULL || stderr_fd < 0);
575 
576   if (child_setup && !warned_about_child_setup)
577     {
578       warned_about_child_setup = TRUE;
579       g_warning ("passing a child setup function to the g_spawn functions is pointless on Windows and it is ignored");
580     }
581 
582   if (stdin_pipe_out != NULL)
583     {
584       if (!make_pipe (stdin_pipe, error))
585         goto cleanup_and_fail;
586       stdin_fd = stdin_pipe[0];
587     }
588 
589   if (stdout_pipe_out != NULL)
590     {
591       if (!make_pipe (stdout_pipe, error))
592         goto cleanup_and_fail;
593       stdout_fd = stdout_pipe[1];
594     }
595 
596   if (stderr_pipe_out != NULL)
597     {
598       if (!make_pipe (stderr_pipe, error))
599         goto cleanup_and_fail;
600       stderr_fd = stderr_pipe[1];
601     }
602 
603   argc = protect_argv (argv, &protected_argv);
604 
605   if (stdin_fd == -1 && stdout_fd == -1 && stderr_fd == -1 &&
606       (flags & G_SPAWN_CHILD_INHERITS_STDIN) &&
607       !(flags & G_SPAWN_STDOUT_TO_DEV_NULL) &&
608       !(flags & G_SPAWN_STDERR_TO_DEV_NULL) &&
609       (working_directory == NULL || !*working_directory) &&
610       (flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN))
611     {
612       /* We can do without the helper process */
613       gboolean retval =
614 	do_spawn_directly (exit_status, do_return_handle, flags,
615 			   argv, envp, (const gchar * const *) protected_argv,
616 			   child_pid, error);
617       g_strfreev (protected_argv);
618       return retval;
619     }
620 
621   if (!make_pipe (child_err_report_pipe, error))
622     goto cleanup_and_fail;
623 
624   if (!make_pipe (helper_sync_pipe, error))
625     goto cleanup_and_fail;
626 
627   new_argv = g_new (char *, argc + 1 + ARG_COUNT);
628   if (GetConsoleWindow () != NULL)
629     helper_process = HELPER_PROCESS "-console.exe";
630   else
631     helper_process = HELPER_PROCESS ".exe";
632 
633   glib_dll_directory = _glib_get_dll_directory ();
634   if (glib_dll_directory != NULL)
635     {
636       helper_process = g_build_filename (glib_dll_directory, helper_process, NULL);
637       g_free (glib_dll_directory);
638     }
639   else
640     helper_process = g_strdup (helper_process);
641 
642   new_argv[0] = protect_argv_string (helper_process);
643 
644   _g_sprintf (args[ARG_CHILD_ERR_REPORT], "%d", child_err_report_pipe[1]);
645   new_argv[ARG_CHILD_ERR_REPORT] = args[ARG_CHILD_ERR_REPORT];
646 
647   /* Make the read end of the child error report pipe
648    * noninherited. Otherwise it will needlessly be inherited by the
649    * helper process, and the started actual user process. As such that
650    * shouldn't harm, but it is unnecessary.
651    */
652   child_err_report_pipe[0] = reopen_noninherited (child_err_report_pipe[0], _O_RDONLY);
653 
654   if (flags & G_SPAWN_FILE_AND_ARGV_ZERO)
655     {
656       /* Overload ARG_CHILD_ERR_REPORT to also encode the
657        * G_SPAWN_FILE_AND_ARGV_ZERO functionality.
658        */
659       strcat (args[ARG_CHILD_ERR_REPORT], "#");
660     }
661 
662   _g_sprintf (args[ARG_HELPER_SYNC], "%d", helper_sync_pipe[0]);
663   new_argv[ARG_HELPER_SYNC] = args[ARG_HELPER_SYNC];
664 
665   /* Make the write end of the sync pipe noninherited. Otherwise the
666    * helper process will inherit it, and thus if this process happens
667    * to crash before writing the sync byte to the pipe, the helper
668    * process won't read but won't get any EOF either, as it has the
669    * write end open itself.
670    */
671   helper_sync_pipe[1] = reopen_noninherited (helper_sync_pipe[1], _O_WRONLY);
672 
673   if (stdin_fd != -1)
674     {
675       _g_sprintf (args[ARG_STDIN], "%d", stdin_fd);
676       new_argv[ARG_STDIN] = args[ARG_STDIN];
677     }
678   else if (flags & G_SPAWN_CHILD_INHERITS_STDIN)
679     {
680       /* Let stdin be alone */
681       new_argv[ARG_STDIN] = "-";
682     }
683   else
684     {
685       /* Keep process from blocking on a read of stdin */
686       new_argv[ARG_STDIN] = "z";
687     }
688 
689   if (stdout_fd != -1)
690     {
691       _g_sprintf (args[ARG_STDOUT], "%d", stdout_fd);
692       new_argv[ARG_STDOUT] = args[ARG_STDOUT];
693     }
694   else if (flags & G_SPAWN_STDOUT_TO_DEV_NULL)
695     {
696       new_argv[ARG_STDOUT] = "z";
697     }
698   else
699     {
700       new_argv[ARG_STDOUT] = "-";
701     }
702 
703   if (stderr_fd != -1)
704     {
705       _g_sprintf (args[ARG_STDERR], "%d", stderr_fd);
706       new_argv[ARG_STDERR] = args[ARG_STDERR];
707     }
708   else if (flags & G_SPAWN_STDERR_TO_DEV_NULL)
709     {
710       new_argv[ARG_STDERR] = "z";
711     }
712   else
713     {
714       new_argv[ARG_STDERR] = "-";
715     }
716 
717   if (working_directory && *working_directory)
718     new_argv[ARG_WORKING_DIRECTORY] = protect_argv_string (working_directory);
719   else
720     new_argv[ARG_WORKING_DIRECTORY] = g_strdup ("-");
721 
722   if (!(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN))
723     new_argv[ARG_CLOSE_DESCRIPTORS] = "y";
724   else
725     new_argv[ARG_CLOSE_DESCRIPTORS] = "-";
726 
727   if (flags & G_SPAWN_SEARCH_PATH)
728     new_argv[ARG_USE_PATH] = "y";
729   else
730     new_argv[ARG_USE_PATH] = "-";
731 
732   if (exit_status == NULL)
733     new_argv[ARG_WAIT] = "-";
734   else
735     new_argv[ARG_WAIT] = "w";
736 
737   for (i = 0; i <= argc; i++)
738     new_argv[ARG_PROGRAM + i] = protected_argv[i];
739 
740   SETUP_DEBUG();
741 
742   if (debug)
743     {
744       g_print ("calling %s with argv:\n", helper_process);
745       for (i = 0; i < argc + 1 + ARG_COUNT; i++)
746 	g_print ("argv[%d]: %s\n", i, (new_argv[i] ? new_argv[i] : "NULL"));
747     }
748 
749   if (!utf8_charv_to_wcharv ((const gchar * const *) new_argv, &wargv, &conv_error_index, &conv_error))
750     {
751       if (conv_error_index == ARG_WORKING_DIRECTORY)
752 	g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
753 		     _("Invalid working directory: %s"),
754 		     conv_error->message);
755       else
756 	g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
757 		     _("Invalid string in argument vector at %d: %s"),
758 		     conv_error_index - ARG_PROGRAM, conv_error->message);
759       g_error_free (conv_error);
760       g_strfreev (protected_argv);
761       g_free (new_argv[0]);
762       g_free (new_argv[ARG_WORKING_DIRECTORY]);
763       g_free (new_argv);
764       g_free (helper_process);
765 
766       goto cleanup_and_fail;
767     }
768 
769   if (!utf8_charv_to_wcharv (envp, &wenvp, NULL, &conv_error))
770     {
771       g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
772 		   _("Invalid string in environment: %s"),
773 		   conv_error->message);
774       g_error_free (conv_error);
775       g_strfreev (protected_argv);
776       g_free (new_argv[0]);
777       g_free (new_argv[ARG_WORKING_DIRECTORY]);
778       g_free (new_argv);
779       g_free (helper_process);
780       g_strfreev ((gchar **) wargv);
781 
782       goto cleanup_and_fail;
783     }
784 
785   whelper = g_utf8_to_utf16 (helper_process, -1, NULL, NULL, NULL);
786   g_free (helper_process);
787 
788   if (wenvp != NULL)
789     rc = _wspawnvpe (P_NOWAIT, whelper, (const wchar_t **) wargv, (const wchar_t **) wenvp);
790   else
791     rc = _wspawnvp (P_NOWAIT, whelper, (const wchar_t **) wargv);
792 
793   errsv = errno;
794 
795   g_free (whelper);
796   g_strfreev ((gchar **) wargv);
797   g_strfreev ((gchar **) wenvp);
798 
799   /* Close the other process's ends of the pipes in this process,
800    * otherwise the reader will never get EOF.
801    */
802   close_and_invalidate (&child_err_report_pipe[1]);
803   close_and_invalidate (&helper_sync_pipe[0]);
804 
805   g_strfreev (protected_argv);
806 
807   g_free (new_argv[0]);
808   g_free (new_argv[ARG_WORKING_DIRECTORY]);
809   g_free (new_argv);
810 
811   /* Check if gspawn-win32-helper couldn't be run */
812   if (rc == -1 && errsv != 0)
813     {
814       g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
815 		   _("Failed to execute helper program (%s)"),
816 		   g_strerror (errsv));
817       goto cleanup_and_fail;
818     }
819 
820   if (exit_status != NULL)
821     {
822       /* Synchronous case. Pass helper's report pipe back to caller,
823        * which takes care of reading it after the grandchild has
824        * finished.
825        */
826       g_assert (err_report != NULL);
827       *err_report = child_err_report_pipe[0];
828       write (helper_sync_pipe[1], " ", 1);
829       close_and_invalidate (&helper_sync_pipe[1]);
830     }
831   else
832     {
833       /* Asynchronous case. We read the helper's report right away. */
834       if (!read_helper_report (child_err_report_pipe[0], helper_report, error))
835 	goto cleanup_and_fail;
836 
837       close_and_invalidate (&child_err_report_pipe[0]);
838 
839       switch (helper_report[0])
840 	{
841 	case CHILD_NO_ERROR:
842 	  if (child_pid && do_return_handle)
843 	    {
844 	      /* rc is our HANDLE for gspawn-win32-helper. It has
845 	       * told us the HANDLE of its child. Duplicate that into
846 	       * a HANDLE valid in this process.
847 	       */
848 	      if (!DuplicateHandle ((HANDLE) rc, (HANDLE) helper_report[1],
849 				    GetCurrentProcess (), (LPHANDLE) child_pid,
850 				    0, TRUE, DUPLICATE_SAME_ACCESS))
851 		{
852 		  char *emsg = g_win32_error_message (GetLastError ());
853 		  g_print("%s\n", emsg);
854 		  *child_pid = 0;
855 		}
856 	    }
857 	  else if (child_pid)
858 	    *child_pid = 0;
859 	  write (helper_sync_pipe[1], " ", 1);
860 	  close_and_invalidate (&helper_sync_pipe[1]);
861 	  break;
862 
863 	default:
864 	  write (helper_sync_pipe[1], " ", 1);
865 	  close_and_invalidate (&helper_sync_pipe[1]);
866 	  set_child_error (helper_report, working_directory, error);
867 	  goto cleanup_and_fail;
868 	}
869     }
870 
871   /* Success against all odds! return the information */
872 
873   if (rc != -1)
874     CloseHandle ((HANDLE) rc);
875 
876   /* Close the other process's ends of the pipes in this process,
877    * otherwise the reader will never get EOF.
878    */
879   close_and_invalidate (&stdin_pipe[0]);
880   close_and_invalidate (&stdout_pipe[1]);
881   close_and_invalidate (&stderr_pipe[1]);
882 
883   if (stdin_pipe_out != NULL)
884     *stdin_pipe_out = stdin_pipe[1];
885   if (stdout_pipe_out != NULL)
886     *stdout_pipe_out = stdout_pipe[0];
887   if (stderr_pipe_out != NULL)
888     *stderr_pipe_out = stderr_pipe[0];
889 
890   return TRUE;
891 
892  cleanup_and_fail:
893 
894   if (rc != -1)
895     CloseHandle ((HANDLE) rc);
896   if (child_err_report_pipe[0] != -1)
897     close (child_err_report_pipe[0]);
898   if (child_err_report_pipe[1] != -1)
899     close (child_err_report_pipe[1]);
900   if (helper_sync_pipe[0] != -1)
901     close (helper_sync_pipe[0]);
902   if (helper_sync_pipe[1] != -1)
903     close (helper_sync_pipe[1]);
904 
905   if (stdin_pipe[0] != -1)
906     close (stdin_pipe[0]);
907   if (stdin_pipe[1] != -1)
908     close (stdin_pipe[1]);
909   if (stdout_pipe[0] != -1)
910     close (stdout_pipe[0]);
911   if (stdout_pipe[1] != -1)
912     close (stdout_pipe[1]);
913   if (stderr_pipe[0] != -1)
914     close (stderr_pipe[0]);
915   if (stderr_pipe[1] != -1)
916     close (stderr_pipe[1]);
917 
918   return FALSE;
919 }
920 
921 gboolean
g_spawn_sync(const gchar * working_directory,gchar ** argv,gchar ** envp,GSpawnFlags flags,GSpawnChildSetupFunc child_setup,gpointer user_data,gchar ** standard_output,gchar ** standard_error,gint * exit_status,GError ** error)922 g_spawn_sync (const gchar          *working_directory,
923               gchar               **argv,
924               gchar               **envp,
925               GSpawnFlags           flags,
926               GSpawnChildSetupFunc  child_setup,
927               gpointer              user_data,
928               gchar               **standard_output,
929               gchar               **standard_error,
930               gint                 *exit_status,
931               GError              **error)
932 {
933   gint outpipe = -1;
934   gint errpipe = -1;
935   gint reportpipe = -1;
936   GIOChannel *outchannel = NULL;
937   GIOChannel *errchannel = NULL;
938   GPollFD outfd, errfd;
939   GPollFD fds[2];
940   gint nfds;
941   gint outindex = -1;
942   gint errindex = -1;
943   gint ret;
944   GString *outstr = NULL;
945   GString *errstr = NULL;
946   gboolean failed;
947   gint status;
948 
949   g_return_val_if_fail (argv != NULL, FALSE);
950   g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
951   g_return_val_if_fail (standard_output == NULL ||
952                         !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
953   g_return_val_if_fail (standard_error == NULL ||
954                         !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
955 
956   /* Just to ensure segfaults if callers try to use
957    * these when an error is reported.
958    */
959   if (standard_output)
960     *standard_output = NULL;
961 
962   if (standard_error)
963     *standard_error = NULL;
964 
965   if (!fork_exec (&status,
966                   FALSE,
967                   working_directory,
968                   (const gchar * const *) argv,
969                   (const gchar * const *) envp,
970                   flags,
971                   child_setup,
972                   user_data,
973                   NULL,
974                   NULL,
975                   standard_output ? &outpipe : NULL,
976                   standard_error ? &errpipe : NULL,
977                   -1,
978                   -1,
979                   -1,
980                   &reportpipe,
981                   error))
982     return FALSE;
983 
984   /* Read data from child. */
985 
986   failed = FALSE;
987 
988   if (outpipe >= 0)
989     {
990       outstr = g_string_new (NULL);
991       outchannel = g_io_channel_win32_new_fd (outpipe);
992       g_io_channel_set_encoding (outchannel, NULL, NULL);
993       g_io_channel_set_buffered (outchannel, FALSE);
994       g_io_channel_win32_make_pollfd (outchannel,
995 				      G_IO_IN | G_IO_ERR | G_IO_HUP,
996 				      &outfd);
997       if (debug)
998 	g_print ("outfd=%p\n", (HANDLE) outfd.fd);
999     }
1000 
1001   if (errpipe >= 0)
1002     {
1003       errstr = g_string_new (NULL);
1004       errchannel = g_io_channel_win32_new_fd (errpipe);
1005       g_io_channel_set_encoding (errchannel, NULL, NULL);
1006       g_io_channel_set_buffered (errchannel, FALSE);
1007       g_io_channel_win32_make_pollfd (errchannel,
1008 				      G_IO_IN | G_IO_ERR | G_IO_HUP,
1009 				      &errfd);
1010       if (debug)
1011 	g_print ("errfd=%p\n", (HANDLE) errfd.fd);
1012     }
1013 
1014   /* Read data until we get EOF on all pipes. */
1015   while (!failed && (outpipe >= 0 || errpipe >= 0))
1016     {
1017       nfds = 0;
1018       if (outpipe >= 0)
1019 	{
1020 	  fds[nfds] = outfd;
1021 	  outindex = nfds;
1022 	  nfds++;
1023 	}
1024       if (errpipe >= 0)
1025 	{
1026 	  fds[nfds] = errfd;
1027 	  errindex = nfds;
1028 	  nfds++;
1029 	}
1030 
1031       if (debug)
1032 	g_print ("g_spawn_sync: calling g_io_channel_win32_poll, nfds=%d\n",
1033 		 nfds);
1034 
1035       ret = g_io_channel_win32_poll (fds, nfds, -1);
1036 
1037       if (ret < 0)
1038         {
1039           failed = TRUE;
1040 
1041           g_set_error_literal (error, G_SPAWN_ERROR, G_SPAWN_ERROR_READ,
1042                                _("Unexpected error in g_io_channel_win32_poll() reading data from a child process"));
1043 
1044           break;
1045         }
1046 
1047       if (outpipe >= 0 && (fds[outindex].revents & G_IO_IN))
1048         {
1049           switch (read_data (outstr, outchannel, error))
1050             {
1051             case READ_FAILED:
1052 	      if (debug)
1053 		g_print ("g_spawn_sync: outchannel: READ_FAILED\n");
1054               failed = TRUE;
1055               break;
1056             case READ_EOF:
1057 	      if (debug)
1058 		g_print ("g_spawn_sync: outchannel: READ_EOF\n");
1059               g_io_channel_unref (outchannel);
1060 	      outchannel = NULL;
1061               close_and_invalidate (&outpipe);
1062               break;
1063             default:
1064 	      if (debug)
1065 		g_print ("g_spawn_sync: outchannel: OK\n");
1066               break;
1067             }
1068 
1069           if (failed)
1070             break;
1071         }
1072 
1073       if (errpipe >= 0 && (fds[errindex].revents & G_IO_IN))
1074         {
1075           switch (read_data (errstr, errchannel, error))
1076             {
1077             case READ_FAILED:
1078 	      if (debug)
1079 		g_print ("g_spawn_sync: errchannel: READ_FAILED\n");
1080               failed = TRUE;
1081               break;
1082             case READ_EOF:
1083 	      if (debug)
1084 		g_print ("g_spawn_sync: errchannel: READ_EOF\n");
1085 	      g_io_channel_unref (errchannel);
1086 	      errchannel = NULL;
1087               close_and_invalidate (&errpipe);
1088               break;
1089             default:
1090 	      if (debug)
1091 		g_print ("g_spawn_sync: errchannel: OK\n");
1092               break;
1093             }
1094 
1095           if (failed)
1096             break;
1097         }
1098     }
1099 
1100   if (reportpipe == -1)
1101     {
1102       /* No helper process, exit status of actual spawned process
1103        * already available.
1104        */
1105       if (exit_status)
1106         *exit_status = status;
1107     }
1108   else
1109     {
1110       /* Helper process was involved. Read its report now after the
1111        * grandchild has finished.
1112        */
1113       gintptr helper_report[2];
1114 
1115       if (!read_helper_report (reportpipe, helper_report, error))
1116 	failed = TRUE;
1117       else
1118 	{
1119 	  switch (helper_report[0])
1120 	    {
1121 	    case CHILD_NO_ERROR:
1122 	      if (exit_status)
1123 		*exit_status = helper_report[1];
1124 	      break;
1125 	    default:
1126 	      set_child_error (helper_report, working_directory, error);
1127 	      failed = TRUE;
1128 	      break;
1129 	    }
1130 	}
1131       close_and_invalidate (&reportpipe);
1132     }
1133 
1134 
1135   /* These should only be open still if we had an error.  */
1136 
1137   if (outchannel != NULL)
1138     g_io_channel_unref (outchannel);
1139   if (errchannel != NULL)
1140     g_io_channel_unref (errchannel);
1141   if (outpipe >= 0)
1142     close_and_invalidate (&outpipe);
1143   if (errpipe >= 0)
1144     close_and_invalidate (&errpipe);
1145 
1146   if (failed)
1147     {
1148       if (outstr)
1149         g_string_free (outstr, TRUE);
1150       if (errstr)
1151         g_string_free (errstr, TRUE);
1152 
1153       return FALSE;
1154     }
1155   else
1156     {
1157       if (standard_output)
1158         *standard_output = g_string_free (outstr, FALSE);
1159 
1160       if (standard_error)
1161         *standard_error = g_string_free (errstr, FALSE);
1162 
1163       return TRUE;
1164     }
1165 }
1166 
1167 gboolean
g_spawn_async_with_pipes(const gchar * working_directory,gchar ** argv,gchar ** envp,GSpawnFlags flags,GSpawnChildSetupFunc child_setup,gpointer user_data,GPid * child_pid,gint * standard_input,gint * standard_output,gint * standard_error,GError ** error)1168 g_spawn_async_with_pipes (const gchar          *working_directory,
1169                           gchar               **argv,
1170                           gchar               **envp,
1171                           GSpawnFlags           flags,
1172                           GSpawnChildSetupFunc  child_setup,
1173                           gpointer              user_data,
1174                           GPid                 *child_pid,
1175                           gint                 *standard_input,
1176                           gint                 *standard_output,
1177                           gint                 *standard_error,
1178                           GError              **error)
1179 {
1180   g_return_val_if_fail (argv != NULL, FALSE);
1181   g_return_val_if_fail (standard_output == NULL ||
1182                         !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
1183   g_return_val_if_fail (standard_error == NULL ||
1184                         !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
1185   /* can't inherit stdin if we have an input pipe. */
1186   g_return_val_if_fail (standard_input == NULL ||
1187                         !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
1188 
1189   return fork_exec (NULL,
1190                     (flags & G_SPAWN_DO_NOT_REAP_CHILD),
1191                     working_directory,
1192                     (const gchar * const *) argv,
1193                     (const gchar * const *) envp,
1194                     flags,
1195                     child_setup,
1196                     user_data,
1197                     child_pid,
1198                     standard_input,
1199                     standard_output,
1200                     standard_error,
1201                     -1,
1202                     -1,
1203                     -1,
1204                     NULL,
1205                     error);
1206 }
1207 
1208 gboolean
g_spawn_async_with_fds(const gchar * working_directory,gchar ** argv,gchar ** envp,GSpawnFlags flags,GSpawnChildSetupFunc child_setup,gpointer user_data,GPid * child_pid,gint stdin_fd,gint stdout_fd,gint stderr_fd,GError ** error)1209 g_spawn_async_with_fds (const gchar          *working_directory,
1210                         gchar               **argv,
1211                         gchar               **envp,
1212                         GSpawnFlags           flags,
1213                         GSpawnChildSetupFunc  child_setup,
1214                         gpointer              user_data,
1215                         GPid                 *child_pid,
1216                         gint                  stdin_fd,
1217                         gint                  stdout_fd,
1218                         gint                  stderr_fd,
1219                         GError              **error)
1220 {
1221   g_return_val_if_fail (argv != NULL, FALSE);
1222   g_return_val_if_fail (stdin_fd == -1 ||
1223                         !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
1224   g_return_val_if_fail (stderr_fd == -1 ||
1225                         !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
1226   /* can't inherit stdin if we have an input pipe. */
1227   g_return_val_if_fail (stdin_fd == -1 ||
1228                         !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
1229 
1230   return fork_exec (NULL,
1231                     (flags & G_SPAWN_DO_NOT_REAP_CHILD),
1232                     working_directory,
1233                     (const gchar * const *) argv,
1234                     (const gchar * const *) envp,
1235                     flags,
1236                     child_setup,
1237                     user_data,
1238                     child_pid,
1239                     NULL,
1240                     NULL,
1241                     NULL,
1242                     stdin_fd,
1243                     stdout_fd,
1244                     stderr_fd,
1245                     NULL,
1246                     error);
1247 
1248 }
1249 
1250 gboolean
g_spawn_async_with_pipes_and_fds(const gchar * working_directory,const gchar * const * argv,const gchar * const * envp,GSpawnFlags flags,GSpawnChildSetupFunc child_setup,gpointer user_data,gint stdin_fd,gint stdout_fd,gint stderr_fd,const gint * source_fds,const gint * target_fds,gsize n_fds,GPid * child_pid_out,gint * stdin_pipe_out,gint * stdout_pipe_out,gint * stderr_pipe_out,GError ** error)1251 g_spawn_async_with_pipes_and_fds (const gchar           *working_directory,
1252                                   const gchar * const   *argv,
1253                                   const gchar * const   *envp,
1254                                   GSpawnFlags            flags,
1255                                   GSpawnChildSetupFunc   child_setup,
1256                                   gpointer               user_data,
1257                                   gint                   stdin_fd,
1258                                   gint                   stdout_fd,
1259                                   gint                   stderr_fd,
1260                                   const gint            *source_fds,
1261                                   const gint            *target_fds,
1262                                   gsize                  n_fds,
1263                                   GPid                  *child_pid_out,
1264                                   gint                  *stdin_pipe_out,
1265                                   gint                  *stdout_pipe_out,
1266                                   gint                  *stderr_pipe_out,
1267                                   GError               **error)
1268 {
1269   g_return_val_if_fail (argv != NULL, FALSE);
1270   g_return_val_if_fail (stdout_pipe_out == NULL ||
1271                         !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
1272   g_return_val_if_fail (stderr_pipe_out == NULL ||
1273                         !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
1274   /* can't inherit stdin if we have an input pipe. */
1275   g_return_val_if_fail (stdin_pipe_out == NULL ||
1276                         !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
1277   /* can’t use pipes and stdin/stdout/stderr FDs */
1278   g_return_val_if_fail (stdin_pipe_out == NULL || stdin_fd < 0, FALSE);
1279   g_return_val_if_fail (stdout_pipe_out == NULL || stdout_fd < 0, FALSE);
1280   g_return_val_if_fail (stderr_pipe_out == NULL || stderr_fd < 0, FALSE);
1281 
1282   /* source_fds/target_fds isn’t supported on Windows at the moment. */
1283   if (n_fds != 0)
1284     {
1285       g_set_error_literal (error, G_SPAWN_ERROR, G_SPAWN_ERROR_INVAL,
1286                            "FD redirection is not supported on Windows at the moment");
1287       return FALSE;
1288     }
1289 
1290   return fork_exec (NULL,
1291                     (flags & G_SPAWN_DO_NOT_REAP_CHILD),
1292                     working_directory,
1293                     argv,
1294                     envp,
1295                     flags,
1296                     child_setup,
1297                     user_data,
1298                     child_pid_out,
1299                     stdin_pipe_out,
1300                     stdout_pipe_out,
1301                     stderr_pipe_out,
1302                     stdin_fd,
1303                     stdout_fd,
1304                     stderr_fd,
1305                     NULL,
1306                     error);
1307 }
1308 
1309 gboolean
g_spawn_command_line_sync(const gchar * command_line,gchar ** standard_output,gchar ** standard_error,gint * exit_status,GError ** error)1310 g_spawn_command_line_sync (const gchar  *command_line,
1311                            gchar       **standard_output,
1312                            gchar       **standard_error,
1313                            gint         *exit_status,
1314                            GError      **error)
1315 {
1316   gboolean retval;
1317   gchar **argv = 0;
1318 
1319   g_return_val_if_fail (command_line != NULL, FALSE);
1320 
1321   if (!g_shell_parse_argv (command_line,
1322                            NULL, &argv,
1323                            error))
1324     return FALSE;
1325 
1326   retval = g_spawn_sync (NULL,
1327                          argv,
1328                          NULL,
1329                          G_SPAWN_SEARCH_PATH,
1330                          NULL,
1331                          NULL,
1332                          standard_output,
1333                          standard_error,
1334                          exit_status,
1335                          error);
1336   g_strfreev (argv);
1337 
1338   return retval;
1339 }
1340 
1341 gboolean
g_spawn_command_line_async(const gchar * command_line,GError ** error)1342 g_spawn_command_line_async (const gchar *command_line,
1343                             GError     **error)
1344 {
1345   gboolean retval;
1346   gchar **argv = 0;
1347 
1348   g_return_val_if_fail (command_line != NULL, FALSE);
1349 
1350   if (!g_shell_parse_argv (command_line,
1351                            NULL, &argv,
1352                            error))
1353     return FALSE;
1354 
1355   retval = g_spawn_async (NULL,
1356                           argv,
1357                           NULL,
1358                           G_SPAWN_SEARCH_PATH,
1359                           NULL,
1360                           NULL,
1361                           NULL,
1362                           error);
1363   g_strfreev (argv);
1364 
1365   return retval;
1366 }
1367 
1368 void
g_spawn_close_pid(GPid pid)1369 g_spawn_close_pid (GPid pid)
1370 {
1371     CloseHandle (pid);
1372 }
1373 
1374 gboolean
g_spawn_check_exit_status(gint exit_status,GError ** error)1375 g_spawn_check_exit_status (gint      exit_status,
1376 			   GError  **error)
1377 {
1378   gboolean ret = FALSE;
1379 
1380   if (exit_status != 0)
1381     {
1382       g_set_error (error, G_SPAWN_EXIT_ERROR, exit_status,
1383 		   _("Child process exited with code %ld"),
1384 		   (long) exit_status);
1385       goto out;
1386     }
1387 
1388   ret = TRUE;
1389  out:
1390   return ret;
1391 }
1392 
1393 #ifdef G_OS_WIN32
1394 
1395 /* Binary compatibility versions. Not for newly compiled code. */
1396 
1397 _GLIB_EXTERN gboolean g_spawn_async_utf8              (const gchar           *working_directory,
1398                                                        gchar                **argv,
1399                                                        gchar                **envp,
1400                                                        GSpawnFlags            flags,
1401                                                        GSpawnChildSetupFunc   child_setup,
1402                                                        gpointer               user_data,
1403                                                        GPid                  *child_pid,
1404                                                        GError               **error);
1405 _GLIB_EXTERN gboolean g_spawn_async_with_pipes_utf8   (const gchar           *working_directory,
1406                                                        gchar                **argv,
1407                                                        gchar                **envp,
1408                                                        GSpawnFlags            flags,
1409                                                        GSpawnChildSetupFunc   child_setup,
1410                                                        gpointer               user_data,
1411                                                        GPid                  *child_pid,
1412                                                        gint                  *standard_input,
1413                                                        gint                  *standard_output,
1414                                                        gint                  *standard_error,
1415                                                        GError               **error);
1416 _GLIB_EXTERN gboolean g_spawn_sync_utf8               (const gchar           *working_directory,
1417                                                        gchar                **argv,
1418                                                        gchar                **envp,
1419                                                        GSpawnFlags            flags,
1420                                                        GSpawnChildSetupFunc   child_setup,
1421                                                        gpointer               user_data,
1422                                                        gchar                **standard_output,
1423                                                        gchar                **standard_error,
1424                                                        gint                  *exit_status,
1425                                                        GError               **error);
1426 _GLIB_EXTERN gboolean g_spawn_command_line_sync_utf8  (const gchar           *command_line,
1427                                                        gchar                **standard_output,
1428                                                        gchar                **standard_error,
1429                                                        gint                  *exit_status,
1430                                                        GError               **error);
1431 _GLIB_EXTERN gboolean g_spawn_command_line_async_utf8 (const gchar           *command_line,
1432                                                        GError               **error);
1433 
1434 gboolean
g_spawn_async_utf8(const gchar * working_directory,gchar ** argv,gchar ** envp,GSpawnFlags flags,GSpawnChildSetupFunc child_setup,gpointer user_data,GPid * child_pid,GError ** error)1435 g_spawn_async_utf8 (const gchar          *working_directory,
1436                     gchar               **argv,
1437                     gchar               **envp,
1438                     GSpawnFlags           flags,
1439                     GSpawnChildSetupFunc  child_setup,
1440                     gpointer              user_data,
1441                     GPid                 *child_pid,
1442                     GError              **error)
1443 {
1444   return g_spawn_async (working_directory,
1445                         argv,
1446                         envp,
1447                         flags,
1448                         child_setup,
1449                         user_data,
1450                         child_pid,
1451                         error);
1452 }
1453 
1454 gboolean
g_spawn_async_with_pipes_utf8(const gchar * working_directory,gchar ** argv,gchar ** envp,GSpawnFlags flags,GSpawnChildSetupFunc child_setup,gpointer user_data,GPid * child_pid,gint * standard_input,gint * standard_output,gint * standard_error,GError ** error)1455 g_spawn_async_with_pipes_utf8 (const gchar          *working_directory,
1456                                gchar               **argv,
1457                                gchar               **envp,
1458                                GSpawnFlags           flags,
1459                                GSpawnChildSetupFunc  child_setup,
1460                                gpointer              user_data,
1461                                GPid                 *child_pid,
1462                                gint                 *standard_input,
1463                                gint                 *standard_output,
1464                                gint                 *standard_error,
1465                                GError              **error)
1466 {
1467   return g_spawn_async_with_pipes (working_directory,
1468                                    argv,
1469                                    envp,
1470                                    flags,
1471                                    child_setup,
1472                                    user_data,
1473                                    child_pid,
1474                                    standard_input,
1475                                    standard_output,
1476                                    standard_error,
1477                                    error);
1478 }
1479 
1480 gboolean
g_spawn_sync_utf8(const gchar * working_directory,gchar ** argv,gchar ** envp,GSpawnFlags flags,GSpawnChildSetupFunc child_setup,gpointer user_data,gchar ** standard_output,gchar ** standard_error,gint * exit_status,GError ** error)1481 g_spawn_sync_utf8 (const gchar          *working_directory,
1482                    gchar               **argv,
1483                    gchar               **envp,
1484                    GSpawnFlags           flags,
1485                    GSpawnChildSetupFunc  child_setup,
1486                    gpointer              user_data,
1487                    gchar               **standard_output,
1488                    gchar               **standard_error,
1489                    gint                 *exit_status,
1490                    GError              **error)
1491 {
1492   return g_spawn_sync (working_directory,
1493                        argv,
1494                        envp,
1495                        flags,
1496                        child_setup,
1497                        user_data,
1498                        standard_output,
1499                        standard_error,
1500                        exit_status,
1501                        error);
1502 }
1503 
1504 gboolean
g_spawn_command_line_sync_utf8(const gchar * command_line,gchar ** standard_output,gchar ** standard_error,gint * exit_status,GError ** error)1505 g_spawn_command_line_sync_utf8 (const gchar  *command_line,
1506                                 gchar       **standard_output,
1507                                 gchar       **standard_error,
1508                                 gint         *exit_status,
1509                                 GError      **error)
1510 {
1511   return g_spawn_command_line_sync (command_line,
1512                                     standard_output,
1513                                     standard_error,
1514                                     exit_status,
1515                                     error);
1516 }
1517 
1518 gboolean
g_spawn_command_line_async_utf8(const gchar * command_line,GError ** error)1519 g_spawn_command_line_async_utf8 (const gchar *command_line,
1520                                  GError     **error)
1521 {
1522   return g_spawn_command_line_async (command_line, error);
1523 }
1524 
1525 #endif /* G_OS_WIN32 */
1526 
1527 #endif /* !GSPAWN_HELPER */
1528