busybox/debianutils/run_parts.c
<<
>>
Prefs
   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 */
  25
  26//usage:#define run_parts_trivial_usage
  27//usage:       "[-a ARG]... [-u UMASK] "
  28//usage:       IF_FEATURE_RUN_PARTS_LONG_OPTIONS("[--reverse] [--test] [--exit-on-error] "IF_FEATURE_RUN_PARTS_FANCY("[--list] "))
  29//usage:       "DIRECTORY"
  30//usage:#define run_parts_full_usage "\n\n"
  31//usage:       "Run a bunch of scripts in DIRECTORY\n"
  32//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(
  35//usage:     "\n        --reverse       Reverse execution order"
  36//usage:     "\n        --test          Dry run"
  37//usage:     "\n        --exit-on-error Exit if a script exits with non-zero"
  38//usage:        IF_FEATURE_RUN_PARTS_FANCY(
  39//usage:     "\n        --list          Print names of matching files even if they are not executable"
  40//usage:        )
  41//usage:        )
  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[2 /* using 1 provokes compiler warning */];
  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_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,
  80};
  81
  82/* 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{
  97        int r = strcmp(*(char **) p1, *(char **) p2);
  98        return (option_mask32 & OPT_r) ? -r : r;
  99}
 100
 101static int FAST_FUNC act(const char *file, struct stat *statbuf, void *args UNUSED_PARAM, int depth)
 102{
 103        if (depth == 1)
 104                return TRUE;
 105
 106        if (depth == 2
 107         && (  !(statbuf->st_mode & (S_IFREG | S_IFLNK))
 108            || invalid_name(file)
 109            || (!(option_mask32 & OPT_l) && access(file, X_OK) != 0))
 110        ) {
 111                return SKIP;
 112        }
 113
 114        names = xrealloc_vector(names, 4, cur);
 115        names[cur++] = xstrdup(file);
 116        /*names[cur] = NULL; - xrealloc_vector did it */
 117
 118        return TRUE;
 119}
 120
 121#if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
 122static const char runparts_longopts[] ALIGN1 =
 123        "arg\0"     Required_argument "a"
 124        "umask\0"   Required_argument "u"
 125//TODO: "verbose\0" No_argument       "v"
 126        "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"
 131#endif
 132        ;
 133#endif
 134
 135int run_parts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 136int run_parts_main(int argc UNUSED_PARAM, char **argv)
 137{
 138        const char *umask_p = "22";
 139        llist_t *arg_list = NULL;
 140        unsigned n;
 141        int ret;
 142
 143        INIT_G();
 144
 145#if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
 146        applet_long_options = runparts_longopts;
 147#endif
 148        /* We require exactly one argument: the directory name */
 149        opt_complementary = "=1:a::";
 150        getopt32(argv, "a:u:", &arg_list, &umask_p);
 151
 152        umask(xstrtou_range(umask_p, 8, 0, 07777));
 153
 154        n = 1;
 155        while (arg_list && n < NUM_CMD) {
 156                cmd[n++] = llist_pop(&arg_list);
 157        }
 158        /* 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],
 163                        ACTION_RECURSE|ACTION_FOLLOWLINKS,
 164                        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;
 180                if (option_mask32 & (OPT_t | OPT_l)) {
 181                        puts(name);
 182                        continue;
 183                }
 184                cmd[0] = name;
 185                ret = spawn_and_wait(cmd);
 186                if (ret == 0)
 187                        continue;
 188                n = 1;
 189                if (ret < 0)
 190                        bb_perror_msg("can't execute '%s'", name);
 191                else /* ret > 0 */
 192                        bb_error_msg("%s exited with code %d", name, ret & 0xff);
 193
 194                if (option_mask32 & OPT_e)
 195                        xfunc_die();
 196        }
 197
 198        return n;
 199}
 200