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