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 ((long) filehandle, mode | _O_NOINHERIT);
117 #ifndef GSPAWN_HELPER
119 #define HELPER_PROCESS "gspawn-win32-helper"
122 protect_argv_string (const gchar *string)
124 const gchar *p = string;
127 gboolean need_dblquotes = FALSE;
130 if (*p == ' ' || *p == '\t')
131 need_dblquotes = TRUE;
137 while (*pp && *pp == '\\')
146 q = retval = g_malloc (len + need_dblquotes*2 + 1);
159 while (*pp && *pp == '\\')
176 protect_argv (gchar **argv,
184 *new_argv = g_new (gchar *, argc+1);
186 /* Quote each argv element if necessary, so that it will get
187 * reconstructed correctly in the C runtime startup code. Note that
188 * the unquoting algorithm in the C runtime is really weird, and
189 * rather different than what Unix shells do. See stdargv.c in the C
190 * runtime sources (in the Platform SDK, in src/crt).
192 * Note that an new_argv[0] constructed by this function should
193 * *not* be passed as the filename argument to a spawn* or exec*
194 * family function. That argument should be the real file name
195 * without any quoting.
197 for (i = 0; i < argc; i++)
198 (*new_argv)[i] = protect_argv_string (argv[i]);
200 (*new_argv)[argc] = NULL;
206 g_spawn_error_quark (void)
208 return g_quark_from_static_string ("g-exec-error-quark");
212 g_spawn_async_utf8 (const gchar *working_directory,
216 GSpawnChildSetupFunc child_setup,
221 g_return_val_if_fail (argv != NULL, FALSE);
223 return g_spawn_async_with_pipes_utf8 (working_directory,
233 /* Avoids a danger in threaded situations (calling close()
234 * on a file descriptor twice, and another thread has
235 * re-opened it since the first close)
238 close_and_invalidate (gint *fd)
249 READ_FAILED = 0, /* FALSE */
255 read_data (GString *str,
256 GIOChannel *iochannel,
265 giostatus = g_io_channel_read_chars (iochannel, buf, sizeof (buf), &bytes, NULL);
271 g_string_append_len (str, buf, bytes);
274 else if (giostatus == G_IO_STATUS_AGAIN)
276 else if (giostatus == G_IO_STATUS_ERROR)
278 g_set_error_literal (error, G_SPAWN_ERROR, G_SPAWN_ERROR_READ,
279 _("Failed to read data from child process"));
288 make_pipe (gint p[2],
291 if (_pipe (p, 4096, _O_BINARY) < 0)
293 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
294 _("Failed to create pipe for communicating with child process (%s)"),
302 /* The helper process writes a status report back to us, through a
303 * pipe, consisting of two ints.
306 read_helper_report (int fd,
312 while (bytes < sizeof(gint)*2)
317 g_print ("%s:read_helper_report: read %d...\n",
319 sizeof(gint)*2 - bytes);
321 chunk = read (fd, ((gchar*)report) + bytes,
322 sizeof(gint)*2 - bytes);
325 g_print ("...got %d bytes\n", chunk);
329 /* Some weird shit happened, bail out */
331 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
332 _("Failed to read from child pipe (%s)"),
339 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
340 _("Failed to read from child pipe (%s)"),
348 if (bytes < sizeof(gint)*2)
355 set_child_error (gint report[2],
356 const gchar *working_directory,
361 case CHILD_CHDIR_FAILED:
362 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
363 _("Failed to change to directory '%s' (%s)"),
365 g_strerror (report[1]));
367 case CHILD_SPAWN_FAILED:
368 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
369 _("Failed to execute child process (%s)"),
370 g_strerror (report[1]));
373 g_assert_not_reached ();
378 utf8_charv_to_wcharv (char **utf8_charv,
383 wchar_t **retval = NULL;
386 if (utf8_charv != NULL)
390 while (utf8_charv[n])
392 retval = g_new (wchar_t *, n + 1);
394 for (i = 0; i < n; i++)
396 retval[i] = g_utf8_to_utf16 (utf8_charv[i], -1, NULL, NULL, error);
397 if (retval[i] == NULL)
402 g_free (retval[--i]);
415 do_spawn_directly (gint *exit_status,
416 gboolean do_return_handle,
420 char **protected_argv,
421 GSpawnChildSetupFunc child_setup,
426 const int mode = (exit_status == NULL) ? P_NOWAIT : P_WAIT;
430 GError *conv_error = NULL;
431 gint conv_error_index;
432 wchar_t *wargv0, **wargv, **wenvp;
434 new_argv = (flags & G_SPAWN_FILE_AND_ARGV_ZERO) ? protected_argv + 1 : protected_argv;
436 wargv0 = g_utf8_to_utf16 (argv[0], -1, NULL, NULL, &conv_error);
439 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
440 _("Invalid program name: %s"),
441 conv_error->message);
442 g_error_free (conv_error);
447 if (!utf8_charv_to_wcharv (new_argv, &wargv, &conv_error_index, &conv_error))
449 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
450 _("Invalid string in argument vector at %d: %s"),
451 conv_error_index, conv_error->message);
452 g_error_free (conv_error);
458 if (!utf8_charv_to_wcharv (envp, &wenvp, NULL, &conv_error))
460 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
461 _("Invalid string in environment: %s"),
462 conv_error->message);
463 g_error_free (conv_error);
465 g_strfreev ((gchar **) wargv);
471 (* child_setup) (user_data);
473 if (flags & G_SPAWN_SEARCH_PATH)
475 rc = _wspawnvpe (mode, wargv0, (const wchar_t **) wargv, (const wchar_t **) wenvp);
477 rc = _wspawnvp (mode, wargv0, (const wchar_t **) wargv);
480 rc = _wspawnve (mode, wargv0, (const wchar_t **) wargv, (const wchar_t **) wenvp);
482 rc = _wspawnv (mode, wargv0, (const wchar_t **) wargv);
485 g_strfreev ((gchar **) wargv);
486 g_strfreev ((gchar **) wenvp);
490 if (rc == -1 && saved_errno != 0)
492 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
493 _("Failed to execute child process (%s)"),
494 g_strerror (saved_errno));
498 if (exit_status == NULL)
500 if (child_handle && do_return_handle)
501 *child_handle = (GPid) rc;
504 CloseHandle ((HANDLE) rc);
516 do_spawn_with_pipes (gint *exit_status,
517 gboolean do_return_handle,
518 const gchar *working_directory,
522 GSpawnChildSetupFunc child_setup,
525 gint *standard_input,
526 gint *standard_output,
527 gint *standard_error,
531 char **protected_argv;
532 char args[ARG_COUNT][10];
538 int stdin_pipe[2] = { -1, -1 };
539 int stdout_pipe[2] = { -1, -1 };
540 int stderr_pipe[2] = { -1, -1 };
541 int child_err_report_pipe[2] = { -1, -1 };
542 int helper_sync_pipe[2] = { -1, -1 };
543 int helper_report[2];
544 static gboolean warned_about_child_setup = FALSE;
545 GError *conv_error = NULL;
546 gint conv_error_index;
547 gchar *helper_process;
548 CONSOLE_CURSOR_INFO cursor_info;
549 wchar_t *whelper, **wargv, **wenvp;
550 extern gchar *_glib_get_installation_directory (void);
553 if (child_setup && !warned_about_child_setup)
555 warned_about_child_setup = TRUE;
556 g_warning ("passing a child setup function to the g_spawn functions is pointless and dangerous on Win32");
559 argc = protect_argv (argv, &protected_argv);
561 if (!standard_input && !standard_output && !standard_error &&
562 (flags & G_SPAWN_CHILD_INHERITS_STDIN) &&
563 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL) &&
564 !(flags & G_SPAWN_STDERR_TO_DEV_NULL) &&
565 (working_directory == NULL || !*working_directory) &&
566 (flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN))
568 /* We can do without the helper process */
570 do_spawn_directly (exit_status, do_return_handle, flags,
571 argv, envp, protected_argv,
572 child_setup, user_data, child_handle,
574 g_strfreev (protected_argv);
578 if (standard_input && !make_pipe (stdin_pipe, error))
579 goto cleanup_and_fail;
581 if (standard_output && !make_pipe (stdout_pipe, error))
582 goto cleanup_and_fail;
584 if (standard_error && !make_pipe (stderr_pipe, error))
585 goto cleanup_and_fail;
587 if (!make_pipe (child_err_report_pipe, error))
588 goto cleanup_and_fail;
590 if (!make_pipe (helper_sync_pipe, error))
591 goto cleanup_and_fail;
593 new_argv = g_new (char *, argc + 1 + ARG_COUNT);
594 if (GetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cursor_info))
595 helper_process = HELPER_PROCESS "-console.exe";
597 helper_process = HELPER_PROCESS ".exe";
599 glib_top = _glib_get_installation_directory ();
600 if (glib_top != NULL)
602 helper_process = g_build_filename (glib_top, "bin", helper_process, NULL);
606 helper_process = g_strdup (helper_process);
608 new_argv[0] = protect_argv_string (helper_process);
610 _g_sprintf (args[ARG_CHILD_ERR_REPORT], "%d", child_err_report_pipe[1]);
611 new_argv[ARG_CHILD_ERR_REPORT] = args[ARG_CHILD_ERR_REPORT];
613 /* Make the read end of the child error report pipe
614 * noninherited. Otherwise it will needlessly be inherited by the
615 * helper process, and the started actual user process. As such that
616 * shouldn't harm, but it is unnecessary.
618 child_err_report_pipe[0] = dup_noninherited (child_err_report_pipe[0], _O_RDONLY);
620 if (flags & G_SPAWN_FILE_AND_ARGV_ZERO)
622 /* Overload ARG_CHILD_ERR_REPORT to also encode the
623 * G_SPAWN_FILE_AND_ARGV_ZERO functionality.
625 strcat (args[ARG_CHILD_ERR_REPORT], "#");
628 _g_sprintf (args[ARG_HELPER_SYNC], "%d", helper_sync_pipe[0]);
629 new_argv[ARG_HELPER_SYNC] = args[ARG_HELPER_SYNC];
631 /* Make the write end of the sync pipe noninherited. Otherwise the
632 * helper process will inherit it, and thus if this process happens
633 * to crash before writing the sync byte to the pipe, the helper
634 * process won't read but won't get any EOF either, as it has the
635 * write end open itself.
637 helper_sync_pipe[1] = dup_noninherited (helper_sync_pipe[1], _O_WRONLY);
641 _g_sprintf (args[ARG_STDIN], "%d", stdin_pipe[0]);
642 new_argv[ARG_STDIN] = args[ARG_STDIN];
644 else if (flags & G_SPAWN_CHILD_INHERITS_STDIN)
646 /* Let stdin be alone */
647 new_argv[ARG_STDIN] = "-";
651 /* Keep process from blocking on a read of stdin */
652 new_argv[ARG_STDIN] = "z";
657 _g_sprintf (args[ARG_STDOUT], "%d", stdout_pipe[1]);
658 new_argv[ARG_STDOUT] = args[ARG_STDOUT];
660 else if (flags & G_SPAWN_STDOUT_TO_DEV_NULL)
662 new_argv[ARG_STDOUT] = "z";
666 new_argv[ARG_STDOUT] = "-";
671 _g_sprintf (args[ARG_STDERR], "%d", stderr_pipe[1]);
672 new_argv[ARG_STDERR] = args[ARG_STDERR];
674 else if (flags & G_SPAWN_STDERR_TO_DEV_NULL)
676 new_argv[ARG_STDERR] = "z";
680 new_argv[ARG_STDERR] = "-";
683 if (working_directory && *working_directory)
684 new_argv[ARG_WORKING_DIRECTORY] = protect_argv_string (working_directory);
686 new_argv[ARG_WORKING_DIRECTORY] = g_strdup ("-");
688 if (!(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN))
689 new_argv[ARG_CLOSE_DESCRIPTORS] = "y";
691 new_argv[ARG_CLOSE_DESCRIPTORS] = "-";
693 if (flags & G_SPAWN_SEARCH_PATH)
694 new_argv[ARG_USE_PATH] = "y";
696 new_argv[ARG_USE_PATH] = "-";
698 if (exit_status == NULL)
699 new_argv[ARG_WAIT] = "-";
701 new_argv[ARG_WAIT] = "w";
703 for (i = 0; i <= argc; i++)
704 new_argv[ARG_PROGRAM + i] = protected_argv[i];
710 g_print ("calling %s with argv:\n", helper_process);
711 for (i = 0; i < argc + 1 + ARG_COUNT; i++)
712 g_print ("argv[%d]: %s\n", i, (new_argv[i] ? new_argv[i] : "NULL"));
715 if (!utf8_charv_to_wcharv (new_argv, &wargv, &conv_error_index, &conv_error))
717 if (conv_error_index == ARG_WORKING_DIRECTORY)
718 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
719 _("Invalid working directory: %s"),
720 conv_error->message);
722 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
723 _("Invalid string in argument vector at %d: %s"),
724 conv_error_index - ARG_PROGRAM, conv_error->message);
725 g_error_free (conv_error);
726 g_strfreev (protected_argv);
727 g_free (new_argv[0]);
728 g_free (new_argv[ARG_WORKING_DIRECTORY]);
730 g_free (helper_process);
732 goto cleanup_and_fail;
735 if (!utf8_charv_to_wcharv (envp, &wenvp, NULL, &conv_error))
737 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
738 _("Invalid string in environment: %s"),
739 conv_error->message);
740 g_error_free (conv_error);
741 g_strfreev (protected_argv);
742 g_free (new_argv[0]);
743 g_free (new_argv[ARG_WORKING_DIRECTORY]);
745 g_free (helper_process);
746 g_strfreev ((gchar **) wargv);
748 goto cleanup_and_fail;
752 (* child_setup) (user_data);
754 whelper = g_utf8_to_utf16 (helper_process, -1, NULL, NULL, NULL);
755 g_free (helper_process);
758 rc = _wspawnvpe (P_NOWAIT, whelper, (const wchar_t **) wargv, (const wchar_t **) wenvp);
760 rc = _wspawnvp (P_NOWAIT, whelper, (const wchar_t **) wargv);
765 g_strfreev ((gchar **) wargv);
766 g_strfreev ((gchar **) wenvp);
768 /* Close the other process's ends of the pipes in this process,
769 * otherwise the reader will never get EOF.
771 close_and_invalidate (&child_err_report_pipe[1]);
772 close_and_invalidate (&helper_sync_pipe[0]);
773 close_and_invalidate (&stdin_pipe[0]);
774 close_and_invalidate (&stdout_pipe[1]);
775 close_and_invalidate (&stderr_pipe[1]);
777 g_strfreev (protected_argv);
779 g_free (new_argv[0]);
780 g_free (new_argv[ARG_WORKING_DIRECTORY]);
783 /* Check if gspawn-win32-helper couldn't be run */
784 if (rc == -1 && saved_errno != 0)
786 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
787 _("Failed to execute helper program (%s)"),
788 g_strerror (saved_errno));
789 goto cleanup_and_fail;
792 if (exit_status != NULL)
794 /* Synchronous case. Pass helper's report pipe back to caller,
795 * which takes care of reading it after the grandchild has
798 g_assert (err_report != NULL);
799 *err_report = child_err_report_pipe[0];
800 write (helper_sync_pipe[1], " ", 1);
801 close_and_invalidate (&helper_sync_pipe[1]);
805 /* Asynchronous case. We read the helper's report right away. */
806 if (!read_helper_report (child_err_report_pipe[0], helper_report, error))
807 goto cleanup_and_fail;
809 close_and_invalidate (&child_err_report_pipe[0]);
811 switch (helper_report[0])
814 if (child_handle && do_return_handle)
816 /* rc is our HANDLE for gspawn-win32-helper. It has
817 * told us the HANDLE of its child. Duplicate that into
818 * a HANDLE valid in this process.
820 if (!DuplicateHandle ((HANDLE) rc, (HANDLE) helper_report[1],
821 GetCurrentProcess (), (LPHANDLE) child_handle,
822 0, TRUE, DUPLICATE_SAME_ACCESS))
824 char *emsg = g_win32_error_message (GetLastError ());
825 g_print("%s\n", emsg);
829 else if (child_handle)
831 write (helper_sync_pipe[1], " ", 1);
832 close_and_invalidate (&helper_sync_pipe[1]);
836 write (helper_sync_pipe[1], " ", 1);
837 close_and_invalidate (&helper_sync_pipe[1]);
838 set_child_error (helper_report, working_directory, error);
839 goto cleanup_and_fail;
843 /* Success against all odds! return the information */
846 *standard_input = stdin_pipe[1];
848 *standard_output = stdout_pipe[0];
850 *standard_error = stderr_pipe[0];
852 CloseHandle ((HANDLE) rc);
859 CloseHandle ((HANDLE) rc);
860 if (child_err_report_pipe[0] != -1)
861 close (child_err_report_pipe[0]);
862 if (child_err_report_pipe[1] != -1)
863 close (child_err_report_pipe[1]);
864 if (helper_sync_pipe[0] != -1)
865 close (helper_sync_pipe[0]);
866 if (helper_sync_pipe[1] != -1)
867 close (helper_sync_pipe[1]);
868 if (stdin_pipe[0] != -1)
869 close (stdin_pipe[0]);
870 if (stdin_pipe[1] != -1)
871 close (stdin_pipe[1]);
872 if (stdout_pipe[0] != -1)
873 close (stdout_pipe[0]);
874 if (stdout_pipe[1] != -1)
875 close (stdout_pipe[1]);
876 if (stderr_pipe[0] != -1)
877 close (stderr_pipe[0]);
878 if (stderr_pipe[1] != -1)
879 close (stderr_pipe[1]);
885 g_spawn_sync_utf8 (const gchar *working_directory,
889 GSpawnChildSetupFunc child_setup,
891 gchar **standard_output,
892 gchar **standard_error,
898 gint reportpipe = -1;
899 GIOChannel *outchannel = NULL;
900 GIOChannel *errchannel = NULL;
901 GPollFD outfd, errfd;
907 GString *outstr = NULL;
908 GString *errstr = NULL;
912 g_return_val_if_fail (argv != NULL, FALSE);
913 g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
914 g_return_val_if_fail (standard_output == NULL ||
915 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
916 g_return_val_if_fail (standard_error == NULL ||
917 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
919 /* Just to ensure segfaults if callers try to use
920 * these when an error is reported.
923 *standard_output = NULL;
926 *standard_error = NULL;
928 if (!do_spawn_with_pipes (&status,
938 standard_output ? &outpipe : NULL,
939 standard_error ? &errpipe : NULL,
944 /* Read data from child. */
950 outstr = g_string_new (NULL);
951 outchannel = g_io_channel_win32_new_fd (outpipe);
952 g_io_channel_set_encoding (outchannel, NULL, NULL);
953 g_io_channel_set_buffered (outchannel, FALSE);
954 g_io_channel_win32_make_pollfd (outchannel,
955 G_IO_IN | G_IO_ERR | G_IO_HUP,
958 g_print ("outfd=%x\n", outfd.fd);
963 errstr = g_string_new (NULL);
964 errchannel = g_io_channel_win32_new_fd (errpipe);
965 g_io_channel_set_encoding (errchannel, NULL, NULL);
966 g_io_channel_set_buffered (errchannel, FALSE);
967 g_io_channel_win32_make_pollfd (errchannel,
968 G_IO_IN | G_IO_ERR | G_IO_HUP,
971 g_print ("errfd=%x\n", errfd.fd);
974 /* Read data until we get EOF on all pipes. */
975 while (!failed && (outpipe >= 0 || errpipe >= 0))
992 g_print ("g_spawn_sync: calling g_io_channel_win32_poll, nfds=%d\n",
995 ret = g_io_channel_win32_poll (fds, nfds, -1);
1001 g_set_error_literal (error, G_SPAWN_ERROR, G_SPAWN_ERROR_READ,
1002 _("Unexpected error in g_io_channel_win32_poll() reading data from a child process"));
1007 if (outpipe >= 0 && (fds[outindex].revents & G_IO_IN))
1009 switch (read_data (outstr, outchannel, error))
1013 g_print ("g_spawn_sync: outchannel: READ_FAILED\n");
1018 g_print ("g_spawn_sync: outchannel: READ_EOF\n");
1019 g_io_channel_unref (outchannel);
1021 close_and_invalidate (&outpipe);
1025 g_print ("g_spawn_sync: outchannel: OK\n");
1033 if (errpipe >= 0 && (fds[errindex].revents & G_IO_IN))
1035 switch (read_data (errstr, errchannel, error))
1039 g_print ("g_spawn_sync: errchannel: READ_FAILED\n");
1044 g_print ("g_spawn_sync: errchannel: READ_EOF\n");
1045 g_io_channel_unref (errchannel);
1047 close_and_invalidate (&errpipe);
1051 g_print ("g_spawn_sync: errchannel: OK\n");
1060 if (reportpipe == -1)
1062 /* No helper process, exit status of actual spawned process
1063 * already available.
1066 *exit_status = status;
1070 /* Helper process was involved. Read its report now after the
1071 * grandchild has finished.
1073 gint helper_report[2];
1075 if (!read_helper_report (reportpipe, helper_report, error))
1079 switch (helper_report[0])
1081 case CHILD_NO_ERROR:
1083 *exit_status = helper_report[1];
1086 set_child_error (helper_report, working_directory, error);
1091 close_and_invalidate (&reportpipe);
1095 /* These should only be open still if we had an error. */
1097 if (outchannel != NULL)
1098 g_io_channel_unref (outchannel);
1099 if (errchannel != NULL)
1100 g_io_channel_unref (errchannel);
1102 close_and_invalidate (&outpipe);
1104 close_and_invalidate (&errpipe);
1109 g_string_free (outstr, TRUE);
1111 g_string_free (errstr, TRUE);
1117 if (standard_output)
1118 *standard_output = g_string_free (outstr, FALSE);
1121 *standard_error = g_string_free (errstr, FALSE);
1128 g_spawn_async_with_pipes_utf8 (const gchar *working_directory,
1132 GSpawnChildSetupFunc child_setup,
1135 gint *standard_input,
1136 gint *standard_output,
1137 gint *standard_error,
1140 g_return_val_if_fail (argv != NULL, FALSE);
1141 g_return_val_if_fail (standard_output == NULL ||
1142 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
1143 g_return_val_if_fail (standard_error == NULL ||
1144 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
1145 /* can't inherit stdin if we have an input pipe. */
1146 g_return_val_if_fail (standard_input == NULL ||
1147 !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
1149 return do_spawn_with_pipes (NULL,
1150 (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)
1230 #if !defined (_WIN64)
1232 /* Binary compatibility versions that take system codepage pathnames,
1233 * argument vectors and environments. These get used only by code
1234 * built against 2.8.1 or earlier. Code built against 2.8.2 or later
1235 * will use the _utf8 versions above (see the #defines in gspawn.h).
1238 #undef g_spawn_async
1239 #undef g_spawn_async_with_pipes
1241 #undef g_spawn_command_line_sync
1242 #undef g_spawn_command_line_async
1245 setup_utf8_copies (const gchar *working_directory,
1246 gchar **utf8_working_directory,
1255 if (working_directory == NULL)
1256 *utf8_working_directory = NULL;
1259 GError *conv_error = NULL;
1261 *utf8_working_directory = g_locale_to_utf8 (working_directory, -1, NULL, NULL, &conv_error);
1262 if (*utf8_working_directory == NULL)
1264 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
1265 _("Invalid working directory: %s"),
1266 conv_error->message);
1267 g_error_free (conv_error);
1275 *utf8_argv = g_new (gchar *, argc + 1);
1276 for (i = 0; i < argc; i++)
1278 GError *conv_error = NULL;
1280 (*utf8_argv)[i] = g_locale_to_utf8 (argv[i], -1, NULL, NULL, &conv_error);
1281 if ((*utf8_argv)[i] == NULL)
1283 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
1284 _("Invalid string in argument vector at %d: %s"),
1285 i, conv_error->message);
1286 g_error_free (conv_error);
1288 g_strfreev (*utf8_argv);
1291 g_free (*utf8_working_directory);
1292 *utf8_working_directory = NULL;
1297 (*utf8_argv)[argc] = NULL;
1308 *utf8_envp = g_new (gchar *, envc + 1);
1309 for (i = 0; i < envc; i++)
1311 GError *conv_error = NULL;
1313 (*utf8_envp)[i] = g_locale_to_utf8 (envp[i], -1, NULL, NULL, &conv_error);
1314 if ((*utf8_envp)[i] == NULL)
1316 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
1317 _("Invalid string in environment: %s"),
1318 conv_error->message);
1319 g_error_free (conv_error);
1321 g_strfreev (*utf8_envp);
1324 g_strfreev (*utf8_argv);
1327 g_free (*utf8_working_directory);
1328 *utf8_working_directory = NULL;
1333 (*utf8_envp)[envc] = NULL;
1339 free_utf8_copies (gchar *utf8_working_directory,
1343 g_free (utf8_working_directory);
1344 g_strfreev (utf8_argv);
1345 g_strfreev (utf8_envp);
1349 g_spawn_async_with_pipes (const gchar *working_directory,
1353 GSpawnChildSetupFunc child_setup,
1356 gint *standard_input,
1357 gint *standard_output,
1358 gint *standard_error,
1361 gchar *utf8_working_directory;
1366 if (!setup_utf8_copies (working_directory, &utf8_working_directory,
1372 retval = g_spawn_async_with_pipes_utf8 (utf8_working_directory,
1373 utf8_argv, utf8_envp,
1374 flags, child_setup, user_data,
1376 standard_input, standard_output, standard_error,
1379 free_utf8_copies (utf8_working_directory, utf8_argv, utf8_envp);
1385 g_spawn_async (const gchar *working_directory,
1389 GSpawnChildSetupFunc child_setup,
1394 return g_spawn_async_with_pipes (working_directory,
1405 g_spawn_sync (const gchar *working_directory,
1409 GSpawnChildSetupFunc child_setup,
1411 gchar **standard_output,
1412 gchar **standard_error,
1416 gchar *utf8_working_directory;
1421 if (!setup_utf8_copies (working_directory, &utf8_working_directory,
1427 retval = g_spawn_sync_utf8 (utf8_working_directory,
1428 utf8_argv, utf8_envp,
1429 flags, child_setup, user_data,
1430 standard_output, standard_error, exit_status,
1433 free_utf8_copies (utf8_working_directory, utf8_argv, utf8_envp);
1439 g_spawn_command_line_sync (const gchar *command_line,
1440 gchar **standard_output,
1441 gchar **standard_error,
1448 g_return_val_if_fail (command_line != NULL, FALSE);
1450 if (!g_shell_parse_argv (command_line,
1455 retval = g_spawn_sync (NULL,
1458 G_SPAWN_SEARCH_PATH,
1471 g_spawn_command_line_async (const gchar *command_line,
1477 g_return_val_if_fail (command_line != NULL, FALSE);
1479 if (!g_shell_parse_argv (command_line,
1484 retval = g_spawn_async (NULL,
1487 G_SPAWN_SEARCH_PATH,
1497 #endif /* !_WIN64 */
1499 #endif /* !GSPAWN_HELPER */
1501 #define __G_SPAWN_C__
1502 #include "galiasdef.c"