run_parts.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * run command from specified directory
  4. *
  5. *
  6. * Copyright (C) 2001 by Emanuele Aina <emanuele.aina@tiscali.it>
  7. * rewrite to vfork usage by
  8. * Copyright (C) 2002 by Vladimir Oleynik <dzo@simtreas.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. *
  16. */
  17. #include <sys/types.h>
  18. #include <sys/wait.h>
  19. #include <stdlib.h>
  20. #include <dirent.h>
  21. #include <unistd.h>
  22. #include <ctype.h>
  23. #include <errno.h>
  24. #include "libbb.h"
  25. /* valid_name */
  26. /* True or false? Is this a valid filename (upper/lower alpha, digits,
  27. * underscores, and hyphens only?)
  28. */
  29. static int valid_name(const struct dirent *d)
  30. {
  31. char *c = d->d_name;
  32. while (*c) {
  33. if (!isalnum(*c) && (*c != '_') && (*c != '-')) {
  34. return 0;
  35. }
  36. ++c;
  37. }
  38. return 1;
  39. }
  40. /* test mode = 1 is the same as official run_parts
  41. * test_mode = 2 means to fail silently on missing directories
  42. */
  43. extern int run_parts(char **args, const unsigned char test_mode, char **env)
  44. {
  45. struct dirent **namelist = 0;
  46. struct stat st;
  47. char *filename;
  48. char *arg0 = args[0];
  49. int entries;
  50. int i;
  51. int exitstatus = 0;
  52. #if __GNUC__
  53. /* Avoid longjmp clobbering */
  54. (void) &i;
  55. (void) &exitstatus;
  56. #endif
  57. /* scandir() isn't POSIX, but it makes things easy. */
  58. entries = scandir(arg0, &namelist, valid_name, alphasort);
  59. if (entries == -1) {
  60. if (test_mode & 2) {
  61. return(2);
  62. }
  63. bb_perror_msg_and_die("failed to open directory %s", arg0);
  64. }
  65. for (i = 0; i < entries; i++) {
  66. filename = concat_path_file(arg0, namelist[i]->d_name);
  67. if (stat(filename, &st) < 0) {
  68. bb_perror_msg_and_die("failed to stat component %s", filename);
  69. }
  70. if (S_ISREG(st.st_mode) && !access(filename, X_OK)) {
  71. if (test_mode) {
  72. puts(filename);
  73. } else {
  74. /* exec_errno is common vfork variable */
  75. volatile int exec_errno = 0;
  76. int result;
  77. int pid;
  78. if ((pid = vfork()) < 0) {
  79. bb_perror_msg_and_die("failed to fork");
  80. } else if (!pid) {
  81. args[0] = filename;
  82. execve(filename, args, env);
  83. exec_errno = errno;
  84. _exit(1);
  85. }
  86. waitpid(pid, &result, 0);
  87. if(exec_errno) {
  88. errno = exec_errno;
  89. bb_perror_msg("failed to exec %s", filename);
  90. exitstatus = 1;
  91. }
  92. if (WIFEXITED(result) && WEXITSTATUS(result)) {
  93. bb_perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result));
  94. exitstatus = 1;
  95. } else if (WIFSIGNALED(result)) {
  96. bb_perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result));
  97. exitstatus = 1;
  98. }
  99. }
  100. }
  101. else if (!S_ISDIR(st.st_mode)) {
  102. bb_error_msg("component %s is not an executable plain file", filename);
  103. exitstatus = 1;
  104. }
  105. free(namelist[i]);
  106. free(filename);
  107. }
  108. free(namelist);
  109. return(exitstatus);
  110. }