source: branches/3.2/mindi-busybox/debianutils/run_parts.c @ 3232

Last change on this file since 3232 was 3232, checked in by bruno, 5 years ago
  • Update mindi-busybox to 1.21.1
File size: 5.5 KB
Line 
1/* vi: set sw=4 ts=4: */
2/*
3 * Mini run-parts implementation for busybox
4 *
5 * Copyright (C) 2007 Bernhard Reutner-Fischer
6 *
7 * Based on a older version that was in busybox which was 1k big.
8 *   Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it>
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>
13 *
14 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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,
21 * taken from debian-utils. I've only removed the long options and the
22 * report mode. As the original run-parts support only long options, I've
23 * broken compatibility because the BusyBox policy doesn't allow them.
24 * The supported options are:
25 * -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.
30 */
31
32//usage:#define run_parts_trivial_usage
33//usage:       "[-t"IF_FEATURE_RUN_PARTS_FANCY("l")"] [-a ARG]... [-u MASK] DIRECTORY"
34//usage:#define run_parts_full_usage "\n\n"
35//usage:       "Run a bunch of scripts in DIRECTORY\n"
36//usage:     "\n    -t  Dry run"
37//usage:    IF_FEATURE_RUN_PARTS_FANCY(
38//usage:     "\n    -l  Print names of matching files even if they are not executable"
39//usage:    )
40//usage:     "\n    -a ARG  Pass ARG as argument to programs"
41//usage:     "\n    -u MASK Set umask to MASK before running programs"
42//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
57#include "libbb.h"
58
59struct globals {
60    char **names;
61    int    cur;
62    char  *cmd[1];
63} FIX_ALIASING;
64#define G (*(struct globals*)&bb_common_bufsiz1)
65#define names (G.names)
66#define cur   (G.cur  )
67#define cmd   (G.cmd  )
68#define INIT_G() do { } while (0)
69
70enum { NUM_CMD = (COMMON_BUFSIZE - sizeof(G)) / sizeof(cmd[0]) - 1 };
71
72enum {
73    OPT_r = (1 << 0),
74    OPT_a = (1 << 1),
75    OPT_u = (1 << 2),
76    OPT_t = (1 << 3),
77    OPT_l = (1 << 4) * ENABLE_FEATURE_RUN_PARTS_FANCY,
78};
79
80#if ENABLE_FEATURE_RUN_PARTS_FANCY
81#define list_mode (option_mask32 & OPT_l)
82#else
83#define list_mode 0
84#endif
85
86/* Is this a valid filename (upper/lower alpha, digits,
87 * underscores, and hyphens only?)
88 */
89static bool invalid_name(const char *c)
90{
91    c = bb_basename(c);
92
93    while (*c && (isalnum(*c) || *c == '_' || *c == '-'))
94        c++;
95
96    return *c; /* TRUE (!0) if terminating NUL is not reached */
97}
98
99static int bb_alphasort(const void *p1, const void *p2)
100{
101    int r = strcmp(*(char **) p1, *(char **) p2);
102    return (option_mask32 & OPT_r) ? -r : r;
103}
104
105static int FAST_FUNC act(const char *file, struct stat *statbuf, void *args UNUSED_PARAM, int depth)
106{
107    if (depth == 1)
108        return TRUE;
109
110    if (depth == 2
111     && (  !(statbuf->st_mode & (S_IFREG | S_IFLNK))
112        || invalid_name(file)
113        || (!list_mode && access(file, X_OK) != 0))
114    ) {
115        return SKIP;
116    }
117
118    names = xrealloc_vector(names, 4, cur);
119    names[cur++] = xstrdup(file);
120    /*names[cur] = NULL; - xrealloc_vector did it */
121
122    return TRUE;
123}
124
125#if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
126static const char runparts_longopts[] ALIGN1 =
127    "arg\0"     Required_argument "a"
128    "umask\0"   Required_argument "u"
129    "test\0"    No_argument       "t"
130#if ENABLE_FEATURE_RUN_PARTS_FANCY
131    "list\0"    No_argument       "l"
132    "reverse\0" No_argument       "r"
133//TODO: "verbose\0" No_argument       "v"
134#endif
135    ;
136#endif
137
138int run_parts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
139int run_parts_main(int argc UNUSED_PARAM, char **argv)
140{
141    const char *umask_p = "22";
142    llist_t *arg_list = NULL;
143    unsigned n;
144    int ret;
145
146    INIT_G();
147
148#if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
149    applet_long_options = runparts_longopts;
150#endif
151    /* We require exactly one argument: the directory name */
152    opt_complementary = "=1:a::";
153    getopt32(argv, "ra:u:t"IF_FEATURE_RUN_PARTS_FANCY("l"), &arg_list, &umask_p);
154
155    umask(xstrtou_range(umask_p, 8, 0, 07777));
156
157    n = 1;
158    while (arg_list && n < NUM_CMD) {
159        cmd[n++] = llist_pop(&arg_list);
160    }
161    /* cmd[n] = NULL; - is already zeroed out */
162
163    /* run-parts has to sort executables by name before running them */
164
165    recursive_action(argv[optind],
166            ACTION_RECURSE|ACTION_FOLLOWLINKS,
167            act,            /* file action */
168            act,            /* dir action */
169            NULL,           /* user data */
170            1               /* depth */
171        );
172
173    if (!names)
174        return 0;
175
176    qsort(names, cur, sizeof(char *), bb_alphasort);
177
178    n = 0;
179    while (1) {
180        char *name = *names++;
181        if (!name)
182            break;
183        if (option_mask32 & (OPT_t | OPT_l)) {
184            puts(name);
185            continue;
186        }
187        cmd[0] = name;
188        ret = spawn_and_wait(cmd);
189        if (ret == 0)
190            continue;
191        n = 1;
192        if (ret < 0)
193            bb_perror_msg("can't execute '%s'", name);
194        else /* ret > 0 */
195            bb_error_msg("%s exited with code %d", name, ret & 0xff);
196    }
197
198    return n;
199}
Note: See TracBrowser for help on using the repository browser.