1 /* gspawn-win32.c - Process launching on Win32
3 * Copyright 2000 Red Hat, Inc.
4 * Copyright 2003 Tor Lillqvist
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
21 * Implementation details on Win32.
23 * - There is no way to set the no-inherit flag for
24 * a "file descriptor" in the MS C runtime. The flag is there,
25 * and the dospawn() function uses it, but unfortunately
26 * this flag can only be set when opening the file.
27 * - As there is no fork(), we cannot reliably change directory
28 * before starting the child process. (There might be several threads
29 * running, and the current directory is common for all threads.)
31 * Thus, we must in many cases use a helper program to handle closing
32 * of (inherited) file descriptors and changing of directory. The
33 * helper process is also needed if the standard input, standard
34 * output, or standard error of the process to be run are supposed to
35 * be redirected somewhere.
37 * The structure of the source code in this file is a mess, I know.
40 /* Define this to get some logging all the time */
41 /* #define G_SPAWN_WIN32_DEBUG */
46 #include "glib-private.h"
47 #include "gprintfint.h"
49 #include "gspawn-private.h"
65 #ifdef G_SPAWN_WIN32_DEBUG
67 #define SETUP_DEBUG() /* empty */
69 static int debug
= -1;
70 #define SETUP_DEBUG() \
75 if (getenv ("G_SPAWN_WIN32_DEBUG") != NULL) \
94 ARG_CHILD_ERR_REPORT
= 1,
99 ARG_WORKING_DIRECTORY
,
100 ARG_CLOSE_DESCRIPTORS
,
104 ARG_COUNT
= ARG_PROGRAM
108 dup_noninherited (int fd
,
113 DuplicateHandle (GetCurrentProcess (), (LPHANDLE
) _get_osfhandle (fd
),
114 GetCurrentProcess (), &filehandle
,
115 0, FALSE
, DUPLICATE_SAME_ACCESS
);
117 return _open_osfhandle ((gintptr
) filehandle
, mode
| _O_NOINHERIT
);
120 #ifndef GSPAWN_HELPER
123 #define HELPER_PROCESS "gspawn-win64-helper"
125 #define HELPER_PROCESS "gspawn-win32-helper"
129 protect_argv_string (const gchar
*string
)
131 const gchar
*p
= string
;
134 gboolean need_dblquotes
= FALSE
;
137 if (*p
== ' ' || *p
== '\t')
138 need_dblquotes
= TRUE
;
144 while (*pp
&& *pp
== '\\')
153 q
= retval
= g_malloc (len
+ need_dblquotes
*2 + 1);
166 while (*pp
&& *pp
== '\\')
183 protect_argv (gchar
**argv
,
191 *new_argv
= g_new (gchar
*, argc
+1);
193 /* Quote each argv element if necessary, so that it will get
194 * reconstructed correctly in the C runtime startup code. Note that
195 * the unquoting algorithm in the C runtime is really weird, and
196 * rather different than what Unix shells do. See stdargv.c in the C
197 * runtime sources (in the Platform SDK, in src/crt).
199 * Note that an new_argv[0] constructed by this function should
200 * *not* be passed as the filename argument to a spawn* or exec*
201 * family function. That argument should be the real file name
202 * without any quoting.
204 for (i
= 0; i
< argc
; i
++)
205 (*new_argv
)[i
] = protect_argv_string (argv
[i
]);
207 (*new_argv
)[argc
] = NULL
;
212 G_DEFINE_QUARK (g
-exec
-error
-quark
, g_spawn_error
)
213 G_DEFINE_QUARK (g
-spawn
-exit
-error
-quark
, g_spawn_exit_error
)
216 g_spawn_async (const gchar
*working_directory
,
220 GSpawnChildSetupFunc child_setup
,
225 g_return_val_if_fail (argv
!= NULL
, FALSE
);
227 return g_spawn_async_with_pipes (working_directory
,
237 /* Avoids a danger in threaded situations (calling close()
238 * on a file descriptor twice, and another thread has
239 * re-opened it since the first close)
242 close_and_invalidate (gint
*fd
)
253 READ_FAILED
= 0, /* FALSE */
259 read_data (GString
*str
,
260 GIOChannel
*iochannel
,
269 giostatus
= g_io_channel_read_chars (iochannel
, buf
, sizeof (buf
), &bytes
, NULL
);
275 g_string_append_len (str
, buf
, bytes
);
278 else if (giostatus
== G_IO_STATUS_AGAIN
)
280 else if (giostatus
== G_IO_STATUS_ERROR
)
282 g_set_error_literal (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_READ
,
283 _("Failed to read data from child process"));
292 make_pipe (gint p
[2],
295 if (_pipe (p
, 4096, _O_BINARY
) < 0)
299 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
300 _("Failed to create pipe for communicating with child process (%s)"),
308 /* The helper process writes a status report back to us, through a
309 * pipe, consisting of two ints.
312 read_helper_report (int fd
,
318 while (bytes
< sizeof(gintptr
)*2)
324 g_print ("%s:read_helper_report: read %" G_GSIZE_FORMAT
"...\n",
326 sizeof(gintptr
)*2 - bytes
);
328 chunk
= read (fd
, ((gchar
*)report
) + bytes
,
329 sizeof(gintptr
)*2 - bytes
);
333 g_print ("...got %d bytes\n", chunk
);
337 /* Some weird shit happened, bail out */
338 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
339 _("Failed to read from child pipe (%s)"),
346 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
347 _("Failed to read from child pipe (%s)"),
355 if (bytes
< sizeof(gintptr
)*2)
362 set_child_error (gintptr report
[2],
363 const gchar
*working_directory
,
368 case CHILD_CHDIR_FAILED
:
369 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_CHDIR
,
370 _("Failed to change to directory “%s” (%s)"),
372 g_strerror (report
[1]));
374 case CHILD_SPAWN_FAILED
:
375 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
376 _("Failed to execute child process (%s)"),
377 g_strerror (report
[1]));
379 case CHILD_SPAWN_NOENT
:
380 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_NOENT
,
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
);
494 g_strfreev ((gchar
**) wargv
);
495 g_strfreev ((gchar
**) wenvp
);
497 if (rc
== -1 && errsv
!= 0)
499 g_set_error (error
, G_SPAWN_ERROR
, _g_spawn_exec_err_to_g_error (errsv
),
500 _("Failed to execute child process (%s)"),
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_fds (gint
*exit_status
,
524 gboolean do_return_handle
,
525 const gchar
*working_directory
,
529 GSpawnChildSetupFunc child_setup
,
537 char **protected_argv
;
538 char args
[ARG_COUNT
][10];
544 int child_err_report_pipe
[2] = { -1, -1 };
545 int helper_sync_pipe
[2] = { -1, -1 };
546 gintptr helper_report
[2];
547 static gboolean warned_about_child_setup
= FALSE
;
548 GError
*conv_error
= NULL
;
549 gint conv_error_index
;
550 gchar
*helper_process
;
551 wchar_t *whelper
, **wargv
, **wenvp
;
552 gchar
*glib_dll_directory
;
554 if (child_setup
&& !warned_about_child_setup
)
556 warned_about_child_setup
= TRUE
;
557 g_warning ("passing a child setup function to the g_spawn functions is pointless on Windows and it is ignored");
560 argc
= protect_argv (argv
, &protected_argv
);
562 if (stdin_fd
== -1 && stdout_fd
== -1 && stderr_fd
== -1 &&
563 (flags
& G_SPAWN_CHILD_INHERITS_STDIN
) &&
564 !(flags
& G_SPAWN_STDOUT_TO_DEV_NULL
) &&
565 !(flags
& G_SPAWN_STDERR_TO_DEV_NULL
) &&
566 (working_directory
== NULL
|| !*working_directory
) &&
567 (flags
& G_SPAWN_LEAVE_DESCRIPTORS_OPEN
))
569 /* We can do without the helper process */
571 do_spawn_directly (exit_status
, do_return_handle
, flags
,
572 argv
, envp
, protected_argv
,
573 child_handle
, error
);
574 g_strfreev (protected_argv
);
578 if (!make_pipe (child_err_report_pipe
, error
))
579 goto cleanup_and_fail
;
581 if (!make_pipe (helper_sync_pipe
, error
))
582 goto cleanup_and_fail
;
584 new_argv
= g_new (char *, argc
+ 1 + ARG_COUNT
);
585 if (GetConsoleWindow () != NULL
)
586 helper_process
= HELPER_PROCESS
"-console.exe";
588 helper_process
= HELPER_PROCESS
".exe";
590 glib_dll_directory
= _glib_get_dll_directory ();
591 if (glib_dll_directory
!= NULL
)
593 helper_process
= g_build_filename (glib_dll_directory
, helper_process
, NULL
);
594 g_free (glib_dll_directory
);
597 helper_process
= g_strdup (helper_process
);
599 new_argv
[0] = protect_argv_string (helper_process
);
601 _g_sprintf (args
[ARG_CHILD_ERR_REPORT
], "%d", child_err_report_pipe
[1]);
602 new_argv
[ARG_CHILD_ERR_REPORT
] = args
[ARG_CHILD_ERR_REPORT
];
604 /* Make the read end of the child error report pipe
605 * noninherited. Otherwise it will needlessly be inherited by the
606 * helper process, and the started actual user process. As such that
607 * shouldn't harm, but it is unnecessary.
609 child_err_report_pipe
[0] = dup_noninherited (child_err_report_pipe
[0], _O_RDONLY
);
611 if (flags
& G_SPAWN_FILE_AND_ARGV_ZERO
)
613 /* Overload ARG_CHILD_ERR_REPORT to also encode the
614 * G_SPAWN_FILE_AND_ARGV_ZERO functionality.
616 strcat (args
[ARG_CHILD_ERR_REPORT
], "#");
619 _g_sprintf (args
[ARG_HELPER_SYNC
], "%d", helper_sync_pipe
[0]);
620 new_argv
[ARG_HELPER_SYNC
] = args
[ARG_HELPER_SYNC
];
622 /* Make the write end of the sync pipe noninherited. Otherwise the
623 * helper process will inherit it, and thus if this process happens
624 * to crash before writing the sync byte to the pipe, the helper
625 * process won't read but won't get any EOF either, as it has the
626 * write end open itself.
628 helper_sync_pipe
[1] = dup_noninherited (helper_sync_pipe
[1], _O_WRONLY
);
632 _g_sprintf (args
[ARG_STDIN
], "%d", stdin_fd
);
633 new_argv
[ARG_STDIN
] = args
[ARG_STDIN
];
635 else if (flags
& G_SPAWN_CHILD_INHERITS_STDIN
)
637 /* Let stdin be alone */
638 new_argv
[ARG_STDIN
] = "-";
642 /* Keep process from blocking on a read of stdin */
643 new_argv
[ARG_STDIN
] = "z";
648 _g_sprintf (args
[ARG_STDOUT
], "%d", stdout_fd
);
649 new_argv
[ARG_STDOUT
] = args
[ARG_STDOUT
];
651 else if (flags
& G_SPAWN_STDOUT_TO_DEV_NULL
)
653 new_argv
[ARG_STDOUT
] = "z";
657 new_argv
[ARG_STDOUT
] = "-";
662 _g_sprintf (args
[ARG_STDERR
], "%d", stderr_fd
);
663 new_argv
[ARG_STDERR
] = args
[ARG_STDERR
];
665 else if (flags
& G_SPAWN_STDERR_TO_DEV_NULL
)
667 new_argv
[ARG_STDERR
] = "z";
671 new_argv
[ARG_STDERR
] = "-";
674 if (working_directory
&& *working_directory
)
675 new_argv
[ARG_WORKING_DIRECTORY
] = protect_argv_string (working_directory
);
677 new_argv
[ARG_WORKING_DIRECTORY
] = g_strdup ("-");
679 if (!(flags
& G_SPAWN_LEAVE_DESCRIPTORS_OPEN
))
680 new_argv
[ARG_CLOSE_DESCRIPTORS
] = "y";
682 new_argv
[ARG_CLOSE_DESCRIPTORS
] = "-";
684 if (flags
& G_SPAWN_SEARCH_PATH
)
685 new_argv
[ARG_USE_PATH
] = "y";
687 new_argv
[ARG_USE_PATH
] = "-";
689 if (exit_status
== NULL
)
690 new_argv
[ARG_WAIT
] = "-";
692 new_argv
[ARG_WAIT
] = "w";
694 for (i
= 0; i
<= argc
; i
++)
695 new_argv
[ARG_PROGRAM
+ i
] = protected_argv
[i
];
701 g_print ("calling %s with argv:\n", helper_process
);
702 for (i
= 0; i
< argc
+ 1 + ARG_COUNT
; i
++)
703 g_print ("argv[%d]: %s\n", i
, (new_argv
[i
] ? new_argv
[i
] : "NULL"));
706 if (!utf8_charv_to_wcharv (new_argv
, &wargv
, &conv_error_index
, &conv_error
))
708 if (conv_error_index
== ARG_WORKING_DIRECTORY
)
709 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_CHDIR
,
710 _("Invalid working directory: %s"),
711 conv_error
->message
);
713 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
714 _("Invalid string in argument vector at %d: %s"),
715 conv_error_index
- ARG_PROGRAM
, conv_error
->message
);
716 g_error_free (conv_error
);
717 g_strfreev (protected_argv
);
718 g_free (new_argv
[0]);
719 g_free (new_argv
[ARG_WORKING_DIRECTORY
]);
721 g_free (helper_process
);
723 goto cleanup_and_fail
;
726 if (!utf8_charv_to_wcharv (envp
, &wenvp
, NULL
, &conv_error
))
728 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
729 _("Invalid string in environment: %s"),
730 conv_error
->message
);
731 g_error_free (conv_error
);
732 g_strfreev (protected_argv
);
733 g_free (new_argv
[0]);
734 g_free (new_argv
[ARG_WORKING_DIRECTORY
]);
736 g_free (helper_process
);
737 g_strfreev ((gchar
**) wargv
);
739 goto cleanup_and_fail
;
742 whelper
= g_utf8_to_utf16 (helper_process
, -1, NULL
, NULL
, NULL
);
743 g_free (helper_process
);
746 rc
= _wspawnvpe (P_NOWAIT
, whelper
, (const wchar_t **) wargv
, (const wchar_t **) wenvp
);
748 rc
= _wspawnvp (P_NOWAIT
, whelper
, (const wchar_t **) wargv
);
753 g_strfreev ((gchar
**) wargv
);
754 g_strfreev ((gchar
**) wenvp
);
756 /* Close the other process's ends of the pipes in this process,
757 * otherwise the reader will never get EOF.
759 close_and_invalidate (&child_err_report_pipe
[1]);
760 close_and_invalidate (&helper_sync_pipe
[0]);
762 g_strfreev (protected_argv
);
764 g_free (new_argv
[0]);
765 g_free (new_argv
[ARG_WORKING_DIRECTORY
]);
768 /* Check if gspawn-win32-helper couldn't be run */
769 if (rc
== -1 && errsv
!= 0)
771 g_set_error (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_FAILED
,
772 _("Failed to execute helper program (%s)"),
774 goto cleanup_and_fail
;
777 if (exit_status
!= NULL
)
779 /* Synchronous case. Pass helper's report pipe back to caller,
780 * which takes care of reading it after the grandchild has
783 g_assert (err_report
!= NULL
);
784 *err_report
= child_err_report_pipe
[0];
785 write (helper_sync_pipe
[1], " ", 1);
786 close_and_invalidate (&helper_sync_pipe
[1]);
790 /* Asynchronous case. We read the helper's report right away. */
791 if (!read_helper_report (child_err_report_pipe
[0], helper_report
, error
))
792 goto cleanup_and_fail
;
794 close_and_invalidate (&child_err_report_pipe
[0]);
796 switch (helper_report
[0])
799 if (child_handle
&& do_return_handle
)
801 /* rc is our HANDLE for gspawn-win32-helper. It has
802 * told us the HANDLE of its child. Duplicate that into
803 * a HANDLE valid in this process.
805 if (!DuplicateHandle ((HANDLE
) rc
, (HANDLE
) helper_report
[1],
806 GetCurrentProcess (), (LPHANDLE
) child_handle
,
807 0, TRUE
, DUPLICATE_SAME_ACCESS
))
809 char *emsg
= g_win32_error_message (GetLastError ());
810 g_print("%s\n", emsg
);
814 else if (child_handle
)
816 write (helper_sync_pipe
[1], " ", 1);
817 close_and_invalidate (&helper_sync_pipe
[1]);
821 write (helper_sync_pipe
[1], " ", 1);
822 close_and_invalidate (&helper_sync_pipe
[1]);
823 set_child_error (helper_report
, working_directory
, error
);
824 goto cleanup_and_fail
;
828 /* Success against all odds! return the information */
831 CloseHandle ((HANDLE
) rc
);
838 CloseHandle ((HANDLE
) rc
);
839 if (child_err_report_pipe
[0] != -1)
840 close (child_err_report_pipe
[0]);
841 if (child_err_report_pipe
[1] != -1)
842 close (child_err_report_pipe
[1]);
843 if (helper_sync_pipe
[0] != -1)
844 close (helper_sync_pipe
[0]);
845 if (helper_sync_pipe
[1] != -1)
846 close (helper_sync_pipe
[1]);
852 do_spawn_with_pipes (gint
*exit_status
,
853 gboolean do_return_handle
,
854 const gchar
*working_directory
,
858 GSpawnChildSetupFunc child_setup
,
860 gint
*standard_input
,
861 gint
*standard_output
,
862 gint
*standard_error
,
866 int stdin_pipe
[2] = { -1, -1 };
867 int stdout_pipe
[2] = { -1, -1 };
868 int stderr_pipe
[2] = { -1, -1 };
870 if (standard_input
&& !make_pipe (stdin_pipe
, error
))
871 goto cleanup_and_fail
;
873 if (standard_output
&& !make_pipe (stdout_pipe
, error
))
874 goto cleanup_and_fail
;
876 if (standard_error
&& !make_pipe (stderr_pipe
, error
))
877 goto cleanup_and_fail
;
879 if (!do_spawn_with_fds (exit_status
,
892 goto cleanup_and_fail
;
894 /* Close the other process's ends of the pipes in this process,
895 * otherwise the reader will never get EOF.
897 close_and_invalidate (&stdin_pipe
[0]);
898 close_and_invalidate (&stdout_pipe
[1]);
899 close_and_invalidate (&stderr_pipe
[1]);
902 *standard_input
= stdin_pipe
[1];
904 *standard_output
= stdout_pipe
[0];
906 *standard_error
= stderr_pipe
[0];
912 if (stdin_pipe
[0] != -1)
913 close (stdin_pipe
[0]);
914 if (stdin_pipe
[1] != -1)
915 close (stdin_pipe
[1]);
916 if (stdout_pipe
[0] != -1)
917 close (stdout_pipe
[0]);
918 if (stdout_pipe
[1] != -1)
919 close (stdout_pipe
[1]);
920 if (stderr_pipe
[0] != -1)
921 close (stderr_pipe
[0]);
922 if (stderr_pipe
[1] != -1)
923 close (stderr_pipe
[1]);
929 g_spawn_sync (const gchar
*working_directory
,
933 GSpawnChildSetupFunc child_setup
,
935 gchar
**standard_output
,
936 gchar
**standard_error
,
942 gint reportpipe
= -1;
943 GIOChannel
*outchannel
= NULL
;
944 GIOChannel
*errchannel
= NULL
;
945 GPollFD outfd
, errfd
;
951 GString
*outstr
= NULL
;
952 GString
*errstr
= NULL
;
956 g_return_val_if_fail (argv
!= NULL
, FALSE
);
957 g_return_val_if_fail (!(flags
& G_SPAWN_DO_NOT_REAP_CHILD
), FALSE
);
958 g_return_val_if_fail (standard_output
== NULL
||
959 !(flags
& G_SPAWN_STDOUT_TO_DEV_NULL
), FALSE
);
960 g_return_val_if_fail (standard_error
== NULL
||
961 !(flags
& G_SPAWN_STDERR_TO_DEV_NULL
), FALSE
);
963 /* Just to ensure segfaults if callers try to use
964 * these when an error is reported.
967 *standard_output
= NULL
;
970 *standard_error
= NULL
;
972 if (!do_spawn_with_pipes (&status
,
981 standard_output
? &outpipe
: NULL
,
982 standard_error
? &errpipe
: NULL
,
987 /* Read data from child. */
993 outstr
= g_string_new (NULL
);
994 outchannel
= g_io_channel_win32_new_fd (outpipe
);
995 g_io_channel_set_encoding (outchannel
, NULL
, NULL
);
996 g_io_channel_set_buffered (outchannel
, FALSE
);
997 g_io_channel_win32_make_pollfd (outchannel
,
998 G_IO_IN
| G_IO_ERR
| G_IO_HUP
,
1001 g_print ("outfd=%p\n", (HANDLE
) outfd
.fd
);
1006 errstr
= g_string_new (NULL
);
1007 errchannel
= g_io_channel_win32_new_fd (errpipe
);
1008 g_io_channel_set_encoding (errchannel
, NULL
, NULL
);
1009 g_io_channel_set_buffered (errchannel
, FALSE
);
1010 g_io_channel_win32_make_pollfd (errchannel
,
1011 G_IO_IN
| G_IO_ERR
| G_IO_HUP
,
1014 g_print ("errfd=%p\n", (HANDLE
) errfd
.fd
);
1017 /* Read data until we get EOF on all pipes. */
1018 while (!failed
&& (outpipe
>= 0 || errpipe
>= 0))
1035 g_print ("g_spawn_sync: calling g_io_channel_win32_poll, nfds=%d\n",
1038 ret
= g_io_channel_win32_poll (fds
, nfds
, -1);
1044 g_set_error_literal (error
, G_SPAWN_ERROR
, G_SPAWN_ERROR_READ
,
1045 _("Unexpected error in g_io_channel_win32_poll() reading data from a child process"));
1050 if (outpipe
>= 0 && (fds
[outindex
].revents
& G_IO_IN
))
1052 switch (read_data (outstr
, outchannel
, error
))
1056 g_print ("g_spawn_sync: outchannel: READ_FAILED\n");
1061 g_print ("g_spawn_sync: outchannel: READ_EOF\n");
1062 g_io_channel_unref (outchannel
);
1064 close_and_invalidate (&outpipe
);
1068 g_print ("g_spawn_sync: outchannel: OK\n");
1076 if (errpipe
>= 0 && (fds
[errindex
].revents
& G_IO_IN
))
1078 switch (read_data (errstr
, errchannel
, error
))
1082 g_print ("g_spawn_sync: errchannel: READ_FAILED\n");
1087 g_print ("g_spawn_sync: errchannel: READ_EOF\n");
1088 g_io_channel_unref (errchannel
);
1090 close_and_invalidate (&errpipe
);
1094 g_print ("g_spawn_sync: errchannel: OK\n");
1103 if (reportpipe
== -1)
1105 /* No helper process, exit status of actual spawned process
1106 * already available.
1109 *exit_status
= status
;
1113 /* Helper process was involved. Read its report now after the
1114 * grandchild has finished.
1116 gintptr helper_report
[2];
1118 if (!read_helper_report (reportpipe
, helper_report
, error
))
1122 switch (helper_report
[0])
1124 case CHILD_NO_ERROR
:
1126 *exit_status
= helper_report
[1];
1129 set_child_error (helper_report
, working_directory
, error
);
1134 close_and_invalidate (&reportpipe
);
1138 /* These should only be open still if we had an error. */
1140 if (outchannel
!= NULL
)
1141 g_io_channel_unref (outchannel
);
1142 if (errchannel
!= NULL
)
1143 g_io_channel_unref (errchannel
);
1145 close_and_invalidate (&outpipe
);
1147 close_and_invalidate (&errpipe
);
1152 g_string_free (outstr
, TRUE
);
1154 g_string_free (errstr
, TRUE
);
1160 if (standard_output
)
1161 *standard_output
= g_string_free (outstr
, FALSE
);
1164 *standard_error
= g_string_free (errstr
, FALSE
);
1171 g_spawn_async_with_pipes (const gchar
*working_directory
,
1175 GSpawnChildSetupFunc child_setup
,
1178 gint
*standard_input
,
1179 gint
*standard_output
,
1180 gint
*standard_error
,
1183 g_return_val_if_fail (argv
!= NULL
, FALSE
);
1184 g_return_val_if_fail (standard_output
== NULL
||
1185 !(flags
& G_SPAWN_STDOUT_TO_DEV_NULL
), FALSE
);
1186 g_return_val_if_fail (standard_error
== NULL
||
1187 !(flags
& G_SPAWN_STDERR_TO_DEV_NULL
), FALSE
);
1188 /* can't inherit stdin if we have an input pipe. */
1189 g_return_val_if_fail (standard_input
== NULL
||
1190 !(flags
& G_SPAWN_CHILD_INHERITS_STDIN
), FALSE
);
1192 return do_spawn_with_pipes (NULL
,
1193 (flags
& G_SPAWN_DO_NOT_REAP_CHILD
),
1208 g_spawn_async_with_fds (const gchar
*working_directory
,
1212 GSpawnChildSetupFunc child_setup
,
1220 g_return_val_if_fail (argv
!= NULL
, FALSE
);
1221 g_return_val_if_fail (stdin_fd
== -1 ||
1222 !(flags
& G_SPAWN_STDOUT_TO_DEV_NULL
), FALSE
);
1223 g_return_val_if_fail (stderr_fd
== -1 ||
1224 !(flags
& G_SPAWN_STDERR_TO_DEV_NULL
), FALSE
);
1225 /* can't inherit stdin if we have an input pipe. */
1226 g_return_val_if_fail (stdin_fd
== -1 ||
1227 !(flags
& G_SPAWN_CHILD_INHERITS_STDIN
), FALSE
);
1229 return do_spawn_with_fds (NULL
,
1230 (flags
& G_SPAWN_DO_NOT_REAP_CHILD
),
1245 g_spawn_command_line_sync (const gchar
*command_line
,
1246 gchar
**standard_output
,
1247 gchar
**standard_error
,
1254 g_return_val_if_fail (command_line
!= NULL
, FALSE
);
1256 if (!g_shell_parse_argv (command_line
,
1261 retval
= g_spawn_sync (NULL
,
1264 G_SPAWN_SEARCH_PATH
,
1277 g_spawn_command_line_async (const gchar
*command_line
,
1283 g_return_val_if_fail (command_line
!= NULL
, FALSE
);
1285 if (!g_shell_parse_argv (command_line
,
1290 retval
= g_spawn_async (NULL
,
1293 G_SPAWN_SEARCH_PATH
,
1304 g_spawn_close_pid (GPid pid
)
1310 g_spawn_check_exit_status (gint exit_status
,
1313 gboolean ret
= FALSE
;
1315 if (exit_status
!= 0)
1317 g_set_error (error
, G_SPAWN_EXIT_ERROR
, exit_status
,
1318 _("Child process exited with code %ld"),
1319 (long) exit_status
);
1330 /* Binary compatibility versions. Not for newly compiled code. */
1332 _GLIB_EXTERN gboolean
g_spawn_async_utf8 (const gchar
*working_directory
,
1336 GSpawnChildSetupFunc child_setup
,
1340 _GLIB_EXTERN gboolean
g_spawn_async_with_pipes_utf8 (const gchar
*working_directory
,
1344 GSpawnChildSetupFunc child_setup
,
1347 gint
*standard_input
,
1348 gint
*standard_output
,
1349 gint
*standard_error
,
1351 _GLIB_EXTERN gboolean
g_spawn_sync_utf8 (const gchar
*working_directory
,
1355 GSpawnChildSetupFunc child_setup
,
1357 gchar
**standard_output
,
1358 gchar
**standard_error
,
1361 _GLIB_EXTERN gboolean
g_spawn_command_line_sync_utf8 (const gchar
*command_line
,
1362 gchar
**standard_output
,
1363 gchar
**standard_error
,
1366 _GLIB_EXTERN gboolean
g_spawn_command_line_async_utf8 (const gchar
*command_line
,
1370 g_spawn_async_utf8 (const gchar
*working_directory
,
1374 GSpawnChildSetupFunc child_setup
,
1379 return g_spawn_async (working_directory
,
1390 g_spawn_async_with_pipes_utf8 (const gchar
*working_directory
,
1394 GSpawnChildSetupFunc child_setup
,
1397 gint
*standard_input
,
1398 gint
*standard_output
,
1399 gint
*standard_error
,
1402 return g_spawn_async_with_pipes (working_directory
,
1416 g_spawn_sync_utf8 (const gchar
*working_directory
,
1420 GSpawnChildSetupFunc child_setup
,
1422 gchar
**standard_output
,
1423 gchar
**standard_error
,
1427 return g_spawn_sync (working_directory
,
1440 g_spawn_command_line_sync_utf8 (const gchar
*command_line
,
1441 gchar
**standard_output
,
1442 gchar
**standard_error
,
1446 return g_spawn_command_line_sync (command_line
,
1454 g_spawn_command_line_async_utf8 (const gchar
*command_line
,
1457 return g_spawn_command_line_async (command_line
, error
);
1460 #endif /* G_OS_WIN32 */
1462 #endif /* !GSPAWN_HELPER */