1 /* gspawn-win32.c - Process launching on Win32
3 * Copyright 2000 Red Hat, Inc.
4 * Copyright 2003 Tor Lillqvist
6 * GLib is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * GLib 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
17 * License along with GLib; see the file COPYING.LIB. If not, write
18 * to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * Implementation details on Win32.
25 * - There is no way to set the no-inherit flag for
26 * a "file descriptor" in the MS C runtime. The flag is there,
27 * and the dospawn() function uses it, but unfortunately
28 * this flag can only be set when opening the file.
29 * - As there is no fork(), we cannot reliably change directory
30 * before starting the child process. (There might be several threads
31 * running, and the current directory is common for all threads.)
33 * Thus, we must in many cases use a helper program to handle closing
34 * of (inherited) file descriptors and changing of directory. The
35 * helper process is also needed if the standard input, standard
36 * output, or standard error of the process to be run are supposed to
37 * be redirected somewhere.
39 * The structure of the source code in this file is a mess, I know.
42 /* Define this to get some logging all the time */
43 /* #define G_SPAWN_WIN32_DEBUG */
48 #include "gprintfint.h"
64 #ifdef G_SPAWN_WIN32_DEBUG
66 #define SETUP_DEBUG() /* empty */
68 static int debug = -1;
69 #define SETUP_DEBUG() \
74 if (getenv ("G_SPAWN_WIN32_DEBUG") != NULL) \
91 ARG_CHILD_ERR_REPORT = 1,
96 ARG_WORKING_DIRECTORY,
97 ARG_CLOSE_DESCRIPTORS,
101 ARG_COUNT = ARG_PROGRAM
105 dup_noninherited (int fd,
110 DuplicateHandle (GetCurrentProcess (), (LPHANDLE) _get_osfhandle (fd),
111 GetCurrentProcess (), &filehandle,
112 0, FALSE, DUPLICATE_SAME_ACCESS);
114 return _open_osfhandle ((gintptr) filehandle, mode | _O_NOINHERIT);
117 #ifndef GSPAWN_HELPER
120 #define HELPER_PROCESS "gspawn-win64-helper"
122 #define HELPER_PROCESS "gspawn-win32-helper"
126 protect_argv_string (const gchar *string)
128 const gchar *p = string;
131 gboolean need_dblquotes = FALSE;
134 if (*p == ' ' || *p == '\t')
135 need_dblquotes = TRUE;
141 while (*pp && *pp == '\\')
150 q = retval = g_malloc (len + need_dblquotes*2 + 1);
163 while (*pp && *pp == '\\')
180 protect_argv (gchar **argv,
188 *new_argv = g_new (gchar *, argc+1);
190 /* Quote each argv element if necessary, so that it will get
191 * reconstructed correctly in the C runtime startup code. Note that
192 * the unquoting algorithm in the C runtime is really weird, and
193 * rather different than what Unix shells do. See stdargv.c in the C
194 * runtime sources (in the Platform SDK, in src/crt).
196 * Note that an new_argv[0] constructed by this function should
197 * *not* be passed as the filename argument to a spawn* or exec*
198 * family function. That argument should be the real file name
199 * without any quoting.
201 for (i = 0; i < argc; i++)
202 (*new_argv)[i] = protect_argv_string (argv[i]);
204 (*new_argv)[argc] = NULL;
210 g_spawn_error_quark (void)
212 return g_quark_from_static_string ("g-exec-error-quark");
216 g_spawn_async_utf8 (const gchar *working_directory,
220 GSpawnChildSetupFunc child_setup,
225 g_return_val_if_fail (argv != NULL, FALSE);
227 return g_spawn_async_with_pipes_utf8 (working_directory,
237 /* Avoids a danger in threaded situations (calling close()
238 * on a file descriptor twice, and another thread has
239 * re-opened it since the first close)
242 close_and_invalidate (gint *fd)
253 READ_FAILED = 0, /* FALSE */
259 read_data (GString *str,
260 GIOChannel *iochannel,
269 giostatus = g_io_channel_read_chars (iochannel, buf, sizeof (buf), &bytes, NULL);
275 g_string_append_len (str, buf, bytes);
278 else if (giostatus == G_IO_STATUS_AGAIN)
280 else if (giostatus == G_IO_STATUS_ERROR)
282 g_set_error_literal (error, G_SPAWN_ERROR, G_SPAWN_ERROR_READ,
283 _("Failed to read data from child process"));
292 make_pipe (gint p[2],
295 if (_pipe (p, 4096, _O_BINARY) < 0)
297 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
298 _("Failed to create pipe for communicating with child process (%s)"),
306 /* The helper process writes a status report back to us, through a
307 * pipe, consisting of two ints.
310 read_helper_report (int fd,
316 while (bytes < sizeof(gintptr)*2)
321 g_print ("%s:read_helper_report: read %" G_GSIZE_FORMAT "...\n",
323 sizeof(gintptr)*2 - bytes);
325 chunk = read (fd, ((gchar*)report) + bytes,
326 sizeof(gintptr)*2 - bytes);
329 g_print ("...got %d bytes\n", chunk);
333 /* Some weird shit happened, bail out */
335 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
336 _("Failed to read from child pipe (%s)"),
343 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
344 _("Failed to read from child pipe (%s)"),
352 if (bytes < sizeof(gintptr)*2)
359 set_child_error (gintptr report[2],
360 const gchar *working_directory,
365 case CHILD_CHDIR_FAILED:
366 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
367 _("Failed to change to directory '%s' (%s)"),
369 g_strerror (report[1]));
371 case CHILD_SPAWN_FAILED:
372 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
373 _("Failed to execute child process (%s)"),
374 g_strerror (report[1]));
377 g_assert_not_reached ();
382 utf8_charv_to_wcharv (char **utf8_charv,
387 wchar_t **retval = NULL;
390 if (utf8_charv != NULL)
394 while (utf8_charv[n])
396 retval = g_new (wchar_t *, n + 1);
398 for (i = 0; i < n; i++)
400 retval[i] = g_utf8_to_utf16 (utf8_charv[i], -1, NULL, NULL, error);
401 if (retval[i] == NULL)
406 g_free (retval[--i]);
419 do_spawn_directly (gint *exit_status,
420 gboolean do_return_handle,
424 char **protected_argv,
425 GSpawnChildSetupFunc child_setup,
430 const int mode = (exit_status == NULL) ? P_NOWAIT : P_WAIT;
434 GError *conv_error = NULL;
435 gint conv_error_index;
436 wchar_t *wargv0, **wargv, **wenvp;
438 new_argv = (flags & G_SPAWN_FILE_AND_ARGV_ZERO) ? protected_argv + 1 : protected_argv;
440 wargv0 = g_utf8_to_utf16 (argv[0], -1, NULL, NULL, &conv_error);
443 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
444 _("Invalid program name: %s"),
445 conv_error->message);
446 g_error_free (conv_error);
451 if (!utf8_charv_to_wcharv (new_argv, &wargv, &conv_error_index, &conv_error))
453 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
454 _("Invalid string in argument vector at %d: %s"),
455 conv_error_index, conv_error->message);
456 g_error_free (conv_error);
462 if (!utf8_charv_to_wcharv (envp, &wenvp, NULL, &conv_error))
464 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
465 _("Invalid string in environment: %s"),
466 conv_error->message);
467 g_error_free (conv_error);
469 g_strfreev ((gchar **) wargv);
475 (* child_setup) (user_data);
477 if (flags & G_SPAWN_SEARCH_PATH)
479 rc = _wspawnvpe (mode, wargv0, (const wchar_t **) wargv, (const wchar_t **) wenvp);
481 rc = _wspawnvp (mode, wargv0, (const wchar_t **) wargv);
484 rc = _wspawnve (mode, wargv0, (const wchar_t **) wargv, (const wchar_t **) wenvp);
486 rc = _wspawnv (mode, wargv0, (const wchar_t **) wargv);
489 g_strfreev ((gchar **) wargv);
490 g_strfreev ((gchar **) wenvp);
494 if (rc == -1 && saved_errno != 0)
496 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
497 _("Failed to execute child process (%s)"),
498 g_strerror (saved_errno));
502 if (exit_status == NULL)
504 if (child_handle && do_return_handle)
505 *child_handle = (GPid) rc;
508 CloseHandle ((HANDLE) rc);
520 do_spawn_with_pipes (gint *exit_status,
521 gboolean do_return_handle,
522 const gchar *working_directory,
526 GSpawnChildSetupFunc child_setup,
529 gint *standard_input,
530 gint *standard_output,
531 gint *standard_error,
535 char **protected_argv;
536 char args[ARG_COUNT][10];
542 int stdin_pipe[2] = { -1, -1 };
543 int stdout_pipe[2] = { -1, -1 };
544 int stderr_pipe[2] = { -1, -1 };
545 int child_err_report_pipe[2] = { -1, -1 };
546 int helper_sync_pipe[2] = { -1, -1 };
547 gintptr helper_report[2];
548 static gboolean warned_about_child_setup = FALSE;
549 GError *conv_error = NULL;
550 gint conv_error_index;
551 gchar *helper_process;
552 CONSOLE_CURSOR_INFO cursor_info;
553 wchar_t *whelper, **wargv, **wenvp;
554 extern gchar *_glib_get_installation_directory (void);
557 if (child_setup && !warned_about_child_setup)
559 warned_about_child_setup = TRUE;
560 g_warning ("passing a child setup function to the g_spawn functions is pointless and dangerous on Win32");
563 argc = protect_argv (argv, &protected_argv);
565 if (!standard_input && !standard_output && !standard_error &&
566 (flags & G_SPAWN_CHILD_INHERITS_STDIN) &&
567 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL) &&
568 !(flags & G_SPAWN_STDERR_TO_DEV_NULL) &&
569 (working_directory == NULL || !*working_directory) &&
570 (flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN))
572 /* We can do without the helper process */
574 do_spawn_directly (exit_status, do_return_handle, flags,
575 argv, envp, protected_argv,
576 child_setup, user_data, child_handle,
578 g_strfreev (protected_argv);
582 if (standard_input && !make_pipe (stdin_pipe, error))
583 goto cleanup_and_fail;
585 if (standard_output && !make_pipe (stdout_pipe, error))
586 goto cleanup_and_fail;
588 if (standard_error && !make_pipe (stderr_pipe, error))
589 goto cleanup_and_fail;
591 if (!make_pipe (child_err_report_pipe, error))
592 goto cleanup_and_fail;
594 if (!make_pipe (helper_sync_pipe, error))
595 goto cleanup_and_fail;
597 new_argv = g_new (char *, argc + 1 + ARG_COUNT);
598 if (GetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cursor_info))
599 helper_process = HELPER_PROCESS "-console.exe";
601 helper_process = HELPER_PROCESS ".exe";
603 glib_top = _glib_get_installation_directory ();
604 if (glib_top != NULL)
606 helper_process = g_build_filename (glib_top, "bin", helper_process, NULL);
610 helper_process = g_strdup (helper_process);
612 new_argv[0] = protect_argv_string (helper_process);
614 _g_sprintf (args[ARG_CHILD_ERR_REPORT], "%d", child_err_report_pipe[1]);
615 new_argv[ARG_CHILD_ERR_REPORT] = args[ARG_CHILD_ERR_REPORT];
617 /* Make the read end of the child error report pipe
618 * noninherited. Otherwise it will needlessly be inherited by the
619 * helper process, and the started actual user process. As such that
620 * shouldn't harm, but it is unnecessary.
622 child_err_report_pipe[0] = dup_noninherited (child_err_report_pipe[0], _O_RDONLY);
624 if (flags & G_SPAWN_FILE_AND_ARGV_ZERO)
626 /* Overload ARG_CHILD_ERR_REPORT to also encode the
627 * G_SPAWN_FILE_AND_ARGV_ZERO functionality.
629 strcat (args[ARG_CHILD_ERR_REPORT], "#");
632 _g_sprintf (args[ARG_HELPER_SYNC], "%d", helper_sync_pipe[0]);
633 new_argv[ARG_HELPER_SYNC] = args[ARG_HELPER_SYNC];
635 /* Make the write end of the sync pipe noninherited. Otherwise the
636 * helper process will inherit it, and thus if this process happens
637 * to crash before writing the sync byte to the pipe, the helper
638 * process won't read but won't get any EOF either, as it has the
639 * write end open itself.
641 helper_sync_pipe[1] = dup_noninherited (helper_sync_pipe[1], _O_WRONLY);
645 _g_sprintf (args[ARG_STDIN], "%d", stdin_pipe[0]);
646 new_argv[ARG_STDIN] = args[ARG_STDIN];
648 else if (flags & G_SPAWN_CHILD_INHERITS_STDIN)
650 /* Let stdin be alone */
651 new_argv[ARG_STDIN] = "-";
655 /* Keep process from blocking on a read of stdin */
656 new_argv[ARG_STDIN] = "z";
661 _g_sprintf (args[ARG_STDOUT], "%d", stdout_pipe[1]);
662 new_argv[ARG_STDOUT] = args[ARG_STDOUT];
664 else if (flags & G_SPAWN_STDOUT_TO_DEV_NULL)
666 new_argv[ARG_STDOUT] = "z";
670 new_argv[ARG_STDOUT] = "-";
675 _g_sprintf (args[ARG_STDERR], "%d", stderr_pipe[1]);
676 new_argv[ARG_STDERR] = args[ARG_STDERR];
678 else if (flags & G_SPAWN_STDERR_TO_DEV_NULL)
680 new_argv[ARG_STDERR] = "z";
684 new_argv[ARG_STDERR] = "-";
687 if (working_directory && *working_directory)
688 new_argv[ARG_WORKING_DIRECTORY] = protect_argv_string (working_directory);
690 new_argv[ARG_WORKING_DIRECTORY] = g_strdup ("-");
692 if (!(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN))
693 new_argv[ARG_CLOSE_DESCRIPTORS] = "y";
695 new_argv[ARG_CLOSE_DESCRIPTORS] = "-";
697 if (flags & G_SPAWN_SEARCH_PATH)
698 new_argv[ARG_USE_PATH] = "y";
700 new_argv[ARG_USE_PATH] = "-";
702 if (exit_status == NULL)
703 new_argv[ARG_WAIT] = "-";
705 new_argv[ARG_WAIT] = "w";
707 for (i = 0; i <= argc; i++)
708 new_argv[ARG_PROGRAM + i] = protected_argv[i];
714 g_print ("calling %s with argv:\n", helper_process);
715 for (i = 0; i < argc + 1 + ARG_COUNT; i++)
716 g_print ("argv[%d]: %s\n", i, (new_argv[i] ? new_argv[i] : "NULL"));
719 if (!utf8_charv_to_wcharv (new_argv, &wargv, &conv_error_index, &conv_error))
721 if (conv_error_index == ARG_WORKING_DIRECTORY)
722 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
723 _("Invalid working directory: %s"),
724 conv_error->message);
726 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
727 _("Invalid string in argument vector at %d: %s"),
728 conv_error_index - ARG_PROGRAM, conv_error->message);
729 g_error_free (conv_error);
730 g_strfreev (protected_argv);
731 g_free (new_argv[0]);
732 g_free (new_argv[ARG_WORKING_DIRECTORY]);
734 g_free (helper_process);
736 goto cleanup_and_fail;
739 if (!utf8_charv_to_wcharv (envp, &wenvp, NULL, &conv_error))
741 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
742 _("Invalid string in environment: %s"),
743 conv_error->message);
744 g_error_free (conv_error);
745 g_strfreev (protected_argv);
746 g_free (new_argv[0]);
747 g_free (new_argv[ARG_WORKING_DIRECTORY]);
749 g_free (helper_process);
750 g_strfreev ((gchar **) wargv);
752 goto cleanup_and_fail;
756 (* child_setup) (user_data);
758 whelper = g_utf8_to_utf16 (helper_process, -1, NULL, NULL, NULL);
759 g_free (helper_process);
762 rc = _wspawnvpe (P_NOWAIT, whelper, (const wchar_t **) wargv, (const wchar_t **) wenvp);
764 rc = _wspawnvp (P_NOWAIT, whelper, (const wchar_t **) wargv);
769 g_strfreev ((gchar **) wargv);
770 g_strfreev ((gchar **) wenvp);
772 /* Close the other process's ends of the pipes in this process,
773 * otherwise the reader will never get EOF.
775 close_and_invalidate (&child_err_report_pipe[1]);
776 close_and_invalidate (&helper_sync_pipe[0]);
777 close_and_invalidate (&stdin_pipe[0]);
778 close_and_invalidate (&stdout_pipe[1]);
779 close_and_invalidate (&stderr_pipe[1]);
781 g_strfreev (protected_argv);
783 g_free (new_argv[0]);
784 g_free (new_argv[ARG_WORKING_DIRECTORY]);
787 /* Check if gspawn-win32-helper couldn't be run */
788 if (rc == -1 && saved_errno != 0)
790 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
791 _("Failed to execute helper program (%s)"),
792 g_strerror (saved_errno));
793 goto cleanup_and_fail;
796 if (exit_status != NULL)
798 /* Synchronous case. Pass helper's report pipe back to caller,
799 * which takes care of reading it after the grandchild has
802 g_assert (err_report != NULL);
803 *err_report = child_err_report_pipe[0];
804 write (helper_sync_pipe[1], " ", 1);
805 close_and_invalidate (&helper_sync_pipe[1]);
809 /* Asynchronous case. We read the helper's report right away. */
810 if (!read_helper_report (child_err_report_pipe[0], helper_report, error))
811 goto cleanup_and_fail;
813 close_and_invalidate (&child_err_report_pipe[0]);
815 switch (helper_report[0])
818 if (child_handle && do_return_handle)
820 /* rc is our HANDLE for gspawn-win32-helper. It has
821 * told us the HANDLE of its child. Duplicate that into
822 * a HANDLE valid in this process.
824 if (!DuplicateHandle ((HANDLE) rc, (HANDLE) helper_report[1],
825 GetCurrentProcess (), (LPHANDLE) child_handle,
826 0, TRUE, DUPLICATE_SAME_ACCESS))
828 char *emsg = g_win32_error_message (GetLastError ());
829 g_print("%s\n", emsg);
833 else if (child_handle)
835 write (helper_sync_pipe[1], " ", 1);
836 close_and_invalidate (&helper_sync_pipe[1]);
840 write (helper_sync_pipe[1], " ", 1);
841 close_and_invalidate (&helper_sync_pipe[1]);
842 set_child_error (helper_report, working_directory, error);
843 goto cleanup_and_fail;
847 /* Success against all odds! return the information */
850 *standard_input = stdin_pipe[1];
852 *standard_output = stdout_pipe[0];
854 *standard_error = stderr_pipe[0];
856 CloseHandle ((HANDLE) rc);
863 CloseHandle ((HANDLE) rc);
864 if (child_err_report_pipe[0] != -1)
865 close (child_err_report_pipe[0]);
866 if (child_err_report_pipe[1] != -1)
867 close (child_err_report_pipe[1]);
868 if (helper_sync_pipe[0] != -1)
869 close (helper_sync_pipe[0]);
870 if (helper_sync_pipe[1] != -1)
871 close (helper_sync_pipe[1]);
872 if (stdin_pipe[0] != -1)
873 close (stdin_pipe[0]);
874 if (stdin_pipe[1] != -1)
875 close (stdin_pipe[1]);
876 if (stdout_pipe[0] != -1)
877 close (stdout_pipe[0]);
878 if (stdout_pipe[1] != -1)
879 close (stdout_pipe[1]);
880 if (stderr_pipe[0] != -1)
881 close (stderr_pipe[0]);
882 if (stderr_pipe[1] != -1)
883 close (stderr_pipe[1]);
889 g_spawn_sync_utf8 (const gchar *working_directory,
893 GSpawnChildSetupFunc child_setup,
895 gchar **standard_output,
896 gchar **standard_error,
902 gint reportpipe = -1;
903 GIOChannel *outchannel = NULL;
904 GIOChannel *errchannel = NULL;
905 GPollFD outfd, errfd;
911 GString *outstr = NULL;
912 GString *errstr = NULL;
916 g_return_val_if_fail (argv != NULL, FALSE);
917 g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
918 g_return_val_if_fail (standard_output == NULL ||
919 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
920 g_return_val_if_fail (standard_error == NULL ||
921 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
923 /* Just to ensure segfaults if callers try to use
924 * these when an error is reported.
927 *standard_output = NULL;
930 *standard_error = NULL;
932 if (!do_spawn_with_pipes (&status,
942 standard_output ? &outpipe : NULL,
943 standard_error ? &errpipe : NULL,
948 /* Read data from child. */
954 outstr = g_string_new (NULL);
955 outchannel = g_io_channel_win32_new_fd (outpipe);
956 g_io_channel_set_encoding (outchannel, NULL, NULL);
957 g_io_channel_set_buffered (outchannel, FALSE);
958 g_io_channel_win32_make_pollfd (outchannel,
959 G_IO_IN | G_IO_ERR | G_IO_HUP,
962 g_print ("outfd=%p\n", (HANDLE) outfd.fd);
967 errstr = g_string_new (NULL);
968 errchannel = g_io_channel_win32_new_fd (errpipe);
969 g_io_channel_set_encoding (errchannel, NULL, NULL);
970 g_io_channel_set_buffered (errchannel, FALSE);
971 g_io_channel_win32_make_pollfd (errchannel,
972 G_IO_IN | G_IO_ERR | G_IO_HUP,
975 g_print ("errfd=%p\n", (HANDLE) errfd.fd);
978 /* Read data until we get EOF on all pipes. */
979 while (!failed && (outpipe >= 0 || errpipe >= 0))
996 g_print ("g_spawn_sync: calling g_io_channel_win32_poll, nfds=%d\n",
999 ret = g_io_channel_win32_poll (fds, nfds, -1);
1005 g_set_error_literal (error, G_SPAWN_ERROR, G_SPAWN_ERROR_READ,
1006 _("Unexpected error in g_io_channel_win32_poll() reading data from a child process"));
1011 if (outpipe >= 0 && (fds[outindex].revents & G_IO_IN))
1013 switch (read_data (outstr, outchannel, error))
1017 g_print ("g_spawn_sync: outchannel: READ_FAILED\n");
1022 g_print ("g_spawn_sync: outchannel: READ_EOF\n");
1023 g_io_channel_unref (outchannel);
1025 close_and_invalidate (&outpipe);
1029 g_print ("g_spawn_sync: outchannel: OK\n");
1037 if (errpipe >= 0 && (fds[errindex].revents & G_IO_IN))
1039 switch (read_data (errstr, errchannel, error))
1043 g_print ("g_spawn_sync: errchannel: READ_FAILED\n");
1048 g_print ("g_spawn_sync: errchannel: READ_EOF\n");
1049 g_io_channel_unref (errchannel);
1051 close_and_invalidate (&errpipe);
1055 g_print ("g_spawn_sync: errchannel: OK\n");
1064 if (reportpipe == -1)
1066 /* No helper process, exit status of actual spawned process
1067 * already available.
1070 *exit_status = status;
1074 /* Helper process was involved. Read its report now after the
1075 * grandchild has finished.
1077 gintptr helper_report[2];
1079 if (!read_helper_report (reportpipe, helper_report, error))
1083 switch (helper_report[0])
1085 case CHILD_NO_ERROR:
1087 *exit_status = helper_report[1];
1090 set_child_error (helper_report, working_directory, error);
1095 close_and_invalidate (&reportpipe);
1099 /* These should only be open still if we had an error. */
1101 if (outchannel != NULL)
1102 g_io_channel_unref (outchannel);
1103 if (errchannel != NULL)
1104 g_io_channel_unref (errchannel);
1106 close_and_invalidate (&outpipe);
1108 close_and_invalidate (&errpipe);
1113 g_string_free (outstr, TRUE);
1115 g_string_free (errstr, TRUE);
1121 if (standard_output)
1122 *standard_output = g_string_free (outstr, FALSE);
1125 *standard_error = g_string_free (errstr, FALSE);
1132 g_spawn_async_with_pipes_utf8 (const gchar *working_directory,
1136 GSpawnChildSetupFunc child_setup,
1139 gint *standard_input,
1140 gint *standard_output,
1141 gint *standard_error,
1144 g_return_val_if_fail (argv != NULL, FALSE);
1145 g_return_val_if_fail (standard_output == NULL ||
1146 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
1147 g_return_val_if_fail (standard_error == NULL ||
1148 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
1149 /* can't inherit stdin if we have an input pipe. */
1150 g_return_val_if_fail (standard_input == NULL ||
1151 !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
1153 return do_spawn_with_pipes (NULL,
1154 (flags & G_SPAWN_DO_NOT_REAP_CHILD),
1170 g_spawn_command_line_sync_utf8 (const gchar *command_line,
1171 gchar **standard_output,
1172 gchar **standard_error,
1179 g_return_val_if_fail (command_line != NULL, FALSE);
1181 if (!g_shell_parse_argv (command_line,
1186 retval = g_spawn_sync_utf8 (NULL,
1189 G_SPAWN_SEARCH_PATH,
1202 g_spawn_command_line_async_utf8 (const gchar *command_line,
1208 g_return_val_if_fail (command_line != NULL, FALSE);
1210 if (!g_shell_parse_argv (command_line,
1215 retval = g_spawn_async_utf8 (NULL,
1218 G_SPAWN_SEARCH_PATH,
1229 g_spawn_close_pid (GPid pid)
1234 #if !defined (_WIN64)
1236 /* Binary compatibility versions that take system codepage pathnames,
1237 * argument vectors and environments. These get used only by code
1238 * built against 2.8.1 or earlier. Code built against 2.8.2 or later
1239 * will use the _utf8 versions above (see the #defines in gspawn.h).
1242 #undef g_spawn_async
1243 #undef g_spawn_async_with_pipes
1245 #undef g_spawn_command_line_sync
1246 #undef g_spawn_command_line_async
1249 setup_utf8_copies (const gchar *working_directory,
1250 gchar **utf8_working_directory,
1259 if (working_directory == NULL)
1260 *utf8_working_directory = NULL;
1263 GError *conv_error = NULL;
1265 *utf8_working_directory = g_locale_to_utf8 (working_directory, -1, NULL, NULL, &conv_error);
1266 if (*utf8_working_directory == NULL)
1268 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
1269 _("Invalid working directory: %s"),
1270 conv_error->message);
1271 g_error_free (conv_error);
1279 *utf8_argv = g_new (gchar *, argc + 1);
1280 for (i = 0; i < argc; i++)
1282 GError *conv_error = NULL;
1284 (*utf8_argv)[i] = g_locale_to_utf8 (argv[i], -1, NULL, NULL, &conv_error);
1285 if ((*utf8_argv)[i] == NULL)
1287 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
1288 _("Invalid string in argument vector at %d: %s"),
1289 i, conv_error->message);
1290 g_error_free (conv_error);
1292 g_strfreev (*utf8_argv);
1295 g_free (*utf8_working_directory);
1296 *utf8_working_directory = NULL;
1301 (*utf8_argv)[argc] = NULL;
1312 *utf8_envp = g_new (gchar *, envc + 1);
1313 for (i = 0; i < envc; i++)
1315 GError *conv_error = NULL;
1317 (*utf8_envp)[i] = g_locale_to_utf8 (envp[i], -1, NULL, NULL, &conv_error);
1318 if ((*utf8_envp)[i] == NULL)
1320 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
1321 _("Invalid string in environment: %s"),
1322 conv_error->message);
1323 g_error_free (conv_error);
1325 g_strfreev (*utf8_envp);
1328 g_strfreev (*utf8_argv);
1331 g_free (*utf8_working_directory);
1332 *utf8_working_directory = NULL;
1337 (*utf8_envp)[envc] = NULL;
1343 free_utf8_copies (gchar *utf8_working_directory,
1347 g_free (utf8_working_directory);
1348 g_strfreev (utf8_argv);
1349 g_strfreev (utf8_envp);
1353 g_spawn_async_with_pipes (const gchar *working_directory,
1357 GSpawnChildSetupFunc child_setup,
1360 gint *standard_input,
1361 gint *standard_output,
1362 gint *standard_error,
1365 gchar *utf8_working_directory;
1370 if (!setup_utf8_copies (working_directory, &utf8_working_directory,
1376 retval = g_spawn_async_with_pipes_utf8 (utf8_working_directory,
1377 utf8_argv, utf8_envp,
1378 flags, child_setup, user_data,
1380 standard_input, standard_output, standard_error,
1383 free_utf8_copies (utf8_working_directory, utf8_argv, utf8_envp);
1389 g_spawn_async (const gchar *working_directory,
1393 GSpawnChildSetupFunc child_setup,
1398 return g_spawn_async_with_pipes (working_directory,
1409 g_spawn_sync (const gchar *working_directory,
1413 GSpawnChildSetupFunc child_setup,
1415 gchar **standard_output,
1416 gchar **standard_error,
1420 gchar *utf8_working_directory;
1425 if (!setup_utf8_copies (working_directory, &utf8_working_directory,
1431 retval = g_spawn_sync_utf8 (utf8_working_directory,
1432 utf8_argv, utf8_envp,
1433 flags, child_setup, user_data,
1434 standard_output, standard_error, exit_status,
1437 free_utf8_copies (utf8_working_directory, utf8_argv, utf8_envp);
1443 g_spawn_command_line_sync (const gchar *command_line,
1444 gchar **standard_output,
1445 gchar **standard_error,
1452 g_return_val_if_fail (command_line != NULL, FALSE);
1454 if (!g_shell_parse_argv (command_line,
1459 retval = g_spawn_sync (NULL,
1462 G_SPAWN_SEARCH_PATH,
1475 g_spawn_command_line_async (const gchar *command_line,
1481 g_return_val_if_fail (command_line != NULL, FALSE);
1483 if (!g_shell_parse_argv (command_line,
1488 retval = g_spawn_async (NULL,
1491 G_SPAWN_SEARCH_PATH,
1501 #endif /* !_WIN64 */
1503 #endif /* !GSPAWN_HELPER */
1505 #define __G_SPAWN_C__
1506 #include "galiasdef.c"