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> |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 11 | * |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 12 | * |
Bernhard Reutner-Fischer | 7fee0c4 | 2006-09-13 16:39:19 +0000 | [diff] [blame] | 13 | * Licensed under GPL v2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | /* This is my first attempt to write a program in C (well, this is my first |
| 17 | * attempt to write a program! :-) . */ |
| 18 | |
| 19 | /* This piece of code is heavily based on the original version of run-parts, |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 20 | * taken from debian-utils. I've only removed the long options and a the |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 21 | * report mode. As the original run-parts support only long options, I've |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 22 | * broken compatibility because the BusyBox policy doesn't allow them. |
| 23 | * The supported options are: |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 24 | * -t test. Print the name of the files to be executed, without |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 25 | * execute them. |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 26 | * -a ARG argument. Pass ARG as an argument the program executed. It can |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 27 | * be repeated to pass multiple arguments. |
| 28 | * -u MASK umask. Set the umask of the program executed to MASK. */ |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 29 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 30 | /* TODO |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 31 | * done - convert calls to error in perror... and remove error() |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 32 | * done - convert malloc/realloc to their x... counterparts |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 33 | * done - remove catch_sigchld |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 34 | * done - use bb's concat_path_file() |
Eric Andersen | fb74a45 | 2001-12-18 14:06:03 +0000 | [diff] [blame] | 35 | * done - declare run_parts_main() as extern and any other function as static? |
| 36 | */ |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 37 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 38 | #include "busybox.h" |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 39 | #include <getopt.h> |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 40 | |
Eric Andersen | a1ed06b | 2003-07-26 09:16:00 +0000 | [diff] [blame] | 41 | static const struct option runparts_long_options[] = { |
Denis Vlasenko | e5667c1 | 2006-11-26 20:13:39 +0000 | [diff] [blame] | 42 | { "test", 0, NULL, 't' }, |
| 43 | { "umask", 1, NULL, 'u' }, |
| 44 | { "arg", 1, NULL, 'a' }, |
| 45 | { 0, 0, 0, 0 } |
Eric Andersen | a1ed06b | 2003-07-26 09:16:00 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
Denis Vlasenko | e5667c1 | 2006-11-26 20:13:39 +0000 | [diff] [blame] | 48 | /* valid_name */ |
| 49 | /* True or false? Is this a valid filename (upper/lower alpha, digits, |
| 50 | * underscores, and hyphens only?) |
| 51 | */ |
| 52 | static int valid_name(const struct dirent *d) |
| 53 | { |
| 54 | const char *c = d->d_name; |
| 55 | |
| 56 | while (*c) { |
| 57 | if (!isalnum(*c) && (*c != '_') && (*c != '-')) { |
| 58 | return 0; |
| 59 | } |
| 60 | ++c; |
| 61 | } |
| 62 | return 1; |
| 63 | } |
| 64 | |
| 65 | /* test mode = 1 is the same as official run_parts |
| 66 | * test_mode = 2 means to fail silently on missing directories |
| 67 | */ |
| 68 | static int run_parts(char **args, const unsigned char test_mode) |
| 69 | { |
| 70 | struct dirent **namelist = 0; |
| 71 | struct stat st; |
| 72 | char *filename; |
| 73 | char *arg0 = args[0]; |
| 74 | int entries; |
| 75 | int i; |
| 76 | int exitstatus = 0; |
| 77 | |
| 78 | #if __GNUC__ |
| 79 | /* Avoid longjmp clobbering */ |
| 80 | (void) &i; |
| 81 | (void) &exitstatus; |
| 82 | #endif |
| 83 | /* scandir() isn't POSIX, but it makes things easy. */ |
| 84 | entries = scandir(arg0, &namelist, valid_name, alphasort); |
| 85 | |
| 86 | if (entries == -1) { |
| 87 | if (test_mode & 2) { |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 88 | return 2; |
Denis Vlasenko | e5667c1 | 2006-11-26 20:13:39 +0000 | [diff] [blame] | 89 | } |
| 90 | bb_perror_msg_and_die("cannot open '%s'", arg0); |
| 91 | } |
| 92 | |
| 93 | for (i = 0; i < entries; i++) { |
| 94 | filename = concat_path_file(arg0, namelist[i]->d_name); |
| 95 | |
| 96 | xstat(filename, &st); |
| 97 | if (S_ISREG(st.st_mode) && !access(filename, X_OK)) { |
| 98 | if (test_mode) { |
| 99 | puts(filename); |
| 100 | } else { |
| 101 | /* exec_errno is common vfork variable */ |
| 102 | volatile int exec_errno = 0; |
| 103 | int result; |
| 104 | int pid; |
| 105 | |
| 106 | if ((pid = vfork()) < 0) { |
| 107 | bb_perror_msg_and_die("failed to fork"); |
| 108 | } else if (!pid) { |
| 109 | args[0] = filename; |
| 110 | execve(filename, args, environ); |
| 111 | exec_errno = errno; |
| 112 | _exit(1); |
| 113 | } |
| 114 | |
| 115 | waitpid(pid, &result, 0); |
| 116 | if (exec_errno) { |
| 117 | errno = exec_errno; |
| 118 | bb_perror_msg("failed to exec %s", filename); |
| 119 | exitstatus = 1; |
| 120 | } |
| 121 | if (WIFEXITED(result) && WEXITSTATUS(result)) { |
| 122 | bb_perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result)); |
| 123 | exitstatus = 1; |
| 124 | } else if (WIFSIGNALED(result)) { |
| 125 | bb_perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result)); |
| 126 | exitstatus = 1; |
| 127 | } |
| 128 | } |
| 129 | } else if (!S_ISDIR(st.st_mode)) { |
| 130 | bb_error_msg("component %s is not an executable plain file", filename); |
| 131 | exitstatus = 1; |
| 132 | } |
| 133 | |
| 134 | free(namelist[i]); |
| 135 | free(filename); |
| 136 | } |
| 137 | free(namelist); |
| 138 | |
| 139 | return exitstatus; |
| 140 | } |
| 141 | |
Eric Andersen | 2a18689 | 2003-07-29 07:05:40 +0000 | [diff] [blame] | 142 | |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 143 | /* run_parts_main */ |
| 144 | /* Process options */ |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame^] | 145 | int run_parts_main(int argc, char **argv); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 146 | int run_parts_main(int argc, char **argv) |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 147 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 148 | char **args = xmalloc(2 * sizeof(char *)); |
| 149 | unsigned char test_mode = 0; |
| 150 | unsigned short argcount = 1; |
| 151 | int opt; |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 152 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 153 | umask(022); |
| 154 | |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 155 | while ((opt = getopt_long(argc, argv, "tu:a:", |
Eric Andersen | a1ed06b | 2003-07-26 09:16:00 +0000 | [diff] [blame] | 156 | runparts_long_options, NULL)) > 0) |
| 157 | { |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 158 | switch (opt) { |
Denis Vlasenko | 1385899 | 2006-10-08 12:49:22 +0000 | [diff] [blame] | 159 | /* Enable test mode */ |
| 160 | case 't': |
| 161 | test_mode++; |
| 162 | break; |
| 163 | /* Set the umask of the programs executed */ |
| 164 | case 'u': |
| 165 | /* Check and set the umask of the program executed. As stated in the original |
| 166 | * run-parts, the octal conversion in libc is not foolproof; it will take the |
| 167 | * 8 and 9 digits under some circumstances. We'll just have to live with it. |
| 168 | */ |
| 169 | umask(xstrtoul_range(optarg, 8, 0, 07777)); |
| 170 | break; |
| 171 | /* Pass an argument to the programs */ |
| 172 | case 'a': |
| 173 | /* Add an argument to the commands that we will call. |
| 174 | * Called once for every argument. */ |
| 175 | args = xrealloc(args, (argcount + 2) * (sizeof(char *))); |
| 176 | args[argcount++] = optarg; |
| 177 | break; |
| 178 | default: |
| 179 | bb_show_usage(); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | /* We require exactly one argument: the directory name */ |
| 184 | if (optind != (argc - 1)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 185 | bb_show_usage(); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 186 | } |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 187 | |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 188 | args[0] = argv[optind]; |
Glenn L McGrath | a69fd2e | 2003-01-08 03:26:47 +0000 | [diff] [blame] | 189 | args[argcount] = 0; |
| 190 | |
Denis Vlasenko | e5667c1 | 2006-11-26 20:13:39 +0000 | [diff] [blame] | 191 | return run_parts(args, test_mode); |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 192 | } |