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 gboolean dont_return_handle,
103 const gchar *working_directory,
106 gboolean close_descriptors,
107 gboolean search_path,
108 gboolean stdout_to_null,
109 gboolean stderr_to_null,
110 gboolean child_inherits_stdin,
111 GSpawnChildSetupFunc child_setup,
114 gint *standard_input,
115 gint *standard_output,
116 gint *standard_error,
121 g_spawn_error_quark (void)
123 static GQuark quark = 0;
125 quark = g_quark_from_static_string ("g-exec-error-quark");
130 g_spawn_async (const gchar *working_directory,
134 GSpawnChildSetupFunc child_setup,
139 g_return_val_if_fail (argv != NULL, FALSE);
141 return g_spawn_async_with_pipes (working_directory,
151 /* Avoids a danger in threaded situations (calling close()
152 * on a file descriptor twice, and another thread has
153 * re-opened it since the first close)
156 close_and_invalidate (gint *fd)
173 READ_FAILED = 0, /* FALSE */
179 read_data (GString *str,
180 GIOChannel *iochannel,
189 giostatus = g_io_channel_read_chars (iochannel, buf, sizeof (buf), &bytes, NULL);
195 g_string_append_len (str, buf, bytes);
198 else if (giostatus == G_IO_STATUS_AGAIN)
200 else if (giostatus == G_IO_STATUS_ERROR)
205 _("Failed to read data from child process"));
214 g_spawn_sync (const gchar *working_directory,
218 GSpawnChildSetupFunc child_setup,
220 gchar **standard_output,
221 gchar **standard_error,
228 GIOChannel *outchannel = NULL;
229 GIOChannel *errchannel = NULL;
230 GPollFD outfd, errfd;
236 GString *outstr = NULL;
237 GString *errstr = NULL;
241 g_return_val_if_fail (argv != NULL, FALSE);
242 g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
243 g_return_val_if_fail (standard_output == NULL ||
244 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
245 g_return_val_if_fail (standard_error == NULL ||
246 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
248 /* Just to ensure segfaults if callers try to use
249 * these when an error is reported.
252 *standard_output = NULL;
255 *standard_error = NULL;
257 if (!do_spawn_with_pipes (FALSE,
262 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
263 (flags & G_SPAWN_SEARCH_PATH) != 0,
264 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
265 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
266 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
271 standard_output ? &outpipe : NULL,
272 standard_error ? &errpipe : NULL,
277 /* Read data from child. */
283 outstr = g_string_new ("");
284 outchannel = g_io_channel_win32_new_fd (outpipe);
285 g_io_channel_set_encoding (outchannel, NULL, NULL);
286 g_io_channel_win32_make_pollfd (outchannel,
287 G_IO_IN | G_IO_ERR | G_IO_HUP,
293 errstr = g_string_new ("");
294 errchannel = g_io_channel_win32_new_fd (errpipe);
295 g_io_channel_set_encoding (errchannel, NULL, NULL);
296 g_io_channel_win32_make_pollfd (errchannel,
297 G_IO_IN | G_IO_ERR | G_IO_HUP,
301 /* Read data until we get EOF on both pipes. */
321 g_print ("%s:g_spawn_sync: calling g_io_channel_win32_poll, nfds=%d\n",
324 ret = g_io_channel_win32_poll (fds, nfds, -1);
333 _("Unexpected error in g_io_channel_win32_poll() reading data from a child process"));
338 if (outpipe >= 0 && (fds[outindex].revents & G_IO_IN))
340 switch (read_data (outstr, outchannel, error))
344 g_print ("g_spawn_sync: outchannel: READ_FAILED\n");
349 g_print ("g_spawn_sync: outchannel: READ_EOF\n");
350 g_io_channel_unref (outchannel);
352 close_and_invalidate (&outpipe);
356 g_print ("g_spawn_sync: outchannel: OK\n");
364 if (errpipe >= 0 && (fds[errindex].revents & G_IO_IN))
366 switch (read_data (errstr, errchannel, error))
370 g_print ("g_spawn_sync: errchannel: READ_FAILED\n");
375 g_print ("g_spawn_sync: errchannel: READ_EOF\n");
376 g_io_channel_unref (errchannel);
378 close_and_invalidate (&errpipe);
382 g_print ("g_spawn_sync: errchannel: OK\n");
391 /* These should only be open still if we had an error. */
393 if (outchannel != NULL)
394 g_io_channel_unref (outchannel);
395 if (errchannel != NULL)
396 g_io_channel_unref (errchannel);
398 close_and_invalidate (&outpipe);
400 close_and_invalidate (&errpipe);
405 g_string_free (outstr, TRUE);
407 g_string_free (errstr, TRUE);
414 *exit_status = status;
417 *standard_output = g_string_free (outstr, FALSE);
420 *standard_error = g_string_free (errstr, FALSE);
427 g_spawn_async_with_pipes (const gchar *working_directory,
431 GSpawnChildSetupFunc child_setup,
434 gint *standard_input,
435 gint *standard_output,
436 gint *standard_error,
439 g_return_val_if_fail (argv != NULL, FALSE);
440 g_return_val_if_fail (standard_output == NULL ||
441 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
442 g_return_val_if_fail (standard_error == NULL ||
443 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
444 /* can't inherit stdin if we have an input pipe. */
445 g_return_val_if_fail (standard_input == NULL ||
446 !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
448 return do_spawn_with_pipes (TRUE,
449 !(flags & G_SPAWN_DO_NOT_REAP_CHILD),
453 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
454 (flags & G_SPAWN_SEARCH_PATH) != 0,
455 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
456 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
457 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
469 g_spawn_command_line_sync (const gchar *command_line,
470 gchar **standard_output,
471 gchar **standard_error,
478 g_return_val_if_fail (command_line != NULL, FALSE);
480 if (!g_shell_parse_argv (command_line,
485 retval = g_spawn_sync (NULL,
501 g_spawn_command_line_async (const gchar *command_line,
507 g_return_val_if_fail (command_line != NULL, FALSE);
509 if (!g_shell_parse_argv (command_line,
514 retval = g_spawn_async (NULL,
528 do_spawn (gboolean dont_wait,
529 gint child_err_report_fd,
533 const gchar *working_directory,
536 gboolean close_descriptors,
537 gboolean search_path,
538 gboolean stdout_to_null,
539 gboolean stderr_to_null,
540 gboolean child_inherits_stdin,
541 GSpawnChildSetupFunc child_setup,
545 gchar args[ARG_COUNT][10];
555 new_argv = g_new (gchar *, argc + 1 + ARG_COUNT);
557 new_argv[0] = "gspawn-win32-helper";
558 sprintf (args[ARG_CHILD_ERR_REPORT], "%d", child_err_report_fd);
559 new_argv[ARG_CHILD_ERR_REPORT] = args[ARG_CHILD_ERR_REPORT];
563 sprintf (args[ARG_STDIN], "%d", stdin_fd);
564 new_argv[ARG_STDIN] = args[ARG_STDIN];
566 else if (child_inherits_stdin)
568 /* Let stdin be alone */
569 new_argv[ARG_STDIN] = "-";
573 /* Keep process from blocking on a read of stdin */
574 new_argv[ARG_STDIN] = "z";
579 sprintf (args[ARG_STDOUT], "%d", stdout_fd);
580 new_argv[ARG_STDOUT] = args[ARG_STDOUT];
582 else if (stdout_to_null)
584 new_argv[ARG_STDOUT] = "z";
588 new_argv[ARG_STDOUT] = "-";
593 sprintf (args[ARG_STDERR], "%d", stderr_fd);
594 new_argv[ARG_STDERR] = args[ARG_STDERR];
596 else if (stderr_to_null)
598 new_argv[ARG_STDERR] = "z";
602 new_argv[ARG_STDERR] = "-";
605 if (working_directory && *working_directory)
606 new_argv[ARG_WORKING_DIRECTORY] = g_strdup (working_directory);
608 new_argv[ARG_WORKING_DIRECTORY] = "-";
610 if (close_descriptors)
611 new_argv[ARG_CLOSE_DESCRIPTORS] = "y";
613 new_argv[ARG_CLOSE_DESCRIPTORS] = "-";
616 new_argv[ARG_USE_PATH] = "y";
618 new_argv[ARG_USE_PATH] = "-";
621 new_argv[ARG_WAIT] = "-";
623 new_argv[ARG_WAIT] = "w";
625 for (i = 0; i <= argc; i++)
626 new_argv[ARG_PROGRAM + i] = argv[i];
628 /* Call user function just before we execute the helper program,
629 * which executes the program. Dunno what's the usefulness of this.
630 * A child setup function used on Unix probably isn't of much use
631 * as such on Win32, anyhow
635 (* child_setup) (user_data);
640 g_print ("calling gspawn-win32-helper with argv:\n");
641 for (i = 0; i < argc + 1 + ARG_COUNT; i++)
642 g_print ("argv[%d]: %s\n", i, (new_argv[i] ? new_argv[i] : "NULL"));
646 /* Let's hope envp hasn't mucked with PATH so that
647 * gspawn-win32-helper.exe isn't found.
649 rc = spawnvpe (P_NOWAIT, "gspawn-win32-helper", new_argv, envp);
651 rc = spawnvp (P_NOWAIT, "gspawn-win32-helper", new_argv);
653 /* Close the child_err_report_fd and the other process's ends of the
654 * pipes in this process, otherwise the reader will never get
657 close (child_err_report_fd);
665 g_free (new_argv[ARG_WORKING_DIRECTORY]);
680 while (bytes < sizeof(gint)*n_ints_in_buf)
685 g_print ("%s:read_ints: trying to read %d bytes from pipe...\n",
687 sizeof(gint)*n_ints_in_buf - bytes);
689 chunk = read (fd, ((gchar*)buf) + bytes,
690 sizeof(gint)*n_ints_in_buf - bytes);
693 g_print ("... got %d bytes\n", chunk);
697 /* Some weird shit happened, bail out */
701 G_SPAWN_ERROR_FAILED,
702 _("Failed to read from child pipe (%s)"),
713 *n_ints_read = bytes/sizeof(gint);
719 do_spawn_with_pipes (gboolean dont_wait,
720 gboolean dont_return_handle,
721 const gchar *working_directory,
724 gboolean close_descriptors,
725 gboolean search_path,
726 gboolean stdout_to_null,
727 gboolean stderr_to_null,
728 gboolean child_inherits_stdin,
729 GSpawnChildSetupFunc child_setup,
732 gint *standard_input,
733 gint *standard_output,
734 gint *standard_error,
738 gint stdin_pipe[2] = { -1, -1 };
739 gint stdout_pipe[2] = { -1, -1 };
740 gint stderr_pipe[2] = { -1, -1 };
741 gint child_err_report_pipe[2] = { -1, -1 };
746 if (!make_pipe (child_err_report_pipe, error))
749 if (standard_input && !make_pipe (stdin_pipe, error))
750 goto cleanup_and_fail;
752 if (standard_output && !make_pipe (stdout_pipe, error))
753 goto cleanup_and_fail;
755 if (standard_error && !make_pipe (stderr_pipe, error))
756 goto cleanup_and_fail;
758 helper = do_spawn (dont_wait,
759 child_err_report_pipe[1],
770 child_inherits_stdin,
774 /* do_spawn() returns -1 if gspawn-win32-helper couldn't be run */
779 G_SPAWN_ERROR_FAILED,
780 _("Failed to execute helper program"));
781 goto cleanup_and_fail;
784 if (!read_ints (child_err_report_pipe[0],
788 goto cleanup_and_fail;
790 /* Error code from gspawn-win32-helper. */
794 if (child_pid && dont_wait && !dont_return_handle)
796 /* helper is our HANDLE for gspawn-win32-helper. It has
797 * told us the HANDLE of its child. Duplicate that into
798 * a HANDLE valid in this process.
800 if (!DuplicateHandle ((HANDLE) helper, (HANDLE) buf[1],
801 GetCurrentProcess (), (LPHANDLE) child_pid,
802 0, TRUE, DUPLICATE_SAME_ACCESS))
809 case CHILD_CHDIR_FAILED:
813 _("Failed to change to directory '%s' (%s)"),
815 g_strerror (buf[1]));
816 goto cleanup_and_fail;
818 case CHILD_SPAWN_FAILED:
821 G_SPAWN_ERROR_FAILED,
822 _("Failed to execute child process (%s)"),
823 g_strerror (buf[1]));
824 goto cleanup_and_fail;
827 /* Success against all odds! return the information */
830 *standard_input = stdin_pipe[1];
832 *standard_output = stdout_pipe[0];
834 *standard_error = stderr_pipe[0];
836 *exit_status = buf[1];
837 CloseHandle ((HANDLE) helper);
843 CloseHandle ((HANDLE) helper);
844 close_and_invalidate (&child_err_report_pipe[0]);
845 close_and_invalidate (&child_err_report_pipe[1]);
846 close_and_invalidate (&stdin_pipe[0]);
847 close_and_invalidate (&stdin_pipe[1]);
848 close_and_invalidate (&stdout_pipe[0]);
849 close_and_invalidate (&stdout_pipe[1]);
850 close_and_invalidate (&stderr_pipe[0]);
851 close_and_invalidate (&stderr_pipe[1]);
857 make_pipe (gint p[2],
864 G_SPAWN_ERROR_FAILED,
865 _("Failed to create pipe for communicating with child process (%s)"),
873 #endif /* !GSPAWN_HELPER */