blob: f528c88ffdbbf923b6be7b91d03cb8e8e15a02a1 [file] [log] [blame]
Eric Andersen7d682902001-10-31 10:59:29 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini run-parts implementation for busybox
4 *
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00005 * Copyright (C) 2007 Bernhard Reutner-Fischer
Eric Andersen7d682902001-10-31 10:59:29 +00006 *
Denys Vlasenko1d7ad7a2012-06-21 09:45:11 +02007 * Based on a older version that was in busybox which was 1k big.
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +00008 * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it>
Eric Andersen7d682902001-10-31 10:59:29 +00009 *
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 Andersenc7bda1c2004-03-15 08:29:22 +000013 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020014 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen7d682902001-10-31 10:59:29 +000015 */
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 Vlasenko1d7ad7a2012-06-21 09:45:11 +020021 * taken from debian-utils. I've only removed the long options and the
Eric Andersen7d682902001-10-31 10:59:29 +000022 * report mode. As the original run-parts support only long options, I've
Eric Andersenc7bda1c2004-03-15 08:29:22 +000023 * broken compatibility because the BusyBox policy doesn't allow them.
Eric Andersenfb74a452001-12-18 14:06:03 +000024 */
Denys Vlasenko28826ac2015-10-19 00:52:26 +020025//config:config RUN_PARTS
Denys Vlasenkob097a842018-12-28 03:20:17 +010026//config: bool "run-parts (6.1 kb)"
Denys Vlasenko28826ac2015-10-19 00:52:26 +020027//config: default y
28//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020029//config: run-parts is a utility designed to run all the scripts in a directory.
Denys Vlasenko28826ac2015-10-19 00:52:26 +020030//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020031//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 Vlasenko28826ac2015-10-19 00:52:26 +020033//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020034//config: In this implementation of run-parts some features (such as report
35//config: mode) are not implemented.
Denys Vlasenko28826ac2015-10-19 00:52:26 +020036//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020037//config: Unless you know that run-parts is used in some of your scripts
38//config: you can safely say N here.
Denys Vlasenko28826ac2015-10-19 00:52:26 +020039//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 Vlasenko28826ac2015-10-19 00:52:26 +020044//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 Vlasenko72089cf2017-07-21 09:50:55 +020050//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 Vlasenko28826ac2015-10-19 00:52:26 +020053
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 Andersen7d682902001-10-31 10:59:29 +000057
Pere Orga6a3e01d2011-04-01 22:56:30 +020058//usage:#define run_parts_trivial_usage
Denys Vlasenko1e43a382013-02-28 10:22:49 +010059//usage: "[-a ARG]... [-u UMASK] "
Peter Korsgaard5d1c5992013-02-28 12:25:49 +010060//usage: IF_FEATURE_RUN_PARTS_LONG_OPTIONS("[--reverse] [--test] [--exit-on-error] "IF_FEATURE_RUN_PARTS_FANCY("[--list] "))
Denys Vlasenko1e43a382013-02-28 10:22:49 +010061//usage: "DIRECTORY"
Pere Orga6a3e01d2011-04-01 22:56:30 +020062//usage:#define run_parts_full_usage "\n\n"
63//usage: "Run a bunch of scripts in DIRECTORY\n"
Denys Vlasenko1e43a382013-02-28 10:22:49 +010064//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 Korsgaard5d1c5992013-02-28 12:25:49 +010067//usage: "\n --reverse Reverse execution order"
Denys Vlasenko1e43a382013-02-28 10:22:49 +010068//usage: "\n --test Dry run"
Peter Korsgaard5d1c5992013-02-28 12:25:49 +010069//usage: "\n --exit-on-error Exit if a script exits with non-zero"
Pere Orga6a3e01d2011-04-01 22:56:30 +020070//usage: IF_FEATURE_RUN_PARTS_FANCY(
Denys Vlasenko1e43a382013-02-28 10:22:49 +010071//usage: "\n --list Print names of matching files even if they are not executable"
Pere Orga6a3e01d2011-04-01 22:56:30 +020072//usage: )
Denys Vlasenko1e43a382013-02-28 10:22:49 +010073//usage: )
Pere Orga6a3e01d2011-04-01 22:56:30 +020074//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 Vlasenkob6adbf12007-05-26 19:00:18 +000089#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020090#include "common_bufsiz.h"
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000091
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000092struct globals {
93 char **names;
94 int cur;
Denys Vlasenko5117eff2013-10-16 14:21:20 +020095 char *cmd[2 /* using 1 provokes compiler warning */];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010096} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020097#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000098#define names (G.names)
99#define cur (G.cur )
100#define cmd (G.cmd )
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200101#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000102
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000103enum { NUM_CMD = (COMMON_BUFSIZE - sizeof(G)) / sizeof(cmd[0]) - 1 };
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000104
105enum {
Denys Vlasenko1e43a382013-02-28 10:22:49 +0100106 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 Vlasenko2a1d0242007-09-23 18:34:49 +0000113};
114
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000115/* Is this a valid filename (upper/lower alpha, digits,
Ján Sáreník8c1f8aa2021-06-05 18:24:57 +0200116 * underscores, hyphens, and non-leading dots only?)
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000117 */
118static bool invalid_name(const char *c)
119{
120 c = bb_basename(c);
121
Ján Sáreník8c1f8aa2021-06-05 18:24:57 +0200122 if (*c == '.')
123 return *c;
124
125 /* Debian run-parts 4.8.3, manpage:
126 * "...the names must consist entirely of ASCII letters,
127 * ASCII digits, ASCII underscores, and ASCII minus-hyphens.
128 * However, the name must not begin with a period."
129 * The last sentence is a giveaway that something is fishy
130 * (why mention leading dot if dots are not allowed anyway?).
131 * Yes, you guessed it right: in fact non-leading dots ARE allowed.
132 */
133 while (isalnum(*c) || *c == '_' || *c == '-' || *c == '.')
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000134 c++;
135
136 return *c; /* TRUE (!0) if terminating NUL is not reached */
137}
138
139static int bb_alphasort(const void *p1, const void *p2)
140{
Denis Vlasenko19fb67e2008-02-28 21:30:22 +0000141 int r = strcmp(*(char **) p1, *(char **) p2);
142 return (option_mask32 & OPT_r) ? -r : r;
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000143}
144
Denys Vlasenko689d0652020-10-01 21:52:16 +0200145static int FAST_FUNC act(struct recursive_state *state,
146 const char *file, struct stat *statbuf)
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000147{
Denys Vlasenko689d0652020-10-01 21:52:16 +0200148 if (state->depth == 0)
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000149 return TRUE;
150
Denys Vlasenko689d0652020-10-01 21:52:16 +0200151 if (state->depth == 1
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000152 && ( !(statbuf->st_mode & (S_IFREG | S_IFLNK))
153 || invalid_name(file)
Denys Vlasenko1e43a382013-02-28 10:22:49 +0100154 || (!(option_mask32 & OPT_l) && access(file, X_OK) != 0))
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000155 ) {
156 return SKIP;
157 }
158
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000159 names = xrealloc_vector(names, 4, cur);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000160 names[cur++] = xstrdup(file);
Denis Vlasenko27842282008-08-04 13:20:36 +0000161 /*names[cur] = NULL; - xrealloc_vector did it */
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000162
163 return TRUE;
164}
165
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000166#if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000167static const char runparts_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000168 "arg\0" Required_argument "a"
169 "umask\0" Required_argument "u"
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000170//TODO: "verbose\0" No_argument "v"
Denys Vlasenko1e43a382013-02-28 10:22:49 +0100171 "reverse\0" No_argument "\xf0"
172 "test\0" No_argument "\xf1"
173 "exit-on-error\0" No_argument "\xf2"
Denys Vlasenko036585a2017-08-08 16:38:18 +0200174# if ENABLE_FEATURE_RUN_PARTS_FANCY
Denys Vlasenko1e43a382013-02-28 10:22:49 +0100175 "list\0" No_argument "\xf3"
Denys Vlasenko036585a2017-08-08 16:38:18 +0200176# endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000177 ;
Denys Vlasenko036585a2017-08-08 16:38:18 +0200178# define GETOPT32 getopt32long
179# define LONGOPTS ,runparts_longopts
180#else
181# define GETOPT32 getopt32
182# define LONGOPTS
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000183#endif
184
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000185int run_parts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000186int run_parts_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen7d682902001-10-31 10:59:29 +0000187{
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000188 const char *umask_p = "22";
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000189 llist_t *arg_list = NULL;
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000190 unsigned n;
191 int ret;
Eric Andersen7d682902001-10-31 10:59:29 +0000192
Ian Wienand378ab682011-09-14 08:41:38 +0200193 INIT_G();
194
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000195 /* We require exactly one argument: the directory name */
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200196 GETOPT32(argv, "^" "a:*u:" "\0" "=1" LONGOPTS,
197 &arg_list, &umask_p
198 );
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000199
200 umask(xstrtou_range(umask_p, 8, 0, 07777));
201
202 n = 1;
203 while (arg_list && n < NUM_CMD) {
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000204 cmd[n++] = llist_pop(&arg_list);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000205 }
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000206 /* cmd[n] = NULL; - is already zeroed out */
207
208 /* run-parts has to sort executables by name before running them */
209
210 recursive_action(argv[optind],
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000211 ACTION_RECURSE|ACTION_FOLLOWLINKS,
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000212 act, /* file action */
213 act, /* dir action */
Denys Vlasenko689d0652020-10-01 21:52:16 +0200214 NULL /* user data */
Denys Vlasenko3c3928f2020-10-01 20:27:28 +0200215 );
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000216
217 if (!names)
218 return 0;
219
220 qsort(names, cur, sizeof(char *), bb_alphasort);
221
222 n = 0;
223 while (1) {
224 char *name = *names++;
225 if (!name)
226 break;
Denis Vlasenko19fb67e2008-02-28 21:30:22 +0000227 if (option_mask32 & (OPT_t | OPT_l)) {
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000228 puts(name);
229 continue;
230 }
231 cmd[0] = name;
Denys Vlasenko8531d762010-03-18 22:44:00 +0100232 ret = spawn_and_wait(cmd);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000233 if (ret == 0)
234 continue;
235 n = 1;
236 if (ret < 0)
Denis Vlasenkof9d4fc32009-04-21 20:40:51 +0000237 bb_perror_msg("can't execute '%s'", name);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000238 else /* ret > 0 */
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +0200239 bb_error_msg("%s: exit status %u", name, ret & 0xff);
Peter Korsgaard0496e822013-02-28 09:59:23 +0100240
241 if (option_mask32 & OPT_e)
242 xfunc_die();
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000243 }
244
245 return n;
Eric Andersen7d682902001-10-31 10:59:29 +0000246}