run_parts.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini run-parts implementation for busybox
  4. *
  5. *
  6. * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it>
  7. *
  8. * Based on the Debian run-parts program, version 1.15
  9. * Copyright (C) 1996 Jeff Noxon <jeff@router.patch.net>,
  10. * Copyright (C) 1996-1999 Guy Maor <maor@debian.org>
  11. *
  12. *
  13. * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
  14. */
  15. /* This is my first attempt to write a program in C (well, this is my first
  16. * attempt to write a program! :-) . */
  17. /* This piece of code is heavily based on the original version of run-parts,
  18. * taken from debian-utils. I've only removed the long options and a the
  19. * report mode. As the original run-parts support only long options, I've
  20. * broken compatibility because the BusyBox policy doesn't allow them.
  21. * The supported options are:
  22. * -t test. Print the name of the files to be executed, without
  23. * execute them.
  24. * -a ARG argument. Pass ARG as an argument the program executed. It can
  25. * be repeated to pass multiple arguments.
  26. * -u MASK umask. Set the umask of the program executed to MASK. */
  27. /* TODO
  28. * done - convert calls to error in perror... and remove error()
  29. * done - convert malloc/realloc to their x... counterparts
  30. * done - remove catch_sigchld
  31. * done - use bb's concat_path_file()
  32. * done - declare run_parts_main() as extern and any other function as static?
  33. */
  34. #include "busybox.h"
  35. #include <getopt.h>
  36. static const struct option runparts_long_options[] = {
  37. { "test", 0, NULL, 't' },
  38. { "umask", 1, NULL, 'u' },
  39. { "arg", 1, NULL, 'a' },
  40. { 0, 0, 0, 0 }
  41. };
  42. /* valid_name */
  43. /* True or false? Is this a valid filename (upper/lower alpha, digits,
  44. * underscores, and hyphens only?)
  45. */
  46. static int valid_name(const struct dirent *d)
  47. {
  48. const char *c = d->d_name;
  49. while (*c) {
  50. if (!isalnum(*c) && (*c != '_') && (*c != '-')) {
  51. return 0;
  52. }
  53. ++c;
  54. }
  55. return 1;
  56. }
  57. /* test mode = 1 is the same as official run_parts
  58. * test_mode = 2 means to fail silently on missing directories
  59. */
  60. static int run_parts(char **args, const unsigned char test_mode)
  61. {
  62. struct dirent **namelist = 0;
  63. struct stat st;
  64. char *filename;
  65. char *arg0 = args[0];
  66. int entries;
  67. int i;
  68. int exitstatus = 0;
  69. #if __GNUC__
  70. /* Avoid longjmp clobbering */
  71. (void) &i;
  72. (void) &exitstatus;
  73. #endif
  74. /* scandir() isn't POSIX, but it makes things easy. */
  75. entries = scandir(arg0, &namelist, valid_name, alphasort);
  76. if (entries == -1) {
  77. if (test_mode & 2) {
  78. return 2;
  79. }
  80. bb_perror_msg_and_die("cannot open '%s'", arg0);
  81. }
  82. for (i = 0; i < entries; i++) {
  83. filename = concat_path_file(arg0, namelist[i]->d_name);
  84. xstat(filename, &st);
  85. if (S_ISREG(st.st_mode) && !access(filename, X_OK)) {
  86. if (test_mode) {
  87. puts(filename);
  88. } else {
  89. /* exec_errno is common vfork variable */
  90. volatile int exec_errno = 0;
  91. int result;
  92. int pid;
  93. if ((pid = vfork()) < 0) {
  94. bb_perror_msg_and_die("failed to fork");
  95. } else if (!pid) {
  96. args[0] = filename;
  97. execve(filename, args, environ);
  98. exec_errno = errno;
  99. _exit(1);
  100. }
  101. waitpid(pid, &result, 0);
  102. if (exec_errno) {
  103. errno = exec_errno;
  104. bb_perror_msg("failed to exec %s", filename);
  105. exitstatus = 1;
  106. }
  107. if (WIFEXITED(result) && WEXITSTATUS(result)) {
  108. bb_perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result));
  109. exitstatus = 1;
  110. } else if (WIFSIGNALED(result)) {
  111. bb_perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result));
  112. exitstatus = 1;
  113. }
  114. }
  115. } else if (!S_ISDIR(st.st_mode)) {
  116. bb_error_msg("component %s is not an executable plain file", filename);
  117. exitstatus = 1;
  118. }
  119. free(namelist[i]);
  120. free(filename);
  121. }
  122. free(namelist);
  123. return exitstatus;
  124. }
  125. /* run_parts_main */
  126. /* Process options */
  127. int run_parts_main(int argc, char **argv)
  128. {
  129. char **args = xmalloc(2 * sizeof(char *));
  130. unsigned char test_mode = 0;
  131. unsigned short argcount = 1;
  132. int opt;
  133. umask(022);
  134. while ((opt = getopt_long(argc, argv, "tu:a:",
  135. runparts_long_options, NULL)) > 0)
  136. {
  137. switch (opt) {
  138. /* Enable test mode */
  139. case 't':
  140. test_mode++;
  141. break;
  142. /* Set the umask of the programs executed */
  143. case 'u':
  144. /* Check and set the umask of the program executed. As stated in the original
  145. * run-parts, the octal conversion in libc is not foolproof; it will take the
  146. * 8 and 9 digits under some circumstances. We'll just have to live with it.
  147. */
  148. umask(xstrtoul_range(optarg, 8, 0, 07777));
  149. break;
  150. /* Pass an argument to the programs */
  151. case 'a':
  152. /* Add an argument to the commands that we will call.
  153. * Called once for every argument. */
  154. args = xrealloc(args, (argcount + 2) * (sizeof(char *)));
  155. args[argcount++] = optarg;
  156. break;
  157. default:
  158. bb_show_usage();
  159. }
  160. }
  161. /* We require exactly one argument: the directory name */
  162. if (optind != (argc - 1)) {
  163. bb_show_usage();
  164. }
  165. args[0] = argv[optind];
  166. args[argcount] = 0;
  167. return run_parts(args, test_mode);
  168. }