aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

commit c9e91a9878726888ca9d41d267a2a2503015a6dc
parent c54d5a0d78ac987dc317ccda5c0422e244b5398f
Author: xplshn <anto@xplshn.com.ar>
Date:   Thu,  2 Jul 2026 12:33:38 -0300

sh: handle CTRL+C,S,Q,Z properly

Signed-off-by: xplshn <anto@xplshn.com.ar>

Diffstat:
MMakefile | 58++++++++++++++++++++--------------------------------------
MMakefile.sh | 211++++++++++++++++++-------------------------------------------------------------
Mbuild.cfg | 52+++++++++++++++++++++++++++++++---------------------
Mcmd/posix/sh/eval.c | 50+++++++++++++++++++++++++++++++++++++++++++++++++-
Mcmd/posix/sh/exec.c | 60+++++++++++++++++++++++++++++++++++++++++++++---------------
Mcmd/posix/sh/exec.h | 2+-
Mcmd/posix/sh/main.c | 2++
Mcmd/pseudo/which.c | 154++++++++++++++++++++++++++++++++++++++++---------------------------------------
Mscripts/genconfig.sh | 450+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
Mshared/config.h | 14++++++++++----
Mshared/libredline/redline.c | 31++++++++++++++++++++++++++++++-
Mshared/libutil/wexec.c | 371+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
Mshared/util.h | 6++++++
Mshared/wexec.h | 39++++++++++++++++++++++++---------------
14 files changed, 940 insertions(+), 560 deletions(-)

diff --git a/Makefile b/Makefile @@ -519,7 +519,7 @@ $(OBJ) $(POSIX_BIN_ALL) $(LINUX_BIN_ALL) $(NET_BIN_ALL) $(XSI_BIN_ALL) $(PSEUDO_ $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $< $(LIB) $(LDLIBS) cmd/posix/bc.c: cmd/posix/bc.y - $(YACC) -d -o $@ cmd/posix/bc.y + YACC='$(YACC)' sh scripts/genconfig.sh bc cmd/posix/bc: cmd/posix/bc.c $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ cmd/posix/bc.c $(LIB) $(LDLIBS) @@ -547,7 +547,7 @@ shared/libutil/libutil.a: $(LIBUTILOBJ) cmd/posix/getconf: cmd/posix/getconf.h cmd/posix/getconf.h: - scripts/getconf.sh > $@ || { rm -f $@; exit 1; } + sh scripts/genconfig.sh getconf box: $(LIB) CC='$(CC)' CPPFLAGS='$(CPPFLAGS)' CFLAGS='$(CFLAGS)' \ @@ -623,19 +623,13 @@ clean: @rm -f $(LIB) @printf " CLEAN compiled binaries\n" @rm -f $(POSIX_BIN_ALL) $(LINUX_BIN_ALL) $(NET_BIN_ALL) $(XSI_BIN_ALL) $(PSEUDO_BIN_ALL) - @printf " CLEAN generated headers\n" - @rm -f cmd/posix/getconf.h cmd/posix/bc.c cmd/posix/bc.h - @rm -f cmd/posix/awk/awkgram.tab.c cmd/posix/awk/awkgram.tab.h - @rm -f cmd/posix/sh/token.h cmd/posix/sh/syntax.c cmd/posix/sh/syntax.h - @rm -f cmd/posix/sh/nodes.c cmd/posix/sh/nodes.h cmd/posix/sh/builtins.c cmd/posix/sh/builtins.h - @printf " CLEAN generated sources\n" - @rm -f cmd/posix/awk/proctab.c + @sh scripts/genconfig.sh clean @printf " CLEAN build tools\n" - @rm -f cmd/posix/awk/maketab cmd/posix/sh/mknodes cmd/posix/sh/mksyntax @rm -f scripts/mkman/mkman @rm -f scripts/mk/config.kv @rm -f scripts/mk/config.mk @rm -f scripts/mk/rules.mk + @printf " CLEAN dev artifacts\n" @rm -f cmd/dev/cc/cc1 cmd/dev/cc/cpp cmd/dev/as/as cmd/dev/ld/ld cmd/dev/ar/ar shared/libaruuelf.so @rm -f cmd/dev/config.h cmd/dev/cc/config.h cmd/dev/version.h @@ -710,41 +704,29 @@ YAPOBJ =\ cmd/extra/yap/prompt.o\ cmd/extra/yap/term.o -cmd/posix/awk/awkgram.tab.c cmd/posix/awk/awkgram.tab.h: cmd/posix/awk/awkgram.y - $(YACC) -d -o cmd/posix/awk/awkgram.tab.c cmd/posix/awk/awkgram.y - @if [ ! -f cmd/posix/awk/awkgram.tab.h ]; then \ - if [ -f y.tab.h ]; then mv y.tab.h cmd/posix/awk/awkgram.tab.h; \ - elif [ -f cmd/posix/awk/y.tab.h ]; then mv cmd/posix/awk/y.tab.h cmd/posix/awk/awkgram.tab.h; fi; \ - fi - -cmd/posix/awk/maketab: cmd/posix/awk/maketab.c cmd/posix/awk/awkgram.tab.h - $(CC) $(CFLAGS) -o $@ cmd/posix/awk/maketab.c +cmd/posix/awk/awkgram.tab.c: cmd/posix/awk/awkgram.y cmd/posix/awk/maketab.c + CC='$(CC)' CFLAGS='$(CFLAGS)' YACC='$(YACC)' sh scripts/genconfig.sh awk -cmd/posix/awk/proctab.c: cmd/posix/awk/maketab - cmd/posix/awk/maketab cmd/posix/awk/awkgram.tab.h > $@ +cmd/posix/awk/awkgram.tab.h: cmd/posix/awk/awkgram.tab.c ; +cmd/posix/awk/maketab: cmd/posix/awk/awkgram.tab.c ; +cmd/posix/awk/proctab.c: cmd/posix/awk/awkgram.tab.c ; $(AWKOBJ): cmd/posix/awk/awk.h cmd/posix/awk/awkgram.tab.h cmd/posix/awk/proto.h cmd/posix/awk/awk: $(AWKOBJ) $(LIB) $(CC) $(LDFLAGS) -o $@ $(AWKOBJ) $(LIB) $(LDLIBS) -lm -cmd/posix/sh/mknodes: cmd/posix/sh/mknodes.c - $(CC) $(CFLAGS) -o $@ cmd/posix/sh/mknodes.c - -cmd/posix/sh/mksyntax: cmd/posix/sh/mksyntax.c - $(CC) $(CPPFLAGS) -Icmd/posix/sh $(CFLAGS) -o $@ cmd/posix/sh/mksyntax.c - -cmd/posix/sh/syntax.c cmd/posix/sh/syntax.h: cmd/posix/sh/mksyntax - cd cmd/posix/sh && ./mksyntax - -cmd/posix/sh/nodes.c cmd/posix/sh/nodes.h: cmd/posix/sh/mknodes cmd/posix/sh/nodetypes cmd/posix/sh/nodes.c.pat - cd cmd/posix/sh && ./mknodes nodetypes nodes.c.pat - -cmd/posix/sh/builtins.c cmd/posix/sh/builtins.h: cmd/posix/sh/mkbuiltins cmd/posix/sh/builtins.def cmd/posix/sh/shell.h scripts/mk/config.kv - cd cmd/posix/sh && sh mkbuiltins . - -cmd/posix/sh/token.h: cmd/posix/sh/mktokens - cd cmd/posix/sh && sh mktokens +cmd/posix/sh/builtins.c: cmd/posix/sh/mknodes.c cmd/posix/sh/mksyntax.c cmd/posix/sh/nodetypes cmd/posix/sh/nodes.c.pat cmd/posix/sh/mkbuiltins cmd/posix/sh/builtins.def cmd/posix/sh/shell.h cmd/posix/sh/mktokens + CC='$(CC)' CFLAGS='$(CFLAGS)' CPPFLAGS='$(CPPFLAGS)' sh scripts/genconfig.sh sh + +cmd/posix/sh/syntax.c: cmd/posix/sh/builtins.c ; +cmd/posix/sh/syntax.h: cmd/posix/sh/builtins.c ; +cmd/posix/sh/nodes.c: cmd/posix/sh/builtins.c ; +cmd/posix/sh/nodes.h: cmd/posix/sh/builtins.c ; +cmd/posix/sh/builtins.h: cmd/posix/sh/builtins.c ; +cmd/posix/sh/token.h: cmd/posix/sh/builtins.c ; +cmd/posix/sh/mknodes: cmd/posix/sh/builtins.c ; +cmd/posix/sh/mksyntax: cmd/posix/sh/builtins.c ; $(SHOBJ): $(SH_GENHDRS) diff --git a/Makefile.sh b/Makefile.sh @@ -25,14 +25,39 @@ while [ $# -gt 0 ]; do shift done +# timestamp comparison +# +mtime() { + stat -c '%Y' "$1" 2>/dev/null || stat -f '%m' "$1" 2>/dev/null || echo 0 +} +newer_than() { + local s t + [ -e "$2" ] || return 0 + s=$(mtime "$1"); t=$(mtime "$2") + [ "$s" -gt "$t" ] +} +any_newer_than() { + local target="$1"; shift + for src do + newer_than "$src" "$target" && return 0 + done + return 1 +} + [ -f build.cfg ] || { printf 'build.cfg not found; see the repository README\n' >&2; exit 1; } + +# generate config files if missing or build.cfg is newer +if [ ! -f scripts/mk/config.kv ] || newer_than build.cfg scripts/mk/config.kv; then + sh scripts/genconfig.sh +fi + # shellcheck disable=SC1091 . ./build.cfg # load config.kv if it exists to override and provide detected TLS settings if [ -f scripts/mk/config.kv ]; then # shellcheck disable=SC1091 - . scripts/mk/config.kv + . ./scripts/mk/config.kv fi # cppflags derived from build.cfg; any feature_* added there is picked up automatically @@ -69,25 +94,6 @@ LIB="shared/libredline/libredline.a shared/libutil/libutil.a shared/libutf/libut # set by multi-file builders for generated headers, cleared after use EXTRA_HDR= -# timestamp comparison -# -mtime() { - stat -c '%Y' "$1" 2>/dev/null || stat -f '%m' "$1" 2>/dev/null || echo 0 -} -newer_than() { - local s t - [ -e "$2" ] || return 0 - s=$(mtime "$1"); t=$(mtime "$2") - [ "$s" -gt "$t" ] -} -any_newer_than() { - local target="$1"; shift - for src do - newer_than "$src" "$target" && return 0 - done - return 1 -} - # parallel job queue # objs_for must not run in a subshell, enqueue writes _qn/_queue in the current shell # @@ -238,59 +244,28 @@ cfg_enabled() { } build_simple_tools() { - local cat="$1" src base var staged="" + local cat="$1" src base var for src in cmd/"$cat"/*.c; do [ -f "$src" ] || continue base=${src##*/}; base=${base%.c} var=$(cfgvar "$cat" "$base") cfg_enabled "$var" || continue - compile_c "$src" "${src%.c}.o" - staged="$staged ${src%.c}" - done - drain - local bin - for bin in $staged; do - if [ "${bin##*/}" = "diff" ]; then - link_bin "$bin" "${bin}.o" -- $LIB -lm + local bin="cmd/$cat/$base" + # shellcheck disable=SC2086 + any_newer_than "$bin" "$src" $HDR $EXTRA_HDR $LIB || continue + if [ "$base" = "diff" ]; then + enqueue " CC $bin" "$CC $src $CPPFLAGS $CFLAGS $LDFLAGS -o $bin $LIB -lm $LDLIBS" else - link_bin "$bin" "${bin}.o" -- $LIB + enqueue " CC $bin" "$CC $src $CPPFLAGS $CFLAGS $LDFLAGS -o $bin $LIB $LDLIBS" fi done + drain } build_awk() { cfg_enabled BUILD_POSIX_AWK || return 0 local dir=cmd/posix/awk - - any_newer_than "$dir/awkgram.tab.c" "$dir/awkgram.y" && { - if command -v yacc >/dev/null 2>&1; then - printf ' YACC %s/awkgram.tab.c\n' "$dir" - yacc -d -o "$dir/awkgram.tab.c" "$dir/awkgram.y" - if [ ! -f "$dir/awkgram.tab.h" ]; then - if [ -f y.tab.h ]; then - mv y.tab.h "$dir/awkgram.tab.h" - elif [ -f "$dir/y.tab.h" ]; then - mv "$dir/y.tab.h" "$dir/awkgram.tab.h" - fi - fi - elif command -v bison >/dev/null 2>&1; then - printf ' BISON %s/awkgram.tab.c\n' "$dir" - bison -d -o "$dir/awkgram.tab.c" "$dir/awkgram.y" - else - printf 'awk: no yacc/bison; using pre-generated awkgram.tab.c\n' >&2 - fi - } - - any_newer_than "$dir/maketab" "$dir/maketab.c" "$dir/awkgram.tab.h" && { - printf ' CC %s/maketab\n' "$dir" - eval "$CC $CFLAGS -o $dir/maketab $dir/maketab.c" - } - - any_newer_than "$dir/proctab.c" "$dir/maketab" && { - printf ' GEN %s/proctab.c\n' "$dir" - "$dir/maketab" "$dir/awkgram.tab.h" > "$dir/proctab.c" - } - + CC="$CC" CFLAGS="$CFLAGS" YACC="$YACC" sh scripts/genconfig.sh awk EXTRA_HDR="$dir/awk.h $dir/awkgram.tab.h $dir/proto.h" objs_for "$dir" maketab.c "-I$dir" EXTRA_HDR= @@ -300,37 +275,7 @@ build_awk() { build_sh() { cfg_enabled BUILD_POSIX_SH || return 0 local dir=cmd/posix/sh - - any_newer_than "$dir/mknodes" "$dir/mknodes.c" && { - printf ' CC %s/mknodes\n' "$dir" - eval "$CC $CFLAGS -o $dir/mknodes $dir/mknodes.c" - } - - any_newer_than "$dir/mksyntax" "$dir/mksyntax.c" && { - printf ' CC %s/mksyntax\n' "$dir" - eval "$CC $CPPFLAGS -I$dir $CFLAGS -o $dir/mksyntax $dir/mksyntax.c" - } - - any_newer_than "$dir/token.h" "$dir/mktokens" && { - printf ' GEN %s/token.h\n' "$dir" - (cd "$dir" && sh mktokens) - } - - any_newer_than "$dir/syntax.c" "$dir/mksyntax" && { - printf ' GEN %s/syntax.c\n' "$dir" - (cd "$dir" && ./mksyntax) - } - - any_newer_than "$dir/nodes.c" "$dir/mknodes" "$dir/nodetypes" "$dir/nodes.c.pat" && { - printf ' GEN %s/nodes.c\n' "$dir" - (cd "$dir" && ./mknodes nodetypes nodes.c.pat) - } - - any_newer_than "$dir/builtins.c" "$dir/mkbuiltins" "$dir/builtins.def" "$dir/shell.h" scripts/mk/config.kv && { - printf ' GEN %s/builtins.c\n' "$dir" - (cd "$dir" && sh mkbuiltins .) - } - + CC="$CC" CFLAGS="$CFLAGS" CPPFLAGS="$CPPFLAGS" sh scripts/genconfig.sh sh EXTRA_HDR="$dir/syntax.h $dir/nodes.h $dir/builtins.h $dir/token.h" objs_for "$dir" "mknodes.c mksyntax.c" "-DSHELL -I$dir" EXTRA_HDR= @@ -349,28 +294,15 @@ build_make() { build_bc() { cfg_enabled BUILD_POSIX_BC || return 0 local src=cmd/posix/bc.c - - any_newer_than "$src" cmd/posix/bc.y && { - printf ' GEN %s\n' "$src" - if command -v yacc >/dev/null 2>&1; then - yacc -o "$src" cmd/posix/bc.y - elif command -v bison >/dev/null 2>&1; then - bison -o "$src" cmd/posix/bc.y - else - printf 'bc: no yacc/bison found\n' >&2; return 1 - fi - } - - compile_c "$src" "${src%.c}.o" - drain - link_bin cmd/posix/bc "${src%.c}.o" -- $LIB + YACC="$YACC" sh scripts/genconfig.sh bc + # shellcheck disable=SC2086 + any_newer_than cmd/posix/bc "$src" $HDR $EXTRA_HDR $LIB || return 0 + printf ' CC cmd/posix/bc\n' + eval "$CC $src $CPPFLAGS $CFLAGS $LDFLAGS -o cmd/posix/bc $LIB $LDLIBS" } build_posix() { - cfg_enabled BUILD_POSIX_GETCONF && [ ! -f cmd/posix/getconf.h ] && { - printf ' GEN cmd/posix/getconf.h\n' - scripts/getconf.sh > cmd/posix/getconf.h || { rm -f cmd/posix/getconf.h; exit 1; } - } + cfg_enabled BUILD_POSIX_GETCONF && sh scripts/genconfig.sh getconf build_simple_tools posix build_awk build_bc @@ -521,49 +453,9 @@ do_regen() { . scripts/mk/config.kv fi - # 3. clean the generated files to force regeneration - rm -f cmd/posix/getconf.h cmd/posix/bc.c cmd/posix/bc.h - rm -f cmd/posix/awk/awkgram.tab.c cmd/posix/awk/awkgram.tab.h cmd/posix/awk/proctab.c cmd/posix/awk/maketab - rm -f cmd/posix/sh/mknodes cmd/posix/sh/mksyntax cmd/posix/sh/token.h cmd/posix/sh/syntax.c cmd/posix/sh/syntax.h cmd/posix/sh/nodes.c cmd/posix/sh/nodes.h cmd/posix/sh/builtins.c cmd/posix/sh/builtins.h - - # 4. run the generation steps - # for getconf.h - printf ' GEN cmd/posix/getconf.h\n' - scripts/getconf.sh > cmd/posix/getconf.h || { rm -f cmd/posix/getconf.h; exit 1; } - - # for bc.c from bc.y - printf ' GEN cmd/posix/bc.c\n' - if command -v yacc >/dev/null 2>&1; then - yacc -o cmd/posix/bc.c cmd/posix/bc.y - elif command -v bison >/dev/null 2>&1; then - bison -o cmd/posix/bc.c cmd/posix/bc.y - fi - - # for awk - local adir=cmd/posix/awk - if command -v yacc >/dev/null 2>&1; then - yacc -d -o "$adir/awkgram.tab.c" "$adir/awkgram.y" - if [ ! -f "$adir/awkgram.tab.h" ]; then - if [ -f y.tab.h ]; then - mv y.tab.h "$adir/awkgram.tab.h" - elif [ -f "$adir/y.tab.h" ]; then - mv "$adir/y.tab.h" "$adir/awkgram.tab.h" - fi - fi - elif command -v bison >/dev/null 2>&1; then - bison -d -o "$adir/awkgram.tab.c" "$adir/awkgram.y" - fi - eval "$CC $CFLAGS -o $adir/maketab $adir/maketab.c" - "$adir/maketab" "$adir/awkgram.tab.h" > "$adir/proctab.c" - - # for sh - local sdir=cmd/posix/sh - eval "$CC $CFLAGS -o $sdir/mknodes $sdir/mknodes.c" - eval "$CC $CPPFLAGS -I$sdir $CFLAGS -o $sdir/mksyntax $sdir/mksyntax.c" - (cd "$sdir" && sh mktokens) - (cd "$sdir" && ./mksyntax) - (cd "$sdir" && ./mknodes nodetypes nodes.c.pat) - (cd "$sdir" && sh mkbuiltins .) + # 3. clean and generate files using scripts/genconfig.sh + CC="$CC" CFLAGS="$CFLAGS" CPPFLAGS="$CPPFLAGS" YACC="$YACC" sh scripts/genconfig.sh clean + CC="$CC" CFLAGS="$CFLAGS" CPPFLAGS="$CPPFLAGS" YACC="$YACC" sh scripts/genconfig.sh all } do_clean() { @@ -573,18 +465,9 @@ do_clean() { find shared -name '*.a' -exec rm -f {} + printf ' CLEAN compiled binaries\n' find cmd -type f ! -name '*.*' -perm -100 -exec rm -f {} + - printf ' CLEAN generated headers\n' - rm -f cmd/posix/getconf.h - rm -f cmd/posix/awk/awkgram.tab.c cmd/posix/awk/awkgram.tab.h - rm -f cmd/posix/sh/token.h cmd/posix/sh/syntax.c cmd/posix/sh/syntax.h - rm -f cmd/posix/sh/nodes.c cmd/posix/sh/nodes.h - rm -f cmd/posix/sh/builtins.c cmd/posix/sh/builtins.h - printf ' CLEAN generated sources\n' - rm -f cmd/posix/awk/proctab.c - rm -f cmd/posix/bc.c + sh scripts/genconfig.sh clean + rm -f scripts/mk/config.mk scripts/mk/config.kv scripts/mk/rules.mk shared/libutil/nofork_list.h printf ' CLEAN build tools\n' - rm -f cmd/posix/awk/maketab - rm -f cmd/posix/sh/mknodes cmd/posix/sh/mksyntax rm -f scripts/mkman/mkman printf ' CLEAN dev artifacts\n' rm -f cmd/dev/cc/cc1 cmd/dev/cc/cpp cmd/dev/as/as cmd/dev/ld/ld cmd/dev/ar/ar diff --git a/build.cfg b/build.cfg @@ -107,9 +107,10 @@ all) FEATURE_SORT_BIG=1; FEATURE_SORT_STABLE=1 FEATURE_OD_ENDIAN=1; FEATURE_SH_EASTER=1; FEATURE_SH_HISTEDIT=1 - FEATURE_INPROC_EXEC=0; FEATURE_SH_LOCAL=1 - FEATURE_SH_LET=1; FEATURE_SH_ULIMIT=1 - FEATURE_SH_SETVAR=1; FEATURE_SH_WORDEXP=1 + FEATURE_SH_LOCAL=1 FEATURE_NOEXEC=0; + FEATURE_NOFORK=0; FEATURE_SH_LET=1; + FEATURE_SH_ULIMIT=1 FEATURE_SH_SETVAR=1; + FEATURE_SH_WORDEXP=1 FEATURE_CAL_EXT=1 FEATURE_LS_COLOR=1 FEATURE_DIFF3_MERGE=1; FEATURE_DIFF3_EDSCRIPT=1 @@ -158,30 +159,39 @@ unset _p # BUILD_DEV_AS=1 # enable a tool whose group isnt in PRESET_UTILS FEATURE_SH_HISTEDIT=1 -# FEATURE_INPROC_EXEC: when set to 1, wexecvp/wexecv/wpopen/wsystem call -# registered tool mains directly instead of fork+exec. this is only meaningful -# inside aruu-box, where every tool has registered its main at startup +# FEATURE_NOEXEC: when set to 1, wexec/wpopen/wsystem can execute registered +# tool mains directly inside the forked child process (skipping execve) +# This is equivalent to Busybox noexec applet support. +# This applies to ALL commands contained within aruu-box, you may also +# specify if some specific command is not an attack vector to also be nofork +# via enabling FEATURE_NOFORK=1 and specifying those commands in NOFORK_LIST # -# the analogy is busybox: a single binary where sh can call grep, sed, awk, etc. -# without spawning a new process for each one. scripts run faster and the whole -# system needs no external PATH at all. aruu-box built with this flag is fully -# self-contained, exactly like a busybox applet collection +#FEATURE_NOEXEC=1 # -# this is optional. set to 0 to build the box without inproc dispatch (each -# tool call still fork+execs). for individual standalone tool binaries this -# flag has no practical effect because no other mains are registered +# FEATURE_NOEXEC_OVERRIDE: only meaningful when FEATURE_NOEXEC=1 +# when set to 1, ARUU_NOEXEC=0 in the environment forces fork+exec at runtime # -FEATURE_INPROC_EXEC=1 +#FEATURE_NOEXEC_OVERRIDE=1 # -# FEATURE_INPROC_EXEC_OVERRIDE: only meaningful when FEATURE_INPROC_EXEC=1 -# when set to 1, ARUU_INPROC_EXEC=0 in the environment forces fork+exec at -# runtime, letting a person reach an external tool that shares a name with -# a builtin without giving its full path. when set to 0, that env var is -# never read at all and inproc dispatch cannot be bypassed except by a tool -# explicitly calling wexec_set_inproc() +# FEATURE_NOFORK: when set to 1, wexecvp/wexecv can run simple commands +# (like cat, echo, pwd, test) directly in the current process without spawning +# a subprocess (noexec+nofork). This is equivalent to Busybox nofork applet support # -FEATURE_INPROC_EXEC_OVERRIDE=1 +FEATURE_NOFORK=1 # +# FEATURE_NOFORK_OVERRIDE: only meaningful when FEATURE_NOFORK=1 +# when set to 1, ARUU_NOFORK=0 in the environment forces fork for nofork commands +# even tho they will still not do execvp() call at runtime +# unless FEATURE_NOEXEC_OVERRIDE was enabled at build time and ARUU_NOEXEC=0 +# +#FEATURE_NOFORK_OVERRIDE=1 +# +# NOFORK_LIST: list of builtins that can run without fork (noexec+nofork) +# +NOFORK_LIST="true false echo pwd test [ basename dirname cat clear yes" +# + +BUILD_EXTRA_YAP=0 # tls backend: set exactly one to 1 to enable tls # diff --git a/cmd/posix/sh/eval.c b/cmd/posix/sh/eval.c @@ -43,6 +43,7 @@ */ #include "../../../shared/paths.h" +#include "../../../shared/wexec.h" #include "builtins.h" #include "error.h" #include "eval.h" @@ -999,7 +1000,8 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd) } /* Fork off a child process if necessary. */ - if (((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN) + if (((cmdentry.cmdtype == CMDNORMAL || cmdentry.cmdtype == CMDUNKNOWN + || (cmdentry.cmdtype == CMDWEXEC && !(wexec_get_nofork() && wexec_is_nofork(argv[0])))) && ((flags & EV_EXIT) == 0 || have_traps())) || ((flags & EV_BACKCMD) != 0 && (cmdentry.cmdtype != CMDBUILTIN || !safe_builtin(cmdentry.u.index, argc, argv)))) { @@ -1094,6 +1096,52 @@ evalcommand(union node *cmd, int flags, struct backcmd *backcmd) } if (jp) exitshell(exitstatus); + } else if (cmdentry.cmdtype == CMDWEXEC) { + savecmdname = commandname; + savetopfile = getcurrentfile(); + cmdenviron = &varlist; + e = -1; + savehandler = handler; + if (setjmp(jmploc.loc)) { + e = exception; + if (e == EXINT) + exitstatus = SIGINT + 128; + goto cmddone_wexec; + } + handler = &jmploc; + mode = REDIR_PUSH; + if (flags == EV_BACKCMD) { + memout.nextc = memout.buf; + mode |= REDIR_BACKQ; + } + redirect(cmd->ncmd.redirect, mode); + outclearerror(out1); + listsetvar(cmdenviron, VNOSET); + commandname = argv[0]; + exitstatus = wexecvp(argv[0], argv); + flushall(); + if (outiserror(out1)) { + warning("write error on stdout"); + if (exitstatus == 0 || exitstatus == 1) + exitstatus = 2; + } + cmddone_wexec: + cmdenviron = NULL; + out1 = &output; + out2 = &errout; + freestdout(); + handler = savehandler; + commandname = savecmdname; + if (jp) + exitshell(exitstatus); + if (flags == EV_BACKCMD) { + backcmd->buf = memout.buf; + backcmd->nleft = memout.buf != NULL ? memout.nextc - memout.buf : 0; + memout.buf = NULL; + memout.nextc = NULL; + memout.bufend = NULL; + memout.bufsize = 64; + } } else if (cmdentry.cmdtype == CMDBUILTIN) { #ifdef DEBUG trputs("builtin command: "); diff --git a/cmd/posix/sh/exec.c b/cmd/posix/sh/exec.c @@ -34,6 +34,7 @@ #include <errno.h> #include <fcntl.h> +#include <signal.h> #include <stdlib.h> #include <sys/stat.h> #include <sys/types.h> @@ -102,17 +103,35 @@ shellexec(char **argv, char **envp, const char *path, int idx) char *cmdname; const char *opt; int e; +#if FEATURE_NOEXEC + struct sigaction sa; + int i; + int ret; +#endif -#if FEATURE_INPROC_EXEC +#if FEATURE_NOEXEC { /* basename of argv[0] for slash-free lookup */ - const char *base = strrchr(argv[0], '/'); - base = base ? base + 1 : argv[0]; - if (wexec_get_inproc() && wexec_is_builtin(base)) { - int ret; + const char *base = strrchr(argv[0], '/'); + extern char **environ; + base = base ? base + 1 : argv[0]; + environ = envp; + if (wexec_get_noexec() && wexec_is_builtin(base)) { + /* reset caught signal handlers to default for inproc command */ + for (i = 1; i < NSIG; i++) { + if (sigaction(i, NULL, &sa) == 0) { + if (sa.sa_handler != SIG_IGN && sa.sa_handler != SIG_DFL) { + sa.sa_handler = SIG_DFL; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + sigaction(i, &sa, NULL); + } + } + } + fflush(stdout); fflush(stderr); - ret = wexecvp(base, argv); + ret = wexec_call_builtin(base, argv); fflush(stdout); fflush(stderr); _exit(ret); @@ -331,9 +350,9 @@ printentry(struct tblentry *cmdp, int verbose) ckfree(name); INTON; } -#if FEATURE_INPROC_EXEC +#if FEATURE_NOEXEC || FEATURE_NOFORK } else if (cmdp->cmdtype == CMDWEXEC) { - out1fmt("inproc %s", cmdp->cmdname); + out1fmt("builtin %s", cmdp->cmdname); #endif #ifdef DEBUG } else { @@ -392,10 +411,10 @@ find_command(const char *name, struct cmdentry *entry, int act, const char *path goto success; } -#if FEATURE_INPROC_EXEC - /* when inproc dispatch is active, registered wexec builtins are available +#if FEATURE_NOEXEC || FEATURE_NOFORK + /* when builtin dispatch is active, registered wexec builtins are available * even if they are not present on the filesystem */ - if (wexec_get_inproc() && wexec_is_builtin(name)) { + if ((wexec_get_noexec() || wexec_get_nofork()) && wexec_is_builtin(name)) { INTOFF; cmdp = cmdlookup(name, 1); if (cmdp->cmdtype == CMDFUNCTION) @@ -821,12 +840,23 @@ typecmd_impl(int argc, char **argv, int cmd, const char *path) out1fmt("%s is a shell builtin\n", argv[i]); break; -#if FEATURE_INPROC_EXEC +#if FEATURE_NOEXEC || FEATURE_NOFORK case CMDWEXEC: - if (cmd == TYPECMD_SMALLV) + if (cmd == TYPECMD_SMALLV) { out1fmt("%s\n", argv[i]); - else - out1fmt("%s is an inproc builtin\n", argv[i]); + } else { + if (wexec_get_nofork() && wexec_is_nofork(argv[i])) { + out1fmt( + "%s is embedded via wexec and registered by mkbox & genconfig.sh as " + "noexec+nofork\n", + argv[i] + ); + } else if (wexec_get_noexec()) { + out1fmt("%s is embedded via wexec and registered by mkbox as noexec\n", argv[i]); + } else { + out1fmt("%s is a shell builtin\n", argv[i]); + } + } break; #endif diff --git a/cmd/posix/sh/exec.h b/cmd/posix/sh/exec.h @@ -39,7 +39,7 @@ #define CMDNORMAL 0 /* command is an executable program */ #define CMDBUILTIN 1 /* command is a shell builtin */ #define CMDFUNCTION 2 /* command is a shell function */ -#define CMDWEXEC 3 /* command is a registered inproc builtin */ +#define CMDWEXEC 3 /* command is a registered builtin applet */ /* values for typecmd_impl's third parameter */ enum { diff --git a/cmd/posix/sh/main.c b/cmd/posix/sh/main.c @@ -42,6 +42,7 @@ #include <unistd.h> #include "../../../shared/paths.h" +#include "../../../shared/wexec.h" #include "builtins.h" #include "cd.h" #include "error.h" @@ -100,6 +101,7 @@ main(int argc, char *argv[]) int login; (void)setlocale(LC_ALL, ""); + wexec_register_env_lookup(lookupvar); initcharset(); state = 0; if (setjmp(main_handler.loc)) { diff --git a/cmd/pseudo/which.c b/cmd/pseudo/which.c @@ -1,7 +1,7 @@ /* See LICENSE file for copyright and license details. */ -#include "wexec.h" -#include "util.h" #include "arg.h" +#include "util.h" +#include "wexec.h" #include <fcntl.h> #include <limits.h> @@ -17,56 +17,56 @@ static int aflag; static int canexec(int fd, const char *name) { - struct stat st; + struct stat st; - if (fstatat(fd, name, &st, 0) < 0 || !S_ISREG(st.st_mode)) - return 0; - return faccessat(fd, name, X_OK, AT_EACCESS) == 0; + if (fstatat(fd, name, &st, 0) < 0 || !S_ISREG(st.st_mode)) + return 0; + return faccessat(fd, name, X_OK, AT_EACCESS) == 0; } static int which(const char *path, const char *name) { - char *ptr, *p; - size_t i, len; - int dirfd, found = 0; + char *ptr, *p; + size_t i, len; + int dirfd, found = 0; - if (strchr(name, '/')) { - found = canexec(AT_FDCWD, name); - if (found) - puts(name); - return found; - } + if (strchr(name, '/')) { + found = canexec(AT_FDCWD, name); + if (found) + puts(name); + return found; + } - ptr = p = enstrdup(3, path); - len = strlen(p); - for (i = 0; i < len + 1; i++) { - if (ptr[i] != ':' && ptr[i] != '\0') - continue; - ptr[i] = '\0'; - if ((dirfd = open(p, O_RDONLY)) >= 0) { - if (canexec(dirfd, name)) { - found = 1; - fputs(p, stdout); - if (i && ptr[i - 1] != '/') - fputc('/', stdout); - puts(name); - } - close(dirfd); - if (!aflag && found) - break; - } - p = ptr + i + 1; - } - free(ptr); + ptr = p = enstrdup(3, path); + len = strlen(p); + for (i = 0; i < len + 1; i++) { + if (ptr[i] != ':' && ptr[i] != '\0') + continue; + ptr[i] = '\0'; + if ((dirfd = open(p, O_RDONLY)) >= 0) { + if (canexec(dirfd, name)) { + found = 1; + fputs(p, stdout); + if (i && ptr[i - 1] != '/') + fputc('/', stdout); + puts(name); + } + close(dirfd); + if (!aflag && found) + break; + } + p = ptr + i + 1; + } + free(ptr); - return found; + return found; } static void usage(void) { - eprintf("usage: %s [-a] name ...\n", argv0); + eprintf("usage: %s [-a] name ...\n", argv0); } // ?man which: locate a command @@ -75,49 +75,51 @@ usage(void) int main(int argc, char *argv[]) { - char *path; - int found = 0, foundall = 1; + char *path; + int found = 0, foundall = 1; - ARGBEGIN { - // ?man -a: print or show all entries - case 'a': - aflag = 1; - break; - default: - usage(); - } ARGEND + ARGBEGIN + { + // ?man -a: print or show all entries + case 'a': + aflag = 1; + break; + default: + usage(); + } + ARGEND - if (!argc) - usage(); + if (!argc) + usage(); - if (!(path = getenv("PATH"))) - enprintf(3, "$PATH is not set\n"); + if (!(path = getenv("PATH"))) + enprintf(3, "$PATH is not set\n"); - for (; *argv; argc--, argv++) { -#if FEATURE_INPROC_EXEC - /* inproc builtins are function pointers with no filesystem path */ - if (wexec_get_inproc() && wexec_is_builtin(*argv)) { - printf("%s: inproc builtin\n", *argv); - found = 1; - if (!aflag) - continue; - } + for (; *argv; argc--, argv++) { +#if FEATURE_NOEXEC + /* builtins are function pointers with no filesystem path */ + if (wexec_get_noexec() && wexec_is_builtin(*argv)) { + printf("%s: builtin applet\n", *argv); + found = 1; + if (!aflag) + continue; + } #endif - if (which(path, *argv)) { - found = 1; - } else { -#if FEATURE_INPROC_EXEC - /* already reported above when inproc was active */ - if (!wexec_get_inproc() || !wexec_is_builtin(*argv)) + if (which(path, *argv)) { + found = 1; + } else { +#if FEATURE_NOEXEC + /* already reported above when builtin was active */ + if (!wexec_get_noexec() || !wexec_is_builtin(*argv)) #endif - { - weprintf("%s: not an external command\n", *argv); - foundall = 0; - } - } - } + { + weprintf("%s: not an external command\n", *argv); + foundall = 0; + } + } + } - if (fshut(stdout, "<stdout>")) - return 2; - return found ? foundall ? 0 : 1 : 2; + if (fshut(stdout, "<stdout>")) + return 2; + return found ? foundall ? 0 : 1 : 2; } diff --git a/scripts/genconfig.sh b/scripts/genconfig.sh @@ -3,6 +3,8 @@ set -e +target=${1:-config} + # load build.cfg if [ -f build.cfg ]; then . ./build.cfg @@ -11,45 +13,128 @@ else exit 1 fi -mkdir -p scripts/mk - -mk_file="scripts/mk/config.mk" -kv_file="scripts/mk/config.kv" - -# clear existing files -: > "${mk_file}" -: > "${kv_file}" - -# write version prefix manprefix -echo "VERSION = ${VERSION}" >> "${mk_file}" -echo "VERSION=\"${VERSION}\"" >> "${kv_file}" - -echo "PREFIX = ${PREFIX}" >> "${mk_file}" -echo "PREFIX=\"${PREFIX}\"" >> "${kv_file}" +# helper to check if source is newer than target +newer_than() { + [ -e "$2" ] || return 0 + local s t + s=$(stat -c '%Y' "$1" 2>/dev/null || stat -f '%m' "$1" 2>/dev/null || echo 0) + t=$(stat -c '%Y' "$2" 2>/dev/null || stat -f '%m' "$2" 2>/dev/null || echo 0) + [ "$s" -gt "$t" ] +} -# default manprefix to prefix/share/man if empty -man_pref="${MANPREFIX:-/usr/local/share/man}" -echo "MANPREFIX = ${man_pref}" >> "${mk_file}" -echo "MANPREFIX=\"${man_pref}\"" >> "${kv_file}" +gen_getconf() { + if newer_than scripts/getconf.sh cmd/posix/getconf.h; then + printf " GEN cmd/posix/getconf.h\n" + scripts/getconf.sh > cmd/posix/getconf.h || { rm -f cmd/posix/getconf.h; exit 1; } + fi +} -# toolchain defaults -echo "RANLIB = ${RANLIB:-ranlib}" >> "${mk_file}" -echo "RANLIB=\"${RANLIB:-ranlib}\"" >> "${kv_file}" +gen_bc() { + if newer_than cmd/posix/bc.y cmd/posix/bc.c; then + printf " GEN cmd/posix/bc.c\n" + CC=${CC:-cc} + YACC=${YACC:-} + if [ -z "$YACC" ]; then + if command -v yacc >/dev/null 2>&1; then + YACC="yacc" + elif command -v bison >/dev/null 2>&1; then + YACC="bison -y" + else + YACC="yacc" + fi + fi + if [ "$YACC" = "bison -y" ]; then + bison -o cmd/posix/bc.c cmd/posix/bc.y + else + $YACC -o cmd/posix/bc.c cmd/posix/bc.y + fi + fi +} -echo "AR = ${AR:-ar}" >> "${mk_file}" -echo "AR=\"${AR:-ar}\"" >> "${kv_file}" +gen_awk() { + local dir="cmd/posix/awk" + CC=${CC:-cc} + YACC=${YACC:-} + if [ -z "$YACC" ]; then + if command -v yacc >/dev/null 2>&1; then + YACC="yacc" + elif command -v bison >/dev/null 2>&1; then + YACC="bison -y" + else + YACC="yacc" + fi + fi + + if newer_than "$dir/awkgram.y" "$dir/awkgram.tab.c" || newer_than "$dir/awkgram.y" "$dir/awkgram.tab.h"; then + printf " YACC $dir/awkgram.tab.c\n" + if [ "$YACC" = "bison -y" ]; then + bison -d -o "$dir/awkgram.tab.c" "$dir/awkgram.y" + else + $YACC -d -o "$dir/awkgram.tab.c" "$dir/awkgram.y" + if [ ! -f "$dir/awkgram.tab.h" ]; then + if [ -f y.tab.h ]; then + mv y.tab.h "$dir/awkgram.tab.h" + elif [ -f "$dir/y.tab.h" ]; then + mv "$dir/y.tab.h" "$dir/awkgram.tab.h" + fi + fi + fi + fi + + if newer_than "$dir/maketab.c" "$dir/maketab" || newer_than "$dir/awkgram.tab.h" "$dir/maketab"; then + printf " CC $dir/maketab\n" + $CC -o "$dir/maketab" "$dir/maketab.c" + fi + + if newer_than "$dir/maketab" "$dir/proctab.c" || newer_than "$dir/awkgram.tab.h" "$dir/proctab.c"; then + printf " GEN $dir/proctab.c\n" + "$dir/maketab" "$dir/awkgram.tab.h" > "$dir/proctab.c" + fi +} -echo "ARFLAGS = ${ARFLAGS:-rc}" >> "${mk_file}" -echo "ARFLAGS=\"${ARFLAGS:-rc}\"" >> "${kv_file}" +gen_sh() { + local dir="cmd/posix/sh" + CC=${CC:-cc} + + if newer_than "$dir/mknodes.c" "$dir/mknodes"; then + printf " CC $dir/mknodes\n" + $CC -o "$dir/mknodes" "$dir/mknodes.c" + fi + + if newer_than "$dir/mksyntax.c" "$dir/mksyntax"; then + printf " CC $dir/mksyntax\n" + $CC -Ishared -I"$dir" -o "$dir/mksyntax" "$dir/mksyntax.c" + fi + + if newer_than "$dir/mktokens" "$dir/token.h"; then + printf " GEN $dir/token.h\n" + (cd "$dir" && sh mktokens) + fi + + if newer_than "$dir/mksyntax" "$dir/syntax.c" || newer_than "$dir/mksyntax" "$dir/syntax.h"; then + printf " GEN $dir/syntax.c\n" + (cd "$dir" && ./mksyntax) + fi + + if newer_than "$dir/mknodes" "$dir/nodes.c" || newer_than "$dir/nodetypes" "$dir/nodes.c" || newer_than "$dir/nodes.c.pat" "$dir/nodes.c"; then + printf " GEN $dir/nodes.c\n" + (cd "$dir" && ./mknodes nodetypes nodes.c.pat) + fi + + if newer_than "$dir/mkbuiltins" "$dir/builtins.c" || newer_than "$dir/builtins.def" "$dir/builtins.c" || newer_than "$dir/shell.h" "$dir/builtins.c" || [ ! -f "$dir/builtins.c" ]; then + printf " GEN $dir/builtins.c\n" + (cd "$dir" && sh mkbuiltins .) + fi +} -# build toggles -echo "" >> "${mk_file}" -echo "# build toggles" >> "${mk_file}" -for g in BUILD_POSIX BUILD_XSI BUILD_NET BUILD_PSEUDO BUILD_EXTRA BUILD_LINUX BUILD_DEV; do - eval "val=\${$g:-0}" - echo "$g = $val" >> "${mk_file}" - echo "$g=\"$val\"" >> "${kv_file}" -done +clean() { + printf " CLEAN generated files\n" + rm -f cmd/posix/getconf.h + rm -f cmd/posix/bc.c cmd/posix/bc.h + rm -f cmd/posix/awk/awkgram.tab.c cmd/posix/awk/awkgram.tab.h cmd/posix/awk/proctab.c cmd/posix/awk/maketab + rm -f cmd/posix/sh/mknodes cmd/posix/sh/mksyntax cmd/posix/sh/token.h cmd/posix/sh/syntax.c cmd/posix/sh/syntax.h cmd/posix/sh/nodes.c cmd/posix/sh/nodes.h cmd/posix/sh/builtins.c cmd/posix/sh/builtins.h + rm -f scripts/mk/config.mk scripts/mk/config.kv scripts/mk/rules.mk shared/libutil/nofork_list.h +} # dynamic tool discovery function list_tools() { @@ -100,110 +185,6 @@ write_subtarget() { fi } -# write subtargets for each category dynamically -for catg in posix xsi net pseudo extra linux dev; do - echo "" >> "${mk_file}" - echo "# ${catg} subtargets" >> "${mk_file}" - tools=$(list_tools "$catg") - for t in $tools; do - write_subtarget "$catg" "$t" - done - - # build the enabled list using lowercase tools - enabled="" - for t in $tools; do - u_group=$(echo "$catg" | tr '[:lower:]' '[:upper:]') - u_tool=$(echo "$t" | tr 'a-z-' 'A-Z_') - - # special cases for dev tools mapping to different variables - if [ "$catg" = "dev" ] && [ "$u_tool" = "AS" ]; then - var_name="BUILD_DEV_CC" - else - var_name="BUILD_${u_group}_${u_tool}" - fi - - eval "has_override=\${$var_name+yes}" - eval "val=\${$var_name:-}" - if [ "$has_override" != "yes" ]; then - if [ "$catg" = "dev" ] && [ "$u_tool" = "AS" ]; then - eval "val=\$BUILD_DEV" - else - eval "val=\$BUILD_${u_group}" - fi - fi - - if [ "$val" = "1" ]; then - # map to the correct path - case "$catg/$t" in - posix/awk) path="cmd/posix/awk/awk" ;; - posix/sh) path="cmd/posix/sh/sh" ;; - posix/make) path="cmd/posix/make/make" ;; - extra/yap) path="cmd/extra/yap/yap" ;; - dev/ar) path="cmd/dev/ar/ar" ;; - dev/as) path="cmd/dev/as/as" ;; - dev/ld) path="cmd/dev/ld/ld" ;; - dev/cc) path="cmd/dev/cc/cc" ;; - *) path="cmd/$catg/$t" ;; - esac - enabled="$enabled $path" - fi - done - u_catg=$(echo "$catg" | tr '[:lower:]' '[:upper:]') - # write the list of enabled binaries - echo "${u_catg}_BIN_CONFIG =${enabled}" >> "${mk_file}" -done - -# write feature flags -echo "" >> "${mk_file}" -echo "# feature flags" >> "${mk_file}" -features=$(set | grep '^FEATURE_[A-Za-z0-9_]*=' | cut -d= -f1 | sort) -for f in $features; do - eval "val=\$$f" - echo "$f = $val" >> "${mk_file}" - echo "$f=\"$val\"" >> "${kv_file}" -done - -# write TLS variables to config files -echo "" >> "${mk_file}" -echo "CPPFLAGS_TLS = ${CPPFLAGS_TLS}" >> "${mk_file}" -echo "CPPFLAGS_TLS=\"${CPPFLAGS_TLS}\"" >> "${kv_file}" - -echo "LDFLAGS_TLS = ${LDFLAGS_TLS}" >> "${mk_file}" -echo "LDFLAGS_TLS=\"${LDFLAGS_TLS}\"" >> "${kv_file}" - -echo "LDLIBS_TLS = ${LDLIBS_TLS}" >> "${mk_file}" -echo "LDLIBS_TLS=\"${LDLIBS_TLS}\"" >> "${kv_file}" - -# write CPPFLAGS with all feature macros -echo "" >> "${mk_file}" -echo "CPPFLAGS =\\" >> "${mk_file}" -echo " -Ishared\\" >> "${mk_file}" -echo " -DPREFIX=\\\"\\\$(PREFIX)\\\"\\" >> "${mk_file}" -echo " -D_DEFAULT_SOURCE\\" >> "${mk_file}" -echo " -D_GNU_SOURCE\\" >> "${mk_file}" -echo " -D_NETBSD_SOURCE\\" >> "${mk_file}" -echo " -D_BSD_SOURCE\\" >> "${mk_file}" -echo " -D_XOPEN_SOURCE=700\\" >> "${mk_file}" -echo " -D_FILE_OFFSET_BITS=64\\" >> "${mk_file}" -for f in $features; do - if [ "$f" = "FEATURE_USE_SSL" ]; then - continue - fi - echo " -D$f=\$($f)\\" >> "${mk_file}" -done -echo " \$(CPPFLAGS_TLS)" >> "${mk_file}" - -# write toolchain configuration -echo "" >> "${mk_file}" -echo "CC = ${CC:-cc}" >> "${mk_file}" -echo "CFLAGS = ${CFLAGS:-}" >> "${mk_file}" -echo "LDFLAGS = ${LDFLAGS:-} \$(LDFLAGS_TLS)" >> "${mk_file}" -echo "LDLIBS = ${LDLIBS:-} \$(LDLIBS_TLS)" >> "${mk_file}" - -# write compilation rules for subdirectories that need special flags or nested structures -rules_file="scripts/mk/rules.mk" -echo "# autogenerated compilation rules" > "${rules_file}" - gen_dir_rules() { local dir="$1" flags="$2" extra_files="$3" f obj files uniq_files [ -d "$dir" ] || return 0 @@ -224,13 +205,186 @@ gen_dir_rules() { done } -echo "" >> "${rules_file}" -gen_dir_rules "cmd/posix/awk" "-Icmd/posix/awk" "cmd/posix/awk/awkgram.tab.c cmd/posix/awk/proctab.c" -gen_dir_rules "cmd/posix/sh" "-DSHELL -Icmd/posix/sh" "cmd/posix/sh/syntax.c cmd/posix/sh/nodes.c cmd/posix/sh/builtins.c" -gen_dir_rules "cmd/extra/yap" "" "" -gen_dir_rules "cmd/dev/ar" "-Icmd/dev/ar" "" -gen_dir_rules "cmd/dev/xcutil" "-Icmd/dev/xcutil" "" -gen_dir_rules "cmd/dev/ld" "-Icmd/dev/xcutil -Icmd/dev/ld" "" -gen_dir_rules "cmd/dev/as" "-Icmd/dev/xcutil -Icmd/dev/as" "" -gen_dir_rules "cmd/dev/as/arch/x64" "-Icmd/dev/xcutil -Icmd/dev/as -Icmd/dev/as/arch/x64" "" -gen_dir_rules "cmd/dev/cc" "-Icmd/dev/cc" "" +case "$target" in + getconf) gen_getconf ;; + bc) gen_bc ;; + awk) gen_awk ;; + sh) gen_sh ;; + clean) clean ;; + all) + gen_getconf + gen_bc + gen_awk + gen_sh + ;; + config) + mkdir -p scripts/mk + + mk_file="scripts/mk/config.mk" + kv_file="scripts/mk/config.kv" + + # clear existing files + : > "${mk_file}" + : > "${kv_file}" + + # write version prefix manprefix + echo "VERSION = ${VERSION}" >> "${mk_file}" + echo "VERSION=\"${VERSION}\"" >> "${kv_file}" + + echo "PREFIX = ${PREFIX}" >> "${mk_file}" + echo "PREFIX=\"${PREFIX}\"" >> "${kv_file}" + + # default manprefix to prefix/share/man if empty + man_pref="${MANPREFIX:-/usr/local/share/man}" + echo "MANPREFIX = ${man_pref}" >> "${mk_file}" + echo "MANPREFIX=\"${man_pref}\"" >> "${kv_file}" + + # toolchain defaults + echo "RANLIB = ${RANLIB:-ranlib}" >> "${mk_file}" + echo "RANLIB=\"${RANLIB:-ranlib}\"" >> "${kv_file}" + + echo "AR = ${AR:-ar}" >> "${mk_file}" + echo "AR=\"${AR:-ar}\"" >> "${kv_file}" + + echo "ARFLAGS = ${ARFLAGS:-rc}" >> "${mk_file}" + echo "ARFLAGS=\"${ARFLAGS:-rc}\"" >> "${kv_file}" + + # build toggles + echo "" >> "${mk_file}" + echo "# build toggles" >> "${mk_file}" + for g in BUILD_POSIX BUILD_XSI BUILD_NET BUILD_PSEUDO BUILD_EXTRA BUILD_LINUX BUILD_DEV; do + eval "val=\${$g:-0}" + echo "$g = $val" >> "${mk_file}" + echo "$g=\"$val\"" >> "${kv_file}" + done + + # write subtargets for each category dynamically + for catg in posix xsi net pseudo extra linux dev; do + echo "" >> "${mk_file}" + echo "# ${catg} subtargets" >> "${mk_file}" + tools=$(list_tools "$catg") + for t in $tools; do + write_subtarget "$catg" "$t" + done + + # build the enabled list using lowercase tools + enabled="" + for t in $tools; do + u_group=$(echo "$catg" | tr '[:lower:]' '[:upper:]') + u_tool=$(echo "$t" | tr 'a-z-' 'A-Z_') + + # special cases for dev tools mapping to different variables + if [ "$catg" = "dev" ] && [ "$u_tool" = "AS" ]; then + var_name="BUILD_DEV_CC" + else + var_name="BUILD_${u_group}_${u_tool}" + fi + + eval "has_override=\${$var_name+yes}" + eval "val=\${$var_name:-}" + if [ "$has_override" != "yes" ]; then + if [ "$catg" = "dev" ] && [ "$u_tool" = "AS" ]; then + eval "val=\$BUILD_DEV" + else + eval "val=\$BUILD_${u_group}" + fi + fi + + if [ "$val" = "1" ]; then + # map to the correct path + case "$catg/$t" in + posix/awk) path="cmd/posix/awk/awk" ;; + posix/sh) path="cmd/posix/sh/sh" ;; + posix/make) path="cmd/posix/make/make" ;; + extra/yap) path="cmd/extra/yap/yap" ;; + dev/ar) path="cmd/dev/ar/ar" ;; + dev/as) path="cmd/dev/as/as" ;; + dev/ld) path="cmd/dev/ld/ld" ;; + dev/cc) path="cmd/dev/cc/cc" ;; + *) path="cmd/$catg/$t" ;; + esac + enabled="$enabled $path" + fi + done + u_catg=$(echo "$catg" | tr '[:lower:]' '[:upper:]') + # write the list of enabled binaries + echo "${u_catg}_BIN_CONFIG =${enabled}" >> "${mk_file}" + done + + # write feature flags + echo "" >> "${mk_file}" + echo "# feature flags" >> "${mk_file}" + features=$(set | grep '^FEATURE_[A-Za-z0-9_]*=' | cut -d= -f1 | sort) + for f in $features; do + eval "val=\$$f" + echo "$f = $val" >> "${mk_file}" + echo "$f=\"$val\"" >> "${kv_file}" + done + + # write TLS variables to config files + echo "" >> "${mk_file}" + echo "CPPFLAGS_TLS = ${CPPFLAGS_TLS}" >> "${mk_file}" + echo "CPPFLAGS_TLS=\"${CPPFLAGS_TLS}\"" >> "${kv_file}" + + echo "LDFLAGS_TLS = ${LDFLAGS_TLS}" >> "${mk_file}" + echo "LDFLAGS_TLS=\"${LDFLAGS_TLS}\"" >> "${kv_file}" + + echo "LDLIBS_TLS = ${LDLIBS_TLS}" >> "${mk_file}" + echo "LDLIBS_TLS=\"${LDLIBS_TLS}\"" >> "${kv_file}" + + # write CPPFLAGS with all feature macros + echo "" >> "${mk_file}" + echo "CPPFLAGS =\\" >> "${mk_file}" + echo " -Ishared\\" >> "${mk_file}" + echo " -DPREFIX=\\\"\\\$(PREFIX)\\\"\\" >> "${mk_file}" + echo " -D_DEFAULT_SOURCE\\" >> "${mk_file}" + echo " -D_GNU_SOURCE\\" >> "${mk_file}" + echo " -D_NETBSD_SOURCE\\" >> "${mk_file}" + echo " -D_BSD_SOURCE\\" >> "${mk_file}" + echo " -D_XOPEN_SOURCE=700\\" >> "${mk_file}" + echo " -D_FILE_OFFSET_BITS=64\\" >> "${mk_file}" + for f in $features; do + if [ "$f" = "FEATURE_USE_SSL" ]; then + continue + fi + echo " -D$f=\$($f)\\" >> "${mk_file}" + done + echo " \$(CPPFLAGS_TLS)" >> "${mk_file}" + + # write toolchain configuration + echo "" >> "${mk_file}" + echo "CC = ${CC:-cc}" >> "${mk_file}" + echo "CFLAGS = ${CFLAGS:-}" >> "${mk_file}" + echo "LDFLAGS = ${LDFLAGS:-} \$(LDFLAGS_TLS)" >> "${mk_file}" + echo "LDLIBS = ${LDLIBS:-} \$(LDLIBS_TLS)" >> "${mk_file}" + + # write compilation rules for subdirectories that need special flags or nested structures + rules_file="scripts/mk/rules.mk" + echo "# autogenerated compilation rules" > "${rules_file}" + + echo "" >> "${rules_file}" + gen_dir_rules "cmd/posix/awk" "-Icmd/posix/awk" "cmd/posix/awk/awkgram.tab.c cmd/posix/awk/proctab.c" + gen_dir_rules "cmd/posix/sh" "-DSHELL -Icmd/posix/sh" "cmd/posix/sh/syntax.c cmd/posix/sh/nodes.c cmd/posix/sh/builtins.c" + gen_dir_rules "cmd/extra/yap" "" "" + gen_dir_rules "cmd/dev/ar" "-Icmd/dev/ar" "" + gen_dir_rules "cmd/dev/xcutil" "-Icmd/dev/xcutil" "" + gen_dir_rules "cmd/dev/ld" "-Icmd/dev/xcutil -Icmd/dev/ld" "" + gen_dir_rules "cmd/dev/as" "-Icmd/dev/xcutil -Icmd/dev/as" "" + gen_dir_rules "cmd/dev/as/arch/x64" "-Icmd/dev/xcutil -Icmd/dev/as -Icmd/dev/as/arch/x64" "" + gen_dir_rules "cmd/dev/cc" "-Icmd/dev/cc" "" + + # generate nofork_list.h + nofork_h="shared/libutil/nofork_list.h" + echo "/* autogenerated by genconfig.sh */" > "${nofork_h}" + echo "static const char *nofork_list[] = {" >> "${nofork_h}" + for app in ${NOFORK_LIST}; do + echo " \"${app}\"," >> "${nofork_h}" + done + echo " NULL" >> "${nofork_h}" + echo "};" >> "${nofork_h}" + ;; + *) + printf "unknown target: %s\n" "$target" >&2 + exit 1 + ;; +esac diff --git a/shared/config.h b/shared/config.h @@ -118,9 +118,15 @@ #ifndef FEATURE_PATCH_STRIP_CR #define FEATURE_PATCH_STRIP_CR 1 #endif -#ifndef FEATURE_INPROC_EXEC -#define FEATURE_INPROC_EXEC 0 +#ifndef FEATURE_NOEXEC +#define FEATURE_NOEXEC 0 #endif -#ifndef FEATURE_INPROC_EXEC_OVERRIDE -#define FEATURE_INPROC_EXEC_OVERRIDE 0 +#ifndef FEATURE_NOEXEC_OVERRIDE +#define FEATURE_NOEXEC_OVERRIDE 0 +#endif +#ifndef FEATURE_NOFORK +#define FEATURE_NOFORK 0 +#endif +#ifndef FEATURE_NOFORK_OVERRIDE +#define FEATURE_NOFORK_OVERRIDE 0 #endif diff --git a/shared/libredline/redline.c b/shared/libredline/redline.c @@ -38,6 +38,8 @@ #define CTRL_U 21 #define CTRL_W 23 #define CTRL_Y 25 +#define CTRL_Z 26 +#define CTRL_QUIT 28 #define BACKSPACE 127 #define ESC 27 @@ -73,6 +75,8 @@ static char *kill_buffer = NULL; static volatile sig_atomic_t winch_received = 0; static struct sigaction orig_sigwinch; +static void redlineEditStop(struct redlineState *l); + static void (*completionCallback)(const char *, struct redlineCompletions *) = NULL; /* return the number of bytes that compose the utf-8 character starting at c */ @@ -330,7 +334,7 @@ enableRawMode(int fd) return -1; raw = orig_termios; - raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); + raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP); raw.c_oflag &= ~(OPOST); raw.c_cflag |= (CS8); raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); @@ -1189,13 +1193,33 @@ redlineEditFeed(struct redlineState *l) } switch (c) { + case 10: case ENTER: if (mlmode) redlineEditMoveEnd(l); return strdup(l->buf); case CTRL_C: + if (write(l->ofd, "^C", 2) == -1) {} errno = EAGAIN; return NULL; + case CTRL_Z: + break; + case CTRL_QUIT: + if (write(l->ofd, "^\\", 2) == -1) {} + redlineEditStop(l); + fflush(stdout); + { + struct sigaction sa, osa; + sa.sa_handler = SIG_DFL; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sigaction(SIGQUIT, &sa, &osa); + kill(getpid(), SIGQUIT); + sigaction(SIGQUIT, &osa, NULL); + } + enableRawMode(l->ifd); + refreshLine(l); + break; case BACKSPACE: case 8: redlineEditBackspace(l); @@ -1321,6 +1345,8 @@ redlineEditFeed(struct redlineState *l) } break; default: + if (c < 32) + break; utf8len = utf8ByteLen(c); utf8[0] = c; if (utf8len > 1) { @@ -1441,6 +1467,9 @@ redline(const char *prompt) free(history[history_len]); } free(l.buf); + if (res == NULL && errno == EAGAIN) { + kill(getpid(), SIGINT); + } return res; } diff --git a/shared/libutil/wexec.c b/shared/libutil/wexec.c @@ -5,6 +5,7 @@ #include "../util.h" #include <errno.h> +#include <setjmp.h> #include <signal.h> #include <stdlib.h> #include <string.h> @@ -12,7 +13,7 @@ #include <sys/wait.h> #include <unistd.h> -#define MAX_BUILTINS 128 +#define MAX_BUILTINS 256 #define MAX_POPEN 32 struct builtin { @@ -31,6 +32,22 @@ struct popen_entry { static struct popen_entry popen_tab[MAX_POPEN]; static int npopen; +static char *(*wexec_env_lookup)(const char *name) = NULL; + +void +wexec_register_env_lookup(char *(*fn)(const char *name)) +{ + wexec_env_lookup = fn; +} + +static const char * +wexec_getenv(const char *name) +{ + if (wexec_env_lookup) + return wexec_env_lookup(name); + return getenv(name); +} + int wpopen_track(FILE *fp, pid_t pid) { @@ -85,49 +102,77 @@ wexec_is_builtin(const char *name) return 0; } -#if FEATURE_INPROC_EXEC -static int inproc_enabled = 1; +int +wexec_is_nofork(const char *name) +{ +#include "nofork_list.h" + int i; + for (i = 0; nofork_list[i]; i++) { + if (strcmp(nofork_list[i], name) == 0) + return 1; + } + return 0; +} -#if FEATURE_INPROC_EXEC_OVERRIDE -static int inproc_env_read = 0; +#if FEATURE_NOEXEC +static int noexec_enabled = 1; -/* ARUU_INPROC_EXEC=0 forces fork+exec at runtime, even though the box was - * built with FEATURE_INPROC_EXEC=1. lets a person reach an external tool - * that shares a name with a builtin without giving its full path. only - * compiled in when the box is also built with FEATURE_INPROC_EXEC_OVERRIDE=1; - * without that flag this env var is never read and inproc dispatch cannot be - * bypassed except through an explicit wexec_set_inproc() call. read once - * and cached, an explicit wexec_set_inproc() call overrides it */ +#if FEATURE_NOEXEC_OVERRIDE static void -inproc_read_env(void) +noexec_read_env(void) { - const char *e; - - if (inproc_env_read) - return; - inproc_env_read = 1; - e = getenv("ARUU_INPROC_EXEC"); + const char *e = wexec_getenv("ARUU_NOEXEC"); if (e && strcmp(e, "0") == 0) - inproc_enabled = 0; + noexec_enabled = 0; + else + noexec_enabled = 1; } #endif void -wexec_set_inproc(int enabled) +wexec_set_noexec(int enabled) +{ + noexec_enabled = enabled; +} + +int +wexec_get_noexec(void) +{ +#if FEATURE_NOEXEC_OVERRIDE + noexec_read_env(); +#endif + return noexec_enabled; +} +#endif + +#if FEATURE_NOFORK +static int nofork_enabled = 1; + +#if FEATURE_NOFORK_OVERRIDE +static void +nofork_read_env(void) { -#if FEATURE_INPROC_EXEC_OVERRIDE - inproc_env_read = 1; + const char *e = wexec_getenv("ARUU_NOFORK"); + if (e && strcmp(e, "0") == 0) + nofork_enabled = 0; + else + nofork_enabled = 1; +} #endif - inproc_enabled = enabled; + +void +wexec_set_nofork(int enabled) +{ + nofork_enabled = enabled; } int -wexec_get_inproc(void) +wexec_get_nofork(void) { -#if FEATURE_INPROC_EXEC_OVERRIDE - inproc_read_env(); +#if FEATURE_NOFORK_OVERRIDE + nofork_read_env(); #endif - return inproc_enabled; + return nofork_enabled; } #endif @@ -137,11 +182,11 @@ wwhich(const char *name) const char *path, *p, *end; char *buf; size_t dlen, nlen; - struct stat st; + struct stat st; -#if FEATURE_INPROC_EXEC - /* when inproc dispatch is active, registered builtins have no filesystem path */ - if (inproc_enabled && wexec_is_builtin(name)) +#if FEATURE_NOEXEC + /* when builtin dispatch is active, registered builtins have no filesystem path */ + if (wexec_get_noexec() && wexec_is_builtin(name)) return NULL; #endif @@ -152,7 +197,7 @@ wwhich(const char *name) return NULL; } - path = getenv("PATH"); + path = wexec_getenv("PATH"); if (path == NULL) path = "/usr/local/bin:/usr/bin:/bin"; @@ -163,9 +208,9 @@ wwhich(const char *name) dlen = end ? (size_t)(end - p) : strlen(p); /* skip empty components (current dir) for safety */ if (dlen > 0) { - buf = emalloc(dlen + 1 + nlen + 1); + buf = emalloc(dlen + 1 + nlen + 1); memcpy(buf, p, dlen); - buf[dlen] = '/'; + buf[dlen] = '/'; memcpy(buf + dlen + 1, name, nlen + 1); if (stat(buf, &st) == 0 && S_ISREG(st.st_mode) && (st.st_mode & 0111)) return buf; @@ -178,13 +223,50 @@ wwhich(const char *name) return NULL; } -#if FEATURE_INPROC_EXEC -static int -call_builtin(const char *name, char *const *argv) +#if FEATURE_NOEXEC || FEATURE_NOFORK +static sigjmp_buf inproc_jmp; +static volatile sig_atomic_t inproc_active = 0; +static volatile int inproc_exit_status = 0; + +static void +inproc_sig_handler(int signo) +{ + if (inproc_active) { + siglongjmp(inproc_jmp, signo); + } +} + +int +wexec_call_builtin(const char *name, char *const *argv) { - int i, argc; - char **p; + struct sigaction sa, osa_int, osa_quit, osa_pipe; + int i, argc, ret, sig; + char **p; + + sig = sigsetjmp(inproc_jmp, 1); + if (sig != 0) { + inproc_active = 0; + sigaction(SIGINT, &osa_int, NULL); + sigaction(SIGQUIT, &osa_quit, NULL); + sigaction(SIGPIPE, &osa_pipe, NULL); + if (sig == 1) + return inproc_exit_status; + kill(getpid(), sig); + return 128 + sig; + } + + sa.sa_handler = inproc_sig_handler; + sigemptyset(&sa.sa_mask); + sa.sa_flags = 0; + sigaction(SIGINT, &sa, &osa_int); + sigaction(SIGQUIT, &sa, &osa_quit); + sa.sa_handler = SIG_IGN; + sigaction(SIGPIPE, &sa, &osa_pipe); + + inproc_active = 1; + + ret = -1; argc = 0; for (p = (char **)argv; *p; p++) argc++; @@ -192,10 +274,20 @@ call_builtin(const char *name, char *const *argv) if (strcmp(builtins[i].name, name) == 0) { fflush(stdout); fflush(stderr); - return builtins[i].fn(argc, (char **)argv); + ret = builtins[i].fn(argc, (char **)argv); + fflush(stdout); + fflush(stderr); + break; } } - return -1; + + inproc_active = 0; + + sigaction(SIGINT, &osa_int, NULL); + sigaction(SIGQUIT, &osa_quit, NULL); + sigaction(SIGPIPE, &osa_pipe, NULL); + + return ret; } #endif @@ -211,6 +303,26 @@ fork_exec(const char *path, char *const *argv, int use_path) return -1; } if (pid == 0) { + const char *base = strrchr(path, '/'); + base = base ? base + 1 : path; +#if FEATURE_NOEXEC + if (wexec_get_noexec() && wexec_is_builtin(base)) { + struct sigaction sa; + int i, ret; + for (i = 1; i < NSIG; i++) { + if (sigaction(i, NULL, &sa) == 0) { + if (sa.sa_handler != SIG_IGN && sa.sa_handler != SIG_DFL) { + sa.sa_handler = SIG_DFL; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + sigaction(i, &sa, NULL); + } + } + } + ret = wexec_call_builtin(base, argv); + _exit(ret); + } +#endif if (use_path) execvp(path, argv); else @@ -231,11 +343,11 @@ fork_exec(const char *path, char *const *argv, int use_path) int wexecvp(const char *name, char *const *argv) { -#if FEATURE_INPROC_EXEC - if (inproc_enabled && wexec_is_builtin(name)) { +#if FEATURE_NOFORK + if (wexec_get_nofork() && wexec_is_builtin(name) && wexec_is_nofork(name)) { fflush(stdout); fflush(stderr); - return call_builtin(name, argv); + return wexec_call_builtin(name, argv); } #endif return fork_exec(name, argv, 1); @@ -244,15 +356,13 @@ wexecvp(const char *name, char *const *argv) int wexecv(const char *path, char *const *argv) { -#if FEATURE_INPROC_EXEC - { - const char *base = strrchr(path, '/'); - base = base ? base + 1 : path; - if (inproc_enabled && wexec_is_builtin(base)) { - fflush(stdout); - fflush(stderr); - return call_builtin(base, argv); - } + const char *base = strrchr(path, '/'); + base = base ? base + 1 : path; +#if FEATURE_NOFORK + if (wexec_get_nofork() && wexec_is_builtin(base) && wexec_is_nofork(base)) { + fflush(stdout); + fflush(stderr); + return wexec_call_builtin(base, argv); } #endif return fork_exec(path, argv, 0); @@ -265,6 +375,10 @@ wpopen(const char *name, char *const *argv, const char *mode) pid_t pid; int parent_end, child_end; char *sh_argv[4]; +#if FEATURE_NOEXEC + struct sigaction sa; + int i; +#endif if (pipe(pfd) < 0) { weprintf("pipe:"); @@ -294,14 +408,27 @@ wpopen(const char *name, char *const *argv, const char *mode) close(child_end); } if (argv) { -#if FEATURE_INPROC_EXEC - if (inproc_enabled && wexec_is_builtin(name)) { +#if FEATURE_NOEXEC + if (wexec_get_noexec() && wexec_is_builtin(name)) { int argc; char **p; + + /* reset caught signal handlers to default for inproc command */ + for (i = 1; i < NSIG; i++) { + if (sigaction(i, NULL, &sa) == 0) { + if (sa.sa_handler != SIG_IGN && sa.sa_handler != SIG_DFL) { + sa.sa_handler = SIG_DFL; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + sigaction(i, &sa, NULL); + } + } + } + argc = 0; for (p = (char **)argv; *p; p++) argc++; - call_builtin(name, argv); + wexec_call_builtin(name, argv); fflush(stdout); fflush(stderr); _exit(0); @@ -314,9 +441,21 @@ wpopen(const char *name, char *const *argv, const char *mode) sh_argv[1] = "-c"; sh_argv[2] = (char *)name; sh_argv[3] = NULL; -#if FEATURE_INPROC_EXEC - if (inproc_enabled && wexec_is_builtin("sh")) { - call_builtin("sh", sh_argv); +#if FEATURE_NOEXEC + if (wexec_get_noexec() && wexec_is_builtin("sh")) { + /* reset caught signal handlers to default for inproc command */ + for (i = 1; i < NSIG; i++) { + if (sigaction(i, NULL, &sa) == 0) { + if (sa.sa_handler != SIG_IGN && sa.sa_handler != SIG_DFL) { + sa.sa_handler = SIG_DFL; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + sigaction(i, &sa, NULL); + } + } + } + + wexec_call_builtin("sh", sh_argv); fflush(stdout); fflush(stderr); _exit(0); @@ -372,14 +511,50 @@ wpclose(FILE *fp) int wsystem(const char *cmd) { -#if FEATURE_INPROC_EXEC - if (inproc_enabled && wexec_is_builtin("sh")) { - char *sh_argv[4]; - sh_argv[0] = "sh"; - sh_argv[1] = "-c"; - sh_argv[2] = (char *)cmd; - sh_argv[3] = NULL; - return wexecvp("sh", sh_argv); +#if FEATURE_NOEXEC + if (wexec_get_noexec() && wexec_is_builtin("sh")) { + pid_t pid; + int st; + + pid = fork(); + if (pid < 0) { + weprintf("fork:"); + return -1; + } + if (pid == 0) { + char *sh_argv[4]; + struct sigaction sa; + int i; + int ret; + + /* reset caught signal handlers to default for inproc command */ + for (i = 1; i < NSIG; i++) { + if (sigaction(i, NULL, &sa) == 0) { + if (sa.sa_handler != SIG_IGN && sa.sa_handler != SIG_DFL) { + sa.sa_handler = SIG_DFL; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + sigaction(i, &sa, NULL); + } + } + } + + sh_argv[0] = "sh"; + sh_argv[1] = "-c"; + sh_argv[2] = (char *)cmd; + sh_argv[3] = NULL; + ret = wexec_call_builtin("sh", sh_argv); + _exit(ret); + } + if (waitpid(pid, &st, 0) < 0) { + weprintf("waitpid:"); + return -1; + } + if (WIFEXITED(st)) + return WEXITSTATUS(st); + if (WIFSIGNALED(st)) + return 128 + WTERMSIG(st); + return -1; } #endif { @@ -397,12 +572,27 @@ wsystem(const char *cmd) void wexecvp_self(const char *name, char *const *argv) { -#if FEATURE_INPROC_EXEC - if (inproc_enabled && wexec_is_builtin(name)) { - int ret; +#if FEATURE_NOEXEC + if (wexec_get_noexec() && wexec_is_builtin(name)) { + struct sigaction sa; + int i; + int ret; + + /* reset caught signal handlers to default for inproc command */ + for (i = 1; i < NSIG; i++) { + if (sigaction(i, NULL, &sa) == 0) { + if (sa.sa_handler != SIG_IGN && sa.sa_handler != SIG_DFL) { + sa.sa_handler = SIG_DFL; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + sigaction(i, &sa, NULL); + } + } + } + fflush(stdout); fflush(stderr); - ret = call_builtin(name, argv); + ret = wexec_call_builtin(name, argv); fflush(stdout); fflush(stderr); exit(ret); @@ -416,15 +606,30 @@ wexecvp_self(const char *name, char *const *argv) void wexecv_self(const char *path, char *const *argv) { -#if FEATURE_INPROC_EXEC +#if FEATURE_NOEXEC { const char *base = strrchr(path, '/'); base = base ? base + 1 : path; - if (inproc_enabled && wexec_is_builtin(base)) { - int ret; + if (wexec_get_noexec() && wexec_is_builtin(base)) { + struct sigaction sa; + int i; + int ret; + + /* reset caught signal handlers to default for inproc command */ + for (i = 1; i < NSIG; i++) { + if (sigaction(i, NULL, &sa) == 0) { + if (sa.sa_handler != SIG_IGN && sa.sa_handler != SIG_DFL) { + sa.sa_handler = SIG_DFL; + sa.sa_flags = 0; + sigemptyset(&sa.sa_mask); + sigaction(i, &sa, NULL); + } + } + } + fflush(stdout); fflush(stderr); - ret = call_builtin(base, argv); + ret = wexec_call_builtin(base, argv); fflush(stdout); fflush(stderr); exit(ret); @@ -435,3 +640,17 @@ wexecv_self(const char *path, char *const *argv) weprintf("execv %s:", path); exit((errno == ENOENT) ? 127 : 126); } + +void(exit)(int); + +void +wexec_exit(int code) +{ +#if FEATURE_NOEXEC || FEATURE_NOFORK + if (inproc_active) { + inproc_exit_status = code; + siglongjmp(inproc_jmp, 1); + } +#endif + (exit)(code); +} diff --git a/shared/util.h b/shared/util.h @@ -170,3 +170,9 @@ int net_del_route(const char *, const char *, const char *, const char *, int); int net_flush_addrs(const char *); int net_set_txqueuelen(const char *, int); int net_set_name(const char *, const char *); + +#if FEATURE_NOEXEC || FEATURE_NOFORK +#include "wexec.h" +#define exit(code) wexec_exit(code) +#define _exit(code) wexec_exit(code) +#endif diff --git a/shared/wexec.h b/shared/wexec.h @@ -7,24 +7,33 @@ typedef int (*wexec_fn)(int argc, char **argv); -int wexec_register(const char *name, wexec_fn fn); -int wexec_is_builtin(const char *name); +int wexec_register(const char *name, wexec_fn fn); +int wexec_is_builtin(const char *name); +void wexec_register_env_lookup(char *(*fn)(const char *name)); +#if FEATURE_NOEXEC || FEATURE_NOFORK +int wexec_call_builtin(const char *name, char *const *argv); +void wexec_exit(int code) __attribute__((noreturn)); +#endif -/* search PATH for name; returns emalloc'd absolute path or NULL if not found. - * when FEATURE_INPROC_EXEC is compiled in and inproc dispatch is enabled, - * returns NULL for registered builtins (they have no filesystem path). */ +/* search path for name, returns emallocd absolute path or null if not found + * when feature_noexec is compiled in and builtin dispatch is enabled, + * returns null for registered builtins (they have no filesystem path) */ char *wwhich(const char *name); -#if FEATURE_INPROC_EXEC -/* runtime toggle: disable to force fork+exec even when builtins are registered. - * when the box is also built with FEATURE_INPROC_EXEC_OVERRIDE=1, - * wexec_get_inproc() additionally honors ARUU_INPROC_EXEC=0 from the - * environment, read once on first use, so a builtin can be bypassed without - * a rebuild. without that second flag, inproc dispatch is hardwired on for - * the whole process and only an explicit wexec_set_inproc() call can - * change it. */ -void wexec_set_inproc(int enabled); -int wexec_get_inproc(void); +int wexec_is_nofork(const char *name); + +#if FEATURE_NOEXEC +void wexec_set_noexec(int enabled); +int wexec_get_noexec(void); +#else +#define wexec_get_noexec() 0 +#endif + +#if FEATURE_NOFORK +void wexec_set_nofork(int enabled); +int wexec_get_nofork(void); +#else +#define wexec_get_nofork() 0 #endif int wexecvp(const char *name, char *const *argv);