3
0

xargs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini xargs implementation for busybox
  4. * Options are supported: "-prtx -n max_arg -s max_chars -e[ouf_str]"
  5. *
  6. * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru>
  7. *
  8. * Special thanks
  9. * - Mark Whitley and Glenn McGrath for stimulus to rewrite :)
  10. * - Mike Rendell <michael@cs.mun.ca>
  11. * and David MacKenzie <djm@gnu.ai.mit.edu>.
  12. *
  13. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  14. *
  15. * xargs is described in the Single Unix Specification v3 at
  16. * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
  17. *
  18. */
  19. #include "busybox.h"
  20. /* COMPAT: SYSV version defaults size (and has a max value of) to 470.
  21. We try to make it as large as possible. */
  22. #if !defined(ARG_MAX) && defined(_SC_ARG_MAX)
  23. #define ARG_MAX sysconf (_SC_ARG_MAX)
  24. #endif
  25. #ifndef ARG_MAX
  26. #define ARG_MAX 470
  27. #endif
  28. #ifdef TEST
  29. # ifndef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
  30. # define CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
  31. # endif
  32. # ifndef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
  33. # define CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
  34. # endif
  35. # ifndef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
  36. # define CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
  37. # endif
  38. # ifndef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
  39. # define CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
  40. # endif
  41. #endif
  42. /*
  43. This function has special algorithm.
  44. Don't use fork and include to main!
  45. */
  46. static int xargs_exec(char *const *args)
  47. {
  48. pid_t p;
  49. volatile int exec_errno = 0; /* shared vfork stack */
  50. int status;
  51. p = vfork();
  52. if (p < 0)
  53. bb_perror_msg_and_die("vfork");
  54. if (p == 0) {
  55. /* vfork -- child */
  56. execvp(args[0], args);
  57. exec_errno = errno; /* set error to shared stack */
  58. _exit(1);
  59. }
  60. /* vfork -- parent */
  61. while (wait(&status) == (pid_t) -1)
  62. if (errno != EINTR)
  63. break;
  64. if (exec_errno) {
  65. errno = exec_errno;
  66. bb_perror_msg("%s", args[0]);
  67. return exec_errno == ENOENT ? 127 : 126;
  68. }
  69. if (WEXITSTATUS(status) == 255) {
  70. bb_error_msg("%s: exited with status 255; aborting", args[0]);
  71. return 124;
  72. }
  73. if (WIFSTOPPED(status)) {
  74. bb_error_msg("%s: stopped by signal %d",
  75. args[0], WSTOPSIG(status));
  76. return 125;
  77. }
  78. if (WIFSIGNALED(status)) {
  79. bb_error_msg("%s: terminated by signal %d",
  80. args[0], WTERMSIG(status));
  81. return 125;
  82. }
  83. if (WEXITSTATUS(status))
  84. return 123;
  85. return 0;
  86. }
  87. typedef struct xlist_s {
  88. char *data;
  89. size_t lenght;
  90. struct xlist_s *link;
  91. } xlist_t;
  92. static int eof_stdin_detected;
  93. #define ISBLANK(c) ((c) == ' ' || (c) == '\t')
  94. #define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \
  95. || (c) == '\f' || (c) == '\v')
  96. #ifdef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES
  97. static xlist_t *process_stdin(xlist_t * list_arg,
  98. const char *eof_str, size_t mc, char *buf)
  99. {
  100. #define NORM 0
  101. #define QUOTE 1
  102. #define BACKSLASH 2
  103. #define SPACE 4
  104. char *s = NULL; /* start word */
  105. char *p = NULL; /* pointer to end word */
  106. char q = 0; /* quote char */
  107. char state = NORM;
  108. char eof_str_detected = 0;
  109. size_t line_l = 0; /* size loaded args line */
  110. int c; /* current char */
  111. xlist_t *cur;
  112. xlist_t *prev;
  113. for (prev = cur = list_arg; cur; cur = cur->link) {
  114. line_l += cur->lenght; /* previous allocated */
  115. if (prev != cur)
  116. prev = prev->link;
  117. }
  118. while (!eof_stdin_detected) {
  119. c = getchar();
  120. if (c == EOF) {
  121. eof_stdin_detected++;
  122. if (s)
  123. goto unexpected_eof;
  124. break;
  125. }
  126. if (eof_str_detected)
  127. continue;
  128. if (state == BACKSLASH) {
  129. state = NORM;
  130. goto set;
  131. } else if (state == QUOTE) {
  132. if (c == q) {
  133. q = 0;
  134. state = NORM;
  135. } else {
  136. goto set;
  137. }
  138. } else { /* if(state == NORM) */
  139. if (ISSPACE(c)) {
  140. if (s) {
  141. unexpected_eof:
  142. state = SPACE;
  143. c = 0;
  144. goto set;
  145. }
  146. } else {
  147. if (s == NULL)
  148. s = p = buf;
  149. if (c == '\\') {
  150. state = BACKSLASH;
  151. } else if (c == '\'' || c == '"') {
  152. q = c;
  153. state = QUOTE;
  154. } else {
  155. set:
  156. if ((size_t)(p - buf) >= mc)
  157. bb_error_msg_and_die("argument line too long");
  158. *p++ = c;
  159. }
  160. }
  161. }
  162. if (state == SPACE) { /* word's delimiter or EOF detected */
  163. if (q) {
  164. bb_error_msg_and_die("unmatched %s quote",
  165. q == '\'' ? "single" : "double");
  166. }
  167. /* word loaded */
  168. if (eof_str) {
  169. eof_str_detected = strcmp(s, eof_str) == 0;
  170. }
  171. if (!eof_str_detected) {
  172. size_t lenght = (p - buf);
  173. cur = xmalloc(sizeof(xlist_t) + lenght);
  174. cur->data = memcpy(cur + 1, s, lenght);
  175. cur->lenght = lenght;
  176. cur->link = NULL;
  177. if (prev == NULL) {
  178. list_arg = cur;
  179. } else {
  180. prev->link = cur;
  181. }
  182. prev = cur;
  183. line_l += lenght;
  184. if (line_l > mc) {
  185. /* stop memory usage :-) */
  186. break;
  187. }
  188. }
  189. s = NULL;
  190. state = NORM;
  191. }
  192. }
  193. return list_arg;
  194. }
  195. #else
  196. /* The variant does not support single quotes, double quotes or backslash */
  197. static xlist_t *process_stdin(xlist_t * list_arg,
  198. const char *eof_str, size_t mc, char *buf)
  199. {
  200. int c; /* current char */
  201. int eof_str_detected = 0;
  202. char *s = NULL; /* start word */
  203. char *p = NULL; /* pointer to end word */
  204. size_t line_l = 0; /* size loaded args line */
  205. xlist_t *cur;
  206. xlist_t *prev;
  207. for (prev = cur = list_arg; cur; cur = cur->link) {
  208. line_l += cur->lenght; /* previous allocated */
  209. if (prev != cur)
  210. prev = prev->link;
  211. }
  212. while (!eof_stdin_detected) {
  213. c = getchar();
  214. if (c == EOF) {
  215. eof_stdin_detected++;
  216. }
  217. if (eof_str_detected)
  218. continue;
  219. if (c == EOF || ISSPACE(c)) {
  220. if (s == NULL)
  221. continue;
  222. c = EOF;
  223. }
  224. if (s == NULL)
  225. s = p = buf;
  226. if ((p - buf) >= mc)
  227. bb_error_msg_and_die("argument line too long");
  228. *p++ = c == EOF ? 0 : c;
  229. if (c == EOF) { /* word's delimiter or EOF detected */
  230. /* word loaded */
  231. if (eof_str) {
  232. eof_str_detected = strcmp(s, eof_str) == 0;
  233. }
  234. if (!eof_str_detected) {
  235. size_t lenght = (p - buf);
  236. cur = xmalloc(sizeof(xlist_t) + lenght);
  237. cur->data = memcpy(cur + 1, s, lenght);
  238. cur->lenght = lenght;
  239. cur->link = NULL;
  240. if (prev == NULL) {
  241. list_arg = cur;
  242. } else {
  243. prev->link = cur;
  244. }
  245. prev = cur;
  246. line_l += lenght;
  247. if (line_l > mc) {
  248. /* stop memory usage :-) */
  249. break;
  250. }
  251. s = NULL;
  252. }
  253. }
  254. }
  255. return list_arg;
  256. }
  257. #endif /* CONFIG_FEATURE_XARGS_SUPPORT_QUOTES */
  258. #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION
  259. /* Prompt the user for a response, and
  260. if the user responds affirmatively, return true;
  261. otherwise, return false. Used "/dev/tty", not stdin. */
  262. static int xargs_ask_confirmation(void)
  263. {
  264. static FILE *tty_stream;
  265. int c, savec;
  266. if (!tty_stream) {
  267. tty_stream = xfopen(CURRENT_TTY, "r");
  268. /* pranoidal security by vodz */
  269. fcntl(fileno(tty_stream), F_SETFD, FD_CLOEXEC);
  270. }
  271. fputs(" ?...", stderr);
  272. fflush(stderr);
  273. c = savec = getc(tty_stream);
  274. while (c != EOF && c != '\n')
  275. c = getc(tty_stream);
  276. if (savec == 'y' || savec == 'Y')
  277. return 1;
  278. return 0;
  279. }
  280. #else
  281. # define xargs_ask_confirmation() 1
  282. #endif /* CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION */
  283. #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM
  284. static xlist_t *process0_stdin(xlist_t * list_arg, const char *eof_str ATTRIBUTE_UNUSED,
  285. size_t mc, char *buf)
  286. {
  287. int c; /* current char */
  288. char *s = NULL; /* start word */
  289. char *p = NULL; /* pointer to end word */
  290. size_t line_l = 0; /* size loaded args line */
  291. xlist_t *cur;
  292. xlist_t *prev;
  293. for (prev = cur = list_arg; cur; cur = cur->link) {
  294. line_l += cur->lenght; /* previous allocated */
  295. if (prev != cur)
  296. prev = prev->link;
  297. }
  298. while (!eof_stdin_detected) {
  299. c = getchar();
  300. if (c == EOF) {
  301. eof_stdin_detected++;
  302. if (s == NULL)
  303. break;
  304. c = 0;
  305. }
  306. if (s == NULL)
  307. s = p = buf;
  308. if ((size_t)(p - buf) >= mc)
  309. bb_error_msg_and_die("argument line too long");
  310. *p++ = c;
  311. if (c == 0) { /* word's delimiter or EOF detected */
  312. /* word loaded */
  313. size_t lenght = (p - buf);
  314. cur = xmalloc(sizeof(xlist_t) + lenght);
  315. cur->data = memcpy(cur + 1, s, lenght);
  316. cur->lenght = lenght;
  317. cur->link = NULL;
  318. if (prev == NULL) {
  319. list_arg = cur;
  320. } else {
  321. prev->link = cur;
  322. }
  323. prev = cur;
  324. line_l += lenght;
  325. if (line_l > mc) {
  326. /* stop memory usage :-) */
  327. break;
  328. }
  329. s = NULL;
  330. }
  331. }
  332. return list_arg;
  333. }
  334. #endif /* CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM */
  335. /* Correct regardless of combination of CONFIG_xxx */
  336. enum {
  337. OPTBIT_VERBOSE = 0,
  338. OPTBIT_NO_EMPTY,
  339. OPTBIT_UPTO_NUMBER,
  340. OPTBIT_UPTO_SIZE,
  341. OPTBIT_EOF_STRING,
  342. USE_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,)
  343. USE_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,)
  344. USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,)
  345. OPT_VERBOSE = 1<<OPTBIT_VERBOSE ,
  346. OPT_NO_EMPTY = 1<<OPTBIT_NO_EMPTY ,
  347. OPT_UPTO_NUMBER = 1<<OPTBIT_UPTO_NUMBER,
  348. OPT_UPTO_SIZE = 1<<OPTBIT_UPTO_SIZE ,
  349. OPT_EOF_STRING = 1<<OPTBIT_EOF_STRING ,
  350. OPT_INTERACTIVE = USE_FEATURE_XARGS_SUPPORT_CONFIRMATION((1<<OPTBIT_INTERACTIVE)) + 0,
  351. OPT_TERMINATE = USE_FEATURE_XARGS_SUPPORT_TERMOPT( (1<<OPTBIT_TERMINATE )) + 0,
  352. OPT_ZEROTERM = USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1<<OPTBIT_ZEROTERM )) + 0,
  353. };
  354. #define OPTION_STR "+trn:s:e::" \
  355. USE_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \
  356. USE_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \
  357. USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0")
  358. int xargs_main(int argc, char **argv)
  359. {
  360. char **args;
  361. int i, n;
  362. xlist_t *list = NULL;
  363. xlist_t *cur;
  364. int child_error = 0;
  365. char *max_args, *max_chars;
  366. int n_max_arg;
  367. size_t n_chars = 0;
  368. long orig_arg_max;
  369. const char *eof_str = "_";
  370. unsigned opt;
  371. size_t n_max_chars;
  372. #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
  373. xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin;
  374. #else
  375. #define read_args process_stdin
  376. #endif
  377. opt = getopt32(argc, argv, OPTION_STR, &max_args, &max_chars, &eof_str);
  378. if (opt & OPT_ZEROTERM)
  379. USE_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
  380. argc -= optind;
  381. argv += optind;
  382. if (!argc) {
  383. /* default behavior is to echo all the filenames */
  384. *argv = "echo";
  385. argc++;
  386. }
  387. orig_arg_max = ARG_MAX;
  388. if (orig_arg_max == -1)
  389. orig_arg_max = LONG_MAX;
  390. orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048 */
  391. if (opt & OPT_UPTO_SIZE) {
  392. n_max_chars = xatoul_range(max_chars, 1, orig_arg_max);
  393. for (i = 0; i < argc; i++) {
  394. n_chars += strlen(*argv) + 1;
  395. }
  396. if (n_max_chars < n_chars) {
  397. bb_error_msg_and_die("cannot fit single argument within argument list size limit");
  398. }
  399. n_max_chars -= n_chars;
  400. } else {
  401. /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which
  402. have it at 1 meg). Things will work fine with a large ARG_MAX but it
  403. will probably hurt the system more than it needs to; an array of this
  404. size is allocated. */
  405. if (orig_arg_max > 20 * 1024)
  406. orig_arg_max = 20 * 1024;
  407. n_max_chars = orig_arg_max;
  408. }
  409. max_chars = xmalloc(n_max_chars);
  410. if (opt & OPT_UPTO_NUMBER) {
  411. n_max_arg = xatoul_range(max_args, 1, INT_MAX);
  412. } else {
  413. n_max_arg = n_max_chars;
  414. }
  415. while ((list = read_args(list, eof_str, n_max_chars, max_chars)) != NULL ||
  416. !(opt & OPT_NO_EMPTY))
  417. {
  418. opt |= OPT_NO_EMPTY;
  419. n = 0;
  420. n_chars = 0;
  421. #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT
  422. for (cur = list; cur;) {
  423. n_chars += cur->lenght;
  424. n++;
  425. cur = cur->link;
  426. if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
  427. if (opt & OPT_TERMINATE)
  428. bb_error_msg_and_die("argument list too long");
  429. break;
  430. }
  431. }
  432. #else
  433. for (cur = list; cur; cur = cur->link) {
  434. n_chars += cur->lenght;
  435. n++;
  436. if (n_chars > n_max_chars || n == n_max_arg) {
  437. break;
  438. }
  439. }
  440. #endif /* CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT */
  441. /* allocate pointers for execvp:
  442. argc*arg, n*arg from stdin, NULL */
  443. args = xzalloc((n + argc + 1) * sizeof(char *));
  444. /* store the command to be executed
  445. (taken from the command line) */
  446. for (i = 0; i < argc; i++)
  447. args[i] = argv[i];
  448. /* (taken from stdin) */
  449. for (cur = list; n; cur = cur->link) {
  450. args[i++] = cur->data;
  451. n--;
  452. }
  453. if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) {
  454. for (i = 0; args[i]; i++) {
  455. if (i)
  456. fputc(' ', stderr);
  457. fputs(args[i], stderr);
  458. }
  459. if (!(opt & OPT_INTERACTIVE))
  460. fputc('\n', stderr);
  461. }
  462. if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) {
  463. child_error = xargs_exec(args);
  464. }
  465. /* clean up */
  466. for (i = argc; args[i]; i++) {
  467. cur = list;
  468. list = list->link;
  469. free(cur);
  470. }
  471. free(args);
  472. if (child_error > 0 && child_error != 123) {
  473. break;
  474. }
  475. }
  476. if (ENABLE_FEATURE_CLEAN_UP) free(max_chars);
  477. return child_error;
  478. }
  479. #ifdef TEST
  480. const char *applet_name = "debug stuff usage";
  481. void bb_show_usage(void)
  482. {
  483. fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n",
  484. applet_name);
  485. exit(1);
  486. }
  487. int main(int argc, char **argv)
  488. {
  489. return xargs_main(argc, argv);
  490. }
  491. #endif /* TEST */