shell_common.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. if (poll(pfd, 1, timeout) <= 0) {
  182. /* timed out, or EINTR */
  183. err = errno;
  184. retval = (const char *)(uintptr_t)1;
  185. goto ret;
  186. }
  187. if (read(fd, &buffer[bufpos], 1) != 1) {
  188. err = errno;
  189. retval = (const char *)(uintptr_t)1;
  190. break;
  191. }
  192. c = buffer[bufpos];
  193. if (!(read_flags & BUILTIN_READ_RAW)) {
  194. if (backslash) {
  195. backslash = 0;
  196. if (c != '\n')
  197. goto put;
  198. continue;
  199. }
  200. if (c == '\\') {
  201. backslash = 1;
  202. continue;
  203. }
  204. }
  205. if (c == delim) /* '\n' or -d CHAR */
  206. break;
  207. if (c == '\0')
  208. continue;
  209. /* $IFS splitting. NOT done if we run "read"
  210. * without variable names (bash compat).
  211. * Thus, "read" and "read REPLY" are not the same.
  212. */
  213. if (argv[0]) {
  214. /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */
  215. const char *is_ifs = strchr(ifs, c);
  216. if (startword && is_ifs) {
  217. if (isspace(c))
  218. continue;
  219. /* it is a non-space ifs char */
  220. startword--;
  221. if (startword == 1) /* first one? */
  222. continue; /* yes, it is not next word yet */
  223. }
  224. startword = 0;
  225. if (argv[1] != NULL && is_ifs) {
  226. buffer[bufpos] = '\0';
  227. bufpos = 0;
  228. params->setvar(*argv, buffer);
  229. argv++;
  230. /* can we skip one non-space ifs char? (2: yes) */
  231. startword = isspace(c) ? 2 : 1;
  232. continue;
  233. }
  234. }
  235. put:
  236. bufpos++;
  237. } while (--nchars);
  238. if (argv[0]) {
  239. /* Remove trailing space $IFS chars */
  240. while (--bufpos >= 0
  241. && isspace(buffer[bufpos])
  242. && strchr(ifs, buffer[bufpos]) != NULL
  243. ) {
  244. continue;
  245. }
  246. buffer[bufpos + 1] = '\0';
  247. /* Last variable takes the entire remainder with delimiters
  248. * (sans trailing whitespace $IFS),
  249. * but ***only "if there are fewer vars than fields"(c)***!
  250. * The "X:Y:" case below: there are two fields,
  251. * and therefore last delimiter (:) is eaten:
  252. * IFS=": "
  253. * echo "X:Y:Z:" | (read x y; echo "|$x|$y|") # |X|Y:Z:|
  254. * echo "X:Y:Z" | (read x y; echo "|$x|$y|") # |X|Y:Z|
  255. * echo "X:Y:" | (read x y; echo "|$x|$y|") # |X|Y|, not |X|Y:|
  256. * echo "X:Y : " | (read x y; echo "|$x|$y|") # |X|Y|
  257. */
  258. if (bufpos >= 0
  259. && strchr(ifs, buffer[bufpos]) != NULL
  260. ) {
  261. /* There _is_ a non-whitespace IFS char */
  262. /* Skip whitespace IFS char before it */
  263. while (--bufpos >= 0
  264. && isspace(buffer[bufpos])
  265. && strchr(ifs, buffer[bufpos]) != NULL
  266. ) {
  267. continue;
  268. }
  269. /* Are there $IFS chars? */
  270. if (strcspn(buffer, ifs) >= ++bufpos) {
  271. /* No: last var takes one field, not more */
  272. /* So, drop trailing IFS delims */
  273. buffer[bufpos] = '\0';
  274. }
  275. }
  276. /* Use the remainder as a value for the next variable */
  277. params->setvar(*argv, buffer);
  278. /* Set the rest to "" */
  279. while (*++argv)
  280. params->setvar(*argv, "");
  281. } else {
  282. /* Note: no $IFS removal */
  283. buffer[bufpos] = '\0';
  284. params->setvar("REPLY", buffer);
  285. }
  286. ret:
  287. free(buffer);
  288. if (read_flags & BUILTIN_READ_SILENT)
  289. tcsetattr(fd, TCSANOW, &old_tty);
  290. errno = err;
  291. return retval;
  292. #undef fd
  293. }
  294. /* ulimit builtin */
  295. struct limits {
  296. uint8_t cmd; /* RLIMIT_xxx fit into it */
  297. uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */
  298. };
  299. /* Order of entries matches order in which bash prints "ulimit -a" */
  300. static const struct limits limits_tbl[] ALIGN2 = {
  301. { RLIMIT_CORE, 9, }, // -c
  302. { RLIMIT_DATA, 10, }, // -d
  303. #ifdef RLIMIT_NICE
  304. { RLIMIT_NICE, 0, }, // -e
  305. #define LIMIT_F_IDX 3
  306. #else
  307. /* for example, Hurd */
  308. #define LIMIT_F_IDX 2
  309. #endif
  310. { RLIMIT_FSIZE, 9, }, // -f
  311. #ifdef RLIMIT_SIGPENDING
  312. { RLIMIT_SIGPENDING, 0, }, // -i
  313. #endif
  314. #ifdef RLIMIT_MEMLOCK
  315. { RLIMIT_MEMLOCK, 10, }, // -l
  316. #endif
  317. #ifdef RLIMIT_RSS
  318. { RLIMIT_RSS, 10, }, // -m
  319. #endif
  320. #ifdef RLIMIT_NOFILE
  321. { RLIMIT_NOFILE, 0, }, // -n
  322. #endif
  323. #ifdef RLIMIT_MSGQUEUE
  324. { RLIMIT_MSGQUEUE, 0, }, // -q
  325. #endif
  326. #ifdef RLIMIT_RTPRIO
  327. { RLIMIT_RTPRIO, 0, }, // -r
  328. #endif
  329. #ifdef RLIMIT_STACK
  330. { RLIMIT_STACK, 10, }, // -s
  331. #endif
  332. #ifdef RLIMIT_CPU
  333. { RLIMIT_CPU, 0, }, // -t
  334. #endif
  335. #ifdef RLIMIT_NPROC
  336. { RLIMIT_NPROC, 0, }, // -u
  337. #endif
  338. #ifdef RLIMIT_AS
  339. { RLIMIT_AS, 10, }, // -v
  340. #endif
  341. #ifdef RLIMIT_LOCKS
  342. { RLIMIT_LOCKS, 0, }, // -x
  343. #endif
  344. };
  345. // 1) bash also shows:
  346. //pipe size (512 bytes, -p) 8
  347. // 2) RLIMIT_RTTIME ("timeout for RT tasks in us") is not in the table
  348. static const char limits_help[] ALIGN1 =
  349. "core file size (blocks)" // -c
  350. "\0""data seg size (kb)" // -d
  351. #ifdef RLIMIT_NICE
  352. "\0""scheduling priority" // -e
  353. #endif
  354. "\0""file size (blocks)" // -f
  355. #ifdef RLIMIT_SIGPENDING
  356. "\0""pending signals" // -i
  357. #endif
  358. #ifdef RLIMIT_MEMLOCK
  359. "\0""max locked memory (kb)" // -l
  360. #endif
  361. #ifdef RLIMIT_RSS
  362. "\0""max memory size (kb)" // -m
  363. #endif
  364. #ifdef RLIMIT_NOFILE
  365. "\0""open files" // -n
  366. #endif
  367. #ifdef RLIMIT_MSGQUEUE
  368. "\0""POSIX message queues (bytes)" // -q
  369. #endif
  370. #ifdef RLIMIT_RTPRIO
  371. "\0""real-time priority" // -r
  372. #endif
  373. #ifdef RLIMIT_STACK
  374. "\0""stack size (kb)" // -s
  375. #endif
  376. #ifdef RLIMIT_CPU
  377. "\0""cpu time (seconds)" // -t
  378. #endif
  379. #ifdef RLIMIT_NPROC
  380. "\0""max user processes" // -u
  381. #endif
  382. #ifdef RLIMIT_AS
  383. "\0""virtual memory (kb)" // -v
  384. #endif
  385. #ifdef RLIMIT_LOCKS
  386. "\0""file locks" // -x
  387. #endif
  388. ;
  389. static const char limit_chars[] ALIGN1 =
  390. "c"
  391. "d"
  392. #ifdef RLIMIT_NICE
  393. "e"
  394. #endif
  395. "f"
  396. #ifdef RLIMIT_SIGPENDING
  397. "i"
  398. #endif
  399. #ifdef RLIMIT_MEMLOCK
  400. "l"
  401. #endif
  402. #ifdef RLIMIT_RSS
  403. "m"
  404. #endif
  405. #ifdef RLIMIT_NOFILE
  406. "n"
  407. #endif
  408. #ifdef RLIMIT_MSGQUEUE
  409. "q"
  410. #endif
  411. #ifdef RLIMIT_RTPRIO
  412. "r"
  413. #endif
  414. #ifdef RLIMIT_STACK
  415. "s"
  416. #endif
  417. #ifdef RLIMIT_CPU
  418. "t"
  419. #endif
  420. #ifdef RLIMIT_NPROC
  421. "u"
  422. #endif
  423. #ifdef RLIMIT_AS
  424. "v"
  425. #endif
  426. #ifdef RLIMIT_LOCKS
  427. "x"
  428. #endif
  429. ;
  430. /* "-": treat args as parameters of option with ASCII code 1 */
  431. static const char ulimit_opt_string[] ALIGN1 = "-HSa"
  432. "c::"
  433. "d::"
  434. #ifdef RLIMIT_NICE
  435. "e::"
  436. #endif
  437. "f::"
  438. #ifdef RLIMIT_SIGPENDING
  439. "i::"
  440. #endif
  441. #ifdef RLIMIT_MEMLOCK
  442. "l::"
  443. #endif
  444. #ifdef RLIMIT_RSS
  445. "m::"
  446. #endif
  447. #ifdef RLIMIT_NOFILE
  448. "n::"
  449. #endif
  450. #ifdef RLIMIT_MSGQUEUE
  451. "q::"
  452. #endif
  453. #ifdef RLIMIT_RTPRIO
  454. "r::"
  455. #endif
  456. #ifdef RLIMIT_STACK
  457. "s::"
  458. #endif
  459. #ifdef RLIMIT_CPU
  460. "t::"
  461. #endif
  462. #ifdef RLIMIT_NPROC
  463. "u::"
  464. #endif
  465. #ifdef RLIMIT_AS
  466. "v::"
  467. #endif
  468. #ifdef RLIMIT_LOCKS
  469. "x::"
  470. #endif
  471. ;
  472. enum {
  473. OPT_hard = (1 << 0),
  474. OPT_soft = (1 << 1),
  475. OPT_all = (1 << 2),
  476. };
  477. static void printlim(unsigned opts, const struct rlimit *limit,
  478. const struct limits *l)
  479. {
  480. rlim_t val;
  481. val = limit->rlim_max;
  482. if (opts & OPT_soft)
  483. val = limit->rlim_cur;
  484. if (val == RLIM_INFINITY)
  485. puts("unlimited");
  486. else {
  487. val >>= l->factor_shift;
  488. printf("%llu\n", (long long) val);
  489. }
  490. }
  491. int FAST_FUNC
  492. shell_builtin_ulimit(char **argv)
  493. {
  494. struct rlimit limit;
  495. unsigned opt_cnt;
  496. unsigned opts;
  497. unsigned argc;
  498. unsigned i;
  499. /* We can't use getopt32: need to handle commands like
  500. * ulimit 123 -c2 -l 456
  501. */
  502. /* In case getopt() was already called:
  503. * reset libc getopt() internal state.
  504. */
  505. GETOPT_RESET();
  506. // bash 4.4.23:
  507. //
  508. // -H and/or -S change meaning even of options *before* them: ulimit -f 2000 -H
  509. // sets hard limit, ulimit -a -H prints hard limits.
  510. //
  511. // -a is equivalent for requesting all limits to be shown.
  512. //
  513. // If -a is specified, attempts to set limits are ignored:
  514. // ulimit -m 1000; ulimit -m 2000 -a
  515. // shows 1000, not 2000. HOWEVER, *implicit* -f form "ulimit 2000 -a"
  516. // DOES set -f limit [we don't implement this quirk], "ulimit -a 2000" does not.
  517. // Options are still parsed: ulimit -az complains about unknown -z opt.
  518. //
  519. // -a is not cumulative: "ulimit -a -a" = "ulimit -a -f -m" = "ulimit -a"
  520. //
  521. // -HSa can be combined in one argument and with one other option (example: -Sm),
  522. // but other options can't: limit value is an optional argument,
  523. // thus "-mf" means "-m f", f is the parameter of -m.
  524. //
  525. // Limit can be set and then printed: ulimit -m 2000 -m
  526. // If set more than once, they are set and printed in order:
  527. // try ulimit -m -m 1000 -m -m 2000 -m -m 3000 -m
  528. //
  529. // Limits are shown in the order of options given:
  530. // ulimit -m -f is not the same as ulimit -f -m.
  531. //
  532. // If both -S and -H are given, show soft limit.
  533. //
  534. // Short printout (limit value only) is printed only if just one option
  535. // is given: ulimit -m. ulimit -f -m prints verbose lines.
  536. // ulimit -f -f prints same verbose line twice.
  537. // ulimit -m 10000 -f prints verbose line for -f.
  538. argc = string_array_len(argv);
  539. /* First pass over options: detect -H/-S/-a status,
  540. * and "bare ulimit" and "only one option" cases
  541. * by counting other opts.
  542. */
  543. opt_cnt = 0;
  544. opts = 0;
  545. while (1) {
  546. int opt_char = getopt(argc, argv, ulimit_opt_string);
  547. if (opt_char == -1)
  548. break;
  549. if (opt_char == 'H') {
  550. opts |= OPT_hard;
  551. continue;
  552. }
  553. if (opt_char == 'S') {
  554. opts |= OPT_soft;
  555. continue;
  556. }
  557. if (opt_char == 'a') {
  558. opts |= OPT_all;
  559. continue;
  560. }
  561. if (opt_char == '?') {
  562. /* bad option. getopt already complained. */
  563. return EXIT_FAILURE;
  564. }
  565. opt_cnt++;
  566. } /* while (there are options) */
  567. if (!(opts & (OPT_hard | OPT_soft)))
  568. opts |= (OPT_hard | OPT_soft);
  569. if (opts & OPT_all) {
  570. const char *help = limits_help;
  571. for (i = 0; i < ARRAY_SIZE(limits_tbl); i++) {
  572. getrlimit(limits_tbl[i].cmd, &limit);
  573. printf("%-32s(-%c) ", help, limit_chars[i]);
  574. printlim(opts, &limit, &limits_tbl[i]);
  575. help += strlen(help) + 1;
  576. }
  577. return EXIT_SUCCESS;
  578. }
  579. /* Second pass: set or print limits, in order */
  580. GETOPT_RESET();
  581. while (1) {
  582. char *val_str;
  583. int opt_char = getopt(argc, argv, ulimit_opt_string);
  584. if (opt_char == -1)
  585. break;
  586. if (opt_char == 'H')
  587. continue;
  588. if (opt_char == 'S')
  589. continue;
  590. //if (opt_char == 'a') - impossible
  591. if (opt_char == 1) /* if "ulimit NNN", -f is assumed */
  592. opt_char = 'f';
  593. i = strchrnul(limit_chars, opt_char) - limit_chars;
  594. //if (i >= ARRAY_SIZE(limits_tbl)) - bad option, impossible
  595. val_str = optarg;
  596. if (!val_str && argv[optind] && argv[optind][0] != '-')
  597. val_str = argv[optind++]; /* ++ skips NN in "-c NN" case */
  598. getrlimit(limits_tbl[i].cmd, &limit);
  599. if (!val_str) {
  600. if (opt_cnt > 1)
  601. printf("%-32s(-%c) ", nth_string(limits_help, i), limit_chars[i]);
  602. printlim(opts, &limit, &limits_tbl[i]);
  603. } else {
  604. rlim_t val = RLIM_INFINITY;
  605. if (strcmp(val_str, "unlimited") != 0) {
  606. if (sizeof(val) == sizeof(int))
  607. val = bb_strtou(val_str, NULL, 10);
  608. else if (sizeof(val) == sizeof(long))
  609. val = bb_strtoul(val_str, NULL, 10);
  610. else
  611. val = bb_strtoull(val_str, NULL, 10);
  612. if (errno) {
  613. bb_error_msg("invalid number '%s'", val_str);
  614. return EXIT_FAILURE;
  615. }
  616. val <<= limits_tbl[i].factor_shift;
  617. }
  618. //bb_error_msg("opt %c val_str:'%s' val:%lld", opt_char, val_str, (long long)val);
  619. /* from man bash: "If neither -H nor -S
  620. * is specified, both the soft and hard
  621. * limits are set. */
  622. if (opts & OPT_hard)
  623. limit.rlim_max = val;
  624. if (opts & OPT_soft)
  625. limit.rlim_cur = val;
  626. //bb_error_msg("setrlimit(%d, %lld, %lld)", limits_tbl[i].cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max);
  627. if (setrlimit(limits_tbl[i].cmd, &limit) < 0) {
  628. bb_simple_perror_msg("error setting limit");
  629. return EXIT_FAILURE;
  630. }
  631. }
  632. } /* while (there are options) */
  633. if (opt_cnt == 0) {
  634. /* "bare ulimit": treat it as if it was -f */
  635. getrlimit(RLIMIT_FSIZE, &limit);
  636. printlim(opts, &limit, &limits_tbl[LIMIT_F_IDX]);
  637. }
  638. return EXIT_SUCCESS;
  639. }