blob: ed60a1a0aff4c363fb51826341f578de019e87d2 [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
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Permission has been granted to redistribute this code under GPL.
Denis Vlasenko724d1962007-10-10 14:41:07 +000011 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020012 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000013 */
14
Denis Vlasenko79cedcb2008-04-08 21:13:28 +000015/* 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 */
Denys Vlasenkoc1947f12009-10-23 01:30:26 +020029#include "busybox.h"
Denys Vlasenko9be47022011-05-16 12:21:31 +020030
Denys Vlasenko8da415e2010-12-05 01:30:14 +010031#if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
32 || defined(__APPLE__) \
33 )
34# include <malloc.h> /* for mallopt */
35#endif
Denys Vlasenko9be47022011-05-16 12:21:31 +020036
Denys Vlasenko3d0e7792010-03-29 11:02:00 +020037/* Try to pull in PAGE_SIZE */
38#ifdef __linux__
39# include <sys/user.h>
40#endif
41#ifdef __GNU__ /* Hurd */
42# include <mach/vm_param.h>
43#endif
Denys Vlasenko9be47022011-05-16 12:21:31 +020044#ifndef PAGE_SIZE
45# define PAGE_SIZE (4*1024) /* guess */
46#endif
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000047
48
49/* Declare <applet>_main() */
50#define PROTOTYPES
51#include "applets.h"
52#undef PROTOTYPES
53
Denis Vlasenko32b2a9f2008-02-22 22:43:22 +000054/* Include generated applet names, pointers to <applet>_main, etc */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +000055#include "applet_tables.h"
Denis Vlasenko468aea22008-04-01 14:47:57 +000056/* ...and if applet_tables generator says we have only one applet... */
57#ifdef SINGLE_APPLET_MAIN
Denys Vlasenko0e5ba082010-06-05 23:11:07 +020058# undef ENABLE_FEATURE_INDIVIDUAL
59# define ENABLE_FEATURE_INDIVIDUAL 1
60# undef IF_FEATURE_INDIVIDUAL
61# define IF_FEATURE_INDIVIDUAL(...) __VA_ARGS__
Denis Vlasenko468aea22008-04-01 14:47:57 +000062#endif
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000063
Denys Vlasenko0e5ba082010-06-05 23:11:07 +020064#include "usage_compressed.h"
65
Denys Vlasenko9be47022011-05-16 12:21:31 +020066
Denys Vlasenko0e5ba082010-06-05 23:11:07 +020067#if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE
68static const char usage_messages[] ALIGN1 = UNPACKED_USAGE;
69#else
70# define usage_messages 0
Denys Vlasenko1fcbff22010-06-26 02:40:08 +020071#endif
Denys Vlasenko0e5ba082010-06-05 23:11:07 +020072
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000073#if ENABLE_FEATURE_COMPRESS_USAGE
74
Denys Vlasenko5c296de2010-07-03 14:28:35 +020075static const char packed_usage[] ALIGN1 = { PACKED_USAGE };
Denys Vlasenko833d4e72010-11-03 02:38:31 +010076# include "archive.h"
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000077static const char *unpack_usage_messages(void)
78{
79 char *outbuf = NULL;
80 bunzip_data *bd;
81 int i;
82
83 i = start_bunzip(&bd,
84 /* src_fd: */ -1,
Denys Vlasenkocaddfc82010-10-28 23:08:53 +020085 /* inbuf: */ packed_usage,
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000086 /* len: */ sizeof(packed_usage));
87 /* read_bunzip can longjmp to start_bunzip, and ultimately
88 * end up here with i != 0 on read data errors! Not trivial */
89 if (!i) {
90 /* Cannot use xmalloc: will leak bd in NOFORK case! */
Denys Vlasenko0e5ba082010-06-05 23:11:07 +020091 outbuf = malloc_or_warn(sizeof(UNPACKED_USAGE));
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000092 if (outbuf)
Denys Vlasenko0e5ba082010-06-05 23:11:07 +020093 read_bunzip(bd, outbuf, sizeof(UNPACKED_USAGE));
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000094 }
95 dealloc_bunzip(bd);
96 return outbuf;
97}
Denys Vlasenko0e5ba082010-06-05 23:11:07 +020098# define dealloc_usage_messages(s) free(s)
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +000099
100#else
101
Denys Vlasenko0e5ba082010-06-05 23:11:07 +0200102# define unpack_usage_messages() usage_messages
103# define dealloc_usage_messages(s) ((void)(s))
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000104
105#endif /* FEATURE_COMPRESS_USAGE */
106
107
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000108void FAST_FUNC bb_show_usage(void)
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000109{
110 if (ENABLE_SHOW_USAGE) {
Denis Vlasenko468aea22008-04-01 14:47:57 +0000111#ifdef SINGLE_APPLET_STR
112 /* Imagine that this applet is "true". Dont suck in printf! */
Lauri Kasanen2b662c52010-11-13 23:16:05 +0100113 const char *usage_string = unpack_usage_messages();
Denis Vlasenko468aea22008-04-01 14:47:57 +0000114
Lauri Kasanen2b662c52010-11-13 23:16:05 +0100115 if (*usage_string == '\b') {
Denis Vlasenko42404082008-11-24 13:42:24 +0000116 full_write2_str("No help available.\n\n");
Denis Vlasenko468aea22008-04-01 14:47:57 +0000117 } else {
Denis Vlasenko42404082008-11-24 13:42:24 +0000118 full_write2_str("Usage: "SINGLE_APPLET_STR" ");
Lauri Kasanen2b662c52010-11-13 23:16:05 +0100119 full_write2_str(usage_string);
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000120 full_write2_str("\n\n");
Denis Vlasenko468aea22008-04-01 14:47:57 +0000121 }
Denys Vlasenko630dde12009-08-30 19:57:49 +0200122 if (ENABLE_FEATURE_CLEAN_UP)
123 dealloc_usage_messages((char*)usage_string);
Denis Vlasenko468aea22008-04-01 14:47:57 +0000124#else
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000125 const char *p;
126 const char *usage_string = p = unpack_usage_messages();
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000127 int ap = find_applet_by_name(applet_name);
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000128
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000129 if (ap < 0) /* never happens, paranoia */
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000130 xfunc_die();
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000131 while (ap) {
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000132 while (*p++) continue;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000133 ap--;
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000134 }
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000135 full_write2_str(bb_banner);
Denys Vlasenko630dde12009-08-30 19:57:49 +0200136 full_write2_str(" multi-call binary.\n");
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000137 if (*p == '\b')
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000138 full_write2_str("\nNo help available.\n\n");
139 else {
140 full_write2_str("\nUsage: ");
141 full_write2_str(applet_name);
142 full_write2_str(" ");
143 full_write2_str(p);
144 full_write2_str("\n\n");
145 }
Denys Vlasenko630dde12009-08-30 19:57:49 +0200146 if (ENABLE_FEATURE_CLEAN_UP)
147 dealloc_usage_messages((char*)usage_string);
Denis Vlasenko468aea22008-04-01 14:47:57 +0000148#endif
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000149 }
150 xfunc_die();
151}
152
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000153#if NUM_APPLETS > 8
Denis Vlasenko745cd172007-11-29 03:31:20 +0000154/* NB: any char pointer will work as well, not necessarily applet_names */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000155static int applet_name_compare(const void *name, const void *v)
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000156{
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000157 int i = (const char *)v - applet_names;
158 return strcmp(name, APPLET_NAME(i));
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000159}
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000160#endif
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000161int FAST_FUNC find_applet_by_name(const char *name)
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000162{
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000163#if NUM_APPLETS > 8
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000164 /* Do a binary search to find the applet entry given the name. */
Denis Vlasenko745cd172007-11-29 03:31:20 +0000165 const char *p;
166 p = bsearch(name, applet_names, ARRAY_SIZE(applet_main), 1, applet_name_compare);
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000167 if (!p)
168 return -1;
169 return p - applet_names;
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000170#else
171 /* A version which does not pull in bsearch */
172 int i = 0;
173 const char *p = applet_names;
174 while (i < NUM_APPLETS) {
175 if (strcmp(name, p) == 0)
176 return i;
177 p += strlen(p) + 1;
178 i++;
179 }
180 return -1;
181#endif
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000182}
183
184
Denis Vlasenko68404f12008-03-17 09:00:54 +0000185void lbb_prepare(const char *applet
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000186 IF_FEATURE_INDIVIDUAL(, char **argv))
Denis Vlasenko468aea22008-04-01 14:47:57 +0000187 MAIN_EXTERNALLY_VISIBLE;
188void lbb_prepare(const char *applet
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000189 IF_FEATURE_INDIVIDUAL(, char **argv))
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000190{
191#ifdef __GLIBC__
192 (*(int **)&bb_errno) = __errno_location();
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000193 barrier();
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000194#endif
Denis Vlasenko15cb4a42007-10-11 10:06:26 +0000195 applet_name = applet;
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000196
197 /* Set locale for everybody except 'init' */
198 if (ENABLE_LOCALE_SUPPORT && getpid() != 1)
199 setlocale(LC_ALL, "");
200
Denis Vlasenko82d38da2007-10-10 14:38:47 +0000201#if ENABLE_FEATURE_INDIVIDUAL
202 /* Redundant for busybox (run_applet_and_exit covers that case)
203 * but needed for "individual applet" mode */
Denys Vlasenko9297dbc2010-07-05 21:37:12 +0200204 if (argv[1]
205 && !argv[2]
206 && strcmp(argv[1], "--help") == 0
207 && strncmp(applet, "busybox", 7) != 0
208 ) {
Denis Vlasenkobd28f6b2008-07-19 08:15:13 +0000209 /* Special case. POSIX says "test --help"
210 * should be no different from e.g. "test --foo". */
211 if (!ENABLE_TEST || strcmp(applet_name, "test") != 0)
212 bb_show_usage();
213 }
Denis Vlasenko82d38da2007-10-10 14:38:47 +0000214#endif
Denis Vlasenkoac7d0e32007-10-08 19:32:12 +0000215}
Denis Vlasenko724d1962007-10-10 14:41:07 +0000216
217/* The code below can well be in applets/applets.c, as it is used only
218 * for busybox binary, not "individual" binaries.
219 * However, keeping it here and linking it into libbusybox.so
220 * (together with remaining tiny applets/applets.o)
221 * makes it possible to avoid --whole-archive at link time.
222 * This makes (shared busybox) + libbusybox smaller.
223 * (--gc-sections would be even better....)
224 */
225
226const char *applet_name;
227#if !BB_MMU
228bool re_execed;
229#endif
230
Denis Vlasenko468aea22008-04-01 14:47:57 +0000231
Denis Vlasenko10f6fb12008-04-29 00:10:27 +0000232/* If not built as a single-applet executable... */
233#if !defined(SINGLE_APPLET_MAIN)
Denis Vlasenko468aea22008-04-01 14:47:57 +0000234
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000235IF_FEATURE_SUID(static uid_t ruid;) /* real uid */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000236
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100237# if ENABLE_FEATURE_SUID_CONFIG
Denis Vlasenko724d1962007-10-10 14:41:07 +0000238
Denys Vlasenko9be47022011-05-16 12:21:31 +0200239static struct suid_config_t {
240 /* next ptr must be first: this struct needs to be llist-compatible */
241 struct suid_config_t *m_next;
Denys Vlasenko4566e172011-05-16 00:01:08 +0200242 struct bb_uidgid_t m_ugid;
Denys Vlasenko9be47022011-05-16 12:21:31 +0200243 int m_applet;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000244 mode_t m_mode;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000245} *suid_config;
246
247static bool suid_cfg_readable;
248
249/* check if u is member of group g */
250static int ingroup(uid_t u, gid_t g)
251{
252 struct group *grp = getgrgid(g);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000253 if (grp) {
254 char **mem;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000255 for (mem = grp->gr_mem; *mem; mem++) {
256 struct passwd *pwd = getpwnam(*mem);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000257 if (pwd && (pwd->pw_uid == u))
258 return 1;
259 }
260 }
261 return 0;
262}
263
264/* This should probably be a libbb routine. In that case,
265 * I'd probably rename it to something like bb_trimmed_slice.
266 */
267static char *get_trimmed_slice(char *s, char *e)
268{
269 /* First, consider the value at e to be nul and back up until we
270 * reach a non-space char. Set the char after that (possibly at
271 * the original e) to nul. */
272 while (e-- > s) {
273 if (!isspace(*e)) {
274 break;
275 }
276 }
277 e[1] = '\0';
278
279 /* Next, advance past all leading space and return a ptr to the
280 * first non-space char; possibly the terminating nul. */
281 return skip_whitespace(s);
282}
283
Denis Vlasenko724d1962007-10-10 14:41:07 +0000284static void parse_config_file(void)
285{
Denys Vlasenko3770b6b2011-05-16 13:19:25 +0200286 /* Don't depend on the tools to combine strings. */
287 static const char config_file[] ALIGN1 = "/etc/busybox.conf";
288
Denys Vlasenko9be47022011-05-16 12:21:31 +0200289 struct suid_config_t *sct_head;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000290 int applet_no;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000291 FILE *f;
292 const char *errmsg;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000293 unsigned lc;
294 smallint section;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000295 struct stat st;
296
Denis Vlasenko724d1962007-10-10 14:41:07 +0000297 ruid = getuid();
298 if (ruid == 0) /* run by root - don't need to even read config file */
299 return;
300
301 if ((stat(config_file, &st) != 0) /* No config file? */
302 || !S_ISREG(st.st_mode) /* Not a regular file? */
303 || (st.st_uid != 0) /* Not owned by root? */
304 || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
Denys Vlasenko4566e172011-05-16 00:01:08 +0200305 || !(f = fopen_for_read(config_file)) /* Cannot open? */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000306 ) {
307 return;
308 }
309
310 suid_cfg_readable = 1;
311 sct_head = NULL;
312 section = lc = 0;
313
314 while (1) {
Denys Vlasenko9be47022011-05-16 12:21:31 +0200315 char buffer[256];
316 char *s;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000317
Denys Vlasenko9be47022011-05-16 12:21:31 +0200318 if (!fgets(buffer, sizeof(buffer), f)) { /* Are we done? */
Denys Vlasenko4566e172011-05-16 00:01:08 +0200319 // Looks like bloat
320 //if (ferror(f)) { /* Make sure it wasn't a read error. */
321 // errmsg = "reading";
322 // goto pe_label;
323 //}
Denis Vlasenko724d1962007-10-10 14:41:07 +0000324 fclose(f);
325 suid_config = sct_head; /* Success, so set the pointer. */
326 return;
327 }
328
Denys Vlasenko9be47022011-05-16 12:21:31 +0200329 s = buffer;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000330 lc++; /* Got a (partial) line. */
331
332 /* If a line is too long for our buffer, we consider it an error.
333 * The following test does mistreat one corner case though.
334 * If the final line of the file does not end with a newline and
335 * yet exactly fills the buffer, it will be treated as too long
336 * even though there isn't really a problem. But it isn't really
337 * worth adding code to deal with such an unlikely situation, and
338 * we do err on the side of caution. Besides, the line would be
339 * too long if it did end with a newline. */
340 if (!strchr(s, '\n') && !feof(f)) {
Denys Vlasenko4566e172011-05-16 00:01:08 +0200341 errmsg = "line too long";
342 goto pe_label;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000343 }
344
345 /* Trim leading and trailing whitespace, ignoring comments, and
346 * check if the resulting string is empty. */
347 s = get_trimmed_slice(s, strchrnul(s, '#'));
348 if (!*s) {
349 continue;
350 }
351
352 /* Check for a section header. */
353
354 if (*s == '[') {
355 /* Unlike the old code, we ignore leading and trailing
356 * whitespace for the section name. We also require that
357 * there are no stray characters after the closing bracket. */
Denys Vlasenko9be47022011-05-16 12:21:31 +0200358 char *e = strchr(s, ']');
Denis Vlasenko724d1962007-10-10 14:41:07 +0000359 if (!e /* Missing right bracket? */
360 || e[1] /* Trailing characters? */
361 || !*(s = get_trimmed_slice(s+1, e)) /* Missing name? */
362 ) {
Denys Vlasenko4566e172011-05-16 00:01:08 +0200363 errmsg = "section header";
364 goto pe_label;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000365 }
366 /* Right now we only have one section so just check it.
367 * If more sections are added in the future, please don't
368 * resort to cascading ifs with multiple strcasecmp calls.
369 * That kind of bloated code is all too common. A loop
370 * and a string table would be a better choice unless the
371 * number of sections is very small. */
372 if (strcasecmp(s, "SUID") == 0) {
373 section = 1;
374 continue;
375 }
376 section = -1; /* Unknown section so set to skip. */
377 continue;
378 }
379
380 /* Process sections. */
381
382 if (section == 1) { /* SUID */
383 /* Since we trimmed leading and trailing space above, we're
384 * now looking for strings of the form
385 * <key>[::space::]*=[::space::]*<value>
386 * where both key and value could contain inner whitespace. */
387
388 /* First get the key (an applet name in our case). */
Denys Vlasenko9be47022011-05-16 12:21:31 +0200389 char *e = strchr(s, '=');
Denis Vlasenko724d1962007-10-10 14:41:07 +0000390 if (e) {
391 s = get_trimmed_slice(s, e);
392 }
393 if (!e || !*s) { /* Missing '=' or empty key. */
Denys Vlasenko4566e172011-05-16 00:01:08 +0200394 errmsg = "keyword";
395 goto pe_label;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000396 }
397
398 /* Ok, we have an applet name. Process the rhs if this
399 * applet is currently built in and ignore it otherwise.
400 * Note: this can hide config file bugs which only pop
401 * up when the busybox configuration is changed. */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000402 applet_no = find_applet_by_name(s);
403 if (applet_no >= 0) {
Denys Vlasenko3770b6b2011-05-16 13:19:25 +0200404 unsigned i;
Denys Vlasenko9be47022011-05-16 12:21:31 +0200405 struct suid_config_t *sct;
406
Denis Vlasenko724d1962007-10-10 14:41:07 +0000407 /* 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. */
Denys Vlasenko4566e172011-05-16 00:01:08 +0200411 sct = xzalloc(sizeof(*sct));
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000412 sct->m_applet = applet_no;
Denys Vlasenko4566e172011-05-16 00:01:08 +0200413 /*sct->m_mode = 0;*/
Denis Vlasenko724d1962007-10-10 14:41:07 +0000414 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++) {
Denys Vlasenko3770b6b2011-05-16 13:19:25 +0200422 /* There are 4 chars for each of user/group/other.
423 * "x-xx" instead of "x-" are to make
424 * "idx > 3" check catch invalid chars.
425 */
426 static const char mode_chars[] ALIGN1 = "Ssx-" "Ssx-" "x-xx";
427 static const unsigned short mode_mask[] ALIGN2 = {
428 S_ISUID, S_ISUID|S_IXUSR, S_IXUSR, 0, /* Ssx- */
429 S_ISGID, S_ISGID|S_IXGRP, S_IXGRP, 0, /* Ssx- */
430 S_IXOTH, 0 /* x- */
431 };
432 const char *q = strchrnul(mode_chars + 4*i, *e);
433 unsigned idx = q - (mode_chars + 4*i);
434 if (idx > 3) {
Denys Vlasenko4566e172011-05-16 00:01:08 +0200435 errmsg = "mode";
436 goto pe_label;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000437 }
Denys Vlasenko3770b6b2011-05-16 13:19:25 +0200438 sct->m_mode |= mode_mask[q - mode_chars];
439 e++;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000440 }
441
Marek Polacekb0b88842011-04-16 17:33:43 +0200442 /* Now get the user/group info. */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000443
444 s = skip_whitespace(e);
445
446 /* Note: we require whitespace between the mode and the
447 * user/group info. */
448 if ((s == e) || !(e = strchr(s, '.'))) {
Denys Vlasenko4566e172011-05-16 00:01:08 +0200449 errmsg = "uid.gid";
450 goto pe_label;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000451 }
452
Denys Vlasenko9be47022011-05-16 12:21:31 +0200453 *e = ':'; /* get_uidgid doesn't understand user.group */
Denys Vlasenko4566e172011-05-16 00:01:08 +0200454 if (get_uidgid(&sct->m_ugid, s, /*allow_numeric:*/ 1) == 0) {
455 errmsg = "unknown user/group";
456 goto pe_label;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000457 }
458 }
459 continue;
460 }
461
462 /* Unknown sections are ignored. */
463
464 /* Encountering configuration lines prior to seeing a
465 * section header is treated as an error. This is how
466 * the old code worked, but it may not be desirable.
467 * We may want to simply ignore such lines in case they
468 * are used in some future version of busybox. */
469 if (!section) {
Denys Vlasenko4566e172011-05-16 00:01:08 +0200470 errmsg = "keyword outside section";
471 goto pe_label;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000472 }
473
474 } /* while (1) */
475
476 pe_label:
Denys Vlasenko9be47022011-05-16 12:21:31 +0200477 fclose(f);
Denys Vlasenko4566e172011-05-16 00:01:08 +0200478 bb_error_msg("parse error in %s, line %u: %s", config_file, lc, errmsg);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000479
Denis Vlasenko724d1962007-10-10 14:41:07 +0000480 /* Release any allocated memory before returning. */
Denys Vlasenko9be47022011-05-16 12:21:31 +0200481 llist_free((llist_t*)sct_head, NULL);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000482}
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100483# else
Denis Vlasenko724d1962007-10-10 14:41:07 +0000484static inline void parse_config_file(void)
485{
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000486 IF_FEATURE_SUID(ruid = getuid();)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000487}
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100488# endif /* FEATURE_SUID_CONFIG */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000489
490
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100491# if ENABLE_FEATURE_SUID
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000492static void check_suid(int applet_no)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000493{
494 gid_t rgid; /* real gid */
495
496 if (ruid == 0) /* set by parse_config_file() */
497 return; /* run by root - no need to check more */
498 rgid = getgid();
499
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100500# if ENABLE_FEATURE_SUID_CONFIG
Denis Vlasenko724d1962007-10-10 14:41:07 +0000501 if (suid_cfg_readable) {
502 uid_t uid;
Denys Vlasenko9be47022011-05-16 12:21:31 +0200503 struct suid_config_t *sct;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000504 mode_t m;
505
506 for (sct = suid_config; sct; sct = sct->m_next) {
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000507 if (sct->m_applet == applet_no)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000508 goto found;
509 }
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000510 goto check_need_suid;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000511 found:
Denys Vlasenko3770b6b2011-05-16 13:19:25 +0200512 /* Is this user allowed to run this applet? */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000513 m = sct->m_mode;
Denys Vlasenko4566e172011-05-16 00:01:08 +0200514 if (sct->m_ugid.uid == ruid)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000515 /* same uid */
516 m >>= 6;
Denys Vlasenko4566e172011-05-16 00:01:08 +0200517 else if ((sct->m_ugid.gid == rgid) || ingroup(ruid, sct->m_ugid.gid))
Denis Vlasenko724d1962007-10-10 14:41:07 +0000518 /* same group / in group */
519 m >>= 3;
Denys Vlasenko3770b6b2011-05-16 13:19:25 +0200520 if (!(m & S_IXOTH)) /* is x bit not set? */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000521 bb_error_msg_and_die("you have no permission to run this applet!");
522
Denis Vlasenko724d1962007-10-10 14:41:07 +0000523 /* We set effective AND saved ids. If saved-id is not set
Denys Vlasenko3770b6b2011-05-16 13:19:25 +0200524 * like we do below, seteuid(0) can still later succeed! */
525
526 /* Are we directed to change gid
527 * (APPLET = *s* USER.GROUP or APPLET = *S* USER.GROUP)?
528 */
529 if (sct->m_mode & S_ISGID)
530 rgid = sct->m_ugid.gid;
531 /* else: we will set egid = rgid, thus dropping sgid effect */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000532 if (setresgid(-1, rgid, rgid))
533 bb_perror_msg_and_die("setresgid");
534
Denys Vlasenko3770b6b2011-05-16 13:19:25 +0200535 /* Are we directed to change uid
536 * (APPLET = s** USER.GROUP or APPLET = S** USER.GROUP)?
537 */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000538 uid = ruid;
539 if (sct->m_mode & S_ISUID)
Denys Vlasenko4566e172011-05-16 00:01:08 +0200540 uid = sct->m_ugid.uid;
Denys Vlasenko3770b6b2011-05-16 13:19:25 +0200541 /* else: we will set euid = ruid, thus dropping suid effect */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000542 if (setresuid(-1, uid, uid))
543 bb_perror_msg_and_die("setresuid");
Denys Vlasenko3770b6b2011-05-16 13:19:25 +0200544
Denys Vlasenko9be47022011-05-16 12:21:31 +0200545 goto ret;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000546 }
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100547# if !ENABLE_FEATURE_SUID_CONFIG_QUIET
Denis Vlasenko724d1962007-10-10 14:41:07 +0000548 {
549 static bool onetime = 0;
550
551 if (!onetime) {
552 onetime = 1;
Denys Vlasenko9be47022011-05-16 12:21:31 +0200553 bb_error_msg("using fallback suid method");
Denis Vlasenko724d1962007-10-10 14:41:07 +0000554 }
555 }
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100556# endif
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000557 check_need_suid:
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100558# endif
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +0100559 if (APPLET_SUID(applet_no) == BB_SUID_REQUIRE) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000560 /* Real uid is not 0. If euid isn't 0 too, suid bit
561 * is most probably not set on our executable */
562 if (geteuid())
Denis Vlasenko15ca51e2007-10-29 19:25:45 +0000563 bb_error_msg_and_die("must be suid to work properly");
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +0100564 } else if (APPLET_SUID(applet_no) == BB_SUID_DROP) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000565 xsetgid(rgid); /* drop all privileges */
566 xsetuid(ruid);
567 }
Denys Vlasenko9be47022011-05-16 12:21:31 +0200568 ret: ;
569# if ENABLE_FEATURE_SUID_CONFIG
570 llist_free((llist_t*)suid_config, NULL);
571# endif
Denis Vlasenko724d1962007-10-10 14:41:07 +0000572}
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100573# else
574# define check_suid(x) ((void)0)
575# endif /* FEATURE_SUID */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000576
577
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100578# if ENABLE_FEATURE_INSTALLER
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400579static const char usr_bin [] ALIGN1 = "/usr/bin/";
580static const char usr_sbin[] ALIGN1 = "/usr/sbin/";
581static const char *const install_dir[] = {
582 &usr_bin [8], /* "/" */
583 &usr_bin [4], /* "/bin/" */
Denys Vlasenkod4d289a2010-10-12 04:18:05 +0200584 &usr_sbin[4] /* "/sbin/" */
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100585# if !ENABLE_INSTALL_NO_USR
Denys Vlasenkod4d289a2010-10-12 04:18:05 +0200586 ,usr_bin
587 ,usr_sbin
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100588# endif
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400589};
590
Denis Vlasenko724d1962007-10-10 14:41:07 +0000591/* create (sym)links for each applet */
Denys Vlasenko3b405432009-07-15 00:35:34 +0200592static void install_links(const char *busybox, int use_symbolic_links,
593 char *custom_install_dir)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000594{
595 /* directory table
596 * this should be consistent w/ the enum,
597 * busybox.h::bb_install_loc_t, or else... */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000598 int (*lf)(const char *, const char *);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000599 char *fpc;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000600 unsigned i;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000601 int rc;
602
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000603 lf = link;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000604 if (use_symbolic_links)
605 lf = symlink;
606
Denis Vlasenko745cd172007-11-29 03:31:20 +0000607 for (i = 0; i < ARRAY_SIZE(applet_main); i++) {
Denis Vlasenko724d1962007-10-10 14:41:07 +0000608 fpc = concat_path_file(
Denys Vlasenko3b405432009-07-15 00:35:34 +0200609 custom_install_dir ? custom_install_dir : install_dir[APPLET_INSTALL_LOC(i)],
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000610 APPLET_NAME(i));
Denis Vlasenko745cd172007-11-29 03:31:20 +0000611 // debug: bb_error_msg("%slinking %s to busybox",
612 // use_symbolic_links ? "sym" : "", fpc);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000613 rc = lf(busybox, fpc);
614 if (rc != 0 && errno != EEXIST) {
615 bb_simple_perror_msg(fpc);
616 }
617 free(fpc);
618 }
619}
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100620# else
621# define install_links(x,y,z) ((void)0)
622# endif
Denis Vlasenko724d1962007-10-10 14:41:07 +0000623
624/* If we were called as "busybox..." */
625static int busybox_main(char **argv)
626{
627 if (!argv[1]) {
628 /* Called without arguments */
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000629 const char *a;
Denys Vlasenko0149f022009-05-19 18:01:42 +0200630 int col;
631 unsigned output_width;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000632 help:
633 output_width = 80;
634 if (ENABLE_FEATURE_AUTOWIDTH) {
635 /* Obtain the terminal width */
636 get_terminal_width_height(0, &output_width, NULL);
637 }
Denis Vlasenko724d1962007-10-10 14:41:07 +0000638
Denis Vlasenko21278df2008-06-25 12:15:46 +0000639 dup2(1, 2);
Denys Vlasenko630dde12009-08-30 19:57:49 +0200640 full_write2_str(bb_banner); /* reuse const string */
641 full_write2_str(" multi-call binary.\n"); /* reuse */
642 full_write2_str(
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400643 "Copyright (C) 1998-2009 Erik Andersen, Rob Landley, Denys Vlasenko\n"
644 "and others. Licensed under GPLv2.\n"
645 "See source distribution for full notice.\n"
646 "\n"
647 "Usage: busybox [function] [arguments]...\n"
Denys Vlasenko819cacf2010-09-12 16:17:37 +0200648 " or: busybox --list[-full]\n"
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400649 " or: function [arguments]...\n"
650 "\n"
651 "\tBusyBox is a multi-call binary that combines many common Unix\n"
652 "\tutilities into a single executable. Most people will create a\n"
653 "\tlink to busybox for each function they wish to use and BusyBox\n"
654 "\twill act like whatever it was invoked as.\n"
655 "\n"
656 "Currently defined functions:\n"
657 );
Denis Vlasenko724d1962007-10-10 14:41:07 +0000658 col = 0;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000659 a = applet_names;
Denys Vlasenko0149f022009-05-19 18:01:42 +0200660 /* prevent last comma to be in the very last pos */
661 output_width--;
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000662 while (*a) {
Denys Vlasenko0149f022009-05-19 18:01:42 +0200663 int len2 = strlen(a) + 2;
664 if (col >= (int)output_width - len2) {
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000665 full_write2_str(",\n");
Denis Vlasenko724d1962007-10-10 14:41:07 +0000666 col = 0;
667 }
Denys Vlasenko0149f022009-05-19 18:01:42 +0200668 if (col == 0) {
669 col = 6;
670 full_write2_str("\t");
671 } else {
672 full_write2_str(", ");
673 }
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000674 full_write2_str(a);
Denys Vlasenko0149f022009-05-19 18:01:42 +0200675 col += len2;
676 a += len2 - 1;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000677 }
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000678 full_write2_str("\n\n");
Denis Vlasenko724d1962007-10-10 14:41:07 +0000679 return 0;
680 }
681
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400682 if (strncmp(argv[1], "--list", 6) == 0) {
683 unsigned i = 0;
684 const char *a = applet_names;
685 dup2(1, 2);
686 while (*a) {
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100687# if ENABLE_FEATURE_INSTALLER
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400688 if (argv[1][6]) /* --list-path? */
689 full_write2_str(install_dir[APPLET_INSTALL_LOC(i)] + 1);
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100690# endif
Denys Vlasenko5a7c7202010-04-20 21:02:57 -0400691 full_write2_str(a);
692 full_write2_str("\n");
693 i++;
694 a += strlen(a) + 1;
695 }
696 return 0;
697 }
698
Denis Vlasenko724d1962007-10-10 14:41:07 +0000699 if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) {
Denys Vlasenko3b405432009-07-15 00:35:34 +0200700 int use_symbolic_links;
Denis Vlasenko724d1962007-10-10 14:41:07 +0000701 const char *busybox;
Denys Vlasenko4a2a86d2011-03-06 06:02:31 +0100702
Denis Vlasenko724d1962007-10-10 14:41:07 +0000703 busybox = xmalloc_readlink(bb_busybox_exec_path);
Denys Vlasenko4a2a86d2011-03-06 06:02:31 +0100704 if (!busybox) {
705 /* bb_busybox_exec_path is usually "/proc/self/exe".
706 * In chroot, readlink("/proc/self/exe") usually fails.
707 * In such case, better use argv[0] as symlink target
708 * if it is a full path name.
709 */
Denys Vlasenko5c942712011-03-12 06:08:28 +0100710 if (argv[0][0] != '/')
711 bb_error_msg_and_die("'%s' is not an absolute path", argv[0]);
712 busybox = argv[0];
Denys Vlasenko4a2a86d2011-03-06 06:02:31 +0100713 }
714 /* busybox --install [-s] [DIR]:
715 * -s: make symlinks
716 * DIR: directory to install links to
717 */
Denys Vlasenko3b405432009-07-15 00:35:34 +0200718 use_symbolic_links = (argv[2] && strcmp(argv[2], "-s") == 0 && argv++);
719 install_links(busybox, use_symbolic_links, argv[2]);
Denis Vlasenko724d1962007-10-10 14:41:07 +0000720 return 0;
721 }
722
723 if (strcmp(argv[1], "--help") == 0) {
724 /* "busybox --help [<applet>]" */
725 if (!argv[2])
726 goto help;
727 /* convert to "<applet> --help" */
728 argv[0] = argv[2];
729 argv[2] = NULL;
730 } else {
731 /* "busybox <applet> arg1 arg2 ..." */
732 argv++;
733 }
734 /* We support "busybox /a/path/to/applet args..." too. Allows for
735 * "#!/bin/busybox"-style wrappers */
736 applet_name = bb_get_last_path_component_nostrip(argv[0]);
737 run_applet_and_exit(applet_name, argv);
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000738
739 /*bb_error_msg_and_die("applet not found"); - sucks in printf */
740 full_write2_str(applet_name);
741 full_write2_str(": applet not found\n");
742 xfunc_die();
Denis Vlasenko724d1962007-10-10 14:41:07 +0000743}
744
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000745void FAST_FUNC run_applet_no_and_exit(int applet_no, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000746{
747 int argc = 1;
748
749 while (argv[argc])
750 argc++;
751
752 /* Reinit some shared global data */
Denis Vlasenko724d1962007-10-10 14:41:07 +0000753 xfunc_error_retval = EXIT_FAILURE;
754
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000755 applet_name = APPLET_NAME(applet_no);
Denis Vlasenkobd28f6b2008-07-19 08:15:13 +0000756 if (argc == 2 && strcmp(argv[1], "--help") == 0) {
757 /* Special case. POSIX says "test --help"
758 * should be no different from e.g. "test --foo". */
759//TODO: just compare applet_no with APPLET_NO_test
760 if (!ENABLE_TEST || strcmp(applet_name, "test") != 0)
761 bb_show_usage();
762 }
Denis Vlasenko724d1962007-10-10 14:41:07 +0000763 if (ENABLE_FEATURE_SUID)
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000764 check_suid(applet_no);
Denis Vlasenko745cd172007-11-29 03:31:20 +0000765 exit(applet_main[applet_no](argc, argv));
Denis Vlasenko724d1962007-10-10 14:41:07 +0000766}
767
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000768void FAST_FUNC run_applet_and_exit(const char *name, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000769{
Denis Vlasenko1aa7e472007-11-28 06:49:03 +0000770 int applet = find_applet_by_name(name);
771 if (applet >= 0)
772 run_applet_no_and_exit(applet, argv);
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100773 if (strncmp(name, "busybox", 7) == 0)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000774 exit(busybox_main(argv));
775}
776
Denis Vlasenko10f6fb12008-04-29 00:10:27 +0000777#endif /* !defined(SINGLE_APPLET_MAIN) */
Denis Vlasenko468aea22008-04-01 14:47:57 +0000778
779
Denis Vlasenko724d1962007-10-10 14:41:07 +0000780
781#if ENABLE_BUILD_LIBBUSYBOX
Denis Vlasenko85c24712008-03-17 09:04:04 +0000782int lbb_main(char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000783#else
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000784int main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko724d1962007-10-10 14:41:07 +0000785#endif
786{
Denys Vlasenko1f7c1672009-09-06 02:12:28 +0200787 /* Tweak malloc for reduced memory consumption */
Denys Vlasenko1f7c1672009-09-06 02:12:28 +0200788#ifdef M_TRIM_THRESHOLD
789 /* M_TRIM_THRESHOLD is the maximum amount of freed top-most memory
790 * to keep before releasing to the OS
791 * Default is way too big: 256k
792 */
793 mallopt(M_TRIM_THRESHOLD, 2 * PAGE_SIZE);
794#endif
795#ifdef M_MMAP_THRESHOLD
796 /* M_MMAP_THRESHOLD is the request size threshold for using mmap()
797 * Default is too big: 256k
798 */
799 mallopt(M_MMAP_THRESHOLD, 8 * PAGE_SIZE - 256);
800#endif
801
Denis Vlasenko724d1962007-10-10 14:41:07 +0000802#if !BB_MMU
803 /* NOMMU re-exec trick sets high-order bit in first byte of name */
804 if (argv[0][0] & 0x80) {
805 re_execed = 1;
806 argv[0][0] &= 0x7f;
807 }
808#endif
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100809
810#if defined(SINGLE_APPLET_MAIN)
811 /* Only one applet is selected in .config */
Denys Vlasenko0ab45da2010-11-29 03:21:55 +0100812 if (argv[1] && strncmp(argv[0], "busybox", 7) == 0) {
Denys Vlasenko8f0af3b2010-11-29 02:55:35 +0100813 /* "busybox <applet> <params>" should still work as expected */
814 argv++;
815 }
816 /* applet_names in this case is just "applet\0\0" */
817 lbb_prepare(applet_names IF_FEATURE_INDIVIDUAL(, argv));
818 return SINGLE_APPLET_MAIN(argc, argv);
819#else
820 lbb_prepare("busybox" IF_FEATURE_INDIVIDUAL(, argv));
821
Denis Vlasenko724d1962007-10-10 14:41:07 +0000822 applet_name = argv[0];
823 if (applet_name[0] == '-')
824 applet_name++;
825 applet_name = bb_basename(applet_name);
826
827 parse_config_file(); /* ...maybe, if FEATURE_SUID_CONFIG */
828
829 run_applet_and_exit(applet_name, argv);
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000830
831 /*bb_error_msg_and_die("applet not found"); - sucks in printf */
832 full_write2_str(applet_name);
833 full_write2_str(": applet not found\n");
834 xfunc_die();
Denis Vlasenko468aea22008-04-01 14:47:57 +0000835#endif
Denis Vlasenko724d1962007-10-10 14:41:07 +0000836}