Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame^] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini run-parts implementation for busybox |
| 4 | * |
| 5 | * |
| 6 | * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it> |
| 7 | * |
| 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> |
| 11 | * |
| 12 | * |
| 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. |
| 17 | * |
| 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. |
| 22 | * |
| 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 |
| 26 | * 02111-1307 USA |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | /* This is my first attempt to write a program in C (well, this is my first |
| 31 | * attempt to write a program! :-) . */ |
| 32 | |
| 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 |
| 39 | * execute them. |
| 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. */ |
| 43 | |
| 44 | /* TODO |
| 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 | * use bb's isdirectory() ? It seems that no applet use it. |
| 49 | * done - use bb's concat_path_file() |
| 50 | * declare run_parts_main() as extern and any other function as static? */ |
| 51 | |
| 52 | #include <stdio.h> |
| 53 | #include <stdarg.h> |
| 54 | #include <stdlib.h> |
| 55 | /* #include <sys/types.h> */ |
| 56 | #include <sys/wait.h> |
| 57 | #include <dirent.h> |
| 58 | #include <sys/stat.h> |
| 59 | #include <unistd.h> |
| 60 | #include <getopt.h> |
| 61 | #include <string.h> |
| 62 | #include <errno.h> |
| 63 | #include <ctype.h> |
| 64 | /* #include <signal.h> |
| 65 | #include <sys/time.h> */ |
| 66 | |
| 67 | #include "busybox.h" |
| 68 | |
| 69 | int test_mode = 0; |
| 70 | int verbose_mode = 0; |
| 71 | int exitstatus = 0; |
| 72 | |
| 73 | int argcount = 0, argsize = 0; |
| 74 | char **args = 0; |
| 75 | |
| 76 | |
| 77 | /* set_umask */ |
| 78 | /* Check and set the umask of the program executed. As stated in the original |
| 79 | * run-parts, the octal conversion in libc is not foolproof; it will take the |
| 80 | * 8 and 9 digits under some circumstances. We'll just have to live with it. |
| 81 | */ |
| 82 | |
| 83 | void set_umask (void) |
| 84 | { |
| 85 | int mask, result; |
| 86 | |
| 87 | /*TODO |
| 88 | * We must substitute sscanf, according to bb's style guide? */ |
| 89 | result = sscanf (optarg, "%o", &mask); |
| 90 | if ((result != 1) || (mask > 07777) || (mask < 0)) { |
| 91 | perror_msg_and_die ("bad umask value"); |
| 92 | } |
| 93 | |
| 94 | umask (mask); |
| 95 | } |
| 96 | |
| 97 | /* add_argument */ |
| 98 | /* Add an argument to the commands that we will call. Called once for |
| 99 | every argument. */ |
| 100 | void add_argument (char *newarg) |
| 101 | { |
| 102 | if (argcount+1 >= argsize) { |
| 103 | argsize = argsize ? argsize*2 : 4; |
| 104 | /*TODO if we convert to xrealloc we lose the verbose error message */ |
| 105 | args = realloc(args, argsize * (sizeof(char*))); |
| 106 | if (!args) { |
| 107 | perror_msg_and_die ("failed to reallocate memory for arguments: %s", strerror(errno)); |
| 108 | } |
| 109 | } |
| 110 | args[argcount++] = newarg; |
| 111 | args[argcount] = 0; |
| 112 | } |
| 113 | |
| 114 | /* valid_name */ |
| 115 | /* True or false? Is this a valid filename (upper/lower alpha, digits, |
| 116 | * underscores, and hyphens only?) |
| 117 | */ |
| 118 | |
| 119 | int valid_name (const struct dirent *d) |
| 120 | { |
| 121 | char *c = d->d_name; |
| 122 | while (*c) { |
| 123 | if (!isalnum(*c) && *c!='_' && *c!='-') { |
| 124 | return 0; |
| 125 | } |
| 126 | ++c; |
| 127 | } |
| 128 | return 1; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /* run_part */ |
| 133 | /* Execute a file */ |
| 134 | |
| 135 | void run_part (char *progname) |
| 136 | { |
| 137 | int result; |
| 138 | int pid; |
| 139 | |
| 140 | |
| 141 | if ((pid=fork()) < 0) { |
| 142 | perror_msg_and_die ("failed to fork: %s", strerror(errno)); |
| 143 | } |
| 144 | else if (!pid) { |
| 145 | args[0] = progname; |
| 146 | execv (progname, args); |
| 147 | perror_msg_and_die ("failed to exec %s: %s", progname, strerror (errno)); |
| 148 | } |
| 149 | |
| 150 | if (0) { |
| 151 | |
| 152 | } else { |
| 153 | |
| 154 | waitpid(pid, &result, 0); |
| 155 | } |
| 156 | |
| 157 | if (WIFEXITED (result) && WEXITSTATUS(result)) { |
| 158 | perror_msg ("%s exited with return code %d", progname, WEXITSTATUS(result)); |
| 159 | exitstatus = 1; |
| 160 | } |
| 161 | else if (WIFSIGNALED (result)) { |
| 162 | perror_msg ("%s exited because of uncaught signal %d", progname, |
| 163 | WTERMSIG(result)); |
| 164 | exitstatus = 1; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /* run_parts */ |
| 169 | /* Find the parts to run & call run_part() */ |
| 170 | |
| 171 | void run_parts (char *dir_name) |
| 172 | { |
| 173 | struct dirent **namelist; |
| 174 | char *filename = NULL; |
| 175 | size_t filename_length, dir_name_length; |
| 176 | int entries, i, result; |
| 177 | struct stat st; |
| 178 | |
| 179 | /* dir_name + "/" */ |
| 180 | dir_name_length = strlen(dir_name) + 1; |
| 181 | |
| 182 | /* dir_name + "/" + ".." + "\0" (This will save one realloc.) */ |
| 183 | filename_length = dir_name_length + 2 + 1; |
| 184 | |
| 185 | /* -- |
| 186 | * Removed this part because I want try to use concat_path_file() */ |
| 187 | |
| 188 | /* if (! (filename = malloc(filename_length))) { |
| 189 | error ("failed to allocate memory for path: %s", strerror(errno)); |
| 190 | exit (1); |
| 191 | } */ |
| 192 | |
| 193 | /* -- */ |
| 194 | |
| 195 | /* scandir() isn't POSIX, but it makes things easy. */ |
| 196 | entries = scandir (dir_name, &namelist, valid_name, alphasort); |
| 197 | |
| 198 | if (entries < 0) { |
| 199 | perror_msg_and_die ("failed to open directory %s: %s", dir_name, strerror (errno)); |
| 200 | } |
| 201 | |
| 202 | for (i = 0; i < entries; i++) { |
| 203 | |
| 204 | /* -- |
| 205 | * Removed this part because I want try to use concat_path_file() */ |
| 206 | |
| 207 | /* if (filename_length < dir_name_length + strlen(namelist[i]->d_name) + 1) { |
| 208 | filename_length = dir_name_length + strlen(namelist[i]->d_name) + 1; |
| 209 | if (!(filename = realloc(filename, filename_length))) { |
| 210 | error ("failed to reallocate memory for path: %s", strerror(errno)); |
| 211 | exit (1); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | */ |
| 216 | |
| 217 | /* -- */ |
| 218 | |
| 219 | |
| 220 | /* -- |
| 221 | * Removed for concat_path_file() */ |
| 222 | |
| 223 | /* strcpy (filename, dir_name); |
| 224 | strcat (filename, "/"); |
| 225 | strcat (filename, namelist[i]->d_name); */ |
| 226 | |
| 227 | /* -- */ |
| 228 | |
| 229 | filename = concat_path_file (dir_name, namelist[i]->d_name); |
| 230 | |
| 231 | result = stat (filename, &st); |
| 232 | if (result < 0) { |
| 233 | perror_msg_and_die ("failed to stat component %s: %s", filename, |
| 234 | strerror (errno)); |
| 235 | } |
| 236 | if (S_ISREG(st.st_mode) && !access (filename, X_OK)) { |
| 237 | if (test_mode) |
| 238 | printf ("run-parts would run %s\n", filename); |
| 239 | else { |
| 240 | run_part (filename); |
| 241 | } |
| 242 | } |
| 243 | /*TODO convert to isdirectory() */ |
| 244 | else if (!S_ISDIR(st.st_mode)) { |
| 245 | printf ("run-parts: component %s is not an executable plain file\n", |
| 246 | filename); |
| 247 | exitstatus = 1; |
| 248 | } |
| 249 | |
| 250 | free (namelist[i]); |
| 251 | } |
| 252 | free (namelist); |
| 253 | free (filename); |
| 254 | } |
| 255 | |
| 256 | /* run_parts_main */ |
| 257 | /* Process options */ |
| 258 | int run_parts_main (int argc, char *argv[]) |
| 259 | { |
| 260 | umask (022); |
| 261 | add_argument(0); |
| 262 | |
| 263 | for (;;) { |
| 264 | int c; |
| 265 | |
| 266 | opterr = 0; |
| 267 | c = getopt(argc, argv, "tu:a:"); |
| 268 | |
| 269 | if (c == EOF) |
| 270 | break; |
| 271 | switch (c) { |
| 272 | case 't': /* Enable test mode */ |
| 273 | test_mode = 1; |
| 274 | break; |
| 275 | case 'u': /* Set the umask of the programs executed */ |
| 276 | set_umask (); |
| 277 | break; |
| 278 | case 'a': /* Pass an argument to the programs */ |
| 279 | add_argument (optarg); |
| 280 | break; |
| 281 | default: |
| 282 | show_usage(); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /* We require exactly one argument: the directory name */ |
| 287 | if (optind != (argc - 1)) { |
| 288 | show_usage(); |
| 289 | } |
| 290 | |
| 291 | run_parts (argv[optind]); |
| 292 | |
| 293 | return exitstatus; |
| 294 | } |