appletlib.c 20 KB

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