1 /* gspawn.c - Process launching
3 * Copyright 2000 Red Hat, Inc.
4 * g_execvpe implementation based on GNU libc execvp:
5 * Copyright 1991, 92, 95, 96, 97, 98, 99 Free Software Foundation, Inc.
7 * GLib is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * GLib is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with GLib; see the file COPYING.LIB. If not, write
19 * to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
27 #include <sys/types.h>
35 #ifdef HAVE_SYS_SELECT_H
36 #include <sys/select.h>
37 #endif /* HAVE_SYS_SELECT_H */
41 static gint g_execute (const gchar *file,
44 gboolean search_path);
46 static gboolean make_pipe (gint p[2],
48 static gboolean fork_exec_with_pipes (gboolean intermediate_child,
49 const gchar *working_directory,
52 gboolean close_descriptors,
54 gboolean stdout_to_null,
55 gboolean stderr_to_null,
56 gboolean child_inherits_stdin,
57 gboolean file_and_argv_zero,
58 GSpawnChildSetupFunc child_setup,
62 gint *standard_output,
67 g_spawn_error_quark (void)
69 static GQuark quark = 0;
71 quark = g_quark_from_static_string ("g-exec-error-quark");
77 * @working_directory: child's current working directory, or %NULL to inherit parent's
78 * @argv: child's argument vector
79 * @envp: child's environment, or %NULL to inherit parent's
80 * @flags: flags from #GSpawnFlags
81 * @child_setup: function to run in the child just before <function>exec()</function>
82 * @user_data: user data for @child_setup
83 * @child_pid: return location for child process ID, or %NULL
84 * @error: return location for error
86 * See g_spawn_async_with_pipes() for a full description; this function
87 * simply calls the g_spawn_async_with_pipes() without any pipes.
89 * Return value: %TRUE on success, %FALSE if error is set
92 g_spawn_async (const gchar *working_directory,
96 GSpawnChildSetupFunc child_setup,
101 g_return_val_if_fail (argv != NULL, FALSE);
103 return g_spawn_async_with_pipes (working_directory,
113 /* Avoids a danger in threaded situations (calling close()
114 * on a file descriptor twice, and another thread has
115 * re-opened it since the first close)
118 close_and_invalidate (gint *fd)
135 READ_FAILED = 0, /* FALSE */
141 read_data (GString *str,
150 bytes = read (fd, &buf, 4096);
156 g_string_append_len (str, buf, bytes);
159 else if (bytes < 0 && errno == EINTR)
166 _("Failed to read data from child process (%s)"),
177 * @working_directory: child's current working directory, or %NULL to inherit parent's
178 * @argv: child's argument vector
179 * @envp: child's environment, or %NULL to inherit parent's
180 * @flags: flags from #GSpawnFlags
181 * @child_setup: function to run in the child just before <function>exec()</function>
182 * @user_data: user data for @child_setup
183 * @standard_output: return location for child output
184 * @standard_error: return location for child error messages
185 * @exit_status: child exit status, as returned by <function>waitpid()</function>
186 * @error: return location for error
188 * Executes a child synchronously (waits for the child to exit before returning).
189 * All output from the child is stored in @standard_output and @standard_error,
190 * if those parameters are non-%NULL. If @exit_status is non-%NULL, the exit
191 * status of the child is stored there as it would be returned by
192 * <function>waitpid()</function>; standard UNIX macros such as
193 * <function>WIFEXITED()</function> and <function>WEXITSTATUS()</function>
194 * must be used to evaluate the exit status. If an error occurs, no data is
195 * returned in @standard_output, @standard_error, or @exit_status.
197 * This function calls g_spawn_async_with_pipes() internally; see that function
198 * for full details on the other parameters.
200 * Return value: %TRUE on success, %FALSE if an error was set.
203 g_spawn_sync (const gchar *working_directory,
207 GSpawnChildSetupFunc child_setup,
209 gchar **standard_output,
210 gchar **standard_error,
219 GString *outstr = NULL;
220 GString *errstr = NULL;
224 g_return_val_if_fail (argv != NULL, FALSE);
225 g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
226 g_return_val_if_fail (standard_output == NULL ||
227 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
228 g_return_val_if_fail (standard_error == NULL ||
229 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
231 /* Just to ensure segfaults if callers try to use
232 * these when an error is reported.
235 *standard_output = NULL;
238 *standard_error = NULL;
240 if (!fork_exec_with_pipes (FALSE,
244 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
245 (flags & G_SPAWN_SEARCH_PATH) != 0,
246 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
247 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
248 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
249 (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
254 standard_output ? &outpipe : NULL,
255 standard_error ? &errpipe : NULL,
259 /* Read data from child. */
265 outstr = g_string_new ("");
270 errstr = g_string_new ("");
273 /* Read data until we get EOF on both pipes. */
282 FD_SET (outpipe, &fds);
284 FD_SET (errpipe, &fds);
286 ret = select (MAX (outpipe, errpipe) + 1,
289 NULL /* no timeout */);
291 if (ret < 0 && errno != EINTR)
298 _("Unexpected error in select() reading data from a child process (%s)"),
304 if (outpipe >= 0 && FD_ISSET (outpipe, &fds))
306 switch (read_data (outstr, outpipe, error))
312 close_and_invalidate (&outpipe);
323 if (errpipe >= 0 && FD_ISSET (errpipe, &fds))
325 switch (read_data (errstr, errpipe, error))
331 close_and_invalidate (&errpipe);
343 /* These should only be open still if we had an error. */
346 close_and_invalidate (&outpipe);
348 close_and_invalidate (&errpipe);
350 /* Wait for child to exit, even if we have
355 ret = waitpid (pid, &status, 0);
361 else if (errno == ECHILD)
365 g_warning ("In call to g_spawn_sync(), exit status of a child process was requested but SIGCHLD action was set to SIG_IGN and ECHILD was received by waitpid(), so exit status can't be returned. This is a bug in the program calling g_spawn_sync(); either don't request the exit status, or don't set the SIGCHLD action.");
369 /* We don't need the exit status. */
374 if (!failed) /* avoid error pileups */
381 _("Unexpected error in waitpid() (%s)"),
390 g_string_free (outstr, TRUE);
392 g_string_free (errstr, TRUE);
399 *exit_status = status;
402 *standard_output = g_string_free (outstr, FALSE);
405 *standard_error = g_string_free (errstr, FALSE);
412 * g_spawn_async_with_pipes:
413 * @working_directory: child's current working directory, or %NULL to inherit parent's
414 * @argv: child's argument vector
415 * @envp: child's environment, or %NULL to inherit parent's
416 * @flags: flags from #GSpawnFlags
417 * @child_setup: function to run in the child just before <function>exec()</function>
418 * @user_data: user data for @child_setup
419 * @child_pid: return location for child process ID, or %NULL
420 * @standard_input: return location for file descriptor to write to child's stdin, or %NULL
421 * @standard_output: return location for file descriptor to read child's stdout, or %NULL
422 * @standard_error: return location for file descriptor to read child's stderr, or %NULL
423 * @error: return location for error
425 * Executes a child program asynchronously (your program will not
426 * block waiting for the child to exit). The child program is
427 * specified by the only argument that must be provided, @argv. @argv
428 * should be a %NULL-terminated array of strings, to be passed as the
429 * argument vector for the child. The first string in @argv is of
430 * course the name of the program to execute. By default, the name of
431 * the program must be a full path; the <envar>PATH</envar> shell variable
432 * will only be searched if you pass the %G_SPAWN_SEARCH_PATH flag.
434 * @envp is a %NULL-terminated array of strings, where each string
435 * has the form <literal>KEY=VALUE</literal>. This will become
436 * the child's environment. If @envp is %NULL, the child inherits its
437 * parent's environment.
439 * @flags should be the bitwise OR of any flags you want to affect the
440 * function's behavior. The %G_SPAWN_DO_NOT_REAP_CHILD means that the
441 * child will not be automatically reaped; you must call
442 * <function>waitpid()</function> or handle %SIGCHLD yourself, or the
443 * child will become a zombie.
444 * %G_SPAWN_LEAVE_DESCRIPTORS_OPEN means that the parent's open file
445 * descriptors will be inherited by the child; otherwise all
446 * descriptors except stdin/stdout/stderr will be closed before
447 * calling <function>exec()</function> in the child. %G_SPAWN_SEARCH_PATH
448 * means that <literal>argv[0]</literal> need not be an absolute path, it
449 * will be looked for in the user's <envar>PATH</envar>.
450 * %G_SPAWN_STDOUT_TO_DEV_NULL means that the child's standard output will
451 * be discarded, instead of going to the same location as the parent's
452 * standard output. If you use this flag, @standard_output must be %NULL.
453 * %G_SPAWN_STDERR_TO_DEV_NULL means that the child's standard error
454 * will be discarded, instead of going to the same location as the parent's
455 * standard error. If you use this flag, @standard_error must be %NULL.
456 * %G_SPAWN_CHILD_INHERITS_STDIN means that the child will inherit the parent's
457 * standard input (by default, the child's standard input is attached to
458 * /dev/null). If you use this flag, @standard_input must be %NULL.
459 * %G_SPAWN_FILE_AND_ARGV_ZERO means that the first element of @argv is
460 * the file to execute, while the remaining elements are the
461 * actual argument vector to pass to the file. Normally
462 * g_spawn_async_with_pipes() uses @argv[0] as the file to execute, and
463 * passes all of @argv to the child.
465 * @child_setup and @user_data are a function and user data to be
466 * called in the child after GLib has performed all the setup it plans
467 * to perform (including creating pipes, closing file descriptors,
468 * etc.) but before calling <function>exec()</function>. That is,
469 * @child_setup is called just before calling <function>exec()</function>
470 * in the child. Obviously actions taken in this function will only affect
471 * the child, not the parent.
473 * If non-%NULL, @child_pid will be filled with the child's process
474 * ID. You can use the process ID to send signals to the child, or
475 * to <function>waitpid()</function> if you specified the
476 * %G_SPAWN_DO_NOT_REAP_CHILD flag.
478 * If non-%NULL, the @standard_input, @standard_output, @standard_error
479 * locations will be filled with file descriptors for writing to the child's
480 * standard input or reading from its standard output or standard error.
481 * The caller of g_spawn_async_with_pipes() must close these file descriptors
482 * when they are no longer in use. If these parameters are %NULL, the corresponding
483 * pipe won't be created.
485 * If @standard_input is NULL, the child's standard input is attached to /dev/null
486 * unless %G_SPAWN_CHILD_INHERITS_STDIN is set.
488 * If @standard_error is NULL, the child's standard error goes to the same location
489 * as the parent's standard error unless %G_SPAWN_STDERR_TO_DEV_NULL is set.
491 * If @standard_output is NULL, the child's standard output goes to the same location
492 * as the parent's standard output unless %G_SPAWN_STDOUT_TO_DEV_NULL is set.
494 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
495 * If an error is set, the function returns %FALSE. Errors
496 * are reported even if they occur in the child (for example if the
497 * executable in <literal>argv[0]</literal> is not found). Typically
498 * the <literal>message</literal> field of returned errors should be displayed
499 * to users. Possible errors are those from the #G_SPAWN_ERROR domain.
501 * If an error occurs, @child_pid, @standard_input, @standard_output,
502 * and @standard_error will not be filled with valid values.
504 * Return value: %TRUE on success, %FALSE if an error was set
507 g_spawn_async_with_pipes (const gchar *working_directory,
511 GSpawnChildSetupFunc child_setup,
514 gint *standard_input,
515 gint *standard_output,
516 gint *standard_error,
519 g_return_val_if_fail (argv != NULL, FALSE);
520 g_return_val_if_fail (standard_output == NULL ||
521 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
522 g_return_val_if_fail (standard_error == NULL ||
523 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
524 /* can't inherit stdin if we have an input pipe. */
525 g_return_val_if_fail (standard_input == NULL ||
526 !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
528 return fork_exec_with_pipes (!(flags & G_SPAWN_DO_NOT_REAP_CHILD),
532 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
533 (flags & G_SPAWN_SEARCH_PATH) != 0,
534 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
535 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
536 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
537 (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
548 * g_spawn_command_line_sync:
549 * @command_line: a command line
550 * @standard_output: return location for child output
551 * @standard_error: return location for child errors
552 * @exit_status: return location for child exit status
553 * @error: return location for errors
555 * A simple version of g_spawn_sync() with little-used parameters
556 * removed, taking a command line instead of an argument vector. See
557 * g_spawn_sync() for full details. @command_line will be parsed by
558 * g_shell_parse_argv(). Unlike g_spawn_sync(), the %G_SPAWN_SEARCH_PATH flag
559 * is enabled. Note that %G_SPAWN_SEARCH_PATH can have security
560 * implications, so consider using g_spawn_sync() directly if
561 * appropriate. Possible errors are those from g_spawn_sync() and those
562 * from g_shell_parse_argv().
564 * On Windows, please note the implications of g_shell_parse_argv()
565 * parsing @command_line. Space is a separator, and backslashes are
566 * special. Thus you cannot simply pass a @command_line consisting of
567 * a canonical Windows path, like "c:\\program files\\app\\app.exe",
568 * as the backslashes will be eaten, and the space will act as a
569 * separator. You need to enclose the path with single quotes, like
570 * "'c:\\program files\\app\\app.exe'".
572 * Return value: %TRUE on success, %FALSE if an error was set
575 g_spawn_command_line_sync (const gchar *command_line,
576 gchar **standard_output,
577 gchar **standard_error,
584 g_return_val_if_fail (command_line != NULL, FALSE);
586 if (!g_shell_parse_argv (command_line,
591 retval = g_spawn_sync (NULL,
607 * g_spawn_command_line_async:
608 * @command_line: a command line
609 * @error: return location for errors
611 * A simple version of g_spawn_async() that parses a command line with
612 * g_shell_parse_argv() and passes it to g_spawn_async(). Runs a
613 * command line in the background. Unlike g_spawn_async(), the
614 * %G_SPAWN_SEARCH_PATH flag is enabled, other flags are not. Note
615 * that %G_SPAWN_SEARCH_PATH can have security implications, so
616 * consider using g_spawn_async() directly if appropriate. Possible
617 * errors are those from g_shell_parse_argv() and g_spawn_async().
619 * The same concerns on Windows apply as for g_spawn_command_line_sync().
621 * Return value: %TRUE on success, %FALSE if error is set.
624 g_spawn_command_line_async (const gchar *command_line,
630 g_return_val_if_fail (command_line != NULL, FALSE);
632 if (!g_shell_parse_argv (command_line,
637 retval = g_spawn_async (NULL,
651 exec_err_to_g_error (gint en)
657 return G_SPAWN_ERROR_ACCES;
663 return G_SPAWN_ERROR_PERM;
669 return G_SPAWN_ERROR_2BIG;
675 return G_SPAWN_ERROR_NOEXEC;
681 return G_SPAWN_ERROR_NAMETOOLONG;
687 return G_SPAWN_ERROR_NOENT;
693 return G_SPAWN_ERROR_NOMEM;
699 return G_SPAWN_ERROR_NOTDIR;
705 return G_SPAWN_ERROR_LOOP;
711 return G_SPAWN_ERROR_TXTBUSY;
717 return G_SPAWN_ERROR_IO;
723 return G_SPAWN_ERROR_NFILE;
729 return G_SPAWN_ERROR_MFILE;
735 return G_SPAWN_ERROR_INVAL;
741 return G_SPAWN_ERROR_ISDIR;
747 return G_SPAWN_ERROR_LIBBAD;
752 return G_SPAWN_ERROR_FAILED;
758 write_err_and_exit (gint fd, gint msg)
762 write (fd, &msg, sizeof(msg));
763 write (fd, &en, sizeof(en));
769 set_cloexec (gint fd)
771 fcntl (fd, F_SETFD, FD_CLOEXEC);
775 sane_dup2 (gint fd1, gint fd2)
780 ret = dup2 (fd1, fd2);
781 if (ret < 0 && errno == EINTR)
796 do_exec (gint child_err_report_fd,
800 const gchar *working_directory,
803 gboolean close_descriptors,
804 gboolean search_path,
805 gboolean stdout_to_null,
806 gboolean stderr_to_null,
807 gboolean child_inherits_stdin,
808 gboolean file_and_argv_zero,
809 GSpawnChildSetupFunc child_setup,
812 if (working_directory && chdir (working_directory) < 0)
813 write_err_and_exit (child_err_report_fd,
816 /* Close all file descriptors but stdin stdout and stderr as
817 * soon as we exec. Note that this includes
818 * child_err_report_fd, which keeps the parent from blocking
819 * forever on the other end of that pipe.
821 if (close_descriptors)
826 open_max = sysconf (_SC_OPEN_MAX);
827 for (i = 3; i < open_max; i++)
832 /* We need to do child_err_report_fd anyway */
833 set_cloexec (child_err_report_fd);
836 /* Redirect pipes as required */
840 /* dup2 can't actually fail here I don't think */
842 if (sane_dup2 (stdin_fd, 0) < 0)
843 write_err_and_exit (child_err_report_fd,
846 /* ignore this if it doesn't work */
847 close_and_invalidate (&stdin_fd);
849 else if (!child_inherits_stdin)
851 /* Keep process from blocking on a read of stdin */
852 gint read_null = open ("/dev/null", O_RDONLY);
853 sane_dup2 (read_null, 0);
854 close_and_invalidate (&read_null);
859 /* dup2 can't actually fail here I don't think */
861 if (sane_dup2 (stdout_fd, 1) < 0)
862 write_err_and_exit (child_err_report_fd,
865 /* ignore this if it doesn't work */
866 close_and_invalidate (&stdout_fd);
868 else if (stdout_to_null)
870 gint write_null = open ("/dev/null", O_WRONLY);
871 sane_dup2 (write_null, 1);
872 close_and_invalidate (&write_null);
877 /* dup2 can't actually fail here I don't think */
879 if (sane_dup2 (stderr_fd, 2) < 0)
880 write_err_and_exit (child_err_report_fd,
883 /* ignore this if it doesn't work */
884 close_and_invalidate (&stderr_fd);
886 else if (stderr_to_null)
888 gint write_null = open ("/dev/null", O_WRONLY);
889 sane_dup2 (write_null, 2);
890 close_and_invalidate (&write_null);
893 /* Call user function just before we exec */
896 (* child_setup) (user_data);
900 file_and_argv_zero ? argv + 1 : argv,
904 write_err_and_exit (child_err_report_fd,
921 if (bytes >= sizeof(gint)*2)
922 break; /* give up, who knows what happened, should not be
928 ((gchar*)buf) + bytes,
929 sizeof(gint) * n_ints_in_buf - bytes);
930 if (chunk < 0 && errno == EINTR)
935 /* Some weird shit happened, bail out */
939 G_SPAWN_ERROR_FAILED,
940 _("Failed to read from child pipe (%s)"),
951 *n_ints_read = (gint)(bytes / sizeof(gint));
957 fork_exec_with_pipes (gboolean intermediate_child,
958 const gchar *working_directory,
961 gboolean close_descriptors,
962 gboolean search_path,
963 gboolean stdout_to_null,
964 gboolean stderr_to_null,
965 gboolean child_inherits_stdin,
966 gboolean file_and_argv_zero,
967 GSpawnChildSetupFunc child_setup,
970 gint *standard_input,
971 gint *standard_output,
972 gint *standard_error,
976 gint stdin_pipe[2] = { -1, -1 };
977 gint stdout_pipe[2] = { -1, -1 };
978 gint stderr_pipe[2] = { -1, -1 };
979 gint child_err_report_pipe[2] = { -1, -1 };
980 gint child_pid_report_pipe[2] = { -1, -1 };
983 if (!make_pipe (child_err_report_pipe, error))
986 if (intermediate_child && !make_pipe (child_pid_report_pipe, error))
987 goto cleanup_and_fail;
989 if (standard_input && !make_pipe (stdin_pipe, error))
990 goto cleanup_and_fail;
992 if (standard_output && !make_pipe (stdout_pipe, error))
993 goto cleanup_and_fail;
995 if (standard_error && !make_pipe (stderr_pipe, error))
996 goto cleanup_and_fail;
1005 _("Failed to fork (%s)"),
1006 g_strerror (errno));
1008 goto cleanup_and_fail;
1012 /* Immediate child. This may or may not be the child that
1013 * actually execs the new process.
1016 /* Be sure we crash if the parent exits
1017 * and we write to the err_report_pipe
1019 signal (SIGPIPE, SIG_DFL);
1021 /* Close the parent's end of the pipes;
1022 * not needed in the close_descriptors case,
1025 close_and_invalidate (&child_err_report_pipe[0]);
1026 close_and_invalidate (&child_pid_report_pipe[0]);
1027 close_and_invalidate (&stdin_pipe[1]);
1028 close_and_invalidate (&stdout_pipe[0]);
1029 close_and_invalidate (&stderr_pipe[0]);
1031 if (intermediate_child)
1033 /* We need to fork an intermediate child that launches the
1034 * final child. The purpose of the intermediate child
1035 * is to exit, so we can waitpid() it immediately.
1036 * Then the grandchild will not become a zombie.
1038 gint grandchild_pid;
1040 grandchild_pid = fork ();
1042 if (grandchild_pid < 0)
1044 /* report -1 as child PID */
1045 write (child_pid_report_pipe[1], &grandchild_pid,
1046 sizeof(grandchild_pid));
1048 write_err_and_exit (child_err_report_pipe[1],
1051 else if (grandchild_pid == 0)
1053 do_exec (child_err_report_pipe[1],
1064 child_inherits_stdin,
1071 write (child_pid_report_pipe[1], &grandchild_pid, sizeof(grandchild_pid));
1072 close_and_invalidate (&child_pid_report_pipe[1]);
1079 /* Just run the child.
1082 do_exec (child_err_report_pipe[1],
1093 child_inherits_stdin,
1106 /* Close the uncared-about ends of the pipes */
1107 close_and_invalidate (&child_err_report_pipe[1]);
1108 close_and_invalidate (&child_pid_report_pipe[1]);
1109 close_and_invalidate (&stdin_pipe[0]);
1110 close_and_invalidate (&stdout_pipe[1]);
1111 close_and_invalidate (&stderr_pipe[1]);
1113 /* If we had an intermediate child, reap it */
1114 if (intermediate_child)
1117 if (waitpid (pid, &status, 0) < 0)
1121 else if (errno == ECHILD)
1122 ; /* do nothing, child already reaped */
1124 g_warning ("waitpid() should not fail in "
1125 "'fork_exec_with_pipes'");
1130 if (!read_ints (child_err_report_pipe[0],
1133 goto cleanup_and_fail;
1137 /* Error from the child. */
1141 case CHILD_CHDIR_FAILED:
1144 G_SPAWN_ERROR_CHDIR,
1145 _("Failed to change to directory '%s' (%s)"),
1147 g_strerror (buf[1]));
1151 case CHILD_EXEC_FAILED:
1154 exec_err_to_g_error (buf[1]),
1155 _("Failed to execute child process \"%s\" (%s)"),
1157 g_strerror (buf[1]));
1161 case CHILD_DUP2_FAILED:
1164 G_SPAWN_ERROR_FAILED,
1165 _("Failed to redirect output or input of child process (%s)"),
1166 g_strerror (buf[1]));
1170 case CHILD_FORK_FAILED:
1174 _("Failed to fork child process (%s)"),
1175 g_strerror (buf[1]));
1181 G_SPAWN_ERROR_FAILED,
1182 _("Unknown error executing child process \"%s\""),
1187 goto cleanup_and_fail;
1190 /* Get child pid from intermediate child pipe. */
1191 if (intermediate_child)
1195 if (!read_ints (child_pid_report_pipe[0],
1196 buf, 1, &n_ints, error))
1197 goto cleanup_and_fail;
1203 G_SPAWN_ERROR_FAILED,
1204 _("Failed to read enough data from child pid pipe (%s)"),
1205 g_strerror (errno));
1206 goto cleanup_and_fail;
1210 /* we have the child pid */
1215 /* Success against all odds! return the information */
1216 close_and_invalidate (&child_err_report_pipe[0]);
1217 close_and_invalidate (&child_pid_report_pipe[0]);
1223 *standard_input = stdin_pipe[1];
1224 if (standard_output)
1225 *standard_output = stdout_pipe[0];
1227 *standard_error = stderr_pipe[0];
1234 /* There was an error from the Child, reap the child to avoid it being
1241 if (waitpid (pid, NULL, 0) < 0)
1245 else if (errno == ECHILD)
1246 ; /* do nothing, child already reaped */
1248 g_warning ("waitpid() should not fail in "
1249 "'fork_exec_with_pipes'");
1253 close_and_invalidate (&child_err_report_pipe[0]);
1254 close_and_invalidate (&child_err_report_pipe[1]);
1255 close_and_invalidate (&child_pid_report_pipe[0]);
1256 close_and_invalidate (&child_pid_report_pipe[1]);
1257 close_and_invalidate (&stdin_pipe[0]);
1258 close_and_invalidate (&stdin_pipe[1]);
1259 close_and_invalidate (&stdout_pipe[0]);
1260 close_and_invalidate (&stdout_pipe[1]);
1261 close_and_invalidate (&stderr_pipe[0]);
1262 close_and_invalidate (&stderr_pipe[1]);
1268 make_pipe (gint p[2],
1275 G_SPAWN_ERROR_FAILED,
1276 _("Failed to create pipe for communicating with child process (%s)"),
1277 g_strerror (errno));
1284 /* Based on execvp from GNU C Library */
1287 script_execute (const gchar *file,
1290 gboolean search_path)
1292 /* Count the arguments. */
1297 /* Construct an argument list for the shell. */
1301 new_argv = g_new0 (gchar*, argc + 2); /* /bin/sh and NULL */
1303 new_argv[0] = (char *) "/bin/sh";
1304 new_argv[1] = (char *) file;
1307 new_argv[argc + 1] = argv[argc];
1311 /* Execute the shell. */
1313 execve (new_argv[0], new_argv, envp);
1315 execv (new_argv[0], new_argv);
1322 my_strchrnul (const gchar *str, gchar c)
1324 gchar *p = (gchar*) str;
1325 while (*p && (*p != c))
1332 g_execute (const gchar *file,
1335 gboolean search_path)
1339 /* We check the simple case first. */
1344 if (!search_path || strchr (file, '/') != NULL)
1346 /* Don't search when it contains a slash. */
1348 execve (file, argv, envp);
1352 if (errno == ENOEXEC)
1353 script_execute (file, argv, envp, FALSE);
1357 gboolean got_eacces = 0;
1358 const gchar *path, *p;
1359 gchar *name, *freeme;
1363 path = g_getenv ("PATH");
1366 /* There is no `PATH' in the environment. The default
1367 * search path in libc is the current directory followed by
1368 * the path `confstr' returns for `_CS_PATH'.
1371 /* In GLib we put . last, for security, and don't use the
1372 * unportable confstr(); UNIX98 does not actually specify
1373 * what to search if PATH is unset. POSIX may, dunno.
1376 path = "/bin:/usr/bin:.";
1379 len = strlen (file) + 1;
1380 pathlen = strlen (path);
1381 freeme = name = g_malloc (pathlen + len + 1);
1383 /* Copy the file name at the top, including '\0' */
1384 memcpy (name + pathlen + 1, file, len);
1385 name = name + pathlen;
1386 /* And add the slash before the filename */
1395 p = my_strchrnul (path, ':');
1398 /* Two adjacent colons, or a colon at the beginning or the end
1399 * of `PATH' means to search the current directory.
1403 startp = memcpy (name - (p - path), path, p - path);
1405 /* Try to execute this name. If it works, execv will not return. */
1407 execve (startp, argv, envp);
1409 execv (startp, argv);
1411 if (errno == ENOEXEC)
1412 script_execute (startp, argv, envp, search_path);
1417 /* Record the we got a `Permission denied' error. If we end
1418 * up finding no executable we can use, we want to diagnose
1419 * that we did find one but were denied access.
1432 /* Those errors indicate the file is missing or not executable
1433 * by us, in which case we want to just try the next path
1439 /* Some other error means we found an executable file, but
1440 * something went wrong executing it; return the error to our
1447 while (*p++ != '\0');
1449 /* We tried every element and none of them worked. */
1451 /* At least one failure was due to permissions, so report that
1459 /* Return the error from the last attempt (probably ENOENT). */