3
0

run_parts.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini run-parts implementation for busybox
  4. *
  5. * Copyright (C) 2007 Bernhard 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. *
  15. * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
  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. /* This piece of code is heavily based on the original version of run-parts,
  20. * taken from debian-utils. I've only removed the long options and a the
  21. * report mode. As the original run-parts support only long options, I've
  22. * broken compatibility because the BusyBox policy doesn't allow them.
  23. * The supported options are:
  24. * -t test. Print the name of the files to be executed, without
  25. * execute them.
  26. * -a ARG argument. Pass ARG as an argument the program executed. It can
  27. * be repeated to pass multiple arguments.
  28. * -u MASK umask. Set the umask of the program executed to MASK.
  29. */
  30. #include <getopt.h>
  31. #include "libbb.h"
  32. struct globals {
  33. char **names;
  34. int cur;
  35. char *cmd[1];
  36. };
  37. #define G (*(struct globals*)&bb_common_bufsiz1)
  38. #define names (G.names)
  39. #define cur (G.cur )
  40. #define cmd (G.cmd )
  41. enum { NUM_CMD = (COMMON_BUFSIZE - sizeof(struct globals)) / sizeof(cmd[0]) };
  42. enum {
  43. RUN_PARTS_OPT_a = (1 << 0),
  44. RUN_PARTS_OPT_u = (1 << 1),
  45. RUN_PARTS_OPT_t = (1 << 2),
  46. RUN_PARTS_OPT_l = (1 << 3) * ENABLE_FEATURE_RUN_PARTS_FANCY,
  47. };
  48. #if ENABLE_FEATURE_RUN_PARTS_FANCY
  49. #define list_mode (option_mask32 & RUN_PARTS_OPT_l)
  50. #else
  51. #define list_mode 0
  52. #endif
  53. /* Is this a valid filename (upper/lower alpha, digits,
  54. * underscores, and hyphens only?)
  55. */
  56. static bool invalid_name(const char *c)
  57. {
  58. c = bb_basename(c);
  59. while (*c && (isalnum(*c) || *c == '_' || *c == '-'))
  60. c++;
  61. return *c; /* TRUE (!0) if terminating NUL is not reached */
  62. }
  63. static int bb_alphasort(const void *p1, const void *p2)
  64. {
  65. return strcmp(*(char **) p1, *(char **) p2);
  66. }
  67. static int act(const char *file, struct stat *statbuf, void *args, int depth)
  68. {
  69. if (depth == 1)
  70. return TRUE;
  71. if (depth == 2
  72. && ( !(statbuf->st_mode & (S_IFREG | S_IFLNK))
  73. || invalid_name(file)
  74. || (!list_mode && access(file, X_OK) != 0))
  75. ) {
  76. return SKIP;
  77. }
  78. names = xrealloc(names, (cur + 2) * sizeof(names[0]));
  79. names[cur++] = xstrdup(file);
  80. names[cur] = NULL;
  81. return TRUE;
  82. }
  83. #if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
  84. static const char runparts_longopts[] ALIGN1 =
  85. "arg\0" Required_argument "a"
  86. "umask\0" Required_argument "u"
  87. "test\0" No_argument "t"
  88. #if ENABLE_FEATURE_RUN_PARTS_FANCY
  89. "list\0" No_argument "l"
  90. //TODO: "reverse\0" No_argument "r"
  91. //TODO: "verbose\0" No_argument "v"
  92. #endif
  93. ;
  94. #endif
  95. int run_parts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  96. int run_parts_main(int argc, char **argv)
  97. {
  98. const char *umask_p = "22";
  99. llist_t *arg_list = NULL;
  100. unsigned n;
  101. int ret;
  102. #if ENABLE_FEATURE_RUN_PARTS_LONG_OPTIONS
  103. applet_long_options = runparts_longopts;
  104. #endif
  105. /* We require exactly one argument: the directory name */
  106. opt_complementary = "=1:a::";
  107. getopt32(argv, "a:u:t"USE_FEATURE_RUN_PARTS_FANCY("l"), &arg_list, &umask_p);
  108. umask(xstrtou_range(umask_p, 8, 0, 07777));
  109. n = 1;
  110. while (arg_list && n < NUM_CMD) {
  111. cmd[n] = arg_list->data;
  112. arg_list = arg_list->link;
  113. n++;
  114. }
  115. /* cmd[n] = NULL; - is already zeroed out */
  116. /* run-parts has to sort executables by name before running them */
  117. recursive_action(argv[optind],
  118. ACTION_RECURSE|ACTION_FOLLOWLINKS,
  119. act, /* file action */
  120. act, /* dir action */
  121. NULL, /* user data */
  122. 1 /* depth */
  123. );
  124. if (!names)
  125. return 0;
  126. qsort(names, cur, sizeof(char *), bb_alphasort);
  127. n = 0;
  128. while (1) {
  129. char *name = *names++;
  130. if (!name)
  131. break;
  132. if (option_mask32 & (RUN_PARTS_OPT_t | RUN_PARTS_OPT_l)) {
  133. puts(name);
  134. continue;
  135. }
  136. cmd[0] = name;
  137. ret = wait4pid(spawn(cmd));
  138. if (ret == 0)
  139. continue;
  140. n = 1;
  141. if (ret < 0)
  142. bb_perror_msg("failed to exec %s", name);
  143. else /* ret > 0 */
  144. bb_error_msg("%s exited with return code %d", name, ret);
  145. }
  146. return n;
  147. }