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 */
38 #warning "FIXME remove gettext hack"
43 static gint
g_execute (const gchar
*file
,
46 gboolean search_path
);
48 static gboolean
make_pipe (gint p
[2],
50 static gboolean
fork_exec_with_pipes (gboolean intermediate_child
,
51 const gchar
*working_directory
,
54 gboolean close_descriptors
,
56 gboolean stdout_to_null
,
57 gboolean stderr_to_null
,
58 gboolean child_inherits_stdin
,
59 GSpawnChildSetupFunc child_setup
,
63 gint
*standard_output
,
68 g_spawn_error_quark (void)
70 static GQuark quark
= 0;
72 quark
= g_quark_from_static_string ("g-exec-error-quark");
78 * @working_directory: child's current working directory, or NULL to inherit parent's
79 * @argv: child's argument vector
80 * @envp: child's environment, or NULL to inherit parent's
81 * @flags: flags from #GSpawnFlags
82 * @child_setup: function to run in the child just before exec()
83 * @user_data: user data for @child_setup
84 * @child_pid: return location for child process ID, or NULL
85 * @error: return location for error
87 * See g_spawn_async_with_pipes() for a full description; this function
88 * simply calls the g_spawn_async_with_pipes() without any pipes.
90 * Return value: TRUE on success, FALSE if error is set
93 g_spawn_async (const gchar
*working_directory
,
97 GSpawnChildSetupFunc child_setup
,
102 g_return_val_if_fail (argv
!= NULL
, FALSE
);
104 return g_spawn_async_with_pipes (working_directory
,
114 /* Avoids a danger in threaded situations (calling close()
115 * on a file descriptor twice, and another thread has
116 * re-opened it since the first close)
119 close_and_invalidate (gint
*fd
)
131 READ_FAILED
= 0, /* FALSE */
137 read_data (GString
*str
,
146 bytes
= read (fd
, &buf
, 4096);
152 g_string_append_len (str
, buf
, bytes
);
155 else if (bytes
< 0 && errno
== EINTR
)
162 _("Failed to read data from child process (%s)"),
173 * @working_directory: child's current working directory, or NULL to inherit parent's
174 * @argv: child's argument vector
175 * @envp: child's environment, or NULL to inherit parent's
176 * @flags: flags from #GSpawnFlags
177 * @child_setup: function to run in the child just before exec()
178 * @user_data: user data for @child_setup
179 * @standard_output: return location for child output
180 * @standard_error: return location for child error messages
181 * @exit_status: child exit status, as returned by waitpid()
182 * @error: return location for error
184 * Executes a child synchronously (waits for the child to exit before returning).
185 * All output from the child is stored in @standard_output and @standard_error,
186 * if those parameters are non-NULL. If @exit_status is non-NULL, the exit status
187 * of the child is stored there as it would be by waitpid(); standard UNIX
188 * macros such as WIFEXITED() and WEXITSTATUS() must be used to evaluate the
189 * exit status. If an error occurs, no data is returned in @standard_output,
190 * @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,
248 standard_output
? &outpipe
: NULL
,
249 standard_error
? &errpipe
: NULL
,
253 /* Read data from child. */
259 outstr
= g_string_new ("");
264 errstr
= g_string_new ("");
267 /* Read data until we get EOF on both pipes. */
276 FD_SET (outpipe
, &fds
);
278 FD_SET (errpipe
, &fds
);
280 ret
= select (MAX (outpipe
, errpipe
) + 1,
283 NULL
/* no timeout */);
285 if (ret
< 0 && errno
!= EINTR
)
292 _("Unexpected error in select() reading data from a child process (%s)"),
298 if (outpipe
>= 0 && FD_ISSET (outpipe
, &fds
))
300 switch (read_data (outstr
, outpipe
, error
))
306 close_and_invalidate (&outpipe
);
317 if (errpipe
>= 0 && FD_ISSET (errpipe
, &fds
))
319 switch (read_data (errstr
, errpipe
, error
))
325 close_and_invalidate (&errpipe
);
337 /* These should only be open still if we had an error. */
340 close_and_invalidate (&outpipe
);
342 close_and_invalidate (&errpipe
);
344 /* Wait for child to exit, even if we have
349 ret
= waitpid (pid
, &status
, 0);
355 else if (errno
== ECHILD
)
359 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.");
363 /* We don't need the exit status. */
368 if (!failed
) /* avoid error pileups */
375 _("Unexpected error in waitpid() (%s)"),
384 g_string_free (outstr
, TRUE
);
386 g_string_free (errstr
, TRUE
);
393 *exit_status
= status
;
396 *standard_output
= g_string_free (outstr
, FALSE
);
399 *standard_error
= g_string_free (errstr
, FALSE
);
406 * g_spawn_async_with_pipes:
407 * @working_directory: child's current working directory, or NULL to inherit parent's
408 * @argv: child's argument vector
409 * @envp: child's environment, or NULL to inherit parent's
410 * @flags: flags from #GSpawnFlags
411 * @child_setup: function to run in the child just before exec()
412 * @user_data: user data for @child_setup
413 * @child_pid: return location for child process ID, or NULL
414 * @standard_input: return location for file descriptor to write to child's stdin, or NULL
415 * @standard_output: return location for file descriptor to read child's stdout, or NULL
416 * @standard_error: return location for file descriptor to read child's stderr, or NULL
417 * @error: return location for error
419 * Executes a child program asynchronously (your program will not
420 * block waiting for the child to exit). The child program is
421 * specified by the only argument that must be provided, @argv. @argv
422 * should be a NULL-terminated array of strings, to be passed as the
423 * argument vector for the child. The first string in @argv is of
424 * course the name of the program to execute. By default, the name of
425 * the program must be a full path; the PATH shell variable will only
426 * be searched if you pass the %G_SPAWN_SEARCH_PATH flag.
428 * @envp is a NULL-terminated array of strings, where each string
429 * has the form <literal>KEY=VALUE</literal>. This will become
430 * the child's environment. If @envp is NULL, the child inherits its
431 * parent's environment.
433 * @flags should be the bitwise OR of any flags you want to affect the
434 * function's behavior. The %G_SPAWN_DO_NOT_REAP_CHILD means that the
435 * child will not be automatically reaped; you must call waitpid() or
436 * handle SIGCHLD yourself, or the child will become a zombie.
437 * %G_SPAWN_LEAVE_DESCRIPTORS_OPEN means that the parent's open file
438 * descriptors will be inherited by the child; otherwise all
439 * descriptors except stdin/stdout/stderr will be closed before
440 * calling exec() in the child. %G_SPAWN_SEARCH_PATH means that
441 * <literal>argv[0]</literal> need not be an absolute path, it
442 * will be looked for in the user's PATH. %G_SPAWN_STDOUT_TO_DEV_NULL
443 * means that the child's standad output will be discarded, instead
444 * of going to the same location as the parent's standard output.
445 * %G_SPAWN_STDERR_TO_DEV_NULL means that the child's standard error
446 * will be discarded. %G_SPAWN_CHILD_INHERITS_STDIN means that
447 * the child will inherit the parent's standard input (by default,
448 * the child's standard input is attached to /dev/null).
450 * @child_setup and @user_data are a function and user data to be
451 * called in the child after GLib has performed all the setup it plans
452 * to perform (including creating pipes, closing file descriptors,
453 * etc.) but before calling exec(). That is, @child_setup is called
454 * just before calling exec() in the child. Obviously actions taken in
455 * this function will only affect the child, not the parent.
457 * If non-NULL, @child_pid will be filled with the child's process
458 * ID. You can use the process ID to send signals to the child, or
459 * to waitpid() if you specified the %G_SPAWN_DO_NOT_REAP_CHILD flag.
461 * If non-NULL, the @standard_input, @standard_output, @standard_error
462 * locations will be filled with file descriptors for writing to the child's
463 * standard input or reading from its standard output or standard error.
464 * The caller of g_spawn_async_with_pipes() must close these file descriptors
465 * when they are no longer in use. If these parameters are NULL, the
466 * corresponding pipe won't be created.
468 * @error can be NULL to ignore errors, or non-NULL to report errors.
469 * If an error is set, the function returns FALSE. Errors
470 * are reported even if they occur in the child (for example if the
471 * executable in <literal>argv[0]</literal> is not found). Typically
472 * the <literal>message</literal> field of returned errors should be displayed
473 * to users. Possible errors are those from the #G_SPAWN_ERROR domain.
475 * If an error occurs, @child_pid, @standard_input, @standard_output,
476 * and @standard_error will not be filled with valid values.
478 * Return value: TRUE on success, FALSE if an error was set
481 g_spawn_async_with_pipes (const gchar
*working_directory
,
485 GSpawnChildSetupFunc child_setup
,
488 gint
*standard_input
,
489 gint
*standard_output
,
490 gint
*standard_error
,
493 g_return_val_if_fail (argv
!= NULL
, FALSE
);
494 g_return_val_if_fail (standard_output
== NULL
||
495 !(flags
& G_SPAWN_STDOUT_TO_DEV_NULL
), FALSE
);
496 g_return_val_if_fail (standard_error
== NULL
||
497 !(flags
& G_SPAWN_STDERR_TO_DEV_NULL
), FALSE
);
498 /* can't inherit stdin if we have an input pipe. */
499 g_return_val_if_fail (standard_input
== NULL
||
500 !(flags
& G_SPAWN_CHILD_INHERITS_STDIN
), FALSE
);
502 return fork_exec_with_pipes (!(flags
& G_SPAWN_DO_NOT_REAP_CHILD
),
506 !(flags
& G_SPAWN_LEAVE_DESCRIPTORS_OPEN
),
507 (flags
& G_SPAWN_SEARCH_PATH
) != 0,
508 (flags
& G_SPAWN_STDOUT_TO_DEV_NULL
) != 0,
509 (flags
& G_SPAWN_STDERR_TO_DEV_NULL
) != 0,
510 (flags
& G_SPAWN_CHILD_INHERITS_STDIN
) != 0,
521 * g_spawn_command_line_sync:
522 * @command_line: a command line
523 * @standard_output: return location for child output
524 * @standard_error: return location for child errors
525 * @exit_status: return location for child exit status
526 * @error: return location for errors
528 * A simple version of g_spawn_sync() with little-used parameters
529 * removed, taking a command line instead of an argument vector. See
530 * g_spawn_sync() for full details. @command_line will be parsed by
531 * g_shell_parse_argv(). Unlike g_spawn_sync(), the %G_SPAWN_SEARCH_PATH flag
532 * is enabled. Note that %G_SPAWN_SEARCH_PATH can have security
533 * implications, so consider using g_spawn_sync() directly if
534 * appropriate. Possible errors are those from g_spawn_sync() and those
535 * from g_shell_parse_argv().
537 * Return value: TRUE on success, FALSE if an error was set
540 g_spawn_command_line_sync (const gchar
*command_line
,
541 gchar
**standard_output
,
542 gchar
**standard_error
,
549 g_return_val_if_fail (command_line
!= NULL
, FALSE
);
551 if (!g_shell_parse_argv (command_line
,
556 retval
= g_spawn_sync (NULL
,
572 * g_spawn_command_line_async:
573 * @command_line: a command line
574 * @error: return location for errors
576 * A simple version of g_spawn_async() that parses a command line with
577 * g_shell_parse_argv() and passes it to g_spawn_async(). Runs a
578 * command line in the background. Unlike g_spawn_async(), the
579 * %G_SPAWN_SEARCH_PATH flag is enabled, other flags are not. Note
580 * that %G_SPAWN_SEARCH_PATH can have security implications, so
581 * consider using g_spawn_async() directly if appropriate. Possible
582 * errors are those from g_shell_parse_argv() and g_spawn_async().
584 * Return value: TRUE on success, FALSE if error is set.
587 g_spawn_command_line_async (const gchar
*command_line
,
593 g_return_val_if_fail (command_line
!= NULL
, FALSE
);
595 if (!g_shell_parse_argv (command_line
,
600 retval
= g_spawn_async (NULL
,
614 exec_err_to_g_error (gint en
)
620 return G_SPAWN_ERROR_ACCES
;
626 return G_SPAWN_ERROR_PERM
;
632 return G_SPAWN_ERROR_2BIG
;
638 return G_SPAWN_ERROR_NOEXEC
;
644 return G_SPAWN_ERROR_NAMETOOLONG
;
650 return G_SPAWN_ERROR_NOENT
;
656 return G_SPAWN_ERROR_NOMEM
;
662 return G_SPAWN_ERROR_NOTDIR
;
668 return G_SPAWN_ERROR_LOOP
;
674 return G_SPAWN_ERROR_TXTBUSY
;
680 return G_SPAWN_ERROR_IO
;
686 return G_SPAWN_ERROR_NFILE
;
692 return G_SPAWN_ERROR_MFILE
;
698 return G_SPAWN_ERROR_INVAL
;
704 return G_SPAWN_ERROR_ISDIR
;
710 return G_SPAWN_ERROR_LIBBAD
;
715 return G_SPAWN_ERROR_FAILED
;
721 write_err_and_exit (gint fd
, gint msg
)
725 write (fd
, &msg
, sizeof(msg
));
726 write (fd
, &en
, sizeof(en
));
732 set_cloexec (gint fd
)
734 fcntl (fd
, F_SETFD
, FD_CLOEXEC
);
738 sane_dup2 (gint fd1
, gint fd2
)
743 ret
= dup2 (fd1
, fd2
);
744 if (ret
< 0 && errno
== EINTR
)
759 do_exec (gint child_err_report_fd
,
763 const gchar
*working_directory
,
766 gboolean close_descriptors
,
767 gboolean search_path
,
768 gboolean stdout_to_null
,
769 gboolean stderr_to_null
,
770 gboolean child_inherits_stdin
,
771 GSpawnChildSetupFunc child_setup
,
774 if (working_directory
&& chdir (working_directory
) < 0)
775 write_err_and_exit (child_err_report_fd
,
778 /* Close all file descriptors but stdin stdout and stderr as
779 * soon as we exec. Note that this includes
780 * child_err_report_fd, which keeps the parent from blocking
781 * forever on the other end of that pipe.
783 if (close_descriptors
)
788 open_max
= sysconf (_SC_OPEN_MAX
);
789 for (i
= 3; i
< open_max
; i
++)
794 /* We need to do child_err_report_fd anyway */
795 set_cloexec (child_err_report_fd
);
798 /* Redirect pipes as required */
802 /* dup2 can't actually fail here I don't think */
804 if (sane_dup2 (stdin_fd
, 0) < 0)
805 write_err_and_exit (child_err_report_fd
,
808 /* ignore this if it doesn't work */
809 close_and_invalidate (&stdin_fd
);
811 else if (!child_inherits_stdin
)
813 /* Keep process from blocking on a read of stdin */
814 gint read_null
= open ("/dev/null", O_RDONLY
);
815 sane_dup2 (read_null
, 0);
816 close_and_invalidate (&read_null
);
821 /* dup2 can't actually fail here I don't think */
823 if (sane_dup2 (stdout_fd
, 1) < 0)
824 write_err_and_exit (child_err_report_fd
,
827 /* ignore this if it doesn't work */
828 close_and_invalidate (&stdout_fd
);
830 else if (stdout_to_null
)
832 gint write_null
= open ("/dev/null", O_WRONLY
);
833 sane_dup2 (write_null
, 1);
834 close_and_invalidate (&write_null
);
839 /* dup2 can't actually fail here I don't think */
841 if (sane_dup2 (stderr_fd
, 2) < 0)
842 write_err_and_exit (child_err_report_fd
,
845 /* ignore this if it doesn't work */
846 close_and_invalidate (&stderr_fd
);
848 else if (stderr_to_null
)
850 gint write_null
= open ("/dev/null", O_WRONLY
);
851 sane_dup2 (write_null
, 2);
852 close_and_invalidate (&write_null
);
855 /* Call user function just before we exec */
858 (* child_setup
) (user_data
);
861 g_execute (argv
[0], argv
, envp
, search_path
);
864 write_err_and_exit (child_err_report_fd
,
881 if (bytes
>= sizeof(gint
)*2)
882 break; /* give up, who knows what happened, should not be
888 ((gchar
*)buf
) + bytes
,
889 sizeof(gint
)*n_ints_in_buf
- bytes
);
890 if (chunk
< 0 && errno
== EINTR
)
895 /* Some weird shit happened, bail out */
899 G_SPAWN_ERROR_FAILED
,
900 _("Failed to read from child pipe (%s)"),
909 g_assert (chunk
> 0);
915 *n_ints_read
= bytes
/4;
921 fork_exec_with_pipes (gboolean intermediate_child
,
922 const gchar
*working_directory
,
925 gboolean close_descriptors
,
926 gboolean search_path
,
927 gboolean stdout_to_null
,
928 gboolean stderr_to_null
,
929 gboolean child_inherits_stdin
,
930 GSpawnChildSetupFunc child_setup
,
933 gint
*standard_input
,
934 gint
*standard_output
,
935 gint
*standard_error
,
939 gint stdin_pipe
[2] = { -1, -1 };
940 gint stdout_pipe
[2] = { -1, -1 };
941 gint stderr_pipe
[2] = { -1, -1 };
942 gint child_err_report_pipe
[2] = { -1, -1 };
943 gint child_pid_report_pipe
[2] = { -1, -1 };
946 if (!make_pipe (child_err_report_pipe
, error
))
949 if (intermediate_child
&& !make_pipe (child_pid_report_pipe
, error
))
950 goto cleanup_and_fail
;
952 if (standard_input
&& !make_pipe (stdin_pipe
, error
))
953 goto cleanup_and_fail
;
955 if (standard_output
&& !make_pipe (stdout_pipe
, error
))
956 goto cleanup_and_fail
;
958 if (standard_error
&& !make_pipe (stderr_pipe
, error
))
959 goto cleanup_and_fail
;
968 _("Failed to fork (%s)"),
971 goto cleanup_and_fail
;
975 /* Immediate child. This may or may not be the child that
976 * actually execs the new process.
979 /* Be sure we crash if the parent exits
980 * and we write to the err_report_pipe
982 signal (SIGPIPE
, SIG_DFL
);
984 /* Close the parent's end of the pipes;
985 * not needed in the close_descriptors case,
988 close_and_invalidate (&child_err_report_pipe
[0]);
989 close_and_invalidate (&child_pid_report_pipe
[0]);
990 close_and_invalidate (&stdin_pipe
[1]);
991 close_and_invalidate (&stdout_pipe
[0]);
992 close_and_invalidate (&stderr_pipe
[0]);
994 if (intermediate_child
)
996 /* We need to fork an intermediate child that launches the
997 * final child. The purpose of the intermediate child
998 * is to exit, so we can waitpid() it immediately.
999 * Then the grandchild will not become a zombie.
1001 gint grandchild_pid
;
1003 grandchild_pid
= fork ();
1005 if (grandchild_pid
< 0)
1007 /* report -1 as child PID */
1008 write (child_pid_report_pipe
[1], &grandchild_pid
,
1009 sizeof(grandchild_pid
));
1011 write_err_and_exit (child_err_report_pipe
[1],
1014 else if (grandchild_pid
== 0)
1016 do_exec (child_err_report_pipe
[1],
1027 child_inherits_stdin
,
1033 write (child_pid_report_pipe
[1], &grandchild_pid
, sizeof(grandchild_pid
));
1034 close_and_invalidate (&child_pid_report_pipe
[1]);
1041 /* Just run the child.
1044 do_exec (child_err_report_pipe
[1],
1055 child_inherits_stdin
,
1067 /* Close the uncared-about ends of the pipes */
1068 close_and_invalidate (&child_err_report_pipe
[1]);
1069 close_and_invalidate (&child_pid_report_pipe
[1]);
1070 close_and_invalidate (&stdin_pipe
[0]);
1071 close_and_invalidate (&stdout_pipe
[1]);
1072 close_and_invalidate (&stderr_pipe
[1]);
1074 /* If we had an intermediate child, reap it */
1075 if (intermediate_child
)
1078 if (waitpid (pid
, &status
, 0) < 0)
1082 else if (errno
== ECHILD
)
1083 ; /* do nothing, child already reaped */
1085 g_warning ("waitpid() should not fail in "
1086 "'fork_exec_with_pipes'");
1091 if (!read_ints (child_err_report_pipe
[0],
1094 goto cleanup_and_fail
;
1098 /* Error from the child. */
1102 case CHILD_CHDIR_FAILED
:
1105 G_SPAWN_ERROR_CHDIR
,
1106 _("Failed to change to directory '%s' (%s)"),
1108 g_strerror (buf
[1]));
1112 case CHILD_EXEC_FAILED
:
1115 exec_err_to_g_error (buf
[1]),
1116 _("Failed to execute child process (%s)"),
1117 g_strerror (buf
[1]));
1121 case CHILD_DUP2_FAILED
:
1124 G_SPAWN_ERROR_FAILED
,
1125 _("Failed to redirect output or input of child process (%s)"),
1126 g_strerror (buf
[1]));
1130 case CHILD_FORK_FAILED
:
1134 _("Failed to fork child process (%s)"),
1135 g_strerror (buf
[1]));
1141 G_SPAWN_ERROR_FAILED
,
1142 _("Unknown error executing child process"));
1146 goto cleanup_and_fail
;
1149 /* Get child pid from intermediate child pipe. */
1150 if (intermediate_child
)
1154 if (!read_ints (child_pid_report_pipe
[0],
1155 buf
, 1, &n_ints
, error
))
1156 goto cleanup_and_fail
;
1162 G_SPAWN_ERROR_FAILED
,
1163 _("Failed to read enough data from child pid pipe (%s)"),
1164 g_strerror (errno
));
1165 goto cleanup_and_fail
;
1169 /* we have the child pid */
1174 /* Success against all odds! return the information */
1180 *standard_input
= stdin_pipe
[1];
1181 if (standard_output
)
1182 *standard_output
= stdout_pipe
[0];
1184 *standard_error
= stderr_pipe
[0];
1190 close_and_invalidate (&child_err_report_pipe
[0]);
1191 close_and_invalidate (&child_err_report_pipe
[1]);
1192 close_and_invalidate (&child_pid_report_pipe
[0]);
1193 close_and_invalidate (&child_pid_report_pipe
[1]);
1194 close_and_invalidate (&stdin_pipe
[0]);
1195 close_and_invalidate (&stdin_pipe
[1]);
1196 close_and_invalidate (&stdout_pipe
[0]);
1197 close_and_invalidate (&stdout_pipe
[1]);
1198 close_and_invalidate (&stderr_pipe
[0]);
1199 close_and_invalidate (&stderr_pipe
[1]);
1205 make_pipe (gint p
[2],
1212 G_SPAWN_ERROR_FAILED
,
1213 _("Failed to create pipe for communicating with child process (%s)"),
1214 g_strerror (errno
));
1221 /* Based on execvp from GNU C Library */
1224 script_execute (const gchar
*file
,
1227 gboolean search_path
)
1229 /* Count the arguments. */
1234 /* Construct an argument list for the shell. */
1238 new_argv
= g_new0 (gchar
*, argc
+ 1);
1240 new_argv
[0] = (char *) "/bin/sh";
1241 new_argv
[1] = (char *) file
;
1244 new_argv
[argc
] = argv
[argc
- 1];
1248 /* Execute the shell. */
1250 execve (new_argv
[0], new_argv
, envp
);
1252 execv (new_argv
[0], new_argv
);
1259 my_strchrnul (const gchar
*str
, gchar c
)
1261 gchar
*p
= (gchar
*) str
;
1262 while (*p
&& (*p
!= c
))
1269 g_execute (const gchar
*file
,
1272 gboolean search_path
)
1276 /* We check the simple case first. */
1281 if (!search_path
|| strchr (file
, '/') != NULL
)
1283 /* Don't search when it contains a slash. */
1285 execve (file
, argv
, envp
);
1289 if (errno
== ENOEXEC
)
1290 script_execute (file
, argv
, envp
, FALSE
);
1294 gboolean got_eacces
= 0;
1295 char *path
, *p
, *name
, *freeme
;
1299 path
= g_getenv ("PATH");
1302 /* There is no `PATH' in the environment. The default
1303 * search path in libc is the current directory followed by
1304 * the path `confstr' returns for `_CS_PATH'.
1307 /* In GLib we put . last, for security, and don't use the
1308 * unportable confstr(); UNIX98 does not actually specify
1309 * what to search if PATH is unset. POSIX may, dunno.
1312 path
= "/bin:/usr/bin:.";
1315 len
= strlen (file
) + 1;
1316 pathlen
= strlen (path
);
1317 freeme
= name
= g_malloc (pathlen
+ len
+ 1);
1319 /* Copy the file name at the top, including '\0' */
1320 memcpy (name
+ pathlen
+ 1, file
, len
);
1321 name
= name
+ pathlen
;
1322 /* And add the slash before the filename */
1331 p
= my_strchrnul (path
, ':');
1334 /* Two adjacent colons, or a colon at the beginning or the end
1335 * of `PATH' means to search the current directory.
1339 startp
= memcpy (name
- (p
- path
), path
, p
- path
);
1341 /* Try to execute this name. If it works, execv will not return. */
1343 execve (startp
, argv
, envp
);
1345 execv (startp
, argv
);
1347 if (errno
== ENOEXEC
)
1348 script_execute (startp
, argv
, envp
, search_path
);
1353 /* Record the we got a `Permission denied' error. If we end
1354 * up finding no executable we can use, we want to diagnose
1355 * that we did find one but were denied access.
1368 /* Those errors indicate the file is missing or not executable
1369 * by us, in which case we want to just try the next path
1375 /* Some other error means we found an executable file, but
1376 * something went wrong executing it; return the error to our
1383 while (*p
++ != '\0');
1385 /* We tried every element and none of them worked. */
1387 /* At least one failure was due to permissions, so report that
1395 /* Return the error from the last attempt (probably ENOENT). */