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 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
30 /* This is my first attempt to write a program in C (well, this is my first
31 * attempt to write a program! :-) . */
33 /* This piece of code is heavily based on the original version of run-parts,
34 * taken from debian-utils. I've only removed the long options and a the
35 * report mode. As the original run-parts support only long options, I've
36 * broken compatibility because the BusyBox policy doesn't allow them.
37 * The supported options are:
38 * -t test. Print the name of the files to be executed, without
40 * -a ARG argument. Pass ARG as an argument the program executed. It can
41 * be repeated to pass multiple arguments.
42 * -u MASK umask. Set the umask of the program executed to MASK. */
45 * done - convert calls to error in perror... and remove error()
46 * done - convert malloc/realloc to their x... counterparts
47 * done - remove catch_sigchld
48 * done - use bb's concat_path_file()
49 * done - declare run_parts_main() as extern and any other function as static?
55 /* #include <sys/types.h> */
67 static int test_mode = 0;
68 static int exitstatus = 0;
70 static int argcount = 0, argsize = 0;
71 static char **args = 0;
75 /* Check and set the umask of the program executed. As stated in the original
76 * run-parts, the octal conversion in libc is not foolproof; it will take the
77 * 8 and 9 digits under some circumstances. We'll just have to live with it.
80 static void set_umask (void)
85 * We must substitute sscanf, according to bb's style guide? */
86 result = sscanf (optarg, "%o", &mask);
87 if ((result != 1) || (mask > 07777) || (mask < 0)) {
88 perror_msg_and_die ("bad umask value");
95 /* Add an argument to the commands that we will call. Called once for
97 static void add_argument (char *newarg)
99 if (argcount+1 >= argsize) {
100 argsize = argsize ? argsize*2 : 4;
101 /*TODO if we convert to xrealloc we lose the verbose error message */
102 args = realloc(args, argsize * (sizeof(char*)));
104 perror_msg_and_die ("failed to reallocate memory for arguments");
107 args[argcount++] = newarg;
112 /* True or false? Is this a valid filename (upper/lower alpha, digits,
113 * underscores, and hyphens only?)
116 static int valid_name (const struct dirent *d)
120 if (!isalnum(*c) && *c!='_' && *c!='-') {
132 static void run_part (char *progname)
138 if ((pid=fork()) < 0) {
139 perror_msg_and_die ("failed to fork");
143 execv (progname, args);
144 perror_msg_and_die ("failed to exec %s", progname);
151 waitpid(pid, &result, 0);
154 if (WIFEXITED (result) && WEXITSTATUS(result)) {
155 perror_msg ("%s exited with return code %d", progname, WEXITSTATUS(result));
158 else if (WIFSIGNALED (result)) {
159 perror_msg ("%s exited because of uncaught signal %d", progname,
166 /* Find the parts to run & call run_part() */
168 static void run_parts (char *dir_name)
170 struct dirent **namelist = 0;
177 /* scandir() isn't POSIX, but it makes things easy. */
178 entries = scandir (dir_name, &namelist, valid_name, alphasort);
181 perror_msg_and_die ("failed to open directory %s", dir_name);
184 for (i = 0; i < entries; i++) {
188 filename = concat_path_file (dir_name, namelist[i]->d_name);
190 if (stat (filename, &st) < 0) {
191 perror_msg_and_die ("failed to stat component %s", filename);
193 if (S_ISREG(st.st_mode) && !access (filename, X_OK)) {
195 printf ("run-parts would run %s\n", filename);
201 else if (!S_ISDIR(st.st_mode)) {
202 error_msg ("component %s is not an executable plain file",
214 /* Process options */
215 int run_parts_main (int argc, char *argv[])
224 c = getopt(argc, argv, "tu:a:");
229 case 't': /* Enable test mode */
232 case 'u': /* Set the umask of the programs executed */
235 case 'a': /* Pass an argument to the programs */
236 add_argument (optarg);
243 /* We require exactly one argument: the directory name */
244 if (optind != (argc - 1)) {
248 run_parts (argv[optind]);