shell_common.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Adapted from ash applet code
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Kenneth Almquist.
  7. *
  8. * Copyright (c) 1989, 1991, 1993, 1994
  9. * The Regents of the University of California. All rights reserved.
  10. *
  11. * Copyright (c) 1997-2005 Herbert Xu <herbert@gondor.apana.org.au>
  12. * was re-ported from NetBSD and debianized.
  13. *
  14. * Copyright (c) 2010 Denys Vlasenko
  15. * Split from ash.c
  16. *
  17. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  18. */
  19. #include "libbb.h"
  20. #include "shell_common.h"
  21. const char defifsvar[] ALIGN1 = "IFS= \t\n";
  22. const char defoptindvar[] ALIGN1 = "OPTIND=1";
  23. /* Compare two strings up to the first '=' or '\0'. */
  24. int FAST_FUNC varcmp(const char *p, const char *q)
  25. {
  26. int c, d;
  27. while ((c = *p) == (d = *q)) {
  28. if (c == '\0' || c == '=')
  29. goto out;
  30. p++;
  31. q++;
  32. }
  33. if (c == '=')
  34. c = '\0';
  35. if (d == '=')
  36. d = '\0';
  37. out:
  38. return c - d;
  39. }
  40. /* read builtin */
  41. /* Needs to be interruptible: shell must handle traps and shell-special signals
  42. * while inside read. To implement this, be sure to not loop on EINTR
  43. * and return errno == EINTR reliably.
  44. */
  45. //TODO: use more efficient setvar() which takes a pointer to malloced "VAR=VAL"
  46. //string. hush naturally has it, and ash has setvareq().
  47. //Here we can simply store "VAR=" at buffer start and store read data directly
  48. //after "=", then pass buffer to setvar() to consume.
  49. const char* FAST_FUNC
  50. shell_builtin_read(struct builtin_read_params *params)
  51. {
  52. struct pollfd pfd[1];
  53. #define fd (pfd[0].fd) /* -u FD */
  54. unsigned err;
  55. unsigned end_ms; /* -t TIMEOUT */
  56. int nchars; /* -n NUM */
  57. char **pp;
  58. char *buffer;
  59. char delim;
  60. struct termios tty, old_tty;
  61. const char *retval;
  62. int bufpos; /* need to be able to hold -1 */
  63. int startword;
  64. smallint backslash;
  65. char **argv;
  66. const char *ifs;
  67. int read_flags;
  68. errno = err = 0;
  69. argv = params->argv;
  70. pp = argv;
  71. while (*pp) {
  72. if (!*pp[0] || endofname(*pp)[0] != '\0') {
  73. /* Mimic bash message */
  74. bb_error_msg("read: '%s': bad variable name", *pp);
  75. return (const char *)(uintptr_t)1;
  76. }
  77. pp++;
  78. }
  79. nchars = 0; /* if != 0, -n is in effect */
  80. if (params->opt_n) {
  81. nchars = bb_strtou(params->opt_n, NULL, 10);
  82. if (nchars < 0 || errno)
  83. return "invalid count";
  84. /* note: "-n 0": off (bash 3.2 does this too) */
  85. }
  86. end_ms = 0;
  87. if (params->opt_t && !ENABLE_FEATURE_SH_READ_FRAC) {
  88. end_ms = bb_strtou(params->opt_t, NULL, 10);
  89. if (errno)
  90. return "invalid timeout";
  91. if (end_ms > UINT_MAX / 2048) /* be safely away from overflow */
  92. end_ms = UINT_MAX / 2048;
  93. end_ms *= 1000;
  94. }
  95. if (params->opt_t && ENABLE_FEATURE_SH_READ_FRAC) {
  96. /* bash 4.3 (maybe earlier) supports -t N.NNNNNN */
  97. char *p;
  98. /* Eat up to three fractional digits */
  99. int frac_digits = 3 + 1;
  100. end_ms = bb_strtou(params->opt_t, &p, 10);
  101. if (end_ms > UINT_MAX / 2048) /* be safely away from overflow */
  102. end_ms = UINT_MAX / 2048;
  103. if (errno) {
  104. /* EINVAL = number is ok, but not NUL terminated */
  105. if (errno != EINVAL || *p != '.')
  106. return "invalid timeout";
  107. /* Do not check the rest: bash allows "0.123456xyz" */
  108. while (*++p && --frac_digits) {
  109. end_ms *= 10;
  110. end_ms += (*p - '0');
  111. if ((unsigned char)(*p - '0') > 9)
  112. return "invalid timeout";
  113. }
  114. }
  115. while (--frac_digits > 0) {
  116. end_ms *= 10;
  117. }
  118. }
  119. fd = STDIN_FILENO;
  120. if (params->opt_u) {
  121. fd = bb_strtou(params->opt_u, NULL, 10);
  122. if (fd < 0 || errno)
  123. return "invalid file descriptor";
  124. }
  125. if (params->opt_t && end_ms == 0) {
  126. /* "If timeout is 0, read returns immediately, without trying
  127. * to read any data. The exit status is 0 if input is available
  128. * on the specified file descriptor, non-zero otherwise."
  129. * bash seems to ignore -p PROMPT for this use case.
  130. */
  131. int r;
  132. pfd[0].events = POLLIN;
  133. r = poll(pfd, 1, /*timeout:*/ 0);
  134. /* Return 0 only if poll returns 1 ("one fd ready"), else return 1: */
  135. return (const char *)(uintptr_t)(r <= 0);
  136. }
  137. if (params->opt_p && isatty(fd)) {
  138. fputs(params->opt_p, stderr);
  139. fflush_all();
  140. }
  141. ifs = params->ifs;
  142. if (ifs == NULL)
  143. ifs = defifs;
  144. read_flags = params->read_flags;
  145. if (nchars || (read_flags & BUILTIN_READ_SILENT)) {
  146. tcgetattr(fd, &tty);
  147. old_tty = tty;
  148. if (nchars) {
  149. tty.c_lflag &= ~ICANON;
  150. // Setting it to more than 1 breaks poll():
  151. // it blocks even if there's data. !??
  152. //tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
  153. /* reads will block only if < 1 char is available */
  154. tty.c_cc[VMIN] = 1;
  155. /* no timeout (reads block forever) */
  156. tty.c_cc[VTIME] = 0;
  157. }
  158. if (read_flags & BUILTIN_READ_SILENT) {
  159. tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
  160. }
  161. /* This forces execution of "restoring" tcgetattr later */
  162. read_flags |= BUILTIN_READ_SILENT;
  163. /* if tcgetattr failed, tcsetattr will fail too.
  164. * Ignoring, it's harmless. */
  165. tcsetattr(fd, TCSANOW, &tty);
  166. }
  167. retval = (const char *)(uintptr_t)0;
  168. startword = 1;
  169. backslash = 0;
  170. if (params->opt_t)
  171. end_ms += (unsigned)monotonic_ms();
  172. buffer = NULL;
  173. bufpos = 0;
  174. delim = params->opt_d ? params->opt_d[0] : '\n';
  175. do {
  176. char c;
  177. int timeout;
  178. if ((bufpos & 0xff) == 0)
  179. buffer = xrealloc(buffer, bufpos + 0x101);
  180. timeout = -1;
  181. if (params->opt_t) {
  182. timeout = end_ms - (unsigned)monotonic_ms();
  183. /* ^^^^^^^^^^^^^ all values are unsigned,
  184. * wrapping math is used here, good even if
  185. * 32-bit unix time wrapped (year 2038+).
  186. */
  187. if (timeout <= 0) { /* already late? */
  188. retval = (const char *)(uintptr_t)1;
  189. goto ret;
  190. }
  191. }
  192. /* We must poll even if timeout is -1:
  193. * we want to be interrupted if signal arrives,
  194. * regardless of SA_RESTART-ness of that signal!
  195. */
  196. errno = 0;
  197. pfd[0].events = POLLIN;
  198. //TODO race with a signal arriving just before the poll!
  199. if (poll(pfd, 1, timeout) <= 0) {
  200. /* timed out, or EINTR */
  201. err = errno;
  202. retval = (const char *)(uintptr_t)1;
  203. goto ret;
  204. }
  205. if (read(fd, &buffer[bufpos], 1) != 1) {
  206. err = errno;
  207. retval = (const char *)(uintptr_t)1;
  208. break;
  209. }
  210. c = buffer[bufpos];
  211. if (!(read_flags & BUILTIN_READ_RAW)) {
  212. if (backslash) {
  213. backslash = 0;
  214. if (c != '\n')
  215. goto put;
  216. continue;
  217. }
  218. if (c == '\\') {
  219. backslash = 1;
  220. continue;
  221. }
  222. }
  223. if (c == delim) /* '\n' or -d CHAR */
  224. break;
  225. if (c == '\0')
  226. continue;
  227. /* $IFS splitting. NOT done if we run "read"
  228. * without variable names (bash compat).
  229. * Thus, "read" and "read REPLY" are not the same.
  230. */
  231. if (argv[0]) {
  232. /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */
  233. const char *is_ifs = strchr(ifs, c);
  234. if (startword && is_ifs) {
  235. if (isspace(c))
  236. continue;
  237. /* it is a non-space ifs char */
  238. startword--;
  239. if (startword == 1) /* first one? */
  240. continue; /* yes, it is not next word yet */
  241. }
  242. startword = 0;
  243. if (argv[1] != NULL && is_ifs) {
  244. buffer[bufpos] = '\0';
  245. bufpos = 0;
  246. params->setvar(*argv, buffer);
  247. argv++;
  248. /* can we skip one non-space ifs char? (2: yes) */
  249. startword = isspace(c) ? 2 : 1;
  250. continue;
  251. }
  252. }
  253. put:
  254. bufpos++;
  255. } while (--nchars);
  256. if (argv[0]) {
  257. /* Remove trailing space $IFS chars */
  258. while (--bufpos >= 0
  259. && isspace(buffer[bufpos])
  260. && strchr(ifs, buffer[bufpos]) != NULL
  261. ) {
  262. continue;
  263. }
  264. buffer[bufpos + 1] = '\0';
  265. /* Last variable takes the entire remainder with delimiters
  266. * (sans trailing whitespace $IFS),
  267. * but ***only "if there are fewer vars than fields"(c)***!
  268. * The "X:Y:" case below: there are two fields,
  269. * and therefore last delimiter (:) is eaten:
  270. * IFS=": "
  271. * echo "X:Y:Z:" | (read x y; echo "|$x|$y|") # |X|Y:Z:|
  272. * echo "X:Y:Z" | (read x y; echo "|$x|$y|") # |X|Y:Z|
  273. * echo "X:Y:" | (read x y; echo "|$x|$y|") # |X|Y|, not |X|Y:|
  274. * echo "X:Y : " | (read x y; echo "|$x|$y|") # |X|Y|
  275. */
  276. if (bufpos >= 0
  277. && strchr(ifs, buffer[bufpos]) != NULL
  278. ) {
  279. /* There _is_ a non-whitespace IFS char */
  280. /* Skip whitespace IFS char before it */
  281. while (--bufpos >= 0
  282. && isspace(buffer[bufpos])
  283. && strchr(ifs, buffer[bufpos]) != NULL
  284. ) {
  285. continue;
  286. }
  287. /* Are there $IFS chars? */
  288. if (strcspn(buffer, ifs) >= ++bufpos) {
  289. /* No: last var takes one field, not more */
  290. /* So, drop trailing IFS delims */
  291. buffer[bufpos] = '\0';
  292. }
  293. }
  294. /* Use the remainder as a value for the next variable */
  295. params->setvar(*argv, buffer);
  296. /* Set the rest to "" */
  297. while (*++argv)
  298. params->setvar(*argv, "");
  299. } else {
  300. /* Note: no $IFS removal */
  301. buffer[bufpos] = '\0';
  302. params->setvar("REPLY", buffer);
  303. }
  304. ret:
  305. free(buffer);
  306. if (read_flags & BUILTIN_READ_SILENT)
  307. tcsetattr(fd, TCSANOW, &old_tty);
  308. errno = err;
  309. return retval;
  310. #undef fd
  311. }
  312. /* ulimit builtin */
  313. struct limits {
  314. uint8_t cmd; /* RLIMIT_xxx fit into it */
  315. uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */
  316. };
  317. /* Order of entries matches order in which bash prints "ulimit -a" */
  318. static const struct limits limits_tbl[] ALIGN2 = {
  319. { RLIMIT_CORE, 9, }, // -c
  320. { RLIMIT_DATA, 10, }, // -d
  321. #ifdef RLIMIT_NICE
  322. { RLIMIT_NICE, 0, }, // -e
  323. #define LIMIT_F_IDX 3
  324. #else
  325. /* for example, Hurd */
  326. #define LIMIT_F_IDX 2
  327. #endif
  328. { RLIMIT_FSIZE, 9, }, // -f
  329. #ifdef RLIMIT_SIGPENDING
  330. { RLIMIT_SIGPENDING, 0, }, // -i
  331. #endif
  332. #ifdef RLIMIT_MEMLOCK
  333. { RLIMIT_MEMLOCK, 10, }, // -l
  334. #endif
  335. #ifdef RLIMIT_RSS
  336. { RLIMIT_RSS, 10, }, // -m
  337. #endif
  338. #ifdef RLIMIT_NOFILE
  339. { RLIMIT_NOFILE, 0, }, // -n
  340. #endif
  341. #ifdef RLIMIT_MSGQUEUE
  342. { RLIMIT_MSGQUEUE, 0, }, // -q
  343. #endif
  344. #ifdef RLIMIT_RTPRIO
  345. { RLIMIT_RTPRIO, 0, }, // -r
  346. #endif
  347. #ifdef RLIMIT_STACK
  348. { RLIMIT_STACK, 10, }, // -s
  349. #endif
  350. #ifdef RLIMIT_CPU
  351. { RLIMIT_CPU, 0, }, // -t
  352. #endif
  353. #ifdef RLIMIT_NPROC
  354. { RLIMIT_NPROC, 0, }, // -u
  355. #endif
  356. #ifdef RLIMIT_AS
  357. { RLIMIT_AS, 10, }, // -v
  358. #endif
  359. #ifdef RLIMIT_LOCKS
  360. { RLIMIT_LOCKS, 0, }, // -x
  361. #endif
  362. };
  363. // 1) bash also shows:
  364. //pipe size (512 bytes, -p) 8
  365. // 2) RLIMIT_RTTIME ("timeout for RT tasks in us") is not in the table
  366. static const char limits_help[] ALIGN1 =
  367. "core file size (blocks)" // -c
  368. "\0""data seg size (kb)" // -d
  369. #ifdef RLIMIT_NICE
  370. "\0""scheduling priority" // -e
  371. #endif
  372. "\0""file size (blocks)" // -f
  373. #ifdef RLIMIT_SIGPENDING
  374. "\0""pending signals" // -i
  375. #endif
  376. #ifdef RLIMIT_MEMLOCK
  377. "\0""max locked memory (kb)" // -l
  378. #endif
  379. #ifdef RLIMIT_RSS
  380. "\0""max memory size (kb)" // -m
  381. #endif
  382. #ifdef RLIMIT_NOFILE
  383. "\0""open files" // -n
  384. #endif
  385. #ifdef RLIMIT_MSGQUEUE
  386. "\0""POSIX message queues (bytes)" // -q
  387. #endif
  388. #ifdef RLIMIT_RTPRIO
  389. "\0""real-time priority" // -r
  390. #endif
  391. #ifdef RLIMIT_STACK
  392. "\0""stack size (kb)" // -s
  393. #endif
  394. #ifdef RLIMIT_CPU
  395. "\0""cpu time (seconds)" // -t
  396. #endif
  397. #ifdef RLIMIT_NPROC
  398. "\0""max user processes" // -u
  399. #endif
  400. #ifdef RLIMIT_AS
  401. "\0""virtual memory (kb)" // -v
  402. #endif
  403. #ifdef RLIMIT_LOCKS
  404. "\0""file locks" // -x
  405. #endif
  406. ;
  407. static const char limit_chars[] ALIGN1 =
  408. "c"
  409. "d"
  410. #ifdef RLIMIT_NICE
  411. "e"
  412. #endif
  413. "f"
  414. #ifdef RLIMIT_SIGPENDING
  415. "i"
  416. #endif
  417. #ifdef RLIMIT_MEMLOCK
  418. "l"
  419. #endif
  420. #ifdef RLIMIT_RSS
  421. "m"
  422. #endif
  423. #ifdef RLIMIT_NOFILE
  424. "n"
  425. #endif
  426. #ifdef RLIMIT_MSGQUEUE
  427. "q"
  428. #endif
  429. #ifdef RLIMIT_RTPRIO
  430. "r"
  431. #endif
  432. #ifdef RLIMIT_STACK
  433. "s"
  434. #endif
  435. #ifdef RLIMIT_CPU
  436. "t"
  437. #endif
  438. #ifdef RLIMIT_NPROC
  439. "u"
  440. #endif
  441. #ifdef RLIMIT_AS
  442. "v"
  443. #endif
  444. #ifdef RLIMIT_LOCKS
  445. "x"
  446. #endif
  447. ;
  448. /* "-": treat args as parameters of option with ASCII code 1 */
  449. static const char ulimit_opt_string[] ALIGN1 = "-HSa"
  450. "c::"
  451. "d::"
  452. #ifdef RLIMIT_NICE
  453. "e::"
  454. #endif
  455. "f::"
  456. #ifdef RLIMIT_SIGPENDING
  457. "i::"
  458. #endif
  459. #ifdef RLIMIT_MEMLOCK
  460. "l::"
  461. #endif
  462. #ifdef RLIMIT_RSS
  463. "m::"
  464. #endif
  465. #ifdef RLIMIT_NOFILE
  466. "n::"
  467. #endif
  468. #ifdef RLIMIT_MSGQUEUE
  469. "q::"
  470. #endif
  471. #ifdef RLIMIT_RTPRIO
  472. "r::"
  473. #endif
  474. #ifdef RLIMIT_STACK
  475. "s::"
  476. #endif
  477. #ifdef RLIMIT_CPU
  478. "t::"
  479. #endif
  480. #ifdef RLIMIT_NPROC
  481. "u::"
  482. #endif
  483. #ifdef RLIMIT_AS
  484. "v::"
  485. #endif
  486. #ifdef RLIMIT_LOCKS
  487. "x::"
  488. #endif
  489. ;
  490. enum {
  491. OPT_hard = (1 << 0),
  492. OPT_soft = (1 << 1),
  493. OPT_all = (1 << 2),
  494. };
  495. static void printlim(unsigned opts, const struct rlimit *limit,
  496. const struct limits *l)
  497. {
  498. rlim_t val;
  499. val = limit->rlim_max;
  500. if (opts & OPT_soft)
  501. val = limit->rlim_cur;
  502. if (val == RLIM_INFINITY)
  503. puts("unlimited");
  504. else {
  505. val >>= l->factor_shift;
  506. printf("%llu\n", (long long) val);
  507. }
  508. }
  509. int FAST_FUNC
  510. shell_builtin_ulimit(char **argv)
  511. {
  512. struct rlimit limit;
  513. unsigned opt_cnt;
  514. unsigned opts;
  515. unsigned argc;
  516. unsigned i;
  517. /* We can't use getopt32: need to handle commands like
  518. * ulimit 123 -c2 -l 456
  519. */
  520. /* In case getopt() was already called:
  521. * reset libc getopt() internal state.
  522. */
  523. GETOPT_RESET();
  524. // bash 4.4.23:
  525. //
  526. // -H and/or -S change meaning even of options *before* them: ulimit -f 2000 -H
  527. // sets hard limit, ulimit -a -H prints hard limits.
  528. //
  529. // -a is equivalent for requesting all limits to be shown.
  530. //
  531. // If -a is specified, attempts to set limits are ignored:
  532. // ulimit -m 1000; ulimit -m 2000 -a
  533. // shows 1000, not 2000. HOWEVER, *implicit* -f form "ulimit 2000 -a"
  534. // DOES set -f limit [we don't implement this quirk], "ulimit -a 2000" does not.
  535. // Options are still parsed: ulimit -az complains about unknown -z opt.
  536. //
  537. // -a is not cumulative: "ulimit -a -a" = "ulimit -a -f -m" = "ulimit -a"
  538. //
  539. // -HSa can be combined in one argument and with one other option (example: -Sm),
  540. // but other options can't: limit value is an optional argument,
  541. // thus "-mf" means "-m f", f is the parameter of -m.
  542. //
  543. // Limit can be set and then printed: ulimit -m 2000 -m
  544. // If set more than once, they are set and printed in order:
  545. // try ulimit -m -m 1000 -m -m 2000 -m -m 3000 -m
  546. //
  547. // Limits are shown in the order of options given:
  548. // ulimit -m -f is not the same as ulimit -f -m.
  549. //
  550. // If both -S and -H are given, show soft limit.
  551. //
  552. // Short printout (limit value only) is printed only if just one option
  553. // is given: ulimit -m. ulimit -f -m prints verbose lines.
  554. // ulimit -f -f prints same verbose line twice.
  555. // ulimit -m 10000 -f prints verbose line for -f.
  556. argc = string_array_len(argv);
  557. /* First pass over options: detect -H/-S/-a status,
  558. * and "bare ulimit" and "only one option" cases
  559. * by counting other opts.
  560. */
  561. opt_cnt = 0;
  562. opts = 0;
  563. while (1) {
  564. int opt_char = getopt(argc, argv, ulimit_opt_string);
  565. if (opt_char == -1)
  566. break;
  567. if (opt_char == 'H') {
  568. opts |= OPT_hard;
  569. continue;
  570. }
  571. if (opt_char == 'S') {
  572. opts |= OPT_soft;
  573. continue;
  574. }
  575. if (opt_char == 'a') {
  576. opts |= OPT_all;
  577. continue;
  578. }
  579. if (opt_char == '?') {
  580. /* bad option. getopt already complained. */
  581. return EXIT_FAILURE;
  582. }
  583. opt_cnt++;
  584. } /* while (there are options) */
  585. if (!(opts & (OPT_hard | OPT_soft)))
  586. opts |= (OPT_hard | OPT_soft);
  587. if (opts & OPT_all) {
  588. const char *help = limits_help;
  589. for (i = 0; i < ARRAY_SIZE(limits_tbl); i++) {
  590. getrlimit(limits_tbl[i].cmd, &limit);
  591. printf("%-32s(-%c) ", help, limit_chars[i]);
  592. printlim(opts, &limit, &limits_tbl[i]);
  593. help += strlen(help) + 1;
  594. }
  595. return EXIT_SUCCESS;
  596. }
  597. /* Second pass: set or print limits, in order */
  598. GETOPT_RESET();
  599. while (1) {
  600. char *val_str;
  601. int opt_char = getopt(argc, argv, ulimit_opt_string);
  602. if (opt_char == -1)
  603. break;
  604. if (opt_char == 'H')
  605. continue;
  606. if (opt_char == 'S')
  607. continue;
  608. //if (opt_char == 'a') - impossible
  609. if (opt_char == 1) /* if "ulimit NNN", -f is assumed */
  610. opt_char = 'f';
  611. i = strchrnul(limit_chars, opt_char) - limit_chars;
  612. //if (i >= ARRAY_SIZE(limits_tbl)) - bad option, impossible
  613. val_str = optarg;
  614. if (!val_str && argv[optind] && argv[optind][0] != '-')
  615. val_str = argv[optind++]; /* ++ skips NN in "-c NN" case */
  616. getrlimit(limits_tbl[i].cmd, &limit);
  617. if (!val_str) {
  618. if (opt_cnt > 1)
  619. printf("%-32s(-%c) ", nth_string(limits_help, i), limit_chars[i]);
  620. printlim(opts, &limit, &limits_tbl[i]);
  621. } else {
  622. rlim_t val = RLIM_INFINITY;
  623. if (strcmp(val_str, "unlimited") != 0) {
  624. if (sizeof(val) == sizeof(int))
  625. val = bb_strtou(val_str, NULL, 10);
  626. else if (sizeof(val) == sizeof(long))
  627. val = bb_strtoul(val_str, NULL, 10);
  628. else
  629. val = bb_strtoull(val_str, NULL, 10);
  630. if (errno) {
  631. bb_error_msg("invalid number '%s'", val_str);
  632. return EXIT_FAILURE;
  633. }
  634. val <<= limits_tbl[i].factor_shift;
  635. }
  636. //bb_error_msg("opt %c val_str:'%s' val:%lld", opt_char, val_str, (long long)val);
  637. /* from man bash: "If neither -H nor -S
  638. * is specified, both the soft and hard
  639. * limits are set. */
  640. if (opts & OPT_hard)
  641. limit.rlim_max = val;
  642. if (opts & OPT_soft)
  643. limit.rlim_cur = val;
  644. //bb_error_msg("setrlimit(%d, %lld, %lld)", limits_tbl[i].cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max);
  645. if (setrlimit(limits_tbl[i].cmd, &limit) < 0) {
  646. bb_simple_perror_msg("error setting limit");
  647. return EXIT_FAILURE;
  648. }
  649. }
  650. } /* while (there are options) */
  651. if (opt_cnt == 0) {
  652. /* "bare ulimit": treat it as if it was -f */
  653. getrlimit(RLIMIT_FSIZE, &limit);
  654. printlim(opts, &limit, &limits_tbl[LIMIT_F_IDX]);
  655. }
  656. return EXIT_SUCCESS;
  657. }