run_parts.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. /* This is my first attempt to write a program in C (well, this is my first
  17. * attempt to write a program! :-) . */
  18. /* This piece of code is heavily based on the original version of run-parts,
  19. * taken from debian-utils. I've only removed the long options and the
  20. * report mode. As the original run-parts support only long options, I've
  21. * broken compatibility because the BusyBox policy doesn't allow them.
  22. */
  23. //config:config RUN_PARTS
  24. //config: bool "run-parts (6.2 kb)"
  25. //config: default y
  26. //config: help
  27. //config: run-parts is a utility designed to run all the scripts in a directory.
  28. //config:
  29. //config: It is useful to set up a directory like cron.daily, where you need to
  30. //config: execute all the scripts in that directory.
  31. //config:
  32. //config: In this implementation of run-parts some features (such as report
  33. //config: mode) are not implemented.
  34. //config:
  35. //config: Unless you know that run-parts is used in some of your scripts
  36. //config: you can safely say N here.
  37. //config:
  38. //config:config FEATURE_RUN_PARTS_LONG_OPTIONS
  39. //config: bool "Enable long options"
  40. //config: default y
  41. //config: depends on RUN_PARTS && LONG_OPTS
  42. //config:
  43. //config:config FEATURE_RUN_PARTS_FANCY
  44. //config: bool "Support additional arguments"
  45. //config: default y
  46. //config: depends on RUN_PARTS
  47. //config: help
  48. //config: Support additional options:
  49. //config: -l --list print the names of the all matching files (not
  50. //config: limited to executables), but don't actually run them.
  51. //applet:IF_RUN_PARTS(APPLET_ODDNAME(run-parts, run_parts, BB_DIR_BIN, BB_SUID_DROP, run_parts))
  52. //kbuild:lib-$(CONFIG_RUN_PARTS) += run_parts.o
  53. //usage:#define run_parts_trivial_usage
  54. //usage: "[-a ARG]... [-u UMASK] "
  55. //usage: IF_FEATURE_RUN_PARTS_LONG_OPTIONS("[--reverse] [--test] [--exit-on-error] "IF_FEATURE_RUN_PARTS_FANCY("[--list] "))
  56. //usage: "DIRECTORY"
  57. //usage:#define run_parts_full_usage "\n\n"
  58. //usage: "Run a bunch of scripts in DIRECTORY\n"
  59. //usage: "\n -a ARG Pass ARG as argument to scripts"
  60. //usage: "\n -u UMASK Set UMASK before running scripts"
  61. //usage: IF_FEATURE_RUN_PARTS_LONG_OPTIONS(
  62. //usage: "\n --reverse Reverse execution order"
  63. //usage: "\n --test Dry run"
  64. //usage: "\n --exit-on-error Exit if a script exits with non-zero"
  65. //usage: IF_FEATURE_RUN_PARTS_FANCY(
  66. //usage: "\n --list Print names of matching files even if they are not executable"
  67. //usage: )
  68. //usage: )
  69. //usage:
  70. //usage:#define run_parts_example_usage
  71. //usage: "$ run-parts -a start /etc/init.d\n"
  72. //usage: "$ run-parts -a stop=now /etc/init.d\n\n"
  73. //usage: "Let's assume you have a script foo/dosomething:\n"
  74. //usage: "#!/bin/sh\n"
  75. //usage: "for i in $*; do eval $i; done; unset i\n"
  76. //usage: "case \"$1\" in\n"
  77. //usage: "start*) echo starting something;;\n"
  78. //usage: "stop*) set -x; shutdown -h $stop;;\n"
  79. //usage: "esac\n\n"
  80. //usage: "Running this yields:\n"
  81. //usage: "$run-parts -a stop=+4m foo/\n"
  82. //usage: "+ shutdown -h +4m"
  83. #include "libbb.h"
  84. #include "common_bufsiz.h"
  85. struct globals {
  86. char **names;
  87. int cur;
  88. char *cmd[2 /* using 1 provokes compiler warning */];
  89. } FIX_ALIASING;
  90. #define G (*(struct globals*)bb_common_bufsiz1)
  91. #define names (G.names)
  92. #define cur (G.cur )
  93. #define cmd (G.cmd )
  94. #define INIT_G() do { setup_common_bufsiz(); } while (0)
  95. enum { NUM_CMD = (COMMON_BUFSIZE - sizeof(G)) / sizeof(cmd[0]) - 1 };
  96. enum {
  97. OPT_a = (1 << 0),
  98. OPT_u = (1 << 1),
  99. OPT_r = (1 << 2) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS,
  100. OPT_t = (1 << 3) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS,
  101. OPT_e = (1 << 4) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS,
  102. OPT_l = (1 << 5) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
  103. * ENABLE_FEATURE_RUN_PARTS_FANCY,
  104. };
  105. /* Is this a valid filename (upper/lower alpha, digits,
  106. * underscores, hyphens, and non-leading dots only?)
  107. */
  108. static bool invalid_name(const char *c)
  109. {
  110. c = bb_basename(c);
  111. if (*c == '.')
  112. return *c;
  113. /* Debian run-parts 4.8.3, manpage:
  114. * "...the names must consist entirely of ASCII letters,
  115. * ASCII digits, ASCII underscores, and ASCII minus-hyphens.
  116. * However, the name must not begin with a period."
  117. * The last sentence is a giveaway that something is fishy
  118. * (why mention leading dot if dots are not allowed anyway?).
  119. * Yes, you guessed it right: in fact non-leading dots ARE allowed.
  120. */
  121. while (isalnum(*c) || *c == '_' || *c == '-' || *c == '.')
  122. c++;
  123. return *c; /* TRUE (!0) if terminating NUL is not reached */
  124. }
  125. static int bb_alphasort(const void *p1, const void *p2)
  126. {
  127. int r = strcmp(*(char **) p1, *(char **) p2);
  128. return (option_mask32 & OPT_r) ? -r : r;
  129. }
  130. static int FAST_FUNC act(struct recursive_state *state,
  131. const char *file, struct stat *statbuf)
  132. {
  133. if (state->depth == 0)
  134. return TRUE;
  135. if (state->depth == 1
  136. && ( !(statbuf->st_mode & (S_IFREG | S_IFLNK))
  137. || invalid_name(file)
  138. || (!(option_mask32 & OPT_l) && access(file, X_OK) != 0))
  139. ) {
  140. return SKIP;
  141. }
  142. names = xrealloc_vector(names, 4, cur);
  143. names[cur++] = xstrdup(file);
  144. /*names[cur] = NULL; - xrealloc_vector did it */
  145. return TRUE;
  146. }
  147. #if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
  148. static const char runparts_longopts[] ALIGN1 =
  149. "arg\0" Required_argument "a"
  150. "umask\0" Required_argument "u"
  151. //TODO: "verbose\0" No_argument "v"
  152. "reverse\0" No_argument "\xf0"
  153. "test\0" No_argument "\xf1"
  154. "exit-on-error\0" No_argument "\xf2"
  155. # if ENABLE_FEATURE_RUN_PARTS_FANCY
  156. "list\0" No_argument "\xf3"
  157. # endif
  158. ;
  159. # define GETOPT32 getopt32long
  160. # define LONGOPTS ,runparts_longopts
  161. #else
  162. # define GETOPT32 getopt32
  163. # define LONGOPTS
  164. #endif
  165. int run_parts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  166. int run_parts_main(int argc UNUSED_PARAM, char **argv)
  167. {
  168. const char *umask_p = "22";
  169. llist_t *arg_list = NULL;
  170. unsigned n;
  171. int ret;
  172. INIT_G();
  173. /* We require exactly one argument: the directory name */
  174. GETOPT32(argv, "^" "a:*u:" "\0" "=1" LONGOPTS,
  175. &arg_list, &umask_p
  176. );
  177. umask(xstrtou_range(umask_p, 8, 0, 07777));
  178. n = 1;
  179. while (arg_list && n < NUM_CMD) {
  180. cmd[n++] = llist_pop(&arg_list);
  181. }
  182. /* cmd[n] = NULL; - is already zeroed out */
  183. /* run-parts has to sort executables by name before running them */
  184. recursive_action(argv[optind],
  185. ACTION_RECURSE|ACTION_FOLLOWLINKS,
  186. act, /* file action */
  187. act, /* dir action */
  188. NULL /* user data */
  189. );
  190. if (!names)
  191. return 0;
  192. qsort(names, cur, sizeof(char *), bb_alphasort);
  193. n = 0;
  194. while (1) {
  195. char *name = *names++;
  196. if (!name)
  197. break;
  198. if (option_mask32 & (OPT_t | OPT_l)) {
  199. puts(name);
  200. continue;
  201. }
  202. cmd[0] = name;
  203. ret = spawn_and_wait(cmd);
  204. if (ret == 0)
  205. continue;
  206. n = 1;
  207. if (ret < 0)
  208. bb_perror_msg("can't execute '%s'", name);
  209. else /* ret > 0 */
  210. bb_error_msg("%s: exit status %u", name, ret & 0xff);
  211. if (option_mask32 & OPT_e)
  212. xfunc_die();
  213. }
  214. return n;
  215. }