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)
130 READ_FAILED = 0, /* FALSE */
136 read_data (GString *str,
145 bytes = read (fd, &buf, 4096);
151 g_string_append_len (str, buf, bytes);
154 else if (bytes < 0 && errno == EINTR)
161 _("Failed to read data from child process (%s)"),
172 * @working_directory: child's current working directory, or %NULL to inherit parent's
173 * @argv: child's argument vector
174 * @envp: child's environment, or %NULL to inherit parent's
175 * @flags: flags from #GSpawnFlags
176 * @child_setup: function to run in the child just before <function>exec()</function>
177 * @user_data: user data for @child_setup
178 * @standard_output: return location for child output
179 * @standard_error: return location for child error messages
180 * @exit_status: child exit status, as returned by <function>waitpid()</function>
181 * @error: return location for error
183 * Executes a child synchronously (waits for the child to exit before returning).
184 * All output from the child is stored in @standard_output and @standard_error,
185 * if those parameters are non-%NULL. If @exit_status is non-%NULL, the exit
186 * status of the child is stored there as it would be returned by
187 * <function>waitpid()</function>; standard UNIX macros such as
188 * <function>WIFEXITED()</function> and <function>WEXITSTATUS()</function>
189 * must be used to evaluate the exit status. If an error occurs, no data is
190 * returned in @standard_output, @standard_error, or @exit_status.
192 * This function calls g_spawn_async_with_pipes() internally; see that function
193 * for full details on the other parameters.
195 * Return value: %TRUE on success, %FALSE if an error was set.
198 g_spawn_sync (const gchar *working_directory,
202 GSpawnChildSetupFunc child_setup,
204 gchar **standard_output,
205 gchar **standard_error,
214 GString *outstr = NULL;
215 GString *errstr = NULL;
219 g_return_val_if_fail (argv != NULL, FALSE);
220 g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
221 g_return_val_if_fail (standard_output == NULL ||
222 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
223 g_return_val_if_fail (standard_error == NULL ||
224 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
226 /* Just to ensure segfaults if callers try to use
227 * these when an error is reported.
230 *standard_output = NULL;
233 *standard_error = NULL;
235 if (!fork_exec_with_pipes (FALSE,
239 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
240 (flags & G_SPAWN_SEARCH_PATH) != 0,
241 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
242 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
243 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
244 (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
249 standard_output ? &outpipe : NULL,
250 standard_error ? &errpipe : NULL,
254 /* Read data from child. */
260 outstr = g_string_new ("");
265 errstr = g_string_new ("");
268 /* Read data until we get EOF on both pipes. */
277 FD_SET (outpipe, &fds);
279 FD_SET (errpipe, &fds);
281 ret = select (MAX (outpipe, errpipe) + 1,
284 NULL /* no timeout */);
286 if (ret < 0 && errno != EINTR)
293 _("Unexpected error in select() reading data from a child process (%s)"),
299 if (outpipe >= 0 && FD_ISSET (outpipe, &fds))
301 switch (read_data (outstr, outpipe, error))
307 close_and_invalidate (&outpipe);
318 if (errpipe >= 0 && FD_ISSET (errpipe, &fds))
320 switch (read_data (errstr, errpipe, error))
326 close_and_invalidate (&errpipe);
338 /* These should only be open still if we had an error. */
341 close_and_invalidate (&outpipe);
343 close_and_invalidate (&errpipe);
345 /* Wait for child to exit, even if we have
350 ret = waitpid (pid, &status, 0);
356 else if (errno == ECHILD)
360 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.");
364 /* We don't need the exit status. */
369 if (!failed) /* avoid error pileups */
376 _("Unexpected error in waitpid() (%s)"),
385 g_string_free (outstr, TRUE);
387 g_string_free (errstr, TRUE);
394 *exit_status = status;
397 *standard_output = g_string_free (outstr, FALSE);
400 *standard_error = g_string_free (errstr, FALSE);
407 * g_spawn_async_with_pipes:
408 * @working_directory: child's current working directory, or %NULL to inherit parent's
409 * @argv: child's argument vector
410 * @envp: child's environment, or %NULL to inherit parent's
411 * @flags: flags from #GSpawnFlags
412 * @child_setup: function to run in the child just before <function>exec()</function>
413 * @user_data: user data for @child_setup
414 * @child_pid: return location for child process ID, or %NULL
415 * @standard_input: return location for file descriptor to write to child's stdin, or %NULL
416 * @standard_output: return location for file descriptor to read child's stdout, or %NULL
417 * @standard_error: return location for file descriptor to read child's stderr, or %NULL
418 * @error: return location for error
420 * Executes a child program asynchronously (your program will not
421 * block waiting for the child to exit). The child program is
422 * specified by the only argument that must be provided, @argv. @argv
423 * should be a %NULL-terminated array of strings, to be passed as the
424 * argument vector for the child. The first string in @argv is of
425 * course the name of the program to execute. By default, the name of
426 * the program must be a full path; the <envar>PATH</envar> shell variable
427 * will only be searched if you pass the %G_SPAWN_SEARCH_PATH flag.
429 * @envp is a %NULL-terminated array of strings, where each string
430 * has the form <literal>KEY=VALUE</literal>. This will become
431 * the child's environment. If @envp is %NULL, the child inherits its
432 * parent's environment.
434 * @flags should be the bitwise OR of any flags you want to affect the
435 * function's behavior. The %G_SPAWN_DO_NOT_REAP_CHILD means that the
436 * child will not be automatically reaped; you must call
437 * <function>waitpid()</function> or handle %SIGCHLD yourself, or the
438 * child will become a zombie.
439 * %G_SPAWN_LEAVE_DESCRIPTORS_OPEN means that the parent's open file
440 * descriptors will be inherited by the child; otherwise all
441 * descriptors except stdin/stdout/stderr will be closed before
442 * calling <function>exec()</function> in the child. %G_SPAWN_SEARCH_PATH
443 * means that <literal>argv[0]</literal> need not be an absolute path, it
444 * will be looked for in the user's <envar>PATH</envar>.
445 * %G_SPAWN_STDOUT_TO_DEV_NULL means that the child's standad output will
446 * be discarded, instead of going to the same location as the parent's
448 * %G_SPAWN_STDERR_TO_DEV_NULL means that the child's standard error
449 * will be discarded. %G_SPAWN_CHILD_INHERITS_STDIN means that
450 * the child will inherit the parent's standard input (by default,
451 * the child's standard input is attached to /dev/null).
452 * %G_SPAWN_FILE_AND_ARGV_ZERO means that the first element of @argv is
453 * the file to execute, while the remaining elements are the
454 * actual argument vector to pass to the file. Normally
455 * g_spawn_async_with_pipes() uses @argv[0] as the file to execute, and
456 * passes all of @argv to the child.
458 * @child_setup and @user_data are a function and user data to be
459 * called in the child after GLib has performed all the setup it plans
460 * to perform (including creating pipes, closing file descriptors,
461 * etc.) but before calling <function>exec()</function>. That is,
462 * @child_setup is called just before calling <function>exec()</function>
463 * in the child. Obviously actions taken in this function will only affect
464 * the child, not the parent.
466 * If non-%NULL, @child_pid will be filled with the child's process
467 * ID. You can use the process ID to send signals to the child, or
468 * to <function>waitpid()</function> if you specified the
469 * %G_SPAWN_DO_NOT_REAP_CHILD flag.
471 * If non-%NULL, the @standard_input, @standard_output, @standard_error
472 * locations will be filled with file descriptors for writing to the child's
473 * standard input or reading from its standard output or standard error.
474 * The caller of g_spawn_async_with_pipes() must close these file descriptors
475 * when they are no longer in use. If these parameters are %NULL, the
476 * corresponding pipe won't be created.
478 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
479 * If an error is set, the function returns %FALSE. Errors
480 * are reported even if they occur in the child (for example if the
481 * executable in <literal>argv[0]</literal> is not found). Typically
482 * the <literal>message</literal> field of returned errors should be displayed
483 * to users. Possible errors are those from the #G_SPAWN_ERROR domain.
485 * If an error occurs, @child_pid, @standard_input, @standard_output,
486 * and @standard_error will not be filled with valid values.
488 * Return value: %TRUE on success, %FALSE if an error was set
491 g_spawn_async_with_pipes (const gchar *working_directory,
495 GSpawnChildSetupFunc child_setup,
498 gint *standard_input,
499 gint *standard_output,
500 gint *standard_error,
503 g_return_val_if_fail (argv != NULL, FALSE);
504 g_return_val_if_fail (standard_output == NULL ||
505 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
506 g_return_val_if_fail (standard_error == NULL ||
507 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
508 /* can't inherit stdin if we have an input pipe. */
509 g_return_val_if_fail (standard_input == NULL ||
510 !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
512 return fork_exec_with_pipes (!(flags & G_SPAWN_DO_NOT_REAP_CHILD),
516 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
517 (flags & G_SPAWN_SEARCH_PATH) != 0,
518 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
519 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
520 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
521 (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
532 * g_spawn_command_line_sync:
533 * @command_line: a command line
534 * @standard_output: return location for child output
535 * @standard_error: return location for child errors
536 * @exit_status: return location for child exit status
537 * @error: return location for errors
539 * A simple version of g_spawn_sync() with little-used parameters
540 * removed, taking a command line instead of an argument vector. See
541 * g_spawn_sync() for full details. @command_line will be parsed by
542 * g_shell_parse_argv(). Unlike g_spawn_sync(), the %G_SPAWN_SEARCH_PATH flag
543 * is enabled. Note that %G_SPAWN_SEARCH_PATH can have security
544 * implications, so consider using g_spawn_sync() directly if
545 * appropriate. Possible errors are those from g_spawn_sync() and those
546 * from g_shell_parse_argv().
548 * Return value: %TRUE on success, %FALSE if an error was set
551 g_spawn_command_line_sync (const gchar *command_line,
552 gchar **standard_output,
553 gchar **standard_error,
560 g_return_val_if_fail (command_line != NULL, FALSE);
562 if (!g_shell_parse_argv (command_line,
567 retval = g_spawn_sync (NULL,
583 * g_spawn_command_line_async:
584 * @command_line: a command line
585 * @error: return location for errors
587 * A simple version of g_spawn_async() that parses a command line with
588 * g_shell_parse_argv() and passes it to g_spawn_async(). Runs a
589 * command line in the background. Unlike g_spawn_async(), the
590 * %G_SPAWN_SEARCH_PATH flag is enabled, other flags are not. Note
591 * that %G_SPAWN_SEARCH_PATH can have security implications, so
592 * consider using g_spawn_async() directly if appropriate. Possible
593 * errors are those from g_shell_parse_argv() and g_spawn_async().
595 * Return value: %TRUE on success, %FALSE if error is set.
598 g_spawn_command_line_async (const gchar *command_line,
604 g_return_val_if_fail (command_line != NULL, FALSE);
606 if (!g_shell_parse_argv (command_line,
611 retval = g_spawn_async (NULL,
625 exec_err_to_g_error (gint en)
631 return G_SPAWN_ERROR_ACCES;
637 return G_SPAWN_ERROR_PERM;
643 return G_SPAWN_ERROR_2BIG;
649 return G_SPAWN_ERROR_NOEXEC;
655 return G_SPAWN_ERROR_NAMETOOLONG;
661 return G_SPAWN_ERROR_NOENT;
667 return G_SPAWN_ERROR_NOMEM;
673 return G_SPAWN_ERROR_NOTDIR;
679 return G_SPAWN_ERROR_LOOP;
685 return G_SPAWN_ERROR_TXTBUSY;
691 return G_SPAWN_ERROR_IO;
697 return G_SPAWN_ERROR_NFILE;
703 return G_SPAWN_ERROR_MFILE;
709 return G_SPAWN_ERROR_INVAL;
715 return G_SPAWN_ERROR_ISDIR;
721 return G_SPAWN_ERROR_LIBBAD;
726 return G_SPAWN_ERROR_FAILED;
732 write_err_and_exit (gint fd, gint msg)
736 write (fd, &msg, sizeof(msg));
737 write (fd, &en, sizeof(en));
743 set_cloexec (gint fd)
745 fcntl (fd, F_SETFD, FD_CLOEXEC);
749 sane_dup2 (gint fd1, gint fd2)
754 ret = dup2 (fd1, fd2);
755 if (ret < 0 && errno == EINTR)
770 do_exec (gint child_err_report_fd,
774 const gchar *working_directory,
777 gboolean close_descriptors,
778 gboolean search_path,
779 gboolean stdout_to_null,
780 gboolean stderr_to_null,
781 gboolean child_inherits_stdin,
782 gboolean file_and_argv_zero,
783 GSpawnChildSetupFunc child_setup,
786 if (working_directory && chdir (working_directory) < 0)
787 write_err_and_exit (child_err_report_fd,
790 /* Close all file descriptors but stdin stdout and stderr as
791 * soon as we exec. Note that this includes
792 * child_err_report_fd, which keeps the parent from blocking
793 * forever on the other end of that pipe.
795 if (close_descriptors)
800 open_max = sysconf (_SC_OPEN_MAX);
801 for (i = 3; i < open_max; i++)
806 /* We need to do child_err_report_fd anyway */
807 set_cloexec (child_err_report_fd);
810 /* Redirect pipes as required */
814 /* dup2 can't actually fail here I don't think */
816 if (sane_dup2 (stdin_fd, 0) < 0)
817 write_err_and_exit (child_err_report_fd,
820 /* ignore this if it doesn't work */
821 close_and_invalidate (&stdin_fd);
823 else if (!child_inherits_stdin)
825 /* Keep process from blocking on a read of stdin */
826 gint read_null = open ("/dev/null", O_RDONLY);
827 sane_dup2 (read_null, 0);
828 close_and_invalidate (&read_null);
833 /* dup2 can't actually fail here I don't think */
835 if (sane_dup2 (stdout_fd, 1) < 0)
836 write_err_and_exit (child_err_report_fd,
839 /* ignore this if it doesn't work */
840 close_and_invalidate (&stdout_fd);
842 else if (stdout_to_null)
844 gint write_null = open ("/dev/null", O_WRONLY);
845 sane_dup2 (write_null, 1);
846 close_and_invalidate (&write_null);
851 /* dup2 can't actually fail here I don't think */
853 if (sane_dup2 (stderr_fd, 2) < 0)
854 write_err_and_exit (child_err_report_fd,
857 /* ignore this if it doesn't work */
858 close_and_invalidate (&stderr_fd);
860 else if (stderr_to_null)
862 gint write_null = open ("/dev/null", O_WRONLY);
863 sane_dup2 (write_null, 2);
864 close_and_invalidate (&write_null);
867 /* Call user function just before we exec */
870 (* child_setup) (user_data);
874 file_and_argv_zero ? argv + 1 : argv,
878 write_err_and_exit (child_err_report_fd,
895 if (bytes >= sizeof(gint)*2)
896 break; /* give up, who knows what happened, should not be
902 ((gchar*)buf) + bytes,
903 sizeof(gint) * n_ints_in_buf - bytes);
904 if (chunk < 0 && errno == EINTR)
909 /* Some weird shit happened, bail out */
913 G_SPAWN_ERROR_FAILED,
914 _("Failed to read from child pipe (%s)"),
925 *n_ints_read = (gint)(bytes / sizeof(gint));
931 fork_exec_with_pipes (gboolean intermediate_child,
932 const gchar *working_directory,
935 gboolean close_descriptors,
936 gboolean search_path,
937 gboolean stdout_to_null,
938 gboolean stderr_to_null,
939 gboolean child_inherits_stdin,
940 gboolean file_and_argv_zero,
941 GSpawnChildSetupFunc child_setup,
944 gint *standard_input,
945 gint *standard_output,
946 gint *standard_error,
950 gint stdin_pipe[2] = { -1, -1 };
951 gint stdout_pipe[2] = { -1, -1 };
952 gint stderr_pipe[2] = { -1, -1 };
953 gint child_err_report_pipe[2] = { -1, -1 };
954 gint child_pid_report_pipe[2] = { -1, -1 };
957 if (!make_pipe (child_err_report_pipe, error))
960 if (intermediate_child && !make_pipe (child_pid_report_pipe, error))
961 goto cleanup_and_fail;
963 if (standard_input && !make_pipe (stdin_pipe, error))
964 goto cleanup_and_fail;
966 if (standard_output && !make_pipe (stdout_pipe, error))
967 goto cleanup_and_fail;
969 if (standard_error && !make_pipe (stderr_pipe, error))
970 goto cleanup_and_fail;
979 _("Failed to fork (%s)"),
982 goto cleanup_and_fail;
986 /* Immediate child. This may or may not be the child that
987 * actually execs the new process.
990 /* Be sure we crash if the parent exits
991 * and we write to the err_report_pipe
993 signal (SIGPIPE, SIG_DFL);
995 /* Close the parent's end of the pipes;
996 * not needed in the close_descriptors case,
999 close_and_invalidate (&child_err_report_pipe[0]);
1000 close_and_invalidate (&child_pid_report_pipe[0]);
1001 close_and_invalidate (&stdin_pipe[1]);
1002 close_and_invalidate (&stdout_pipe[0]);
1003 close_and_invalidate (&stderr_pipe[0]);
1005 if (intermediate_child)
1007 /* We need to fork an intermediate child that launches the
1008 * final child. The purpose of the intermediate child
1009 * is to exit, so we can waitpid() it immediately.
1010 * Then the grandchild will not become a zombie.
1012 gint grandchild_pid;
1014 grandchild_pid = fork ();
1016 if (grandchild_pid < 0)
1018 /* report -1 as child PID */
1019 write (child_pid_report_pipe[1], &grandchild_pid,
1020 sizeof(grandchild_pid));
1022 write_err_and_exit (child_err_report_pipe[1],
1025 else if (grandchild_pid == 0)
1027 do_exec (child_err_report_pipe[1],
1038 child_inherits_stdin,
1045 write (child_pid_report_pipe[1], &grandchild_pid, sizeof(grandchild_pid));
1046 close_and_invalidate (&child_pid_report_pipe[1]);
1053 /* Just run the child.
1056 do_exec (child_err_report_pipe[1],
1067 child_inherits_stdin,
1080 /* Close the uncared-about ends of the pipes */
1081 close_and_invalidate (&child_err_report_pipe[1]);
1082 close_and_invalidate (&child_pid_report_pipe[1]);
1083 close_and_invalidate (&stdin_pipe[0]);
1084 close_and_invalidate (&stdout_pipe[1]);
1085 close_and_invalidate (&stderr_pipe[1]);
1087 /* If we had an intermediate child, reap it */
1088 if (intermediate_child)
1091 if (waitpid (pid, &status, 0) < 0)
1095 else if (errno == ECHILD)
1096 ; /* do nothing, child already reaped */
1098 g_warning ("waitpid() should not fail in "
1099 "'fork_exec_with_pipes'");
1104 if (!read_ints (child_err_report_pipe[0],
1107 goto cleanup_and_fail;
1111 /* Error from the child. */
1115 case CHILD_CHDIR_FAILED:
1118 G_SPAWN_ERROR_CHDIR,
1119 _("Failed to change to directory '%s' (%s)"),
1121 g_strerror (buf[1]));
1125 case CHILD_EXEC_FAILED:
1128 exec_err_to_g_error (buf[1]),
1129 _("Failed to execute child process \"%s\" (%s)"),
1131 g_strerror (buf[1]));
1135 case CHILD_DUP2_FAILED:
1138 G_SPAWN_ERROR_FAILED,
1139 _("Failed to redirect output or input of child process (%s)"),
1140 g_strerror (buf[1]));
1144 case CHILD_FORK_FAILED:
1148 _("Failed to fork child process (%s)"),
1149 g_strerror (buf[1]));
1155 G_SPAWN_ERROR_FAILED,
1156 _("Unknown error executing child process \"%s\""),
1161 goto cleanup_and_fail;
1164 /* Get child pid from intermediate child pipe. */
1165 if (intermediate_child)
1169 if (!read_ints (child_pid_report_pipe[0],
1170 buf, 1, &n_ints, error))
1171 goto cleanup_and_fail;
1177 G_SPAWN_ERROR_FAILED,
1178 _("Failed to read enough data from child pid pipe (%s)"),
1179 g_strerror (errno));
1180 goto cleanup_and_fail;
1184 /* we have the child pid */
1189 /* Success against all odds! return the information */
1195 *standard_input = stdin_pipe[1];
1196 if (standard_output)
1197 *standard_output = stdout_pipe[0];
1199 *standard_error = stderr_pipe[0];
1205 close_and_invalidate (&child_err_report_pipe[0]);
1206 close_and_invalidate (&child_err_report_pipe[1]);
1207 close_and_invalidate (&child_pid_report_pipe[0]);
1208 close_and_invalidate (&child_pid_report_pipe[1]);
1209 close_and_invalidate (&stdin_pipe[0]);
1210 close_and_invalidate (&stdin_pipe[1]);
1211 close_and_invalidate (&stdout_pipe[0]);
1212 close_and_invalidate (&stdout_pipe[1]);
1213 close_and_invalidate (&stderr_pipe[0]);
1214 close_and_invalidate (&stderr_pipe[1]);
1220 make_pipe (gint p[2],
1227 G_SPAWN_ERROR_FAILED,
1228 _("Failed to create pipe for communicating with child process (%s)"),
1229 g_strerror (errno));
1236 /* Based on execvp from GNU C Library */
1239 script_execute (const gchar *file,
1242 gboolean search_path)
1244 /* Count the arguments. */
1249 /* Construct an argument list for the shell. */
1253 new_argv = g_new0 (gchar*, argc + 2); /* /bin/sh and NULL */
1255 new_argv[0] = (char *) "/bin/sh";
1256 new_argv[1] = (char *) file;
1259 new_argv[argc + 1] = argv[argc];
1263 /* Execute the shell. */
1265 execve (new_argv[0], new_argv, envp);
1267 execv (new_argv[0], new_argv);
1274 my_strchrnul (const gchar *str, gchar c)
1276 gchar *p = (gchar*) str;
1277 while (*p && (*p != c))
1284 g_execute (const gchar *file,
1287 gboolean search_path)
1291 /* We check the simple case first. */
1296 if (!search_path || strchr (file, '/') != NULL)
1298 /* Don't search when it contains a slash. */
1300 execve (file, argv, envp);
1304 if (errno == ENOEXEC)
1305 script_execute (file, argv, envp, FALSE);
1309 gboolean got_eacces = 0;
1310 const gchar *path, *p;
1311 gchar *name, *freeme;
1315 path = g_getenv ("PATH");
1318 /* There is no `PATH' in the environment. The default
1319 * search path in libc is the current directory followed by
1320 * the path `confstr' returns for `_CS_PATH'.
1323 /* In GLib we put . last, for security, and don't use the
1324 * unportable confstr(); UNIX98 does not actually specify
1325 * what to search if PATH is unset. POSIX may, dunno.
1328 path = "/bin:/usr/bin:.";
1331 len = strlen (file) + 1;
1332 pathlen = strlen (path);
1333 freeme = name = g_malloc (pathlen + len + 1);
1335 /* Copy the file name at the top, including '\0' */
1336 memcpy (name + pathlen + 1, file, len);
1337 name = name + pathlen;
1338 /* And add the slash before the filename */
1347 p = my_strchrnul (path, ':');
1350 /* Two adjacent colons, or a colon at the beginning or the end
1351 * of `PATH' means to search the current directory.
1355 startp = memcpy (name - (p - path), path, p - path);
1357 /* Try to execute this name. If it works, execv will not return. */
1359 execve (startp, argv, envp);
1361 execv (startp, argv);
1363 if (errno == ENOEXEC)
1364 script_execute (startp, argv, envp, search_path);
1369 /* Record the we got a `Permission denied' error. If we end
1370 * up finding no executable we can use, we want to diagnose
1371 * that we did find one but were denied access.
1384 /* Those errors indicate the file is missing or not executable
1385 * by us, in which case we want to just try the next path
1391 /* Some other error means we found an executable file, but
1392 * something went wrong executing it; return the error to our
1399 while (*p++ != '\0');
1401 /* We tried every element and none of them worked. */
1403 /* At least one failure was due to permissions, so report that
1411 /* Return the error from the last attempt (probably ENOENT). */