aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-11-01 09:53:25 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2018-11-01 10:15:13 +0100
commit4f2ef4a836be37b25808c94f41c7c85895db6f93 (patch)
treee224e4f3b8513b35655da80b26369d42bd62d24d /libbb
parent552796791f8c5aa12dfb790e3a94c2d905f367ee (diff)
downloadbusybox-w32-4f2ef4a836be37b25808c94f41c7c85895db6f93.tar.gz
busybox-w32-4f2ef4a836be37b25808c94f41c7c85895db6f93.tar.bz2
busybox-w32-4f2ef4a836be37b25808c94f41c7c85895db6f93.zip
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 <rmy@pobox.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb')
-rw-r--r--libbb/appletlib.c83
-rw-r--r--libbb/lineedit.c6
2 files changed, 88 insertions, 1 deletions
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index 319bcc263..08720082e 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -50,6 +50,16 @@
50 50
51#include "usage_compressed.h" 51#include "usage_compressed.h"
52 52
53#if ENABLE_ASH_EMBEDDED_SCRIPTS
54# define DEFINE_script_names 1
55# include "embedded_scripts.h"
56#else
57# define NUM_SCRIPTS 0
58#endif
59#if NUM_SCRIPTS > 0
60# include "bb_archive.h"
61static const char packed_scripts[] ALIGN1 = { PACKED_SCRIPTS };
62#endif
53 63
54/* "Do not compress usage text if uncompressed text is small 64/* "Do not compress usage text if uncompressed text is small
55 * and we don't include bunzip2 code for other reasons" 65 * and we don't include bunzip2 code for other reasons"
@@ -953,7 +963,71 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **ar
953} 963}
954# endif /* NUM_APPLETS > 0 */ 964# endif /* NUM_APPLETS > 0 */
955 965
956# if ENABLE_BUSYBOX || NUM_APPLETS > 0 966# if NUM_SCRIPTS > 0
967static char *
968unpack_scripts(void)
969{
970 char *outbuf = NULL;
971 bunzip_data *bd;
972 int i;
973 jmp_buf jmpbuf;
974
975 /* Setup for I/O error handling via longjmp */
976 i = setjmp(jmpbuf);
977 if (i == 0) {
978 i = start_bunzip(&jmpbuf,
979 &bd,
980 /* src_fd: */ -1,
981 /* inbuf: */ packed_scripts,
982 /* len: */ sizeof(packed_scripts)
983 );
984 }
985 /* read_bunzip can longjmp and end up here with i != 0
986 * on read data errors! Not trivial */
987 if (i == 0) {
988 outbuf = xmalloc(UNPACKED_SCRIPTS_LENGTH);
989 read_bunzip(bd, outbuf, UNPACKED_SCRIPTS_LENGTH);
990 }
991 dealloc_bunzip(bd);
992 return outbuf;
993}
994
995/*
996 * In standalone shell mode we sometimes want the index of the script
997 * and sometimes the index offset by NUM_APPLETS.
998 */
999static int
1000find_script_by_name(const char *arg)
1001{
1002 const char *s = script_names;
1003 int i = 0;
1004
1005 while (*s) {
1006 if (strcmp(arg, s) == 0)
1007 return i;
1008 i++;
1009 while (*s++ != '\0')
1010 continue;
1011 }
1012 return -1;
1013}
1014
1015char* FAST_FUNC
1016get_script_content(unsigned n)
1017{
1018 char *t = unpack_scripts();
1019 if (t) {
1020 while (n != 0) {
1021 while (*t++ != '\0')
1022 continue;
1023 n--;
1024 }
1025 }
1026 return t;
1027}
1028# endif /* NUM_SCRIPTS > 0 */
1029
1030# if ENABLE_BUSYBOX || NUM_APPLETS > 0 || NUM_SCRIPTS > 0
957static NORETURN void run_applet_and_exit(const char *name, char **argv) 1031static NORETURN void run_applet_and_exit(const char *name, char **argv)
958{ 1032{
959# if ENABLE_BUSYBOX 1033# if ENABLE_BUSYBOX
@@ -968,6 +1042,13 @@ static NORETURN void run_applet_and_exit(const char *name, char **argv)
968 run_applet_no_and_exit(applet, name, argv); 1042 run_applet_no_and_exit(applet, name, argv);
969 } 1043 }
970# endif 1044# endif
1045# if NUM_SCRIPTS > 0
1046 {
1047 int script = find_script_by_name(name);
1048 if (script >= 0)
1049 exit(ash_main(-script - 1, argv));
1050 }
1051# endif
971 1052
972 /*bb_error_msg_and_die("applet not found"); - links in printf */ 1053 /*bb_error_msg_and_die("applet not found"); - links in printf */
973 full_write2_str(applet_name); 1054 full_write2_str(applet_name);
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index b1e971f88..aef1911d9 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -41,6 +41,12 @@
41#include "busybox.h" 41#include "busybox.h"
42#include "NUM_APPLETS.h" 42#include "NUM_APPLETS.h"
43#include "unicode.h" 43#include "unicode.h"
44#if ENABLE_ASH_EMBEDDED_SCRIPTS
45# include "embedded_scripts.h"
46#else
47# define NUM_SCRIPTS 0
48#endif
49
44#ifndef _POSIX_VDISABLE 50#ifndef _POSIX_VDISABLE
45# define _POSIX_VDISABLE '\0' 51# define _POSIX_VDISABLE '\0'
46#endif 52#endif