run_parts.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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"
  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: help
  43. //config: Support long options for the run-parts applet.
  44. //config:
  45. //config:config FEATURE_RUN_PARTS_FANCY
  46. //config: bool "Support additional arguments"
  47. //config: default y
  48. //config: depends on RUN_PARTS
  49. //config: help
  50. //config: Support additional options:
  51. //config: -l --list print the names of the all matching files (not
  52. //config: limited to executables), but don't actually run them.
  53. //applet:IF_RUN_PARTS(APPLET_ODDNAME(run-parts, run_parts, BB_DIR_BIN, BB_SUID_DROP, run_parts))
  54. //kbuild:lib-$(CONFIG_RUN_PARTS) += run_parts.o
  55. //usage:#define run_parts_trivial_usage
  56. //usage: "[-a ARG]... [-u UMASK] "
  57. //usage: IF_FEATURE_RUN_PARTS_LONG_OPTIONS("[--reverse] [--test] [--exit-on-error] "IF_FEATURE_RUN_PARTS_FANCY("[--list] "))
  58. //usage: "DIRECTORY"
  59. //usage:#define run_parts_full_usage "\n\n"
  60. //usage: "Run a bunch of scripts in DIRECTORY\n"
  61. //usage: "\n -a ARG Pass ARG as argument to scripts"
  62. //usage: "\n -u UMASK Set UMASK before running scripts"
  63. //usage: IF_FEATURE_RUN_PARTS_LONG_OPTIONS(
  64. //usage: "\n --reverse Reverse execution order"
  65. //usage: "\n --test Dry run"
  66. //usage: "\n --exit-on-error Exit if a script exits with non-zero"
  67. //usage: IF_FEATURE_RUN_PARTS_FANCY(
  68. //usage: "\n --list Print names of matching files even if they are not executable"
  69. //usage: )
  70. //usage: )
  71. //usage:
  72. //usage:#define run_parts_example_usage
  73. //usage: "$ run-parts -a start /etc/init.d\n"
  74. //usage: "$ run-parts -a stop=now /etc/init.d\n\n"
  75. //usage: "Let's assume you have a script foo/dosomething:\n"
  76. //usage: "#!/bin/sh\n"
  77. //usage: "for i in $*; do eval $i; done; unset i\n"
  78. //usage: "case \"$1\" in\n"
  79. //usage: "start*) echo starting something;;\n"
  80. //usage: "stop*) set -x; shutdown -h $stop;;\n"
  81. //usage: "esac\n\n"
  82. //usage: "Running this yields:\n"
  83. //usage: "$run-parts -a stop=+4m foo/\n"
  84. //usage: "+ shutdown -h +4m"
  85. #include "libbb.h"
  86. struct globals {
  87. char **names;
  88. int cur;
  89. char *cmd[2 /* using 1 provokes compiler warning */];
  90. } FIX_ALIASING;
  91. #define G (*(struct globals*)&bb_common_bufsiz1)
  92. #define names (G.names)
  93. #define cur (G.cur )
  94. #define cmd (G.cmd )
  95. #define INIT_G() do { } while (0)
  96. enum { NUM_CMD = (COMMON_BUFSIZE - sizeof(G)) / sizeof(cmd[0]) - 1 };
  97. enum {
  98. OPT_a = (1 << 0),
  99. OPT_u = (1 << 1),
  100. OPT_r = (1 << 2) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS,
  101. OPT_t = (1 << 3) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS,
  102. OPT_e = (1 << 4) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS,
  103. OPT_l = (1 << 5) * ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
  104. * ENABLE_FEATURE_RUN_PARTS_FANCY,
  105. };
  106. /* Is this a valid filename (upper/lower alpha, digits,
  107. * underscores, and hyphens only?)
  108. */
  109. static bool invalid_name(const char *c)
  110. {
  111. c = bb_basename(c);
  112. while (*c && (isalnum(*c) || *c == '_' || *c == '-'))
  113. c++;
  114. return *c; /* TRUE (!0) if terminating NUL is not reached */
  115. }
  116. static int bb_alphasort(const void *p1, const void *p2)
  117. {
  118. int r = strcmp(*(char **) p1, *(char **) p2);
  119. return (option_mask32 & OPT_r) ? -r : r;
  120. }
  121. static int FAST_FUNC act(const char *file, struct stat *statbuf, void *args UNUSED_PARAM, int depth)
  122. {
  123. if (depth == 1)
  124. return TRUE;
  125. if (depth == 2
  126. && ( !(statbuf->st_mode & (S_IFREG | S_IFLNK))
  127. || invalid_name(file)
  128. || (!(option_mask32 & OPT_l) && access(file, X_OK) != 0))
  129. ) {
  130. return SKIP;
  131. }
  132. names = xrealloc_vector(names, 4, cur);
  133. names[cur++] = xstrdup(file);
  134. /*names[cur] = NULL; - xrealloc_vector did it */
  135. return TRUE;
  136. }
  137. #if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
  138. static const char runparts_longopts[] ALIGN1 =
  139. "arg\0" Required_argument "a"
  140. "umask\0" Required_argument "u"
  141. //TODO: "verbose\0" No_argument "v"
  142. "reverse\0" No_argument "\xf0"
  143. "test\0" No_argument "\xf1"
  144. "exit-on-error\0" No_argument "\xf2"
  145. #if ENABLE_FEATURE_RUN_PARTS_FANCY
  146. "list\0" No_argument "\xf3"
  147. #endif
  148. ;
  149. #endif
  150. int run_parts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  151. int run_parts_main(int argc UNUSED_PARAM, char **argv)
  152. {
  153. const char *umask_p = "22";
  154. llist_t *arg_list = NULL;
  155. unsigned n;
  156. int ret;
  157. INIT_G();
  158. #if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
  159. applet_long_options = runparts_longopts;
  160. #endif
  161. /* We require exactly one argument: the directory name */
  162. opt_complementary = "=1:a::";
  163. getopt32(argv, "a:u:", &arg_list, &umask_p);
  164. umask(xstrtou_range(umask_p, 8, 0, 07777));
  165. n = 1;
  166. while (arg_list && n < NUM_CMD) {
  167. cmd[n++] = llist_pop(&arg_list);
  168. }
  169. /* cmd[n] = NULL; - is already zeroed out */
  170. /* run-parts has to sort executables by name before running them */
  171. recursive_action(argv[optind],
  172. ACTION_RECURSE|ACTION_FOLLOWLINKS,
  173. act, /* file action */
  174. act, /* dir action */
  175. NULL, /* user data */
  176. 1 /* depth */
  177. );
  178. if (!names)
  179. return 0;
  180. qsort(names, cur, sizeof(char *), bb_alphasort);
  181. n = 0;
  182. while (1) {
  183. char *name = *names++;
  184. if (!name)
  185. break;
  186. if (option_mask32 & (OPT_t | OPT_l)) {
  187. puts(name);
  188. continue;
  189. }
  190. cmd[0] = name;
  191. ret = spawn_and_wait(cmd);
  192. if (ret == 0)
  193. continue;
  194. n = 1;
  195. if (ret < 0)
  196. bb_perror_msg("can't execute '%s'", name);
  197. else /* ret > 0 */
  198. bb_error_msg("%s: exit status %u", name, ret & 0xff);
  199. if (option_mask32 & OPT_e)
  200. xfunc_die();
  201. }
  202. return n;
  203. }