blob: 527fae227c5121d8265531c7e2fcdd057eb0952e [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 */
Eric Andersen7d682902001-10-31 10:59:29 +000025
Pere Orga6a3e01d2011-04-01 22:56:30 +020026//usage:#define run_parts_trivial_usage
Denys Vlasenko1e43a382013-02-28 10:22:49 +010027//usage: "[-a ARG]... [-u UMASK] "
Peter Korsgaard5d1c5992013-02-28 12:25:49 +010028//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 +010029//usage: "DIRECTORY"
Pere Orga6a3e01d2011-04-01 22:56:30 +020030//usage:#define run_parts_full_usage "\n\n"
31//usage: "Run a bunch of scripts in DIRECTORY\n"
Denys Vlasenko1e43a382013-02-28 10:22:49 +010032//usage: "\n -a ARG Pass ARG as argument to scripts"
33//usage: "\n -u UMASK Set UMASK before running scripts"
34//usage: IF_FEATURE_RUN_PARTS_LONG_OPTIONS(
Peter Korsgaard5d1c5992013-02-28 12:25:49 +010035//usage: "\n --reverse Reverse execution order"
Denys Vlasenko1e43a382013-02-28 10:22:49 +010036//usage: "\n --test Dry run"
Peter Korsgaard5d1c5992013-02-28 12:25:49 +010037//usage: "\n --exit-on-error Exit if a script exits with non-zero"
Pere Orga6a3e01d2011-04-01 22:56:30 +020038//usage: IF_FEATURE_RUN_PARTS_FANCY(
Denys Vlasenko1e43a382013-02-28 10:22:49 +010039//usage: "\n --list Print names of matching files even if they are not executable"
Pere Orga6a3e01d2011-04-01 22:56:30 +020040//usage: )
Denys Vlasenko1e43a382013-02-28 10:22:49 +010041//usage: )
Pere Orga6a3e01d2011-04-01 22:56:30 +020042//usage:
43//usage:#define run_parts_example_usage
44//usage: "$ run-parts -a start /etc/init.d\n"
45//usage: "$ run-parts -a stop=now /etc/init.d\n\n"
46//usage: "Let's assume you have a script foo/dosomething:\n"
47//usage: "#!/bin/sh\n"
48//usage: "for i in $*; do eval $i; done; unset i\n"
49//usage: "case \"$1\" in\n"
50//usage: "start*) echo starting something;;\n"
51//usage: "stop*) set -x; shutdown -h $stop;;\n"
52//usage: "esac\n\n"
53//usage: "Running this yields:\n"
54//usage: "$run-parts -a stop=+4m foo/\n"
55//usage: "+ shutdown -h +4m"
56
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000057#include "libbb.h"
58
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000059struct globals {
60 char **names;
61 int cur;
Denys Vlasenko5117eff2013-10-16 14:21:20 +020062 char *cmd[2 /* using 1 provokes compiler warning */];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010063} FIX_ALIASING;
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000064#define G (*(struct globals*)&bb_common_bufsiz1)
65#define names (G.names)
66#define cur (G.cur )
67#define cmd (G.cmd )
Ian Wienand378ab682011-09-14 08:41:38 +020068#define INIT_G() do { } while (0)
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000069
Denis Vlasenkod50dda82008-06-15 05:40:56 +000070enum { NUM_CMD = (COMMON_BUFSIZE - sizeof(G)) / sizeof(cmd[0]) - 1 };
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000071
72enum {
Denys Vlasenko1e43a382013-02-28 10:22:49 +010073 OPT_a = (1 << 0),
74 OPT_u = (1 << 1),
75 OPT_r = (1 << 2) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS,
76 OPT_t = (1 << 3) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS,
77 OPT_e = (1 << 4) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS,
78 OPT_l = (1 << 5) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
79 * ENABLE_FEATURE_RUN_PARTS_FANCY,
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000080};
81
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000082/* Is this a valid filename (upper/lower alpha, digits,
83 * underscores, and hyphens only?)
84 */
85static bool invalid_name(const char *c)
86{
87 c = bb_basename(c);
88
89 while (*c && (isalnum(*c) || *c == '_' || *c == '-'))
90 c++;
91
92 return *c; /* TRUE (!0) if terminating NUL is not reached */
93}
94
95static int bb_alphasort(const void *p1, const void *p2)
96{
Denis Vlasenko19fb67e2008-02-28 21:30:22 +000097 int r = strcmp(*(char **) p1, *(char **) p2);
98 return (option_mask32 & OPT_r) ? -r : r;
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000099}
100
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000101static int FAST_FUNC act(const char *file, struct stat *statbuf, void *args UNUSED_PARAM, int depth)
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000102{
103 if (depth == 1)
104 return TRUE;
105
106 if (depth == 2
107 && ( !(statbuf->st_mode & (S_IFREG | S_IFLNK))
108 || invalid_name(file)
Denys Vlasenko1e43a382013-02-28 10:22:49 +0100109 || (!(option_mask32 & OPT_l) && access(file, X_OK) != 0))
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000110 ) {
111 return SKIP;
112 }
113
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000114 names = xrealloc_vector(names, 4, cur);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000115 names[cur++] = xstrdup(file);
Denis Vlasenko27842282008-08-04 13:20:36 +0000116 /*names[cur] = NULL; - xrealloc_vector did it */
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000117
118 return TRUE;
119}
120
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000121#if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000122static const char runparts_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000123 "arg\0" Required_argument "a"
124 "umask\0" Required_argument "u"
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000125//TODO: "verbose\0" No_argument "v"
Denys Vlasenko1e43a382013-02-28 10:22:49 +0100126 "reverse\0" No_argument "\xf0"
127 "test\0" No_argument "\xf1"
128 "exit-on-error\0" No_argument "\xf2"
129#if ENABLE_FEATURE_RUN_PARTS_FANCY
130 "list\0" No_argument "\xf3"
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000131#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000132 ;
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000133#endif
134
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000135int run_parts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000136int run_parts_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen7d682902001-10-31 10:59:29 +0000137{
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000138 const char *umask_p = "22";
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000139 llist_t *arg_list = NULL;
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000140 unsigned n;
141 int ret;
Eric Andersen7d682902001-10-31 10:59:29 +0000142
Ian Wienand378ab682011-09-14 08:41:38 +0200143 INIT_G();
144
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000145#if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000146 applet_long_options = runparts_longopts;
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000147#endif
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000148 /* We require exactly one argument: the directory name */
149 opt_complementary = "=1:a::";
Denys Vlasenko1e43a382013-02-28 10:22:49 +0100150 getopt32(argv, "a:u:", &arg_list, &umask_p);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000151
152 umask(xstrtou_range(umask_p, 8, 0, 07777));
153
154 n = 1;
155 while (arg_list && n < NUM_CMD) {
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000156 cmd[n++] = llist_pop(&arg_list);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000157 }
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000158 /* cmd[n] = NULL; - is already zeroed out */
159
160 /* run-parts has to sort executables by name before running them */
161
162 recursive_action(argv[optind],
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000163 ACTION_RECURSE|ACTION_FOLLOWLINKS,
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000164 act, /* file action */
165 act, /* dir action */
166 NULL, /* user data */
167 1 /* depth */
168 );
169
170 if (!names)
171 return 0;
172
173 qsort(names, cur, sizeof(char *), bb_alphasort);
174
175 n = 0;
176 while (1) {
177 char *name = *names++;
178 if (!name)
179 break;
Denis Vlasenko19fb67e2008-02-28 21:30:22 +0000180 if (option_mask32 & (OPT_t | OPT_l)) {
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000181 puts(name);
182 continue;
183 }
184 cmd[0] = name;
Denys Vlasenko8531d762010-03-18 22:44:00 +0100185 ret = spawn_and_wait(cmd);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000186 if (ret == 0)
187 continue;
188 n = 1;
189 if (ret < 0)
Denis Vlasenkof9d4fc32009-04-21 20:40:51 +0000190 bb_perror_msg("can't execute '%s'", name);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000191 else /* ret > 0 */
Denys Vlasenko8531d762010-03-18 22:44:00 +0100192 bb_error_msg("%s exited with code %d", name, ret & 0xff);
Peter Korsgaard0496e822013-02-28 09:59:23 +0100193
194 if (option_mask32 & OPT_e)
195 xfunc_die();
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000196 }
197
198 return n;
Eric Andersen7d682902001-10-31 10:59:29 +0000199}