3
0

shell_common.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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': not a valid identifier", *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 (c == '\0')
  194. continue;
  195. if (!(read_flags & BUILTIN_READ_RAW)) {
  196. if (backslash) {
  197. backslash = 0;
  198. if (c != '\n')
  199. goto put;
  200. continue;
  201. }
  202. if (c == '\\') {
  203. backslash = 1;
  204. continue;
  205. }
  206. }
  207. if (c == delim) /* '\n' or -d CHAR */
  208. break;
  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 (!params->opt_d && 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. static const struct limits limits_tbl[] = {
  300. { RLIMIT_CORE, 9, }, // -c
  301. { RLIMIT_DATA, 10, }, // -d
  302. { RLIMIT_NICE, 0, }, // -e
  303. { RLIMIT_FSIZE, 9, }, // -f
  304. #define LIMIT_F_IDX 3
  305. #ifdef RLIMIT_SIGPENDING
  306. { RLIMIT_SIGPENDING, 0, }, // -i
  307. #endif
  308. #ifdef RLIMIT_MEMLOCK
  309. { RLIMIT_MEMLOCK, 10, }, // -l
  310. #endif
  311. #ifdef RLIMIT_RSS
  312. { RLIMIT_RSS, 10, }, // -m
  313. #endif
  314. #ifdef RLIMIT_NOFILE
  315. { RLIMIT_NOFILE, 0, }, // -n
  316. #endif
  317. #ifdef RLIMIT_MSGQUEUE
  318. { RLIMIT_MSGQUEUE, 0, }, // -q
  319. #endif
  320. #ifdef RLIMIT_RTPRIO
  321. { RLIMIT_RTPRIO, 0, }, // -r
  322. #endif
  323. #ifdef RLIMIT_STACK
  324. { RLIMIT_STACK, 10, }, // -s
  325. #endif
  326. #ifdef RLIMIT_CPU
  327. { RLIMIT_CPU, 0, }, // -t
  328. #endif
  329. #ifdef RLIMIT_NPROC
  330. { RLIMIT_NPROC, 0, }, // -u
  331. #endif
  332. #ifdef RLIMIT_AS
  333. { RLIMIT_AS, 10, }, // -v
  334. #endif
  335. #ifdef RLIMIT_LOCKS
  336. { RLIMIT_LOCKS, 0, }, // -x
  337. #endif
  338. };
  339. // bash also shows:
  340. //pipe size (512 bytes, -p) 8
  341. static const char limits_help[] ALIGN1 =
  342. "core file size (blocks)" // -c
  343. "\0""data seg size (kb)" // -d
  344. "\0""scheduling priority" // -e
  345. "\0""file size (blocks)" // -f
  346. #ifdef RLIMIT_SIGPENDING
  347. "\0""pending signals" // -i
  348. #endif
  349. #ifdef RLIMIT_MEMLOCK
  350. "\0""max locked memory (kb)" // -l
  351. #endif
  352. #ifdef RLIMIT_RSS
  353. "\0""max memory size (kb)" // -m
  354. #endif
  355. #ifdef RLIMIT_NOFILE
  356. "\0""open files" // -n
  357. #endif
  358. #ifdef RLIMIT_MSGQUEUE
  359. "\0""POSIX message queues (bytes)" // -q
  360. #endif
  361. #ifdef RLIMIT_RTPRIO
  362. "\0""real-time priority" // -r
  363. #endif
  364. #ifdef RLIMIT_STACK
  365. "\0""stack size (kb)" // -s
  366. #endif
  367. #ifdef RLIMIT_CPU
  368. "\0""cpu time (seconds)" // -t
  369. #endif
  370. #ifdef RLIMIT_NPROC
  371. "\0""max user processes" // -u
  372. #endif
  373. #ifdef RLIMIT_AS
  374. "\0""virtual memory (kb)" // -v
  375. #endif
  376. #ifdef RLIMIT_LOCKS
  377. "\0""file locks" // -x
  378. #endif
  379. ;
  380. static const char limit_chars[] ALIGN1 =
  381. "c"
  382. "d"
  383. "e"
  384. "f"
  385. #ifdef RLIMIT_SIGPENDING
  386. "i"
  387. #endif
  388. #ifdef RLIMIT_MEMLOCK
  389. "l"
  390. #endif
  391. #ifdef RLIMIT_RSS
  392. "m"
  393. #endif
  394. #ifdef RLIMIT_NOFILE
  395. "n"
  396. #endif
  397. #ifdef RLIMIT_MSGQUEUE
  398. "q"
  399. #endif
  400. #ifdef RLIMIT_RTPRIO
  401. "r"
  402. #endif
  403. #ifdef RLIMIT_STACK
  404. "s"
  405. #endif
  406. #ifdef RLIMIT_CPU
  407. "t"
  408. #endif
  409. #ifdef RLIMIT_NPROC
  410. "u"
  411. #endif
  412. #ifdef RLIMIT_AS
  413. "v"
  414. #endif
  415. #ifdef RLIMIT_LOCKS
  416. "x"
  417. #endif
  418. ;
  419. /* "-": treat args as parameters of option with ASCII code 1 */
  420. static const char ulimit_opt_string[] ALIGN1 = "-HSa"
  421. "c::"
  422. "d::"
  423. "e::"
  424. "f::"
  425. #ifdef RLIMIT_SIGPENDING
  426. "i::"
  427. #endif
  428. #ifdef RLIMIT_MEMLOCK
  429. "l::"
  430. #endif
  431. #ifdef RLIMIT_RSS
  432. "m::"
  433. #endif
  434. #ifdef RLIMIT_NOFILE
  435. "n::"
  436. #endif
  437. #ifdef RLIMIT_MSGQUEUE
  438. "q::"
  439. #endif
  440. #ifdef RLIMIT_RTPRIO
  441. "r::"
  442. #endif
  443. #ifdef RLIMIT_STACK
  444. "s::"
  445. #endif
  446. #ifdef RLIMIT_CPU
  447. "t::"
  448. #endif
  449. #ifdef RLIMIT_NPROC
  450. "u::"
  451. #endif
  452. #ifdef RLIMIT_AS
  453. "v::"
  454. #endif
  455. #ifdef RLIMIT_LOCKS
  456. "x::"
  457. #endif
  458. ;
  459. enum {
  460. OPT_hard = (1 << 0),
  461. OPT_soft = (1 << 1),
  462. OPT_all = (1 << 2),
  463. };
  464. static void printlim(unsigned opts, const struct rlimit *limit,
  465. const struct limits *l)
  466. {
  467. rlim_t val;
  468. val = limit->rlim_max;
  469. if (opts & OPT_soft)
  470. val = limit->rlim_cur;
  471. if (val == RLIM_INFINITY)
  472. puts("unlimited");
  473. else {
  474. val >>= l->factor_shift;
  475. printf("%llu\n", (long long) val);
  476. }
  477. }
  478. int FAST_FUNC
  479. shell_builtin_ulimit(char **argv)
  480. {
  481. struct rlimit limit;
  482. unsigned opt_cnt;
  483. unsigned opts;
  484. unsigned argc;
  485. unsigned i;
  486. /* We can't use getopt32: need to handle commands like
  487. * ulimit 123 -c2 -l 456
  488. */
  489. /* In case getopt() was already called:
  490. * reset libc getopt() internal state.
  491. */
  492. GETOPT_RESET();
  493. // bash 4.4.23:
  494. //
  495. // -H and/or -S change meaning even of options *before* them: ulimit -f 2000 -H
  496. // sets hard limit, ulimit -a -H prints hard limits.
  497. //
  498. // -a is equivalent for requesting all limits to be shown.
  499. //
  500. // If -a is specified, attempts to set limits are ignored:
  501. // ulimit -m 1000; ulimit -m 2000 -a
  502. // shows 1000, not 2000. HOWEVER, *implicit* -f form "ulimit 2000 -a"
  503. // DOES set -f limit [we don't implement this quirk], "ulimit -a 2000" does not.
  504. // Options are still parsed: ulimit -az complains about unknown -z opt.
  505. //
  506. // -a is not cumulative: "ulimit -a -a" = "ulimit -a -f -m" = "ulimit -a"
  507. //
  508. // -HSa can be combined in one argument and with one other option (example: -Sm),
  509. // but other options can't: limit value is an optional argument,
  510. // thus "-mf" means "-m f", f is the parameter of -m.
  511. //
  512. // Limit can be set and then printed: ulimit -m 2000 -m
  513. // If set more than once, they are set and printed in order:
  514. // try ulimit -m -m 1000 -m -m 2000 -m -m 3000 -m
  515. //
  516. // Limits are shown in the order of options given:
  517. // ulimit -m -f is not the same as ulimit -f -m.
  518. //
  519. // If both -S and -H are given, show soft limit.
  520. //
  521. // Short printout (limit value only) is printed only if just one option
  522. // is given: ulimit -m. ulimit -f -m prints verbose lines.
  523. // ulimit -f -f prints same verbose line twice.
  524. // ulimit -m 10000 -f prints verbose line for -f.
  525. argc = string_array_len(argv);
  526. /* First pass over options: detect -H/-S/-a status,
  527. * and "bare ulimit" and "only one option" cases
  528. * by counting other opts.
  529. */
  530. opt_cnt = 0;
  531. opts = 0;
  532. while (1) {
  533. int opt_char = getopt(argc, argv, ulimit_opt_string);
  534. if (opt_char == -1)
  535. break;
  536. if (opt_char == 'H') {
  537. opts |= OPT_hard;
  538. continue;
  539. }
  540. if (opt_char == 'S') {
  541. opts |= OPT_soft;
  542. continue;
  543. }
  544. if (opt_char == 'a') {
  545. opts |= OPT_all;
  546. continue;
  547. }
  548. if (opt_char == '?') {
  549. /* bad option. getopt already complained. */
  550. return EXIT_FAILURE;
  551. }
  552. opt_cnt++;
  553. } /* while (there are options) */
  554. if (!(opts & (OPT_hard | OPT_soft)))
  555. opts |= (OPT_hard | OPT_soft);
  556. if (opts & OPT_all) {
  557. const char *help = limits_help;
  558. for (i = 0; i < ARRAY_SIZE(limits_tbl); i++) {
  559. getrlimit(limits_tbl[i].cmd, &limit);
  560. printf("%-32s(-%c) ", help, limit_chars[i]);
  561. printlim(opts, &limit, &limits_tbl[i]);
  562. help += strlen(help) + 1;
  563. }
  564. return EXIT_SUCCESS;
  565. }
  566. /* Second pass: set or print limits, in order */
  567. GETOPT_RESET();
  568. while (1) {
  569. char *val_str;
  570. int opt_char = getopt(argc, argv, ulimit_opt_string);
  571. if (opt_char == -1)
  572. break;
  573. if (opt_char == 'H')
  574. continue;
  575. if (opt_char == 'S')
  576. continue;
  577. //if (opt_char == 'a') - impossible
  578. if (opt_char == 1) /* if "ulimit NNN", -f is assumed */
  579. opt_char = 'f';
  580. i = strchrnul(limit_chars, opt_char) - limit_chars;
  581. //if (i >= ARRAY_SIZE(limits_tbl)) - bad option, impossible
  582. val_str = optarg;
  583. if (!val_str && argv[optind] && argv[optind][0] != '-')
  584. val_str = argv[optind++]; /* ++ skips NN in "-c NN" case */
  585. getrlimit(limits_tbl[i].cmd, &limit);
  586. if (!val_str) {
  587. if (opt_cnt > 1)
  588. printf("%-32s(-%c) ", nth_string(limits_help, i), limit_chars[i]);
  589. printlim(opts, &limit, &limits_tbl[i]);
  590. } else {
  591. rlim_t val = RLIM_INFINITY;
  592. if (strcmp(val_str, "unlimited") != 0) {
  593. if (sizeof(val) == sizeof(int))
  594. val = bb_strtou(val_str, NULL, 10);
  595. else if (sizeof(val) == sizeof(long))
  596. val = bb_strtoul(val_str, NULL, 10);
  597. else
  598. val = bb_strtoull(val_str, NULL, 10);
  599. if (errno) {
  600. bb_error_msg("invalid number '%s'", val_str);
  601. return EXIT_FAILURE;
  602. }
  603. val <<= limits_tbl[i].factor_shift;
  604. }
  605. //bb_error_msg("opt %c val_str:'%s' val:%lld", opt_char, val_str, (long long)val);
  606. /* from man bash: "If neither -H nor -S
  607. * is specified, both the soft and hard
  608. * limits are set. */
  609. if (opts & OPT_hard)
  610. limit.rlim_max = val;
  611. if (opts & OPT_soft)
  612. limit.rlim_cur = val;
  613. //bb_error_msg("setrlimit(%d, %lld, %lld)", limits_tbl[i].cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max);
  614. if (setrlimit(limits_tbl[i].cmd, &limit) < 0) {
  615. bb_simple_perror_msg("error setting limit");
  616. return EXIT_FAILURE;
  617. }
  618. }
  619. } /* while (there are options) */
  620. if (opt_cnt == 0) {
  621. /* "bare ulimit": treat it as if it was -f */
  622. getrlimit(limits_tbl[LIMIT_F_IDX].cmd, &limit);
  623. printlim(opts, &limit, &limits_tbl[LIMIT_F_IDX]);
  624. }
  625. return EXIT_SUCCESS;
  626. }