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 --- shell/Config.src | 14 ++++++++++++++ shell/ash.c | 16 +--------------- shell/hush.c | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 15 deletions(-) (limited to 'shell') 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(); -- cgit v1.2.3