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 standad output will
451 * be discarded, instead of going to the same location as the parent's
453 * %G_SPAWN_STDERR_TO_DEV_NULL means that the child's standard error
454 * will be discarded. %G_SPAWN_CHILD_INHERITS_STDIN means that
455 * the child will inherit the parent's standard input (by default,
456 * the child's standard input is attached to /dev/null).
457 * %G_SPAWN_FILE_AND_ARGV_ZERO means that the first element of @argv is
458 * the file to execute, while the remaining elements are the
459 * actual argument vector to pass to the file. Normally
460 * g_spawn_async_with_pipes() uses @argv[0] as the file to execute, and
461 * passes all of @argv to the child.
463 * @child_setup and @user_data are a function and user data to be
464 * called in the child after GLib has performed all the setup it plans
465 * to perform (including creating pipes, closing file descriptors,
466 * etc.) but before calling <function>exec()</function>. That is,
467 * @child_setup is called just before calling <function>exec()</function>
468 * in the child. Obviously actions taken in this function will only affect
469 * the child, not the parent.
471 * If non-%NULL, @child_pid will be filled with the child's process
472 * ID. You can use the process ID to send signals to the child, or
473 * to <function>waitpid()</function> if you specified the
474 * %G_SPAWN_DO_NOT_REAP_CHILD flag.
476 * If non-%NULL, the @standard_input, @standard_output, @standard_error
477 * locations will be filled with file descriptors for writing to the child's
478 * standard input or reading from its standard output or standard error.
479 * The caller of g_spawn_async_with_pipes() must close these file descriptors
480 * when they are no longer in use. If these parameters are %NULL, the
481 * corresponding pipe won't be created.
483 * @error can be %NULL to ignore errors, or non-%NULL to report errors.
484 * If an error is set, the function returns %FALSE. Errors
485 * are reported even if they occur in the child (for example if the
486 * executable in <literal>argv[0]</literal> is not found). Typically
487 * the <literal>message</literal> field of returned errors should be displayed
488 * to users. Possible errors are those from the #G_SPAWN_ERROR domain.
490 * If an error occurs, @child_pid, @standard_input, @standard_output,
491 * and @standard_error will not be filled with valid values.
493 * Return value: %TRUE on success, %FALSE if an error was set
496 g_spawn_async_with_pipes (const gchar *working_directory,
500 GSpawnChildSetupFunc child_setup,
503 gint *standard_input,
504 gint *standard_output,
505 gint *standard_error,
508 g_return_val_if_fail (argv != NULL, FALSE);
509 g_return_val_if_fail (standard_output == NULL ||
510 !(flags & G_SPAWN_STDOUT_TO_DEV_NULL), FALSE);
511 g_return_val_if_fail (standard_error == NULL ||
512 !(flags & G_SPAWN_STDERR_TO_DEV_NULL), FALSE);
513 /* can't inherit stdin if we have an input pipe. */
514 g_return_val_if_fail (standard_input == NULL ||
515 !(flags & G_SPAWN_CHILD_INHERITS_STDIN), FALSE);
517 return fork_exec_with_pipes (!(flags & G_SPAWN_DO_NOT_REAP_CHILD),
521 !(flags & G_SPAWN_LEAVE_DESCRIPTORS_OPEN),
522 (flags & G_SPAWN_SEARCH_PATH) != 0,
523 (flags & G_SPAWN_STDOUT_TO_DEV_NULL) != 0,
524 (flags & G_SPAWN_STDERR_TO_DEV_NULL) != 0,
525 (flags & G_SPAWN_CHILD_INHERITS_STDIN) != 0,
526 (flags & G_SPAWN_FILE_AND_ARGV_ZERO) != 0,
537 * g_spawn_command_line_sync:
538 * @command_line: a command line
539 * @standard_output: return location for child output
540 * @standard_error: return location for child errors
541 * @exit_status: return location for child exit status
542 * @error: return location for errors
544 * A simple version of g_spawn_sync() with little-used parameters
545 * removed, taking a command line instead of an argument vector. See
546 * g_spawn_sync() for full details. @command_line will be parsed by
547 * g_shell_parse_argv(). Unlike g_spawn_sync(), the %G_SPAWN_SEARCH_PATH flag
548 * is enabled. Note that %G_SPAWN_SEARCH_PATH can have security
549 * implications, so consider using g_spawn_sync() directly if
550 * appropriate. Possible errors are those from g_spawn_sync() and those
551 * from g_shell_parse_argv().
553 * On Windows, please note the implications of g_shell_parse_argv()
554 * parsing @command_line. Space is a separator, and backslashes are
555 * special. Thus you cannot simply pass a @command_line consisting of
556 * a canonical Windows path, like "c:\\program files\\app\\app.exe",
557 * as the backslashes will be eaten, and the space will act as a
558 * separator. You need to enclose the path with single quotes, like
559 * "'c:\\program files\\app\\app.exe'".
561 * Return value: %TRUE on success, %FALSE if an error was set
564 g_spawn_command_line_sync (const gchar *command_line,
565 gchar **standard_output,
566 gchar **standard_error,
573 g_return_val_if_fail (command_line != NULL, FALSE);
575 if (!g_shell_parse_argv (command_line,
580 retval = g_spawn_sync (NULL,
596 * g_spawn_command_line_async:
597 * @command_line: a command line
598 * @error: return location for errors
600 * A simple version of g_spawn_async() that parses a command line with
601 * g_shell_parse_argv() and passes it to g_spawn_async(). Runs a
602 * command line in the background. Unlike g_spawn_async(), the
603 * %G_SPAWN_SEARCH_PATH flag is enabled, other flags are not. Note
604 * that %G_SPAWN_SEARCH_PATH can have security implications, so
605 * consider using g_spawn_async() directly if appropriate. Possible
606 * errors are those from g_shell_parse_argv() and g_spawn_async().
608 * The same concerns on Windows apply as for g_spawn_command_line_sync().
610 * Return value: %TRUE on success, %FALSE if error is set.
613 g_spawn_command_line_async (const gchar *command_line,
619 g_return_val_if_fail (command_line != NULL, FALSE);
621 if (!g_shell_parse_argv (command_line,
626 retval = g_spawn_async (NULL,
640 exec_err_to_g_error (gint en)
646 return G_SPAWN_ERROR_ACCES;
652 return G_SPAWN_ERROR_PERM;
658 return G_SPAWN_ERROR_2BIG;
664 return G_SPAWN_ERROR_NOEXEC;
670 return G_SPAWN_ERROR_NAMETOOLONG;
676 return G_SPAWN_ERROR_NOENT;
682 return G_SPAWN_ERROR_NOMEM;
688 return G_SPAWN_ERROR_NOTDIR;
694 return G_SPAWN_ERROR_LOOP;
700 return G_SPAWN_ERROR_TXTBUSY;
706 return G_SPAWN_ERROR_IO;
712 return G_SPAWN_ERROR_NFILE;
718 return G_SPAWN_ERROR_MFILE;
724 return G_SPAWN_ERROR_INVAL;
730 return G_SPAWN_ERROR_ISDIR;
736 return G_SPAWN_ERROR_LIBBAD;
741 return G_SPAWN_ERROR_FAILED;
747 write_err_and_exit (gint fd, gint msg)
751 write (fd, &msg, sizeof(msg));
752 write (fd, &en, sizeof(en));
758 set_cloexec (gint fd)
760 fcntl (fd, F_SETFD, FD_CLOEXEC);
764 sane_dup2 (gint fd1, gint fd2)
769 ret = dup2 (fd1, fd2);
770 if (ret < 0 && errno == EINTR)
785 do_exec (gint child_err_report_fd,
789 const gchar *working_directory,
792 gboolean close_descriptors,
793 gboolean search_path,
794 gboolean stdout_to_null,
795 gboolean stderr_to_null,
796 gboolean child_inherits_stdin,
797 gboolean file_and_argv_zero,
798 GSpawnChildSetupFunc child_setup,
801 if (working_directory && chdir (working_directory) < 0)
802 write_err_and_exit (child_err_report_fd,
805 /* Close all file descriptors but stdin stdout and stderr as
806 * soon as we exec. Note that this includes
807 * child_err_report_fd, which keeps the parent from blocking
808 * forever on the other end of that pipe.
810 if (close_descriptors)
815 open_max = sysconf (_SC_OPEN_MAX);
816 for (i = 3; i < open_max; i++)
821 /* We need to do child_err_report_fd anyway */
822 set_cloexec (child_err_report_fd);
825 /* Redirect pipes as required */
829 /* dup2 can't actually fail here I don't think */
831 if (sane_dup2 (stdin_fd, 0) < 0)
832 write_err_and_exit (child_err_report_fd,
835 /* ignore this if it doesn't work */
836 close_and_invalidate (&stdin_fd);
838 else if (!child_inherits_stdin)
840 /* Keep process from blocking on a read of stdin */
841 gint read_null = open ("/dev/null", O_RDONLY);
842 sane_dup2 (read_null, 0);
843 close_and_invalidate (&read_null);
848 /* dup2 can't actually fail here I don't think */
850 if (sane_dup2 (stdout_fd, 1) < 0)
851 write_err_and_exit (child_err_report_fd,
854 /* ignore this if it doesn't work */
855 close_and_invalidate (&stdout_fd);
857 else if (stdout_to_null)
859 gint write_null = open ("/dev/null", O_WRONLY);
860 sane_dup2 (write_null, 1);
861 close_and_invalidate (&write_null);
866 /* dup2 can't actually fail here I don't think */
868 if (sane_dup2 (stderr_fd, 2) < 0)
869 write_err_and_exit (child_err_report_fd,
872 /* ignore this if it doesn't work */
873 close_and_invalidate (&stderr_fd);
875 else if (stderr_to_null)
877 gint write_null = open ("/dev/null", O_WRONLY);
878 sane_dup2 (write_null, 2);
879 close_and_invalidate (&write_null);
882 /* Call user function just before we exec */
885 (* child_setup) (user_data);
889 file_and_argv_zero ? argv + 1 : argv,
893 write_err_and_exit (child_err_report_fd,
910 if (bytes >= sizeof(gint)*2)
911 break; /* give up, who knows what happened, should not be
917 ((gchar*)buf) + bytes,
918 sizeof(gint) * n_ints_in_buf - bytes);
919 if (chunk < 0 && errno == EINTR)
924 /* Some weird shit happened, bail out */
928 G_SPAWN_ERROR_FAILED,
929 _("Failed to read from child pipe (%s)"),
940 *n_ints_read = (gint)(bytes / sizeof(gint));
946 fork_exec_with_pipes (gboolean intermediate_child,
947 const gchar *working_directory,
950 gboolean close_descriptors,
951 gboolean search_path,
952 gboolean stdout_to_null,
953 gboolean stderr_to_null,
954 gboolean child_inherits_stdin,
955 gboolean file_and_argv_zero,
956 GSpawnChildSetupFunc child_setup,
959 gint *standard_input,
960 gint *standard_output,
961 gint *standard_error,
965 gint stdin_pipe[2] = { -1, -1 };
966 gint stdout_pipe[2] = { -1, -1 };
967 gint stderr_pipe[2] = { -1, -1 };
968 gint child_err_report_pipe[2] = { -1, -1 };
969 gint child_pid_report_pipe[2] = { -1, -1 };
972 if (!make_pipe (child_err_report_pipe, error))
975 if (intermediate_child && !make_pipe (child_pid_report_pipe, error))
976 goto cleanup_and_fail;
978 if (standard_input && !make_pipe (stdin_pipe, error))
979 goto cleanup_and_fail;
981 if (standard_output && !make_pipe (stdout_pipe, error))
982 goto cleanup_and_fail;
984 if (standard_error && !make_pipe (stderr_pipe, error))
985 goto cleanup_and_fail;
994 _("Failed to fork (%s)"),
997 goto cleanup_and_fail;
1001 /* Immediate child. This may or may not be the child that
1002 * actually execs the new process.
1005 /* Be sure we crash if the parent exits
1006 * and we write to the err_report_pipe
1008 signal (SIGPIPE, SIG_DFL);
1010 /* Close the parent's end of the pipes;
1011 * not needed in the close_descriptors case,
1014 close_and_invalidate (&child_err_report_pipe[0]);
1015 close_and_invalidate (&child_pid_report_pipe[0]);
1016 close_and_invalidate (&stdin_pipe[1]);
1017 close_and_invalidate (&stdout_pipe[0]);
1018 close_and_invalidate (&stderr_pipe[0]);
1020 if (intermediate_child)
1022 /* We need to fork an intermediate child that launches the
1023 * final child. The purpose of the intermediate child
1024 * is to exit, so we can waitpid() it immediately.
1025 * Then the grandchild will not become a zombie.
1027 gint grandchild_pid;
1029 grandchild_pid = fork ();
1031 if (grandchild_pid < 0)
1033 /* report -1 as child PID */
1034 write (child_pid_report_pipe[1], &grandchild_pid,
1035 sizeof(grandchild_pid));
1037 write_err_and_exit (child_err_report_pipe[1],
1040 else if (grandchild_pid == 0)
1042 do_exec (child_err_report_pipe[1],
1053 child_inherits_stdin,
1060 write (child_pid_report_pipe[1], &grandchild_pid, sizeof(grandchild_pid));
1061 close_and_invalidate (&child_pid_report_pipe[1]);
1068 /* Just run the child.
1071 do_exec (child_err_report_pipe[1],
1082 child_inherits_stdin,
1095 /* Close the uncared-about ends of the pipes */
1096 close_and_invalidate (&child_err_report_pipe[1]);
1097 close_and_invalidate (&child_pid_report_pipe[1]);
1098 close_and_invalidate (&stdin_pipe[0]);
1099 close_and_invalidate (&stdout_pipe[1]);
1100 close_and_invalidate (&stderr_pipe[1]);
1102 /* If we had an intermediate child, reap it */
1103 if (intermediate_child)
1106 if (waitpid (pid, &status, 0) < 0)
1110 else if (errno == ECHILD)
1111 ; /* do nothing, child already reaped */
1113 g_warning ("waitpid() should not fail in "
1114 "'fork_exec_with_pipes'");
1119 if (!read_ints (child_err_report_pipe[0],
1122 goto cleanup_and_fail;
1126 /* Error from the child. */
1130 case CHILD_CHDIR_FAILED:
1133 G_SPAWN_ERROR_CHDIR,
1134 _("Failed to change to directory '%s' (%s)"),
1136 g_strerror (buf[1]));
1140 case CHILD_EXEC_FAILED:
1143 exec_err_to_g_error (buf[1]),
1144 _("Failed to execute child process \"%s\" (%s)"),
1146 g_strerror (buf[1]));
1150 case CHILD_DUP2_FAILED:
1153 G_SPAWN_ERROR_FAILED,
1154 _("Failed to redirect output or input of child process (%s)"),
1155 g_strerror (buf[1]));
1159 case CHILD_FORK_FAILED:
1163 _("Failed to fork child process (%s)"),
1164 g_strerror (buf[1]));
1170 G_SPAWN_ERROR_FAILED,
1171 _("Unknown error executing child process \"%s\""),
1176 goto cleanup_and_fail;
1179 /* Get child pid from intermediate child pipe. */
1180 if (intermediate_child)
1184 if (!read_ints (child_pid_report_pipe[0],
1185 buf, 1, &n_ints, error))
1186 goto cleanup_and_fail;
1192 G_SPAWN_ERROR_FAILED,
1193 _("Failed to read enough data from child pid pipe (%s)"),
1194 g_strerror (errno));
1195 goto cleanup_and_fail;
1199 /* we have the child pid */
1204 /* Success against all odds! return the information */
1205 close_and_invalidate (&child_err_report_pipe[0]);
1206 close_and_invalidate (&child_pid_report_pipe[0]);
1212 *standard_input = stdin_pipe[1];
1213 if (standard_output)
1214 *standard_output = stdout_pipe[0];
1216 *standard_error = stderr_pipe[0];
1222 close_and_invalidate (&child_err_report_pipe[0]);
1223 close_and_invalidate (&child_err_report_pipe[1]);
1224 close_and_invalidate (&child_pid_report_pipe[0]);
1225 close_and_invalidate (&child_pid_report_pipe[1]);
1226 close_and_invalidate (&stdin_pipe[0]);
1227 close_and_invalidate (&stdin_pipe[1]);
1228 close_and_invalidate (&stdout_pipe[0]);
1229 close_and_invalidate (&stdout_pipe[1]);
1230 close_and_invalidate (&stderr_pipe[0]);
1231 close_and_invalidate (&stderr_pipe[1]);
1237 make_pipe (gint p[2],
1244 G_SPAWN_ERROR_FAILED,
1245 _("Failed to create pipe for communicating with child process (%s)"),
1246 g_strerror (errno));
1253 /* Based on execvp from GNU C Library */
1256 script_execute (const gchar *file,
1259 gboolean search_path)
1261 /* Count the arguments. */
1266 /* Construct an argument list for the shell. */
1270 new_argv = g_new0 (gchar*, argc + 2); /* /bin/sh and NULL */
1272 new_argv[0] = (char *) "/bin/sh";
1273 new_argv[1] = (char *) file;
1276 new_argv[argc + 1] = argv[argc];
1280 /* Execute the shell. */
1282 execve (new_argv[0], new_argv, envp);
1284 execv (new_argv[0], new_argv);
1291 my_strchrnul (const gchar *str, gchar c)
1293 gchar *p = (gchar*) str;
1294 while (*p && (*p != c))
1301 g_execute (const gchar *file,
1304 gboolean search_path)
1308 /* We check the simple case first. */
1313 if (!search_path || strchr (file, '/') != NULL)
1315 /* Don't search when it contains a slash. */
1317 execve (file, argv, envp);
1321 if (errno == ENOEXEC)
1322 script_execute (file, argv, envp, FALSE);
1326 gboolean got_eacces = 0;
1327 const gchar *path, *p;
1328 gchar *name, *freeme;
1332 path = g_getenv ("PATH");
1335 /* There is no `PATH' in the environment. The default
1336 * search path in libc is the current directory followed by
1337 * the path `confstr' returns for `_CS_PATH'.
1340 /* In GLib we put . last, for security, and don't use the
1341 * unportable confstr(); UNIX98 does not actually specify
1342 * what to search if PATH is unset. POSIX may, dunno.
1345 path = "/bin:/usr/bin:.";
1348 len = strlen (file) + 1;
1349 pathlen = strlen (path);
1350 freeme = name = g_malloc (pathlen + len + 1);
1352 /* Copy the file name at the top, including '\0' */
1353 memcpy (name + pathlen + 1, file, len);
1354 name = name + pathlen;
1355 /* And add the slash before the filename */
1364 p = my_strchrnul (path, ':');
1367 /* Two adjacent colons, or a colon at the beginning or the end
1368 * of `PATH' means to search the current directory.
1372 startp = memcpy (name - (p - path), path, p - path);
1374 /* Try to execute this name. If it works, execv will not return. */
1376 execve (startp, argv, envp);
1378 execv (startp, argv);
1380 if (errno == ENOEXEC)
1381 script_execute (startp, argv, envp, search_path);
1386 /* Record the we got a `Permission denied' error. If we end
1387 * up finding no executable we can use, we want to diagnose
1388 * that we did find one but were denied access.
1401 /* Those errors indicate the file is missing or not executable
1402 * by us, in which case we want to just try the next path
1408 /* Some other error means we found an executable file, but
1409 * something went wrong executing it; return the error to our
1416 while (*p++ != '\0');
1418 /* We tried every element and none of them worked. */
1420 /* At least one failure was due to permissions, so report that
1428 /* Return the error from the last attempt (probably ENOENT). */