blob: 6be83edf9ffc313587a3aa53cd6cef8b8728b55f [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.
24 * The supported options are:
Denis Vlasenko19fb67e2008-02-28 21:30:22 +000025 * -t test. Print the name of the files to be executed, without
26 * execute them.
27 * -a ARG argument. Pass ARG as an argument the program executed. It can
28 * be repeated to pass multiple arguments.
29 * -u MASK umask. Set the umask of the program executed to MASK.
Peter Korsgaard0496e822013-02-28 09:59:23 +010030 * -e exit as soon as a script returns with a non-zero exit code
Eric Andersenfb74a452001-12-18 14:06:03 +000031 */
Eric Andersen7d682902001-10-31 10:59:29 +000032
Pere Orga6a3e01d2011-04-01 22:56:30 +020033//usage:#define run_parts_trivial_usage
Peter Korsgaard0496e822013-02-28 09:59:23 +010034//usage: "[-t"IF_FEATURE_RUN_PARTS_FANCY("l")"] [-a ARG]... [-u MASK] [-e] DIRECTORY"
Pere Orga6a3e01d2011-04-01 22:56:30 +020035//usage:#define run_parts_full_usage "\n\n"
36//usage: "Run a bunch of scripts in DIRECTORY\n"
Denys Vlasenko1d7ad7a2012-06-21 09:45:11 +020037//usage: "\n -t Dry run"
Pere Orga6a3e01d2011-04-01 22:56:30 +020038//usage: IF_FEATURE_RUN_PARTS_FANCY(
Denys Vlasenko1d7ad7a2012-06-21 09:45:11 +020039//usage: "\n -l Print names of matching files even if they are not executable"
Pere Orga6a3e01d2011-04-01 22:56:30 +020040//usage: )
Denys Vlasenko1d7ad7a2012-06-21 09:45:11 +020041//usage: "\n -a ARG Pass ARG as argument to programs"
42//usage: "\n -u MASK Set umask to MASK before running programs"
Peter Korsgaard0496e822013-02-28 09:59:23 +010043//usage: "\n -e Exit as soon as a script returns with a non-zero exit code"
Pere Orga6a3e01d2011-04-01 22:56:30 +020044//usage:
45//usage:#define run_parts_example_usage
46//usage: "$ run-parts -a start /etc/init.d\n"
47//usage: "$ run-parts -a stop=now /etc/init.d\n\n"
48//usage: "Let's assume you have a script foo/dosomething:\n"
49//usage: "#!/bin/sh\n"
50//usage: "for i in $*; do eval $i; done; unset i\n"
51//usage: "case \"$1\" in\n"
52//usage: "start*) echo starting something;;\n"
53//usage: "stop*) set -x; shutdown -h $stop;;\n"
54//usage: "esac\n\n"
55//usage: "Running this yields:\n"
56//usage: "$run-parts -a stop=+4m foo/\n"
57//usage: "+ shutdown -h +4m"
58
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000059#include "libbb.h"
60
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000061struct globals {
62 char **names;
63 int cur;
64 char *cmd[1];
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010065} FIX_ALIASING;
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000066#define G (*(struct globals*)&bb_common_bufsiz1)
67#define names (G.names)
68#define cur (G.cur )
69#define cmd (G.cmd )
Ian Wienand378ab682011-09-14 08:41:38 +020070#define INIT_G() do { } while (0)
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000071
Denis Vlasenkod50dda82008-06-15 05:40:56 +000072enum { NUM_CMD = (COMMON_BUFSIZE - sizeof(G)) / sizeof(cmd[0]) - 1 };
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000073
74enum {
Denis Vlasenko19fb67e2008-02-28 21:30:22 +000075 OPT_r = (1 << 0),
76 OPT_a = (1 << 1),
77 OPT_u = (1 << 2),
78 OPT_t = (1 << 3),
Peter Korsgaard0496e822013-02-28 09:59:23 +010079 OPT_e = (1 << 4),
80 OPT_l = (1 << 5) * ENABLE_FEATURE_RUN_PARTS_FANCY,
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000081};
82
83#if ENABLE_FEATURE_RUN_PARTS_FANCY
Denis Vlasenko19fb67e2008-02-28 21:30:22 +000084#define list_mode (option_mask32 & OPT_l)
Denis Vlasenko2a1d0242007-09-23 18:34:49 +000085#else
86#define list_mode 0
87#endif
88
89/* Is this a valid filename (upper/lower alpha, digits,
90 * underscores, and hyphens only?)
91 */
92static bool invalid_name(const char *c)
93{
94 c = bb_basename(c);
95
96 while (*c && (isalnum(*c) || *c == '_' || *c == '-'))
97 c++;
98
99 return *c; /* TRUE (!0) if terminating NUL is not reached */
100}
101
102static int bb_alphasort(const void *p1, const void *p2)
103{
Denis Vlasenko19fb67e2008-02-28 21:30:22 +0000104 int r = strcmp(*(char **) p1, *(char **) p2);
105 return (option_mask32 & OPT_r) ? -r : r;
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000106}
107
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000108static int FAST_FUNC act(const char *file, struct stat *statbuf, void *args UNUSED_PARAM, int depth)
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000109{
110 if (depth == 1)
111 return TRUE;
112
113 if (depth == 2
114 && ( !(statbuf->st_mode & (S_IFREG | S_IFLNK))
115 || invalid_name(file)
116 || (!list_mode && access(file, X_OK) != 0))
117 ) {
118 return SKIP;
119 }
120
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000121 names = xrealloc_vector(names, 4, cur);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000122 names[cur++] = xstrdup(file);
Denis Vlasenko27842282008-08-04 13:20:36 +0000123 /*names[cur] = NULL; - xrealloc_vector did it */
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000124
125 return TRUE;
126}
127
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000128#if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000129static const char runparts_longopts[] ALIGN1 =
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000130 "arg\0" Required_argument "a"
131 "umask\0" Required_argument "u"
132 "test\0" No_argument "t"
Peter Korsgaard0496e822013-02-28 09:59:23 +0100133 "exit-on-error\0" No_argument "e"
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000134#if ENABLE_FEATURE_RUN_PARTS_FANCY
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000135 "list\0" No_argument "l"
Denis Vlasenko19fb67e2008-02-28 21:30:22 +0000136 "reverse\0" No_argument "r"
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000137//TODO: "verbose\0" No_argument "v"
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000138#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000139 ;
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000140#endif
141
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000142int run_parts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000143int run_parts_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen7d682902001-10-31 10:59:29 +0000144{
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000145 const char *umask_p = "22";
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000146 llist_t *arg_list = NULL;
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000147 unsigned n;
148 int ret;
Eric Andersen7d682902001-10-31 10:59:29 +0000149
Ian Wienand378ab682011-09-14 08:41:38 +0200150 INIT_G();
151
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000152#if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
Denis Vlasenkobdc88fd2007-07-23 17:14:14 +0000153 applet_long_options = runparts_longopts;
Bernhard Reutner-Fischerb7cffd42007-03-28 20:35:13 +0000154#endif
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000155 /* We require exactly one argument: the directory name */
156 opt_complementary = "=1:a::";
Peter Korsgaard0496e822013-02-28 09:59:23 +0100157 getopt32(argv, "ra:u:te"IF_FEATURE_RUN_PARTS_FANCY("l"), &arg_list, &umask_p);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000158
159 umask(xstrtou_range(umask_p, 8, 0, 07777));
160
161 n = 1;
162 while (arg_list && n < NUM_CMD) {
Denis Vlasenkod50dda82008-06-15 05:40:56 +0000163 cmd[n++] = llist_pop(&arg_list);
Glenn L McGrath545106f2002-11-11 06:21:00 +0000164 }
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000165 /* cmd[n] = NULL; - is already zeroed out */
166
167 /* run-parts has to sort executables by name before running them */
168
169 recursive_action(argv[optind],
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000170 ACTION_RECURSE|ACTION_FOLLOWLINKS,
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000171 act, /* file action */
172 act, /* dir action */
173 NULL, /* user data */
174 1 /* depth */
175 );
176
177 if (!names)
178 return 0;
179
180 qsort(names, cur, sizeof(char *), bb_alphasort);
181
182 n = 0;
183 while (1) {
184 char *name = *names++;
185 if (!name)
186 break;
Denis Vlasenko19fb67e2008-02-28 21:30:22 +0000187 if (option_mask32 & (OPT_t | OPT_l)) {
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000188 puts(name);
189 continue;
190 }
191 cmd[0] = name;
Denys Vlasenko8531d762010-03-18 22:44:00 +0100192 ret = spawn_and_wait(cmd);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000193 if (ret == 0)
194 continue;
195 n = 1;
196 if (ret < 0)
Denis Vlasenkof9d4fc32009-04-21 20:40:51 +0000197 bb_perror_msg("can't execute '%s'", name);
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000198 else /* ret > 0 */
Denys Vlasenko8531d762010-03-18 22:44:00 +0100199 bb_error_msg("%s exited with code %d", name, ret & 0xff);
Peter Korsgaard0496e822013-02-28 09:59:23 +0100200
201 if (option_mask32 & OPT_e)
202 xfunc_die();
Denis Vlasenko2a1d0242007-09-23 18:34:49 +0000203 }
204
205 return n;
Eric Andersen7d682902001-10-31 10:59:29 +0000206}