appletlib.c 21 KB

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