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 | * |
Denys Vlasenko | 1d7ad7a | 2012-06-21 09:45:11 +0200 | [diff] [blame] | 7 | * Based on a older version that was in busybox which was 1k big. |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 14 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* This is my first attempt to write a program in C (well, this is my first |
| 18 | * attempt to write a program! :-) . */ |
| 19 | |
| 20 | /* This piece of code is heavily based on the original version of run-parts, |
Denys Vlasenko | 1d7ad7a | 2012-06-21 09:45:11 +0200 | [diff] [blame] | 21 | * taken from debian-utils. I've only removed the long options and the |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 22 | * 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] | 23 | * broken compatibility because the BusyBox policy doesn't allow them. |
Eric Andersen | fb74a45 | 2001-12-18 14:06:03 +0000 | [diff] [blame] | 24 | */ |
Denys Vlasenko | 28826ac | 2015-10-19 00:52:26 +0200 | [diff] [blame] | 25 | //config:config RUN_PARTS |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 26 | //config: bool "run-parts (6.1 kb)" |
Denys Vlasenko | 28826ac | 2015-10-19 00:52:26 +0200 | [diff] [blame] | 27 | //config: default y |
| 28 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 29 | //config: run-parts is a utility designed to run all the scripts in a directory. |
Denys Vlasenko | 28826ac | 2015-10-19 00:52:26 +0200 | [diff] [blame] | 30 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 31 | //config: It is useful to set up a directory like cron.daily, where you need to |
| 32 | //config: execute all the scripts in that directory. |
Denys Vlasenko | 28826ac | 2015-10-19 00:52:26 +0200 | [diff] [blame] | 33 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 34 | //config: In this implementation of run-parts some features (such as report |
| 35 | //config: mode) are not implemented. |
Denys Vlasenko | 28826ac | 2015-10-19 00:52:26 +0200 | [diff] [blame] | 36 | //config: |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 37 | //config: Unless you know that run-parts is used in some of your scripts |
| 38 | //config: you can safely say N here. |
Denys Vlasenko | 28826ac | 2015-10-19 00:52:26 +0200 | [diff] [blame] | 39 | //config: |
| 40 | //config:config FEATURE_RUN_PARTS_LONG_OPTIONS |
| 41 | //config: bool "Enable long options" |
| 42 | //config: default y |
| 43 | //config: depends on RUN_PARTS && LONG_OPTS |
Denys Vlasenko | 28826ac | 2015-10-19 00:52:26 +0200 | [diff] [blame] | 44 | //config: |
| 45 | //config:config FEATURE_RUN_PARTS_FANCY |
| 46 | //config: bool "Support additional arguments" |
| 47 | //config: default y |
| 48 | //config: depends on RUN_PARTS |
| 49 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 50 | //config: Support additional options: |
| 51 | //config: -l --list print the names of the all matching files (not |
| 52 | //config: limited to executables), but don't actually run them. |
Denys Vlasenko | 28826ac | 2015-10-19 00:52:26 +0200 | [diff] [blame] | 53 | |
| 54 | //applet:IF_RUN_PARTS(APPLET_ODDNAME(run-parts, run_parts, BB_DIR_BIN, BB_SUID_DROP, run_parts)) |
| 55 | |
| 56 | //kbuild:lib-$(CONFIG_RUN_PARTS) += run_parts.o |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 57 | |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 58 | //usage:#define run_parts_trivial_usage |
Denys Vlasenko | 1e43a38 | 2013-02-28 10:22:49 +0100 | [diff] [blame] | 59 | //usage: "[-a ARG]... [-u UMASK] " |
Peter Korsgaard | 5d1c599 | 2013-02-28 12:25:49 +0100 | [diff] [blame] | 60 | //usage: IF_FEATURE_RUN_PARTS_LONG_OPTIONS("[--reverse] [--test] [--exit-on-error] "IF_FEATURE_RUN_PARTS_FANCY("[--list] ")) |
Denys Vlasenko | 1e43a38 | 2013-02-28 10:22:49 +0100 | [diff] [blame] | 61 | //usage: "DIRECTORY" |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 62 | //usage:#define run_parts_full_usage "\n\n" |
| 63 | //usage: "Run a bunch of scripts in DIRECTORY\n" |
Denys Vlasenko | 1e43a38 | 2013-02-28 10:22:49 +0100 | [diff] [blame] | 64 | //usage: "\n -a ARG Pass ARG as argument to scripts" |
| 65 | //usage: "\n -u UMASK Set UMASK before running scripts" |
| 66 | //usage: IF_FEATURE_RUN_PARTS_LONG_OPTIONS( |
Peter Korsgaard | 5d1c599 | 2013-02-28 12:25:49 +0100 | [diff] [blame] | 67 | //usage: "\n --reverse Reverse execution order" |
Denys Vlasenko | 1e43a38 | 2013-02-28 10:22:49 +0100 | [diff] [blame] | 68 | //usage: "\n --test Dry run" |
Peter Korsgaard | 5d1c599 | 2013-02-28 12:25:49 +0100 | [diff] [blame] | 69 | //usage: "\n --exit-on-error Exit if a script exits with non-zero" |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 70 | //usage: IF_FEATURE_RUN_PARTS_FANCY( |
Denys Vlasenko | 1e43a38 | 2013-02-28 10:22:49 +0100 | [diff] [blame] | 71 | //usage: "\n --list Print names of matching files even if they are not executable" |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 72 | //usage: ) |
Denys Vlasenko | 1e43a38 | 2013-02-28 10:22:49 +0100 | [diff] [blame] | 73 | //usage: ) |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 74 | //usage: |
| 75 | //usage:#define run_parts_example_usage |
| 76 | //usage: "$ run-parts -a start /etc/init.d\n" |
| 77 | //usage: "$ run-parts -a stop=now /etc/init.d\n\n" |
| 78 | //usage: "Let's assume you have a script foo/dosomething:\n" |
| 79 | //usage: "#!/bin/sh\n" |
| 80 | //usage: "for i in $*; do eval $i; done; unset i\n" |
| 81 | //usage: "case \"$1\" in\n" |
| 82 | //usage: "start*) echo starting something;;\n" |
| 83 | //usage: "stop*) set -x; shutdown -h $stop;;\n" |
| 84 | //usage: "esac\n\n" |
| 85 | //usage: "Running this yields:\n" |
| 86 | //usage: "$run-parts -a stop=+4m foo/\n" |
| 87 | //usage: "+ shutdown -h +4m" |
| 88 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 89 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 90 | #include "common_bufsiz.h" |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 91 | |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 92 | struct globals { |
| 93 | char **names; |
| 94 | int cur; |
Denys Vlasenko | 5117eff | 2013-10-16 14:21:20 +0200 | [diff] [blame] | 95 | char *cmd[2 /* using 1 provokes compiler warning */]; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 96 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 97 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 98 | #define names (G.names) |
| 99 | #define cur (G.cur ) |
| 100 | #define cmd (G.cmd ) |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 101 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 102 | |
Denis Vlasenko | d50dda8 | 2008-06-15 05:40:56 +0000 | [diff] [blame] | 103 | enum { NUM_CMD = (COMMON_BUFSIZE - sizeof(G)) / sizeof(cmd[0]) - 1 }; |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 104 | |
| 105 | enum { |
Denys Vlasenko | 1e43a38 | 2013-02-28 10:22:49 +0100 | [diff] [blame] | 106 | OPT_a = (1 << 0), |
| 107 | OPT_u = (1 << 1), |
| 108 | OPT_r = (1 << 2) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS, |
| 109 | OPT_t = (1 << 3) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS, |
| 110 | OPT_e = (1 << 4) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS, |
| 111 | OPT_l = (1 << 5) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS |
| 112 | * ENABLE_FEATURE_RUN_PARTS_FANCY, |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 115 | /* Is this a valid filename (upper/lower alpha, digits, |
| 116 | * underscores, and hyphens only?) |
| 117 | */ |
| 118 | static bool invalid_name(const char *c) |
| 119 | { |
| 120 | c = bb_basename(c); |
| 121 | |
| 122 | while (*c && (isalnum(*c) || *c == '_' || *c == '-')) |
| 123 | c++; |
| 124 | |
| 125 | return *c; /* TRUE (!0) if terminating NUL is not reached */ |
| 126 | } |
| 127 | |
| 128 | static int bb_alphasort(const void *p1, const void *p2) |
| 129 | { |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 130 | int r = strcmp(*(char **) p1, *(char **) p2); |
| 131 | return (option_mask32 & OPT_r) ? -r : r; |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 134 | 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] | 135 | { |
Denys Vlasenko | 3c3928f | 2020-10-01 20:27:28 +0200 | [diff] [blame^] | 136 | if (depth == 0) |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 137 | return TRUE; |
| 138 | |
Denys Vlasenko | 3c3928f | 2020-10-01 20:27:28 +0200 | [diff] [blame^] | 139 | if (depth == 1 |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 140 | && ( !(statbuf->st_mode & (S_IFREG | S_IFLNK)) |
| 141 | || invalid_name(file) |
Denys Vlasenko | 1e43a38 | 2013-02-28 10:22:49 +0100 | [diff] [blame] | 142 | || (!(option_mask32 & OPT_l) && access(file, X_OK) != 0)) |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 143 | ) { |
| 144 | return SKIP; |
| 145 | } |
| 146 | |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 147 | names = xrealloc_vector(names, 4, cur); |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 148 | names[cur++] = xstrdup(file); |
Denis Vlasenko | 2784228 | 2008-08-04 13:20:36 +0000 | [diff] [blame] | 149 | /*names[cur] = NULL; - xrealloc_vector did it */ |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 150 | |
| 151 | return TRUE; |
| 152 | } |
| 153 | |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 154 | #if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 155 | static const char runparts_longopts[] ALIGN1 = |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 156 | "arg\0" Required_argument "a" |
| 157 | "umask\0" Required_argument "u" |
Denis Vlasenko | bdc88fd | 2007-07-23 17:14:14 +0000 | [diff] [blame] | 158 | //TODO: "verbose\0" No_argument "v" |
Denys Vlasenko | 1e43a38 | 2013-02-28 10:22:49 +0100 | [diff] [blame] | 159 | "reverse\0" No_argument "\xf0" |
| 160 | "test\0" No_argument "\xf1" |
| 161 | "exit-on-error\0" No_argument "\xf2" |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 162 | # if ENABLE_FEATURE_RUN_PARTS_FANCY |
Denys Vlasenko | 1e43a38 | 2013-02-28 10:22:49 +0100 | [diff] [blame] | 163 | "list\0" No_argument "\xf3" |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 164 | # endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 165 | ; |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame] | 166 | # define GETOPT32 getopt32long |
| 167 | # define LONGOPTS ,runparts_longopts |
| 168 | #else |
| 169 | # define GETOPT32 getopt32 |
| 170 | # define LONGOPTS |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 171 | #endif |
| 172 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 173 | int run_parts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 174 | int run_parts_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 175 | { |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 176 | const char *umask_p = "22"; |
Bernhard Reutner-Fischer | b7cffd4 | 2007-03-28 20:35:13 +0000 | [diff] [blame] | 177 | llist_t *arg_list = NULL; |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 178 | unsigned n; |
| 179 | int ret; |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 180 | |
Ian Wienand | 378ab68 | 2011-09-14 08:41:38 +0200 | [diff] [blame] | 181 | INIT_G(); |
| 182 | |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 183 | /* We require exactly one argument: the directory name */ |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 184 | GETOPT32(argv, "^" "a:*u:" "\0" "=1" LONGOPTS, |
| 185 | &arg_list, &umask_p |
| 186 | ); |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 187 | |
| 188 | umask(xstrtou_range(umask_p, 8, 0, 07777)); |
| 189 | |
| 190 | n = 1; |
| 191 | while (arg_list && n < NUM_CMD) { |
Denis Vlasenko | d50dda8 | 2008-06-15 05:40:56 +0000 | [diff] [blame] | 192 | cmd[n++] = llist_pop(&arg_list); |
Glenn L McGrath | 545106f | 2002-11-11 06:21:00 +0000 | [diff] [blame] | 193 | } |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 194 | /* cmd[n] = NULL; - is already zeroed out */ |
| 195 | |
| 196 | /* run-parts has to sort executables by name before running them */ |
| 197 | |
| 198 | recursive_action(argv[optind], |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 199 | ACTION_RECURSE|ACTION_FOLLOWLINKS, |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 200 | act, /* file action */ |
| 201 | act, /* dir action */ |
| 202 | NULL, /* user data */ |
Denys Vlasenko | 3c3928f | 2020-10-01 20:27:28 +0200 | [diff] [blame^] | 203 | 0 /* depth */ |
| 204 | ); |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 205 | |
| 206 | if (!names) |
| 207 | return 0; |
| 208 | |
| 209 | qsort(names, cur, sizeof(char *), bb_alphasort); |
| 210 | |
| 211 | n = 0; |
| 212 | while (1) { |
| 213 | char *name = *names++; |
| 214 | if (!name) |
| 215 | break; |
Denis Vlasenko | 19fb67e | 2008-02-28 21:30:22 +0000 | [diff] [blame] | 216 | if (option_mask32 & (OPT_t | OPT_l)) { |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 217 | puts(name); |
| 218 | continue; |
| 219 | } |
| 220 | cmd[0] = name; |
Denys Vlasenko | 8531d76 | 2010-03-18 22:44:00 +0100 | [diff] [blame] | 221 | ret = spawn_and_wait(cmd); |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 222 | if (ret == 0) |
| 223 | continue; |
| 224 | n = 1; |
| 225 | if (ret < 0) |
Denis Vlasenko | f9d4fc3 | 2009-04-21 20:40:51 +0000 | [diff] [blame] | 226 | bb_perror_msg("can't execute '%s'", name); |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 227 | else /* ret > 0 */ |
Denys Vlasenko | 7f3a2a2 | 2015-10-08 11:24:44 +0200 | [diff] [blame] | 228 | bb_error_msg("%s: exit status %u", name, ret & 0xff); |
Peter Korsgaard | 0496e82 | 2013-02-28 09:59:23 +0100 | [diff] [blame] | 229 | |
| 230 | if (option_mask32 & OPT_e) |
| 231 | xfunc_die(); |
Denis Vlasenko | 2a1d024 | 2007-09-23 18:34:49 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | return n; |
Eric Andersen | 7d68290 | 2001-10-31 10:59:29 +0000 | [diff] [blame] | 235 | } |