shell_common.c 17 KB

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