appletlib.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) tons of folks. Tracking down who wrote what
  6. * isn't something I'm going to worry about... If you wrote something
  7. * here, please feel free to acknowledge your work.
  8. *
  9. * Based in part on code from sash, Copyright (c) 1999 by David I. Bell
  10. * Permission has been granted to redistribute this code under GPL.
  11. *
  12. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  13. */
  14. /* We are trying to not use printf, this benefits the case when selected
  15. * applets are really simple. Example:
  16. *
  17. * $ ./busybox
  18. * ...
  19. * Currently defined functions:
  20. * basename, false, true
  21. *
  22. * $ size busybox
  23. * text data bss dec hex filename
  24. * 4473 52 72 4597 11f5 busybox
  25. *
  26. * FEATURE_INSTALLER or FEATURE_SUID will still link printf routines in. :(
  27. */
  28. #include "busybox.h"
  29. #if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
  30. || defined(__APPLE__) \
  31. )
  32. # include <malloc.h> /* for mallopt */
  33. #endif
  34. /* Declare <applet>_main() */
  35. #define PROTOTYPES
  36. #include "applets.h"
  37. #undef PROTOTYPES
  38. /* Include generated applet names, pointers to <applet>_main, etc */
  39. #include "applet_tables.h"
  40. /* ...and if applet_tables generator says we have only one applet... */
  41. #ifdef SINGLE_APPLET_MAIN
  42. # undef ENABLE_FEATURE_INDIVIDUAL
  43. # define ENABLE_FEATURE_INDIVIDUAL 1
  44. # undef IF_FEATURE_INDIVIDUAL
  45. # define IF_FEATURE_INDIVIDUAL(...) __VA_ARGS__
  46. #endif
  47. #include "usage_compressed.h"
  48. #if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE
  49. static const char usage_messages[] ALIGN1 = UNPACKED_USAGE;
  50. #else
  51. # define usage_messages 0
  52. #endif
  53. #if ENABLE_FEATURE_COMPRESS_USAGE
  54. static const char packed_usage[] ALIGN1 = { PACKED_USAGE };
  55. # include "bb_archive.h"
  56. static const char *unpack_usage_messages(void)
  57. {
  58. char *outbuf = NULL;
  59. bunzip_data *bd;
  60. int i;
  61. i = start_bunzip(&bd,
  62. /* src_fd: */ -1,
  63. /* inbuf: */ packed_usage,
  64. /* len: */ sizeof(packed_usage));
  65. /* read_bunzip can longjmp to start_bunzip, and ultimately
  66. * end up here with i != 0 on read data errors! Not trivial */
  67. if (!i) {
  68. /* Cannot use xmalloc: will leak bd in NOFORK case! */
  69. outbuf = malloc_or_warn(sizeof(UNPACKED_USAGE));
  70. if (outbuf)
  71. read_bunzip(bd, outbuf, sizeof(UNPACKED_USAGE));
  72. }
  73. dealloc_bunzip(bd);
  74. return outbuf;
  75. }
  76. # define dealloc_usage_messages(s) free(s)
  77. #else
  78. # define unpack_usage_messages() usage_messages
  79. # define dealloc_usage_messages(s) ((void)(s))
  80. #endif /* FEATURE_COMPRESS_USAGE */
  81. void FAST_FUNC bb_show_usage(void)
  82. {
  83. if (ENABLE_SHOW_USAGE) {
  84. #ifdef SINGLE_APPLET_STR
  85. /* Imagine that this applet is "true". Dont suck in printf! */
  86. const char *usage_string = unpack_usage_messages();
  87. if (*usage_string == '\b') {
  88. full_write2_str("No help available.\n\n");
  89. } else {
  90. full_write2_str("Usage: "SINGLE_APPLET_STR" ");
  91. full_write2_str(usage_string);
  92. full_write2_str("\n\n");
  93. }
  94. if (ENABLE_FEATURE_CLEAN_UP)
  95. dealloc_usage_messages((char*)usage_string);
  96. #else
  97. const char *p;
  98. const char *usage_string = p = unpack_usage_messages();
  99. int ap = find_applet_by_name(applet_name);
  100. if (ap < 0) /* never happens, paranoia */
  101. xfunc_die();
  102. while (ap) {
  103. while (*p++) continue;
  104. ap--;
  105. }
  106. full_write2_str(bb_banner);
  107. full_write2_str(" multi-call binary.\n");
  108. if (*p == '\b')
  109. full_write2_str("\nNo help available.\n\n");
  110. else {
  111. full_write2_str("\nUsage: ");
  112. full_write2_str(applet_name);
  113. full_write2_str(" ");
  114. full_write2_str(p);
  115. full_write2_str("\n\n");
  116. }
  117. if (ENABLE_FEATURE_CLEAN_UP)
  118. dealloc_usage_messages((char*)usage_string);
  119. #endif
  120. }
  121. xfunc_die();
  122. }
  123. #if NUM_APPLETS > 8
  124. static int applet_name_compare(const void *name, const void *idx)
  125. {
  126. int i = (int)(ptrdiff_t)idx - 1;
  127. return strcmp(name, APPLET_NAME(i));
  128. }
  129. #endif
  130. int FAST_FUNC find_applet_by_name(const char *name)
  131. {
  132. #if NUM_APPLETS > 8
  133. /* Do a binary search to find the applet entry given the name. */
  134. const char *p;
  135. p = bsearch(name, (void*)(ptrdiff_t)1, ARRAY_SIZE(applet_main), 1, applet_name_compare);
  136. /*
  137. * if (!p) return -1;
  138. * ^^^^^^^^^^^^^^^^^^ the code below will do this if p == NULL :)
  139. */
  140. return (int)(ptrdiff_t)p - 1;
  141. #else
  142. /* A version which does not pull in bsearch */
  143. int i = 0;
  144. const char *p = applet_names;
  145. while (i < NUM_APPLETS) {
  146. if (strcmp(name, p) == 0)
  147. return i;
  148. p += strlen(p) + 1;
  149. i++;
  150. }
  151. return -1;
  152. #endif
  153. }
  154. void lbb_prepare(const char *applet
  155. IF_FEATURE_INDIVIDUAL(, char **argv))
  156. MAIN_EXTERNALLY_VISIBLE;
  157. void lbb_prepare(const char *applet
  158. IF_FEATURE_INDIVIDUAL(, char **argv))
  159. {
  160. #ifdef __GLIBC__
  161. (*(int **)&bb_errno) = __errno_location();
  162. barrier();
  163. #endif
  164. applet_name = applet;
  165. if (ENABLE_LOCALE_SUPPORT)
  166. setlocale(LC_ALL, "");
  167. #if ENABLE_FEATURE_INDIVIDUAL
  168. /* Redundant for busybox (run_applet_and_exit covers that case)
  169. * but needed for "individual applet" mode */
  170. if (argv[1]
  171. && !argv[2]
  172. && strcmp(argv[1], "--help") == 0
  173. && strncmp(applet, "busybox", 7) != 0
  174. ) {
  175. /* Special case. POSIX says "test --help"
  176. * should be no different from e.g. "test --foo". */
  177. if (!ENABLE_TEST || strcmp(applet_name, "test") != 0)
  178. bb_show_usage();
  179. }
  180. #endif
  181. }
  182. /* The code below can well be in applets/applets.c, as it is used only
  183. * for busybox binary, not "individual" binaries.
  184. * However, keeping it here and linking it into libbusybox.so
  185. * (together with remaining tiny applets/applets.o)
  186. * makes it possible to avoid --whole-archive at link time.
  187. * This makes (shared busybox) + libbusybox smaller.
  188. * (--gc-sections would be even better....)
  189. */
  190. const char *applet_name;
  191. #if !BB_MMU
  192. bool re_execed;
  193. #endif
  194. /* If not built as a single-applet executable... */
  195. #if !defined(SINGLE_APPLET_MAIN)
  196. IF_FEATURE_SUID(static uid_t ruid;) /* real uid */
  197. # if ENABLE_FEATURE_SUID_CONFIG
  198. static struct suid_config_t {
  199. /* next ptr must be first: this struct needs to be llist-compatible */
  200. struct suid_config_t *m_next;
  201. struct bb_uidgid_t m_ugid;
  202. int m_applet;
  203. mode_t m_mode;
  204. } *suid_config;
  205. static bool suid_cfg_readable;
  206. /* check if u is member of group g */
  207. static int ingroup(uid_t u, gid_t g)
  208. {
  209. struct group *grp = getgrgid(g);
  210. if (grp) {
  211. char **mem;
  212. for (mem = grp->gr_mem; *mem; mem++) {
  213. struct passwd *pwd = getpwnam(*mem);
  214. if (pwd && (pwd->pw_uid == u))
  215. return 1;
  216. }
  217. }
  218. return 0;
  219. }
  220. /* libbb candidate */
  221. static char *get_trimmed_slice(char *s, char *e)
  222. {
  223. /* First, consider the value at e to be nul and back up until we
  224. * reach a non-space char. Set the char after that (possibly at
  225. * the original e) to nul. */
  226. while (e-- > s) {
  227. if (!isspace(*e)) {
  228. break;
  229. }
  230. }
  231. e[1] = '\0';
  232. /* Next, advance past all leading space and return a ptr to the
  233. * first non-space char; possibly the terminating nul. */
  234. return skip_whitespace(s);
  235. }
  236. static void parse_config_file(void)
  237. {
  238. /* Don't depend on the tools to combine strings. */
  239. static const char config_file[] ALIGN1 = "/etc/busybox.conf";
  240. struct suid_config_t *sct_head;
  241. int applet_no;
  242. FILE *f;
  243. const char *errmsg;
  244. unsigned lc;
  245. smallint section;
  246. struct stat st;
  247. ruid = getuid();
  248. if (ruid == 0) /* run by root - don't need to even read config file */
  249. return;
  250. if ((stat(config_file, &st) != 0) /* No config file? */
  251. || !S_ISREG(st.st_mode) /* Not a regular file? */
  252. || (st.st_uid != 0) /* Not owned by root? */
  253. || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
  254. || !(f = fopen_for_read(config_file)) /* Cannot open? */
  255. ) {
  256. return;
  257. }
  258. suid_cfg_readable = 1;
  259. sct_head = NULL;
  260. section = lc = 0;
  261. while (1) {
  262. char buffer[256];
  263. char *s;
  264. if (!fgets(buffer, sizeof(buffer), f)) { /* Are we done? */
  265. // Looks like bloat
  266. //if (ferror(f)) { /* Make sure it wasn't a read error. */
  267. // errmsg = "reading";
  268. // goto pe_label;
  269. //}
  270. fclose(f);
  271. suid_config = sct_head; /* Success, so set the pointer. */
  272. return;
  273. }
  274. s = buffer;
  275. lc++; /* Got a (partial) line. */
  276. /* If a line is too long for our buffer, we consider it an error.
  277. * The following test does mistreat one corner case though.
  278. * If the final line of the file does not end with a newline and
  279. * yet exactly fills the buffer, it will be treated as too long
  280. * even though there isn't really a problem. But it isn't really
  281. * worth adding code to deal with such an unlikely situation, and
  282. * we do err on the side of caution. Besides, the line would be
  283. * too long if it did end with a newline. */
  284. if (!strchr(s, '\n') && !feof(f)) {
  285. errmsg = "line too long";
  286. goto pe_label;
  287. }
  288. /* Trim leading and trailing whitespace, ignoring comments, and
  289. * check if the resulting string is empty. */
  290. s = get_trimmed_slice(s, strchrnul(s, '#'));
  291. if (!*s) {
  292. continue;
  293. }
  294. /* Check for a section header. */
  295. if (*s == '[') {
  296. /* Unlike the old code, we ignore leading and trailing
  297. * whitespace for the section name. We also require that
  298. * there are no stray characters after the closing bracket. */
  299. char *e = strchr(s, ']');
  300. if (!e /* Missing right bracket? */
  301. || e[1] /* Trailing characters? */
  302. || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
  303. ) {
  304. errmsg = "section header";
  305. goto pe_label;
  306. }
  307. /* Right now we only have one section so just check it.
  308. * If more sections are added in the future, please don't
  309. * resort to cascading ifs with multiple strcasecmp calls.
  310. * That kind of bloated code is all too common. A loop
  311. * and a string table would be a better choice unless the
  312. * number of sections is very small. */
  313. if (strcasecmp(s, "SUID") == 0) {
  314. section = 1;
  315. continue;
  316. }
  317. section = -1; /* Unknown section so set to skip. */
  318. continue;
  319. }
  320. /* Process sections. */
  321. if (section == 1) { /* SUID */
  322. /* Since we trimmed leading and trailing space above, we're
  323. * now looking for strings of the form
  324. * <key>[::space::]*=[::space::]*<value>
  325. * where both key and value could contain inner whitespace. */
  326. /* First get the key (an applet name in our case). */
  327. char *e = strchr(s, '=');
  328. if (e) {
  329. s = get_trimmed_slice(s, e);
  330. }
  331. if (!e || !*s) { /* Missing '=' or empty key. */
  332. errmsg = "keyword";
  333. goto pe_label;
  334. }
  335. /* Ok, we have an applet name. Process the rhs if this
  336. * applet is currently built in and ignore it otherwise.
  337. * Note: this can hide config file bugs which only pop
  338. * up when the busybox configuration is changed. */
  339. applet_no = find_applet_by_name(s);
  340. if (applet_no >= 0) {
  341. unsigned i;
  342. struct suid_config_t *sct;
  343. /* Note: We currently don't check for duplicates!
  344. * The last config line for each applet will be the
  345. * one used since we insert at the head of the list.
  346. * I suppose this could be considered a feature. */
  347. sct = xzalloc(sizeof(*sct));
  348. sct->m_applet = applet_no;
  349. /*sct->m_mode = 0;*/
  350. sct->m_next = sct_head;
  351. sct_head = sct;
  352. /* Get the specified mode. */
  353. e = skip_whitespace(e+1);
  354. for (i = 0; i < 3; i++) {
  355. /* There are 4 chars for each of user/group/other.
  356. * "x-xx" instead of "x-" are to make
  357. * "idx > 3" check catch invalid chars.
  358. */
  359. static const char mode_chars[] ALIGN1 = "Ssx-" "Ssx-" "x-xx";
  360. static const unsigned short mode_mask[] ALIGN2 = {
  361. S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* Ssx- */
  362. S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* Ssx- */
  363. S_IXOTH, 0 /* x- */
  364. };
  365. const char *q = strchrnul(mode_chars + 4*i, *e);
  366. unsigned idx = q - (mode_chars + 4*i);
  367. if (idx > 3) {
  368. errmsg = "mode";
  369. goto pe_label;
  370. }
  371. sct->m_mode |= mode_mask[q - mode_chars];
  372. e++;
  373. }
  374. /* Now get the user/group info. */
  375. s = skip_whitespace(e);
  376. /* Default is 0.0, else parse USER.GROUP: */
  377. if (*s) {
  378. /* We require whitespace between mode and USER.GROUP */
  379. if ((s == e) || !(e = strchr(s, '.'))) {
  380. errmsg = "uid.gid";
  381. goto pe_label;
  382. }
  383. *e = ':'; /* get_uidgid needs USER:GROUP syntax */
  384. if (get_uidgid(&sct->m_ugid, s, /*allow_numeric:*/ 1) == 0) {
  385. errmsg = "unknown user/group";
  386. goto pe_label;
  387. }
  388. }
  389. }
  390. continue;
  391. }
  392. /* Unknown sections are ignored. */
  393. /* Encountering configuration lines prior to seeing a
  394. * section header is treated as an error. This is how
  395. * the old code worked, but it may not be desirable.
  396. * We may want to simply ignore such lines in case they
  397. * are used in some future version of busybox. */
  398. if (!section) {
  399. errmsg = "keyword outside section";
  400. goto pe_label;
  401. }
  402. } /* while (1) */
  403. pe_label:
  404. fclose(f);
  405. bb_error_msg("parse error in %s, line %u: %s", config_file, lc, errmsg);
  406. /* Release any allocated memory before returning. */
  407. llist_free((llist_t*)sct_head, NULL);
  408. }
  409. # else
  410. static inline void parse_config_file(void)
  411. {
  412. IF_FEATURE_SUID(ruid = getuid();)
  413. }
  414. # endif /* FEATURE_SUID_CONFIG */
  415. # if ENABLE_FEATURE_SUID
  416. static void check_suid(int applet_no)
  417. {
  418. gid_t rgid; /* real gid */
  419. if (ruid == 0) /* set by parse_config_file() */
  420. return; /* run by root - no need to check more */
  421. rgid = getgid();
  422. # if ENABLE_FEATURE_SUID_CONFIG
  423. if (suid_cfg_readable) {
  424. uid_t uid;
  425. struct suid_config_t *sct;
  426. mode_t m;
  427. for (sct = suid_config; sct; sct = sct->m_next) {
  428. if (sct->m_applet == applet_no)
  429. goto found;
  430. }
  431. goto check_need_suid;
  432. found:
  433. /* Is this user allowed to run this applet? */
  434. m = sct->m_mode;
  435. if (sct->m_ugid.uid == ruid)
  436. /* same uid */
  437. m >>= 6;
  438. else if ((sct->m_ugid.gid == rgid) || ingroup(ruid, sct->m_ugid.gid))
  439. /* same group / in group */
  440. m >>= 3;
  441. if (!(m & S_IXOTH)) /* is x bit not set? */
  442. bb_error_msg_and_die("you have no permission to run this applet");
  443. /* We set effective AND saved ids. If saved-id is not set
  444. * like we do below, seteuid(0) can still later succeed! */
  445. /* Are we directed to change gid
  446. * (APPLET = *s* USER.GROUP or APPLET = *S* USER.GROUP)?
  447. */
  448. if (sct->m_mode & S_ISGID)
  449. rgid = sct->m_ugid.gid;
  450. /* else: we will set egid = rgid, thus dropping sgid effect */
  451. if (setresgid(-1, rgid, rgid))
  452. bb_perror_msg_and_die("setresgid");
  453. /* Are we directed to change uid
  454. * (APPLET = s** USER.GROUP or APPLET = S** USER.GROUP)?
  455. */
  456. uid = ruid;
  457. if (sct->m_mode & S_ISUID)
  458. uid = sct->m_ugid.uid;
  459. /* else: we will set euid = ruid, thus dropping suid effect */
  460. if (setresuid(-1, uid, uid))
  461. bb_perror_msg_and_die("setresuid");
  462. goto ret;
  463. }
  464. # if !ENABLE_FEATURE_SUID_CONFIG_QUIET
  465. {
  466. static bool onetime = 0;
  467. if (!onetime) {
  468. onetime = 1;
  469. bb_error_msg("using fallback suid method");
  470. }
  471. }
  472. # endif
  473. check_need_suid:
  474. # endif
  475. if (APPLET_SUID(applet_no) == BB_SUID_REQUIRE) {
  476. /* Real uid is not 0. If euid isn't 0 too, suid bit
  477. * is most probably not set on our executable */
  478. if (geteuid())
  479. bb_error_msg_and_die("must be suid to work properly");
  480. } else if (APPLET_SUID(applet_no) == BB_SUID_DROP) {
  481. xsetgid(rgid); /* drop all privileges */
  482. xsetuid(ruid);
  483. }
  484. # if ENABLE_FEATURE_SUID_CONFIG
  485. ret: ;
  486. llist_free((llist_t*)suid_config, NULL);
  487. # endif
  488. }
  489. # else
  490. # define check_suid(x) ((void)0)
  491. # endif /* FEATURE_SUID */
  492. # if ENABLE_FEATURE_INSTALLER
  493. static const char usr_bin [] ALIGN1 = "/usr/bin/";
  494. static const char usr_sbin[] ALIGN1 = "/usr/sbin/";
  495. static const char *const install_dir[] = {
  496. &usr_bin [8], /* "/" */
  497. &usr_bin [4], /* "/bin/" */
  498. &usr_sbin[4] /* "/sbin/" */
  499. # if !ENABLE_INSTALL_NO_USR
  500. ,usr_bin
  501. ,usr_sbin
  502. # endif
  503. };
  504. /* create (sym)links for each applet */
  505. static void install_links(const char *busybox, int use_symbolic_links,
  506. char *custom_install_dir)
  507. {
  508. /* directory table
  509. * this should be consistent w/ the enum,
  510. * busybox.h::bb_install_loc_t, or else... */
  511. int (*lf)(const char *, const char *);
  512. char *fpc;
  513. unsigned i;
  514. int rc;
  515. lf = link;
  516. if (use_symbolic_links)
  517. lf = symlink;
  518. for (i = 0; i < ARRAY_SIZE(applet_main); i++) {
  519. fpc = concat_path_file(
  520. custom_install_dir ? custom_install_dir : install_dir[APPLET_INSTALL_LOC(i)],
  521. APPLET_NAME(i));
  522. // debug: bb_error_msg("%slinking %s to busybox",
  523. // use_symbolic_links ? "sym" : "", fpc);
  524. rc = lf(busybox, fpc);
  525. if (rc != 0 && errno != EEXIST) {
  526. bb_simple_perror_msg(fpc);
  527. }
  528. free(fpc);
  529. }
  530. }
  531. # else
  532. static void install_links(const char *busybox UNUSED_PARAM,
  533. int use_symbolic_links UNUSED_PARAM,
  534. char *custom_install_dir UNUSED_PARAM)
  535. {
  536. }
  537. # endif
  538. /* If we were called as "busybox..." */
  539. static int busybox_main(char **argv)
  540. {
  541. if (!argv[1]) {
  542. /* Called without arguments */
  543. const char *a;
  544. int col;
  545. unsigned output_width;
  546. help:
  547. output_width = 80;
  548. if (ENABLE_FEATURE_AUTOWIDTH) {
  549. /* Obtain the terminal width */
  550. get_terminal_width_height(0, &output_width, NULL);
  551. }
  552. dup2(1, 2);
  553. full_write2_str(bb_banner); /* reuse const string */
  554. full_write2_str(" multi-call binary.\n"); /* reuse */
  555. full_write2_str(
  556. "BusyBox is copyrighted by many authors between 1998-2012.\n"
  557. "Licensed under GPLv2. See source distribution for detailed\n"
  558. "copyright notices.\n"
  559. "\n"
  560. "Usage: busybox [function [arguments]...]\n"
  561. " or: busybox --list"IF_FEATURE_INSTALLER("[-full]")"\n"
  562. IF_FEATURE_INSTALLER(
  563. " or: busybox --install [-s] [DIR]\n"
  564. )
  565. " or: function [arguments]...\n"
  566. "\n"
  567. "\tBusyBox is a multi-call binary that combines many common Unix\n"
  568. "\tutilities into a single executable. Most people will create a\n"
  569. "\tlink to busybox for each function they wish to use and BusyBox\n"
  570. "\twill act like whatever it was invoked as.\n"
  571. "\n"
  572. "Currently defined functions:\n"
  573. );
  574. col = 0;
  575. a = applet_names;
  576. /* prevent last comma to be in the very last pos */
  577. output_width--;
  578. while (*a) {
  579. int len2 = strlen(a) + 2;
  580. if (col >= (int)output_width - len2) {
  581. full_write2_str(",\n");
  582. col = 0;
  583. }
  584. if (col == 0) {
  585. col = 6;
  586. full_write2_str("\t");
  587. } else {
  588. full_write2_str(", ");
  589. }
  590. full_write2_str(a);
  591. col += len2;
  592. a += len2 - 1;
  593. }
  594. full_write2_str("\n\n");
  595. return 0;
  596. }
  597. if (strncmp(argv[1], "--list", 6) == 0) {
  598. unsigned i = 0;
  599. const char *a = applet_names;
  600. dup2(1, 2);
  601. while (*a) {
  602. # if ENABLE_FEATURE_INSTALLER
  603. if (argv[1][6]) /* --list-full? */
  604. full_write2_str(install_dir[APPLET_INSTALL_LOC(i)] + 1);
  605. # endif
  606. full_write2_str(a);
  607. full_write2_str("\n");
  608. i++;
  609. a += strlen(a) + 1;
  610. }
  611. return 0;
  612. }
  613. if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) {
  614. int use_symbolic_links;
  615. const char *busybox;
  616. busybox = xmalloc_readlink(bb_busybox_exec_path);
  617. if (!busybox) {
  618. /* bb_busybox_exec_path is usually "/proc/self/exe".
  619. * In chroot, readlink("/proc/self/exe") usually fails.
  620. * In such case, better use argv[0] as symlink target
  621. * if it is a full path name.
  622. */
  623. if (argv[0][0] != '/')
  624. bb_error_msg_and_die("'%s' is not an absolute path", argv[0]);
  625. busybox = argv[0];
  626. }
  627. /* busybox --install [-s] [DIR]:
  628. * -s: make symlinks
  629. * DIR: directory to install links to
  630. */
  631. use_symbolic_links = (argv[2] && strcmp(argv[2], "-s") == 0 && ++argv);
  632. install_links(busybox, use_symbolic_links, argv[2]);
  633. return 0;
  634. }
  635. if (strcmp(argv[1], "--help") == 0) {
  636. /* "busybox --help [<applet>]" */
  637. if (!argv[2])
  638. goto help;
  639. /* convert to "<applet> --help" */
  640. argv[0] = argv[2];
  641. argv[2] = NULL;
  642. } else {
  643. /* "busybox <applet> arg1 arg2 ..." */
  644. argv++;
  645. }
  646. /* We support "busybox /a/path/to/applet args..." too. Allows for
  647. * "#!/bin/busybox"-style wrappers */
  648. applet_name = bb_get_last_path_component_nostrip(argv[0]);
  649. run_applet_and_exit(applet_name, argv);
  650. /*bb_error_msg_and_die("applet not found"); - sucks in printf */
  651. full_write2_str(applet_name);
  652. full_write2_str(": applet not found\n");
  653. /* POSIX: "If a command is not found, the exit status shall be 127" */
  654. exit(127);
  655. }
  656. void FAST_FUNC run_applet_no_and_exit(int applet_no, char **argv)
  657. {
  658. int argc = 1;
  659. while (argv[argc])
  660. argc++;
  661. /* Reinit some shared global data */
  662. xfunc_error_retval = EXIT_FAILURE;
  663. applet_name = APPLET_NAME(applet_no);
  664. #if defined APPLET_NO_test
  665. /* Special case. POSIX says "test --help"
  666. * should be no different from e.g. "test --foo".
  667. * Thus for "test", we skip --help check.
  668. */
  669. if (applet_no != APPLET_NO_test)
  670. #endif
  671. {
  672. if (argc == 2 && strcmp(argv[1], "--help") == 0) {
  673. #if defined APPLET_NO_false
  674. /* Someone insisted that "false --help" must exit 1. Sigh */
  675. if (applet_no != APPLET_NO_false)
  676. #endif
  677. {
  678. /* Make "foo --help" exit with 0: */
  679. xfunc_error_retval = 0;
  680. }
  681. bb_show_usage();
  682. }
  683. }
  684. if (ENABLE_FEATURE_SUID)
  685. check_suid(applet_no);
  686. exit(applet_main[applet_no](argc, argv));
  687. }
  688. void FAST_FUNC run_applet_and_exit(const char *name, char **argv)
  689. {
  690. int applet = find_applet_by_name(name);
  691. if (applet >= 0)
  692. run_applet_no_and_exit(applet, argv);
  693. if (strncmp(name, "busybox", 7) == 0)
  694. exit(busybox_main(argv));
  695. }
  696. #endif /* !defined(SINGLE_APPLET_MAIN) */
  697. #if ENABLE_BUILD_LIBBUSYBOX
  698. int lbb_main(char **argv)
  699. #else
  700. int main(int argc UNUSED_PARAM, char **argv)
  701. #endif
  702. {
  703. /* Tweak malloc for reduced memory consumption */
  704. #ifdef M_TRIM_THRESHOLD
  705. /* M_TRIM_THRESHOLD is the maximum amount of freed top-most memory
  706. * to keep before releasing to the OS
  707. * Default is way too big: 256k
  708. */
  709. mallopt(M_TRIM_THRESHOLD, 8 * 1024);
  710. #endif
  711. #ifdef M_MMAP_THRESHOLD
  712. /* M_MMAP_THRESHOLD is the request size threshold for using mmap()
  713. * Default is too big: 256k
  714. */
  715. mallopt(M_MMAP_THRESHOLD, 32 * 1024 - 256);
  716. #endif
  717. #if !BB_MMU
  718. /* NOMMU re-exec trick sets high-order bit in first byte of name */
  719. if (argv[0][0] & 0x80) {
  720. re_execed = 1;
  721. argv[0][0] &= 0x7f;
  722. }
  723. #endif
  724. #if defined(SINGLE_APPLET_MAIN)
  725. /* Only one applet is selected in .config */
  726. if (argv[1] && strncmp(argv[0], "busybox", 7) == 0) {
  727. /* "busybox <applet> <params>" should still work as expected */
  728. argv++;
  729. }
  730. /* applet_names in this case is just "applet\0\0" */
  731. lbb_prepare(applet_names IF_FEATURE_INDIVIDUAL(, argv));
  732. return SINGLE_APPLET_MAIN(argc, argv);
  733. #else
  734. lbb_prepare("busybox" IF_FEATURE_INDIVIDUAL(, argv));
  735. applet_name = argv[0];
  736. if (applet_name[0] == '-')
  737. applet_name++;
  738. applet_name = bb_basename(applet_name);
  739. parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */
  740. run_applet_and_exit(applet_name, argv);
  741. /*bb_error_msg_and_die("applet not found"); - sucks in printf */
  742. full_write2_str(applet_name);
  743. full_write2_str(": applet not found\n");
  744. /* POSIX: "If a command is not found, the exit status shall be 127" */
  745. exit(127);
  746. #endif
  747. }