1 /* vi: set sw=4 ts=4: */
3 * Mini run-parts implementation for busybox
6 * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it>
8 * Based on the Debian run-parts program, version 1.15
9 * Copyright (C) 1996 Jeff Noxon <jeff@router.patch.net>,
10 * Copyright (C) 1996-1999 Guy Maor <maor@debian.org>
13 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
16 /* This is my first attempt to write a program in C (well, this is my first
17 * attempt to write a program! :-) . */
19 /* This piece of code is heavily based on the original version of run-parts,
20 * taken from debian-utils. I've only removed the long options and a the
21 * report mode. As the original run-parts support only long options, I've
22 * broken compatibility because the BusyBox policy doesn't allow them.
23 * The supported options are:
24 * -t test. Print the name of the files to be executed, without
26 * -a ARG argument. Pass ARG as an argument the program executed. It can
27 * be repeated to pass multiple arguments.
28 * -u MASK umask. Set the umask of the program executed to MASK. */
31 * done - convert calls to error in perror... and remove error()
32 * done - convert malloc/realloc to their x... counterparts
33 * done - remove catch_sigchld
34 * done - use bb's concat_path_file()
35 * done - declare run_parts_main() as extern and any other function as static?
41 static const struct option runparts_long_options[] = {
42 { "test", 0, NULL, 't' },
43 { "umask", 1, NULL, 'u' },
44 { "arg", 1, NULL, 'a' },
49 /* True or false? Is this a valid filename (upper/lower alpha, digits,
50 * underscores, and hyphens only?)
52 static int valid_name(const struct dirent *d)
54 const char *c = d->d_name;
57 if (!isalnum(*c) && (*c != '_') && (*c != '-')) {
65 /* test mode = 1 is the same as official run_parts
66 * test_mode = 2 means to fail silently on missing directories
68 static int run_parts(char **args, const unsigned char test_mode)
70 struct dirent **namelist = 0;
79 /* Avoid longjmp clobbering */
83 /* scandir() isn't POSIX, but it makes things easy. */
84 entries = scandir(arg0, &namelist, valid_name, alphasort);
90 bb_perror_msg_and_die("cannot open '%s'", arg0);
93 for (i = 0; i < entries; i++) {
94 filename = concat_path_file(arg0, namelist[i]->d_name);
97 if (S_ISREG(st.st_mode) && !access(filename, X_OK)) {
101 /* exec_errno is common vfork variable */
102 volatile int exec_errno = 0;
106 if ((pid = vfork()) < 0) {
107 bb_perror_msg_and_die("failed to fork");
110 execve(filename, args, environ);
115 waitpid(pid, &result, 0);
118 bb_perror_msg("failed to exec %s", filename);
121 if (WIFEXITED(result) && WEXITSTATUS(result)) {
122 bb_perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result));
124 } else if (WIFSIGNALED(result)) {
125 bb_perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result));
129 } else if (!S_ISDIR(st.st_mode)) {
130 bb_error_msg("component %s is not an executable plain file", filename);
144 /* Process options */
145 int run_parts_main(int argc, char **argv)
147 char **args = xmalloc(2 * sizeof(char *));
148 unsigned char test_mode = 0;
149 unsigned short argcount = 1;
154 while ((opt = getopt_long(argc, argv, "tu:a:",
155 runparts_long_options, NULL)) > 0)
158 /* Enable test mode */
162 /* Set the umask of the programs executed */
164 /* Check and set the umask of the program executed. As stated in the original
165 * run-parts, the octal conversion in libc is not foolproof; it will take the
166 * 8 and 9 digits under some circumstances. We'll just have to live with it.
168 umask(xstrtoul_range(optarg, 8, 0, 07777));
170 /* Pass an argument to the programs */
172 /* Add an argument to the commands that we will call.
173 * Called once for every argument. */
174 args = xrealloc(args, (argcount + 2) * (sizeof(char *)));
175 args[argcount++] = optarg;
182 /* We require exactly one argument: the directory name */
183 if (optind != (argc - 1)) {
187 args[0] = argv[optind];
190 return run_parts(args, test_mode);