blob: 13617c6e16195022443a43211e38e4946926a5ca [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
26//config: bool "run-parts"
27//config: default y
28//config: help
29//config: run-parts is a utility designed to run all the scripts in a directory.
30//config:
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.
33//config:
34//config: In this implementation of run-parts some features (such as report
35//config: mode) are not implemented.
36//config:
37//config: Unless you know that run-parts is used in some of your scripts
38//config: you can safely say N here.
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
44//config: help
45//config: Support long options for the run-parts applet.
46//config:
47//config:config FEATURE_RUN_PARTS_FANCY
48//config: bool "Support additional arguments"
49//config: default y
50//config: depends on RUN_PARTS
51//config: help
52//config: Support additional options:
53//config: -l --list print the names of the all matching files (not
54//config: limited to executables), but don't actually run them.
55
56//applet:IF_RUN_PARTS(APPLET_ODDNAME(run-parts, run_parts, BB_DIR_BIN, BB_SUID_DROP, run_parts))
57
58//kbuild:lib-$(CONFIG_RUN_PARTS) += run_parts.o
Eric Andersen7d682902001-10-31 10:59:29 +000059
Pere Orga6a3e01d2011-04-01 22:56:30 +020060//usage:#define run_parts_trivial_usage
Denys Vlasenko1e43a382013-02-28 10:22:49 +010061//usage: "[-a ARG]... [-u UMASK] "
Peter Korsgaard5d1c5992013-02-28 12:25:49 +010062//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 +010063//usage: "DIRECTORY"
Pere Orga6a3e01d2011-04-01 22:56:30 +020064//usage:#define run_parts_full_usage "\n\n"
65//usage: "Run a bunch of scripts in DIRECTORY\n"
Denys Vlasenko1e43a382013-02-28 10:22:49 +010066//usage: "\n -a ARG Pass ARG as argument to scripts"
67//usage: "\n -u UMASK Set UMASK before running scripts"
68//usage: IF_FEATURE_RUN_PARTS_LONG_OPTIONS(
Peter Korsgaard5d1c5992013-02-28 12:25:49 +010069//usage: "\n --reverse Reverse execution order"
Denys Vlasenko1e43a382013-02-28 10:22:49 +010070//usage: "\n --test Dry run"
Peter Korsgaard5d1c5992013-02-28 12:25:49 +010071//usage: "\n --exit-on-error Exit if a script exits with non-zero"
Pere Orga6a3e01d2011-04-01 22:56:30 +020072//usage: IF_FEATURE_RUN_PARTS_FANCY(
Denys Vlasenko1e43a382013-02-28 10:22:49 +010073//usage: "\n --list Print names of matching files even if they are not executable"
Pere Orga6a3e01d2011-04-01 22:56:30 +020074//usage: )
Denys Vlasenko1e43a382013-02-28 10:22:49 +010075//usage: )
Pere Orga6a3e01d2011-04-01 22:56:30 +020076//usage:
77//usage:#define run_parts_example_usage
78//usage: "$ run-parts -a start /etc/init.d\n"
79//usage: "$ run-parts -a stop=now /etc/init.d\n\n"
80//usage: "Let's assume you have a script foo/dosomething:\n"
81//usage: "#!/bin/sh\n"
82//usage: "for i in $*; do eval $i; done; unset i\n"
83//usage: "case \"$1\" in\n"
84//usage: "start*) echo starting something;;\n"
85//usage: "stop*) set -x; shutdown -h $stop;;\n"
86//usage: "esac\n\n"
87//usage: "Running this yields:\n"
88//usage: "$run-parts -a stop=+4m foo/\n"
89//usage: "+ shutdown -h +4m"
90
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000091#include "libbb.h"
92
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000093struct globals {
94 char **names;
95 int cur;
Denys Vlasenko5117eff2013-10-16 14:21:20 +020096 char *cmd[2 /* using 1 provokes compiler warning */];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010097} FIX_ALIASING;
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000098#define G (*(struct globals*)&bb_common_bufsiz1)
99#define names (G.names)
100#define cur (G.cur )
101#define cmd (G.cmd )
Ian Wienand378ab682011-09-14 08:41:38 +0200102#define INIT_G() do { } while (0)
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000103
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000104enum { NUM_CMD = (COMMON_BUFSIZE - sizeof(G)) / sizeof(cmd[0]) - 1 };
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000105
106enum {
Denys Vlasenko1e43a382013-02-28 10:22:49 +0100107 OPT_a = (1 << 0),
108 OPT_u = (1 << 1),
109 OPT_r = (1 << 2) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS,
110 OPT_t = (1 << 3) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS,
111 OPT_e = (1 << 4) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS,
112 OPT_l = (1 << 5) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
113 * ENABLE_FEATURE_RUN_PARTS_FANCY,
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000114};
115
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000116/* Is this a valid filename (upper/lower alpha, digits,
117 * underscores, and hyphens only?)
118 */
119static bool invalid_name(const char *c)
120{
121 c = bb_basename(c);
122
123 while (*c && (isalnum(*c) || *c == '_' || *c == '-'))
124 c++;
125
126 return *c; /* TRUE (!0) if terminating NUL is not reached */
127}
128
129static int bb_alphasort(const void *p1, const void *p2)
130{
Denis Vlasenko19fb67e2008-02-28 21:30:22 +0000131 int r = strcmp(*(char **) p1, *(char **) p2);
132 return (option_mask32 & OPT_r) ? -r : r;
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000133}
134
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000135static int FAST_FUNC act(const char *file, struct stat *statbuf, void *args UNUSED_PARAM, int depth)
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000136{
137 if (depth == 1)
138 return TRUE;
139
140 if (depth == 2
141 && ( !(statbuf->st_mode & (S_IFREG | S_IFLNK))
142 || invalid_name(file)
Denys Vlasenko1e43a382013-02-28 10:22:49 +0100143 || (!(option_mask32 & OPT_l) && access(file, X_OK) != 0))
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000144 ) {
145 return SKIP;
146 }
147
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000148 names = xrealloc_vector(names, 4, cur);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000149 names[cur++] = xstrdup(file);
Denis Vlasenko27842282008-08-04 13:20:36 +0000150 /*names[cur] = NULL; - xrealloc_vector did it */
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000151
152 return TRUE;
153}
154
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000155#if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000156static const char runparts_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000157 "arg\0" Required_argument "a"
158 "umask\0" Required_argument "u"
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000159//TODO: "verbose\0" No_argument "v"
Denys Vlasenko1e43a382013-02-28 10:22:49 +0100160 "reverse\0" No_argument "\xf0"
161 "test\0" No_argument "\xf1"
162 "exit-on-error\0" No_argument "\xf2"
163#if ENABLE_FEATURE_RUN_PARTS_FANCY
164 "list\0" No_argument "\xf3"
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000165#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000166 ;
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000167#endif
168
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000169int run_parts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000170int run_parts_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen7d682902001-10-31 10:59:29 +0000171{
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000172 const char *umask_p = "22";
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000173 llist_t *arg_list = NULL;
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000174 unsigned n;
175 int ret;
Eric Andersen7d682902001-10-31 10:59:29 +0000176
Ian Wienand378ab682011-09-14 08:41:38 +0200177 INIT_G();
178
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000179#if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000180 applet_long_options = runparts_longopts;
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000181#endif
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000182 /* We require exactly one argument: the directory name */
183 opt_complementary = "=1:a::";
Denys Vlasenko1e43a382013-02-28 10:22:49 +0100184 getopt32(argv, "a:u:", &arg_list, &umask_p);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000185
186 umask(xstrtou_range(umask_p, 8, 0, 07777));
187
188 n = 1;
189 while (arg_list && n < NUM_CMD) {
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000190 cmd[n++] = llist_pop(&arg_list);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000191 }
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000192 /* cmd[n] = NULL; - is already zeroed out */
193
194 /* run-parts has to sort executables by name before running them */
195
196 recursive_action(argv[optind],
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000197 ACTION_RECURSE|ACTION_FOLLOWLINKS,
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000198 act, /* file action */
199 act, /* dir action */
200 NULL, /* user data */
201 1 /* depth */
202 );
203
204 if (!names)
205 return 0;
206
207 qsort(names, cur, sizeof(char *), bb_alphasort);
208
209 n = 0;
210 while (1) {
211 char *name = *names++;
212 if (!name)
213 break;
Denis Vlasenko19fb67e2008-02-28 21:30:22 +0000214 if (option_mask32 & (OPT_t | OPT_l)) {
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000215 puts(name);
216 continue;
217 }
218 cmd[0] = name;
Denys Vlasenko8531d762010-03-18 22:44:00 +0100219 ret = spawn_and_wait(cmd);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000220 if (ret == 0)
221 continue;
222 n = 1;
223 if (ret < 0)
Denis Vlasenkof9d4fc32009-04-21 20:40:51 +0000224 bb_perror_msg("can't execute '%s'", name);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000225 else /* ret > 0 */
Denys Vlasenko7f3a2a22015-10-08 11:24:44 +0200226 bb_error_msg("%s: exit status %u", name, ret & 0xff);
Peter Korsgaard0496e822013-02-28 09:59:23 +0100227
228 if (option_mask32 & OPT_e)
229 xfunc_die();
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000230 }
231
232 return n;
Eric Andersen7d682902001-10-31 10:59:29 +0000233}