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 exec()
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 exec()
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 waitpid()
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 status
184 * of the child is stored there as it would be by waitpid(); standard UNIX
185 * macros such as WIFEXITED() and WEXITSTATUS() must be used to evaluate the
186 * exit status. If an error occurs, no data is returned in @standard_output,
187 * @standard_error, or @exit_status.
189 * This function calls g_spawn_async_with_pipes() internally; see that function
190 * for full details on the other parameters.
192 * Return value: TRUE on success, FALSE if an error was set.
195 g_spawn_sync (const gchar *working_directory,
199 GSpawnChildSetupFunc child_setup,
201 gchar **standard_output,
202 gchar **standard_error,
211 GString *outstr = NULL;
212 GString *errstr = NULL;
216 g_return_val_if_fail (argv != NULL, FALSE);
217 g_return_val_if_fail (!(flags & G_SPAWN_DO_NOT_REAP_CHILD), FALSE);
218 g_return_val_if_fail (standard_output == NULL ||
219 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
220 g_return_val_if_fail (standard_error == NULL ||
221 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
223 /* Just to ensure segfaults if callers try to use
224 * these when an error is reported.
227 *standard_output = NULL;
230 *standard_error = NULL;
232 if (!fork_exec_with_pipes (FALSE,
236 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
237 (flags & G_SPAWN_SEARCH_PATH) != 0,
238 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
239 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
240 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
241 (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
246 standard_output ? &outpipe : NULL,
247 standard_error ? &errpipe : NULL,
251 /* Read data from child. */
257 outstr = g_string_new ("");
262 errstr = g_string_new ("");
265 /* Read data until we get EOF on both pipes. */
274 FD_SET (outpipe, &fds);
276 FD_SET (errpipe, &fds);
278 ret = select (MAX (outpipe, errpipe) + 1,
281 NULL /* no timeout */);
283 if (ret < 0 && errno != EINTR)
290 _("Unexpected error in select() reading data from a child process (%s)"),
296 if (outpipe >= 0 && FD_ISSET (outpipe, &fds))
298 switch (read_data (outstr, outpipe, error))
304 close_and_invalidate (&outpipe);
315 if (errpipe >= 0 && FD_ISSET (errpipe, &fds))
317 switch (read_data (errstr, errpipe, error))
323 close_and_invalidate (&errpipe);
335 /* These should only be open still if we had an error. */
338 close_and_invalidate (&outpipe);
340 close_and_invalidate (&errpipe);
342 /* Wait for child to exit, even if we have
347 ret = waitpid (pid, &status, 0);
353 else if (errno == ECHILD)
357 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.");
361 /* We don't need the exit status. */
366 if (!failed) /* avoid error pileups */
373 _("Unexpected error in waitpid() (%s)"),
382 g_string_free (outstr, TRUE);
384 g_string_free (errstr, TRUE);
391 *exit_status = status;
394 *standard_output = g_string_free (outstr, FALSE);
397 *standard_error = g_string_free (errstr, FALSE);
404 * g_spawn_async_with_pipes:
405 * @working_directory: child's current working directory, or NULL to inherit parent's
406 * @argv: child's argument vector
407 * @envp: child's environment, or NULL to inherit parent's
408 * @flags: flags from #GSpawnFlags
409 * @child_setup: function to run in the child just before exec()
410 * @user_data: user data for @child_setup
411 * @child_pid: return location for child process ID, or NULL
412 * @standard_input: return location for file descriptor to write to child's stdin, or NULL
413 * @standard_output: return location for file descriptor to read child's stdout, or NULL
414 * @standard_error: return location for file descriptor to read child's stderr, or NULL
415 * @error: return location for error
417 * Executes a child program asynchronously (your program will not
418 * block waiting for the child to exit). The child program is
419 * specified by the only argument that must be provided, @argv. @argv
420 * should be a NULL-terminated array of strings, to be passed as the
421 * argument vector for the child. The first string in @argv is of
422 * course the name of the program to execute. By default, the name of
423 * the program must be a full path; the PATH shell variable will only
424 * be searched if you pass the %G_SPAWN_SEARCH_PATH flag.
426 * @envp is a NULL-terminated array of strings, where each string
427 * has the form <literal>KEY=VALUE</literal>. This will become
428 * the child's environment. If @envp is NULL, the child inherits its
429 * parent's environment.
431 * @flags should be the bitwise OR of any flags you want to affect the
432 * function's behavior. The %G_SPAWN_DO_NOT_REAP_CHILD means that the
433 * child will not be automatically reaped; you must call waitpid() or
434 * handle SIGCHLD yourself, or the child will become a zombie.
435 * %G_SPAWN_LEAVE_DESCRIPTORS_OPEN means that the parent's open file
436 * descriptors will be inherited by the child; otherwise all
437 * descriptors except stdin/stdout/stderr will be closed before
438 * calling exec() in the child. %G_SPAWN_SEARCH_PATH means that
439 * <literal>argv[0]</literal> need not be an absolute path, it
440 * will be looked for in the user's PATH. %G_SPAWN_STDOUT_TO_DEV_NULL
441 * means that the child's standad output will be discarded, instead
442 * of going to the same location as the parent's standard output.
443 * %G_SPAWN_STDERR_TO_DEV_NULL means that the child's standard error
444 * will be discarded. %G_SPAWN_CHILD_INHERITS_STDIN means that
445 * the child will inherit the parent's standard input (by default,
446 * the child's standard input is attached to /dev/null).
447 * %G_SPAWN_FILE_AND_ARGV_ZERO means that the first element of @argv is
448 * the file to execute, while the remaining elements are the
449 * actual argument vector to pass to the file. Normally
450 * g_spawn_async_with_pipes() uses @argv[0] as the file to execute, and
451 * passes all of @argv to the child.
453 * @child_setup and @user_data are a function and user data to be
454 * called in the child after GLib has performed all the setup it plans
455 * to perform (including creating pipes, closing file descriptors,
456 * etc.) but before calling exec(). That is, @child_setup is called
457 * just before calling exec() in the child. Obviously actions taken in
458 * this function will only affect the child, not the parent.
460 * If non-NULL, @child_pid will be filled with the child's process
461 * ID. You can use the process ID to send signals to the child, or
462 * to waitpid() if you specified the %G_SPAWN_DO_NOT_REAP_CHILD flag.
464 * If non-NULL, the @standard_input, @standard_output, @standard_error
465 * locations will be filled with file descriptors for writing to the child's
466 * standard input or reading from its standard output or standard error.
467 * The caller of g_spawn_async_with_pipes() must close these file descriptors
468 * when they are no longer in use. If these parameters are NULL, the
469 * corresponding pipe won't be created.
471 * @error can be NULL to ignore errors, or non-NULL to report errors.
472 * If an error is set, the function returns FALSE. Errors
473 * are reported even if they occur in the child (for example if the
474 * executable in <literal>argv[0]</literal> is not found). Typically
475 * the <literal>message</literal> field of returned errors should be displayed
476 * to users. Possible errors are those from the #G_SPAWN_ERROR domain.
478 * If an error occurs, @child_pid, @standard_input, @standard_output,
479 * and @standard_error will not be filled with valid values.
481 * Return value: TRUE on success, FALSE if an error was set
484 g_spawn_async_with_pipes (const gchar *working_directory,
488 GSpawnChildSetupFunc child_setup,
491 gint *standard_input,
492 gint *standard_output,
493 gint *standard_error,
496 g_return_val_if_fail (argv != NULL, FALSE);
497 g_return_val_if_fail (standard_output == NULL ||
498 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
499 g_return_val_if_fail (standard_error == NULL ||
500 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
501 /* can't inherit stdin if we have an input pipe. */
502 g_return_val_if_fail (standard_input == NULL ||
503 !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
505 return fork_exec_with_pipes (!(flags & G_SPAWN_DO_NOT_REAP_CHILD),
509 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
510 (flags & G_SPAWN_SEARCH_PATH) != 0,
511 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
512 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
513 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
514 (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
525 * g_spawn_command_line_sync:
526 * @command_line: a command line
527 * @standard_output: return location for child output
528 * @standard_error: return location for child errors
529 * @exit_status: return location for child exit status
530 * @error: return location for errors
532 * A simple version of g_spawn_sync() with little-used parameters
533 * removed, taking a command line instead of an argument vector. See
534 * g_spawn_sync() for full details. @command_line will be parsed by
535 * g_shell_parse_argv(). Unlike g_spawn_sync(), the %G_SPAWN_SEARCH_PATH flag
536 * is enabled. Note that %G_SPAWN_SEARCH_PATH can have security
537 * implications, so consider using g_spawn_sync() directly if
538 * appropriate. Possible errors are those from g_spawn_sync() and those
539 * from g_shell_parse_argv().
541 * Return value: TRUE on success, FALSE if an error was set
544 g_spawn_command_line_sync (const gchar *command_line,
545 gchar **standard_output,
546 gchar **standard_error,
553 g_return_val_if_fail (command_line != NULL, FALSE);
555 if (!g_shell_parse_argv (command_line,
560 retval = g_spawn_sync (NULL,
576 * g_spawn_command_line_async:
577 * @command_line: a command line
578 * @error: return location for errors
580 * A simple version of g_spawn_async() that parses a command line with
581 * g_shell_parse_argv() and passes it to g_spawn_async(). Runs a
582 * command line in the background. Unlike g_spawn_async(), the
583 * %G_SPAWN_SEARCH_PATH flag is enabled, other flags are not. Note
584 * that %G_SPAWN_SEARCH_PATH can have security implications, so
585 * consider using g_spawn_async() directly if appropriate. Possible
586 * errors are those from g_shell_parse_argv() and g_spawn_async().
588 * Return value: TRUE on success, FALSE if error is set.
591 g_spawn_command_line_async (const gchar *command_line,
597 g_return_val_if_fail (command_line != NULL, FALSE);
599 if (!g_shell_parse_argv (command_line,
604 retval = g_spawn_async (NULL,
618 exec_err_to_g_error (gint en)
624 return G_SPAWN_ERROR_ACCES;
630 return G_SPAWN_ERROR_PERM;
636 return G_SPAWN_ERROR_2BIG;
642 return G_SPAWN_ERROR_NOEXEC;
648 return G_SPAWN_ERROR_NAMETOOLONG;
654 return G_SPAWN_ERROR_NOENT;
660 return G_SPAWN_ERROR_NOMEM;
666 return G_SPAWN_ERROR_NOTDIR;
672 return G_SPAWN_ERROR_LOOP;
678 return G_SPAWN_ERROR_TXTBUSY;
684 return G_SPAWN_ERROR_IO;
690 return G_SPAWN_ERROR_NFILE;
696 return G_SPAWN_ERROR_MFILE;
702 return G_SPAWN_ERROR_INVAL;
708 return G_SPAWN_ERROR_ISDIR;
714 return G_SPAWN_ERROR_LIBBAD;
719 return G_SPAWN_ERROR_FAILED;
725 write_err_and_exit (gint fd, gint msg)
729 write (fd, &msg, sizeof(msg));
730 write (fd, &en, sizeof(en));
736 set_cloexec (gint fd)
738 fcntl (fd, F_SETFD, FD_CLOEXEC);
742 sane_dup2 (gint fd1, gint fd2)
747 ret = dup2 (fd1, fd2);
748 if (ret < 0 && errno == EINTR)
763 do_exec (gint child_err_report_fd,
767 const gchar *working_directory,
770 gboolean close_descriptors,
771 gboolean search_path,
772 gboolean stdout_to_null,
773 gboolean stderr_to_null,
774 gboolean child_inherits_stdin,
775 gboolean file_and_argv_zero,
776 GSpawnChildSetupFunc child_setup,
779 if (working_directory && chdir (working_directory) < 0)
780 write_err_and_exit (child_err_report_fd,
783 /* Close all file descriptors but stdin stdout and stderr as
784 * soon as we exec. Note that this includes
785 * child_err_report_fd, which keeps the parent from blocking
786 * forever on the other end of that pipe.
788 if (close_descriptors)
793 open_max = sysconf (_SC_OPEN_MAX);
794 for (i = 3; i < open_max; i++)
799 /* We need to do child_err_report_fd anyway */
800 set_cloexec (child_err_report_fd);
803 /* Redirect pipes as required */
807 /* dup2 can't actually fail here I don't think */
809 if (sane_dup2 (stdin_fd, 0) < 0)
810 write_err_and_exit (child_err_report_fd,
813 /* ignore this if it doesn't work */
814 close_and_invalidate (&stdin_fd);
816 else if (!child_inherits_stdin)
818 /* Keep process from blocking on a read of stdin */
819 gint read_null = open ("/dev/null", O_RDONLY);
820 sane_dup2 (read_null, 0);
821 close_and_invalidate (&read_null);
826 /* dup2 can't actually fail here I don't think */
828 if (sane_dup2 (stdout_fd, 1) < 0)
829 write_err_and_exit (child_err_report_fd,
832 /* ignore this if it doesn't work */
833 close_and_invalidate (&stdout_fd);
835 else if (stdout_to_null)
837 gint write_null = open ("/dev/null", O_WRONLY);
838 sane_dup2 (write_null, 1);
839 close_and_invalidate (&write_null);
844 /* dup2 can't actually fail here I don't think */
846 if (sane_dup2 (stderr_fd, 2) < 0)
847 write_err_and_exit (child_err_report_fd,
850 /* ignore this if it doesn't work */
851 close_and_invalidate (&stderr_fd);
853 else if (stderr_to_null)
855 gint write_null = open ("/dev/null", O_WRONLY);
856 sane_dup2 (write_null, 2);
857 close_and_invalidate (&write_null);
860 /* Call user function just before we exec */
863 (* child_setup) (user_data);
867 file_and_argv_zero ? argv + 1 : argv,
871 write_err_and_exit (child_err_report_fd,
888 if (bytes >= sizeof(gint)*2)
889 break; /* give up, who knows what happened, should not be
895 ((gchar*)buf) + bytes,
896 sizeof(gint)*n_ints_in_buf - bytes);
897 if (chunk < 0 && errno == EINTR)
902 /* Some weird shit happened, bail out */
906 G_SPAWN_ERROR_FAILED,
907 _("Failed to read from child pipe (%s)"),
916 g_assert (chunk > 0);
922 *n_ints_read = bytes/4;
928 fork_exec_with_pipes (gboolean intermediate_child,
929 const gchar *working_directory,
932 gboolean close_descriptors,
933 gboolean search_path,
934 gboolean stdout_to_null,
935 gboolean stderr_to_null,
936 gboolean child_inherits_stdin,
937 gboolean file_and_argv_zero,
938 GSpawnChildSetupFunc child_setup,
941 gint *standard_input,
942 gint *standard_output,
943 gint *standard_error,
947 gint stdin_pipe[2] = { -1, -1 };
948 gint stdout_pipe[2] = { -1, -1 };
949 gint stderr_pipe[2] = { -1, -1 };
950 gint child_err_report_pipe[2] = { -1, -1 };
951 gint child_pid_report_pipe[2] = { -1, -1 };
954 if (!make_pipe (child_err_report_pipe, error))
957 if (intermediate_child && !make_pipe (child_pid_report_pipe, error))
958 goto cleanup_and_fail;
960 if (standard_input && !make_pipe (stdin_pipe, error))
961 goto cleanup_and_fail;
963 if (standard_output && !make_pipe (stdout_pipe, error))
964 goto cleanup_and_fail;
966 if (standard_error && !make_pipe (stderr_pipe, error))
967 goto cleanup_and_fail;
976 _("Failed to fork (%s)"),
979 goto cleanup_and_fail;
983 /* Immediate child. This may or may not be the child that
984 * actually execs the new process.
987 /* Be sure we crash if the parent exits
988 * and we write to the err_report_pipe
990 signal (SIGPIPE, SIG_DFL);
992 /* Close the parent's end of the pipes;
993 * not needed in the close_descriptors case,
996 close_and_invalidate (&child_err_report_pipe[0]);
997 close_and_invalidate (&child_pid_report_pipe[0]);
998 close_and_invalidate (&stdin_pipe[1]);
999 close_and_invalidate (&stdout_pipe[0]);
1000 close_and_invalidate (&stderr_pipe[0]);
1002 if (intermediate_child)
1004 /* We need to fork an intermediate child that launches the
1005 * final child. The purpose of the intermediate child
1006 * is to exit, so we can waitpid() it immediately.
1007 * Then the grandchild will not become a zombie.
1009 gint grandchild_pid;
1011 grandchild_pid = fork ();
1013 if (grandchild_pid < 0)
1015 /* report -1 as child PID */
1016 write (child_pid_report_pipe[1], &grandchild_pid,
1017 sizeof(grandchild_pid));
1019 write_err_and_exit (child_err_report_pipe[1],
1022 else if (grandchild_pid == 0)
1024 do_exec (child_err_report_pipe[1],
1035 child_inherits_stdin,
1042 write (child_pid_report_pipe[1], &grandchild_pid, sizeof(grandchild_pid));
1043 close_and_invalidate (&child_pid_report_pipe[1]);
1050 /* Just run the child.
1053 do_exec (child_err_report_pipe[1],
1064 child_inherits_stdin,
1077 /* Close the uncared-about ends of the pipes */
1078 close_and_invalidate (&child_err_report_pipe[1]);
1079 close_and_invalidate (&child_pid_report_pipe[1]);
1080 close_and_invalidate (&stdin_pipe[0]);
1081 close_and_invalidate (&stdout_pipe[1]);
1082 close_and_invalidate (&stderr_pipe[1]);
1084 /* If we had an intermediate child, reap it */
1085 if (intermediate_child)
1088 if (waitpid (pid, &status, 0) < 0)
1092 else if (errno == ECHILD)
1093 ; /* do nothing, child already reaped */
1095 g_warning ("waitpid() should not fail in "
1096 "'fork_exec_with_pipes'");
1101 if (!read_ints (child_err_report_pipe[0],
1104 goto cleanup_and_fail;
1108 /* Error from the child. */
1112 case CHILD_CHDIR_FAILED:
1115 G_SPAWN_ERROR_CHDIR,
1116 _("Failed to change to directory '%s' (%s)"),
1118 g_strerror (buf[1]));
1122 case CHILD_EXEC_FAILED:
1125 exec_err_to_g_error (buf[1]),
1126 _("Failed to execute child process (%s)"),
1127 g_strerror (buf[1]));
1131 case CHILD_DUP2_FAILED:
1134 G_SPAWN_ERROR_FAILED,
1135 _("Failed to redirect output or input of child process (%s)"),
1136 g_strerror (buf[1]));
1140 case CHILD_FORK_FAILED:
1144 _("Failed to fork child process (%s)"),
1145 g_strerror (buf[1]));
1151 G_SPAWN_ERROR_FAILED,
1152 _("Unknown error executing child process"));
1156 goto cleanup_and_fail;
1159 /* Get child pid from intermediate child pipe. */
1160 if (intermediate_child)
1164 if (!read_ints (child_pid_report_pipe[0],
1165 buf, 1, &n_ints, error))
1166 goto cleanup_and_fail;
1172 G_SPAWN_ERROR_FAILED,
1173 _("Failed to read enough data from child pid pipe (%s)"),
1174 g_strerror (errno));
1175 goto cleanup_and_fail;
1179 /* we have the child pid */
1184 /* Success against all odds! return the information */
1190 *standard_input = stdin_pipe[1];
1191 if (standard_output)
1192 *standard_output = stdout_pipe[0];
1194 *standard_error = stderr_pipe[0];
1200 close_and_invalidate (&child_err_report_pipe[0]);
1201 close_and_invalidate (&child_err_report_pipe[1]);
1202 close_and_invalidate (&child_pid_report_pipe[0]);
1203 close_and_invalidate (&child_pid_report_pipe[1]);
1204 close_and_invalidate (&stdin_pipe[0]);
1205 close_and_invalidate (&stdin_pipe[1]);
1206 close_and_invalidate (&stdout_pipe[0]);
1207 close_and_invalidate (&stdout_pipe[1]);
1208 close_and_invalidate (&stderr_pipe[0]);
1209 close_and_invalidate (&stderr_pipe[1]);
1215 make_pipe (gint p[2],
1222 G_SPAWN_ERROR_FAILED,
1223 _("Failed to create pipe for communicating with child process (%s)"),
1224 g_strerror (errno));
1231 /* Based on execvp from GNU C Library */
1234 script_execute (const gchar *file,
1237 gboolean search_path)
1239 /* Count the arguments. */
1244 /* Construct an argument list for the shell. */
1248 new_argv = g_new0 (gchar*, argc + 1);
1250 new_argv[0] = (char *) "/bin/sh";
1251 new_argv[1] = (char *) file;
1254 new_argv[argc] = argv[argc - 1];
1258 /* Execute the shell. */
1260 execve (new_argv[0], new_argv, envp);
1262 execv (new_argv[0], new_argv);
1269 my_strchrnul (const gchar *str, gchar c)
1271 gchar *p = (gchar*) str;
1272 while (*p && (*p != c))
1279 g_execute (const gchar *file,
1282 gboolean search_path)
1286 /* We check the simple case first. */
1291 if (!search_path || strchr (file, '/') != NULL)
1293 /* Don't search when it contains a slash. */
1295 execve (file, argv, envp);
1299 if (errno == ENOEXEC)
1300 script_execute (file, argv, envp, FALSE);
1304 gboolean got_eacces = 0;
1305 const gchar *path, *p;
1306 gchar *name, *freeme;
1310 path = g_getenv ("PATH");
1313 /* There is no `PATH' in the environment. The default
1314 * search path in libc is the current directory followed by
1315 * the path `confstr' returns for `_CS_PATH'.
1318 /* In GLib we put . last, for security, and don't use the
1319 * unportable confstr(); UNIX98 does not actually specify
1320 * what to search if PATH is unset. POSIX may, dunno.
1323 path = "/bin:/usr/bin:.";
1326 len = strlen (file) + 1;
1327 pathlen = strlen (path);
1328 freeme = name = g_malloc (pathlen + len + 1);
1330 /* Copy the file name at the top, including '\0' */
1331 memcpy (name + pathlen + 1, file, len);
1332 name = name + pathlen;
1333 /* And add the slash before the filename */
1342 p = my_strchrnul (path, ':');
1345 /* Two adjacent colons, or a colon at the beginning or the end
1346 * of `PATH' means to search the current directory.
1350 startp = memcpy (name - (p - path), path, p - path);
1352 /* Try to execute this name. If it works, execv will not return. */
1354 execve (startp, argv, envp);
1356 execv (startp, argv);
1358 if (errno == ENOEXEC)
1359 script_execute (startp, argv, envp, search_path);
1364 /* Record the we got a `Permission denied' error. If we end
1365 * up finding no executable we can use, we want to diagnose
1366 * that we did find one but were denied access.
1379 /* Those errors indicate the file is missing or not executable
1380 * by us, in which case we want to just try the next path
1386 /* Some other error means we found an executable file, but
1387 * something went wrong executing it; return the error to our
1394 while (*p++ != '\0');
1396 /* We tried every element and none of them worked. */
1398 /* At least one failure was due to permissions, so report that
1406 /* Return the error from the last attempt (probably ENOENT). */