appletlib.c 22 KB

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