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 */
57 #ifdef G_SPAWN_WIN32_DEBUG
59 #define SETUP_DEBUG() /* empty */
62 static int debug = -1;
63 #define SETUP_DEBUG() \
68 if (getenv ("G_SPAWN_WIN32_DEBUG") != NULL) \
85 ARG_CHILD_ERR_REPORT = 1,
89 ARG_WORKING_DIRECTORY,
90 ARG_CLOSE_DESCRIPTORS,
94 ARG_COUNT = ARG_PROGRAM
99 static gboolean make_pipe (gint p[2],
101 static gboolean do_spawn_with_pipes (gboolean dont_wait,
102 const gchar *working_directory,
105 gboolean close_descriptors,
106 gboolean search_path,
107 gboolean stdout_to_null,
108 gboolean stderr_to_null,
109 gboolean child_inherits_stdin,
110 GSpawnChildSetupFunc child_setup,
113 gint *standard_input,
114 gint *standard_output,
115 gint *standard_error,
120 g_spawn_error_quark (void)
122 static GQuark quark = 0;
124 quark = g_quark_from_static_string ("g-exec-error-quark");
129 g_spawn_async (const gchar *working_directory,
133 GSpawnChildSetupFunc child_setup,
138 g_return_val_if_fail (argv != NULL, FALSE);
140 return g_spawn_async_with_pipes (working_directory,
150 /* Avoids a danger in threaded situations (calling close()
151 * on a file descriptor twice, and another thread has
152 * re-opened it since the first close)
155 close_and_invalidate (gint *fd)
172 READ_FAILED = 0, /* FALSE */
178 read_data (GString *str,
179 GIOChannel *iochannel,
188 giostatus = g_io_channel_read_chars (iochannel, buf, sizeof (buf), &bytes, NULL);
194 g_string_append_len (str, buf, bytes);
197 else if (giostatus == G_IO_STATUS_AGAIN)
199 else if (giostatus == G_IO_STATUS_ERROR)
204 _("Failed to read data from child process"));
213 g_spawn_sync (const gchar *working_directory,
217 GSpawnChildSetupFunc child_setup,
219 gchar **standard_output,
220 gchar **standard_error,
227 GIOChannel *outchannel = NULL;
228 GIOChannel *errchannel = NULL;
229 GPollFD outfd, errfd;
235 GString *outstr = NULL;
236 GString *errstr = NULL;
240 g_return_val_if_fail (argv != NULL, FALSE);
241 g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
242 g_return_val_if_fail (standard_output == NULL ||
243 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
244 g_return_val_if_fail (standard_error == NULL ||
245 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
247 /* Just to ensure segfaults if callers try to use
248 * these when an error is reported.
251 *standard_output = NULL;
254 *standard_error = NULL;
256 if (!do_spawn_with_pipes (FALSE,
260 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
261 (flags & G_SPAWN_SEARCH_PATH) != 0,
262 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
263 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
264 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
269 standard_output ? &outpipe : NULL,
270 standard_error ? &errpipe : NULL,
275 /* Read data from child. */
281 outstr = g_string_new ("");
282 outchannel = g_io_channel_win32_new_fd (outpipe);
283 g_io_channel_set_encoding (outchannel, NULL, NULL);
284 g_io_channel_win32_make_pollfd (outchannel,
285 G_IO_IN | G_IO_ERR | G_IO_HUP,
291 errstr = g_string_new ("");
292 errchannel = g_io_channel_win32_new_fd (errpipe);
293 g_io_channel_set_encoding (errchannel, NULL, NULL);
294 g_io_channel_win32_make_pollfd (errchannel,
295 G_IO_IN | G_IO_ERR | G_IO_HUP,
299 /* Read data until we get EOF on both pipes. */
319 g_print ("%s:g_spawn_sync: calling g_io_channel_win32_poll, nfds=%d\n",
322 ret = g_io_channel_win32_poll (fds, nfds, -1);
331 _("Unexpected error in g_io_channel_win32_poll() reading data from a child process"));
336 if (outpipe >= 0 && (fds[outindex].revents & G_IO_IN))
338 switch (read_data (outstr, outchannel, error))
342 g_print ("g_spawn_sync: outchannel: READ_FAILED\n");
347 g_print ("g_spawn_sync: outchannel: READ_EOF\n");
348 g_io_channel_unref (outchannel);
350 close_and_invalidate (&outpipe);
354 g_print ("g_spawn_sync: outchannel: OK\n");
362 if (errpipe >= 0 && (fds[errindex].revents & G_IO_IN))
364 switch (read_data (errstr, errchannel, error))
368 g_print ("g_spawn_sync: errchannel: READ_FAILED\n");
373 g_print ("g_spawn_sync: errchannel: READ_EOF\n");
374 g_io_channel_unref (errchannel);
376 close_and_invalidate (&errpipe);
380 g_print ("g_spawn_sync: errchannel: OK\n");
389 /* These should only be open still if we had an error. */
391 if (outchannel != NULL)
392 g_io_channel_unref (outchannel);
393 if (errchannel != NULL)
394 g_io_channel_unref (errchannel);
396 close_and_invalidate (&outpipe);
398 close_and_invalidate (&errpipe);
403 g_string_free (outstr, TRUE);
405 g_string_free (errstr, TRUE);
412 *exit_status = status;
415 *standard_output = g_string_free (outstr, FALSE);
418 *standard_error = g_string_free (errstr, FALSE);
425 g_spawn_async_with_pipes (const gchar *working_directory,
429 GSpawnChildSetupFunc child_setup,
432 gint *standard_input,
433 gint *standard_output,
434 gint *standard_error,
437 g_return_val_if_fail (argv != NULL, FALSE);
438 g_return_val_if_fail (standard_output == NULL ||
439 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
440 g_return_val_if_fail (standard_error == NULL ||
441 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
442 /* can't inherit stdin if we have an input pipe. */
443 g_return_val_if_fail (standard_input == NULL ||
444 !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
446 return do_spawn_with_pipes (TRUE,
450 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
451 (flags & G_SPAWN_SEARCH_PATH) != 0,
452 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
453 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
454 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
466 g_spawn_command_line_sync (const gchar *command_line,
467 gchar **standard_output,
468 gchar **standard_error,
475 g_return_val_if_fail (command_line != NULL, FALSE);
477 if (!g_shell_parse_argv (command_line,
482 retval = g_spawn_sync (NULL,
498 g_spawn_command_line_async (const gchar *command_line,
504 g_return_val_if_fail (command_line != NULL, FALSE);
506 if (!g_shell_parse_argv (command_line,
511 retval = g_spawn_async (NULL,
525 do_spawn (gboolean dont_wait,
526 gint child_err_report_fd,
530 const gchar *working_directory,
533 gboolean close_descriptors,
534 gboolean search_path,
535 gboolean stdout_to_null,
536 gboolean stderr_to_null,
537 gboolean child_inherits_stdin,
538 GSpawnChildSetupFunc child_setup,
542 gchar args[ARG_COUNT][10];
552 new_argv = g_new (gchar *, argc + 1 + ARG_COUNT);
554 new_argv[0] = "gspawn-win32-helper";
555 sprintf (args[ARG_CHILD_ERR_REPORT], "%d", child_err_report_fd);
556 new_argv[ARG_CHILD_ERR_REPORT] = args[ARG_CHILD_ERR_REPORT];
560 sprintf (args[ARG_STDIN], "%d", stdin_fd);
561 new_argv[ARG_STDIN] = args[ARG_STDIN];
563 else if (child_inherits_stdin)
565 /* Let stdin be alone */
566 new_argv[ARG_STDIN] = "-";
570 /* Keep process from blocking on a read of stdin */
571 new_argv[ARG_STDIN] = "z";
576 sprintf (args[ARG_STDOUT], "%d", stdout_fd);
577 new_argv[ARG_STDOUT] = args[ARG_STDOUT];
579 else if (stdout_to_null)
581 new_argv[ARG_STDOUT] = "z";
585 new_argv[ARG_STDOUT] = "-";
590 sprintf (args[ARG_STDERR], "%d", stderr_fd);
591 new_argv[ARG_STDERR] = args[ARG_STDERR];
593 else if (stderr_to_null)
595 new_argv[ARG_STDERR] = "z";
599 new_argv[ARG_STDERR] = "-";
602 if (working_directory && *working_directory)
603 new_argv[ARG_WORKING_DIRECTORY] = g_strdup (working_directory);
605 new_argv[ARG_WORKING_DIRECTORY] = "-";
607 if (close_descriptors)
608 new_argv[ARG_CLOSE_DESCRIPTORS] = "y";
610 new_argv[ARG_CLOSE_DESCRIPTORS] = "-";
613 new_argv[ARG_USE_PATH] = "y";
615 new_argv[ARG_USE_PATH] = "-";
618 new_argv[ARG_WAIT] = "-";
620 new_argv[ARG_WAIT] = "w";
622 for (i = 0; i <= argc; i++)
623 new_argv[ARG_PROGRAM + i] = argv[i];
625 /* Call user function just before we execute the helper program,
626 * which executes the program. Dunno what's the usefulness of this.
627 * A child setup function used on Unix probably isn't of much use
628 * as such on Win32, anyhow
632 (* child_setup) (user_data);
637 g_print ("calling gspawn-win32-helper with argv:\n");
638 for (i = 0; i < argc + 1 + ARG_COUNT; i++)
639 g_print ("argv[%d]: %s\n", i, (new_argv[i] ? new_argv[i] : "NULL"));
643 /* Let's hope envp hasn't mucked with PATH so that
644 * gspawn-win32-helper.exe isn't found.
646 rc = spawnvpe (P_NOWAIT, "gspawn-win32-helper", new_argv, envp);
648 rc = spawnvp (P_NOWAIT, "gspawn-win32-helper", new_argv);
650 /* Close the child_err_report_fd and the other process's ends of the
651 * pipes in this process, otherwise the reader will never get
654 close (child_err_report_fd);
662 g_free (new_argv[ARG_WORKING_DIRECTORY]);
677 while (bytes < sizeof(gint)*n_ints_in_buf)
682 g_print ("%s:read_ints: trying to read %d bytes from pipe...\n",
684 sizeof(gint)*n_ints_in_buf - bytes);
686 chunk = read (fd, ((gchar*)buf) + bytes,
687 sizeof(gint)*n_ints_in_buf - bytes);
690 g_print ("... got %d bytes\n", chunk);
694 /* Some weird shit happened, bail out */
698 G_SPAWN_ERROR_FAILED,
699 _("Failed to read from child pipe (%s)"),
710 *n_ints_read = bytes/sizeof(gint);
716 do_spawn_with_pipes (gboolean dont_wait,
717 const gchar *working_directory,
720 gboolean close_descriptors,
721 gboolean search_path,
722 gboolean stdout_to_null,
723 gboolean stderr_to_null,
724 gboolean child_inherits_stdin,
725 GSpawnChildSetupFunc child_setup,
728 gint *standard_input,
729 gint *standard_output,
730 gint *standard_error,
734 gint stdin_pipe[2] = { -1, -1 };
735 gint stdout_pipe[2] = { -1, -1 };
736 gint stderr_pipe[2] = { -1, -1 };
737 gint child_err_report_pipe[2] = { -1, -1 };
742 if (!make_pipe (child_err_report_pipe, error))
745 if (standard_input && !make_pipe (stdin_pipe, error))
746 goto cleanup_and_fail;
748 if (standard_output && !make_pipe (stdout_pipe, error))
749 goto cleanup_and_fail;
751 if (standard_error && !make_pipe (stderr_pipe, error))
752 goto cleanup_and_fail;
754 helper = do_spawn (dont_wait,
755 child_err_report_pipe[1],
766 child_inherits_stdin,
770 /* do_spawn() returns -1 if gspawn-win32-helper couldn't be run */
775 G_SPAWN_ERROR_FAILED,
776 _("Failed to execute helper program"));
777 goto cleanup_and_fail;
780 if (!read_ints (child_err_report_pipe[0],
784 goto cleanup_and_fail;
786 /* Error code from gspawn-win32-helper. */
790 if (child_pid && dont_wait)
792 /* helper is our HANDLE for gspawn-win32-helper. It has
793 * told us the HANDLE of its child. Duplicate that into
794 * a HANDLE valid in this process.
796 if (!DuplicateHandle ((HANDLE) helper, (HANDLE) buf[1],
797 GetCurrentProcess (), (LPHANDLE) child_pid,
798 0, TRUE, DUPLICATE_SAME_ACCESS))
805 case CHILD_CHDIR_FAILED:
809 _("Failed to change to directory '%s' (%s)"),
811 g_strerror (buf[1]));
812 goto cleanup_and_fail;
814 case CHILD_SPAWN_FAILED:
817 G_SPAWN_ERROR_FAILED,
818 _("Failed to execute child process (%s)"),
819 g_strerror (buf[1]));
820 goto cleanup_and_fail;
823 /* Success against all odds! return the information */
826 *standard_input = stdin_pipe[1];
828 *standard_output = stdout_pipe[0];
830 *standard_error = stderr_pipe[0];
832 *exit_status = buf[1];
838 CloseHandle ((HANDLE) helper);
839 close_and_invalidate (&child_err_report_pipe[0]);
840 close_and_invalidate (&child_err_report_pipe[1]);
841 close_and_invalidate (&stdin_pipe[0]);
842 close_and_invalidate (&stdin_pipe[1]);
843 close_and_invalidate (&stdout_pipe[0]);
844 close_and_invalidate (&stdout_pipe[1]);
845 close_and_invalidate (&stderr_pipe[0]);
846 close_and_invalidate (&stderr_pipe[1]);
852 make_pipe (gint p[2],
859 G_SPAWN_ERROR_FAILED,
860 _("Failed to create pipe for communicating with child process (%s)"),
868 #endif /* !GSPAWN_HELPER */