appletlib.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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. /* Set locale for everybody except 'init' */
  166. if (ENABLE_LOCALE_SUPPORT && getpid() != 1)
  167. setlocale(LC_ALL, "");
  168. #if ENABLE_FEATURE_INDIVIDUAL
  169. /* Redundant for busybox (run_applet_and_exit covers that case)
  170. * but needed for "individual applet" mode */
  171. if (argv[1]
  172. && !argv[2]
  173. && strcmp(argv[1], "--help") == 0
  174. && strncmp(applet, "busybox", 7) != 0
  175. ) {
  176. /* Special case. POSIX says "test --help"
  177. * should be no different from e.g. "test --foo". */
  178. if (!ENABLE_TEST || strcmp(applet_name, "test") != 0)
  179. bb_show_usage();
  180. }
  181. #endif
  182. }
  183. /* The code below can well be in applets/applets.c, as it is used only
  184. * for busybox binary, not "individual" binaries.
  185. * However, keeping it here and linking it into libbusybox.so
  186. * (together with remaining tiny applets/applets.o)
  187. * makes it possible to avoid --whole-archive at link time.
  188. * This makes (shared busybox) + libbusybox smaller.
  189. * (--gc-sections would be even better....)
  190. */
  191. const char *applet_name;
  192. #if !BB_MMU
  193. bool re_execed;
  194. #endif
  195. /* If not built as a single-applet executable... */
  196. #if !defined(SINGLE_APPLET_MAIN)
  197. IF_FEATURE_SUID(static uid_t ruid;) /* real uid */
  198. # if ENABLE_FEATURE_SUID_CONFIG
  199. static struct suid_config_t {
  200. /* next ptr must be first: this struct needs to be llist-compatible */
  201. struct suid_config_t *m_next;
  202. struct bb_uidgid_t m_ugid;
  203. int m_applet;
  204. mode_t m_mode;
  205. } *suid_config;
  206. static bool suid_cfg_readable;
  207. /* check if u is member of group g */
  208. static int ingroup(uid_t u, gid_t g)
  209. {
  210. struct group *grp = getgrgid(g);
  211. if (grp) {
  212. char **mem;
  213. for (mem = grp->gr_mem; *mem; mem++) {
  214. struct passwd *pwd = getpwnam(*mem);
  215. if (pwd && (pwd->pw_uid == u))
  216. return 1;
  217. }
  218. }
  219. return 0;
  220. }
  221. /* libbb candidate */
  222. static char *get_trimmed_slice(char *s, char *e)
  223. {
  224. /* First, consider the value at e to be nul and back up until we
  225. * reach a non-space char. Set the char after that (possibly at
  226. * the original e) to nul. */
  227. while (e-- > s) {
  228. if (!isspace(*e)) {
  229. break;
  230. }
  231. }
  232. e[1] = '\0';
  233. /* Next, advance past all leading space and return a ptr to the
  234. * first non-space char; possibly the terminating nul. */
  235. return skip_whitespace(s);
  236. }
  237. static void parse_config_file(void)
  238. {
  239. /* Don't depend on the tools to combine strings. */
  240. static const char config_file[] ALIGN1 = "/etc/busybox.conf";
  241. struct suid_config_t *sct_head;
  242. int applet_no;
  243. FILE *f;
  244. const char *errmsg;
  245. unsigned lc;
  246. smallint section;
  247. struct stat st;
  248. ruid = getuid();
  249. if (ruid == 0) /* run by root - don't need to even read config file */
  250. return;
  251. if ((stat(config_file, &st) != 0) /* No config file? */
  252. || !S_ISREG(st.st_mode) /* Not a regular file? */
  253. || (st.st_uid != 0) /* Not owned by root? */
  254. || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
  255. || !(f = fopen_for_read(config_file)) /* Cannot open? */
  256. ) {
  257. return;
  258. }
  259. suid_cfg_readable = 1;
  260. sct_head = NULL;
  261. section = lc = 0;
  262. while (1) {
  263. char buffer[256];
  264. char *s;
  265. if (!fgets(buffer, sizeof(buffer), f)) { /* Are we done? */
  266. // Looks like bloat
  267. //if (ferror(f)) { /* Make sure it wasn't a read error. */
  268. // errmsg = "reading";
  269. // goto pe_label;
  270. //}
  271. fclose(f);
  272. suid_config = sct_head; /* Success, so set the pointer. */
  273. return;
  274. }
  275. s = buffer;
  276. lc++; /* Got a (partial) line. */
  277. /* If a line is too long for our buffer, we consider it an error.
  278. * The following test does mistreat one corner case though.
  279. * If the final line of the file does not end with a newline and
  280. * yet exactly fills the buffer, it will be treated as too long
  281. * even though there isn't really a problem. But it isn't really
  282. * worth adding code to deal with such an unlikely situation, and
  283. * we do err on the side of caution. Besides, the line would be
  284. * too long if it did end with a newline. */
  285. if (!strchr(s, '\n') && !feof(f)) {
  286. errmsg = "line too long";
  287. goto pe_label;
  288. }
  289. /* Trim leading and trailing whitespace, ignoring comments, and
  290. * check if the resulting string is empty. */
  291. s = get_trimmed_slice(s, strchrnul(s, '#'));
  292. if (!*s) {
  293. continue;
  294. }
  295. /* Check for a section header. */
  296. if (*s == '[') {
  297. /* Unlike the old code, we ignore leading and trailing
  298. * whitespace for the section name. We also require that
  299. * there are no stray characters after the closing bracket. */
  300. char *e = strchr(s, ']');
  301. if (!e /* Missing right bracket? */
  302. || e[1] /* Trailing characters? */
  303. || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
  304. ) {
  305. errmsg = "section header";
  306. goto pe_label;
  307. }
  308. /* Right now we only have one section so just check it.
  309. * If more sections are added in the future, please don't
  310. * resort to cascading ifs with multiple strcasecmp calls.
  311. * That kind of bloated code is all too common. A loop
  312. * and a string table would be a better choice unless the
  313. * number of sections is very small. */
  314. if (strcasecmp(s, "SUID") == 0) {
  315. section = 1;
  316. continue;
  317. }
  318. section = -1; /* Unknown section so set to skip. */
  319. continue;
  320. }
  321. /* Process sections. */
  322. if (section == 1) { /* SUID */
  323. /* Since we trimmed leading and trailing space above, we're
  324. * now looking for strings of the form
  325. * <key>[::space::]*=[::space::]*<value>
  326. * where both key and value could contain inner whitespace. */
  327. /* First get the key (an applet name in our case). */
  328. char *e = strchr(s, '=');
  329. if (e) {
  330. s = get_trimmed_slice(s, e);
  331. }
  332. if (!e || !*s) { /* Missing '=' or empty key. */
  333. errmsg = "keyword";
  334. goto pe_label;
  335. }
  336. /* Ok, we have an applet name. Process the rhs if this
  337. * applet is currently built in and ignore it otherwise.
  338. * Note: this can hide config file bugs which only pop
  339. * up when the busybox configuration is changed. */
  340. applet_no = find_applet_by_name(s);
  341. if (applet_no >= 0) {
  342. unsigned i;
  343. struct suid_config_t *sct;
  344. /* Note: We currently don't check for duplicates!
  345. * The last config line for each applet will be the
  346. * one used since we insert at the head of the list.
  347. * I suppose this could be considered a feature. */
  348. sct = xzalloc(sizeof(*sct));
  349. sct->m_applet = applet_no;
  350. /*sct->m_mode = 0;*/
  351. sct->m_next = sct_head;
  352. sct_head = sct;
  353. /* Get the specified mode. */
  354. e = skip_whitespace(e+1);
  355. for (i = 0; i < 3; i++) {
  356. /* There are 4 chars for each of user/group/other.
  357. * "x-xx" instead of "x-" are to make
  358. * "idx > 3" check catch invalid chars.
  359. */
  360. static const char mode_chars[] ALIGN1 = "Ssx-" "Ssx-" "x-xx";
  361. static const unsigned short mode_mask[] ALIGN2 = {
  362. S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* Ssx- */
  363. S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* Ssx- */
  364. S_IXOTH, 0 /* x- */
  365. };
  366. const char *q = strchrnul(mode_chars + 4*i, *e);
  367. unsigned idx = q - (mode_chars + 4*i);
  368. if (idx > 3) {
  369. errmsg = "mode";
  370. goto pe_label;
  371. }
  372. sct->m_mode |= mode_mask[q - mode_chars];
  373. e++;
  374. }
  375. /* Now get the user/group info. */
  376. s = skip_whitespace(e);
  377. /* Default is 0.0, else parse USER.GROUP: */
  378. if (*s) {
  379. /* We require whitespace between mode and USER.GROUP */
  380. if ((s == e) || !(e = strchr(s, '.'))) {
  381. errmsg = "uid.gid";
  382. goto pe_label;
  383. }
  384. *e = ':'; /* get_uidgid needs USER:GROUP syntax */
  385. if (get_uidgid(&sct->m_ugid, s, /*allow_numeric:*/ 1) == 0) {
  386. errmsg = "unknown user/group";
  387. goto pe_label;
  388. }
  389. }
  390. }
  391. continue;
  392. }
  393. /* Unknown sections are ignored. */
  394. /* Encountering configuration lines prior to seeing a
  395. * section header is treated as an error. This is how
  396. * the old code worked, but it may not be desirable.
  397. * We may want to simply ignore such lines in case they
  398. * are used in some future version of busybox. */
  399. if (!section) {
  400. errmsg = "keyword outside section";
  401. goto pe_label;
  402. }
  403. } /* while (1) */
  404. pe_label:
  405. fclose(f);
  406. bb_error_msg("parse error in %s, line %u: %s", config_file, lc, errmsg);
  407. /* Release any allocated memory before returning. */
  408. llist_free((llist_t*)sct_head, NULL);
  409. }
  410. # else
  411. static inline void parse_config_file(void)
  412. {
  413. IF_FEATURE_SUID(ruid = getuid();)
  414. }
  415. # endif /* FEATURE_SUID_CONFIG */
  416. # if ENABLE_FEATURE_SUID
  417. static void check_suid(int applet_no)
  418. {
  419. gid_t rgid; /* real gid */
  420. if (ruid == 0) /* set by parse_config_file() */
  421. return; /* run by root - no need to check more */
  422. rgid = getgid();
  423. # if ENABLE_FEATURE_SUID_CONFIG
  424. if (suid_cfg_readable) {
  425. uid_t uid;
  426. struct suid_config_t *sct;
  427. mode_t m;
  428. for (sct = suid_config; sct; sct = sct->m_next) {
  429. if (sct->m_applet == applet_no)
  430. goto found;
  431. }
  432. goto check_need_suid;
  433. found:
  434. /* Is this user allowed to run this applet? */
  435. m = sct->m_mode;
  436. if (sct->m_ugid.uid == ruid)
  437. /* same uid */
  438. m >>= 6;
  439. else if ((sct->m_ugid.gid == rgid) || ingroup(ruid, sct->m_ugid.gid))
  440. /* same group / in group */
  441. m >>= 3;
  442. if (!(m & S_IXOTH)) /* is x bit not set? */
  443. bb_error_msg_and_die("you have no permission to run this applet");
  444. /* We set effective AND saved ids. If saved-id is not set
  445. * like we do below, seteuid(0) can still later succeed! */
  446. /* Are we directed to change gid
  447. * (APPLET = *s* USER.GROUP or APPLET = *S* USER.GROUP)?
  448. */
  449. if (sct->m_mode & S_ISGID)
  450. rgid = sct->m_ugid.gid;
  451. /* else: we will set egid = rgid, thus dropping sgid effect */
  452. if (setresgid(-1, rgid, rgid))
  453. bb_perror_msg_and_die("setresgid");
  454. /* Are we directed to change uid
  455. * (APPLET = s** USER.GROUP or APPLET = S** USER.GROUP)?
  456. */
  457. uid = ruid;
  458. if (sct->m_mode & S_ISUID)
  459. uid = sct->m_ugid.uid;
  460. /* else: we will set euid = ruid, thus dropping suid effect */
  461. if (setresuid(-1, uid, uid))
  462. bb_perror_msg_and_die("setresuid");
  463. goto ret;
  464. }
  465. # if !ENABLE_FEATURE_SUID_CONFIG_QUIET
  466. {
  467. static bool onetime = 0;
  468. if (!onetime) {
  469. onetime = 1;
  470. bb_error_msg("using fallback suid method");
  471. }
  472. }
  473. # endif
  474. check_need_suid:
  475. # endif
  476. if (APPLET_SUID(applet_no) == BB_SUID_REQUIRE) {
  477. /* Real uid is not 0. If euid isn't 0 too, suid bit
  478. * is most probably not set on our executable */
  479. if (geteuid())
  480. bb_error_msg_and_die("must be suid to work properly");
  481. } else if (APPLET_SUID(applet_no) == BB_SUID_DROP) {
  482. xsetgid(rgid); /* drop all privileges */
  483. xsetuid(ruid);
  484. }
  485. # if ENABLE_FEATURE_SUID_CONFIG
  486. ret: ;
  487. llist_free((llist_t*)suid_config, NULL);
  488. # endif
  489. }
  490. # else
  491. # define check_suid(x) ((void)0)
  492. # endif /* FEATURE_SUID */
  493. # if ENABLE_FEATURE_INSTALLER
  494. static const char usr_bin [] ALIGN1 = "/usr/bin/";
  495. static const char usr_sbin[] ALIGN1 = "/usr/sbin/";
  496. static const char *const install_dir[] = {
  497. &usr_bin [8], /* "/" */
  498. &usr_bin [4], /* "/bin/" */
  499. &usr_sbin[4] /* "/sbin/" */
  500. # if !ENABLE_INSTALL_NO_USR
  501. ,usr_bin
  502. ,usr_sbin
  503. # endif
  504. };
  505. /* create (sym)links for each applet */
  506. static void install_links(const char *busybox, int use_symbolic_links,
  507. char *custom_install_dir)
  508. {
  509. /* directory table
  510. * this should be consistent w/ the enum,
  511. * busybox.h::bb_install_loc_t, or else... */
  512. int (*lf)(const char *, const char *);
  513. char *fpc;
  514. unsigned i;
  515. int rc;
  516. lf = link;
  517. if (use_symbolic_links)
  518. lf = symlink;
  519. for (i = 0; i < ARRAY_SIZE(applet_main); i++) {
  520. fpc = concat_path_file(
  521. custom_install_dir ? custom_install_dir : install_dir[APPLET_INSTALL_LOC(i)],
  522. APPLET_NAME(i));
  523. // debug: bb_error_msg("%slinking %s to busybox",
  524. // use_symbolic_links ? "sym" : "", fpc);
  525. rc = lf(busybox, fpc);
  526. if (rc != 0 && errno != EEXIST) {
  527. bb_simple_perror_msg(fpc);
  528. }
  529. free(fpc);
  530. }
  531. }
  532. # else
  533. # define install_links(x,y,z) ((void)0)
  534. # endif
  535. /* If we were called as "busybox..." */
  536. static int busybox_main(char **argv)
  537. {
  538. if (!argv[1]) {
  539. /* Called without arguments */
  540. const char *a;
  541. int col;
  542. unsigned output_width;
  543. help:
  544. output_width = 80;
  545. if (ENABLE_FEATURE_AUTOWIDTH) {
  546. /* Obtain the terminal width */
  547. get_terminal_width_height(0, &output_width, NULL);
  548. }
  549. dup2(1, 2);
  550. full_write2_str(bb_banner); /* reuse const string */
  551. full_write2_str(" multi-call binary.\n"); /* reuse */
  552. full_write2_str(
  553. "BusyBox is copyrighted by many authors between 1998-2012.\n"
  554. "Licensed under GPLv2. See source distribution for detailed\n"
  555. "copyright notices.\n"
  556. "\n"
  557. "Usage: busybox [function [arguments]...]\n"
  558. " or: busybox --list"IF_FEATURE_INSTALLER("[-full]")"\n"
  559. IF_FEATURE_INSTALLER(
  560. " or: busybox --install [-s] [DIR]\n"
  561. )
  562. " or: function [arguments]...\n"
  563. "\n"
  564. "\tBusyBox is a multi-call binary that combines many common Unix\n"
  565. "\tutilities into a single executable. Most people will create a\n"
  566. "\tlink to busybox for each function they wish to use and BusyBox\n"
  567. "\twill act like whatever it was invoked as.\n"
  568. "\n"
  569. "Currently defined functions:\n"
  570. );
  571. col = 0;
  572. a = applet_names;
  573. /* prevent last comma to be in the very last pos */
  574. output_width--;
  575. while (*a) {
  576. int len2 = strlen(a) + 2;
  577. if (col >= (int)output_width - len2) {
  578. full_write2_str(",\n");
  579. col = 0;
  580. }
  581. if (col == 0) {
  582. col = 6;
  583. full_write2_str("\t");
  584. } else {
  585. full_write2_str(", ");
  586. }
  587. full_write2_str(a);
  588. col += len2;
  589. a += len2 - 1;
  590. }
  591. full_write2_str("\n\n");
  592. return 0;
  593. }
  594. if (strncmp(argv[1], "--list", 6) == 0) {
  595. unsigned i = 0;
  596. const char *a = applet_names;
  597. dup2(1, 2);
  598. while (*a) {
  599. # if ENABLE_FEATURE_INSTALLER
  600. if (argv[1][6]) /* --list-full? */
  601. full_write2_str(install_dir[APPLET_INSTALL_LOC(i)] + 1);
  602. # endif
  603. full_write2_str(a);
  604. full_write2_str("\n");
  605. i++;
  606. a += strlen(a) + 1;
  607. }
  608. return 0;
  609. }
  610. if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) {
  611. int use_symbolic_links;
  612. const char *busybox;
  613. busybox = xmalloc_readlink(bb_busybox_exec_path);
  614. if (!busybox) {
  615. /* bb_busybox_exec_path is usually "/proc/self/exe".
  616. * In chroot, readlink("/proc/self/exe") usually fails.
  617. * In such case, better use argv[0] as symlink target
  618. * if it is a full path name.
  619. */
  620. if (argv[0][0] != '/')
  621. bb_error_msg_and_die("'%s' is not an absolute path", argv[0]);
  622. busybox = argv[0];
  623. }
  624. /* busybox --install [-s] [DIR]:
  625. * -s: make symlinks
  626. * DIR: directory to install links to
  627. */
  628. use_symbolic_links = (argv[2] && strcmp(argv[2], "-s") == 0 && ++argv);
  629. install_links(busybox, use_symbolic_links, argv[2]);
  630. return 0;
  631. }
  632. if (strcmp(argv[1], "--help") == 0) {
  633. /* "busybox --help [<applet>]" */
  634. if (!argv[2])
  635. goto help;
  636. /* convert to "<applet> --help" */
  637. argv[0] = argv[2];
  638. argv[2] = NULL;
  639. } else {
  640. /* "busybox <applet> arg1 arg2 ..." */
  641. argv++;
  642. }
  643. /* We support "busybox /a/path/to/applet args..." too. Allows for
  644. * "#!/bin/busybox"-style wrappers */
  645. applet_name = bb_get_last_path_component_nostrip(argv[0]);
  646. run_applet_and_exit(applet_name, argv);
  647. /*bb_error_msg_and_die("applet not found"); - sucks in printf */
  648. full_write2_str(applet_name);
  649. full_write2_str(": applet not found\n");
  650. xfunc_die();
  651. }
  652. void FAST_FUNC run_applet_no_and_exit(int applet_no, char **argv)
  653. {
  654. int argc = 1;
  655. while (argv[argc])
  656. argc++;
  657. /* Reinit some shared global data */
  658. xfunc_error_retval = EXIT_FAILURE;
  659. applet_name = APPLET_NAME(applet_no);
  660. if (argc == 2 && strcmp(argv[1], "--help") == 0) {
  661. /* Special case. POSIX says "test --help"
  662. * should be no different from e.g. "test --foo". */
  663. //TODO: just compare applet_no with APPLET_NO_test
  664. if (!ENABLE_TEST || strcmp(applet_name, "test") != 0) {
  665. /* If you want "foo --help" to return 0: */
  666. xfunc_error_retval = 0;
  667. bb_show_usage();
  668. }
  669. }
  670. if (ENABLE_FEATURE_SUID)
  671. check_suid(applet_no);
  672. exit(applet_main[applet_no](argc, argv));
  673. }
  674. void FAST_FUNC run_applet_and_exit(const char *name, char **argv)
  675. {
  676. int applet = find_applet_by_name(name);
  677. if (applet >= 0)
  678. run_applet_no_and_exit(applet, argv);
  679. if (strncmp(name, "busybox", 7) == 0)
  680. exit(busybox_main(argv));
  681. }
  682. #endif /* !defined(SINGLE_APPLET_MAIN) */
  683. #if ENABLE_BUILD_LIBBUSYBOX
  684. int lbb_main(char **argv)
  685. #else
  686. int main(int argc UNUSED_PARAM, char **argv)
  687. #endif
  688. {
  689. /* Tweak malloc for reduced memory consumption */
  690. #ifdef M_TRIM_THRESHOLD
  691. /* M_TRIM_THRESHOLD is the maximum amount of freed top-most memory
  692. * to keep before releasing to the OS
  693. * Default is way too big: 256k
  694. */
  695. mallopt(M_TRIM_THRESHOLD, 8 * 1024);
  696. #endif
  697. #ifdef M_MMAP_THRESHOLD
  698. /* M_MMAP_THRESHOLD is the request size threshold for using mmap()
  699. * Default is too big: 256k
  700. */
  701. mallopt(M_MMAP_THRESHOLD, 32 * 1024 - 256);
  702. #endif
  703. #if !BB_MMU
  704. /* NOMMU re-exec trick sets high-order bit in first byte of name */
  705. if (argv[0][0] & 0x80) {
  706. re_execed = 1;
  707. argv[0][0] &= 0x7f;
  708. }
  709. #endif
  710. #if defined(SINGLE_APPLET_MAIN)
  711. /* Only one applet is selected in .config */
  712. if (argv[1] && strncmp(argv[0], "busybox", 7) == 0) {
  713. /* "busybox <applet> <params>" should still work as expected */
  714. argv++;
  715. }
  716. /* applet_names in this case is just "applet\0\0" */
  717. lbb_prepare(applet_names IF_FEATURE_INDIVIDUAL(, argv));
  718. return SINGLE_APPLET_MAIN(argc, argv);
  719. #else
  720. lbb_prepare("busybox" IF_FEATURE_INDIVIDUAL(, argv));
  721. applet_name = argv[0];
  722. if (applet_name[0] == '-')
  723. applet_name++;
  724. applet_name = bb_basename(applet_name);
  725. parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */
  726. run_applet_and_exit(applet_name, argv);
  727. /*bb_error_msg_and_die("applet not found"); - sucks in printf */
  728. full_write2_str(applet_name);
  729. full_write2_str(": applet not found\n");
  730. xfunc_die();
  731. #endif
  732. }