1 /* gspawn-win32.c - Process launching on Win32
3 * Copyright 2000 Red Hat, Inc.
5 * GLib is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * GLib is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with GLib; see the file COPYING.LIB. If not, write
17 * to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 * Implementation details on Win32.
24 * - There is no way to set the no-inherit flag for
25 * a "file descriptor" in the MS C runtime. The flag is there,
26 * and the dospawn() function uses it, but unfortunately
27 * this flag can only be set when opening the file.
28 * - As there is no fork(), we cannot reliably change directory
29 * before starting the child process. (There might be several threads
30 * running, and the current directory is common for all threads.)
32 * Thus, we must in most cases use a helper program to handle closing
33 * of (inherited) file descriptors and changing of directory. In fact,
34 * we do it all the time.
37 /* Define this to get some logging all the time */
38 /* #define G_SPAWN_WIN32_DEBUG */
43 #include "gprintfint.h"
58 #ifdef G_SPAWN_WIN32_DEBUG
60 #define SETUP_DEBUG() /* empty */
63 static int debug = -1;
64 #define SETUP_DEBUG() \
69 if (getenv ("G_SPAWN_WIN32_DEBUG") != NULL) \
86 ARG_CHILD_ERR_REPORT = 1,
90 ARG_WORKING_DIRECTORY,
91 ARG_CLOSE_DESCRIPTORS,
95 ARG_COUNT = ARG_PROGRAM
100 static gboolean make_pipe (gint p[2],
102 static gboolean do_spawn_with_pipes (gboolean dont_wait,
103 gboolean dont_return_handle,
104 const gchar *working_directory,
107 gboolean close_descriptors,
108 gboolean search_path,
109 gboolean stdout_to_null,
110 gboolean stderr_to_null,
111 gboolean child_inherits_stdin,
112 GSpawnChildSetupFunc child_setup,
115 gint *standard_input,
116 gint *standard_output,
117 gint *standard_error,
122 g_spawn_error_quark (void)
124 static GQuark quark = 0;
126 quark = g_quark_from_static_string ("g-exec-error-quark");
131 g_spawn_async (const gchar *working_directory,
135 GSpawnChildSetupFunc child_setup,
140 g_return_val_if_fail (argv != NULL, FALSE);
142 return g_spawn_async_with_pipes (working_directory,
152 /* Avoids a danger in threaded situations (calling close()
153 * on a file descriptor twice, and another thread has
154 * re-opened it since the first close)
157 close_and_invalidate (gint *fd)
174 READ_FAILED = 0, /* FALSE */
180 read_data (GString *str,
181 GIOChannel *iochannel,
190 giostatus = g_io_channel_read_chars (iochannel, buf, sizeof (buf), &bytes, NULL);
196 g_string_append_len (str, buf, bytes);
199 else if (giostatus == G_IO_STATUS_AGAIN)
201 else if (giostatus == G_IO_STATUS_ERROR)
206 _("Failed to read data from child process"));
215 g_spawn_sync (const gchar *working_directory,
219 GSpawnChildSetupFunc child_setup,
221 gchar **standard_output,
222 gchar **standard_error,
229 GIOChannel *outchannel = NULL;
230 GIOChannel *errchannel = NULL;
231 GPollFD outfd, errfd;
237 GString *outstr = NULL;
238 GString *errstr = NULL;
242 g_return_val_if_fail (argv != NULL, FALSE);
243 g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
244 g_return_val_if_fail (standard_output == NULL ||
245 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
246 g_return_val_if_fail (standard_error == NULL ||
247 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
249 /* Just to ensure segfaults if callers try to use
250 * these when an error is reported.
253 *standard_output = NULL;
256 *standard_error = NULL;
258 if (!do_spawn_with_pipes (FALSE,
263 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
264 (flags & G_SPAWN_SEARCH_PATH) != 0,
265 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
266 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
267 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
272 standard_output ? &outpipe : NULL,
273 standard_error ? &errpipe : NULL,
278 /* Read data from child. */
284 outstr = g_string_new ("");
285 outchannel = g_io_channel_win32_new_fd (outpipe);
286 g_io_channel_set_encoding (outchannel, NULL, NULL);
287 g_io_channel_win32_make_pollfd (outchannel,
288 G_IO_IN | G_IO_ERR | G_IO_HUP,
294 errstr = g_string_new ("");
295 errchannel = g_io_channel_win32_new_fd (errpipe);
296 g_io_channel_set_encoding (errchannel, NULL, NULL);
297 g_io_channel_win32_make_pollfd (errchannel,
298 G_IO_IN | G_IO_ERR | G_IO_HUP,
302 /* Read data until we get EOF on both pipes. */
322 g_print ("%s:g_spawn_sync: calling g_io_channel_win32_poll, nfds=%d\n",
325 ret = g_io_channel_win32_poll (fds, nfds, -1);
334 _("Unexpected error in g_io_channel_win32_poll() reading data from a child process"));
339 if (outpipe >= 0 && (fds[outindex].revents & G_IO_IN))
341 switch (read_data (outstr, outchannel, error))
345 g_print ("g_spawn_sync: outchannel: READ_FAILED\n");
350 g_print ("g_spawn_sync: outchannel: READ_EOF\n");
351 g_io_channel_unref (outchannel);
353 close_and_invalidate (&outpipe);
357 g_print ("g_spawn_sync: outchannel: OK\n");
365 if (errpipe >= 0 && (fds[errindex].revents & G_IO_IN))
367 switch (read_data (errstr, errchannel, error))
371 g_print ("g_spawn_sync: errchannel: READ_FAILED\n");
376 g_print ("g_spawn_sync: errchannel: READ_EOF\n");
377 g_io_channel_unref (errchannel);
379 close_and_invalidate (&errpipe);
383 g_print ("g_spawn_sync: errchannel: OK\n");
392 /* These should only be open still if we had an error. */
394 if (outchannel != NULL)
395 g_io_channel_unref (outchannel);
396 if (errchannel != NULL)
397 g_io_channel_unref (errchannel);
399 close_and_invalidate (&outpipe);
401 close_and_invalidate (&errpipe);
406 g_string_free (outstr, TRUE);
408 g_string_free (errstr, TRUE);
415 *exit_status = status;
418 *standard_output = g_string_free (outstr, FALSE);
421 *standard_error = g_string_free (errstr, FALSE);
428 g_spawn_async_with_pipes (const gchar *working_directory,
432 GSpawnChildSetupFunc child_setup,
435 gint *standard_input,
436 gint *standard_output,
437 gint *standard_error,
440 g_return_val_if_fail (argv != NULL, FALSE);
441 g_return_val_if_fail (standard_output == NULL ||
442 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
443 g_return_val_if_fail (standard_error == NULL ||
444 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
445 /* can't inherit stdin if we have an input pipe. */
446 g_return_val_if_fail (standard_input == NULL ||
447 !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
449 return do_spawn_with_pipes (TRUE,
450 !(flags & G_SPAWN_DO_NOT_REAP_CHILD),
454 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
455 (flags & G_SPAWN_SEARCH_PATH) != 0,
456 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
457 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
458 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
470 g_spawn_command_line_sync (const gchar *command_line,
471 gchar **standard_output,
472 gchar **standard_error,
479 g_return_val_if_fail (command_line != NULL, FALSE);
481 if (!g_shell_parse_argv (command_line,
486 retval = g_spawn_sync (NULL,
502 g_spawn_command_line_async (const gchar *command_line,
508 g_return_val_if_fail (command_line != NULL, FALSE);
510 if (!g_shell_parse_argv (command_line,
515 retval = g_spawn_async (NULL,
529 do_spawn (gboolean dont_wait,
530 gint child_err_report_fd,
534 const gchar *working_directory,
537 gboolean close_descriptors,
538 gboolean search_path,
539 gboolean stdout_to_null,
540 gboolean stderr_to_null,
541 gboolean child_inherits_stdin,
542 GSpawnChildSetupFunc child_setup,
546 gchar args[ARG_COUNT][10];
556 new_argv = g_new (gchar *, argc + 1 + ARG_COUNT);
558 new_argv[0] = "gspawn-win32-helper";
559 _g_sprintf (args[ARG_CHILD_ERR_REPORT], "%d", child_err_report_fd);
560 new_argv[ARG_CHILD_ERR_REPORT] = args[ARG_CHILD_ERR_REPORT];
564 _g_sprintf (args[ARG_STDIN], "%d", stdin_fd);
565 new_argv[ARG_STDIN] = args[ARG_STDIN];
567 else if (child_inherits_stdin)
569 /* Let stdin be alone */
570 new_argv[ARG_STDIN] = "-";
574 /* Keep process from blocking on a read of stdin */
575 new_argv[ARG_STDIN] = "z";
580 _g_sprintf (args[ARG_STDOUT], "%d", stdout_fd);
581 new_argv[ARG_STDOUT] = args[ARG_STDOUT];
583 else if (stdout_to_null)
585 new_argv[ARG_STDOUT] = "z";
589 new_argv[ARG_STDOUT] = "-";
594 _g_sprintf (args[ARG_STDERR], "%d", stderr_fd);
595 new_argv[ARG_STDERR] = args[ARG_STDERR];
597 else if (stderr_to_null)
599 new_argv[ARG_STDERR] = "z";
603 new_argv[ARG_STDERR] = "-";
606 if (working_directory && *working_directory)
607 new_argv[ARG_WORKING_DIRECTORY] = g_strdup (working_directory);
609 new_argv[ARG_WORKING_DIRECTORY] = "-";
611 if (close_descriptors)
612 new_argv[ARG_CLOSE_DESCRIPTORS] = "y";
614 new_argv[ARG_CLOSE_DESCRIPTORS] = "-";
617 new_argv[ARG_USE_PATH] = "y";
619 new_argv[ARG_USE_PATH] = "-";
622 new_argv[ARG_WAIT] = "-";
624 new_argv[ARG_WAIT] = "w";
626 for (i = 0; i <= argc; i++)
627 new_argv[ARG_PROGRAM + i] = argv[i];
629 /* Call user function just before we execute the helper program,
630 * which executes the program. Dunno what's the usefulness of this.
631 * A child setup function used on Unix probably isn't of much use
632 * as such on Win32, anyhow
636 (* child_setup) (user_data);
641 g_print ("calling gspawn-win32-helper with argv:\n");
642 for (i = 0; i < argc + 1 + ARG_COUNT; i++)
643 g_print ("argv[%d]: %s\n", i, (new_argv[i] ? new_argv[i] : "NULL"));
647 /* Let's hope envp hasn't mucked with PATH so that
648 * gspawn-win32-helper.exe isn't found.
650 rc = spawnvpe (P_NOWAIT, "gspawn-win32-helper", new_argv, envp);
652 rc = spawnvp (P_NOWAIT, "gspawn-win32-helper", new_argv);
654 /* Close the child_err_report_fd and the other process's ends of the
655 * pipes in this process, otherwise the reader will never get
658 close (child_err_report_fd);
666 g_free (new_argv[ARG_WORKING_DIRECTORY]);
681 while (bytes < sizeof(gint)*n_ints_in_buf)
686 g_print ("%s:read_ints: trying to read %d bytes from pipe...\n",
688 sizeof(gint)*n_ints_in_buf - bytes);
690 chunk = read (fd, ((gchar*)buf) + bytes,
691 sizeof(gint)*n_ints_in_buf - bytes);
694 g_print ("... got %d bytes\n", chunk);
698 /* Some weird shit happened, bail out */
702 G_SPAWN_ERROR_FAILED,
703 _("Failed to read from child pipe (%s)"),
714 *n_ints_read = bytes/sizeof(gint);
720 do_spawn_with_pipes (gboolean dont_wait,
721 gboolean dont_return_handle,
722 const gchar *working_directory,
725 gboolean close_descriptors,
726 gboolean search_path,
727 gboolean stdout_to_null,
728 gboolean stderr_to_null,
729 gboolean child_inherits_stdin,
730 GSpawnChildSetupFunc child_setup,
733 gint *standard_input,
734 gint *standard_output,
735 gint *standard_error,
739 gint stdin_pipe[2] = { -1, -1 };
740 gint stdout_pipe[2] = { -1, -1 };
741 gint stderr_pipe[2] = { -1, -1 };
742 gint child_err_report_pipe[2] = { -1, -1 };
747 if (!make_pipe (child_err_report_pipe, error))
750 if (standard_input && !make_pipe (stdin_pipe, error))
751 goto cleanup_and_fail;
753 if (standard_output && !make_pipe (stdout_pipe, error))
754 goto cleanup_and_fail;
756 if (standard_error && !make_pipe (stderr_pipe, error))
757 goto cleanup_and_fail;
759 helper = do_spawn (dont_wait,
760 child_err_report_pipe[1],
771 child_inherits_stdin,
775 /* do_spawn() returns -1 if gspawn-win32-helper couldn't be run */
780 G_SPAWN_ERROR_FAILED,
781 _("Failed to execute helper program"));
782 goto cleanup_and_fail;
785 if (!read_ints (child_err_report_pipe[0],
789 goto cleanup_and_fail;
791 /* Error code from gspawn-win32-helper. */
795 if (child_pid && dont_wait && !dont_return_handle)
797 /* helper is our HANDLE for gspawn-win32-helper. It has
798 * told us the HANDLE of its child. Duplicate that into
799 * a HANDLE valid in this process.
801 if (!DuplicateHandle ((HANDLE) helper, (HANDLE) buf[1],
802 GetCurrentProcess (), (LPHANDLE) child_pid,
803 0, TRUE, DUPLICATE_SAME_ACCESS))
810 case CHILD_CHDIR_FAILED:
814 _("Failed to change to directory '%s' (%s)"),
816 g_strerror (buf[1]));
817 goto cleanup_and_fail;
819 case CHILD_SPAWN_FAILED:
822 G_SPAWN_ERROR_FAILED,
823 _("Failed to execute child process (%s)"),
824 g_strerror (buf[1]));
825 goto cleanup_and_fail;
828 /* Success against all odds! return the information */
831 *standard_input = stdin_pipe[1];
833 *standard_output = stdout_pipe[0];
835 *standard_error = stderr_pipe[0];
837 *exit_status = buf[1];
838 CloseHandle ((HANDLE) helper);
844 CloseHandle ((HANDLE) helper);
845 close_and_invalidate (&child_err_report_pipe[0]);
846 close_and_invalidate (&child_err_report_pipe[1]);
847 close_and_invalidate (&stdin_pipe[0]);
848 close_and_invalidate (&stdin_pipe[1]);
849 close_and_invalidate (&stdout_pipe[0]);
850 close_and_invalidate (&stdout_pipe[1]);
851 close_and_invalidate (&stderr_pipe[0]);
852 close_and_invalidate (&stderr_pipe[1]);
858 make_pipe (gint p[2],
865 G_SPAWN_ERROR_FAILED,
866 _("Failed to create pipe for communicating with child process (%s)"),
874 #endif /* !GSPAWN_HELPER */