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.
25 #include <sys/types.h>
33 #ifdef HAVE_SYS_SELECT_H
34 #include <sys/select.h>
35 #endif /* HAVE_SYS_SELECT_H */
39 static gint g_execute (const gchar *file,
42 gboolean search_path);
44 static gboolean make_pipe (gint p[2],
46 static gboolean fork_exec_with_pipes (gboolean intermediate_child,
47 const gchar *working_directory,
50 gboolean close_descriptors,
52 gboolean stdout_to_null,
53 gboolean stderr_to_null,
54 gboolean child_inherits_stdin,
55 gboolean file_and_argv_zero,
56 GSpawnChildSetupFunc child_setup,
60 gint *standard_output,
65 g_spawn_error_quark (void)
67 static GQuark quark = 0;
69 quark = g_quark_from_static_string ("g-exec-error-quark");
75 * @working_directory: child's current working directory, or %NULL to inherit parent's
76 * @argv: child's argument vector
77 * @envp: child's environment, or %NULL to inherit parent's
78 * @flags: flags from #GSpawnFlags
79 * @child_setup: function to run in the child just before <function>exec()</function>
80 * @user_data: user data for @child_setup
81 * @child_pid: return location for child process ID, or %NULL
82 * @error: return location for error
84 * See g_spawn_async_with_pipes() for a full description; this function
85 * simply calls the g_spawn_async_with_pipes() without any pipes.
87 * Return value: %TRUE on success, %FALSE if error is set
90 g_spawn_async (const gchar *working_directory,
94 GSpawnChildSetupFunc child_setup,
99 g_return_val_if_fail (argv != NULL, FALSE);
101 return g_spawn_async_with_pipes (working_directory,
111 /* Avoids a danger in threaded situations (calling close()
112 * on a file descriptor twice, and another thread has
113 * re-opened it since the first close)
116 close_and_invalidate (gint *fd)
128 READ_FAILED = 0, /* FALSE */
134 read_data (GString *str,
143 bytes = read (fd, &buf, 4096);
149 g_string_append_len (str, buf, bytes);
152 else if (bytes < 0 && errno == EINTR)
159 _("Failed to read data from child process (%s)"),
170 * @working_directory: child's current working directory, or %NULL to inherit parent's
171 * @argv: child's argument vector
172 * @envp: child's environment, or %NULL to inherit parent's
173 * @flags: flags from #GSpawnFlags
174 * @child_setup: function to run in the child just before <function>exec()</function>
175 * @user_data: user data for @child_setup
176 * @standard_output: return location for child output
177 * @standard_error: return location for child error messages
178 * @exit_status: child exit status, as returned by <function>waitpid()</function>
179 * @error: return location for error
181 * Executes a child synchronously (waits for the child to exit before returning).
182 * All output from the child is stored in @standard_output and @standard_error,
183 * if those parameters are non-%NULL. If @exit_status is non-%NULL, the exit
184 * status of the child is stored there as it would be returned by
185 * <function>waitpid()</function>; standard UNIX macros such as
186 * <function>WIFEXITED()</function> and <function>WEXITSTATUS()</function>
187 * must be used to evaluate the exit status. If an error occurs, no data is
188 * returned in @standard_output, @standard_error, or @exit_status.
190 * This function calls g_spawn_async_with_pipes() internally; see that function
191 * for full details on the other parameters.
193 * Return value: %TRUE on success, %FALSE if an error was set.
196 g_spawn_sync (const gchar *working_directory,
200 GSpawnChildSetupFunc child_setup,
202 gchar **standard_output,
203 gchar **standard_error,
212 GString *outstr = NULL;
213 GString *errstr = NULL;
217 g_return_val_if_fail (argv != NULL, FALSE);
218 g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
219 g_return_val_if_fail (standard_output == NULL ||
220 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
221 g_return_val_if_fail (standard_error == NULL ||
222 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
224 /* Just to ensure segfaults if callers try to use
225 * these when an error is reported.
228 *standard_output = NULL;
231 *standard_error = NULL;
233 if (!fork_exec_with_pipes (FALSE,
237 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
238 (flags & G_SPAWN_SEARCH_PATH) != 0,
239 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
240 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
241 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
242 (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
247 standard_output ? &outpipe : NULL,
248 standard_error ? &errpipe : NULL,
252 /* Read data from child. */
258 outstr = g_string_new ("");
263 errstr = g_string_new ("");
266 /* Read data until we get EOF on both pipes. */
275 FD_SET (outpipe, &fds);
277 FD_SET (errpipe, &fds);
279 ret = select (MAX (outpipe, errpipe) + 1,
282 NULL /* no timeout */);
284 if (ret < 0 && errno != EINTR)
291 _("Unexpected error in select() reading data from a child process (%s)"),
297 if (outpipe >= 0 && FD_ISSET (outpipe, &fds))
299 switch (read_data (outstr, outpipe, error))
305 close_and_invalidate (&outpipe);
316 if (errpipe >= 0 && FD_ISSET (errpipe, &fds))
318 switch (read_data (errstr, errpipe, error))
324 close_and_invalidate (&errpipe);
336 /* These should only be open still if we had an error. */
339 close_and_invalidate (&outpipe);
341 close_and_invalidate (&errpipe);
343 /* Wait for child to exit, even if we have
348 ret = waitpid (pid, &status, 0);
354 else if (errno == ECHILD)
358 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.");
362 /* We don't need the exit status. */
367 if (!failed) /* avoid error pileups */
374 _("Unexpected error in waitpid() (%s)"),
383 g_string_free (outstr, TRUE);
385 g_string_free (errstr, TRUE);
392 *exit_status = status;
395 *standard_output = g_string_free (outstr, FALSE);
398 *standard_error = g_string_free (errstr, FALSE);
405 * g_spawn_async_with_pipes:
406 * @working_directory: child's current working directory, or %NULL to inherit parent's
407 * @argv: child's argument vector
408 * @envp: child's environment, or %NULL to inherit parent's
409 * @flags: flags from #GSpawnFlags
410 * @child_setup: function to run in the child just before <function>exec()</function>
411 * @user_data: user data for @child_setup
412 * @child_pid: return location for child process ID, or %NULL
413 * @standard_input: return location for file descriptor to write to child's stdin, or %NULL
414 * @standard_output: return location for file descriptor to read child's stdout, or %NULL
415 * @standard_error: return location for file descriptor to read child's stderr, or %NULL
416 * @error: return location for error
418 * Executes a child program asynchronously (your program will not
419 * block waiting for the child to exit). The child program is
420 * specified by the only argument that must be provided, @argv. @argv
421 * should be a %NULL-terminated array of strings, to be passed as the
422 * argument vector for the child. The first string in @argv is of
423 * course the name of the program to execute. By default, the name of
424 * the program must be a full path; the <envar>PATH</envar> shell variable
425 * will only be searched if you pass the %G_SPAWN_SEARCH_PATH flag.
427 * @envp is a %NULL-terminated array of strings, where each string
428 * has the form <literal>KEY=VALUE</literal>. This will become
429 * the child's environment. If @envp is %NULL, the child inherits its
430 * parent's environment.
432 * @flags should be the bitwise OR of any flags you want to affect the
433 * function's behavior. The %G_SPAWN_DO_NOT_REAP_CHILD means that the
434 * child will not be automatically reaped; you must call
435 * <function>waitpid()</function> or handle %SIGCHLD yourself, or the
436 * child will become a zombie.
437 * %G_SPAWN_LEAVE_DESCRIPTORS_OPEN means that the parent's open file
438 * descriptors will be inherited by the child; otherwise all
439 * descriptors except stdin/stdout/stderr will be closed before
440 * calling <function>exec()</function> in the child. %G_SPAWN_SEARCH_PATH
441 * means that <literal>argv[0]</literal> need not be an absolute path, it
442 * will be looked for in the user's <envar>PATH</envar>.
443 * %G_SPAWN_STDOUT_TO_DEV_NULL means that the child's standad output will
444 * be discarded, instead of going to the same location as the parent's
446 * %G_SPAWN_STDERR_TO_DEV_NULL means that the child's standard error
447 * will be discarded. %G_SPAWN_CHILD_INHERITS_STDIN means that
448 * the child will inherit the parent's standard input (by default,
449 * the child's standard input is attached to /dev/null).
450 * %G_SPAWN_FILE_AND_ARGV_ZERO means that the first element of @argv is
451 * the file to execute, while the remaining elements are the
452 * actual argument vector to pass to the file. Normally
453 * g_spawn_async_with_pipes() uses @argv[0] as the file to execute, and
454 * passes all of @argv to the child.
456 * @child_setup and @user_data are a function and user data to be
457 * called in the child after GLib has performed all the setup it plans
458 * to perform (including creating pipes, closing file descriptors,
459 * etc.) but before calling <function>exec()</function>. That is,
460 * @child_setup is called just before calling <function>exec()</function>
461 * in the child. Obviously actions taken in this function will only affect
462 * the child, not the parent.
464 * If non-%NULL, @child_pid will be filled with the child's process
465 * ID. You can use the process ID to send signals to the child, or
466 * to <function>waitpid()</function> if you specified the
467 * %G_SPAWN_DO_NOT_REAP_CHILD flag.
469 * If non-%NULL, the @standard_input, @standard_output, @standard_error
470 * locations will be filled with file descriptors for writing to the child's
471 * standard input or reading from its standard output or standard error.
472 * The caller of g_spawn_async_with_pipes() must close these file descriptors
473 * when they are no longer in use. If these parameters are %NULL, the
474 * corresponding pipe won't be created.
476 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
477 * If an error is set, the function returns %FALSE. Errors
478 * are reported even if they occur in the child (for example if the
479 * executable in <literal>argv[0]</literal> is not found). Typically
480 * the <literal>message</literal> field of returned errors should be displayed
481 * to users. Possible errors are those from the #G_SPAWN_ERROR domain.
483 * If an error occurs, @child_pid, @standard_input, @standard_output,
484 * and @standard_error will not be filled with valid values.
486 * Return value: %TRUE on success, %FALSE if an error was set
489 g_spawn_async_with_pipes (const gchar *working_directory,
493 GSpawnChildSetupFunc child_setup,
496 gint *standard_input,
497 gint *standard_output,
498 gint *standard_error,
501 g_return_val_if_fail (argv != NULL, FALSE);
502 g_return_val_if_fail (standard_output == NULL ||
503 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
504 g_return_val_if_fail (standard_error == NULL ||
505 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
506 /* can't inherit stdin if we have an input pipe. */
507 g_return_val_if_fail (standard_input == NULL ||
508 !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
510 return fork_exec_with_pipes (!(flags & G_SPAWN_DO_NOT_REAP_CHILD),
514 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
515 (flags & G_SPAWN_SEARCH_PATH) != 0,
516 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
517 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
518 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
519 (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
530 * g_spawn_command_line_sync:
531 * @command_line: a command line
532 * @standard_output: return location for child output
533 * @standard_error: return location for child errors
534 * @exit_status: return location for child exit status
535 * @error: return location for errors
537 * A simple version of g_spawn_sync() with little-used parameters
538 * removed, taking a command line instead of an argument vector. See
539 * g_spawn_sync() for full details. @command_line will be parsed by
540 * g_shell_parse_argv(). Unlike g_spawn_sync(), the %G_SPAWN_SEARCH_PATH flag
541 * is enabled. Note that %G_SPAWN_SEARCH_PATH can have security
542 * implications, so consider using g_spawn_sync() directly if
543 * appropriate. Possible errors are those from g_spawn_sync() and those
544 * from g_shell_parse_argv().
546 * Return value: %TRUE on success, %FALSE if an error was set
549 g_spawn_command_line_sync (const gchar *command_line,
550 gchar **standard_output,
551 gchar **standard_error,
558 g_return_val_if_fail (command_line != NULL, FALSE);
560 if (!g_shell_parse_argv (command_line,
565 retval = g_spawn_sync (NULL,
581 * g_spawn_command_line_async:
582 * @command_line: a command line
583 * @error: return location for errors
585 * A simple version of g_spawn_async() that parses a command line with
586 * g_shell_parse_argv() and passes it to g_spawn_async(). Runs a
587 * command line in the background. Unlike g_spawn_async(), the
588 * %G_SPAWN_SEARCH_PATH flag is enabled, other flags are not. Note
589 * that %G_SPAWN_SEARCH_PATH can have security implications, so
590 * consider using g_spawn_async() directly if appropriate. Possible
591 * errors are those from g_shell_parse_argv() and g_spawn_async().
593 * Return value: %TRUE on success, %FALSE if error is set.
596 g_spawn_command_line_async (const gchar *command_line,
602 g_return_val_if_fail (command_line != NULL, FALSE);
604 if (!g_shell_parse_argv (command_line,
609 retval = g_spawn_async (NULL,
623 exec_err_to_g_error (gint en)
629 return G_SPAWN_ERROR_ACCES;
635 return G_SPAWN_ERROR_PERM;
641 return G_SPAWN_ERROR_2BIG;
647 return G_SPAWN_ERROR_NOEXEC;
653 return G_SPAWN_ERROR_NAMETOOLONG;
659 return G_SPAWN_ERROR_NOENT;
665 return G_SPAWN_ERROR_NOMEM;
671 return G_SPAWN_ERROR_NOTDIR;
677 return G_SPAWN_ERROR_LOOP;
683 return G_SPAWN_ERROR_TXTBUSY;
689 return G_SPAWN_ERROR_IO;
695 return G_SPAWN_ERROR_NFILE;
701 return G_SPAWN_ERROR_MFILE;
707 return G_SPAWN_ERROR_INVAL;
713 return G_SPAWN_ERROR_ISDIR;
719 return G_SPAWN_ERROR_LIBBAD;
724 return G_SPAWN_ERROR_FAILED;
730 write_err_and_exit (gint fd, gint msg)
734 write (fd, &msg, sizeof(msg));
735 write (fd, &en, sizeof(en));
741 set_cloexec (gint fd)
743 fcntl (fd, F_SETFD, FD_CLOEXEC);
747 sane_dup2 (gint fd1, gint fd2)
752 ret = dup2 (fd1, fd2);
753 if (ret < 0 && errno == EINTR)
768 do_exec (gint child_err_report_fd,
772 const gchar *working_directory,
775 gboolean close_descriptors,
776 gboolean search_path,
777 gboolean stdout_to_null,
778 gboolean stderr_to_null,
779 gboolean child_inherits_stdin,
780 gboolean file_and_argv_zero,
781 GSpawnChildSetupFunc child_setup,
784 if (working_directory && chdir (working_directory) < 0)
785 write_err_and_exit (child_err_report_fd,
788 /* Close all file descriptors but stdin stdout and stderr as
789 * soon as we exec. Note that this includes
790 * child_err_report_fd, which keeps the parent from blocking
791 * forever on the other end of that pipe.
793 if (close_descriptors)
798 open_max = sysconf (_SC_OPEN_MAX);
799 for (i = 3; i < open_max; i++)
804 /* We need to do child_err_report_fd anyway */
805 set_cloexec (child_err_report_fd);
808 /* Redirect pipes as required */
812 /* dup2 can't actually fail here I don't think */
814 if (sane_dup2 (stdin_fd, 0) < 0)
815 write_err_and_exit (child_err_report_fd,
818 /* ignore this if it doesn't work */
819 close_and_invalidate (&stdin_fd);
821 else if (!child_inherits_stdin)
823 /* Keep process from blocking on a read of stdin */
824 gint read_null = open ("/dev/null", O_RDONLY);
825 sane_dup2 (read_null, 0);
826 close_and_invalidate (&read_null);
831 /* dup2 can't actually fail here I don't think */
833 if (sane_dup2 (stdout_fd, 1) < 0)
834 write_err_and_exit (child_err_report_fd,
837 /* ignore this if it doesn't work */
838 close_and_invalidate (&stdout_fd);
840 else if (stdout_to_null)
842 gint write_null = open ("/dev/null", O_WRONLY);
843 sane_dup2 (write_null, 1);
844 close_and_invalidate (&write_null);
849 /* dup2 can't actually fail here I don't think */
851 if (sane_dup2 (stderr_fd, 2) < 0)
852 write_err_and_exit (child_err_report_fd,
855 /* ignore this if it doesn't work */
856 close_and_invalidate (&stderr_fd);
858 else if (stderr_to_null)
860 gint write_null = open ("/dev/null", O_WRONLY);
861 sane_dup2 (write_null, 2);
862 close_and_invalidate (&write_null);
865 /* Call user function just before we exec */
868 (* child_setup) (user_data);
872 file_and_argv_zero ? argv + 1 : argv,
876 write_err_and_exit (child_err_report_fd,
893 if (bytes >= sizeof(gint)*2)
894 break; /* give up, who knows what happened, should not be
900 ((gchar*)buf) + bytes,
901 sizeof(gint) * n_ints_in_buf - bytes);
902 if (chunk < 0 && errno == EINTR)
907 /* Some weird shit happened, bail out */
911 G_SPAWN_ERROR_FAILED,
912 _("Failed to read from child pipe (%s)"),
923 *n_ints_read = (gint)(bytes / sizeof(gint));
929 fork_exec_with_pipes (gboolean intermediate_child,
930 const gchar *working_directory,
933 gboolean close_descriptors,
934 gboolean search_path,
935 gboolean stdout_to_null,
936 gboolean stderr_to_null,
937 gboolean child_inherits_stdin,
938 gboolean file_and_argv_zero,
939 GSpawnChildSetupFunc child_setup,
942 gint *standard_input,
943 gint *standard_output,
944 gint *standard_error,
948 gint stdin_pipe[2] = { -1, -1 };
949 gint stdout_pipe[2] = { -1, -1 };
950 gint stderr_pipe[2] = { -1, -1 };
951 gint child_err_report_pipe[2] = { -1, -1 };
952 gint child_pid_report_pipe[2] = { -1, -1 };
955 if (!make_pipe (child_err_report_pipe, error))
958 if (intermediate_child && !make_pipe (child_pid_report_pipe, error))
959 goto cleanup_and_fail;
961 if (standard_input && !make_pipe (stdin_pipe, error))
962 goto cleanup_and_fail;
964 if (standard_output && !make_pipe (stdout_pipe, error))
965 goto cleanup_and_fail;
967 if (standard_error && !make_pipe (stderr_pipe, error))
968 goto cleanup_and_fail;
977 _("Failed to fork (%s)"),
980 goto cleanup_and_fail;
984 /* Immediate child. This may or may not be the child that
985 * actually execs the new process.
988 /* Be sure we crash if the parent exits
989 * and we write to the err_report_pipe
991 signal (SIGPIPE, SIG_DFL);
993 /* Close the parent's end of the pipes;
994 * not needed in the close_descriptors case,
997 close_and_invalidate (&child_err_report_pipe[0]);
998 close_and_invalidate (&child_pid_report_pipe[0]);
999 close_and_invalidate (&stdin_pipe[1]);
1000 close_and_invalidate (&stdout_pipe[0]);
1001 close_and_invalidate (&stderr_pipe[0]);
1003 if (intermediate_child)
1005 /* We need to fork an intermediate child that launches the
1006 * final child. The purpose of the intermediate child
1007 * is to exit, so we can waitpid() it immediately.
1008 * Then the grandchild will not become a zombie.
1010 gint grandchild_pid;
1012 grandchild_pid = fork ();
1014 if (grandchild_pid < 0)
1016 /* report -1 as child PID */
1017 write (child_pid_report_pipe[1], &grandchild_pid,
1018 sizeof(grandchild_pid));
1020 write_err_and_exit (child_err_report_pipe[1],
1023 else if (grandchild_pid == 0)
1025 do_exec (child_err_report_pipe[1],
1036 child_inherits_stdin,
1043 write (child_pid_report_pipe[1], &grandchild_pid, sizeof(grandchild_pid));
1044 close_and_invalidate (&child_pid_report_pipe[1]);
1051 /* Just run the child.
1054 do_exec (child_err_report_pipe[1],
1065 child_inherits_stdin,
1078 /* Close the uncared-about ends of the pipes */
1079 close_and_invalidate (&child_err_report_pipe[1]);
1080 close_and_invalidate (&child_pid_report_pipe[1]);
1081 close_and_invalidate (&stdin_pipe[0]);
1082 close_and_invalidate (&stdout_pipe[1]);
1083 close_and_invalidate (&stderr_pipe[1]);
1085 /* If we had an intermediate child, reap it */
1086 if (intermediate_child)
1089 if (waitpid (pid, &status, 0) < 0)
1093 else if (errno == ECHILD)
1094 ; /* do nothing, child already reaped */
1096 g_warning ("waitpid() should not fail in "
1097 "'fork_exec_with_pipes'");
1102 if (!read_ints (child_err_report_pipe[0],
1105 goto cleanup_and_fail;
1109 /* Error from the child. */
1113 case CHILD_CHDIR_FAILED:
1116 G_SPAWN_ERROR_CHDIR,
1117 _("Failed to change to directory '%s' (%s)"),
1119 g_strerror (buf[1]));
1123 case CHILD_EXEC_FAILED:
1126 exec_err_to_g_error (buf[1]),
1127 _("Failed to execute child process \"%s\" (%s)"),
1129 g_strerror (buf[1]));
1133 case CHILD_DUP2_FAILED:
1136 G_SPAWN_ERROR_FAILED,
1137 _("Failed to redirect output or input of child process (%s)"),
1138 g_strerror (buf[1]));
1142 case CHILD_FORK_FAILED:
1146 _("Failed to fork child process (%s)"),
1147 g_strerror (buf[1]));
1153 G_SPAWN_ERROR_FAILED,
1154 _("Unknown error executing child process \"%s\""),
1159 goto cleanup_and_fail;
1162 /* Get child pid from intermediate child pipe. */
1163 if (intermediate_child)
1167 if (!read_ints (child_pid_report_pipe[0],
1168 buf, 1, &n_ints, error))
1169 goto cleanup_and_fail;
1175 G_SPAWN_ERROR_FAILED,
1176 _("Failed to read enough data from child pid pipe (%s)"),
1177 g_strerror (errno));
1178 goto cleanup_and_fail;
1182 /* we have the child pid */
1187 /* Success against all odds! return the information */
1193 *standard_input = stdin_pipe[1];
1194 if (standard_output)
1195 *standard_output = stdout_pipe[0];
1197 *standard_error = stderr_pipe[0];
1203 close_and_invalidate (&child_err_report_pipe[0]);
1204 close_and_invalidate (&child_err_report_pipe[1]);
1205 close_and_invalidate (&child_pid_report_pipe[0]);
1206 close_and_invalidate (&child_pid_report_pipe[1]);
1207 close_and_invalidate (&stdin_pipe[0]);
1208 close_and_invalidate (&stdin_pipe[1]);
1209 close_and_invalidate (&stdout_pipe[0]);
1210 close_and_invalidate (&stdout_pipe[1]);
1211 close_and_invalidate (&stderr_pipe[0]);
1212 close_and_invalidate (&stderr_pipe[1]);
1218 make_pipe (gint p[2],
1225 G_SPAWN_ERROR_FAILED,
1226 _("Failed to create pipe for communicating with child process (%s)"),
1227 g_strerror (errno));
1234 /* Based on execvp from GNU C Library */
1237 script_execute (const gchar *file,
1240 gboolean search_path)
1242 /* Count the arguments. */
1247 /* Construct an argument list for the shell. */
1251 new_argv = g_new0 (gchar*, argc + 1);
1253 new_argv[0] = (char *) "/bin/sh";
1254 new_argv[1] = (char *) file;
1257 new_argv[argc] = argv[argc - 1];
1261 /* Execute the shell. */
1263 execve (new_argv[0], new_argv, envp);
1265 execv (new_argv[0], new_argv);
1272 my_strchrnul (const gchar *str, gchar c)
1274 gchar *p = (gchar*) str;
1275 while (*p && (*p != c))
1282 g_execute (const gchar *file,
1285 gboolean search_path)
1289 /* We check the simple case first. */
1294 if (!search_path || strchr (file, '/') != NULL)
1296 /* Don't search when it contains a slash. */
1298 execve (file, argv, envp);
1302 if (errno == ENOEXEC)
1303 script_execute (file, argv, envp, FALSE);
1307 gboolean got_eacces = 0;
1308 const gchar *path, *p;
1309 gchar *name, *freeme;
1313 path = g_getenv ("PATH");
1316 /* There is no `PATH' in the environment. The default
1317 * search path in libc is the current directory followed by
1318 * the path `confstr' returns for `_CS_PATH'.
1321 /* In GLib we put . last, for security, and don't use the
1322 * unportable confstr(); UNIX98 does not actually specify
1323 * what to search if PATH is unset. POSIX may, dunno.
1326 path = "/bin:/usr/bin:.";
1329 len = strlen (file) + 1;
1330 pathlen = strlen (path);
1331 freeme = name = g_malloc (pathlen + len + 1);
1333 /* Copy the file name at the top, including '\0' */
1334 memcpy (name + pathlen + 1, file, len);
1335 name = name + pathlen;
1336 /* And add the slash before the filename */
1345 p = my_strchrnul (path, ':');
1348 /* Two adjacent colons, or a colon at the beginning or the end
1349 * of `PATH' means to search the current directory.
1353 startp = memcpy (name - (p - path), path, p - path);
1355 /* Try to execute this name. If it works, execv will not return. */
1357 execve (startp, argv, envp);
1359 execv (startp, argv);
1361 if (errno == ENOEXEC)
1362 script_execute (startp, argv, envp, search_path);
1367 /* Record the we got a `Permission denied' error. If we end
1368 * up finding no executable we can use, we want to diagnose
1369 * that we did find one but were denied access.
1382 /* Those errors indicate the file is missing or not executable
1383 * by us, in which case we want to just try the next path
1389 /* Some other error means we found an executable file, but
1390 * something went wrong executing it; return the error to our
1397 while (*p++ != '\0');
1399 /* We tried every element and none of them worked. */
1401 /* At least one failure was due to permissions, so report that
1409 /* Return the error from the last attempt (probably ENOENT). */