appletlib.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. /* NB: any char pointer will work as well, not necessarily applet_names */
  125. static int applet_name_compare(const void *name, const void *v)
  126. {
  127. int i = (const char *)v - applet_names;
  128. return strcmp(name, APPLET_NAME(i));
  129. }
  130. #endif
  131. int FAST_FUNC find_applet_by_name(const char *name)
  132. {
  133. #if NUM_APPLETS > 8
  134. /* Do a binary search to find the applet entry given the name. */
  135. const char *p;
  136. p = bsearch(name, applet_names, ARRAY_SIZE(applet_main), 1, applet_name_compare);
  137. if (!p)
  138. return -1;
  139. return p - applet_names;
  140. #else
  141. /* A version which does not pull in bsearch */
  142. int i = 0;
  143. const char *p = applet_names;
  144. while (i < NUM_APPLETS) {
  145. if (strcmp(name, p) == 0)
  146. return i;
  147. p += strlen(p) + 1;
  148. i++;
  149. }
  150. return -1;
  151. #endif
  152. }
  153. void lbb_prepare(const char *applet
  154. IF_FEATURE_INDIVIDUAL(, char **argv))
  155. MAIN_EXTERNALLY_VISIBLE;
  156. void lbb_prepare(const char *applet
  157. IF_FEATURE_INDIVIDUAL(, char **argv))
  158. {
  159. #ifdef __GLIBC__
  160. (*(int **)&bb_errno) = __errno_location();
  161. barrier();
  162. #endif
  163. applet_name = applet;
  164. /* Set locale for everybody except 'init' */
  165. if (ENABLE_LOCALE_SUPPORT && getpid() != 1)
  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. # define install_links(x,y,z) ((void)0)
  533. # endif
  534. /* If we were called as "busybox..." */
  535. static int busybox_main(char **argv)
  536. {
  537. if (!argv[1]) {
  538. /* Called without arguments */
  539. const char *a;
  540. int col;
  541. unsigned output_width;
  542. help:
  543. output_width = 80;
  544. if (ENABLE_FEATURE_AUTOWIDTH) {
  545. /* Obtain the terminal width */
  546. get_terminal_width_height(0, &output_width, NULL);
  547. }
  548. dup2(1, 2);
  549. full_write2_str(bb_banner); /* reuse const string */
  550. full_write2_str(" multi-call binary.\n"); /* reuse */
  551. full_write2_str(
  552. "Copyright (C) 1998-2011 Erik Andersen, Rob Landley, Denys Vlasenko\n"
  553. "and others. Licensed under GPLv2.\n"
  554. "See source distribution for full notice.\n"
  555. "\n"
  556. "Usage: busybox [function] [arguments]...\n"
  557. " or: busybox --list"IF_FEATURE_INSTALLER("[-full]")"\n"
  558. IF_FEATURE_INSTALLER(
  559. " or: busybox --install [-s] [DIR]\n"
  560. )
  561. " or: function [arguments]...\n"
  562. "\n"
  563. "\tBusyBox is a multi-call binary that combines many common Unix\n"
  564. "\tutilities into a single executable. Most people will create a\n"
  565. "\tlink to busybox for each function they wish to use and BusyBox\n"
  566. "\twill act like whatever it was invoked as.\n"
  567. "\n"
  568. "Currently defined functions:\n"
  569. );
  570. col = 0;
  571. a = applet_names;
  572. /* prevent last comma to be in the very last pos */
  573. output_width--;
  574. while (*a) {
  575. int len2 = strlen(a) + 2;
  576. if (col >= (int)output_width - len2) {
  577. full_write2_str(",\n");
  578. col = 0;
  579. }
  580. if (col == 0) {
  581. col = 6;
  582. full_write2_str("\t");
  583. } else {
  584. full_write2_str(", ");
  585. }
  586. full_write2_str(a);
  587. col += len2;
  588. a += len2 - 1;
  589. }
  590. full_write2_str("\n\n");
  591. return 0;
  592. }
  593. if (strncmp(argv[1], "--list", 6) == 0) {
  594. unsigned i = 0;
  595. const char *a = applet_names;
  596. dup2(1, 2);
  597. while (*a) {
  598. # if ENABLE_FEATURE_INSTALLER
  599. if (argv[1][6]) /* --list-full? */
  600. full_write2_str(install_dir[APPLET_INSTALL_LOC(i)] + 1);
  601. # endif
  602. full_write2_str(a);
  603. full_write2_str("\n");
  604. i++;
  605. a += strlen(a) + 1;
  606. }
  607. return 0;
  608. }
  609. if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) {
  610. int use_symbolic_links;
  611. const char *busybox;
  612. busybox = xmalloc_readlink(bb_busybox_exec_path);
  613. if (!busybox) {
  614. /* bb_busybox_exec_path is usually "/proc/self/exe".
  615. * In chroot, readlink("/proc/self/exe") usually fails.
  616. * In such case, better use argv[0] as symlink target
  617. * if it is a full path name.
  618. */
  619. if (argv[0][0] != '/')
  620. bb_error_msg_and_die("'%s' is not an absolute path", argv[0]);
  621. busybox = argv[0];
  622. }
  623. /* busybox --install [-s] [DIR]:
  624. * -s: make symlinks
  625. * DIR: directory to install links to
  626. */
  627. use_symbolic_links = (argv[2] && strcmp(argv[2], "-s") == 0 && ++argv);
  628. install_links(busybox, use_symbolic_links, argv[2]);
  629. return 0;
  630. }
  631. if (strcmp(argv[1], "--help") == 0) {
  632. /* "busybox --help [<applet>]" */
  633. if (!argv[2])
  634. goto help;
  635. /* convert to "<applet> --help" */
  636. argv[0] = argv[2];
  637. argv[2] = NULL;
  638. } else {
  639. /* "busybox <applet> arg1 arg2 ..." */
  640. argv++;
  641. }
  642. /* We support "busybox /a/path/to/applet args..." too. Allows for
  643. * "#!/bin/busybox"-style wrappers */
  644. applet_name = bb_get_last_path_component_nostrip(argv[0]);
  645. run_applet_and_exit(applet_name, argv);
  646. /*bb_error_msg_and_die("applet not found"); - sucks in printf */
  647. full_write2_str(applet_name);
  648. full_write2_str(": applet not found\n");
  649. xfunc_die();
  650. }
  651. void FAST_FUNC run_applet_no_and_exit(int applet_no, char **argv)
  652. {
  653. int argc = 1;
  654. while (argv[argc])
  655. argc++;
  656. /* Reinit some shared global data */
  657. xfunc_error_retval = EXIT_FAILURE;
  658. applet_name = APPLET_NAME(applet_no);
  659. if (argc == 2 && strcmp(argv[1], "--help") == 0) {
  660. /* Special case. POSIX says "test --help"
  661. * should be no different from e.g. "test --foo". */
  662. //TODO: just compare applet_no with APPLET_NO_test
  663. if (!ENABLE_TEST || strcmp(applet_name, "test") != 0)
  664. bb_show_usage();
  665. }
  666. if (ENABLE_FEATURE_SUID)
  667. check_suid(applet_no);
  668. exit(applet_main[applet_no](argc, argv));
  669. }
  670. void FAST_FUNC run_applet_and_exit(const char *name, char **argv)
  671. {
  672. int applet = find_applet_by_name(name);
  673. if (applet >= 0)
  674. run_applet_no_and_exit(applet, argv);
  675. if (strncmp(name, "busybox", 7) == 0)
  676. exit(busybox_main(argv));
  677. }
  678. #endif /* !defined(SINGLE_APPLET_MAIN) */
  679. #if ENABLE_BUILD_LIBBUSYBOX
  680. int lbb_main(char **argv)
  681. #else
  682. int main(int argc UNUSED_PARAM, char **argv)
  683. #endif
  684. {
  685. /* Tweak malloc for reduced memory consumption */
  686. #ifdef M_TRIM_THRESHOLD
  687. /* M_TRIM_THRESHOLD is the maximum amount of freed top-most memory
  688. * to keep before releasing to the OS
  689. * Default is way too big: 256k
  690. */
  691. mallopt(M_TRIM_THRESHOLD, 8 * 1024);
  692. #endif
  693. #ifdef M_MMAP_THRESHOLD
  694. /* M_MMAP_THRESHOLD is the request size threshold for using mmap()
  695. * Default is too big: 256k
  696. */
  697. mallopt(M_MMAP_THRESHOLD, 32 * 1024 - 256);
  698. #endif
  699. #if !BB_MMU
  700. /* NOMMU re-exec trick sets high-order bit in first byte of name */
  701. if (argv[0][0] & 0x80) {
  702. re_execed = 1;
  703. argv[0][0] &= 0x7f;
  704. }
  705. #endif
  706. #if defined(SINGLE_APPLET_MAIN)
  707. /* Only one applet is selected in .config */
  708. if (argv[1] && strncmp(argv[0], "busybox", 7) == 0) {
  709. /* "busybox <applet> <params>" should still work as expected */
  710. argv++;
  711. }
  712. /* applet_names in this case is just "applet\0\0" */
  713. lbb_prepare(applet_names IF_FEATURE_INDIVIDUAL(, argv));
  714. return SINGLE_APPLET_MAIN(argc, argv);
  715. #else
  716. lbb_prepare("busybox" IF_FEATURE_INDIVIDUAL(, argv));
  717. applet_name = argv[0];
  718. if (applet_name[0] == '-')
  719. applet_name++;
  720. applet_name = bb_basename(applet_name);
  721. parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */
  722. run_applet_and_exit(applet_name, argv);
  723. /*bb_error_msg_and_die("applet not found"); - sucks in printf */
  724. full_write2_str(applet_name);
  725. full_write2_str(": applet not found\n");
  726. xfunc_die();
  727. #endif
  728. }