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 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
19 #include <sys/types.h>
30 /* True or false? Is this a valid filename (upper/lower alpha, digits,
31 * underscores, and hyphens only?)
33 static int valid_name(const struct dirent *d)
38 if (!isalnum(*c) && (*c != '_') && (*c != '-')) {
46 /* test mode = 1 is the same as official run_parts
47 * test_mode = 2 means to fail silently on missing directories
50 extern int run_parts(char **args, const unsigned char test_mode, char **env)
52 struct dirent **namelist = 0;
61 /* Avoid longjmp clobbering */
65 /* scandir() isn't POSIX, but it makes things easy. */
66 entries = scandir(arg0, &namelist, valid_name, alphasort);
72 bb_perror_msg_and_die("failed to open directory %s", arg0);
75 for (i = 0; i < entries; i++) {
77 filename = concat_path_file(arg0, namelist[i]->d_name);
79 if (stat(filename, &st) < 0) {
80 bb_perror_msg_and_die("failed to stat component %s", filename);
82 if (S_ISREG(st.st_mode) && !access(filename, X_OK)) {
86 /* exec_errno is common vfork variable */
87 volatile int exec_errno = 0;
91 if ((pid = vfork()) < 0) {
92 bb_perror_msg_and_die("failed to fork");
95 execve(filename, args, env);
100 waitpid(pid, &result, 0);
103 bb_perror_msg("failed to exec %s", filename);
106 if (WIFEXITED(result) && WEXITSTATUS(result)) {
107 bb_perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result));
109 } else if (WIFSIGNALED(result)) {
110 bb_perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result));
115 else if (!S_ISDIR(st.st_mode)) {
116 bb_error_msg("component %s is not an executable plain file", filename);