shell_common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. int FAST_FUNC is_well_formed_var_name(const char *s, char terminator)
  23. {
  24. if (!s || !(isalpha(*s) || *s == '_'))
  25. return 0;
  26. do
  27. s++;
  28. while (isalnum(*s) || *s == '_');
  29. return *s == terminator;
  30. }
  31. /* read builtin */
  32. //TODO: use more efficient setvar() which takes a pointer to malloced "VAR=VAL"
  33. //string. hush naturally has it, and ash has setvareq().
  34. //Here we can simply store "VAR=" at buffer start and store read data directly
  35. //after "=", then pass buffer to setvar() to consume.
  36. const char* FAST_FUNC
  37. shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
  38. char **argv,
  39. const char *ifs,
  40. int read_flags,
  41. const char *opt_n,
  42. const char *opt_p,
  43. const char *opt_t,
  44. const char *opt_u
  45. )
  46. {
  47. unsigned end_ms; /* -t TIMEOUT */
  48. int fd; /* -u FD */
  49. int nchars; /* -n NUM */
  50. char **pp;
  51. char *buffer;
  52. struct termios tty, old_tty;
  53. const char *retval;
  54. int bufpos; /* need to be able to hold -1 */
  55. int startword;
  56. smallint backslash;
  57. pp = argv;
  58. while (*pp) {
  59. if (!is_well_formed_var_name(*pp, '\0')) {
  60. /* Mimic bash message */
  61. bb_error_msg("read: '%s': not a valid identifier", *pp);
  62. return (const char *)(uintptr_t)1;
  63. }
  64. pp++;
  65. }
  66. nchars = 0; /* if != 0, -n is in effect */
  67. if (opt_n) {
  68. nchars = bb_strtou(opt_n, NULL, 10);
  69. if (nchars < 0 || errno)
  70. return "invalid count";
  71. /* note: "-n 0": off (bash 3.2 does this too) */
  72. }
  73. end_ms = 0;
  74. if (opt_t) {
  75. end_ms = bb_strtou(opt_t, NULL, 10);
  76. if (errno || end_ms > UINT_MAX / 2048)
  77. return "invalid timeout";
  78. end_ms *= 1000;
  79. #if 0 /* even bash has no -t N.NNN support */
  80. ts.tv_sec = bb_strtou(opt_t, &p, 10);
  81. ts.tv_usec = 0;
  82. /* EINVAL means number is ok, but not terminated by NUL */
  83. if (*p == '.' && errno == EINVAL) {
  84. char *p2;
  85. if (*++p) {
  86. int scale;
  87. ts.tv_usec = bb_strtou(p, &p2, 10);
  88. if (errno)
  89. return "invalid timeout";
  90. scale = p2 - p;
  91. /* normalize to usec */
  92. if (scale > 6)
  93. return "invalid timeout";
  94. while (scale++ < 6)
  95. ts.tv_usec *= 10;
  96. }
  97. } else if (ts.tv_sec < 0 || errno) {
  98. return "invalid timeout";
  99. }
  100. if (!(ts.tv_sec | ts.tv_usec)) { /* both are 0? */
  101. return "invalid timeout";
  102. }
  103. #endif /* if 0 */
  104. }
  105. fd = STDIN_FILENO;
  106. if (opt_u) {
  107. fd = bb_strtou(opt_u, NULL, 10);
  108. if (fd < 0 || errno)
  109. return "invalid file descriptor";
  110. }
  111. if (opt_p && isatty(fd)) {
  112. fputs(opt_p, stderr);
  113. fflush_all();
  114. }
  115. if (ifs == NULL)
  116. ifs = defifs;
  117. if (nchars || (read_flags & BUILTIN_READ_SILENT)) {
  118. tcgetattr(fd, &tty);
  119. old_tty = tty;
  120. if (nchars) {
  121. tty.c_lflag &= ~ICANON;
  122. tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
  123. }
  124. if (read_flags & BUILTIN_READ_SILENT) {
  125. tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
  126. }
  127. /* This forces execution of "restoring" tcgetattr later */
  128. read_flags |= BUILTIN_READ_SILENT;
  129. /* if tcgetattr failed, tcsetattr will fail too.
  130. * Ignoring, it's harmless. */
  131. tcsetattr(fd, TCSANOW, &tty);
  132. }
  133. retval = (const char *)(uintptr_t)0;
  134. startword = 1;
  135. backslash = 0;
  136. if (end_ms) /* NB: end_ms stays nonzero: */
  137. end_ms = ((unsigned)monotonic_ms() + end_ms) | 1;
  138. buffer = NULL;
  139. bufpos = 0;
  140. do {
  141. char c;
  142. if (end_ms) {
  143. int timeout;
  144. struct pollfd pfd[1];
  145. pfd[0].fd = fd;
  146. pfd[0].events = POLLIN;
  147. timeout = end_ms - (unsigned)monotonic_ms();
  148. if (timeout <= 0 /* already late? */
  149. || safe_poll(pfd, 1, timeout) != 1 /* no? wait... */
  150. ) { /* timed out! */
  151. retval = (const char *)(uintptr_t)1;
  152. goto ret;
  153. }
  154. }
  155. if ((bufpos & 0xff) == 0)
  156. buffer = xrealloc(buffer, bufpos + 0x100);
  157. if (nonblock_safe_read(fd, &buffer[bufpos], 1) != 1) {
  158. retval = (const char *)(uintptr_t)1;
  159. break;
  160. }
  161. c = buffer[bufpos];
  162. if (c == '\0')
  163. continue;
  164. if (backslash) {
  165. backslash = 0;
  166. if (c != '\n')
  167. goto put;
  168. continue;
  169. }
  170. if (!(read_flags & BUILTIN_READ_RAW) && c == '\\') {
  171. backslash = 1;
  172. continue;
  173. }
  174. if (c == '\n')
  175. break;
  176. /* $IFS splitting. NOT done if we run "read"
  177. * without variable names (bash compat).
  178. * Thus, "read" and "read REPLY" are not the same.
  179. */
  180. if (argv[0]) {
  181. /* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_05 */
  182. const char *is_ifs = strchr(ifs, c);
  183. if (startword && is_ifs) {
  184. if (isspace(c))
  185. continue;
  186. /* it is a non-space ifs char */
  187. startword--;
  188. if (startword == 1) /* first one? */
  189. continue; /* yes, it is not next word yet */
  190. }
  191. startword = 0;
  192. if (argv[1] != NULL && is_ifs) {
  193. buffer[bufpos] = '\0';
  194. bufpos = 0;
  195. setvar(*argv, buffer);
  196. argv++;
  197. /* can we skip one non-space ifs char? (2: yes) */
  198. startword = isspace(c) ? 2 : 1;
  199. continue;
  200. }
  201. }
  202. put:
  203. bufpos++;
  204. } while (--nchars);
  205. if (argv[0]) {
  206. /* Remove trailing space $IFS chars */
  207. while (--bufpos >= 0 && isspace(buffer[bufpos]) && strchr(ifs, buffer[bufpos]) != NULL)
  208. continue;
  209. buffer[bufpos + 1] = '\0';
  210. /* Use the remainder as a value for the next variable */
  211. setvar(*argv, buffer);
  212. /* Set the rest to "" */
  213. while (*++argv)
  214. setvar(*argv, "");
  215. } else {
  216. /* Note: no $IFS removal */
  217. buffer[bufpos] = '\0';
  218. setvar("REPLY", buffer);
  219. }
  220. ret:
  221. free(buffer);
  222. if (read_flags & BUILTIN_READ_SILENT)
  223. tcsetattr(fd, TCSANOW, &old_tty);
  224. return retval;
  225. }
  226. /* ulimit builtin */
  227. struct limits {
  228. uint8_t cmd; /* RLIMIT_xxx fit into it */
  229. uint8_t factor_shift; /* shift by to get rlim_{cur,max} values */
  230. char option;
  231. const char *name;
  232. };
  233. static const struct limits limits_tbl[] = {
  234. #ifdef RLIMIT_FSIZE
  235. { RLIMIT_FSIZE, 9, 'f', "file size (blocks)" },
  236. #endif
  237. #ifdef RLIMIT_CPU
  238. { RLIMIT_CPU, 0, 't', "cpu time (seconds)" },
  239. #endif
  240. #ifdef RLIMIT_DATA
  241. { RLIMIT_DATA, 10, 'd', "data seg size (kb)" },
  242. #endif
  243. #ifdef RLIMIT_STACK
  244. { RLIMIT_STACK, 10, 's', "stack size (kb)" },
  245. #endif
  246. #ifdef RLIMIT_CORE
  247. { RLIMIT_CORE, 9, 'c', "core file size (blocks)" },
  248. #endif
  249. #ifdef RLIMIT_RSS
  250. { RLIMIT_RSS, 10, 'm', "resident set size (kb)" },
  251. #endif
  252. #ifdef RLIMIT_MEMLOCK
  253. { RLIMIT_MEMLOCK, 10, 'l', "locked memory (kb)" },
  254. #endif
  255. #ifdef RLIMIT_NPROC
  256. { RLIMIT_NPROC, 0, 'p', "processes" },
  257. #endif
  258. #ifdef RLIMIT_NOFILE
  259. { RLIMIT_NOFILE, 0, 'n', "file descriptors" },
  260. #endif
  261. #ifdef RLIMIT_AS
  262. { RLIMIT_AS, 10, 'v', "address space (kb)" },
  263. #endif
  264. #ifdef RLIMIT_LOCKS
  265. { RLIMIT_LOCKS, 0, 'w', "locks" },
  266. #endif
  267. };
  268. enum {
  269. OPT_hard = (1 << 0),
  270. OPT_soft = (1 << 1),
  271. };
  272. /* "-": treat args as parameters of option with ASCII code 1 */
  273. static const char ulimit_opt_string[] = "-HSa"
  274. #ifdef RLIMIT_FSIZE
  275. "f::"
  276. #endif
  277. #ifdef RLIMIT_CPU
  278. "t::"
  279. #endif
  280. #ifdef RLIMIT_DATA
  281. "d::"
  282. #endif
  283. #ifdef RLIMIT_STACK
  284. "s::"
  285. #endif
  286. #ifdef RLIMIT_CORE
  287. "c::"
  288. #endif
  289. #ifdef RLIMIT_RSS
  290. "m::"
  291. #endif
  292. #ifdef RLIMIT_MEMLOCK
  293. "l::"
  294. #endif
  295. #ifdef RLIMIT_NPROC
  296. "p::"
  297. #endif
  298. #ifdef RLIMIT_NOFILE
  299. "n::"
  300. #endif
  301. #ifdef RLIMIT_AS
  302. "v::"
  303. #endif
  304. #ifdef RLIMIT_LOCKS
  305. "w::"
  306. #endif
  307. ;
  308. static void printlim(unsigned opts, const struct rlimit *limit,
  309. const struct limits *l)
  310. {
  311. rlim_t val;
  312. val = limit->rlim_max;
  313. if (!(opts & OPT_hard))
  314. val = limit->rlim_cur;
  315. if (val == RLIM_INFINITY)
  316. printf("unlimited\n");
  317. else {
  318. val >>= l->factor_shift;
  319. printf("%llu\n", (long long) val);
  320. }
  321. }
  322. int FAST_FUNC
  323. shell_builtin_ulimit(char **argv)
  324. {
  325. unsigned opts;
  326. unsigned argc;
  327. /* We can't use getopt32: need to handle commands like
  328. * ulimit 123 -c2 -l 456
  329. */
  330. /* In case getopt was already called:
  331. * reset the libc getopt() function, which keeps internal state.
  332. */
  333. #ifdef __GLIBC__
  334. optind = 0;
  335. #else /* BSD style */
  336. optind = 1;
  337. /* optreset = 1; */
  338. #endif
  339. /* optarg = NULL; opterr = 0; optopt = 0; - do we need this?? */
  340. argc = 1;
  341. while (argv[argc])
  342. argc++;
  343. opts = 0;
  344. while (1) {
  345. struct rlimit limit;
  346. const struct limits *l;
  347. int opt_char = getopt(argc, argv, ulimit_opt_string);
  348. if (opt_char == -1)
  349. break;
  350. if (opt_char == 'H') {
  351. opts |= OPT_hard;
  352. continue;
  353. }
  354. if (opt_char == 'S') {
  355. opts |= OPT_soft;
  356. continue;
  357. }
  358. if (opt_char == 'a') {
  359. for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
  360. getrlimit(l->cmd, &limit);
  361. printf("-%c: %-30s ", l->option, l->name);
  362. printlim(opts, &limit, l);
  363. }
  364. continue;
  365. }
  366. if (opt_char == 1)
  367. opt_char = 'f';
  368. for (l = limits_tbl; l != &limits_tbl[ARRAY_SIZE(limits_tbl)]; l++) {
  369. if (opt_char == l->option) {
  370. char *val_str;
  371. getrlimit(l->cmd, &limit);
  372. val_str = optarg;
  373. if (!val_str && argv[optind] && argv[optind][0] != '-')
  374. val_str = argv[optind++]; /* ++ skips NN in "-c NN" case */
  375. if (val_str) {
  376. rlim_t val;
  377. if (strcmp(val_str, "unlimited") == 0)
  378. val = RLIM_INFINITY;
  379. else {
  380. if (sizeof(val) == sizeof(int))
  381. val = bb_strtou(val_str, NULL, 10);
  382. else if (sizeof(val) == sizeof(long))
  383. val = bb_strtoul(val_str, NULL, 10);
  384. else
  385. val = bb_strtoull(val_str, NULL, 10);
  386. if (errno) {
  387. bb_error_msg("invalid number '%s'", val_str);
  388. return EXIT_FAILURE;
  389. }
  390. val <<= l->factor_shift;
  391. }
  392. //bb_error_msg("opt %c val_str:'%s' val:%lld", opt_char, val_str, (long long)val);
  393. /* from man bash: "If neither -H nor -S
  394. * is specified, both the soft and hard
  395. * limits are set. */
  396. if (!opts)
  397. opts = OPT_hard + OPT_soft;
  398. if (opts & OPT_hard)
  399. limit.rlim_max = val;
  400. if (opts & OPT_soft)
  401. limit.rlim_cur = val;
  402. //bb_error_msg("setrlimit(%d, %lld, %lld)", l->cmd, (long long)limit.rlim_cur, (long long)limit.rlim_max);
  403. if (setrlimit(l->cmd, &limit) < 0) {
  404. bb_perror_msg("error setting limit");
  405. return EXIT_FAILURE;
  406. }
  407. } else {
  408. printlim(opts, &limit, l);
  409. }
  410. break;
  411. }
  412. } /* for (every possible opt) */
  413. if (l == &limits_tbl[ARRAY_SIZE(limits_tbl)]) {
  414. /* bad option. getopt already complained. */
  415. break;
  416. }
  417. } /* while (there are options) */
  418. return 0;
  419. }