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 */
55 #ifdef G_SPAWN_WIN32_DEBUG
57 #define SETUP_DEBUG() /* empty */
60 static int debug = -1;
61 #define SETUP_DEBUG() \
66 if (getenv ("G_SPAWN_WIN32_DEBUG") != NULL) \
83 ARG_CHILD_ERR_REPORT = 1,
87 ARG_WORKING_DIRECTORY,
88 ARG_CLOSE_DESCRIPTORS,
92 ARG_COUNT = ARG_PROGRAM
97 static gboolean make_pipe (gint p[2],
99 static gboolean fork_exec_with_pipes (gboolean dont_wait,
100 const gchar *working_directory,
103 gboolean close_descriptors,
104 gboolean search_path,
105 gboolean stdout_to_null,
106 gboolean stderr_to_null,
107 gboolean child_inherits_stdin,
108 GSpawnChildSetupFunc child_setup,
110 gint *standard_input,
111 gint *standard_output,
112 gint *standard_error,
117 g_spawn_error_quark (void)
119 static GQuark quark = 0;
121 quark = g_quark_from_static_string ("g-exec-error-quark");
127 * @working_directory: child's current working directory, or NULL to inherit parent's
128 * @argv: child's argument vector
129 * @envp: child's environment, or NULL to inherit parent's
130 * @flags: flags from #GSpawnFlags
131 * @child_setup: function to run in the child just before exec()
132 * @user_data: user data for @child_setup
133 * @child_pid: return location for child process ID, or NULL
134 * @error: return location for error
136 * See g_spawn_async_with_pipes() for a full description; this function
137 * simply calls the g_spawn_async_with_pipes() without any pipes.
139 * Return value: TRUE on success, FALSE if error is set
142 g_spawn_async (const gchar *working_directory,
146 GSpawnChildSetupFunc child_setup,
151 g_return_val_if_fail (argv != NULL, FALSE);
153 return g_spawn_async_with_pipes (working_directory,
163 /* Avoids a danger in threaded situations (calling close()
164 * on a file descriptor twice, and another thread has
165 * re-opened it since the first close)
168 close_and_invalidate (gint *fd)
180 READ_FAILED = 0, /* FALSE */
186 read_data (GString *str,
187 GIOChannel *iochannel,
196 gioerror = g_io_channel_read (iochannel, buf, sizeof (buf), &bytes);
202 g_string_append_len (str, buf, bytes);
205 else if (gioerror == G_IO_ERROR_AGAIN)
207 else if (gioerror != G_IO_ERROR_NONE)
212 _("Failed to read data from child process"));
222 * @working_directory: child's current working directory, or NULL to inherit parent's
223 * @argv: child's argument vector
224 * @envp: child's environment, or NULL to inherit parent's
225 * @flags: flags from #GSpawnFlags
226 * @child_setup: function to run in the child just before exec()
227 * @user_data: user data for @child_setup
228 * @standard_output: return location for child output
229 * @standard_error: return location for child error messages
230 * @exit_status: child exit status, as returned by waitpid()
231 * @error: return location for error
233 * Executes a child synchronously (waits for the child to exit before returning).
234 * All output from the child is stored in @standard_output and @standard_error,
235 * if those parameters are non-NULL. If @exit_status is non-NULL, the exit status
236 * of the child is stored there as it would be by waitpid(); standard UNIX
237 * macros such as WIFEXITED() and WEXITSTATUS() must be used to evaluate the
238 * exit status. If an error occurs, no data is returned in @standard_output,
239 * @standard_error, or @exit_status.
241 * This function calls g_spawn_async_with_pipes() internally; see that function
242 * for full details on the other parameters.
244 * Return value: TRUE on success, FALSE if an error was set.
247 g_spawn_sync (const gchar *working_directory,
251 GSpawnChildSetupFunc child_setup,
253 gchar **standard_output,
254 gchar **standard_error,
260 GIOChannel *outchannel = NULL;
261 GIOChannel *errchannel = NULL;
262 GPollFD outfd, errfd;
268 GString *outstr = NULL;
269 GString *errstr = NULL;
273 g_return_val_if_fail (argv != NULL, FALSE);
274 g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
275 g_return_val_if_fail (standard_output == NULL ||
276 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
277 g_return_val_if_fail (standard_error == NULL ||
278 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
280 /* Just to ensure segfaults if callers try to use
281 * these when an error is reported.
284 *standard_output = NULL;
287 *standard_error = NULL;
289 if (!fork_exec_with_pipes (FALSE,
293 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
294 (flags & G_SPAWN_SEARCH_PATH) != 0,
295 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
296 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
297 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
301 standard_output ? &outpipe : NULL,
302 standard_error ? &errpipe : NULL,
307 /* Read data from child. */
313 outstr = g_string_new ("");
314 outchannel = g_io_channel_win32_new_fd (outpipe);
315 g_io_channel_win32_make_pollfd (outchannel,
316 G_IO_IN | G_IO_ERR | G_IO_HUP,
322 errstr = g_string_new ("");
323 errchannel = g_io_channel_win32_new_fd (errpipe);
324 g_io_channel_win32_make_pollfd (errchannel,
325 G_IO_IN | G_IO_ERR | G_IO_HUP,
329 /* Read data until we get EOF on both pipes. */
349 g_print ("%s:g_spawn_sync: calling g_io_channel_win32_poll, nfds=%d\n",
352 ret = g_io_channel_win32_poll (fds, nfds, -1);
361 _("Unexpected error in g_io_channel_win32_poll() reading data from a child process"));
366 if (outpipe >= 0 && (fds[outindex].revents & G_IO_IN))
368 switch (read_data (outstr, outchannel, error))
372 g_print ("g_spawn_sync: outchannel: READ_FAILED\n");
377 g_print ("g_spawn_sync: outchannel: READ_EOF\n");
378 g_io_channel_unref (outchannel);
380 close_and_invalidate (&outpipe);
384 g_print ("g_spawn_sync: outchannel: OK\n");
392 if (errpipe >= 0 && (fds[errindex].revents & G_IO_IN))
394 switch (read_data (errstr, errchannel, error))
398 g_print ("g_spawn_sync: errchannel: READ_FAILED\n");
403 g_print ("g_spawn_sync: errchannel: READ_EOF\n");
404 g_io_channel_unref (errchannel);
406 close_and_invalidate (&errpipe);
410 g_print ("g_spawn_sync: errchannel: OK\n");
419 /* These should only be open still if we had an error. */
421 if (outchannel != NULL)
422 g_io_channel_unref (outchannel);
423 if (errchannel != NULL)
424 g_io_channel_unref (errchannel);
426 close_and_invalidate (&outpipe);
428 close_and_invalidate (&errpipe);
433 g_string_free (outstr, TRUE);
435 g_string_free (errstr, TRUE);
442 *exit_status = status;
445 *standard_output = g_string_free (outstr, FALSE);
448 *standard_error = g_string_free (errstr, FALSE);
455 * g_spawn_async_with_pipes:
456 * @working_directory: child's current working directory, or NULL to inherit parent's
457 * @argv: child's argument vector
458 * @envp: child's environment, or NULL to inherit parent's
459 * @flags: flags from #GSpawnFlags
460 * @child_setup: function to run in the child just before exec()
461 * @user_data: user data for @child_setup
462 * @child_pid: return location for child process ID, or NULL
463 * @standard_input: return location for file descriptor to write to child's stdin, or NULL
464 * @standard_output: return location for file descriptor to read child's stdout, or NULL
465 * @standard_error: return location for file descriptor to read child's stderr, or NULL
466 * @error: return location for error
468 * Executes a child program asynchronously (your program will not
469 * block waiting for the child to exit). The child program is
470 * specified by the only argument that must be provided, @argv. @argv
471 * should be a NULL-terminated array of strings, to be passed as the
472 * argument vector for the child. The first string in @argv is of
473 * course the name of the program to execute. By default, the name of
474 * the program must be a full path; the PATH shell variable will only
475 * be searched if you pass the %G_SPAWN_SEARCH_PATH flag.
477 * @envp is a NULL-terminated array of strings, where each string
478 * has the form <literal>KEY=VALUE</literal>. This will become
479 * the child's environment. If @envp is NULL, the child inherits its
480 * parent's environment.
482 * @flags should be the bitwise OR of any flags you want to affect the
483 * function's behavior. The %G_SPAWN_DO_NOT_REAP_CHILD means that the
484 * child will not be automatically reaped; you must call waitpid() or
485 * handle SIGCHLD yourself, or the child will become a zombie.
486 * %G_SPAWN_LEAVE_DESCRIPTORS_OPEN means that the parent's open file
487 * descriptors will be inherited by the child; otherwise all
488 * descriptors except stdin/stdout/stderr will be closed before
489 * calling exec() in the child. %G_SPAWN_SEARCH_PATH means that
490 * <literal>argv[0]</literal> need not be an absolute path, it
491 * will be looked for in the user's PATH. %G_SPAWN_STDOUT_TO_DEV_NULL
492 * means that the child's standad output will be discarded, instead
493 * of going to the same location as the parent's standard output.
494 * %G_SPAWN_STDERR_TO_DEV_NULL means that the child's standard error
495 * will be discarded. %G_SPAWN_CHILD_INHERITS_STDIN means that
496 * the child will inherit the parent's standard input (by default,
497 * the child's standard input is attached to /dev/null).
499 * @child_setup and @user_data are a function and user data to be
500 * called in the child after GLib has performed all the setup it plans
501 * to perform (including creating pipes, closing file descriptors,
502 * etc.) but before calling exec(). That is, @child_setup is called
503 * just before calling exec() in the child. Obviously actions taken in
504 * this function will only affect the child, not the parent.
506 * If non-NULL, @child_pid will be filled with the child's process
507 * ID. You can use the process ID to send signals to the child, or
508 * to waitpid() if you specified the %G_SPAWN_DO_NOT_REAP_CHILD flag.
510 * If non-NULL, the @standard_input, @standard_output, @standard_error
511 * locations will be filled with file descriptors for writing to the child's
512 * standard input or reading from its standard output or standard error.
513 * The caller of g_spawn_async_with_pipes() must close these file descriptors
514 * when they are no longer in use. If these parameters are NULL, the
515 * corresponding pipe won't be created.
517 * @error can be NULL to ignore errors, or non-NULL to report errors.
518 * If an error is set, the function returns FALSE. Errors
519 * are reported even if they occur in the child (for example if the
520 * executable in <literal>argv[0]</literal> is not found). Typically
521 * the <literal>message</literal> field of returned errors should be displayed
522 * to users. Possible errors are those from the #G_SPAWN_ERROR domain.
524 * If an error occurs, @child_pid, @standard_input, @standard_output,
525 * and @standard_error will not be filled with valid values.
527 * Return value: TRUE on success, FALSE if an error was set
530 g_spawn_async_with_pipes (const gchar *working_directory,
534 GSpawnChildSetupFunc child_setup,
537 gint *standard_input,
538 gint *standard_output,
539 gint *standard_error,
542 g_return_val_if_fail (argv != NULL, FALSE);
543 g_return_val_if_fail (standard_output == NULL ||
544 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
545 g_return_val_if_fail (standard_error == NULL ||
546 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
547 /* can't inherit stdin if we have an input pipe. */
548 g_return_val_if_fail (standard_input == NULL ||
549 !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
551 return fork_exec_with_pipes (!(flags & G_SPAWN_DO_NOT_REAP_CHILD),
555 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
556 (flags & G_SPAWN_SEARCH_PATH) != 0,
557 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
558 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
559 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
570 * g_spawn_command_line_sync:
571 * @command_line: a command line
572 * @standard_output: return location for child output
573 * @standard_error: return location for child errors
574 * @exit_status: return location for child exit status
575 * @error: return location for errors
577 * A simple version of g_spawn_sync() with little-used parameters
578 * removed, taking a command line instead of an argument vector. See
579 * g_spawn_sync() for full details. @command_line will be parsed by
580 * g_shell_parse_argv(). Unlike g_spawn_sync(), the %G_SPAWN_SEARCH_PATH flag
581 * is enabled. Note that %G_SPAWN_SEARCH_PATH can have security
582 * implications, so consider using g_spawn_sync() directly if
583 * appropriate. Possible errors are those from g_spawn_sync() and those
584 * from g_shell_parse_argv().
586 * Return value: TRUE on success, FALSE if an error was set
589 g_spawn_command_line_sync (const gchar *command_line,
590 gchar **standard_output,
591 gchar **standard_error,
598 g_return_val_if_fail (command_line != NULL, FALSE);
600 if (!g_shell_parse_argv (command_line,
605 retval = g_spawn_sync (NULL,
621 * g_spawn_command_line_async:
622 * @command_line: a command line
623 * @error: return location for errors
625 * A simple version of g_spawn_async() that parses a command line with
626 * g_shell_parse_argv() and passes it to g_spawn_async(). Runs a
627 * command line in the background. Unlike g_spawn_async(), the
628 * %G_SPAWN_SEARCH_PATH flag is enabled, other flags are not. Note
629 * that %G_SPAWN_SEARCH_PATH can have security implications, so
630 * consider using g_spawn_async() directly if appropriate. Possible
631 * errors are those from g_shell_parse_argv() and g_spawn_async().
633 * Return value: TRUE on success, FALSE if error is set.
636 g_spawn_command_line_async (const gchar *command_line,
642 g_return_val_if_fail (command_line != NULL, FALSE);
644 if (!g_shell_parse_argv (command_line,
649 retval = g_spawn_async (NULL,
663 do_exec (gboolean dont_wait,
664 gint child_err_report_fd,
668 const gchar *working_directory,
671 gboolean close_descriptors,
672 gboolean search_path,
673 gboolean stdout_to_null,
674 gboolean stderr_to_null,
675 gboolean child_inherits_stdin,
676 GSpawnChildSetupFunc child_setup,
680 gchar args[ARG_COUNT][10];
689 new_argv = g_new (gchar *, argc + 1 + ARG_COUNT);
691 new_argv[0] = "gspawn-win32-helper";
692 sprintf (args[ARG_CHILD_ERR_REPORT], "%d", child_err_report_fd);
693 new_argv[ARG_CHILD_ERR_REPORT] = args[ARG_CHILD_ERR_REPORT];
697 sprintf (args[ARG_STDIN], "%d", stdin_fd);
698 new_argv[ARG_STDIN] = args[ARG_STDIN];
700 else if (child_inherits_stdin)
702 /* Let stdin be alone */
703 new_argv[ARG_STDIN] = "-";
707 /* Keep process from blocking on a read of stdin */
708 new_argv[ARG_STDIN] = "z";
713 sprintf (args[ARG_STDOUT], "%d", stdout_fd);
714 new_argv[ARG_STDOUT] = args[ARG_STDOUT];
716 else if (stdout_to_null)
718 new_argv[ARG_STDOUT] = "z";
722 new_argv[ARG_STDOUT] = "-";
727 sprintf (args[ARG_STDERR], "%d", stderr_fd);
728 new_argv[ARG_STDERR] = args[ARG_STDERR];
730 else if (stderr_to_null)
732 new_argv[ARG_STDERR] = "z";
736 new_argv[ARG_STDERR] = "-";
739 if (working_directory && *working_directory)
740 new_argv[ARG_WORKING_DIRECTORY] = working_directory;
742 new_argv[ARG_WORKING_DIRECTORY] = "-";
744 if (close_descriptors)
745 new_argv[ARG_CLOSE_DESCRIPTORS] = "y";
747 new_argv[ARG_CLOSE_DESCRIPTORS] = "-";
750 new_argv[ARG_USE_PATH] = "y";
752 new_argv[ARG_USE_PATH] = "-";
755 new_argv[ARG_WAIT] = "-";
757 new_argv[ARG_WAIT] = "w";
759 for (i = 0; i <= argc; i++)
760 new_argv[ARG_PROGRAM + i] = argv[i];
762 /* Call user function just before we execute the helper program,
763 * which executes the program. Dunno what's the usefulness of this.
764 * A child setup function used on Unix probably isn't of much use
765 * as such on Win32, anyhow.
769 (* child_setup) (user_data);
774 g_print ("calling gspawn-win32-helper with argv:\n");
775 for (i = 0; i < argc + 1 + ARG_COUNT; i++)
776 g_print ("argv[%d]: %s\n", i, (new_argv[i] ? new_argv[i] : "NULL"));
780 /* Let's hope envp hasn't mucked with PATH so that
781 * gspawn-win32-helper.exe isn't found.
783 spawnvpe (P_NOWAIT, "gspawn-win32-helper", new_argv, envp);
785 spawnvp (P_NOWAIT, "gspawn-win32-helper", new_argv);
787 /* FIXME: What if gspawn-win32-helper.exe isn't found? */
789 /* Close the child_err_report_fd and the other process's ends of the
790 * pipes in this process, otherwise the reader will never get
793 close (child_err_report_fd);
815 while (bytes < sizeof(gint)*n_ints_in_buf)
820 g_print ("%s:read_ints: trying to read %d bytes from pipe...\n",
822 sizeof(gint)*n_ints_in_buf - bytes);
824 chunk = read (fd, ((gchar*)buf) + bytes,
825 sizeof(gint)*n_ints_in_buf - bytes);
828 g_print ("... got %d bytes\n", chunk);
832 /* Some weird shit happened, bail out */
836 G_SPAWN_ERROR_FAILED,
837 _("Failed to read from child pipe (%s)"),
848 *n_ints_read = bytes/sizeof(gint);
854 fork_exec_with_pipes (gboolean dont_wait,
855 const gchar *working_directory,
858 gboolean close_descriptors,
859 gboolean search_path,
860 gboolean stdout_to_null,
861 gboolean stderr_to_null,
862 gboolean child_inherits_stdin,
863 GSpawnChildSetupFunc child_setup,
865 gint *standard_input,
866 gint *standard_output,
867 gint *standard_error,
871 gint stdin_pipe[2] = { -1, -1 };
872 gint stdout_pipe[2] = { -1, -1 };
873 gint stderr_pipe[2] = { -1, -1 };
874 gint child_err_report_pipe[2] = { -1, -1 };
880 if (!make_pipe (child_err_report_pipe, error))
883 if (standard_input && !make_pipe (stdin_pipe, error))
884 goto cleanup_and_fail;
886 if (standard_output && !make_pipe (stdout_pipe, error))
887 goto cleanup_and_fail;
889 if (standard_error && !make_pipe (stderr_pipe, error))
890 goto cleanup_and_fail;
892 status = do_exec (dont_wait,
893 child_err_report_pipe[1],
904 child_inherits_stdin,
908 if (!read_ints (child_err_report_pipe[0],
911 goto cleanup_and_fail;
915 /* Error from the child. */
922 case CHILD_CHDIR_FAILED:
926 _("Failed to change to directory '%s' (%s)"),
928 g_strerror (buf[1]));
929 goto cleanup_and_fail;
931 case CHILD_SPAWN_FAILED:
934 G_SPAWN_ERROR_FAILED,
935 _("Failed to execute child process (%s)"),
936 g_strerror (buf[1]));
937 goto cleanup_and_fail;
941 /* Success against all odds! return the information */
944 *standard_input = stdin_pipe[1];
946 *standard_output = stdout_pipe[0];
948 *standard_error = stderr_pipe[0];
950 *exit_status = status;
955 close_and_invalidate (&child_err_report_pipe[0]);
956 close_and_invalidate (&child_err_report_pipe[1]);
957 close_and_invalidate (&stdin_pipe[0]);
958 close_and_invalidate (&stdin_pipe[1]);
959 close_and_invalidate (&stdout_pipe[0]);
960 close_and_invalidate (&stdout_pipe[1]);
961 close_and_invalidate (&stderr_pipe[0]);
962 close_and_invalidate (&stderr_pipe[1]);
968 make_pipe (gint p[2],
975 G_SPAWN_ERROR_FAILED,
976 _("Failed to create pipe for communicating with child process (%s)"),
984 #endif /* !GSPAWN_HELPER */