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"
63 #ifdef G_SPAWN_WIN32_DEBUG
65 #define SETUP_DEBUG() /* empty */
67 static int debug = -1;
68 #define SETUP_DEBUG() \
73 if (getenv ("G_SPAWN_WIN32_DEBUG") != NULL) \
90 ARG_CHILD_ERR_REPORT = 1,
95 ARG_WORKING_DIRECTORY,
96 ARG_CLOSE_DESCRIPTORS,
100 ARG_COUNT = ARG_PROGRAM
104 dup_noninherited (int fd,
109 DuplicateHandle (GetCurrentProcess (), (LPHANDLE) _get_osfhandle (fd),
110 GetCurrentProcess (), &filehandle,
111 0, FALSE, DUPLICATE_SAME_ACCESS);
113 return _open_osfhandle ((gintptr) filehandle, mode | _O_NOINHERIT);
116 #ifndef GSPAWN_HELPER
119 #define HELPER_PROCESS "gspawn-win64-helper"
121 #define HELPER_PROCESS "gspawn-win32-helper"
125 protect_argv_string (const gchar *string)
127 const gchar *p = string;
130 gboolean need_dblquotes = FALSE;
133 if (*p == ' ' || *p == '\t')
134 need_dblquotes = TRUE;
140 while (*pp && *pp == '\\')
149 q = retval = g_malloc (len + need_dblquotes*2 + 1);
162 while (*pp && *pp == '\\')
179 protect_argv (gchar **argv,
187 *new_argv = g_new (gchar *, argc+1);
189 /* Quote each argv element if necessary, so that it will get
190 * reconstructed correctly in the C runtime startup code. Note that
191 * the unquoting algorithm in the C runtime is really weird, and
192 * rather different than what Unix shells do. See stdargv.c in the C
193 * runtime sources (in the Platform SDK, in src/crt).
195 * Note that an new_argv[0] constructed by this function should
196 * *not* be passed as the filename argument to a spawn* or exec*
197 * family function. That argument should be the real file name
198 * without any quoting.
200 for (i = 0; i < argc; i++)
201 (*new_argv)[i] = protect_argv_string (argv[i]);
203 (*new_argv)[argc] = NULL;
209 g_spawn_error_quark (void)
211 return g_quark_from_static_string ("g-exec-error-quark");
215 g_spawn_exit_error_quark (void)
217 return g_quark_from_static_string ("g-spawn-exit-error-quark");
221 g_spawn_async_utf8 (const gchar *working_directory,
225 GSpawnChildSetupFunc child_setup,
230 g_return_val_if_fail (argv != NULL, FALSE);
232 return g_spawn_async_with_pipes_utf8 (working_directory,
242 /* Avoids a danger in threaded situations (calling close()
243 * on a file descriptor twice, and another thread has
244 * re-opened it since the first close)
247 close_and_invalidate (gint *fd)
258 READ_FAILED = 0, /* FALSE */
264 read_data (GString *str,
265 GIOChannel *iochannel,
274 giostatus = g_io_channel_read_chars (iochannel, buf, sizeof (buf), &bytes, NULL);
280 g_string_append_len (str, buf, bytes);
283 else if (giostatus == G_IO_STATUS_AGAIN)
285 else if (giostatus == G_IO_STATUS_ERROR)
287 g_set_error_literal (error, G_SPAWN_ERROR, G_SPAWN_ERROR_READ,
288 _("Failed to read data from child process"));
297 make_pipe (gint p[2],
300 if (_pipe (p, 4096, _O_BINARY) < 0)
304 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
305 _("Failed to create pipe for communicating with child process (%s)"),
313 /* The helper process writes a status report back to us, through a
314 * pipe, consisting of two ints.
317 read_helper_report (int fd,
323 while (bytes < sizeof(gintptr)*2)
328 g_print ("%s:read_helper_report: read %" G_GSIZE_FORMAT "...\n",
330 sizeof(gintptr)*2 - bytes);
332 chunk = read (fd, ((gchar*)report) + bytes,
333 sizeof(gintptr)*2 - bytes);
336 g_print ("...got %d bytes\n", chunk);
342 /* Some weird shit happened, bail out */
343 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
344 _("Failed to read from child pipe (%s)"),
351 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
352 _("Failed to read from child pipe (%s)"),
360 if (bytes < sizeof(gintptr)*2)
367 set_child_error (gintptr report[2],
368 const gchar *working_directory,
373 case CHILD_CHDIR_FAILED:
374 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
375 _("Failed to change to directory '%s' (%s)"),
377 g_strerror (report[1]));
379 case CHILD_SPAWN_FAILED:
380 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
381 _("Failed to execute child process (%s)"),
382 g_strerror (report[1]));
385 g_assert_not_reached ();
390 utf8_charv_to_wcharv (char **utf8_charv,
395 wchar_t **retval = NULL;
398 if (utf8_charv != NULL)
402 while (utf8_charv[n])
404 retval = g_new (wchar_t *, n + 1);
406 for (i = 0; i < n; i++)
408 retval[i] = g_utf8_to_utf16 (utf8_charv[i], -1, NULL, NULL, error);
409 if (retval[i] == NULL)
414 g_free (retval[--i]);
427 do_spawn_directly (gint *exit_status,
428 gboolean do_return_handle,
432 char **protected_argv,
436 const int mode = (exit_status == NULL) ? P_NOWAIT : P_WAIT;
440 GError *conv_error = NULL;
441 gint conv_error_index;
442 wchar_t *wargv0, **wargv, **wenvp;
444 new_argv = (flags & G_SPAWN_FILE_AND_ARGV_ZERO) ? protected_argv + 1 : protected_argv;
446 wargv0 = g_utf8_to_utf16 (argv[0], -1, NULL, NULL, &conv_error);
449 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
450 _("Invalid program name: %s"),
451 conv_error->message);
452 g_error_free (conv_error);
457 if (!utf8_charv_to_wcharv (new_argv, &wargv, &conv_error_index, &conv_error))
459 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
460 _("Invalid string in argument vector at %d: %s"),
461 conv_error_index, conv_error->message);
462 g_error_free (conv_error);
468 if (!utf8_charv_to_wcharv (envp, &wenvp, NULL, &conv_error))
470 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
471 _("Invalid string in environment: %s"),
472 conv_error->message);
473 g_error_free (conv_error);
475 g_strfreev ((gchar **) wargv);
480 if (flags & G_SPAWN_SEARCH_PATH)
482 rc = _wspawnvpe (mode, wargv0, (const wchar_t **) wargv, (const wchar_t **) wenvp);
484 rc = _wspawnvp (mode, wargv0, (const wchar_t **) wargv);
487 rc = _wspawnve (mode, wargv0, (const wchar_t **) wargv, (const wchar_t **) wenvp);
489 rc = _wspawnv (mode, wargv0, (const wchar_t **) wargv);
492 g_strfreev ((gchar **) wargv);
493 g_strfreev ((gchar **) wenvp);
497 if (rc == -1 && saved_errno != 0)
499 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
500 _("Failed to execute child process (%s)"),
501 g_strerror (saved_errno));
505 if (exit_status == NULL)
507 if (child_handle && do_return_handle)
508 *child_handle = (GPid) rc;
511 CloseHandle ((HANDLE) rc);
523 do_spawn_with_pipes (gint *exit_status,
524 gboolean do_return_handle,
525 const gchar *working_directory,
529 GSpawnChildSetupFunc child_setup,
531 gint *standard_input,
532 gint *standard_output,
533 gint *standard_error,
537 char **protected_argv;
538 char args[ARG_COUNT][10];
544 int stdin_pipe[2] = { -1, -1 };
545 int stdout_pipe[2] = { -1, -1 };
546 int stderr_pipe[2] = { -1, -1 };
547 int child_err_report_pipe[2] = { -1, -1 };
548 int helper_sync_pipe[2] = { -1, -1 };
549 gintptr helper_report[2];
550 static gboolean warned_about_child_setup = FALSE;
551 GError *conv_error = NULL;
552 gint conv_error_index;
553 gchar *helper_process;
554 CONSOLE_CURSOR_INFO cursor_info;
555 wchar_t *whelper, **wargv, **wenvp;
556 extern gchar *_glib_get_dll_directory (void);
557 gchar *glib_dll_directory;
559 if (child_setup && !warned_about_child_setup)
561 warned_about_child_setup = TRUE;
562 g_warning ("passing a child setup function to the g_spawn functions is pointless on Windows and it is ignored");
565 argc = protect_argv (argv, &protected_argv);
567 if (!standard_input && !standard_output && !standard_error &&
568 (flags & G_SPAWN_CHILD_INHERITS_STDIN) &&
569 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL) &&
570 !(flags & G_SPAWN_STDERR_TO_DEV_NULL) &&
571 (working_directory == NULL || !*working_directory) &&
572 (flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN))
574 /* We can do without the helper process */
576 do_spawn_directly (exit_status, do_return_handle, flags,
577 argv, envp, protected_argv,
578 child_handle, error);
579 g_strfreev (protected_argv);
583 if (standard_input && !make_pipe (stdin_pipe, error))
584 goto cleanup_and_fail;
586 if (standard_output && !make_pipe (stdout_pipe, error))
587 goto cleanup_and_fail;
589 if (standard_error && !make_pipe (stderr_pipe, error))
590 goto cleanup_and_fail;
592 if (!make_pipe (child_err_report_pipe, error))
593 goto cleanup_and_fail;
595 if (!make_pipe (helper_sync_pipe, error))
596 goto cleanup_and_fail;
598 new_argv = g_new (char *, argc + 1 + ARG_COUNT);
599 if (GetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cursor_info))
600 helper_process = HELPER_PROCESS "-console.exe";
602 helper_process = HELPER_PROCESS ".exe";
604 glib_dll_directory = _glib_get_dll_directory ();
605 if (glib_dll_directory != NULL)
607 helper_process = g_build_filename (glib_dll_directory, helper_process, NULL);
608 g_free (glib_dll_directory);
611 helper_process = g_strdup (helper_process);
613 new_argv[0] = protect_argv_string (helper_process);
615 _g_sprintf (args[ARG_CHILD_ERR_REPORT], "%d", child_err_report_pipe[1]);
616 new_argv[ARG_CHILD_ERR_REPORT] = args[ARG_CHILD_ERR_REPORT];
618 /* Make the read end of the child error report pipe
619 * noninherited. Otherwise it will needlessly be inherited by the
620 * helper process, and the started actual user process. As such that
621 * shouldn't harm, but it is unnecessary.
623 child_err_report_pipe[0] = dup_noninherited (child_err_report_pipe[0], _O_RDONLY);
625 if (flags & G_SPAWN_FILE_AND_ARGV_ZERO)
627 /* Overload ARG_CHILD_ERR_REPORT to also encode the
628 * G_SPAWN_FILE_AND_ARGV_ZERO functionality.
630 strcat (args[ARG_CHILD_ERR_REPORT], "#");
633 _g_sprintf (args[ARG_HELPER_SYNC], "%d", helper_sync_pipe[0]);
634 new_argv[ARG_HELPER_SYNC] = args[ARG_HELPER_SYNC];
636 /* Make the write end of the sync pipe noninherited. Otherwise the
637 * helper process will inherit it, and thus if this process happens
638 * to crash before writing the sync byte to the pipe, the helper
639 * process won't read but won't get any EOF either, as it has the
640 * write end open itself.
642 helper_sync_pipe[1] = dup_noninherited (helper_sync_pipe[1], _O_WRONLY);
646 _g_sprintf (args[ARG_STDIN], "%d", stdin_pipe[0]);
647 new_argv[ARG_STDIN] = args[ARG_STDIN];
649 else if (flags & G_SPAWN_CHILD_INHERITS_STDIN)
651 /* Let stdin be alone */
652 new_argv[ARG_STDIN] = "-";
656 /* Keep process from blocking on a read of stdin */
657 new_argv[ARG_STDIN] = "z";
662 _g_sprintf (args[ARG_STDOUT], "%d", stdout_pipe[1]);
663 new_argv[ARG_STDOUT] = args[ARG_STDOUT];
665 else if (flags & G_SPAWN_STDOUT_TO_DEV_NULL)
667 new_argv[ARG_STDOUT] = "z";
671 new_argv[ARG_STDOUT] = "-";
676 _g_sprintf (args[ARG_STDERR], "%d", stderr_pipe[1]);
677 new_argv[ARG_STDERR] = args[ARG_STDERR];
679 else if (flags & G_SPAWN_STDERR_TO_DEV_NULL)
681 new_argv[ARG_STDERR] = "z";
685 new_argv[ARG_STDERR] = "-";
688 if (working_directory && *working_directory)
689 new_argv[ARG_WORKING_DIRECTORY] = protect_argv_string (working_directory);
691 new_argv[ARG_WORKING_DIRECTORY] = g_strdup ("-");
693 if (!(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN))
694 new_argv[ARG_CLOSE_DESCRIPTORS] = "y";
696 new_argv[ARG_CLOSE_DESCRIPTORS] = "-";
698 if (flags & G_SPAWN_SEARCH_PATH)
699 new_argv[ARG_USE_PATH] = "y";
701 new_argv[ARG_USE_PATH] = "-";
703 if (exit_status == NULL)
704 new_argv[ARG_WAIT] = "-";
706 new_argv[ARG_WAIT] = "w";
708 for (i = 0; i <= argc; i++)
709 new_argv[ARG_PROGRAM + i] = protected_argv[i];
715 g_print ("calling %s with argv:\n", helper_process);
716 for (i = 0; i < argc + 1 + ARG_COUNT; i++)
717 g_print ("argv[%d]: %s\n", i, (new_argv[i] ? new_argv[i] : "NULL"));
720 if (!utf8_charv_to_wcharv (new_argv, &wargv, &conv_error_index, &conv_error))
722 if (conv_error_index == ARG_WORKING_DIRECTORY)
723 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
724 _("Invalid working directory: %s"),
725 conv_error->message);
727 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
728 _("Invalid string in argument vector at %d: %s"),
729 conv_error_index - ARG_PROGRAM, conv_error->message);
730 g_error_free (conv_error);
731 g_strfreev (protected_argv);
732 g_free (new_argv[0]);
733 g_free (new_argv[ARG_WORKING_DIRECTORY]);
735 g_free (helper_process);
737 goto cleanup_and_fail;
740 if (!utf8_charv_to_wcharv (envp, &wenvp, NULL, &conv_error))
742 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
743 _("Invalid string in environment: %s"),
744 conv_error->message);
745 g_error_free (conv_error);
746 g_strfreev (protected_argv);
747 g_free (new_argv[0]);
748 g_free (new_argv[ARG_WORKING_DIRECTORY]);
750 g_free (helper_process);
751 g_strfreev ((gchar **) wargv);
753 goto cleanup_and_fail;
756 whelper = g_utf8_to_utf16 (helper_process, -1, NULL, NULL, NULL);
757 g_free (helper_process);
760 rc = _wspawnvpe (P_NOWAIT, whelper, (const wchar_t **) wargv, (const wchar_t **) wenvp);
762 rc = _wspawnvp (P_NOWAIT, whelper, (const wchar_t **) wargv);
767 g_strfreev ((gchar **) wargv);
768 g_strfreev ((gchar **) wenvp);
770 /* Close the other process's ends of the pipes in this process,
771 * otherwise the reader will never get EOF.
773 close_and_invalidate (&child_err_report_pipe[1]);
774 close_and_invalidate (&helper_sync_pipe[0]);
775 close_and_invalidate (&stdin_pipe[0]);
776 close_and_invalidate (&stdout_pipe[1]);
777 close_and_invalidate (&stderr_pipe[1]);
779 g_strfreev (protected_argv);
781 g_free (new_argv[0]);
782 g_free (new_argv[ARG_WORKING_DIRECTORY]);
785 /* Check if gspawn-win32-helper couldn't be run */
786 if (rc == -1 && saved_errno != 0)
788 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
789 _("Failed to execute helper program (%s)"),
790 g_strerror (saved_errno));
791 goto cleanup_and_fail;
794 if (exit_status != NULL)
796 /* Synchronous case. Pass helper's report pipe back to caller,
797 * which takes care of reading it after the grandchild has
800 g_assert (err_report != NULL);
801 *err_report = child_err_report_pipe[0];
802 write (helper_sync_pipe[1], " ", 1);
803 close_and_invalidate (&helper_sync_pipe[1]);
807 /* Asynchronous case. We read the helper's report right away. */
808 if (!read_helper_report (child_err_report_pipe[0], helper_report, error))
809 goto cleanup_and_fail;
811 close_and_invalidate (&child_err_report_pipe[0]);
813 switch (helper_report[0])
816 if (child_handle && do_return_handle)
818 /* rc is our HANDLE for gspawn-win32-helper. It has
819 * told us the HANDLE of its child. Duplicate that into
820 * a HANDLE valid in this process.
822 if (!DuplicateHandle ((HANDLE) rc, (HANDLE) helper_report[1],
823 GetCurrentProcess (), (LPHANDLE) child_handle,
824 0, TRUE, DUPLICATE_SAME_ACCESS))
826 char *emsg = g_win32_error_message (GetLastError ());
827 g_print("%s\n", emsg);
831 else if (child_handle)
833 write (helper_sync_pipe[1], " ", 1);
834 close_and_invalidate (&helper_sync_pipe[1]);
838 write (helper_sync_pipe[1], " ", 1);
839 close_and_invalidate (&helper_sync_pipe[1]);
840 set_child_error (helper_report, working_directory, error);
841 goto cleanup_and_fail;
845 /* Success against all odds! return the information */
848 *standard_input = stdin_pipe[1];
850 *standard_output = stdout_pipe[0];
852 *standard_error = stderr_pipe[0];
854 CloseHandle ((HANDLE) rc);
861 CloseHandle ((HANDLE) rc);
862 if (child_err_report_pipe[0] != -1)
863 close (child_err_report_pipe[0]);
864 if (child_err_report_pipe[1] != -1)
865 close (child_err_report_pipe[1]);
866 if (helper_sync_pipe[0] != -1)
867 close (helper_sync_pipe[0]);
868 if (helper_sync_pipe[1] != -1)
869 close (helper_sync_pipe[1]);
870 if (stdin_pipe[0] != -1)
871 close (stdin_pipe[0]);
872 if (stdin_pipe[1] != -1)
873 close (stdin_pipe[1]);
874 if (stdout_pipe[0] != -1)
875 close (stdout_pipe[0]);
876 if (stdout_pipe[1] != -1)
877 close (stdout_pipe[1]);
878 if (stderr_pipe[0] != -1)
879 close (stderr_pipe[0]);
880 if (stderr_pipe[1] != -1)
881 close (stderr_pipe[1]);
887 g_spawn_sync_utf8 (const gchar *working_directory,
891 GSpawnChildSetupFunc child_setup,
893 gchar **standard_output,
894 gchar **standard_error,
900 gint reportpipe = -1;
901 GIOChannel *outchannel = NULL;
902 GIOChannel *errchannel = NULL;
903 GPollFD outfd, errfd;
909 GString *outstr = NULL;
910 GString *errstr = NULL;
914 g_return_val_if_fail (argv != NULL, FALSE);
915 g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
916 g_return_val_if_fail (standard_output == NULL ||
917 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
918 g_return_val_if_fail (standard_error == NULL ||
919 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
921 /* Just to ensure segfaults if callers try to use
922 * these when an error is reported.
925 *standard_output = NULL;
928 *standard_error = NULL;
930 if (!do_spawn_with_pipes (&status,
939 standard_output ? &outpipe : NULL,
940 standard_error ? &errpipe : NULL,
945 /* Read data from child. */
951 outstr = g_string_new (NULL);
952 outchannel = g_io_channel_win32_new_fd (outpipe);
953 g_io_channel_set_encoding (outchannel, NULL, NULL);
954 g_io_channel_set_buffered (outchannel, FALSE);
955 g_io_channel_win32_make_pollfd (outchannel,
956 G_IO_IN | G_IO_ERR | G_IO_HUP,
959 g_print ("outfd=%p\n", (HANDLE) outfd.fd);
964 errstr = g_string_new (NULL);
965 errchannel = g_io_channel_win32_new_fd (errpipe);
966 g_io_channel_set_encoding (errchannel, NULL, NULL);
967 g_io_channel_set_buffered (errchannel, FALSE);
968 g_io_channel_win32_make_pollfd (errchannel,
969 G_IO_IN | G_IO_ERR | G_IO_HUP,
972 g_print ("errfd=%p\n", (HANDLE) errfd.fd);
975 /* Read data until we get EOF on all pipes. */
976 while (!failed && (outpipe >= 0 || errpipe >= 0))
993 g_print ("g_spawn_sync: calling g_io_channel_win32_poll, nfds=%d\n",
996 ret = g_io_channel_win32_poll (fds, nfds, -1);
1002 g_set_error_literal (error, G_SPAWN_ERROR, G_SPAWN_ERROR_READ,
1003 _("Unexpected error in g_io_channel_win32_poll() reading data from a child process"));
1008 if (outpipe >= 0 && (fds[outindex].revents & G_IO_IN))
1010 switch (read_data (outstr, outchannel, error))
1014 g_print ("g_spawn_sync: outchannel: READ_FAILED\n");
1019 g_print ("g_spawn_sync: outchannel: READ_EOF\n");
1020 g_io_channel_unref (outchannel);
1022 close_and_invalidate (&outpipe);
1026 g_print ("g_spawn_sync: outchannel: OK\n");
1034 if (errpipe >= 0 && (fds[errindex].revents & G_IO_IN))
1036 switch (read_data (errstr, errchannel, error))
1040 g_print ("g_spawn_sync: errchannel: READ_FAILED\n");
1045 g_print ("g_spawn_sync: errchannel: READ_EOF\n");
1046 g_io_channel_unref (errchannel);
1048 close_and_invalidate (&errpipe);
1052 g_print ("g_spawn_sync: errchannel: OK\n");
1061 if (reportpipe == -1)
1063 /* No helper process, exit status of actual spawned process
1064 * already available.
1067 *exit_status = status;
1071 /* Helper process was involved. Read its report now after the
1072 * grandchild has finished.
1074 gintptr helper_report[2];
1076 if (!read_helper_report (reportpipe, helper_report, error))
1080 switch (helper_report[0])
1082 case CHILD_NO_ERROR:
1084 *exit_status = helper_report[1];
1087 set_child_error (helper_report, working_directory, error);
1092 close_and_invalidate (&reportpipe);
1096 /* These should only be open still if we had an error. */
1098 if (outchannel != NULL)
1099 g_io_channel_unref (outchannel);
1100 if (errchannel != NULL)
1101 g_io_channel_unref (errchannel);
1103 close_and_invalidate (&outpipe);
1105 close_and_invalidate (&errpipe);
1110 g_string_free (outstr, TRUE);
1112 g_string_free (errstr, TRUE);
1118 if (standard_output)
1119 *standard_output = g_string_free (outstr, FALSE);
1122 *standard_error = g_string_free (errstr, FALSE);
1129 g_spawn_async_with_pipes_utf8 (const gchar *working_directory,
1133 GSpawnChildSetupFunc child_setup,
1136 gint *standard_input,
1137 gint *standard_output,
1138 gint *standard_error,
1141 g_return_val_if_fail (argv != NULL, FALSE);
1142 g_return_val_if_fail (standard_output == NULL ||
1143 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
1144 g_return_val_if_fail (standard_error == NULL ||
1145 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
1146 /* can't inherit stdin if we have an input pipe. */
1147 g_return_val_if_fail (standard_input == NULL ||
1148 !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
1150 return do_spawn_with_pipes (NULL,
1151 (flags & G_SPAWN_DO_NOT_REAP_CHILD),
1166 g_spawn_command_line_sync_utf8 (const gchar *command_line,
1167 gchar **standard_output,
1168 gchar **standard_error,
1175 g_return_val_if_fail (command_line != NULL, FALSE);
1177 if (!g_shell_parse_argv (command_line,
1182 retval = g_spawn_sync_utf8 (NULL,
1185 G_SPAWN_SEARCH_PATH,
1198 g_spawn_command_line_async_utf8 (const gchar *command_line,
1204 g_return_val_if_fail (command_line != NULL, FALSE);
1206 if (!g_shell_parse_argv (command_line,
1211 retval = g_spawn_async_utf8 (NULL,
1214 G_SPAWN_SEARCH_PATH,
1225 g_spawn_close_pid (GPid pid)
1231 g_spawn_check_exit_status (gint exit_status,
1234 gboolean ret = FALSE;
1236 if (exit_status != 0)
1238 g_set_error (error, G_SPAWN_EXIT_ERROR, exit_status,
1239 _("Child process exited with code %ld"),
1240 (long) exit_status);
1249 #if !defined (_WIN64)
1251 /* Binary compatibility versions that take system codepage pathnames,
1252 * argument vectors and environments. These get used only by code
1253 * built against 2.8.1 or earlier. Code built against 2.8.2 or later
1254 * will use the _utf8 versions above (see the #defines in gspawn.h).
1257 #undef g_spawn_async
1258 #undef g_spawn_async_with_pipes
1260 #undef g_spawn_command_line_sync
1261 #undef g_spawn_command_line_async
1264 setup_utf8_copies (const gchar *working_directory,
1265 gchar **utf8_working_directory,
1274 if (working_directory == NULL)
1275 *utf8_working_directory = NULL;
1278 GError *conv_error = NULL;
1280 *utf8_working_directory = g_locale_to_utf8 (working_directory, -1, NULL, NULL, &conv_error);
1281 if (*utf8_working_directory == NULL)
1283 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
1284 _("Invalid working directory: %s"),
1285 conv_error->message);
1286 g_error_free (conv_error);
1294 *utf8_argv = g_new (gchar *, argc + 1);
1295 for (i = 0; i < argc; i++)
1297 GError *conv_error = NULL;
1299 (*utf8_argv)[i] = g_locale_to_utf8 (argv[i], -1, NULL, NULL, &conv_error);
1300 if ((*utf8_argv)[i] == NULL)
1302 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
1303 _("Invalid string in argument vector at %d: %s"),
1304 i, conv_error->message);
1305 g_error_free (conv_error);
1307 g_strfreev (*utf8_argv);
1310 g_free (*utf8_working_directory);
1311 *utf8_working_directory = NULL;
1316 (*utf8_argv)[argc] = NULL;
1327 *utf8_envp = g_new (gchar *, envc + 1);
1328 for (i = 0; i < envc; i++)
1330 GError *conv_error = NULL;
1332 (*utf8_envp)[i] = g_locale_to_utf8 (envp[i], -1, NULL, NULL, &conv_error);
1333 if ((*utf8_envp)[i] == NULL)
1335 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
1336 _("Invalid string in environment: %s"),
1337 conv_error->message);
1338 g_error_free (conv_error);
1340 g_strfreev (*utf8_envp);
1343 g_strfreev (*utf8_argv);
1346 g_free (*utf8_working_directory);
1347 *utf8_working_directory = NULL;
1352 (*utf8_envp)[envc] = NULL;
1358 free_utf8_copies (gchar *utf8_working_directory,
1362 g_free (utf8_working_directory);
1363 g_strfreev (utf8_argv);
1364 g_strfreev (utf8_envp);
1368 g_spawn_async_with_pipes (const gchar *working_directory,
1372 GSpawnChildSetupFunc child_setup,
1375 gint *standard_input,
1376 gint *standard_output,
1377 gint *standard_error,
1380 gchar *utf8_working_directory;
1385 if (!setup_utf8_copies (working_directory, &utf8_working_directory,
1391 retval = g_spawn_async_with_pipes_utf8 (utf8_working_directory,
1392 utf8_argv, utf8_envp,
1393 flags, child_setup, user_data,
1395 standard_input, standard_output, standard_error,
1398 free_utf8_copies (utf8_working_directory, utf8_argv, utf8_envp);
1404 g_spawn_async (const gchar *working_directory,
1408 GSpawnChildSetupFunc child_setup,
1413 return g_spawn_async_with_pipes (working_directory,
1424 g_spawn_sync (const gchar *working_directory,
1428 GSpawnChildSetupFunc child_setup,
1430 gchar **standard_output,
1431 gchar **standard_error,
1435 gchar *utf8_working_directory;
1440 if (!setup_utf8_copies (working_directory, &utf8_working_directory,
1446 retval = g_spawn_sync_utf8 (utf8_working_directory,
1447 utf8_argv, utf8_envp,
1448 flags, child_setup, user_data,
1449 standard_output, standard_error, exit_status,
1452 free_utf8_copies (utf8_working_directory, utf8_argv, utf8_envp);
1458 g_spawn_command_line_sync (const gchar *command_line,
1459 gchar **standard_output,
1460 gchar **standard_error,
1467 g_return_val_if_fail (command_line != NULL, FALSE);
1469 if (!g_shell_parse_argv (command_line,
1474 retval = g_spawn_sync (NULL,
1477 G_SPAWN_SEARCH_PATH,
1490 g_spawn_command_line_async (const gchar *command_line,
1496 g_return_val_if_fail (command_line != NULL, FALSE);
1498 if (!g_shell_parse_argv (command_line,
1503 retval = g_spawn_async (NULL,
1506 G_SPAWN_SEARCH_PATH,
1516 #endif /* !_WIN64 */
1518 #endif /* !GSPAWN_HELPER */