blob: 80adff5e712f688d203efb33e68be4aa1e06d9b5 [file] [log] [blame]
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +00001/* vi: set sw=4 ts=4: */
2/*
Denis Vlasenko724d1962007-10-10 14:41:07 +00003 * Utility routines.
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +00004 *
Denis Vlasenko724d1962007-10-10 14:41:07 +00005 * 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.
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +00008 *
Denis Vlasenko724d1962007-10-10 14:41:07 +00009 * 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.
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000013 */
14
15#include <assert.h>
16#include "busybox.h"
17
18
19/* Declare <applet>_main() */
20#define PROTOTYPES
21#include "applets.h"
22#undef PROTOTYPES
23
24#if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE
25/* Define usage_messages[] */
26static const char usage_messages[] ALIGN1 = ""
27#define MAKE_USAGE
28#include "usage.h"
29#include "applets.h"
30;
31#undef MAKE_USAGE
32#else
33#define usage_messages 0
34#endif /* SHOW_USAGE */
35
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000036
Denis Vlasenko1aa7e472007-11-28 06:49:03 +000037/* Include generated applet names, pointers to <apllet>_main, etc */
38#include "applet_tables.h"
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000039
40
41#if ENABLE_FEATURE_COMPRESS_USAGE
42
43#include "usage_compressed.h"
44#include "unarchive.h"
45
46static const char *unpack_usage_messages(void)
47{
48 char *outbuf = NULL;
49 bunzip_data *bd;
50 int i;
51
52 i = start_bunzip(&bd,
53 /* src_fd: */ -1,
54 /* inbuf: */ packed_usage,
55 /* len: */ sizeof(packed_usage));
56 /* read_bunzip can longjmp to start_bunzip, and ultimately
57 * end up here with i != 0 on read data errors! Not trivial */
58 if (!i) {
59 /* Cannot use xmalloc: will leak bd in NOFORK case! */
60 outbuf = malloc_or_warn(SIZEOF_usage_messages);
61 if (outbuf)
62 read_bunzip(bd, outbuf, SIZEOF_usage_messages);
63 }
64 dealloc_bunzip(bd);
65 return outbuf;
66}
67#define dealloc_usage_messages(s) free(s)
68
69#else
70
71#define unpack_usage_messages() usage_messages
72#define dealloc_usage_messages(s) ((void)(s))
73
74#endif /* FEATURE_COMPRESS_USAGE */
75
76
77void bb_show_usage(void)
78{
79 if (ENABLE_SHOW_USAGE) {
80 const char *format_string;
81 const char *p;
82 const char *usage_string = p = unpack_usage_messages();
Denis Vlasenko1aa7e472007-11-28 06:49:03 +000083 int ap = find_applet_by_name(applet_name);
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000084
Denis Vlasenko1aa7e472007-11-28 06:49:03 +000085 if (ap < 0) /* never happens, paranoia */
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000086 xfunc_die();
87
Denis Vlasenko1aa7e472007-11-28 06:49:03 +000088 while (ap) {
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000089 while (*p++) continue;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +000090 ap--;
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000091 }
92
93 fprintf(stderr, "%s multi-call binary\n", bb_banner);
94 format_string = "\nUsage: %s %s\n\n";
95 if (*p == '\b')
96 format_string = "\nNo help available.\n\n";
97 fprintf(stderr, format_string, applet_name, p);
98 dealloc_usage_messages((char*)usage_string);
99 }
100 xfunc_die();
101}
102
103
Denis Vlasenko745cd172007-11-29 03:31:20 +0000104/* NB: any char pointer will work as well, not necessarily applet_names */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000105static int applet_name_compare(const void *name, const void *v)
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000106{
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000107 int i = (const char *)v - applet_names;
108 return strcmp(name, APPLET_NAME(i));
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000109}
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000110int find_applet_by_name(const char *name)
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000111{
112 /* Do a binary search to find the applet entry given the name. */
Denis Vlasenko745cd172007-11-29 03:31:20 +0000113 const char *p;
114 p = bsearch(name, applet_names, ARRAY_SIZE(applet_main), 1, applet_name_compare);
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000115 if (!p)
116 return -1;
117 return p - applet_names;
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000118}
119
120
121#ifdef __GLIBC__
122/* Make it reside in R/W memory: */
123int *const bb_errno __attribute__ ((section (".data")));
124#endif
125
Denis Vlasenko15cb4a42007-10-11 10:06:26 +0000126void lbb_prepare(const char *applet, char **argv)
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000127{
128#ifdef __GLIBC__
129 (*(int **)&bb_errno) = __errno_location();
130#endif
Denis Vlasenko15cb4a42007-10-11 10:06:26 +0000131 applet_name = applet;
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000132
133 /* Set locale for everybody except 'init' */
134 if (ENABLE_LOCALE_SUPPORT && getpid() != 1)
135 setlocale(LC_ALL, "");
136
Denis Vlasenko82d38da2007-10-10 14:38:47 +0000137#if ENABLE_FEATURE_INDIVIDUAL
138 /* Redundant for busybox (run_applet_and_exit covers that case)
139 * but needed for "individual applet" mode */
Denis Vlasenkod419a9f2007-10-08 20:45:42 +0000140 if (argv[1] && strcmp(argv[1], "--help") == 0)
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000141 bb_show_usage();
Denis Vlasenko82d38da2007-10-10 14:38:47 +0000142#endif
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000143}
Denis Vlasenko724d1962007-10-10 14:41:07 +0000144
145/* The code below can well be in applets/applets.c, as it is used only
146 * for busybox binary, not "individual" binaries.
147 * However, keeping it here and linking it into libbusybox.so
148 * (together with remaining tiny applets/applets.o)
149 * makes it possible to avoid --whole-archive at link time.
150 * This makes (shared busybox) + libbusybox smaller.
151 * (--gc-sections would be even better....)
152 */
153
154const char *applet_name;
155#if !BB_MMU
156bool re_execed;
157#endif
158
159USE_FEATURE_SUID(static uid_t ruid;) /* real uid */
160
161#if ENABLE_FEATURE_SUID_CONFIG
162
163/* applets[] is const, so we have to define this "override" structure */
164static struct BB_suid_config {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000165 int m_applet;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000166 uid_t m_uid;
167 gid_t m_gid;
168 mode_t m_mode;
169 struct BB_suid_config *m_next;
170} *suid_config;
171
172static bool suid_cfg_readable;
173
174/* check if u is member of group g */
175static int ingroup(uid_t u, gid_t g)
176{
177 struct group *grp = getgrgid(g);
178
179 if (grp) {
180 char **mem;
181
182 for (mem = grp->gr_mem; *mem; mem++) {
183 struct passwd *pwd = getpwnam(*mem);
184
185 if (pwd && (pwd->pw_uid == u))
186 return 1;
187 }
188 }
189 return 0;
190}
191
192/* This should probably be a libbb routine. In that case,
193 * I'd probably rename it to something like bb_trimmed_slice.
194 */
195static char *get_trimmed_slice(char *s, char *e)
196{
197 /* First, consider the value at e to be nul and back up until we
198 * reach a non-space char. Set the char after that (possibly at
199 * the original e) to nul. */
200 while (e-- > s) {
201 if (!isspace(*e)) {
202 break;
203 }
204 }
205 e[1] = '\0';
206
207 /* Next, advance past all leading space and return a ptr to the
208 * first non-space char; possibly the terminating nul. */
209 return skip_whitespace(s);
210}
211
212/* Don't depend on the tools to combine strings. */
213static const char config_file[] ALIGN1 = "/etc/busybox.conf";
214
215/* We don't supply a value for the nul, so an index adjustment is
216 * necessary below. Also, we use unsigned short here to save some
217 * space even though these are really mode_t values. */
218static const unsigned short mode_mask[] ALIGN2 = {
219 /* SST sst xxx --- */
220 S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* user */
221 S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* group */
222 0, S_IXOTH, S_IXOTH, 0 /* other */
223};
224
225#define parse_error(x) do { errmsg = x; goto pe_label; } while (0)
226
227static void parse_config_file(void)
228{
229 struct BB_suid_config *sct_head;
230 struct BB_suid_config *sct;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000231 int applet_no;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000232 FILE *f;
233 const char *errmsg;
234 char *s;
235 char *e;
236 int i;
237 unsigned lc;
238 smallint section;
239 char buffer[256];
240 struct stat st;
241
242 assert(!suid_config); /* Should be set to NULL by bss init. */
243
244 ruid = getuid();
245 if (ruid == 0) /* run by root - don't need to even read config file */
246 return;
247
248 if ((stat(config_file, &st) != 0) /* No config file? */
249 || !S_ISREG(st.st_mode) /* Not a regular file? */
250 || (st.st_uid != 0) /* Not owned by root? */
251 || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
252 || !(f = fopen(config_file, "r")) /* Cannot open? */
253 ) {
254 return;
255 }
256
257 suid_cfg_readable = 1;
258 sct_head = NULL;
259 section = lc = 0;
260
261 while (1) {
262 s = buffer;
263
264 if (!fgets(s, sizeof(buffer), f)) { /* Are we done? */
265 if (ferror(f)) { /* Make sure it wasn't a read error. */
266 parse_error("reading");
267 }
268 fclose(f);
269 suid_config = sct_head; /* Success, so set the pointer. */
270 return;
271 }
272
273 lc++; /* Got a (partial) line. */
274
275 /* If a line is too long for our buffer, we consider it an error.
276 * The following test does mistreat one corner case though.
277 * If the final line of the file does not end with a newline and
278 * yet exactly fills the buffer, it will be treated as too long
279 * even though there isn't really a problem. But it isn't really
280 * worth adding code to deal with such an unlikely situation, and
281 * we do err on the side of caution. Besides, the line would be
282 * too long if it did end with a newline. */
283 if (!strchr(s, '\n') && !feof(f)) {
284 parse_error("line too long");
285 }
286
287 /* Trim leading and trailing whitespace, ignoring comments, and
288 * check if the resulting string is empty. */
289 s = get_trimmed_slice(s, strchrnul(s, '#'));
290 if (!*s) {
291 continue;
292 }
293
294 /* Check for a section header. */
295
296 if (*s == '[') {
297 /* Unlike the old code, we ignore leading and trailing
298 * whitespace for the section name. We also require that
299 * there are no stray characters after the closing bracket. */
300 e = strchr(s, ']');
301 if (!e /* Missing right bracket? */
302 || e[1] /* Trailing characters? */
303 || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
304 ) {
305 parse_error("section header");
306 }
307 /* Right now we only have one section so just check it.
308 * If more sections are added in the future, please don't
309 * resort to cascading ifs with multiple strcasecmp calls.
310 * That kind of bloated code is all too common. A loop
311 * and a string table would be a better choice unless the
312 * number of sections is very small. */
313 if (strcasecmp(s, "SUID") == 0) {
314 section = 1;
315 continue;
316 }
317 section = -1; /* Unknown section so set to skip. */
318 continue;
319 }
320
321 /* Process sections. */
322
323 if (section == 1) { /* SUID */
324 /* Since we trimmed leading and trailing space above, we're
325 * now looking for strings of the form
326 * <key>[::space::]*=[::space::]*<value>
327 * where both key and value could contain inner whitespace. */
328
329 /* First get the key (an applet name in our case). */
330 e = strchr(s, '=');
331 if (e) {
332 s = get_trimmed_slice(s, e);
333 }
334 if (!e || !*s) { /* Missing '=' or empty key. */
335 parse_error("keyword");
336 }
337
338 /* Ok, we have an applet name. Process the rhs if this
339 * applet is currently built in and ignore it otherwise.
340 * Note: this can hide config file bugs which only pop
341 * up when the busybox configuration is changed. */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000342 applet_no = find_applet_by_name(s);
343 if (applet_no >= 0) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000344 /* Note: We currently don't check for duplicates!
345 * The last config line for each applet will be the
346 * one used since we insert at the head of the list.
347 * I suppose this could be considered a feature. */
348 sct = xmalloc(sizeof(struct BB_suid_config));
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000349 sct->m_applet = applet_no;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000350 sct->m_mode = 0;
351 sct->m_next = sct_head;
352 sct_head = sct;
353
354 /* Get the specified mode. */
355
356 e = skip_whitespace(e+1);
357
358 for (i = 0; i < 3; i++) {
359 /* There are 4 chars + 1 nul for each of user/group/other. */
360 static const char mode_chars[] ALIGN1 = "Ssx-\0" "Ssx-\0" "Ttx-";
361
362 const char *q;
363 q = strchrnul(mode_chars + 5*i, *e++);
364 if (!*q) {
365 parse_error("mode");
366 }
367 /* Adjust by -i to account for nul. */
368 sct->m_mode |= mode_mask[(q - mode_chars) - i];
369 }
370
371 /* Now get the the user/group info. */
372
373 s = skip_whitespace(e);
374
375 /* Note: we require whitespace between the mode and the
376 * user/group info. */
377 if ((s == e) || !(e = strchr(s, '.'))) {
378 parse_error("<uid>.<gid>");
379 }
380 *e++ = '\0';
381
382 /* We can't use get_ug_id here since it would exit()
383 * if a uid or gid was not found. Oh well... */
384 sct->m_uid = bb_strtoul(s, NULL, 10);
385 if (errno) {
386 struct passwd *pwd = getpwnam(s);
387 if (!pwd) {
388 parse_error("user");
389 }
390 sct->m_uid = pwd->pw_uid;
391 }
392
393 sct->m_gid = bb_strtoul(e, NULL, 10);
394 if (errno) {
395 struct group *grp;
396 grp = getgrnam(e);
397 if (!grp) {
398 parse_error("group");
399 }
400 sct->m_gid = grp->gr_gid;
401 }
402 }
403 continue;
404 }
405
406 /* Unknown sections are ignored. */
407
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
417 } /* while (1) */
418
419 pe_label:
420 fprintf(stderr, "Parse error in %s, line %d: %s\n",
421 config_file, lc, errmsg);
422
423 fclose(f);
424 /* Release any allocated memory before returning. */
425 while (sct_head) {
426 sct = sct_head->m_next;
427 free(sct_head);
428 sct_head = sct;
429 }
430}
431#else
432static inline void parse_config_file(void)
433{
434 USE_FEATURE_SUID(ruid = getuid();)
435}
436#endif /* FEATURE_SUID_CONFIG */
437
438
439#if ENABLE_FEATURE_SUID
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000440static void check_suid(int applet_no)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000441{
442 gid_t rgid; /* real gid */
443
444 if (ruid == 0) /* set by parse_config_file() */
445 return; /* run by root - no need to check more */
446 rgid = getgid();
447
448#if ENABLE_FEATURE_SUID_CONFIG
449 if (suid_cfg_readable) {
450 uid_t uid;
451 struct BB_suid_config *sct;
452 mode_t m;
453
454 for (sct = suid_config; sct; sct = sct->m_next) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000455 if (sct->m_applet == applet_no)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000456 goto found;
457 }
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000458 goto check_need_suid;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000459 found:
460 m = sct->m_mode;
461 if (sct->m_uid == ruid)
462 /* same uid */
463 m >>= 6;
464 else if ((sct->m_gid == rgid) || ingroup(ruid, sct->m_gid))
465 /* same group / in group */
466 m >>= 3;
467
468 if (!(m & S_IXOTH)) /* is x bit not set ? */
469 bb_error_msg_and_die("you have no permission to run this applet!");
470
471 /* _both_ sgid and group_exec have to be set for setegid */
472 if ((sct->m_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
473 rgid = sct->m_gid;
474 /* else (no setegid) we will set egid = rgid */
475
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
481 /* do we have to set effective uid? */
482 uid = ruid;
483 if (sct->m_mode & S_ISUID)
484 uid = sct->m_uid;
485 /* else (no seteuid) we will set euid = ruid */
486
487 if (setresuid(-1, uid, uid))
488 bb_perror_msg_and_die("setresuid");
489 return;
490 }
491#if !ENABLE_FEATURE_SUID_CONFIG_QUIET
492 {
493 static bool onetime = 0;
494
495 if (!onetime) {
496 onetime = 1;
497 fprintf(stderr, "Using fallback suid method\n");
498 }
499 }
500#endif
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000501 check_need_suid:
Denis Vlasenko724d1962007-10-10 14:41:07 +0000502#endif
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000503 if (APPLET_SUID(applet_no) == _BB_SUID_ALWAYS) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000504 /* Real uid is not 0. If euid isn't 0 too, suid bit
505 * is most probably not set on our executable */
506 if (geteuid())
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000507 bb_error_msg_and_die("must be suid to work properly");
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000508 } else if (APPLET_SUID(applet_no) == _BB_SUID_NEVER) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000509 xsetgid(rgid); /* drop all privileges */
510 xsetuid(ruid);
511 }
512}
513#else
514#define check_suid(x) ((void)0)
515#endif /* FEATURE_SUID */
516
517
518#if ENABLE_FEATURE_INSTALLER
519/* create (sym)links for each applet */
520static void install_links(const char *busybox, int use_symbolic_links)
521{
522 /* directory table
523 * this should be consistent w/ the enum,
524 * busybox.h::bb_install_loc_t, or else... */
525 static const char usr_bin [] ALIGN1 = "/usr/bin";
526 static const char usr_sbin[] ALIGN1 = "/usr/sbin";
527 static const char *const install_dir[] = {
528 &usr_bin [8], /* "", equivalent to "/" for concat_path_file() */
529 &usr_bin [4], /* "/bin" */
530 &usr_sbin[4], /* "/sbin" */
531 usr_bin,
532 usr_sbin
533 };
534
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000535 int (*lf)(const char *, const char *);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000536 char *fpc;
537 int i;
538 int rc;
539
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000540 lf = link;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000541 if (use_symbolic_links)
542 lf = symlink;
543
Denis Vlasenko745cd172007-11-29 03:31:20 +0000544 for (i = 0; i < ARRAY_SIZE(applet_main); i++) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000545 fpc = concat_path_file(
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000546 install_dir[APPLET_INSTALL_LOC(i)],
547 APPLET_NAME(i));
Denis Vlasenko745cd172007-11-29 03:31:20 +0000548 // debug: bb_error_msg("%slinking %s to busybox",
549 // use_symbolic_links ? "sym" : "", fpc);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000550 rc = lf(busybox, fpc);
551 if (rc != 0 && errno != EEXIST) {
552 bb_simple_perror_msg(fpc);
553 }
554 free(fpc);
555 }
556}
557#else
558#define install_links(x,y) ((void)0)
559#endif /* FEATURE_INSTALLER */
560
561/* If we were called as "busybox..." */
562static int busybox_main(char **argv)
563{
564 if (!argv[1]) {
565 /* Called without arguments */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000566 const char *a;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000567 int col, output_width;
568 help:
569 output_width = 80;
570 if (ENABLE_FEATURE_AUTOWIDTH) {
571 /* Obtain the terminal width */
572 get_terminal_width_height(0, &output_width, NULL);
573 }
574 /* leading tab and room to wrap */
575 output_width -= sizeof("start-stop-daemon, ") + 8;
576
577 printf("%s multi-call binary\n", bb_banner); /* reuse const string... */
Denis Vlasenkofcfb5c02007-12-24 12:16:24 +0000578 printf("Copyright (C) 1998-2007 Erik Andersen, Rob Landley, Denys Vlasenko\n"
579 "and others. Licensed under GPLv2.\n"
580 "See source distribution for full notice.\n"
Denis Vlasenko724d1962007-10-10 14:41:07 +0000581 "\n"
582 "Usage: busybox [function] [arguments]...\n"
Denis Vlasenkofcfb5c02007-12-24 12:16:24 +0000583 " or: function [arguments]...\n"
Denis Vlasenko724d1962007-10-10 14:41:07 +0000584 "\n"
585 "\tBusyBox is a multi-call binary that combines many common Unix\n"
586 "\tutilities into a single executable. Most people will create a\n"
587 "\tlink to busybox for each function they wish to use and BusyBox\n"
588 "\twill act like whatever it was invoked as!\n"
Denis Vlasenkofcfb5c02007-12-24 12:16:24 +0000589 "\n"
590 "Currently defined functions:\n");
Denis Vlasenko724d1962007-10-10 14:41:07 +0000591 col = 0;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000592 a = applet_names;
593 while (*a) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000594 if (col > output_width) {
595 puts(",");
596 col = 0;
597 }
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000598 col += printf("%s%s", (col ? ", " : "\t"), a);
599 a += strlen(a) + 1;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000600 }
601 puts("\n");
602 return 0;
603 }
604
605 if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) {
606 const char *busybox;
607 busybox = xmalloc_readlink(bb_busybox_exec_path);
608 if (!busybox)
609 busybox = bb_busybox_exec_path;
610 /* -s makes symlinks */
611 install_links(busybox, argv[2] && strcmp(argv[2], "-s") == 0);
612 return 0;
613 }
614
615 if (strcmp(argv[1], "--help") == 0) {
616 /* "busybox --help [<applet>]" */
617 if (!argv[2])
618 goto help;
619 /* convert to "<applet> --help" */
620 argv[0] = argv[2];
621 argv[2] = NULL;
622 } else {
623 /* "busybox <applet> arg1 arg2 ..." */
624 argv++;
625 }
626 /* We support "busybox /a/path/to/applet args..." too. Allows for
627 * "#!/bin/busybox"-style wrappers */
628 applet_name = bb_get_last_path_component_nostrip(argv[0]);
629 run_applet_and_exit(applet_name, argv);
630 bb_error_msg_and_die("applet not found");
631}
632
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000633void run_applet_no_and_exit(int applet_no, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000634{
635 int argc = 1;
636
637 while (argv[argc])
638 argc++;
639
640 /* Reinit some shared global data */
641 optind = 1;
642 xfunc_error_retval = EXIT_FAILURE;
643
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000644 applet_name = APPLET_NAME(applet_no);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000645 if (argc == 2 && !strcmp(argv[1], "--help"))
646 bb_show_usage();
647 if (ENABLE_FEATURE_SUID)
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000648 check_suid(applet_no);
Denis Vlasenko745cd172007-11-29 03:31:20 +0000649 exit(applet_main[applet_no](argc, argv));
Denis Vlasenko724d1962007-10-10 14:41:07 +0000650}
651
652void run_applet_and_exit(const char *name, char **argv)
653{
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000654 int applet = find_applet_by_name(name);
655 if (applet >= 0)
656 run_applet_no_and_exit(applet, argv);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000657 if (!strncmp(name, "busybox", 7))
658 exit(busybox_main(argv));
659}
660
661
662#if ENABLE_BUILD_LIBBUSYBOX
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000663int lbb_main(int argc, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000664#else
665int main(int argc, char **argv)
666#endif
667{
Denis Vlasenko15cb4a42007-10-11 10:06:26 +0000668 lbb_prepare("busybox", argv);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000669
670#if !BB_MMU
671 /* NOMMU re-exec trick sets high-order bit in first byte of name */
672 if (argv[0][0] & 0x80) {
673 re_execed = 1;
674 argv[0][0] &= 0x7f;
675 }
676#endif
677 applet_name = argv[0];
678 if (applet_name[0] == '-')
679 applet_name++;
680 applet_name = bb_basename(applet_name);
681
682 parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */
683
684 run_applet_and_exit(applet_name, argv);
685 bb_error_msg_and_die("applet not found");
686}