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 * On Windows, please note the implications of g_shell_parse_argv()
549 * parsing @command_line. Space is a separator, and backslashes are
550 * special. Thus you cannot simply pass a @command_line consisting of
551 * a canonical Windows path, like "c:\\program files\\app\\app.exe",
552 * as the backslashes will be eaten, and the space will act as a
553 * separator. You need to enclose the path with single quotes, like
554 * "'c:\\program files\\app\\app.exe'".
556 * Return value: %TRUE on success, %FALSE if an error was set
559 g_spawn_command_line_sync (const gchar *command_line,
560 gchar **standard_output,
561 gchar **standard_error,
568 g_return_val_if_fail (command_line != NULL, FALSE);
570 if (!g_shell_parse_argv (command_line,
575 retval = g_spawn_sync (NULL,
591 * g_spawn_command_line_async:
592 * @command_line: a command line
593 * @error: return location for errors
595 * A simple version of g_spawn_async() that parses a command line with
596 * g_shell_parse_argv() and passes it to g_spawn_async(). Runs a
597 * command line in the background. Unlike g_spawn_async(), the
598 * %G_SPAWN_SEARCH_PATH flag is enabled, other flags are not. Note
599 * that %G_SPAWN_SEARCH_PATH can have security implications, so
600 * consider using g_spawn_async() directly if appropriate. Possible
601 * errors are those from g_shell_parse_argv() and g_spawn_async().
603 * The same concerns on Windows apply as for g_spawn_command_line_sync().
605 * Return value: %TRUE on success, %FALSE if error is set.
608 g_spawn_command_line_async (const gchar *command_line,
614 g_return_val_if_fail (command_line != NULL, FALSE);
616 if (!g_shell_parse_argv (command_line,
621 retval = g_spawn_async (NULL,
635 exec_err_to_g_error (gint en)
641 return G_SPAWN_ERROR_ACCES;
647 return G_SPAWN_ERROR_PERM;
653 return G_SPAWN_ERROR_2BIG;
659 return G_SPAWN_ERROR_NOEXEC;
665 return G_SPAWN_ERROR_NAMETOOLONG;
671 return G_SPAWN_ERROR_NOENT;
677 return G_SPAWN_ERROR_NOMEM;
683 return G_SPAWN_ERROR_NOTDIR;
689 return G_SPAWN_ERROR_LOOP;
695 return G_SPAWN_ERROR_TXTBUSY;
701 return G_SPAWN_ERROR_IO;
707 return G_SPAWN_ERROR_NFILE;
713 return G_SPAWN_ERROR_MFILE;
719 return G_SPAWN_ERROR_INVAL;
725 return G_SPAWN_ERROR_ISDIR;
731 return G_SPAWN_ERROR_LIBBAD;
736 return G_SPAWN_ERROR_FAILED;
742 write_err_and_exit (gint fd, gint msg)
746 write (fd, &msg, sizeof(msg));
747 write (fd, &en, sizeof(en));
753 set_cloexec (gint fd)
755 fcntl (fd, F_SETFD, FD_CLOEXEC);
759 sane_dup2 (gint fd1, gint fd2)
764 ret = dup2 (fd1, fd2);
765 if (ret < 0 && errno == EINTR)
780 do_exec (gint child_err_report_fd,
784 const gchar *working_directory,
787 gboolean close_descriptors,
788 gboolean search_path,
789 gboolean stdout_to_null,
790 gboolean stderr_to_null,
791 gboolean child_inherits_stdin,
792 gboolean file_and_argv_zero,
793 GSpawnChildSetupFunc child_setup,
796 if (working_directory && chdir (working_directory) < 0)
797 write_err_and_exit (child_err_report_fd,
800 /* Close all file descriptors but stdin stdout and stderr as
801 * soon as we exec. Note that this includes
802 * child_err_report_fd, which keeps the parent from blocking
803 * forever on the other end of that pipe.
805 if (close_descriptors)
810 open_max = sysconf (_SC_OPEN_MAX);
811 for (i = 3; i < open_max; i++)
816 /* We need to do child_err_report_fd anyway */
817 set_cloexec (child_err_report_fd);
820 /* Redirect pipes as required */
824 /* dup2 can't actually fail here I don't think */
826 if (sane_dup2 (stdin_fd, 0) < 0)
827 write_err_and_exit (child_err_report_fd,
830 /* ignore this if it doesn't work */
831 close_and_invalidate (&stdin_fd);
833 else if (!child_inherits_stdin)
835 /* Keep process from blocking on a read of stdin */
836 gint read_null = open ("/dev/null", O_RDONLY);
837 sane_dup2 (read_null, 0);
838 close_and_invalidate (&read_null);
843 /* dup2 can't actually fail here I don't think */
845 if (sane_dup2 (stdout_fd, 1) < 0)
846 write_err_and_exit (child_err_report_fd,
849 /* ignore this if it doesn't work */
850 close_and_invalidate (&stdout_fd);
852 else if (stdout_to_null)
854 gint write_null = open ("/dev/null", O_WRONLY);
855 sane_dup2 (write_null, 1);
856 close_and_invalidate (&write_null);
861 /* dup2 can't actually fail here I don't think */
863 if (sane_dup2 (stderr_fd, 2) < 0)
864 write_err_and_exit (child_err_report_fd,
867 /* ignore this if it doesn't work */
868 close_and_invalidate (&stderr_fd);
870 else if (stderr_to_null)
872 gint write_null = open ("/dev/null", O_WRONLY);
873 sane_dup2 (write_null, 2);
874 close_and_invalidate (&write_null);
877 /* Call user function just before we exec */
880 (* child_setup) (user_data);
884 file_and_argv_zero ? argv + 1 : argv,
888 write_err_and_exit (child_err_report_fd,
905 if (bytes >= sizeof(gint)*2)
906 break; /* give up, who knows what happened, should not be
912 ((gchar*)buf) + bytes,
913 sizeof(gint) * n_ints_in_buf - bytes);
914 if (chunk < 0 && errno == EINTR)
919 /* Some weird shit happened, bail out */
923 G_SPAWN_ERROR_FAILED,
924 _("Failed to read from child pipe (%s)"),
935 *n_ints_read = (gint)(bytes / sizeof(gint));
941 fork_exec_with_pipes (gboolean intermediate_child,
942 const gchar *working_directory,
945 gboolean close_descriptors,
946 gboolean search_path,
947 gboolean stdout_to_null,
948 gboolean stderr_to_null,
949 gboolean child_inherits_stdin,
950 gboolean file_and_argv_zero,
951 GSpawnChildSetupFunc child_setup,
954 gint *standard_input,
955 gint *standard_output,
956 gint *standard_error,
960 gint stdin_pipe[2] = { -1, -1 };
961 gint stdout_pipe[2] = { -1, -1 };
962 gint stderr_pipe[2] = { -1, -1 };
963 gint child_err_report_pipe[2] = { -1, -1 };
964 gint child_pid_report_pipe[2] = { -1, -1 };
967 if (!make_pipe (child_err_report_pipe, error))
970 if (intermediate_child && !make_pipe (child_pid_report_pipe, error))
971 goto cleanup_and_fail;
973 if (standard_input && !make_pipe (stdin_pipe, error))
974 goto cleanup_and_fail;
976 if (standard_output && !make_pipe (stdout_pipe, error))
977 goto cleanup_and_fail;
979 if (standard_error && !make_pipe (stderr_pipe, error))
980 goto cleanup_and_fail;
989 _("Failed to fork (%s)"),
992 goto cleanup_and_fail;
996 /* Immediate child. This may or may not be the child that
997 * actually execs the new process.
1000 /* Be sure we crash if the parent exits
1001 * and we write to the err_report_pipe
1003 signal (SIGPIPE, SIG_DFL);
1005 /* Close the parent's end of the pipes;
1006 * not needed in the close_descriptors case,
1009 close_and_invalidate (&child_err_report_pipe[0]);
1010 close_and_invalidate (&child_pid_report_pipe[0]);
1011 close_and_invalidate (&stdin_pipe[1]);
1012 close_and_invalidate (&stdout_pipe[0]);
1013 close_and_invalidate (&stderr_pipe[0]);
1015 if (intermediate_child)
1017 /* We need to fork an intermediate child that launches the
1018 * final child. The purpose of the intermediate child
1019 * is to exit, so we can waitpid() it immediately.
1020 * Then the grandchild will not become a zombie.
1022 gint grandchild_pid;
1024 grandchild_pid = fork ();
1026 if (grandchild_pid < 0)
1028 /* report -1 as child PID */
1029 write (child_pid_report_pipe[1], &grandchild_pid,
1030 sizeof(grandchild_pid));
1032 write_err_and_exit (child_err_report_pipe[1],
1035 else if (grandchild_pid == 0)
1037 do_exec (child_err_report_pipe[1],
1048 child_inherits_stdin,
1055 write (child_pid_report_pipe[1], &grandchild_pid, sizeof(grandchild_pid));
1056 close_and_invalidate (&child_pid_report_pipe[1]);
1063 /* Just run the child.
1066 do_exec (child_err_report_pipe[1],
1077 child_inherits_stdin,
1090 /* Close the uncared-about ends of the pipes */
1091 close_and_invalidate (&child_err_report_pipe[1]);
1092 close_and_invalidate (&child_pid_report_pipe[1]);
1093 close_and_invalidate (&stdin_pipe[0]);
1094 close_and_invalidate (&stdout_pipe[1]);
1095 close_and_invalidate (&stderr_pipe[1]);
1097 /* If we had an intermediate child, reap it */
1098 if (intermediate_child)
1101 if (waitpid (pid, &status, 0) < 0)
1105 else if (errno == ECHILD)
1106 ; /* do nothing, child already reaped */
1108 g_warning ("waitpid() should not fail in "
1109 "'fork_exec_with_pipes'");
1114 if (!read_ints (child_err_report_pipe[0],
1117 goto cleanup_and_fail;
1121 /* Error from the child. */
1125 case CHILD_CHDIR_FAILED:
1128 G_SPAWN_ERROR_CHDIR,
1129 _("Failed to change to directory '%s' (%s)"),
1131 g_strerror (buf[1]));
1135 case CHILD_EXEC_FAILED:
1138 exec_err_to_g_error (buf[1]),
1139 _("Failed to execute child process \"%s\" (%s)"),
1141 g_strerror (buf[1]));
1145 case CHILD_DUP2_FAILED:
1148 G_SPAWN_ERROR_FAILED,
1149 _("Failed to redirect output or input of child process (%s)"),
1150 g_strerror (buf[1]));
1154 case CHILD_FORK_FAILED:
1158 _("Failed to fork child process (%s)"),
1159 g_strerror (buf[1]));
1165 G_SPAWN_ERROR_FAILED,
1166 _("Unknown error executing child process \"%s\""),
1171 goto cleanup_and_fail;
1174 /* Get child pid from intermediate child pipe. */
1175 if (intermediate_child)
1179 if (!read_ints (child_pid_report_pipe[0],
1180 buf, 1, &n_ints, error))
1181 goto cleanup_and_fail;
1187 G_SPAWN_ERROR_FAILED,
1188 _("Failed to read enough data from child pid pipe (%s)"),
1189 g_strerror (errno));
1190 goto cleanup_and_fail;
1194 /* we have the child pid */
1199 /* Success against all odds! return the information */
1205 *standard_input = stdin_pipe[1];
1206 if (standard_output)
1207 *standard_output = stdout_pipe[0];
1209 *standard_error = stderr_pipe[0];
1215 close_and_invalidate (&child_err_report_pipe[0]);
1216 close_and_invalidate (&child_err_report_pipe[1]);
1217 close_and_invalidate (&child_pid_report_pipe[0]);
1218 close_and_invalidate (&child_pid_report_pipe[1]);
1219 close_and_invalidate (&stdin_pipe[0]);
1220 close_and_invalidate (&stdin_pipe[1]);
1221 close_and_invalidate (&stdout_pipe[0]);
1222 close_and_invalidate (&stdout_pipe[1]);
1223 close_and_invalidate (&stderr_pipe[0]);
1224 close_and_invalidate (&stderr_pipe[1]);
1230 make_pipe (gint p[2],
1237 G_SPAWN_ERROR_FAILED,
1238 _("Failed to create pipe for communicating with child process (%s)"),
1239 g_strerror (errno));
1246 /* Based on execvp from GNU C Library */
1249 script_execute (const gchar *file,
1252 gboolean search_path)
1254 /* Count the arguments. */
1259 /* Construct an argument list for the shell. */
1263 new_argv = g_new0 (gchar*, argc + 2); /* /bin/sh and NULL */
1265 new_argv[0] = (char *) "/bin/sh";
1266 new_argv[1] = (char *) file;
1269 new_argv[argc + 1] = argv[argc];
1273 /* Execute the shell. */
1275 execve (new_argv[0], new_argv, envp);
1277 execv (new_argv[0], new_argv);
1284 my_strchrnul (const gchar *str, gchar c)
1286 gchar *p = (gchar*) str;
1287 while (*p && (*p != c))
1294 g_execute (const gchar *file,
1297 gboolean search_path)
1301 /* We check the simple case first. */
1306 if (!search_path || strchr (file, '/') != NULL)
1308 /* Don't search when it contains a slash. */
1310 execve (file, argv, envp);
1314 if (errno == ENOEXEC)
1315 script_execute (file, argv, envp, FALSE);
1319 gboolean got_eacces = 0;
1320 const gchar *path, *p;
1321 gchar *name, *freeme;
1325 path = g_getenv ("PATH");
1328 /* There is no `PATH' in the environment. The default
1329 * search path in libc is the current directory followed by
1330 * the path `confstr' returns for `_CS_PATH'.
1333 /* In GLib we put . last, for security, and don't use the
1334 * unportable confstr(); UNIX98 does not actually specify
1335 * what to search if PATH is unset. POSIX may, dunno.
1338 path = "/bin:/usr/bin:.";
1341 len = strlen (file) + 1;
1342 pathlen = strlen (path);
1343 freeme = name = g_malloc (pathlen + len + 1);
1345 /* Copy the file name at the top, including '\0' */
1346 memcpy (name + pathlen + 1, file, len);
1347 name = name + pathlen;
1348 /* And add the slash before the filename */
1357 p = my_strchrnul (path, ':');
1360 /* Two adjacent colons, or a colon at the beginning or the end
1361 * of `PATH' means to search the current directory.
1365 startp = memcpy (name - (p - path), path, p - path);
1367 /* Try to execute this name. If it works, execv will not return. */
1369 execve (startp, argv, envp);
1371 execv (startp, argv);
1373 if (errno == ENOEXEC)
1374 script_execute (startp, argv, envp, search_path);
1379 /* Record the we got a `Permission denied' error. If we end
1380 * up finding no executable we can use, we want to diagnose
1381 * that we did find one but were denied access.
1394 /* Those errors indicate the file is missing or not executable
1395 * by us, in which case we want to just try the next path
1401 /* Some other error means we found an executable file, but
1402 * something went wrong executing it; return the error to our
1409 while (*p++ != '\0');
1411 /* We tried every element and none of them worked. */
1413 /* At least one failure was due to permissions, so report that
1421 /* Return the error from the last attempt (probably ENOENT). */