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 | * |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 5 | * Copyright (C) 2007 Bernhard Reutner-Fischer |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 6 | * |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 7 | * Based on a older version that was in busybox which was 1k big.. |
| 8 | * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it> |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 9 | * |
| 10 | * Based on the Debian run-parts program, version 1.15 |
| 11 | * Copyright (C) 1996 Jeff Noxon <jeff@router.patch.net>, |
| 12 | * Copyright (C) 1996-1999 Guy Maor <maor@debian.org> |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 13 | * |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 14 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 15 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | /* This is my first attempt to write a program in C (well, this is my first |
| 19 | * attempt to write a program! :-) . */ |
| 20 | |
| 21 | /* 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] | 22 | * 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] | 23 | * 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] | 24 | * broken compatibility because the BusyBox policy doesn't allow them. |
| 25 | * The supported options are: |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 26 | * -t test. Print the name of the files to be executed, without |
| 27 | * execute them. |
| 28 | * -a ARG argument. Pass ARG as an argument the program executed. It can |
| 29 | * be repeated to pass multiple arguments. |
| 30 | * -u MASK umask. Set the umask of the program executed to MASK. |
Eric Andersen | fb74a45 | 2001-12-18 14:06:03 +0000 | [diff] [blame] | 31 | */ |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 32 | |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 33 | //usage:#define run_parts_trivial_usage |
| 34 | //usage: "[-t] "IF_FEATURE_RUN_PARTS_FANCY("[-l] ")"[-a ARG] [-u MASK] DIRECTORY" |
| 35 | //usage:#define run_parts_full_usage "\n\n" |
| 36 | //usage: "Run a bunch of scripts in DIRECTORY\n" |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 37 | //usage: "\n -t Print what would be run, but don't actually run anything" |
| 38 | //usage: "\n -a ARG Pass ARG as argument for every program" |
| 39 | //usage: "\n -u MASK Set the umask to MASK before running every program" |
| 40 | //usage: IF_FEATURE_RUN_PARTS_FANCY( |
| 41 | //usage: "\n -l Print names of all matching files even if they are not executable" |
| 42 | //usage: ) |
| 43 | //usage: |
| 44 | //usage:#define run_parts_example_usage |
| 45 | //usage: "$ run-parts -a start /etc/init.d\n" |
| 46 | //usage: "$ run-parts -a stop=now /etc/init.d\n\n" |
| 47 | //usage: "Let's assume you have a script foo/dosomething:\n" |
| 48 | //usage: "#!/bin/sh\n" |
| 49 | //usage: "for i in $*; do eval $i; done; unset i\n" |
| 50 | //usage: "case \"$1\" in\n" |
| 51 | //usage: "start*) echo starting something;;\n" |
| 52 | //usage: "stop*) set -x; shutdown -h $stop;;\n" |
| 53 | //usage: "esac\n\n" |
| 54 | //usage: "Running this yields:\n" |
| 55 | //usage: "$run-parts -a stop=+4m foo/\n" |
| 56 | //usage: "+ shutdown -h +4m" |
| 57 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 58 | #include "libbb.h" |
| 59 | |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 60 | struct globals { |
| 61 | char **names; |
| 62 | int cur; |
| 63 | char *cmd[1]; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 64 | } FIX_ALIASING; |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 65 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 66 | #define names (G.names) |
| 67 | #define cur (G.cur ) |
| 68 | #define cmd (G.cmd ) |
Ian Wienand | 378ab68 | 2011-09-14 08:41:38 +0200 | [diff] [blame] | 69 | #define INIT_G() do { } while (0) |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 70 | |
Denis Vlasenko | d50dda8 | 2008-06-15 05:40:56 +0000 | [diff] [blame] | 71 | enum { NUM_CMD = (COMMON_BUFSIZE - sizeof(G)) / sizeof(cmd[0]) - 1 }; |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 72 | |
| 73 | enum { |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 74 | OPT_r = (1 << 0), |
| 75 | OPT_a = (1 << 1), |
| 76 | OPT_u = (1 << 2), |
| 77 | OPT_t = (1 << 3), |
| 78 | OPT_l = (1 << 4) * ENABLE_FEATURE_RUN_PARTS_FANCY, |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | #if ENABLE_FEATURE_RUN_PARTS_FANCY |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 82 | #define list_mode (option_mask32 & OPT_l) |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 83 | #else |
| 84 | #define list_mode 0 |
| 85 | #endif |
| 86 | |
| 87 | /* Is this a valid filename (upper/lower alpha, digits, |
| 88 | * underscores, and hyphens only?) |
| 89 | */ |
| 90 | static bool invalid_name(const char *c) |
| 91 | { |
| 92 | c = bb_basename(c); |
| 93 | |
| 94 | while (*c && (isalnum(*c) || *c == '_' || *c == '-')) |
| 95 | c++; |
| 96 | |
| 97 | return *c; /* TRUE (!0) if terminating NUL is not reached */ |
| 98 | } |
| 99 | |
| 100 | static int bb_alphasort(const void *p1, const void *p2) |
| 101 | { |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 102 | int r = strcmp(*(char **) p1, *(char **) p2); |
| 103 | return (option_mask32 & OPT_r) ? -r : r; |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 106 | static int FAST_FUNC act(const char *file, struct stat *statbuf, void *args UNUSED_PARAM, int depth) |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 107 | { |
| 108 | if (depth == 1) |
| 109 | return TRUE; |
| 110 | |
| 111 | if (depth == 2 |
| 112 | && ( !(statbuf->st_mode & (S_IFREG | S_IFLNK)) |
| 113 | || invalid_name(file) |
| 114 | || (!list_mode && access(file, X_OK) != 0)) |
| 115 | ) { |
| 116 | return SKIP; |
| 117 | } |
| 118 | |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 119 | names = xrealloc_vector(names, 4, cur); |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 120 | names[cur++] = xstrdup(file); |
Denis Vlasenko | 2784228 | 2008-08-04 13:20:36 +0000 | [diff] [blame] | 121 | /*names[cur] = NULL; - xrealloc_vector did it */ |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 122 | |
| 123 | return TRUE; |
| 124 | } |
| 125 | |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 126 | #if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 127 | static const char runparts_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 128 | "arg\0" Required_argument "a" |
| 129 | "umask\0" Required_argument "u" |
| 130 | "test\0" No_argument "t" |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 131 | #if ENABLE_FEATURE_RUN_PARTS_FANCY |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 132 | "list\0" No_argument "l" |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 133 | "reverse\0" No_argument "r" |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 134 | //TODO: "verbose\0" No_argument "v" |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 135 | #endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 136 | ; |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 137 | #endif |
| 138 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 139 | int run_parts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 140 | int run_parts_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 141 | { |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 142 | const char *umask_p = "22"; |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 143 | llist_t *arg_list = NULL; |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 144 | unsigned n; |
| 145 | int ret; |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 146 | |
Ian Wienand | 378ab68 | 2011-09-14 08:41:38 +0200 | [diff] [blame] | 147 | INIT_G(); |
| 148 | |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 149 | #if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 150 | applet_long_options = runparts_longopts; |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 151 | #endif |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 152 | /* We require exactly one argument: the directory name */ |
| 153 | opt_complementary = "=1:a::"; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 154 | getopt32(argv, "ra:u:t"IF_FEATURE_RUN_PARTS_FANCY("l"), &arg_list, &umask_p); |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 155 | |
| 156 | umask(xstrtou_range(umask_p, 8, 0, 07777)); |
| 157 | |
| 158 | n = 1; |
| 159 | while (arg_list && n < NUM_CMD) { |
Denis Vlasenko | d50dda8 | 2008-06-15 05:40:56 +0000 | [diff] [blame] | 160 | cmd[n++] = llist_pop(&arg_list); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 161 | } |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 162 | /* cmd[n] = NULL; - is already zeroed out */ |
| 163 | |
| 164 | /* run-parts has to sort executables by name before running them */ |
| 165 | |
| 166 | recursive_action(argv[optind], |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 167 | ACTION_RECURSE|ACTION_FOLLOWLINKS, |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 168 | act, /* file action */ |
| 169 | act, /* dir action */ |
| 170 | NULL, /* user data */ |
| 171 | 1 /* depth */ |
| 172 | ); |
| 173 | |
| 174 | if (!names) |
| 175 | return 0; |
| 176 | |
| 177 | qsort(names, cur, sizeof(char *), bb_alphasort); |
| 178 | |
| 179 | n = 0; |
| 180 | while (1) { |
| 181 | char *name = *names++; |
| 182 | if (!name) |
| 183 | break; |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 184 | if (option_mask32 & (OPT_t | OPT_l)) { |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 185 | puts(name); |
| 186 | continue; |
| 187 | } |
| 188 | cmd[0] = name; |
Denys Vlasenko | 8531d76 | 2010-03-18 22:44:00 +0100 | [diff] [blame] | 189 | ret = spawn_and_wait(cmd); |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 190 | if (ret == 0) |
| 191 | continue; |
| 192 | n = 1; |
| 193 | if (ret < 0) |
Denis Vlasenko | f9d4fc3 | 2009-04-21 20:40:51 +0000 | [diff] [blame] | 194 | bb_perror_msg("can't execute '%s'", name); |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 195 | else /* ret > 0 */ |
Denys Vlasenko | 8531d76 | 2010-03-18 22:44:00 +0100 | [diff] [blame] | 196 | bb_error_msg("%s exited with code %d", name, ret & 0xff); |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | return n; |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 200 | } |