From 4f2ef4a836be37b25808c94f41c7c85895db6f93 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 1 Nov 2018 09:53:25 +0100 Subject: ash: allow shell scripts to be embedded in the binary To assist in the deployment of shell scripts it may be convenient to embed them in the BusyBox binary. 'Embed scripts in the binary' takes any files in the directory 'embed', concatenates them with null separators, compresses them and embeds them in the binary. When scripts are embedded in the binary, scripts can be run as 'busybox SCRIPT [ARGS]' or by usual (sym)link mechanism. embed/nologin is provided as an example. function old new delta packed_scripts - 123 +123 unpack_scripts - 87 +87 ash_main 1103 1171 +68 run_applet_and_exit 78 128 +50 get_script_content - 32 +32 script_names - 10 +10 expmeta 663 659 -4 ------------------------------------------------------------------------------ (add/remove: 4/0 grow/shrink: 2/1 up/down: 370/-4) Total: 366 bytes Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- shell/ash.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'shell') diff --git a/shell/ash.c b/shell/ash.c index dc1a55a6b..25468d796 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -148,6 +148,21 @@ //config: you to run the specified command or builtin, //config: even when there is a function with the same name. //config: +//config:config ASH_EMBEDDED_SCRIPTS +//config: bool "Embed scripts in the binary" +//config: default y +//config: depends on ASH || SH_IS_ASH || BASH_IS_ASH +//config: help +//config: Allow scripts to be compressed and embedded in the BusyBox +//config: binary. The scripts should be placed in the 'embed' directory +//config: at build time. In standalone shell mode such scripts can be +//config: run directly and are subject to tab completion; otherwise they +//config: can be run by giving their name as an argument to the shell. +//config: For convenience shell aliases are created. The '-L' shell +//config: argument lists the names of the scripts. Like applets scripts +//config: can be run as 'busybox name ...' or by linking their name to +//config: the binary. +//config: //config:endif # ash options //applet:IF_ASH(APPLET(ash, BB_DIR_BIN, BB_SUID_DROP)) @@ -181,6 +196,11 @@ #include #include /* for setting $HOSTNAME */ #include "busybox.h" /* for applet_names */ +#if ENABLE_ASH_EMBEDDED_SCRIPTS +# include "embedded_scripts.h" +#else +# define NUM_SCRIPTS 0 +#endif /* So far, all bash compat is controlled by one config option */ /* Separate defines document which part of code implements what */ @@ -14021,13 +14041,17 @@ procargs(char **argv) int login_sh; xargv = argv; +#if NUM_SCRIPTS > 0 + if (minusc) + goto setarg0; +#endif login_sh = xargv[0] && xargv[0][0] == '-'; arg0 = xargv[0]; /* if (xargv[0]) - mmm, this is always true! */ xargv++; + argptr = xargv; for (i = 0; i < NOPTS; i++) optlist[i] = 2; - argptr = xargv; if (options(/*cmdline:*/ 1, &login_sh)) { /* it already printed err message */ raise_exception(EXERROR); @@ -14130,6 +14154,7 @@ extern int etext(); */ int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int ash_main(int argc UNUSED_PARAM, char **argv) +/* note: 'argc' is used only if embedded scripts are enabled */ { volatile smallint state; struct jmploc jmploc; @@ -14183,6 +14208,12 @@ int ash_main(int argc UNUSED_PARAM, char **argv) init(); setstackmark(&smark); + +#if NUM_SCRIPTS > 0 + if (argc < 0) + /* Non-NULL minusc tells procargs that an embedded script is being run */ + minusc = get_script_content(-argc - 1); +#endif login_sh = procargs(argv); #if DEBUG TRACE(("Shell args: ")); -- cgit v1.2.3-55-g6feb From aa2959c90d9c3217ddb6f482b82fef7234ad9bde Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 1 Nov 2018 10:28:04 +0100 Subject: claenups for previous commit Signed-off-by: Denys Vlasenko --- libbb/appletlib.c | 36 ++++++++++++++++-------------------- shell/ash.c | 11 +++-------- 2 files changed, 19 insertions(+), 28 deletions(-) (limited to 'shell') diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 08720082e..4e6d1c3d6 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -964,6 +964,22 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **ar # endif /* NUM_APPLETS > 0 */ # if NUM_SCRIPTS > 0 +static int +find_script_by_name(const char *arg) +{ + const char *s = script_names; + int i = 0; + + while (*s) { + if (strcmp(arg, s) == 0) + return i; + i++; + while (*s++ != '\0') + continue; + } + return -1; +} + static char * unpack_scripts(void) { @@ -992,26 +1008,6 @@ unpack_scripts(void) return outbuf; } -/* - * In standalone shell mode we sometimes want the index of the script - * and sometimes the index offset by NUM_APPLETS. - */ -static int -find_script_by_name(const char *arg) -{ - const char *s = script_names; - int i = 0; - - while (*s) { - if (strcmp(arg, s) == 0) - return i; - i++; - while (*s++ != '\0') - continue; - } - return -1; -} - char* FAST_FUNC get_script_content(unsigned n) { diff --git a/shell/ash.c b/shell/ash.c index 25468d796..3adb6d0d2 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -153,15 +153,10 @@ //config: default y //config: depends on ASH || SH_IS_ASH || BASH_IS_ASH //config: help -//config: Allow scripts to be compressed and embedded in the BusyBox +//config: Allow scripts to be compressed and embedded in the busybox //config: binary. The scripts should be placed in the 'embed' directory -//config: at build time. In standalone shell mode such scripts can be -//config: run directly and are subject to tab completion; otherwise they -//config: can be run by giving their name as an argument to the shell. -//config: For convenience shell aliases are created. The '-L' shell -//config: argument lists the names of the scripts. Like applets scripts -//config: can be run as 'busybox name ...' or by linking their name to -//config: the binary. +//config: at build time. Like applets, scripts can be run as +//config: 'busybox SCRIPT ...' or by linking their name to the binary. //config: //config:endif # ash options -- cgit v1.2.3-55-g6feb From 151de441e7a19ce3afba0c59b083240a801222cf Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 1 Nov 2018 11:04:47 +0100 Subject: ash: recognize embedded scripts in SH_STANDALONE mode function old new delta find_script_by_name - 51 +51 shellexec 254 271 +17 find_command 990 1007 +17 evalcommand 1653 1661 +8 doCommands 2233 2222 -11 run_applet_and_exit 128 100 -28 ------------------------------------------------------------------------------ (add/remove: 1/0 grow/shrink: 6/4 up/down: 104/-52) Total: 52 bytes Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- include/libbb.h | 2 +- libbb/appletlib.c | 8 ++++---- shell/ash.c | 12 ++++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) (limited to 'shell') diff --git a/include/libbb.h b/include/libbb.h index affff5874..a32608ebd 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1322,7 +1322,7 @@ void bb_logenv_override(void) FAST_FUNC; #endif /* Embedded script support */ -//int find_script_by_name(const char *arg IF_FEATURE_SH_STANDALONE(, int offset)) FAST_FUNC; +int find_script_by_name(const char *name) FAST_FUNC; char *get_script_content(unsigned n) FAST_FUNC; /* Applets which are useful from another applets */ diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 4e6d1c3d6..92d99fbe8 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -964,20 +964,20 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **ar # endif /* NUM_APPLETS > 0 */ # if NUM_SCRIPTS > 0 -static int -find_script_by_name(const char *arg) +int FAST_FUNC +find_script_by_name(const char *name) { const char *s = script_names; int i = 0; while (*s) { - if (strcmp(arg, s) == 0) + if (strcmp(name, s) == 0) return i; i++; while (*s++ != '\0') continue; } - return -1; + return -0x10000; /* make it so that NUM_APPLETS + is still < 0 */ } static char * diff --git a/shell/ash.c b/shell/ash.c index 3adb6d0d2..58ecf6d2c 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -8085,6 +8085,9 @@ static void shellexec(char *prog, char **argv, const char *path, int idx) if (strchr(prog, '/') != NULL #if ENABLE_FEATURE_SH_STANDALONE || (applet_no = find_applet_by_name(prog)) >= 0 +# if NUM_SCRIPTS > 0 + || (applet_no = NUM_APPLETS + find_script_by_name(prog)) >= 0 +# endif #endif ) { tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) prog, argv, envp); @@ -10186,6 +10189,10 @@ evalcommand(union node *cmd, int flags) */ /* find_command() encodes applet_no as (-2 - applet_no) */ int applet_no = (- cmdentry.u.index - 2); +# if NUM_SCRIPTS > 0 + /* Applets are ok, but not embedded scripts */ + if (applet_no < NUM_APPLETS) +# endif if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) { char **sv_environ; @@ -13368,6 +13375,11 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path) #if ENABLE_FEATURE_SH_STANDALONE { int applet_no = find_applet_by_name(name); +# if NUM_SCRIPTS > 0 + if (applet_no < 0) + /* embedded script indices are offset by NUM_APPLETS */ + applet_no = NUM_APPLETS + find_script_by_name(name); +# endif if (applet_no >= 0) { entry->cmdtype = CMDNORMAL; entry->u.index = -2 - applet_no; -- cgit v1.2.3-55-g6feb From ca82b5354feca83374c78a7bdd43ef3aab9204e9 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 1 Nov 2018 11:45:03 +0100 Subject: ash: in tryexec(), ensure we don't try to run embedded scripts as applets Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- shell/ash.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'shell') diff --git a/shell/ash.c b/shell/ash.c index 58ecf6d2c..19004aad8 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -8017,6 +8017,10 @@ tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) const char *cmd, char **argv, c { #if ENABLE_FEATURE_SH_STANDALONE if (applet_no >= 0) { +# if NUM_SCRIPTS > 0 + if (applet_no >= NUM_APPLETS) + goto run_script; +# endif if (APPLET_IS_NOEXEC(applet_no)) { clearenv(); while (*envp) @@ -8039,7 +8043,11 @@ tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) const char *cmd, char **argv, c #else execve(cmd, argv, envp); #endif + if (cmd != bb_busybox_exec_path && errno == ENOEXEC) { +#if ENABLE_FEATURE_SH_STANDALONE && NUM_SCRIPTS > 0 + run_script: +#endif /* Run "cmd" as a shell script: * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html * "If the execve() function fails with ENOEXEC, the shell -- cgit v1.2.3-55-g6feb From b0df5af0fad7969a10d3910465bfd0ff518b1358 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 1 Nov 2018 12:50:33 +0100 Subject: ash: fix thinko in last commit Signed-off-by: Denys Vlasenko --- shell/ash.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'shell') diff --git a/shell/ash.c b/shell/ash.c index 19004aad8..88f2b5bd6 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -8018,8 +8018,7 @@ tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) const char *cmd, char **argv, c #if ENABLE_FEATURE_SH_STANDALONE if (applet_no >= 0) { # if NUM_SCRIPTS > 0 - if (applet_no >= NUM_APPLETS) - goto run_script; + if (applet_no < NUM_APPLETS) # endif if (APPLET_IS_NOEXEC(applet_no)) { clearenv(); @@ -8045,9 +8044,6 @@ tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) const char *cmd, char **argv, c #endif if (cmd != bb_busybox_exec_path && errno == ENOEXEC) { -#if ENABLE_FEATURE_SH_STANDALONE && NUM_SCRIPTS > 0 - run_script: -#endif /* Run "cmd" as a shell script: * http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html * "If the execve() function fails with ENOEXEC, the shell -- cgit v1.2.3-55-g6feb From 8767c12774d9392565f20d3ea4e28bb1b6075a31 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 5 Nov 2018 13:13:08 +0000 Subject: ash: minor fixes Ensure that login_sh is initialised in procargs even when running an embedded script. The argc argument to ash_main isn't unused when embedded scripts are present. Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- shell/ash.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'shell') diff --git a/shell/ash.c b/shell/ash.c index 88f2b5bd6..90eaf6faf 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -14052,11 +14052,11 @@ procargs(char **argv) int login_sh; xargv = argv; + login_sh = xargv[0] && xargv[0][0] == '-'; #if NUM_SCRIPTS > 0 if (minusc) goto setarg0; #endif - login_sh = xargv[0] && xargv[0][0] == '-'; arg0 = xargv[0]; /* if (xargv[0]) - mmm, this is always true! */ xargv++; @@ -14164,7 +14164,11 @@ extern int etext(); * is used to figure out how far we had gotten. */ int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +#if NUM_SCRIPTS > 0 +int ash_main(int argc, char **argv) +#else int ash_main(int argc UNUSED_PARAM, char **argv) +#endif /* note: 'argc' is used only if embedded scripts are enabled */ { volatile smallint state; -- cgit v1.2.3-55-g6feb From 060f0a050a13a37c642f9b1a9d70c1e9861823cf Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Fri, 9 Nov 2018 12:00:39 +0000 Subject: hush: correct description for HUSH_TICK config option The HUSH_TICK configuration option enables command substitution, not process substitution. Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- shell/hush.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'shell') diff --git a/shell/hush.c b/shell/hush.c index 881331c5b..431010f09 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -157,7 +157,7 @@ //config: but no separate process group is formed. //config: //config:config HUSH_TICK -//config: bool "Support process substitution" +//config: bool "Support command substitution" //config: default y //config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH //config: help -- cgit v1.2.3-55-g6feb From e6a63bf683f47027d36dc21b62b2f5cc3eb30a30 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 12 Nov 2018 21:10:54 +0000 Subject: ash: ensure variables are fully initialised when unset When a variable is unset by calling setvar(name, NULL, 0) the code to initialise the new, empty variable fails to initialise the last character of the string. Attempts to read the contents of the unset variable will result in the uninitialised character at the end of the string being accessed. For example, running BusyBox under Valgrind and unsetting PATH: $ valgrind ./busybox_unstripped sh ==21249== Memcheck, a memory error detector ==21249== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==21249== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info ==21249== Command: ./busybox_unstripped sh ==21249== /data2/git/build_fix_8721 $ unset PATH /data2/git/build_fix_8721 $ 0 ==21249== Conditional jump or move depends on uninitialised value(s) ==21249== at 0x451371: path_advance (ash.c:2555) ==21249== by 0x456E22: find_command (ash.c:13407) ==21249== by 0x458425: evalcommand (ash.c:10139) ==21249== by 0x454CBC: evaltree (ash.c:9131) ==21249== by 0x456C80: cmdloop (ash.c:13164) Closes https://bugs.busybox.net/show_bug.cgi?id=8721 v2: On the dash mailing list Harald van Dijk was kind enough to point out a flaw in my reasoning and provide an alternative patch. Sadly his patch adds 2 bytes of bloat. Using xzalloc to zero the whole string gives a bloat of -3 bytes. function old new delta setvar 172 169 -3 Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- shell/ash.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'shell') diff --git a/shell/ash.c b/shell/ash.c index 90eaf6faf..b1f8f15d2 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -2420,13 +2420,12 @@ setvar(const char *name, const char *val, int flags) } INT_OFF; - nameeq = ckmalloc(namelen + vallen + 2); + nameeq = ckzalloc(namelen + vallen + 2); p = mempcpy(nameeq, name, namelen); if (val) { *p++ = '='; - p = mempcpy(p, val, vallen); + memcpy(p, val, vallen); } - *p = '\0'; vp = setvareq(nameeq, flags | VNOSAVE); INT_ON; -- cgit v1.2.3-55-g6feb From 3778898f97a64e7b42b53194af7f3b93cc9c07a3 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sat, 17 Nov 2018 17:48:14 +0000 Subject: Treat custom and applet scripts as applets BusyBox has support for embedded shell scripts. Two types can be distinguished: custom scripts and scripts implementing applets. Custom scripts should be placed in the 'embed' directory at build time. They are given a default applet configuration and appear as applets to the user but no further configuration is possible. Applet scripts are integrated with the BusyBox build system and are intended to be used to ship standard applets that just happen to be implemented as scripts. They can be configured at build time and appear just like native applets. Such scripts should be placed in the 'applets_sh' directory. A stub C program should be written to provide the usual applet configuration details and placed in a suitable subsystem directory. It may be helpful to have a configuration option to enable any dependencies the script requires: see the 'nologin' applet for an example. function old new delta scripted_main - 41 +41 applet_names 2773 2781 +8 applet_main 1600 1604 +4 i2cdetect_main 672 674 +2 applet_suid 100 101 +1 applet_install_loc 200 201 +1 applet_flags 100 101 +1 packed_usage 33180 33179 -1 tryexec 159 152 -7 evalcommand 1661 1653 -8 script_names 9 - -9 packed_scripts 123 114 -9 complete_cmd_dir_file 826 811 -15 shellexec 271 254 -17 find_command 1007 990 -17 busybox_main 642 624 -18 run_applet_and_exit 100 78 -22 find_script_by_name 51 - -51 ------------------------------------------------------------------------------ (add/remove: 1/2 grow/shrink: 6/9 up/down: 58/-174) Total: -116 bytes text data bss dec hex filename 950034 477 7296 957807 e9d6f busybox_old 949918 477 7296 957691 e9cfb busybox_unstripped Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- .gitignore | 5 +++ Makefile | 4 +- applets/busybox.mkscripts | 16 +++++++ applets_sh/README | 5 --- applets_sh/dos2unix | 5 --- applets_sh/nologin | 3 ++ applets_sh/tac | 7 --- applets_sh/unix2dos | 5 --- embed/nologin | 4 -- include/applets.src.h | 21 +++++++++ include/libbb.h | 2 +- libbb/appletlib.c | 72 ++++++++++++++---------------- libbb/lineedit.c | 20 ++------- scripts/embedded_scripts | 107 ++++++++++++++++++++++++++++++++++----------- scripts/gen_build_files.sh | 21 ++++++++- shell/ash.c | 19 ++------ util-linux/nologin.c | 27 ++++++++++++ 17 files changed, 217 insertions(+), 126 deletions(-) create mode 100755 applets/busybox.mkscripts delete mode 100644 applets_sh/README delete mode 100755 applets_sh/dos2unix create mode 100755 applets_sh/nologin delete mode 100755 applets_sh/tac delete mode 100755 applets_sh/unix2dos delete mode 100755 embed/nologin create mode 100644 util-linux/nologin.c (limited to 'shell') diff --git a/.gitignore b/.gitignore index c03c2e8a6..becd9bf6d 100644 --- a/.gitignore +++ b/.gitignore @@ -56,3 +56,8 @@ cscope.po.out # tags TAGS + +# +# user-supplied scripts +# +/embed diff --git a/Makefile b/Makefile index 8a0dbdf49..c19280476 100644 --- a/Makefile +++ b/Makefile @@ -361,7 +361,7 @@ scripts/basic/%: scripts_basic ; # This target generates Kbuild's and Config.in's from *.c files PHONY += gen_build_files -gen_build_files: $(wildcard $(srctree)/*/*.c) $(wildcard $(srctree)/*/*/*.c) +gen_build_files: $(wildcard $(srctree)/*/*.c) $(wildcard $(srctree)/*/*/*.c) $(wildcard embed/*) $(Q)$(srctree)/scripts/gen_build_files.sh $(srctree) $(objtree) # bbox: we have helpers in applets/ @@ -851,7 +851,7 @@ quiet_cmd_gen_common_bufsiz = GEN include/common_bufsiz.h quiet_cmd_split_autoconf = SPLIT include/autoconf.h -> include/config/* cmd_split_autoconf = scripts/basic/split-include include/autoconf.h include/config quiet_cmd_gen_embedded_scripts = GEN include/embedded_scripts.h - cmd_gen_embedded_scripts = scripts/embedded_scripts include/embedded_scripts.h embed + cmd_gen_embedded_scripts = $(srctree)/scripts/embedded_scripts include/embedded_scripts.h $(srctree)/embed $(srctree)/applets_sh #bbox# piggybacked generation of few .h files include/config/MARKER: scripts/basic/split-include include/autoconf.h $(wildcard embed/*) scripts/embedded_scripts $(call cmd,split_autoconf) diff --git a/applets/busybox.mkscripts b/applets/busybox.mkscripts new file mode 100755 index 000000000..935685cba --- /dev/null +++ b/applets/busybox.mkscripts @@ -0,0 +1,16 @@ +#!/bin/sh +# Make busybox scripted applet list file. + +# input $1: full path to Config.h +# input $2: full path to applets.h +# output (stdout): list of pathnames that should be linked to busybox + +export LC_ALL=POSIX +export LC_CTYPE=POSIX + +CONFIG_H=${1:-include/autoconf.h} +APPLETS_H=${2:-include/applets.h} +$HOSTCC -E -DMAKE_SCRIPTS -include $CONFIG_H $APPLETS_H | + awk '/^[ \t]*SCRIPT/{ + print $2 + }' diff --git a/applets_sh/README b/applets_sh/README deleted file mode 100644 index 9dcd38ae3..000000000 --- a/applets_sh/README +++ /dev/null @@ -1,5 +0,0 @@ -This directory contains examples of applets implemented as shell scripts. - -So far these scripts are not hooked to the build system and are not -installed by "make install". If you want to use them, -you need to install them by hand. diff --git a/applets_sh/dos2unix b/applets_sh/dos2unix deleted file mode 100755 index 0fd5206f6..000000000 --- a/applets_sh/dos2unix +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -# TODO: use getopt to avoid parsing options as filenames, -# and to support -- and --help -[ $# -ne 0 ] && DASH_I=-i -sed $DASH_I -e 's/\r$//' "$@" diff --git a/applets_sh/nologin b/applets_sh/nologin new file mode 100755 index 000000000..4ed5f6ca3 --- /dev/null +++ b/applets_sh/nologin @@ -0,0 +1,3 @@ +cat /etc/nologin.txt 2>/dev/null || echo This account is not available +sleep 5 +exit 1 diff --git a/applets_sh/tac b/applets_sh/tac deleted file mode 100755 index c5a8e39c1..000000000 --- a/applets_sh/tac +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/sh -# TODO: use getopt to avoid parsing options as filenames, -# and to support -- and --help -for i in "$@" -do -sed -e '1!G;h;$!d' "$i" -done diff --git a/applets_sh/unix2dos b/applets_sh/unix2dos deleted file mode 100755 index 70e042906..000000000 --- a/applets_sh/unix2dos +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -# TODO: use getopt to avoid parsing options as filenames, -# and to support -- and --help -[ $# -ne 0 ] && DASH_I=-i -sed $DASH_I -e 's/$/\r/' "$@" diff --git a/embed/nologin b/embed/nologin deleted file mode 100755 index 3768eaaa7..000000000 --- a/embed/nologin +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh -cat /etc/nologin.txt 2>/dev/null || echo "This account is not available" -sleep 5 -exit 1 diff --git a/include/applets.src.h b/include/applets.src.h index 2ddf120ad..a9db5d160 100644 --- a/include/applets.src.h +++ b/include/applets.src.h @@ -22,41 +22,60 @@ s - suid type: BB_SUID_REQUIRE or BB_SUID_MAYBE applet. */ +#define NOUSAGE_STR "\b" + +#define dummy_trivial_usage NOUSAGE_STR \ + +#define dummy_full_usage "" \ + #if defined(PROTOTYPES) # define APPLET(name,l,s) int name##_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; # define APPLET_ODDNAME(name,main,l,s,help) int main##_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; # define APPLET_NOEXEC(name,main,l,s,help) int main##_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; # define APPLET_NOFORK(name,main,l,s,help) int main##_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; +# define APPLET_SCRIPTED(name,main,l,s,help) #elif defined(NAME_MAIN) # define APPLET(name,l,s) name name##_main # define APPLET_ODDNAME(name,main,l,s,help) name main##_main # define APPLET_NOEXEC(name,main,l,s,help) name main##_main # define APPLET_NOFORK(name,main,l,s,help) name main##_main +# define APPLET_SCRIPTED(name,main,l,s,help) name scripted_main #elif defined(MAKE_USAGE) && ENABLE_FEATURE_VERBOSE_USAGE # define APPLET(name,l,s) MAKE_USAGE(#name, name##_trivial_usage name##_full_usage) # define APPLET_ODDNAME(name,main,l,s,help) MAKE_USAGE(#name, help##_trivial_usage help##_full_usage) # define APPLET_NOEXEC(name,main,l,s,help) MAKE_USAGE(#name, help##_trivial_usage help##_full_usage) # define APPLET_NOFORK(name,main,l,s,help) MAKE_USAGE(#name, help##_trivial_usage help##_full_usage) +# define APPLET_SCRIPTED(name,main,l,s,help) MAKE_USAGE(#name, help##_trivial_usage help##_full_usage) #elif defined(MAKE_USAGE) && !ENABLE_FEATURE_VERBOSE_USAGE # define APPLET(name,l,s) MAKE_USAGE(#name, name##_trivial_usage) # define APPLET_ODDNAME(name,main,l,s,help) MAKE_USAGE(#name, help##_trivial_usage) # define APPLET_NOEXEC(name,main,l,s,help) MAKE_USAGE(#name, help##_trivial_usage) # define APPLET_NOFORK(name,main,l,s,help) MAKE_USAGE(#name, help##_trivial_usage) +# define APPLET_SCRIPTED(name,main,l,s,help) MAKE_USAGE(#name, help##_trivial_usage) #elif defined(MAKE_LINKS) # define APPLET(name,l,c) LINK l name # define APPLET_ODDNAME(name,main,l,s,help) LINK l name # define APPLET_NOEXEC(name,main,l,s,help) LINK l name # define APPLET_NOFORK(name,main,l,s,help) LINK l name +# define APPLET_SCRIPTED(name,main,l,s,help) LINK l name #elif defined(MAKE_SUID) # define APPLET(name,l,s) SUID s l name # define APPLET_ODDNAME(name,main,l,s,help) SUID s l name # define APPLET_NOEXEC(name,main,l,s,help) SUID s l name # define APPLET_NOFORK(name,main,l,s,help) SUID s l name +# define APPLET_SCRIPTED(name,main,l,s,help) SUID s l name + +#elif defined(MAKE_SCRIPTS) +# define APPLET(name,l,s) +# define APPLET_ODDNAME(name,main,l,s,help) +# define APPLET_NOEXEC(name,main,l,s,help) +# define APPLET_NOFORK(name,main,l,s,help) +# define APPLET_SCRIPTED(name,main,l,s,help) SCRIPT name #else static struct bb_applet applets[] = { /* name, main, location, need_suid */ @@ -64,6 +83,7 @@ s - suid type: # define APPLET_ODDNAME(name,main,l,s,help) { #name, #main, l, s }, # define APPLET_NOEXEC(name,main,l,s,help) { #name, #main, l, s, 1 }, # define APPLET_NOFORK(name,main,l,s,help) { #name, #main, l, s, 1, 1 }, +# define APPLET_SCRIPTED(name,main,l,s,help) { #name, #main, l, s }, #endif #if ENABLE_INSTALL_NO_USR @@ -84,3 +104,4 @@ INSERT #undef APPLET_ODDNAME #undef APPLET_NOEXEC #undef APPLET_NOFORK +#undef APPLET_SCRIPTED diff --git a/include/libbb.h b/include/libbb.h index a32608ebd..aa9e9d019 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1322,8 +1322,8 @@ void bb_logenv_override(void) FAST_FUNC; #endif /* Embedded script support */ -int find_script_by_name(const char *name) FAST_FUNC; char *get_script_content(unsigned n) FAST_FUNC; +int scripted_main(int argc, char** argv); /* Applets which are useful from another applets */ int bb_cat(char** argv) FAST_FUNC; diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 6dfaf1f41..a0ebaca29 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -51,7 +51,7 @@ #include "usage_compressed.h" #if ENABLE_ASH_EMBEDDED_SCRIPTS -# define DEFINE_script_names 1 +# define DEFINE_SCRIPT_DATA 1 # include "embedded_scripts.h" #else # define NUM_SCRIPTS 0 @@ -818,27 +818,21 @@ int busybox_main(int argc UNUSED_PARAM, char **argv) /* prevent last comma to be in the very last pos */ output_width--; a = applet_names; - { -# if NUM_SCRIPTS > 0 - int i; - for (i = 0; i < 2; i++, a = script_names) -# endif - while (*a) { - int len2 = strlen(a) + 2; - if (col >= (int)output_width - len2) { - full_write2_str(",\n"); - col = 0; - } - if (col == 0) { - col = 6; - full_write2_str("\t"); - } else { - full_write2_str(", "); - } - full_write2_str(a); - col += len2; - a += len2 - 1; + while (*a) { + int len2 = strlen(a) + 2; + if (col >= (int)output_width - len2) { + full_write2_str(",\n"); + col = 0; + } + if (col == 0) { + col = 6; + full_write2_str("\t"); + } else { + full_write2_str(", "); } + full_write2_str(a); + col += len2; + a += len2 - 1; } full_write2_str("\n"); return 0; @@ -946,20 +940,25 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **ar # endif /* NUM_APPLETS > 0 */ # if NUM_SCRIPTS > 0 -int FAST_FUNC -find_script_by_name(const char *name) +static int find_script_by_name(const char *name) { - const char *s = script_names; - int i = 0; + int i; + int applet = find_applet_by_name(name); - while (*s) { - if (strcmp(name, s) == 0) - return i; - i++; - while (*s++ != '\0') - continue; + if (applet >= 0) { + for (i = 0; i < NUM_SCRIPTS; ++i) + if (applet_numbers[i] == applet) + return i; } - return -0x10000; /* make it so that NUM_APPLETS + is still < 0 */ + return -1; +} + +int scripted_main(int argc UNUSED_PARAM, char **argv) +{ + int script = find_script_by_name(applet_name); + if (script >= 0) + exit(ash_main(-script - 1, argv)); + return 0; } char* FAST_FUNC @@ -978,7 +977,7 @@ get_script_content(unsigned n) } # endif /* NUM_SCRIPTS > 0 */ -# if ENABLE_BUSYBOX || NUM_APPLETS > 0 || NUM_SCRIPTS > 0 +# if ENABLE_BUSYBOX || NUM_APPLETS > 0 static NORETURN void run_applet_and_exit(const char *name, char **argv) { # if ENABLE_BUSYBOX @@ -993,13 +992,6 @@ static NORETURN void run_applet_and_exit(const char *name, char **argv) run_applet_no_and_exit(applet, name, argv); } # endif -# if NUM_SCRIPTS > 0 - { - int script = find_script_by_name(name); - if (script >= 0) - exit(ash_main(-script - 1, argv)); - } -# endif /*bb_error_msg_and_die("applet not found"); - links in printf */ full_write2_str(applet_name); diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 618e7c221..b1e971f88 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c @@ -41,12 +41,6 @@ #include "busybox.h" #include "NUM_APPLETS.h" #include "unicode.h" -#if ENABLE_ASH_EMBEDDED_SCRIPTS -# include "embedded_scripts.h" -#else -# define NUM_SCRIPTS 0 -#endif - #ifndef _POSIX_VDISABLE # define _POSIX_VDISABLE '\0' #endif @@ -812,20 +806,14 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type) } pf_len = strlen(pfind); -# if ENABLE_FEATURE_SH_STANDALONE && (NUM_APPLETS != 1 || NUM_SCRIPTS > 0) +# if ENABLE_FEATURE_SH_STANDALONE && NUM_APPLETS != 1 if (type == FIND_EXE_ONLY && !dirbuf) { - const char *p; -# if NUM_APPLETS != 1 && NUM_SCRIPTS > 0 - for (i = 0, p = applet_names; i < 2; i++, p = script_names) -# elif NUM_APPLETS != 1 /* and NUM_SCRIPTS == 0 */ - p = applet_names; -# else /* NUM_APPLETS == 1 && NUM_SCRIPTS > 0 */ - p = script_names; -# endif + const char *p = applet_names; + while (*p) { if (strncmp(pfind, p, pf_len) == 0) add_match(xstrdup(p)); - while (*p++) + while (*p++ != '\0') continue; } } diff --git a/scripts/embedded_scripts b/scripts/embedded_scripts index 7245ba6e0..b7a023ce0 100755 --- a/scripts/embedded_scripts +++ b/scripts/embedded_scripts @@ -1,7 +1,8 @@ #!/bin/sh target="$1" -loc="$2" +custom_loc="$2" +applet_loc="$3" test "$target" || exit 1 test "$SED" || SED=sed @@ -14,46 +15,102 @@ if test $? != 0; then exit 1 fi -exec >"$target.$$" - -scripts="" -if [ -d "$loc" ] +custom_scripts="" +if [ -d "$custom_loc" ] then - scripts=$(cd $loc; ls * 2>/dev/null) + custom_scripts=$(cd $custom_loc; ls * 2>/dev/null) fi +all_scripts=$(applets/busybox.mkscripts) + +# all_scripts includes applet scripts and custom scripts, sort them out +applet_scripts="" +for i in $all_scripts +do + found=0 + for j in $custom_scripts + do + if [ "$i" = "$j" ] + then + found=1 + break; + fi + done + if [ $found -eq 0 ] + then + # anything that isn't a custom script is an applet script + applet_scripts="$applet_scripts $i" + fi +done -n=$(echo $scripts | wc -w) +# we know the custom scripts are present but applet scripts might have +# become detached from their configuration +for i in $applet_scripts +do + #if [ ! -f "$applet_loc/$i" -a ! -f "$custom_loc/$i" ] + if [ ! -f "$applet_loc/$i" ] + then + echo "missing applet script $i" + exit 1 + fi +done -if [ $n -ne 0 ] +n=$(echo $custom_scripts $applet_scripts | wc -w) +nall=$(echo $all_scripts | wc -w) + +if [ $n -ne $nall ] then - printf '#ifdef DEFINE_script_names\n' - printf 'const char script_names[] ALIGN1 = ' - for i in $scripts + echo "script mismatch $n != $nall" + exit 1 +fi + +concatenate_scripts() { + for i in $custom_scripts + do + cat $custom_loc/$i + printf '\000' + done + for i in $applet_scripts do - printf '"%s\\0"' $i + cat $applet_loc/$i + printf '\000' done - printf ';\n' +} + +exec >"$target.$$" + +if [ $n -ne 0 ] +then + printf '#ifdef DEFINE_SCRIPT_DATA\n' + if [ $n -ne 0 ] + then + printf 'const uint16_t applet_numbers[] = {\n' + for i in $custom_scripts $applet_scripts + do + # TODO support applets with names including invalid characters + printf '\tAPPLET_NO_%s,\n' $i + done + printf '};\n' + fi printf '#else\n' - printf 'extern const char script_names[] ALIGN1;\n' + if [ $n -ne 0 ] + then + printf 'extern const uint16_t applet_numbers[];\n' + fi printf '#endif\n' fi -printf "#define NUM_SCRIPTS $n\n\n" + +printf "\n" +printf '#define NUM_SCRIPTS %d\n' $n +printf "\n" if [ $n -ne 0 ] then printf '#define UNPACKED_SCRIPTS_LENGTH ' - for i in $scripts - do - cat $loc/$i - printf '\000' - done | wc -c + concatenate_scripts | wc -c printf '#define PACKED_SCRIPTS \\\n' - for i in $scripts - do - cat $loc/$i - printf '\000' - done | bzip2 -1 | $DD bs=2 skip=1 2>/dev/null | od -v -b \ + concatenate_scripts | bzip2 -1 | $DD bs=2 skip=1 2>/dev/null | \ + od -v -b \ | grep -v '^ ' \ | $SED -e 's/^[^ ]*//' \ -e 's/ //g' \ diff --git a/scripts/gen_build_files.sh b/scripts/gen_build_files.sh index f79fa2f83..64e4bffa9 100755 --- a/scripts/gen_build_files.sh +++ b/scripts/gen_build_files.sh @@ -17,12 +17,26 @@ status() { printf ' %-8s%s\n' "$1" "$2"; } gen() { status "GEN" "$@"; } chk() { status "CHK" "$@"; } +# scripts in the 'embed' directory are treated as fake applets +custom_scripts() +{ + custom_loc="$1" + if [ -d "$custom_loc" ] + then + for i in $(cd "$custom_loc"; ls *) + do + printf "APPLET_SCRIPTED(%s, scripted, BB_DIR_USR_BIN, BB_SUID_DROP, dummy)\n" $i; + done + fi +} + generate() { # NB: data to be inserted at INSERT line is coming on stdin src="$1" dst="$2" header="$3" + loc="$4" #chk "${dst}" { # Need to use printf: different shells have inconsistent @@ -32,6 +46,10 @@ generate() sed -n '/^INSERT$/ q; p' "${src}" # copy stdin to stdout cat + if [ -n "$loc" ] + then + custom_scripts "$loc" + fi # print everything after INSERT line sed -n '/^INSERT$/ { :l @@ -53,7 +71,8 @@ sed -n 's@^//applet:@@p' "$srctree"/*/*.c "$srctree"/*/*/*.c \ | generate \ "$srctree/include/applets.src.h" \ "include/applets.h" \ - "/* DO NOT EDIT. This file is generated from applets.src.h */" + "/* DO NOT EDIT. This file is generated from applets.src.h */" \ + "$srctree/embed" # (Re)generate include/usage.h # We add line continuation backslash after each line, diff --git a/shell/ash.c b/shell/ash.c index b1f8f15d2..44b3569dc 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -158,6 +158,10 @@ //config: at build time. Like applets, scripts can be run as //config: 'busybox SCRIPT ...' or by linking their name to the binary. //config: +//config: This also allows applets to be implemented as scripts: place +//config: the script in 'applets_sh' and a stub C file containing +//config: configuration in the appropriate subsystem directory. +//config: //config:endif # ash options //applet:IF_ASH(APPLET(ash, BB_DIR_BIN, BB_SUID_DROP)) @@ -8016,9 +8020,6 @@ tryexec(IF_FEATURE_SH_STANDALONE(int applet_no,) const char *cmd, char **argv, c { #if ENABLE_FEATURE_SH_STANDALONE if (applet_no >= 0) { -# if NUM_SCRIPTS > 0 - if (applet_no < NUM_APPLETS) -# endif if (APPLET_IS_NOEXEC(applet_no)) { clearenv(); while (*envp) @@ -8088,9 +8089,6 @@ static void shellexec(char *prog, char **argv, const char *path, int idx) if (strchr(prog, '/') != NULL #if ENABLE_FEATURE_SH_STANDALONE || (applet_no = find_applet_by_name(prog)) >= 0 -# if NUM_SCRIPTS > 0 - || (applet_no = NUM_APPLETS + find_script_by_name(prog)) >= 0 -# endif #endif ) { tryexec(IF_FEATURE_SH_STANDALONE(applet_no,) prog, argv, envp); @@ -10192,10 +10190,6 @@ evalcommand(union node *cmd, int flags) */ /* find_command() encodes applet_no as (-2 - applet_no) */ int applet_no = (- cmdentry.u.index - 2); -# if NUM_SCRIPTS > 0 - /* Applets are ok, but not embedded scripts */ - if (applet_no < NUM_APPLETS) -# endif if (applet_no >= 0 && APPLET_IS_NOFORK(applet_no)) { char **sv_environ; @@ -13378,11 +13372,6 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path) #if ENABLE_FEATURE_SH_STANDALONE { int applet_no = find_applet_by_name(name); -# if NUM_SCRIPTS > 0 - if (applet_no < 0) - /* embedded script indices are offset by NUM_APPLETS */ - applet_no = NUM_APPLETS + find_script_by_name(name); -# endif if (applet_no >= 0) { entry->cmdtype = CMDNORMAL; entry->u.index = -2 - applet_no; diff --git a/util-linux/nologin.c b/util-linux/nologin.c new file mode 100644 index 000000000..cc619bf8a --- /dev/null +++ b/util-linux/nologin.c @@ -0,0 +1,27 @@ +//config:config NOLOGIN +//config: bool "nologin" +//config: default y +//config: depends on ASH_EMBEDDED_SCRIPTS +//config: help +//config: Politely refuse a login +//config: +//config:config NOLOGIN_DEPENDENCIES +//config: bool "Dependencies for nologin" +//config: default y +//config: depends on NOLOGIN +//config: select CAT +//config: select ECHO +//config: select SLEEP +//config: help +//config: nologin is implemented as a shell script. It requires the +//config: following in the runtime environment: +//config: cat echo sleep +//config: If you know these will be available externally you can +//config: disable this option. + +//applet:IF_NOLOGIN(APPLET_SCRIPTED(nologin, scripted, BB_DIR_USR_SBIN, BB_SUID_DROP, nologin)) + +//usage:#define nologin_trivial_usage +//usage: "" +//usage:#define nologin_full_usage "\n\n" +//usage: "Politely refuse a login" -- cgit v1.2.3-55-g6feb From 03ad7ae08189ed88dd7e0fcb6c6001fbf3b12efb Mon Sep 17 00:00:00 2001 From: Christoph Schulz Date: Tue, 20 Nov 2018 17:45:52 +0100 Subject: ash: reset tokpushback before prompting while parsing heredoc The parser reads from an already freed memory location, thereby causing unpredictable results, in the following situation: - ENABLE_ASH_EXPAND_PRMT is enabled - heredoc is being parsed - command substitution is used within heredoc Examples where this bug crops up are (PS2 is set to "> "): $ cat < `echo abc` > EOF -sh: O: not found $ cat < $(echo abc) > EOF -sh: {garbage}: not found The presumable reason is that setprompt_if() causes a nested expansion when ENABLE_ASH_EXPAND_PRMT is enabled, therefore leaving "wordtext" in an unusable state. However, when parseheredoc() is called, "tokpushback" is non-zero, which causes the next call to xxreadtoken() to return TWORD, causing the caller to use the invalid "wordtoken" instead of reading the next valid token. The call chain is: list() -> peektoken() [sets tokpushback to 1] -> parseheredoc() -> setprompt_if() -> pushstackmark() -> expandstr() -> readtoken1() [sets lasttoken to TWORD, wordtoken points to expanded prompt] -> popstackmark() [invalidates wordtoken, leaves lasttoken as is] -> readtoken1() -> ...parsebackq -> list() -> andor() -> pipeline() -> readtoken() -> xxreadtoken() [tokpushback non-zero, reuse lasttoken and wordtext] Note that in almost all other contexts, each call to setprompt_if() is preceded by setting "tokpushback" to zero. One exception is "oldstyle" backquote parsing in readtoken1(), but there "tokpushback" is reset afterwards. The other exception is nlprompt(), but this function is only used within readtoken1() (but in contexts where no nested calls to xxreadtoken() occur) and xxreadtoken() (where "tokpushback" is guaranteed to be zero). function old new delta parseheredoc 124 131 +7 Signed-off-by: Christoph Schulz Signed-off-by: Denys Vlasenko --- shell/ash.c | 1 + 1 file changed, 1 insertion(+) (limited to 'shell') diff --git a/shell/ash.c b/shell/ash.c index 44b3569dc..04e4006c8 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -12961,6 +12961,7 @@ parseheredoc(void) heredoclist = NULL; while (here) { + tokpushback = 0; setprompt_if(needprompt, 2); readtoken1(pgetc(), here->here->type == NHERE ? SQSYNTAX : DQSYNTAX, here->eofmark, here->striptabs); -- cgit v1.2.3-55-g6feb From 71df2d3589e3e682cd6770f41f0b184841b78702 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Tue, 27 Nov 2018 14:34:25 +0000 Subject: hush: allow hush to run embedded scripts Embedded scripts require a shell to be present in the BusyBox binary. Allow either ash or hush to be used for this purpose. If both are enabled ash takes precedence. The size of the binary is unchanged in the default configuration: both ash and hush are present but support for embedded scripts isn't compiled into hush. Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- archival/libarchive/Kbuild.src | 2 +- docs/embedded-scripts.txt | 21 ++++++++------------- include/libbb.h | 5 +++++ libbb/appletlib.c | 8 +++++++- scripts/embedded_scripts | 8 ++++++++ scripts/gen_build_files.sh | 2 +- shell/Config.src | 14 ++++++++++++++ shell/ash.c | 16 +--------------- shell/hush.c | 16 ++++++++++++++++ util-linux/nologin.c | 2 +- 10 files changed, 62 insertions(+), 32 deletions(-) (limited to 'shell') diff --git a/archival/libarchive/Kbuild.src b/archival/libarchive/Kbuild.src index 12e66a88b..d2f284b08 100644 --- a/archival/libarchive/Kbuild.src +++ b/archival/libarchive/Kbuild.src @@ -91,7 +91,7 @@ lib-$(CONFIG_FEATURE_SEAMLESS_LZMA) += open_transformer.o decompress_unlzma. lib-$(CONFIG_FEATURE_SEAMLESS_XZ) += open_transformer.o decompress_unxz.o lib-$(CONFIG_FEATURE_COMPRESS_USAGE) += open_transformer.o decompress_bunzip2.o lib-$(CONFIG_FEATURE_COMPRESS_BBCONFIG) += open_transformer.o decompress_bunzip2.o -lib-$(CONFIG_ASH_EMBEDDED_SCRIPTS) += open_transformer.o decompress_bunzip2.o +lib-$(CONFIG_FEATURE_SH_EMBEDDED_SCRIPTS) += open_transformer.o decompress_bunzip2.o ifneq ($(lib-y),) lib-y += $(COMMON_FILES) diff --git a/docs/embedded-scripts.txt b/docs/embedded-scripts.txt index 1b0c5b591..7a273d698 100644 --- a/docs/embedded-scripts.txt +++ b/docs/embedded-scripts.txt @@ -3,13 +3,9 @@ Embedded Shell Scripts in BusyBox BusyBox allows applets to be implemented as shell scripts. Since this obviously requires a shell to interpret the scripts the feature -depends on having a shell (specifically, ash) built into the binary. -Support for embedded scripts also has to be enabled. - -To embed scripts in BusyBox you must enable these configuration options: - - ASH - ASH_EMBEDDED_SCRIPTS +depends on having a shell built into the binary. Either ash or hush +will do. If both are present ash will be used. Support for embedded +scripts also has to be enabled. It's unlikely that your applet will be implemented as a pure shell script: it will probably need some external commands. If these are @@ -75,10 +71,9 @@ code for the native applet: The only difference is that the applet is specified as being of type APPLET_SCRIPTED. It would also be useful to include details of any -dependencies the script has. We can assume that ash is available. -No external commands are used by our mu script, but it does depend on -optional shell features. We can ensure these are selected by adding -this to the configuration: +dependencies the script has. No external commands are used by our mu +script, but it does depend on optional shell features. We can ensure +these are selected by adding this to the configuration: //config:config MU_DEPENDENCIES //config: bool "Enable dependencies for mu" @@ -87,8 +82,8 @@ this to the configuration: //config: select ASH_RANDOM_SUPPORT //config: select FEATURE_SH_MATH //config: help -//config: mu is implemented as a shell script. It requires ash -//config: support for $RANDOM and arithmetic. +//config: mu is implemented as a shell script. It requires support +//config: for $RANDOM and arithmetic. The configuration data should be placed in a C file in an appropriate subdirectory. There isn't any C code, though! In this case the file diff --git a/include/libbb.h b/include/libbb.h index ebd090e18..b560cc2eb 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -1348,6 +1348,11 @@ int ash_main(int argc, char** argv) MAIN_EXTERNALLY_VISIBLE #endif ; +int hush_main(int argc, char** argv) +#if ENABLE_HUSH || ENABLE_SH_IS_HUSH || ENABLE_BASH_IS_HUSH + MAIN_EXTERNALLY_VISIBLE +#endif +; /* If shell needs them, they exist even if not enabled as applets */ int echo_main(int argc, char** argv) IF_ECHO(MAIN_EXTERNALLY_VISIBLE); int printf_main(int argc, char **argv) IF_PRINTF(MAIN_EXTERNALLY_VISIBLE); diff --git a/libbb/appletlib.c b/libbb/appletlib.c index a79a37efb..cd09b620c 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -50,7 +50,7 @@ #include "usage_compressed.h" -#if ENABLE_ASH_EMBEDDED_SCRIPTS +#if ENABLE_FEATURE_SH_EMBEDDED_SCRIPTS # define DEFINE_SCRIPT_DATA 1 # include "embedded_scripts.h" #else @@ -774,7 +774,13 @@ int scripted_main(int argc UNUSED_PARAM, char **argv) { int script = find_script_by_name(applet_name); if (script >= 0) +#if ENABLE_ASH || ENABLE_SH_IS_ASH || ENABLE_BASH_IS_ASH exit(ash_main(-script - 1, argv)); +#elif ENABLE_HUSH || ENABLE_SH_IS_HUSH || ENABLE_BASH_IS_HUSH + exit(hush_main(-script - 1, argv)); +#else + return 1; +#endif return 0; } diff --git a/scripts/embedded_scripts b/scripts/embedded_scripts index 86ad44d1d..aa7bf3e8a 100755 --- a/scripts/embedded_scripts +++ b/scripts/embedded_scripts @@ -1,5 +1,7 @@ #!/bin/sh +. ./.config || exit 1 + target="$1" custom_loc="$2" applet_loc="$3" @@ -8,6 +10,12 @@ test "$target" || exit 1 test "$SED" || SED=sed test "$DD" || DD=dd +if [ x"$CONFIG_FEATURE_SH_EMBEDDED_SCRIPTS" != x"y" ] +then + printf '#define NUM_SCRIPTS 0\n' >"$target" + exit 0 +fi + # Some people were bitten by their system lacking a (proper) od od -v -b /dev/null if test $? != 0; then diff --git a/scripts/gen_build_files.sh b/scripts/gen_build_files.sh index 362632df3..92de681ac 100755 --- a/scripts/gen_build_files.sh +++ b/scripts/gen_build_files.sh @@ -25,7 +25,7 @@ custom_scripts() then for i in $(cd "$custom_loc"; ls * 2>/dev/null) do - printf "APPLET_SCRIPTED(%s, scripted, BB_DIR_USR_BIN, BB_SUID_DROP, scripted)\n" $i; + printf "IF_FEATURE_SH_EMBEDDED_SCRIPTS(APPLET_SCRIPTED(%s, scripted, BB_DIR_USR_BIN, BB_SUID_DROP, scripted))\n" $i; done fi } diff --git a/shell/Config.src b/shell/Config.src index 959d3cb42..bc7218fe5 100644 --- a/shell/Config.src +++ b/shell/Config.src @@ -161,6 +161,20 @@ config FEATURE_SH_HISTFILESIZE to set shell history size. Note that its max value is capped by "History size" setting in library tuning section. +config FEATURE_SH_EMBEDDED_SCRIPTS + bool "Embed scripts in the binary" + default y + depends on ASH || HUSH || SH_IS_ASH || BASH_IS_ASH || SH_IS_HUSH || BASH_IS_HUSH + help + Allow scripts to be compressed and embedded in the busybox + binary. The scripts should be placed in the 'embed' directory + at build time. Like applets, scripts can be run as + 'busybox SCRIPT ...' or by linking their name to the binary. + + This also allows applets to be implemented as scripts: place + the script in 'applets_sh' and a stub C file containing + configuration in the appropriate subsystem directory. + endif # Options common to all shells endmenu diff --git a/shell/ash.c b/shell/ash.c index 04e4006c8..9ce1d1a76 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -148,20 +148,6 @@ //config: you to run the specified command or builtin, //config: even when there is a function with the same name. //config: -//config:config ASH_EMBEDDED_SCRIPTS -//config: bool "Embed scripts in the binary" -//config: default y -//config: depends on ASH || SH_IS_ASH || BASH_IS_ASH -//config: help -//config: Allow scripts to be compressed and embedded in the busybox -//config: binary. The scripts should be placed in the 'embed' directory -//config: at build time. Like applets, scripts can be run as -//config: 'busybox SCRIPT ...' or by linking their name to the binary. -//config: -//config: This also allows applets to be implemented as scripts: place -//config: the script in 'applets_sh' and a stub C file containing -//config: configuration in the appropriate subsystem directory. -//config: //config:endif # ash options //applet:IF_ASH(APPLET(ash, BB_DIR_BIN, BB_SUID_DROP)) @@ -195,7 +181,7 @@ #include #include /* for setting $HOSTNAME */ #include "busybox.h" /* for applet_names */ -#if ENABLE_ASH_EMBEDDED_SCRIPTS +#if ENABLE_FEATURE_SH_EMBEDDED_SCRIPTS # include "embedded_scripts.h" #else # define NUM_SCRIPTS 0 diff --git a/shell/hush.c b/shell/hush.c index 431010f09..90191408d 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -367,6 +367,11 @@ # define PIPE_BUF 4096 /* amount of buffering in a pipe */ #endif +#if ENABLE_FEATURE_SH_EMBEDDED_SCRIPTS && !(ENABLE_ASH || ENABLE_SH_IS_ASH || ENABLE_BASH_IS_ASH) +# include "embedded_scripts.h" +#else +# define NUM_SCRIPTS 0 +#endif /* So far, all bash compat is controlled by one config option */ /* Separate defines document which part of code implements what */ @@ -9951,6 +9956,14 @@ int hush_main(int argc, char **argv) /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */ flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0; builtin_argc = 0; +#if NUM_SCRIPTS > 0 + if (argc < 0) { + optarg = get_script_content(-argc - 1); + optind = 0; + argc = string_array_len(argv); + goto run_script; + } +#endif while (1) { int opt = getopt(argc, argv, "+c:exinsl" #if !BB_MMU @@ -9974,6 +9987,9 @@ int hush_main(int argc, char **argv) * Note: the form without ARG0 never happens: * sh ... -c 'builtin' BARGV... "" */ +#if NUM_SCRIPTS > 0 + run_script: +#endif if (!G.root_pid) { G.root_pid = getpid(); G.root_ppid = getppid(); diff --git a/util-linux/nologin.c b/util-linux/nologin.c index 0982fff3d..5e5e42305 100644 --- a/util-linux/nologin.c +++ b/util-linux/nologin.c @@ -1,7 +1,7 @@ //config:config NOLOGIN //config: bool "nologin" //config: default y -//config: depends on ASH_EMBEDDED_SCRIPTS +//config: depends on FEATURE_SH_EMBEDDED_SCRIPTS //config: help //config: Politely refuse a login //config: -- cgit v1.2.3-55-g6feb