1 /* gspawn-win32.c - Process launching on Win32
3 * Copyright 2000 Red Hat, Inc.
4 * Copyright 2003 Tor Lillqvist
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.
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.
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/>.
21 * Implementation details on Win32.
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.)
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.
37 * The structure of the source code in this file is a mess, I know.
40 /* Define this to get some logging all the time */
41 /* #define G_SPAWN_WIN32_DEBUG */
46 #include "glib-private.h"
47 #include "gprintfint.h"
49 #include "gspawn-private.h"
65 #ifdef G_SPAWN_WIN32_DEBUG
67 #define SETUP_DEBUG() /* empty */
69 static int debug = -1;
70 #define SETUP_DEBUG() \
75 if (g_getenv ("G_SPAWN_WIN32_DEBUG") != NULL) \
94 ARG_CHILD_ERR_REPORT = 1,
99 ARG_WORKING_DIRECTORY,
100 ARG_CLOSE_DESCRIPTORS,
104 ARG_COUNT = ARG_PROGRAM
108 reopen_noninherited (int fd,
113 DuplicateHandle (GetCurrentProcess (), (LPHANDLE) _get_osfhandle (fd),
114 GetCurrentProcess (), &filehandle,
115 0, FALSE, DUPLICATE_SAME_ACCESS);
117 return _open_osfhandle ((gintptr) filehandle, mode | _O_NOINHERIT);
120 #ifndef GSPAWN_HELPER
123 #define HELPER_PROCESS "gspawn-win64-helper"
125 #define HELPER_PROCESS "gspawn-win32-helper"
128 /* This logic has a copy for wchar_t in gspawn-win32-helper.c, protect_wargv() */
130 protect_argv_string (const gchar *string)
132 const gchar *p = string;
136 gboolean need_dblquotes = FALSE;
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 == '\\')
149 q = retval = g_malloc (len + need_dblquotes*2 + 1);
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
162 /* Add backslash for escaping quote itself */
164 /* Add backslash for every preceding backslash for escaping it */
165 for (;pre_bslash > 0; --pre_bslash)
169 /* Count length of continuous sequence of preceding backslashes. */
181 /* Add backslash for every preceding backslash for escaping it,
182 * do NOT escape quote itself.
184 for (;pre_bslash > 0; --pre_bslash)
194 protect_argv (const gchar * const *argv,
202 *new_argv = g_new (gchar *, argc+1);
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).
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.
215 for (i = 0; i < argc; i++)
216 (*new_argv)[i] = protect_argv_string (argv[i]);
218 (*new_argv)[argc] = NULL;
223 G_DEFINE_QUARK (g-exec-error-quark, g_spawn_error)
224 G_DEFINE_QUARK (g-spawn-exit-error-quark, g_spawn_exit_error)
227 g_spawn_async (const gchar *working_directory,
231 GSpawnChildSetupFunc child_setup,
236 g_return_val_if_fail (argv != NULL, FALSE);
238 return g_spawn_async_with_pipes (working_directory,
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)
253 close_and_invalidate (gint *fd)
264 READ_FAILED = 0, /* FALSE */
270 read_data (GString *str,
271 GIOChannel *iochannel,
280 giostatus = g_io_channel_read_chars (iochannel, buf, sizeof (buf), &bytes, NULL);
286 g_string_append_len (str, buf, bytes);
289 else if (giostatus == G_IO_STATUS_AGAIN)
291 else if (giostatus == G_IO_STATUS_ERROR)
293 g_set_error_literal (error, G_SPAWN_ERROR, G_SPAWN_ERROR_READ,
294 _("Failed to read data from child process"));
303 make_pipe (gint p[2],
306 if (_pipe (p, 4096, _O_BINARY) < 0)
310 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
311 _("Failed to create pipe for communicating with child process (%s)"),
319 /* The helper process writes a status report back to us, through a
320 * pipe, consisting of two ints.
323 read_helper_report (int fd,
329 while (bytes < sizeof(gintptr)*2)
335 g_print ("%s:read_helper_report: read %" G_GSIZE_FORMAT "...\n",
337 sizeof(gintptr)*2 - bytes);
339 chunk = read (fd, ((gchar*)report) + bytes,
340 sizeof(gintptr)*2 - bytes);
344 g_print ("...got %d bytes\n", chunk);
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)"),
357 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
358 _("Failed to read from child pipe (%s)"),
366 if (bytes < sizeof(gintptr)*2)
373 set_child_error (gintptr report[2],
374 const gchar *working_directory,
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)"),
383 g_strerror (report[1]));
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]));
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]));
396 g_assert_not_reached ();
401 utf8_charv_to_wcharv (const gchar * const *utf8_charv,
406 wchar_t **retval = NULL;
409 if (utf8_charv != NULL)
413 while (utf8_charv[n])
415 retval = g_new (wchar_t *, n + 1);
417 for (i = 0; i < n; i++)
419 retval[i] = g_utf8_to_utf16 (utf8_charv[i], -1, NULL, NULL, error);
420 if (retval[i] == NULL)
425 g_free (retval[--i]);
438 do_spawn_directly (gint *exit_status,
439 gboolean do_return_handle,
441 const gchar * const *argv,
442 const gchar * const *envp,
443 const gchar * const *protected_argv,
447 const int mode = (exit_status == NULL) ? P_NOWAIT : P_WAIT;
448 const gchar * const *new_argv;
451 GError *conv_error = NULL;
452 gint conv_error_index;
453 wchar_t *wargv0, **wargv, **wenvp;
455 new_argv = (flags & G_SPAWN_FILE_AND_ARGV_ZERO) ? protected_argv + 1 : protected_argv;
457 wargv0 = g_utf8_to_utf16 (argv[0], -1, NULL, NULL, &conv_error);
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);
468 if (!utf8_charv_to_wcharv (new_argv, &wargv, &conv_error_index, &conv_error))
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);
479 if (!utf8_charv_to_wcharv (envp, &wenvp, NULL, &conv_error))
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);
486 g_strfreev ((gchar **) wargv);
491 if (flags & G_SPAWN_SEARCH_PATH)
493 rc = _wspawnvpe (mode, wargv0, (const wchar_t **) wargv, (const wchar_t **) wenvp);
495 rc = _wspawnvp (mode, wargv0, (const wchar_t **) wargv);
498 rc = _wspawnve (mode, wargv0, (const wchar_t **) wargv, (const wchar_t **) wenvp);
500 rc = _wspawnv (mode, wargv0, (const wchar_t **) wargv);
505 g_strfreev ((gchar **) wargv);
506 g_strfreev ((gchar **) wenvp);
508 if (rc == -1 && errsv != 0)
510 g_set_error (error, G_SPAWN_ERROR, _g_spawn_exec_err_to_g_error (errsv),
511 _("Failed to execute child process (%s)"),
516 if (exit_status == NULL)
518 if (child_pid && do_return_handle)
519 *child_pid = (GPid) rc;
522 CloseHandle ((HANDLE) rc);
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,
540 GSpawnChildSetupFunc child_setup,
543 gint *stdin_pipe_out,
544 gint *stdout_pipe_out,
545 gint *stderr_pipe_out,
552 char **protected_argv;
553 char args[ARG_COUNT][10];
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 };
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);
576 if (child_setup && !warned_about_child_setup)
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");
582 if (stdin_pipe_out != NULL)
584 if (!make_pipe (stdin_pipe, error))
585 goto cleanup_and_fail;
586 stdin_fd = stdin_pipe[0];
589 if (stdout_pipe_out != NULL)
591 if (!make_pipe (stdout_pipe, error))
592 goto cleanup_and_fail;
593 stdout_fd = stdout_pipe[1];
596 if (stderr_pipe_out != NULL)
598 if (!make_pipe (stderr_pipe, error))
599 goto cleanup_and_fail;
600 stderr_fd = stderr_pipe[1];
603 argc = protect_argv (argv, &protected_argv);
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))
612 /* We can do without the helper process */
614 do_spawn_directly (exit_status, do_return_handle, flags,
615 argv, envp, (const gchar * const *) protected_argv,
617 g_strfreev (protected_argv);
621 if (!make_pipe (child_err_report_pipe, error))
622 goto cleanup_and_fail;
624 if (!make_pipe (helper_sync_pipe, error))
625 goto cleanup_and_fail;
627 new_argv = g_new (char *, argc + 1 + ARG_COUNT);
628 if (GetConsoleWindow () != NULL)
629 helper_process = HELPER_PROCESS "-console.exe";
631 helper_process = HELPER_PROCESS ".exe";
633 glib_dll_directory = _glib_get_dll_directory ();
634 if (glib_dll_directory != NULL)
636 helper_process = g_build_filename (glib_dll_directory, helper_process, NULL);
637 g_free (glib_dll_directory);
640 helper_process = g_strdup (helper_process);
642 new_argv[0] = protect_argv_string (helper_process);
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];
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.
652 child_err_report_pipe[0] = reopen_noninherited (child_err_report_pipe[0], _O_RDONLY);
654 if (flags & G_SPAWN_FILE_AND_ARGV_ZERO)
656 /* Overload ARG_CHILD_ERR_REPORT to also encode the
657 * G_SPAWN_FILE_AND_ARGV_ZERO functionality.
659 strcat (args[ARG_CHILD_ERR_REPORT], "#");
662 _g_sprintf (args[ARG_HELPER_SYNC], "%d", helper_sync_pipe[0]);
663 new_argv[ARG_HELPER_SYNC] = args[ARG_HELPER_SYNC];
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.
671 helper_sync_pipe[1] = reopen_noninherited (helper_sync_pipe[1], _O_WRONLY);
675 _g_sprintf (args[ARG_STDIN], "%d", stdin_fd);
676 new_argv[ARG_STDIN] = args[ARG_STDIN];
678 else if (flags & G_SPAWN_CHILD_INHERITS_STDIN)
680 /* Let stdin be alone */
681 new_argv[ARG_STDIN] = "-";
685 /* Keep process from blocking on a read of stdin */
686 new_argv[ARG_STDIN] = "z";
691 _g_sprintf (args[ARG_STDOUT], "%d", stdout_fd);
692 new_argv[ARG_STDOUT] = args[ARG_STDOUT];
694 else if (flags & G_SPAWN_STDOUT_TO_DEV_NULL)
696 new_argv[ARG_STDOUT] = "z";
700 new_argv[ARG_STDOUT] = "-";
705 _g_sprintf (args[ARG_STDERR], "%d", stderr_fd);
706 new_argv[ARG_STDERR] = args[ARG_STDERR];
708 else if (flags & G_SPAWN_STDERR_TO_DEV_NULL)
710 new_argv[ARG_STDERR] = "z";
714 new_argv[ARG_STDERR] = "-";
717 if (working_directory && *working_directory)
718 new_argv[ARG_WORKING_DIRECTORY] = protect_argv_string (working_directory);
720 new_argv[ARG_WORKING_DIRECTORY] = g_strdup ("-");
722 if (!(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN))
723 new_argv[ARG_CLOSE_DESCRIPTORS] = "y";
725 new_argv[ARG_CLOSE_DESCRIPTORS] = "-";
727 if (flags & G_SPAWN_SEARCH_PATH)
728 new_argv[ARG_USE_PATH] = "y";
730 new_argv[ARG_USE_PATH] = "-";
732 if (exit_status == NULL)
733 new_argv[ARG_WAIT] = "-";
735 new_argv[ARG_WAIT] = "w";
737 for (i = 0; i <= argc; i++)
738 new_argv[ARG_PROGRAM + i] = protected_argv[i];
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"));
749 if (!utf8_charv_to_wcharv ((const gchar * const *) new_argv, &wargv, &conv_error_index, &conv_error))
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);
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]);
764 g_free (helper_process);
766 goto cleanup_and_fail;
769 if (!utf8_charv_to_wcharv (envp, &wenvp, NULL, &conv_error))
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]);
779 g_free (helper_process);
780 g_strfreev ((gchar **) wargv);
782 goto cleanup_and_fail;
785 whelper = g_utf8_to_utf16 (helper_process, -1, NULL, NULL, NULL);
786 g_free (helper_process);
789 rc = _wspawnvpe (P_NOWAIT, whelper, (const wchar_t **) wargv, (const wchar_t **) wenvp);
791 rc = _wspawnvp (P_NOWAIT, whelper, (const wchar_t **) wargv);
796 g_strfreev ((gchar **) wargv);
797 g_strfreev ((gchar **) wenvp);
799 /* Close the other process's ends of the pipes in this process,
800 * otherwise the reader will never get EOF.
802 close_and_invalidate (&child_err_report_pipe[1]);
803 close_and_invalidate (&helper_sync_pipe[0]);
805 g_strfreev (protected_argv);
807 g_free (new_argv[0]);
808 g_free (new_argv[ARG_WORKING_DIRECTORY]);
811 /* Check if gspawn-win32-helper couldn't be run */
812 if (rc == -1 && errsv != 0)
814 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
815 _("Failed to execute helper program (%s)"),
817 goto cleanup_and_fail;
820 if (exit_status != NULL)
822 /* Synchronous case. Pass helper's report pipe back to caller,
823 * which takes care of reading it after the grandchild has
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]);
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;
837 close_and_invalidate (&child_err_report_pipe[0]);
839 switch (helper_report[0])
842 if (child_pid && do_return_handle)
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.
848 if (!DuplicateHandle ((HANDLE) rc, (HANDLE) helper_report[1],
849 GetCurrentProcess (), (LPHANDLE) child_pid,
850 0, TRUE, DUPLICATE_SAME_ACCESS))
852 char *emsg = g_win32_error_message (GetLastError ());
853 g_print("%s\n", emsg);
859 write (helper_sync_pipe[1], " ", 1);
860 close_and_invalidate (&helper_sync_pipe[1]);
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;
871 /* Success against all odds! return the information */
874 CloseHandle ((HANDLE) rc);
876 /* Close the other process's ends of the pipes in this process,
877 * otherwise the reader will never get EOF.
879 close_and_invalidate (&stdin_pipe[0]);
880 close_and_invalidate (&stdout_pipe[1]);
881 close_and_invalidate (&stderr_pipe[1]);
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];
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]);
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]);
922 g_spawn_sync (const gchar *working_directory,
926 GSpawnChildSetupFunc child_setup,
928 gchar **standard_output,
929 gchar **standard_error,
935 gint reportpipe = -1;
936 GIOChannel *outchannel = NULL;
937 GIOChannel *errchannel = NULL;
938 GPollFD outfd, errfd;
944 GString *outstr = NULL;
945 GString *errstr = NULL;
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);
956 /* Just to ensure segfaults if callers try to use
957 * these when an error is reported.
960 *standard_output = NULL;
963 *standard_error = NULL;
965 if (!fork_exec (&status,
968 (const gchar * const *) argv,
969 (const gchar * const *) envp,
975 standard_output ? &outpipe : NULL,
976 standard_error ? &errpipe : NULL,
984 /* Read data from child. */
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,
998 g_print ("outfd=%p\n", (HANDLE) outfd.fd);
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,
1011 g_print ("errfd=%p\n", (HANDLE) errfd.fd);
1014 /* Read data until we get EOF on all pipes. */
1015 while (!failed && (outpipe >= 0 || errpipe >= 0))
1032 g_print ("g_spawn_sync: calling g_io_channel_win32_poll, nfds=%d\n",
1035 ret = g_io_channel_win32_poll (fds, nfds, -1);
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"));
1047 if (outpipe >= 0 && (fds[outindex].revents & G_IO_IN))
1049 switch (read_data (outstr, outchannel, error))
1053 g_print ("g_spawn_sync: outchannel: READ_FAILED\n");
1058 g_print ("g_spawn_sync: outchannel: READ_EOF\n");
1059 g_io_channel_unref (outchannel);
1061 close_and_invalidate (&outpipe);
1065 g_print ("g_spawn_sync: outchannel: OK\n");
1073 if (errpipe >= 0 && (fds[errindex].revents & G_IO_IN))
1075 switch (read_data (errstr, errchannel, error))
1079 g_print ("g_spawn_sync: errchannel: READ_FAILED\n");
1084 g_print ("g_spawn_sync: errchannel: READ_EOF\n");
1085 g_io_channel_unref (errchannel);
1087 close_and_invalidate (&errpipe);
1091 g_print ("g_spawn_sync: errchannel: OK\n");
1100 if (reportpipe == -1)
1102 /* No helper process, exit status of actual spawned process
1103 * already available.
1106 *exit_status = status;
1110 /* Helper process was involved. Read its report now after the
1111 * grandchild has finished.
1113 gintptr helper_report[2];
1115 if (!read_helper_report (reportpipe, helper_report, error))
1119 switch (helper_report[0])
1121 case CHILD_NO_ERROR:
1123 *exit_status = helper_report[1];
1126 set_child_error (helper_report, working_directory, error);
1131 close_and_invalidate (&reportpipe);
1135 /* These should only be open still if we had an error. */
1137 if (outchannel != NULL)
1138 g_io_channel_unref (outchannel);
1139 if (errchannel != NULL)
1140 g_io_channel_unref (errchannel);
1142 close_and_invalidate (&outpipe);
1144 close_and_invalidate (&errpipe);
1149 g_string_free (outstr, TRUE);
1151 g_string_free (errstr, TRUE);
1157 if (standard_output)
1158 *standard_output = g_string_free (outstr, FALSE);
1161 *standard_error = g_string_free (errstr, FALSE);
1168 g_spawn_async_with_pipes (const gchar *working_directory,
1172 GSpawnChildSetupFunc child_setup,
1175 gint *standard_input,
1176 gint *standard_output,
1177 gint *standard_error,
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);
1189 return fork_exec (NULL,
1190 (flags & G_SPAWN_DO_NOT_REAP_CHILD),
1192 (const gchar * const *) argv,
1193 (const gchar * const *) envp,
1209 g_spawn_async_with_fds (const gchar *working_directory,
1213 GSpawnChildSetupFunc child_setup,
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);
1230 return fork_exec (NULL,
1231 (flags & G_SPAWN_DO_NOT_REAP_CHILD),
1233 (const gchar * const *) argv,
1234 (const gchar * const *) envp,
1251 g_spawn_async_with_pipes_and_fds (const gchar *working_directory,
1252 const gchar * const *argv,
1253 const gchar * const *envp,
1255 GSpawnChildSetupFunc child_setup,
1260 const gint *source_fds,
1261 const gint *target_fds,
1263 GPid *child_pid_out,
1264 gint *stdin_pipe_out,
1265 gint *stdout_pipe_out,
1266 gint *stderr_pipe_out,
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);
1282 /* source_fds/target_fds isn’t supported on Windows at the moment. */
1285 g_set_error_literal (error, G_SPAWN_ERROR, G_SPAWN_ERROR_INVAL,
1286 "FD redirection is not supported on Windows at the moment");
1290 return fork_exec (NULL,
1291 (flags & G_SPAWN_DO_NOT_REAP_CHILD),
1310 g_spawn_command_line_sync (const gchar *command_line,
1311 gchar **standard_output,
1312 gchar **standard_error,
1319 g_return_val_if_fail (command_line != NULL, FALSE);
1321 if (!g_shell_parse_argv (command_line,
1326 retval = g_spawn_sync (NULL,
1329 G_SPAWN_SEARCH_PATH,
1342 g_spawn_command_line_async (const gchar *command_line,
1348 g_return_val_if_fail (command_line != NULL, FALSE);
1350 if (!g_shell_parse_argv (command_line,
1355 retval = g_spawn_async (NULL,
1358 G_SPAWN_SEARCH_PATH,
1369 g_spawn_close_pid (GPid pid)
1375 g_spawn_check_exit_status (gint exit_status,
1378 gboolean ret = FALSE;
1380 if (exit_status != 0)
1382 g_set_error (error, G_SPAWN_EXIT_ERROR, exit_status,
1383 _("Child process exited with code %ld"),
1384 (long) exit_status);
1395 /* Binary compatibility versions. Not for newly compiled code. */
1397 _GLIB_EXTERN gboolean g_spawn_async_utf8 (const gchar *working_directory,
1401 GSpawnChildSetupFunc child_setup,
1405 _GLIB_EXTERN gboolean g_spawn_async_with_pipes_utf8 (const gchar *working_directory,
1409 GSpawnChildSetupFunc child_setup,
1412 gint *standard_input,
1413 gint *standard_output,
1414 gint *standard_error,
1416 _GLIB_EXTERN gboolean g_spawn_sync_utf8 (const gchar *working_directory,
1420 GSpawnChildSetupFunc child_setup,
1422 gchar **standard_output,
1423 gchar **standard_error,
1426 _GLIB_EXTERN gboolean g_spawn_command_line_sync_utf8 (const gchar *command_line,
1427 gchar **standard_output,
1428 gchar **standard_error,
1431 _GLIB_EXTERN gboolean g_spawn_command_line_async_utf8 (const gchar *command_line,
1435 g_spawn_async_utf8 (const gchar *working_directory,
1439 GSpawnChildSetupFunc child_setup,
1444 return g_spawn_async (working_directory,
1455 g_spawn_async_with_pipes_utf8 (const gchar *working_directory,
1459 GSpawnChildSetupFunc child_setup,
1462 gint *standard_input,
1463 gint *standard_output,
1464 gint *standard_error,
1467 return g_spawn_async_with_pipes (working_directory,
1481 g_spawn_sync_utf8 (const gchar *working_directory,
1485 GSpawnChildSetupFunc child_setup,
1487 gchar **standard_output,
1488 gchar **standard_error,
1492 return g_spawn_sync (working_directory,
1505 g_spawn_command_line_sync_utf8 (const gchar *command_line,
1506 gchar **standard_output,
1507 gchar **standard_error,
1511 return g_spawn_command_line_sync (command_line,
1519 g_spawn_command_line_async_utf8 (const gchar *command_line,
1522 return g_spawn_command_line_async (command_line, error);
1525 #endif /* G_OS_WIN32 */
1527 #endif /* !GSPAWN_HELPER */