appletlib.c 21 KB

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