appletlib.c 22 KB

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