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 most 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 /* Mingw doesn't have prototypes for these */
64 int _wspawnvpe (int, const wchar_t *, const wchar_t **, const wchar_t **);
65 int _wspawnvp (int, const wchar_t *, const wchar_t **);
66 int _wspawnve (int, const wchar_t *, const wchar_t **, const wchar_t **);
67 int _wspawnv (int, const wchar_t *, const wchar_t **);
72 #ifdef G_SPAWN_WIN32_DEBUG
74 #define SETUP_DEBUG() /* empty */
76 static int debug = -1;
77 #define SETUP_DEBUG() \
82 if (getenv ("G_SPAWN_WIN32_DEBUG") != NULL) \
99 ARG_CHILD_ERR_REPORT = 1,
103 ARG_WORKING_DIRECTORY,
104 ARG_CLOSE_DESCRIPTORS,
108 ARG_COUNT = ARG_PROGRAM
111 #ifndef GSPAWN_HELPER
113 #define HELPER_PROCESS "gspawn-win32-helper"
116 protect_argv_string (const gchar *string)
118 const gchar *p = string;
121 gboolean need_dblquotes = FALSE;
124 if (*p == ' ' || *p == '\t')
125 need_dblquotes = TRUE;
131 while (*pp && *pp == '\\')
140 q = retval = g_malloc (len + need_dblquotes*2 + 1);
153 while (*pp && *pp == '\\')
170 protect_argv (gchar **argv,
178 *new_argv = g_new (gchar *, argc+1);
180 /* Quote each argv element if necessary, so that it will get
181 * reconstructed correctly in the C runtime startup code. Note that
182 * the unquoting algorithm in the C runtime is really weird, and
183 * rather different than what Unix shells do. See stdargv.c in the C
184 * runtime sources (in the Platform SDK, in src/crt).
186 * Note that an new_argv[0] constructed by this function should
187 * *not* be passed as the filename argument to a spawn* or exec*
188 * family function. That argument should be the real file name
189 * without any quoting.
191 for (i = 0; i < argc; i++)
192 (*new_argv)[i] = protect_argv_string (argv[i]);
194 (*new_argv)[argc] = NULL;
200 g_spawn_error_quark (void)
202 return g_quark_from_static_string ("g-exec-error-quark");
206 g_spawn_async_utf8 (const gchar *working_directory,
210 GSpawnChildSetupFunc child_setup,
215 g_return_val_if_fail (argv != NULL, FALSE);
217 return g_spawn_async_with_pipes_utf8 (working_directory,
227 /* Avoids a danger in threaded situations (calling close()
228 * on a file descriptor twice, and another thread has
229 * re-opened it since the first close)
232 close_and_invalidate (gint *fd)
243 READ_FAILED = 0, /* FALSE */
249 read_data (GString *str,
250 GIOChannel *iochannel,
259 giostatus = g_io_channel_read_chars (iochannel, buf, sizeof (buf), &bytes, NULL);
265 g_string_append_len (str, buf, bytes);
268 else if (giostatus == G_IO_STATUS_AGAIN)
270 else if (giostatus == G_IO_STATUS_ERROR)
272 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_READ,
273 _("Failed to read data from child process"));
282 make_pipe (gint p[2],
287 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
288 _("Failed to create pipe for communicating with child process (%s)"),
296 /* The helper process writes a status report back to us, through a
297 * pipe, consisting of two ints.
300 read_helper_report (int fd,
306 while (bytes < sizeof(gint)*2)
311 g_print ("%s:read_helper_report: read %d...\n",
313 sizeof(gint)*2 - bytes);
315 chunk = read (fd, ((gchar*)report) + bytes,
316 sizeof(gint)*2 - bytes);
319 g_print ("...got %d bytes\n", chunk);
323 /* Some weird shit happened, bail out */
325 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
326 _("Failed to read from child pipe (%s)"),
337 if (bytes < sizeof(gint)*2)
344 set_child_error (gint report[2],
345 const gchar *working_directory,
350 case CHILD_CHDIR_FAILED:
351 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
352 _("Failed to change to directory '%s' (%s)"),
354 g_strerror (report[1]));
356 case CHILD_SPAWN_FAILED:
357 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
358 _("Failed to execute child process (%s)"),
359 g_strerror (report[1]));
362 g_assert_not_reached ();
367 utf8_charv_to_wcharv (char **utf8_charv,
372 wchar_t **retval = NULL;
375 if (utf8_charv != NULL)
379 while (utf8_charv[n])
381 retval = g_new (wchar_t *, n + 1);
383 for (i = 0; i < n; i++)
385 retval[i] = g_utf8_to_utf16 (utf8_charv[i], -1, NULL, NULL, error);
386 if (retval[i] == NULL)
391 g_free (retval[--i]);
404 do_spawn_directly (gint *exit_status,
405 gboolean do_return_handle,
409 char **protected_argv,
410 GSpawnChildSetupFunc child_setup,
415 const int mode = (exit_status == NULL) ? P_NOWAIT : P_WAIT;
419 GError *conv_error = NULL;
420 gint conv_error_index;
421 wchar_t *wargv0, **wargv, **wenvp;
423 new_argv = (flags & G_SPAWN_FILE_AND_ARGV_ZERO) ? protected_argv + 1 : protected_argv;
425 wargv0 = g_utf8_to_utf16 (argv[0], -1, NULL, NULL, &conv_error);
428 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
429 _("Invalid program name: %s"),
430 conv_error->message);
431 g_error_free (conv_error);
436 if (!utf8_charv_to_wcharv (new_argv, &wargv, &conv_error_index, &conv_error))
438 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
439 _("Invalid string in argument vector at %d: %s"),
440 conv_error_index, conv_error->message);
441 g_error_free (conv_error);
447 if (!utf8_charv_to_wcharv (envp, &wenvp, NULL, &conv_error))
449 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
450 _("Invalid string in environment: %s"),
451 conv_error->message);
452 g_error_free (conv_error);
454 g_strfreev ((gchar **) wargv);
460 (* child_setup) (user_data);
462 if (flags & G_SPAWN_SEARCH_PATH)
464 rc = _wspawnvpe (mode, wargv0, (const wchar_t **) wargv, (const wchar_t **) wenvp);
466 rc = _wspawnvp (mode, wargv0, (const wchar_t **) wargv);
469 rc = _wspawnve (mode, wargv0, (const wchar_t **) wargv, (const wchar_t **) wenvp);
471 rc = _wspawnv (mode, wargv0, (const wchar_t **) wargv);
474 g_strfreev ((gchar **) wargv);
475 g_strfreev ((gchar **) wenvp);
479 if (rc == -1 && saved_errno != 0)
481 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
482 _("Failed to execute child process (%s)"),
483 g_strerror (saved_errno));
487 if (exit_status == NULL)
489 if (child_handle && do_return_handle)
490 *child_handle = (GPid) rc;
493 CloseHandle ((HANDLE) rc);
505 do_spawn_with_pipes (gint *exit_status,
506 gboolean do_return_handle,
507 const gchar *working_directory,
511 GSpawnChildSetupFunc child_setup,
514 gint *standard_input,
515 gint *standard_output,
516 gint *standard_error,
520 char **protected_argv;
521 char args[ARG_COUNT][10];
527 int stdin_pipe[2] = { -1, -1 };
528 int stdout_pipe[2] = { -1, -1 };
529 int stderr_pipe[2] = { -1, -1 };
530 int child_err_report_pipe[2] = { -1, -1 };
531 int helper_report[2];
532 static gboolean warned_about_child_setup = FALSE;
533 GError *conv_error = NULL;
534 gint conv_error_index;
535 gchar *helper_process;
536 CONSOLE_CURSOR_INFO cursor_info;
537 wchar_t *whelper, **wargv, **wenvp;
541 if (child_setup && !warned_about_child_setup)
543 warned_about_child_setup = TRUE;
544 g_warning ("passing a child setup function to the g_spawn functions is pointless and dangerous on Win32");
547 argc = protect_argv (argv, &protected_argv);
549 if (!standard_input && !standard_output && !standard_error &&
550 (flags & G_SPAWN_CHILD_INHERITS_STDIN) &&
551 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL) &&
552 !(flags & G_SPAWN_STDERR_TO_DEV_NULL) &&
553 (working_directory == NULL || !*working_directory) &&
554 (flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN))
556 /* We can do without the helper process */
558 do_spawn_directly (exit_status, do_return_handle, flags,
559 argv, envp, protected_argv,
560 child_setup, user_data, child_handle,
562 g_strfreev (protected_argv);
566 if (standard_input && !make_pipe (stdin_pipe, error))
567 goto cleanup_and_fail;
569 if (standard_output && !make_pipe (stdout_pipe, error))
570 goto cleanup_and_fail;
572 if (standard_error && !make_pipe (stderr_pipe, error))
573 goto cleanup_and_fail;
575 if (!make_pipe (child_err_report_pipe, error))
576 goto cleanup_and_fail;
578 new_argv = g_new (char *, argc + 1 + ARG_COUNT);
579 if (GetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &cursor_info))
580 helper_process = HELPER_PROCESS "-console.exe";
582 helper_process = HELPER_PROCESS ".exe";
583 new_argv[0] = helper_process;
584 _g_sprintf (args[ARG_CHILD_ERR_REPORT], "%d", child_err_report_pipe[1]);
585 new_argv[ARG_CHILD_ERR_REPORT] = args[ARG_CHILD_ERR_REPORT];
587 if (flags & G_SPAWN_FILE_AND_ARGV_ZERO)
589 /* Overload ARG_CHILD_ERR_REPORT to also encode the
590 * G_SPAWN_FILE_AND_ARGV_ZERO functionality.
592 strcat (args[ARG_CHILD_ERR_REPORT], "#");
597 _g_sprintf (args[ARG_STDIN], "%d", stdin_pipe[0]);
598 new_argv[ARG_STDIN] = args[ARG_STDIN];
600 else if (flags & G_SPAWN_CHILD_INHERITS_STDIN)
602 /* Let stdin be alone */
603 new_argv[ARG_STDIN] = "-";
607 /* Keep process from blocking on a read of stdin */
608 new_argv[ARG_STDIN] = "z";
613 _g_sprintf (args[ARG_STDOUT], "%d", stdout_pipe[1]);
614 new_argv[ARG_STDOUT] = args[ARG_STDOUT];
616 else if (flags & G_SPAWN_STDOUT_TO_DEV_NULL)
618 new_argv[ARG_STDOUT] = "z";
622 new_argv[ARG_STDOUT] = "-";
627 _g_sprintf (args[ARG_STDERR], "%d", stderr_pipe[1]);
628 new_argv[ARG_STDERR] = args[ARG_STDERR];
630 else if (flags & G_SPAWN_STDERR_TO_DEV_NULL)
632 new_argv[ARG_STDERR] = "z";
636 new_argv[ARG_STDERR] = "-";
639 if (working_directory && *working_directory)
640 new_argv[ARG_WORKING_DIRECTORY] = protect_argv_string (working_directory);
642 new_argv[ARG_WORKING_DIRECTORY] = g_strdup ("-");
644 if (!(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN))
645 new_argv[ARG_CLOSE_DESCRIPTORS] = "y";
647 new_argv[ARG_CLOSE_DESCRIPTORS] = "-";
649 if (flags & G_SPAWN_SEARCH_PATH)
650 new_argv[ARG_USE_PATH] = "y";
652 new_argv[ARG_USE_PATH] = "-";
654 if (exit_status == NULL)
655 new_argv[ARG_WAIT] = "-";
657 new_argv[ARG_WAIT] = "w";
659 for (i = 0; i <= argc; i++)
660 new_argv[ARG_PROGRAM + i] = protected_argv[i];
664 g_print ("calling %s with argv:\n", helper_process);
665 for (i = 0; i < argc + 1 + ARG_COUNT; i++)
666 g_print ("argv[%d]: %s\n", i, (new_argv[i] ? new_argv[i] : "NULL"));
669 whelper = g_utf8_to_utf16 (helper_process, -1, NULL, NULL, NULL);
671 if (!utf8_charv_to_wcharv (new_argv, &wargv, &conv_error_index, &conv_error))
673 if (conv_error_index == ARG_WORKING_DIRECTORY)
674 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
675 _("Invalid working directory: %s"),
676 conv_error->message);
678 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
679 _("Invalid string in argument vector at %d: %s"),
680 conv_error_index - ARG_PROGRAM, conv_error->message);
681 g_error_free (conv_error);
682 g_strfreev (protected_argv);
683 g_free (new_argv[ARG_WORKING_DIRECTORY]);
690 if (!utf8_charv_to_wcharv (envp, &wenvp, NULL, &conv_error))
692 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
693 _("Invalid string in environment: %s"),
694 conv_error->message);
695 g_error_free (conv_error);
696 g_strfreev (protected_argv);
697 g_free (new_argv[ARG_WORKING_DIRECTORY]);
700 g_strfreev ((gchar **) wargv);
706 (* child_setup) (user_data);
709 /* Let's hope envp hasn't mucked with PATH so that
710 * gspawn-win32-helper.exe isn't found.
712 rc = _wspawnvpe (P_NOWAIT, whelper, (const wchar_t **) wargv, (const wchar_t **) wenvp);
714 rc = _wspawnvp (P_NOWAIT, whelper, (const wchar_t **) wargv);
719 g_strfreev ((gchar **) wargv);
720 g_strfreev ((gchar **) wenvp);
722 /* Close the other process's ends of the pipes in this process,
723 * otherwise the reader will never get EOF.
725 close_and_invalidate (&child_err_report_pipe[1]);
726 close_and_invalidate (&stdin_pipe[0]);
727 close_and_invalidate (&stdout_pipe[1]);
728 close_and_invalidate (&stderr_pipe[1]);
730 g_strfreev (protected_argv);
732 g_free (new_argv[ARG_WORKING_DIRECTORY]);
735 /* Check if gspawn-win32-helper couldn't be run */
736 if (rc == -1 && saved_errno != 0)
738 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
739 _("Failed to execute helper program (%s)"),
740 g_strerror (saved_errno));
741 goto cleanup_and_fail;
744 if (exit_status != NULL)
746 /* Synchronous case. Pass helper's report pipe back to caller,
747 * which takes care of reading it after the grandchild has
750 g_assert (err_report != NULL);
751 *err_report = child_err_report_pipe[0];
755 /* Asynchronous case. We read the helper's report right away. */
756 if (!read_helper_report (child_err_report_pipe[0], helper_report, error))
757 goto cleanup_and_fail;
759 close (child_err_report_pipe[0]);
761 switch (helper_report[0])
764 if (child_handle && do_return_handle)
766 /* rc is our HANDLE for gspawn-win32-helper. It has
767 * told us the HANDLE of its child. Duplicate that into
768 * a HANDLE valid in this process.
770 if (!DuplicateHandle ((HANDLE) rc, (HANDLE) helper_report[1],
771 GetCurrentProcess (), (LPHANDLE) child_handle,
772 0, TRUE, DUPLICATE_SAME_ACCESS))
775 else if (child_handle)
780 set_child_error (helper_report, working_directory, error);
781 goto cleanup_and_fail;
785 /* Success against all odds! return the information */
788 *standard_input = stdin_pipe[1];
790 *standard_output = stdout_pipe[0];
792 *standard_error = stderr_pipe[0];
794 CloseHandle ((HANDLE) rc);
800 CloseHandle ((HANDLE) rc);
801 if (child_err_report_pipe[0] != -1)
802 close (child_err_report_pipe[0]);
803 if (child_err_report_pipe[1] != -1)
804 close (child_err_report_pipe[1]);
805 if (stdin_pipe[0] != -1)
806 close (stdin_pipe[0]);
807 if (stdin_pipe[1] != -1)
808 close (stdin_pipe[1]);
809 if (stdout_pipe[0] != -1)
810 close (stdout_pipe[0]);
811 if (stdout_pipe[1] != -1)
812 close (stdout_pipe[1]);
813 if (stderr_pipe[0] != -1)
814 close (stderr_pipe[0]);
815 if (stderr_pipe[1] != -1)
816 close (stderr_pipe[1]);
822 g_spawn_sync_utf8 (const gchar *working_directory,
826 GSpawnChildSetupFunc child_setup,
828 gchar **standard_output,
829 gchar **standard_error,
835 gint reportpipe = -1;
836 GIOChannel *outchannel = NULL;
837 GIOChannel *errchannel = NULL;
838 GPollFD outfd, errfd;
844 GString *outstr = NULL;
845 GString *errstr = NULL;
849 g_return_val_if_fail (argv != NULL, FALSE);
850 g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
851 g_return_val_if_fail (standard_output == NULL ||
852 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
853 g_return_val_if_fail (standard_error == NULL ||
854 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
856 /* Just to ensure segfaults if callers try to use
857 * these when an error is reported.
860 *standard_output = NULL;
863 *standard_error = NULL;
865 if (!do_spawn_with_pipes (&status,
875 standard_output ? &outpipe : NULL,
876 standard_error ? &errpipe : NULL,
881 /* Read data from child. */
887 outstr = g_string_new (NULL);
888 outchannel = g_io_channel_win32_new_fd (outpipe);
889 g_io_channel_set_encoding (outchannel, NULL, NULL);
890 g_io_channel_set_buffered (outchannel, FALSE);
891 g_io_channel_win32_make_pollfd (outchannel,
892 G_IO_IN | G_IO_ERR | G_IO_HUP,
895 g_print ("outfd=%x\n", outfd.fd);
900 errstr = g_string_new (NULL);
901 errchannel = g_io_channel_win32_new_fd (errpipe);
902 g_io_channel_set_encoding (errchannel, NULL, NULL);
903 g_io_channel_set_buffered (errchannel, FALSE);
904 g_io_channel_win32_make_pollfd (errchannel,
905 G_IO_IN | G_IO_ERR | G_IO_HUP,
908 g_print ("errfd=%x\n", errfd.fd);
911 /* Read data until we get EOF on all pipes. */
912 while (!failed && (outpipe >= 0 || errpipe >= 0))
929 g_print ("g_spawn_sync: calling g_io_channel_win32_poll, nfds=%d\n",
932 ret = g_io_channel_win32_poll (fds, nfds, -1);
938 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_READ,
939 _("Unexpected error in g_io_channel_win32_poll() reading data from a child process"));
944 if (outpipe >= 0 && (fds[outindex].revents & G_IO_IN))
946 switch (read_data (outstr, outchannel, error))
950 g_print ("g_spawn_sync: outchannel: READ_FAILED\n");
955 g_print ("g_spawn_sync: outchannel: READ_EOF\n");
956 g_io_channel_unref (outchannel);
958 close_and_invalidate (&outpipe);
962 g_print ("g_spawn_sync: outchannel: OK\n");
970 if (errpipe >= 0 && (fds[errindex].revents & G_IO_IN))
972 switch (read_data (errstr, errchannel, error))
976 g_print ("g_spawn_sync: errchannel: READ_FAILED\n");
981 g_print ("g_spawn_sync: errchannel: READ_EOF\n");
982 g_io_channel_unref (errchannel);
984 close_and_invalidate (&errpipe);
988 g_print ("g_spawn_sync: errchannel: OK\n");
997 if (reportpipe == -1)
999 /* No helper process, exit status of actual spawned process
1000 * already available.
1003 *exit_status = status;
1007 /* Helper process was involved. Read its report now after the
1008 * grandchild has finished.
1010 gint helper_report[2];
1012 if (!read_helper_report (reportpipe, helper_report, error))
1016 switch (helper_report[0])
1018 case CHILD_NO_ERROR:
1020 *exit_status = helper_report[1];
1023 set_child_error (helper_report, working_directory, error);
1028 close_and_invalidate (&reportpipe);
1032 /* These should only be open still if we had an error. */
1034 if (outchannel != NULL)
1035 g_io_channel_unref (outchannel);
1036 if (errchannel != NULL)
1037 g_io_channel_unref (errchannel);
1039 close_and_invalidate (&outpipe);
1041 close_and_invalidate (&errpipe);
1046 g_string_free (outstr, TRUE);
1048 g_string_free (errstr, TRUE);
1054 if (standard_output)
1055 *standard_output = g_string_free (outstr, FALSE);
1058 *standard_error = g_string_free (errstr, FALSE);
1065 g_spawn_async_with_pipes_utf8 (const gchar *working_directory,
1069 GSpawnChildSetupFunc child_setup,
1072 gint *standard_input,
1073 gint *standard_output,
1074 gint *standard_error,
1077 g_return_val_if_fail (argv != NULL, FALSE);
1078 g_return_val_if_fail (standard_output == NULL ||
1079 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
1080 g_return_val_if_fail (standard_error == NULL ||
1081 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
1082 /* can't inherit stdin if we have an input pipe. */
1083 g_return_val_if_fail (standard_input == NULL ||
1084 !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
1086 return do_spawn_with_pipes (NULL,
1087 (flags & G_SPAWN_DO_NOT_REAP_CHILD),
1103 g_spawn_command_line_sync_utf8 (const gchar *command_line,
1104 gchar **standard_output,
1105 gchar **standard_error,
1112 g_return_val_if_fail (command_line != NULL, FALSE);
1114 if (!g_shell_parse_argv (command_line,
1119 retval = g_spawn_sync_utf8 (NULL,
1122 G_SPAWN_SEARCH_PATH,
1135 g_spawn_command_line_async_utf8 (const gchar *command_line,
1141 g_return_val_if_fail (command_line != NULL, FALSE);
1143 if (!g_shell_parse_argv (command_line,
1148 retval = g_spawn_async_utf8 (NULL,
1151 G_SPAWN_SEARCH_PATH,
1162 g_spawn_close_pid (GPid pid)
1167 /* Binary compatibility versions that take system codepage pathnames,
1168 * argument vectors and environments. These get used only by code
1169 * built against 2.8.1 or earlier. Code built against 2.8.2 or later
1170 * will use the _utf8 versions above (see the #defines in gspawn.h).
1173 #undef g_spawn_async
1174 #undef g_spawn_async_with_pipes
1176 #undef g_spawn_command_line_sync
1177 #undef g_spawn_command_line_async
1180 setup_utf8_copies (const gchar *working_directory,
1181 gchar **utf8_working_directory,
1190 if (working_directory == NULL)
1191 *utf8_working_directory = NULL;
1194 GError *conv_error = NULL;
1196 *utf8_working_directory = g_locale_to_utf8 (working_directory, -1, NULL, NULL, &conv_error);
1197 if (*utf8_working_directory == NULL)
1199 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_CHDIR,
1200 _("Invalid working directory: %s"),
1201 conv_error->message);
1202 g_error_free (conv_error);
1210 *utf8_argv = g_new (gchar *, argc + 1);
1211 for (i = 0; i < argc; i++)
1213 GError *conv_error = NULL;
1215 (*utf8_argv)[i] = g_locale_to_utf8 (argv[i], -1, NULL, NULL, &conv_error);
1216 if ((*utf8_argv)[i] == NULL)
1218 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
1219 _("Invalid string in argument vector at %d: %s"),
1220 i, conv_error->message);
1221 g_error_free (conv_error);
1223 g_strfreev (*utf8_argv);
1226 g_free (*utf8_working_directory);
1227 *utf8_working_directory = NULL;
1232 (*utf8_argv)[argc] = NULL;
1243 *utf8_envp = g_new (gchar *, envc + 1);
1244 for (i = 0; i < envc; i++)
1246 GError *conv_error = NULL;
1248 (*utf8_envp)[i] = g_locale_to_utf8 (envp[i], -1, NULL, NULL, &conv_error);
1249 if ((*utf8_envp)[i] == NULL)
1251 g_set_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED,
1252 _("Invalid string in environment: %s"),
1253 conv_error->message);
1254 g_error_free (conv_error);
1256 g_strfreev (*utf8_envp);
1259 g_strfreev (*utf8_argv);
1262 g_free (*utf8_working_directory);
1263 *utf8_working_directory = NULL;
1268 (*utf8_envp)[envc] = NULL;
1274 free_utf8_copies (gchar *utf8_working_directory,
1278 g_free (utf8_working_directory);
1279 g_strfreev (utf8_argv);
1280 g_strfreev (utf8_envp);
1284 g_spawn_async_with_pipes (const gchar *working_directory,
1288 GSpawnChildSetupFunc child_setup,
1291 gint *standard_input,
1292 gint *standard_output,
1293 gint *standard_error,
1296 gchar *utf8_working_directory;
1301 if (!setup_utf8_copies (working_directory, &utf8_working_directory,
1307 retval = g_spawn_async_with_pipes_utf8 (utf8_working_directory,
1308 utf8_argv, utf8_envp,
1309 flags, child_setup, user_data,
1311 standard_input, standard_output, standard_error,
1314 free_utf8_copies (utf8_working_directory, utf8_argv, utf8_envp);
1320 g_spawn_async (const gchar *working_directory,
1324 GSpawnChildSetupFunc child_setup,
1329 return g_spawn_async_with_pipes (working_directory,
1340 g_spawn_sync (const gchar *working_directory,
1344 GSpawnChildSetupFunc child_setup,
1346 gchar **standard_output,
1347 gchar **standard_error,
1351 gchar *utf8_working_directory;
1356 if (!setup_utf8_copies (working_directory, &utf8_working_directory,
1362 retval = g_spawn_sync_utf8 (utf8_working_directory,
1363 utf8_argv, utf8_envp,
1364 flags, child_setup, user_data,
1365 standard_output, standard_error, exit_status,
1368 free_utf8_copies (utf8_working_directory, utf8_argv, utf8_envp);
1374 g_spawn_command_line_sync (const gchar *command_line,
1375 gchar **standard_output,
1376 gchar **standard_error,
1383 g_return_val_if_fail (command_line != NULL, FALSE);
1385 if (!g_shell_parse_argv (command_line,
1390 retval = g_spawn_sync (NULL,
1393 G_SPAWN_SEARCH_PATH,
1406 g_spawn_command_line_async (const gchar *command_line,
1412 g_return_val_if_fail (command_line != NULL, FALSE);
1414 if (!g_shell_parse_argv (command_line,
1419 retval = g_spawn_async (NULL,
1422 G_SPAWN_SEARCH_PATH,
1432 #endif /* !GSPAWN_HELPER */
1434 #define __G_SPAWN_C__
1435 #include "galiasdef.c"