1 /* vi: set sw=4 ts=4: */
3 * run command from specified directory
6 * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it>
7 * rewrite to vfork usage by
8 * Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
10 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14 #include <sys/types.h>
25 /* True or false? Is this a valid filename (upper/lower alpha, digits,
26 * underscores, and hyphens only?)
28 static int valid_name(const struct dirent *d)
30 const char *c = d->d_name;
33 if (!isalnum(*c) && (*c != '_') && (*c != '-')) {
41 /* test mode = 1 is the same as official run_parts
42 * test_mode = 2 means to fail silently on missing directories
45 int run_parts(char **args, const unsigned char test_mode, char **env)
47 struct dirent **namelist = 0;
56 /* Avoid longjmp clobbering */
60 /* scandir() isn't POSIX, but it makes things easy. */
61 entries = scandir(arg0, &namelist, valid_name, alphasort);
67 bb_perror_msg_and_die("unable to open `%s'", arg0);
70 for (i = 0; i < entries; i++) {
72 filename = concat_path_file(arg0, namelist[i]->d_name);
75 if (S_ISREG(st.st_mode) && !access(filename, X_OK)) {
79 /* exec_errno is common vfork variable */
80 volatile int exec_errno = 0;
84 if ((pid = vfork()) < 0) {
85 bb_perror_msg_and_die("failed to fork");
88 execve(filename, args, env);
93 waitpid(pid, &result, 0);
96 bb_perror_msg("failed to exec %s", filename);
99 if (WIFEXITED(result) && WEXITSTATUS(result)) {
100 bb_perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result));
102 } else if (WIFSIGNALED(result)) {
103 bb_perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result));
108 else if (!S_ISDIR(st.st_mode)) {
109 bb_error_msg("component %s is not an executable plain file", filename);