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"
64 /* Mingw doesn't have prototypes for these */
65 int _wspawnvpe (int, const wchar_t *, const wchar_t **, const wchar_t **);
66 int _wspawnvp (int, const wchar_t *, const wchar_t **);
67 int _wspawnve (int, const wchar_t *, const wchar_t **, const wchar_t **);
68 int _wspawnv (int, const wchar_t *, const wchar_t **);
71 #ifdef G_SPAWN_WIN32_DEBUG
73 #define SETUP_DEBUG() /* empty */
75 static int debug
= -1;
76 #define SETUP_DEBUG() \
81 if (getenv ("G_SPAWN_WIN32_DEBUG") != NULL) \
98 ARG_CHILD_ERR_REPORT
= 1,
102 ARG_WORKING_DIRECTORY
,
103 ARG_CLOSE_DESCRIPTORS
,
107 ARG_COUNT
= ARG_PROGRAM
110 #ifndef GSPAWN_HELPER
112 #define HELPER_PROCESS "gspawn-win32-helper"
115 protect_argv_string (const gchar
*string
)
117 const gchar
*p
= string
;
120 gboolean need_dblquotes
= FALSE
;
123 if (*p
== ' ' || *p
== '\t')
124 need_dblquotes
= TRUE
;
130 while (*pp
&& *pp
== '\\')
139 q
= retval
= g_malloc (len
+ need_dblquotes
*2 + 1);
152 while (*pp
&& *pp
== '\\')
169 protect_argv (gchar
**argv
,
177 *new_argv
= g_new (gchar
*, argc
+1);
179 /* Quote each argv element if necessary, so that it will get
180 * reconstructed correctly in the C runtime startup code. Note that
181 * the unquoting algorithm in the C runtime is really weird, and
182 * rather different than what Unix shells do. See stdargv.c in the C
183 * runtime sources (in the Platform SDK, in src/crt).
185 * Note that an new_argv[0] constructed by this function should
186 * *not* be passed as the filename argument to a spawn* or exec*
187 * family function. That argument should be the real file name
188 * without any quoting.
190 for (i
= 0; i
< argc
; i
++)
191 (*new_argv
)[i
] = protect_argv_string (argv
[i
]);
193 (*new_argv
)[argc
] = NULL
;
199 g_spawn_error_quark (void)
201 return g_quark_from_static_string ("g-exec-error-quark");
205 g_spawn_async_utf8 (const gchar
*working_directory
,
209 GSpawnChildSetupFunc child_setup
,
214 g_return_val_if_fail (argv
!= NULL
, FALSE
);
216 return g_spawn_async_with_pipes_utf8 (working_directory
,
226 /* Avoids a danger in threaded situations (calling close()
227 * on a file descriptor twice, and another thread has
228 * re-opened it since the first close)
231 close_and_invalidate (gint
*fd
)
242 READ_FAILED
= 0, /* FALSE */
248 read_data (GString
*str
,
249 GIOChannel
*iochannel
,
258 giostatus
= g_io_channel_read_chars (iochannel
, buf
, sizeof (buf
), &bytes
, NULL
);
264 g_string_append_len (str
, buf
, bytes
);
267 else if (giostatus
== G_IO_STATUS_AGAIN
)
269 else if (giostatus
== G_IO_STATUS_ERROR
)
271 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_READ
,
272 _("Failed to read data from child process"));
281 make_pipe (gint p
[2],
286 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
287 _("Failed to create pipe for communicating with child process (%s)"),
295 /* The helper process writes a status report back to us, through a
296 * pipe, consisting of two ints.
299 read_helper_report (int fd
,
305 while (bytes
< sizeof(gint
)*2)
310 g_print ("%s:read_helper_report: read %d...\n",
312 sizeof(gint
)*2 - bytes
);
314 chunk
= read (fd
, ((gchar
*)report
) + bytes
,
315 sizeof(gint
)*2 - bytes
);
318 g_print ("...got %d bytes\n", chunk
);
322 /* Some weird shit happened, bail out */
324 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
325 _("Failed to read from child pipe (%s)"),
336 if (bytes
< sizeof(gint
)*2)
343 set_child_error (gint report
[2],
344 const gchar
*working_directory
,
349 case CHILD_CHDIR_FAILED
:
350 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_CHDIR
,
351 _("Failed to change to directory '%s' (%s)"),
353 g_strerror (report
[1]));
355 case CHILD_SPAWN_FAILED
:
356 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
357 _("Failed to execute child process (%s)"),
358 g_strerror (report
[1]));
361 g_assert_not_reached ();
366 utf8_charv_to_wcharv (char **utf8_charv
,
371 wchar_t **retval
= NULL
;
374 if (utf8_charv
!= NULL
)
378 while (utf8_charv
[n
])
380 retval
= g_new (wchar_t *, n
+ 1);
382 for (i
= 0; i
< n
; i
++)
384 retval
[i
] = g_utf8_to_utf16 (utf8_charv
[i
], -1, NULL
, NULL
, error
);
385 if (retval
[i
] == NULL
)
390 g_free (retval
[--i
]);
403 do_spawn_directly (gint
*exit_status
,
404 gboolean do_return_handle
,
408 char **protected_argv
,
409 GSpawnChildSetupFunc child_setup
,
414 const int mode
= (exit_status
== NULL
) ? P_NOWAIT
: P_WAIT
;
418 GError
*conv_error
= NULL
;
419 gint conv_error_index
;
420 wchar_t *wargv0
, **wargv
, **wenvp
;
422 new_argv
= (flags
& G_SPAWN_FILE_AND_ARGV_ZERO
) ? protected_argv
+ 1 : protected_argv
;
424 wargv0
= g_utf8_to_utf16 (argv
[0], -1, NULL
, NULL
, &conv_error
);
427 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
428 _("Invalid program name: %s"),
429 conv_error
->message
);
430 g_error_free (conv_error
);
435 if (!utf8_charv_to_wcharv (new_argv
, &wargv
, &conv_error_index
, &conv_error
))
437 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
438 _("Invalid string in argument vector at %d: %s"),
439 conv_error_index
, conv_error
->message
);
440 g_error_free (conv_error
);
446 if (!utf8_charv_to_wcharv (envp
, &wenvp
, NULL
, &conv_error
))
448 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
449 _("Invalid string in environment: %s"),
450 conv_error
->message
);
451 g_error_free (conv_error
);
453 g_strfreev ((gchar
**) wargv
);
459 (* child_setup
) (user_data
);
461 if (flags
& G_SPAWN_SEARCH_PATH
)
463 rc
= _wspawnvpe (mode
, wargv0
, (const wchar_t **) wargv
, (const wchar_t **) wenvp
);
465 rc
= _wspawnvp (mode
, wargv0
, (const wchar_t **) wargv
);
468 rc
= _wspawnve (mode
, wargv0
, (const wchar_t **) wargv
, (const wchar_t **) wenvp
);
470 rc
= _wspawnv (mode
, wargv0
, (const wchar_t **) wargv
);
473 g_strfreev ((gchar
**) wargv
);
474 g_strfreev ((gchar
**) wenvp
);
478 if (rc
== -1 && saved_errno
!= 0)
480 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
481 _("Failed to execute child process (%s)"),
482 g_strerror (saved_errno
));
486 if (exit_status
== NULL
)
488 if (child_handle
&& do_return_handle
)
489 *child_handle
= (GPid
) rc
;
492 CloseHandle ((HANDLE
) rc
);
504 do_spawn_with_pipes (gint
*exit_status
,
505 gboolean do_return_handle
,
506 const gchar
*working_directory
,
510 GSpawnChildSetupFunc child_setup
,
513 gint
*standard_input
,
514 gint
*standard_output
,
515 gint
*standard_error
,
519 char **protected_argv
;
520 char args
[ARG_COUNT
][10];
526 int stdin_pipe
[2] = { -1, -1 };
527 int stdout_pipe
[2] = { -1, -1 };
528 int stderr_pipe
[2] = { -1, -1 };
529 int child_err_report_pipe
[2] = { -1, -1 };
530 int helper_report
[2];
531 static gboolean warned_about_child_setup
= FALSE
;
532 GError
*conv_error
= NULL
;
533 gint conv_error_index
;
534 gchar
*helper_process
;
535 CONSOLE_CURSOR_INFO cursor_info
;
536 wchar_t *whelper
, **wargv
, **wenvp
;
540 if (child_setup
&& !warned_about_child_setup
)
542 warned_about_child_setup
= TRUE
;
543 g_warning ("passing a child setup function to the g_spawn functions is pointless and dangerous on Win32");
546 argc
= protect_argv (argv
, &protected_argv
);
548 if (!standard_input
&& !standard_output
&& !standard_error
&&
549 (flags
& G_SPAWN_CHILD_INHERITS_STDIN
) &&
550 !(flags
& G_SPAWN_STDOUT_TO_DEV_NULL
) &&
551 !(flags
& G_SPAWN_STDERR_TO_DEV_NULL
) &&
552 (working_directory
== NULL
|| !*working_directory
) &&
553 (flags
& G_SPAWN_LEAVE_DESCRIPTORS_OPEN
))
555 /* We can do without the helper process */
557 do_spawn_directly (exit_status
, do_return_handle
, flags
,
558 argv
, envp
, protected_argv
,
559 child_setup
, user_data
, child_handle
,
561 g_strfreev (protected_argv
);
565 if (standard_input
&& !make_pipe (stdin_pipe
, error
))
566 goto cleanup_and_fail
;
568 if (standard_output
&& !make_pipe (stdout_pipe
, error
))
569 goto cleanup_and_fail
;
571 if (standard_error
&& !make_pipe (stderr_pipe
, error
))
572 goto cleanup_and_fail
;
574 if (!make_pipe (child_err_report_pipe
, error
))
575 goto cleanup_and_fail
;
577 new_argv
= g_new (char *, argc
+ 1 + ARG_COUNT
);
578 if (GetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE
), &cursor_info
))
579 helper_process
= HELPER_PROCESS
"-console.exe";
581 helper_process
= HELPER_PROCESS
".exe";
582 new_argv
[0] = helper_process
;
583 _g_sprintf (args
[ARG_CHILD_ERR_REPORT
], "%d", child_err_report_pipe
[1]);
584 new_argv
[ARG_CHILD_ERR_REPORT
] = args
[ARG_CHILD_ERR_REPORT
];
586 if (flags
& G_SPAWN_FILE_AND_ARGV_ZERO
)
588 /* Overload ARG_CHILD_ERR_REPORT to also encode the
589 * G_SPAWN_FILE_AND_ARGV_ZERO functionality.
591 strcat (args
[ARG_CHILD_ERR_REPORT
], "#");
596 _g_sprintf (args
[ARG_STDIN
], "%d", stdin_pipe
[0]);
597 new_argv
[ARG_STDIN
] = args
[ARG_STDIN
];
599 else if (flags
& G_SPAWN_CHILD_INHERITS_STDIN
)
601 /* Let stdin be alone */
602 new_argv
[ARG_STDIN
] = "-";
606 /* Keep process from blocking on a read of stdin */
607 new_argv
[ARG_STDIN
] = "z";
612 _g_sprintf (args
[ARG_STDOUT
], "%d", stdout_pipe
[1]);
613 new_argv
[ARG_STDOUT
] = args
[ARG_STDOUT
];
615 else if (flags
& G_SPAWN_STDOUT_TO_DEV_NULL
)
617 new_argv
[ARG_STDOUT
] = "z";
621 new_argv
[ARG_STDOUT
] = "-";
626 _g_sprintf (args
[ARG_STDERR
], "%d", stderr_pipe
[1]);
627 new_argv
[ARG_STDERR
] = args
[ARG_STDERR
];
629 else if (flags
& G_SPAWN_STDERR_TO_DEV_NULL
)
631 new_argv
[ARG_STDERR
] = "z";
635 new_argv
[ARG_STDERR
] = "-";
638 if (working_directory
&& *working_directory
)
639 new_argv
[ARG_WORKING_DIRECTORY
] = protect_argv_string (working_directory
);
641 new_argv
[ARG_WORKING_DIRECTORY
] = g_strdup ("-");
643 if (!(flags
& G_SPAWN_LEAVE_DESCRIPTORS_OPEN
))
644 new_argv
[ARG_CLOSE_DESCRIPTORS
] = "y";
646 new_argv
[ARG_CLOSE_DESCRIPTORS
] = "-";
648 if (flags
& G_SPAWN_SEARCH_PATH
)
649 new_argv
[ARG_USE_PATH
] = "y";
651 new_argv
[ARG_USE_PATH
] = "-";
653 if (exit_status
== NULL
)
654 new_argv
[ARG_WAIT
] = "-";
656 new_argv
[ARG_WAIT
] = "w";
658 for (i
= 0; i
<= argc
; i
++)
659 new_argv
[ARG_PROGRAM
+ i
] = protected_argv
[i
];
663 g_print ("calling %s with argv:\n", helper_process
);
664 for (i
= 0; i
< argc
+ 1 + ARG_COUNT
; i
++)
665 g_print ("argv[%d]: %s\n", i
, (new_argv
[i
] ? new_argv
[i
] : "NULL"));
668 whelper
= g_utf8_to_utf16 (helper_process
, -1, NULL
, NULL
, NULL
);
670 if (!utf8_charv_to_wcharv (new_argv
, &wargv
, &conv_error_index
, &conv_error
))
672 if (conv_error_index
== ARG_WORKING_DIRECTORY
)
673 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_CHDIR
,
674 _("Invalid working directory: %s"),
675 conv_error
->message
);
677 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
678 _("Invalid string in argument vector at %d: %s"),
679 conv_error_index
- ARG_PROGRAM
, conv_error
->message
);
680 g_error_free (conv_error
);
681 g_strfreev (protected_argv
);
682 g_free (new_argv
[ARG_WORKING_DIRECTORY
]);
689 if (!utf8_charv_to_wcharv (envp
, &wenvp
, NULL
, &conv_error
))
691 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
692 _("Invalid string in environment: %s"),
693 conv_error
->message
);
694 g_error_free (conv_error
);
695 g_strfreev (protected_argv
);
696 g_free (new_argv
[ARG_WORKING_DIRECTORY
]);
699 g_strfreev ((gchar
**) wargv
);
705 (* child_setup
) (user_data
);
708 /* Let's hope envp hasn't mucked with PATH so that
709 * gspawn-win32-helper.exe isn't found.
711 rc
= _wspawnvpe (P_NOWAIT
, whelper
, (const wchar_t **) wargv
, (const wchar_t **) wenvp
);
713 rc
= _wspawnvp (P_NOWAIT
, whelper
, (const wchar_t **) wargv
);
718 g_strfreev ((gchar
**) wargv
);
719 g_strfreev ((gchar
**) wenvp
);
721 /* Close the other process's ends of the pipes in this process,
722 * otherwise the reader will never get EOF.
724 close_and_invalidate (&child_err_report_pipe
[1]);
725 close_and_invalidate (&stdin_pipe
[0]);
726 close_and_invalidate (&stdout_pipe
[1]);
727 close_and_invalidate (&stderr_pipe
[1]);
729 g_strfreev (protected_argv
);
731 g_free (new_argv
[ARG_WORKING_DIRECTORY
]);
734 /* Check if gspawn-win32-helper couldn't be run */
735 if (rc
== -1 && saved_errno
!= 0)
737 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
738 _("Failed to execute helper program (%s)"),
739 g_strerror (saved_errno
));
740 goto cleanup_and_fail
;
743 if (exit_status
!= NULL
)
745 /* Synchronous case. Pass helper's report pipe back to caller,
746 * which takes care of reading it after the grandchild has
749 g_assert (err_report
!= NULL
);
750 *err_report
= child_err_report_pipe
[0];
754 /* Asynchronous case. We read the helper's report right away. */
755 if (!read_helper_report (child_err_report_pipe
[0], helper_report
, error
))
756 goto cleanup_and_fail
;
758 close (child_err_report_pipe
[0]);
760 switch (helper_report
[0])
763 if (child_handle
&& do_return_handle
)
765 /* rc is our HANDLE for gspawn-win32-helper. It has
766 * told us the HANDLE of its child. Duplicate that into
767 * a HANDLE valid in this process.
769 if (!DuplicateHandle ((HANDLE
) rc
, (HANDLE
) helper_report
[1],
770 GetCurrentProcess (), (LPHANDLE
) child_handle
,
771 0, TRUE
, DUPLICATE_SAME_ACCESS
))
774 else if (child_handle
)
779 set_child_error (helper_report
, working_directory
, error
);
780 goto cleanup_and_fail
;
784 /* Success against all odds! return the information */
787 *standard_input
= stdin_pipe
[1];
789 *standard_output
= stdout_pipe
[0];
791 *standard_error
= stderr_pipe
[0];
793 CloseHandle ((HANDLE
) rc
);
799 CloseHandle ((HANDLE
) rc
);
800 if (child_err_report_pipe
[0] != -1)
801 close (child_err_report_pipe
[0]);
802 if (child_err_report_pipe
[1] != -1)
803 close (child_err_report_pipe
[1]);
804 if (stdin_pipe
[0] != -1)
805 close (stdin_pipe
[0]);
806 if (stdin_pipe
[1] != -1)
807 close (stdin_pipe
[1]);
808 if (stdout_pipe
[0] != -1)
809 close (stdout_pipe
[0]);
810 if (stdout_pipe
[1] != -1)
811 close (stdout_pipe
[1]);
812 if (stderr_pipe
[0] != -1)
813 close (stderr_pipe
[0]);
814 if (stderr_pipe
[1] != -1)
815 close (stderr_pipe
[1]);
821 g_spawn_sync_utf8 (const gchar
*working_directory
,
825 GSpawnChildSetupFunc child_setup
,
827 gchar
**standard_output
,
828 gchar
**standard_error
,
834 gint reportpipe
= -1;
835 GIOChannel
*outchannel
= NULL
;
836 GIOChannel
*errchannel
= NULL
;
837 GPollFD outfd
, errfd
;
843 GString
*outstr
= NULL
;
844 GString
*errstr
= NULL
;
848 g_return_val_if_fail (argv
!= NULL
, FALSE
);
849 g_return_val_if_fail (!(flags
& G_SPAWN_DO_NOT_REAP_CHILD
), FALSE
);
850 g_return_val_if_fail (standard_output
== NULL
||
851 !(flags
& G_SPAWN_STDOUT_TO_DEV_NULL
), FALSE
);
852 g_return_val_if_fail (standard_error
== NULL
||
853 !(flags
& G_SPAWN_STDERR_TO_DEV_NULL
), FALSE
);
855 /* Just to ensure segfaults if callers try to use
856 * these when an error is reported.
859 *standard_output
= NULL
;
862 *standard_error
= NULL
;
864 if (!do_spawn_with_pipes (&status
,
874 standard_output
? &outpipe
: NULL
,
875 standard_error
? &errpipe
: NULL
,
880 /* Read data from child. */
886 outstr
= g_string_new (NULL
);
887 outchannel
= g_io_channel_win32_new_fd (outpipe
);
888 g_io_channel_set_encoding (outchannel
, NULL
, NULL
);
889 g_io_channel_set_buffered (outchannel
, FALSE
);
890 g_io_channel_win32_make_pollfd (outchannel
,
891 G_IO_IN
| G_IO_ERR
| G_IO_HUP
,
894 g_print ("outfd=%x\n", outfd
.fd
);
899 errstr
= g_string_new (NULL
);
900 errchannel
= g_io_channel_win32_new_fd (errpipe
);
901 g_io_channel_set_encoding (errchannel
, NULL
, NULL
);
902 g_io_channel_set_buffered (errchannel
, FALSE
);
903 g_io_channel_win32_make_pollfd (errchannel
,
904 G_IO_IN
| G_IO_ERR
| G_IO_HUP
,
907 g_print ("errfd=%x\n", errfd
.fd
);
910 /* Read data until we get EOF on all pipes. */
911 while (!failed
&& (outpipe
>= 0 || errpipe
>= 0))
928 g_print ("g_spawn_sync: calling g_io_channel_win32_poll, nfds=%d\n",
931 ret
= g_io_channel_win32_poll (fds
, nfds
, -1);
937 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_READ
,
938 _("Unexpected error in g_io_channel_win32_poll() reading data from a child process"));
943 if (outpipe
>= 0 && (fds
[outindex
].revents
& G_IO_IN
))
945 switch (read_data (outstr
, outchannel
, error
))
949 g_print ("g_spawn_sync: outchannel: READ_FAILED\n");
954 g_print ("g_spawn_sync: outchannel: READ_EOF\n");
955 g_io_channel_unref (outchannel
);
957 close_and_invalidate (&outpipe
);
961 g_print ("g_spawn_sync: outchannel: OK\n");
969 if (errpipe
>= 0 && (fds
[errindex
].revents
& G_IO_IN
))
971 switch (read_data (errstr
, errchannel
, error
))
975 g_print ("g_spawn_sync: errchannel: READ_FAILED\n");
980 g_print ("g_spawn_sync: errchannel: READ_EOF\n");
981 g_io_channel_unref (errchannel
);
983 close_and_invalidate (&errpipe
);
987 g_print ("g_spawn_sync: errchannel: OK\n");
996 if (reportpipe
== -1)
998 /* No helper process, exit status of actual spawned process
1002 *exit_status
= status
;
1006 /* Helper process was involved. Read its report now after the
1007 * grandchild has finished.
1009 gint helper_report
[2];
1011 if (!read_helper_report (reportpipe
, helper_report
, error
))
1015 switch (helper_report
[0])
1017 case CHILD_NO_ERROR
:
1019 *exit_status
= helper_report
[1];
1022 set_child_error (helper_report
, working_directory
, error
);
1027 close_and_invalidate (&reportpipe
);
1031 /* These should only be open still if we had an error. */
1033 if (outchannel
!= NULL
)
1034 g_io_channel_unref (outchannel
);
1035 if (errchannel
!= NULL
)
1036 g_io_channel_unref (errchannel
);
1038 close_and_invalidate (&outpipe
);
1040 close_and_invalidate (&errpipe
);
1045 g_string_free (outstr
, TRUE
);
1047 g_string_free (errstr
, TRUE
);
1053 if (standard_output
)
1054 *standard_output
= g_string_free (outstr
, FALSE
);
1057 *standard_error
= g_string_free (errstr
, FALSE
);
1064 g_spawn_async_with_pipes_utf8 (const gchar
*working_directory
,
1068 GSpawnChildSetupFunc child_setup
,
1071 gint
*standard_input
,
1072 gint
*standard_output
,
1073 gint
*standard_error
,
1076 g_return_val_if_fail (argv
!= NULL
, FALSE
);
1077 g_return_val_if_fail (standard_output
== NULL
||
1078 !(flags
& G_SPAWN_STDOUT_TO_DEV_NULL
), FALSE
);
1079 g_return_val_if_fail (standard_error
== NULL
||
1080 !(flags
& G_SPAWN_STDERR_TO_DEV_NULL
), FALSE
);
1081 /* can't inherit stdin if we have an input pipe. */
1082 g_return_val_if_fail (standard_input
== NULL
||
1083 !(flags
& G_SPAWN_CHILD_INHERITS_STDIN
), FALSE
);
1085 return do_spawn_with_pipes (NULL
,
1086 (flags
& G_SPAWN_DO_NOT_REAP_CHILD
),
1102 g_spawn_command_line_sync_utf8 (const gchar
*command_line
,
1103 gchar
**standard_output
,
1104 gchar
**standard_error
,
1111 g_return_val_if_fail (command_line
!= NULL
, FALSE
);
1113 if (!g_shell_parse_argv (command_line
,
1118 retval
= g_spawn_sync_utf8 (NULL
,
1121 G_SPAWN_SEARCH_PATH
,
1134 g_spawn_command_line_async_utf8 (const gchar
*command_line
,
1140 g_return_val_if_fail (command_line
!= NULL
, FALSE
);
1142 if (!g_shell_parse_argv (command_line
,
1147 retval
= g_spawn_async_utf8 (NULL
,
1150 G_SPAWN_SEARCH_PATH
,
1161 g_spawn_close_pid (GPid pid
)
1166 /* Binary compatibility versions that take system codepage pathnames,
1167 * argument vectors and environments. These get used only by code
1168 * built against 2.8.1 or earlier. Code built against 2.8.2 or later
1169 * will use the _utf8 versions above (see the #defines in gspawn.h).
1172 #undef g_spawn_async
1173 #undef g_spawn_async_with_pipes
1175 #undef g_spawn_command_line_sync
1176 #undef g_spawn_command_line_async
1179 setup_utf8_copies (const gchar
*working_directory
,
1180 gchar
**utf8_working_directory
,
1189 if (working_directory
== NULL
)
1190 *utf8_working_directory
= NULL
;
1193 GError
*conv_error
= NULL
;
1195 *utf8_working_directory
= g_locale_to_utf8 (working_directory
, -1, NULL
, NULL
, &conv_error
);
1196 if (*utf8_working_directory
== NULL
)
1198 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_CHDIR
,
1199 _("Invalid working directory: %s"),
1200 conv_error
->message
);
1201 g_error_free (conv_error
);
1209 *utf8_argv
= g_new (gchar
*, argc
+ 1);
1210 for (i
= 0; i
< argc
; i
++)
1212 GError
*conv_error
= NULL
;
1214 (*utf8_argv
)[i
] = g_locale_to_utf8 (argv
[i
], -1, NULL
, NULL
, &conv_error
);
1215 if ((*utf8_argv
)[i
] == NULL
)
1217 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
1218 _("Invalid string in argument vector at %d: %s"),
1219 i
, conv_error
->message
);
1220 g_error_free (conv_error
);
1222 g_strfreev (*utf8_argv
);
1225 g_free (*utf8_working_directory
);
1226 *utf8_working_directory
= NULL
;
1231 (*utf8_argv
)[argc
] = NULL
;
1242 *utf8_envp
= g_new (gchar
*, envc
+ 1);
1243 for (i
= 0; i
< envc
; i
++)
1245 GError
*conv_error
= NULL
;
1247 (*utf8_envp
)[i
] = g_locale_to_utf8 (envp
[i
], -1, NULL
, NULL
, &conv_error
);
1248 if ((*utf8_envp
)[i
] == NULL
)
1250 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
1251 _("Invalid string in environment: %s"),
1252 conv_error
->message
);
1253 g_error_free (conv_error
);
1255 g_strfreev (*utf8_envp
);
1258 g_strfreev (*utf8_argv
);
1261 g_free (*utf8_working_directory
);
1262 *utf8_working_directory
= NULL
;
1267 (*utf8_envp
)[envc
] = NULL
;
1273 free_utf8_copies (gchar
*utf8_working_directory
,
1277 g_free (utf8_working_directory
);
1278 g_strfreev (utf8_argv
);
1279 g_strfreev (utf8_envp
);
1283 g_spawn_async_with_pipes (const gchar
*working_directory
,
1287 GSpawnChildSetupFunc child_setup
,
1290 gint
*standard_input
,
1291 gint
*standard_output
,
1292 gint
*standard_error
,
1295 gchar
*utf8_working_directory
;
1300 if (!setup_utf8_copies (working_directory
, &utf8_working_directory
,
1306 retval
= g_spawn_async_with_pipes_utf8 (utf8_working_directory
,
1307 utf8_argv
, utf8_envp
,
1308 flags
, child_setup
, user_data
,
1310 standard_input
, standard_output
, standard_error
,
1313 free_utf8_copies (utf8_working_directory
, utf8_argv
, utf8_envp
);
1319 g_spawn_async (const gchar
*working_directory
,
1323 GSpawnChildSetupFunc child_setup
,
1328 return g_spawn_async_with_pipes (working_directory
,
1339 g_spawn_sync (const gchar
*working_directory
,
1343 GSpawnChildSetupFunc child_setup
,
1345 gchar
**standard_output
,
1346 gchar
**standard_error
,
1350 gchar
*utf8_working_directory
;
1355 if (!setup_utf8_copies (working_directory
, &utf8_working_directory
,
1361 retval
= g_spawn_sync_utf8 (utf8_working_directory
,
1362 utf8_argv
, utf8_envp
,
1363 flags
, child_setup
, user_data
,
1364 standard_output
, standard_error
, exit_status
,
1367 free_utf8_copies (utf8_working_directory
, utf8_argv
, utf8_envp
);
1373 g_spawn_command_line_sync (const gchar
*command_line
,
1374 gchar
**standard_output
,
1375 gchar
**standard_error
,
1382 g_return_val_if_fail (command_line
!= NULL
, FALSE
);
1384 if (!g_shell_parse_argv (command_line
,
1389 retval
= g_spawn_sync (NULL
,
1392 G_SPAWN_SEARCH_PATH
,
1405 g_spawn_command_line_async (const gchar
*command_line
,
1411 g_return_val_if_fail (command_line
!= NULL
, FALSE
);
1413 if (!g_shell_parse_argv (command_line
,
1418 retval
= g_spawn_async (NULL
,
1421 G_SPAWN_SEARCH_PATH
,
1431 #endif /* !GSPAWN_HELPER */
1433 #define __G_SPAWN_C__
1434 #include "galiasdef.c"