fun bug fix

This commit is contained in:
2026-10-13 13:05:44 +02:00
parent df5dca96c6
commit 9723a6ad93
3 changed files with 155 additions and 44 deletions
+3 -3
View File
@@ -91,7 +91,7 @@ A bar-style prompt showing current time, execution time of the last command
| Function | Module | Description |
| --- | --- | --- |
| `busy` | fun | Monitor /dev/urandom for a hex pattern — look busy |
| `busy` | fun | Monitor /dev/urandom for a hex pattern — look busy; the pattern can also be given as argument; runs until a key (or Ctrl-C) is pressed, needs a terminal |
| `check_updates` | updates | Check whether a newer profile version is available online; when called with `-q` at startup a 3-second network timeout is applied so a slow or absent network never delays the prompt |
| `clean` | filefct | Erase backup files in given directories, optionally recursive |
| `conf_dump` | conf | Display the profile configuration file; `-s/--section NAME` restricts output to one section; `-a/--all` also lists commented-out keys (documented defaults, skipped when the key is set); an optional pattern argument filters keys by substring match; honours `NO_COLOR` |
@@ -275,8 +275,8 @@ change the default without having to pass flags every time.
| Key | Default | Description |
| --- | --- | --- |
| `BUSY_DEFAULT_PATTERN` | `[0-9a-f]` | Hex pattern matched by `busy` |
| `BUSY_DEFAULT_DELAY` | `0.1` | Polling delay (seconds) for `busy` |
| `BUSY_DEFAULT_PATTERN` | `ca fe` | String searched (fixed string, case-insensitive) in the hexdump lines printed by `busy` |
| `BUSY_DEFAULT_DELAY` | `0` | Delay between printed lines for `busy`, in milliseconds (integer, `0` = none) |
| `FAKE_COMPILE_DEFAULT_MIN_DELAY` | `40` | Min delay between output lines for `fake_compile` (ms) |
| `FAKE_COMPILE_DEFAULT_MAX_DELAY` | `150` | Max delay between output lines for `fake_compile` (ms) |
| `FAKE_COMPILE_DEFAULT_LANG` | `c` | Default language preset for `fake_compile` (`c`, `cpp`, `java`, `python`, `random`) |
+8
View File
@@ -63,6 +63,14 @@ Versions follow `MAJOR.MINOR.PATCH-REVISION_STAGE_N` (e.g. `3.99.1-4_rc_1`).
`findbig` accepted an invalid configured limit; names starting with `-` are
handled; `finddead` and `findzero` no longer return 1 when the last entry
was skipped.
- `fun.sh`: the configured delays and language of `busy`, `fake_compile` and
`hack` are now validated (a non-numeric value was evaluated as an
arithmetic expression, and an invalid language looped without sleeping);
Ctrl-C no longer leaves the background job running, nor does a shell without
job control; a missing terminal is reported; the positional pattern of
`busy` documented in its usage now works; `grep`/`read` no longer mangle
patterns starting with `-` or lines with leading blanks; `awk` is no longer
forked for each pause.
- `compress.sh`: option flags and loop variables no longer leak into the
interactive shell.
+144 -41
View File
@@ -37,11 +37,84 @@
load_conf "fun"
# ------------------------------------------------------------------------------
# Make non-IT peoples think you're busy doing something hard
# Shared helpers of the demo functions below (busy, fake_compile, hack).
# They are exported because the public functions are.
# True when $1 is a non-negative integer. Every value that ends up in an
# arithmetic context must pass this first (this includes values coming from the
# configuration file): bash evaluates non-numeric strings inside [[ -gt ]] or
# $(( )) as expressions, which can run arbitrary commands.
# Usage: _fun_is_uint <value>
_fun_is_uint()
{
[[ "$1" =~ ^[0-9]+$ ]]
}
export -f _fun_is_uint
# Convert a number of milliseconds into a seconds string usable by sleep
# (1500 -> 1.500), in pure bash (no awk fork per call).
# Usage: _fun_ms_to_s <ms>
_fun_ms_to_s()
{
printf '%d.%03d' $(( $1 / 1000 )) $(( $1 % 1000 ))
}
export -f _fun_ms_to_s
# Fail with a message unless a terminal is available: the demos wait for a key
# read from /dev/tty to stop.
# Usage: _fun_need_tty <command-name>
_fun_need_tty()
{
if ! { : </dev/tty; } 2>/dev/null; then
disp E "$1: a terminal is required (it runs until a key is pressed)."
return 1
fi
}
export -f _fun_need_tty
# Wait for a key press, then stop the background job $1 and its children.
# The job runs as "( ... ) &"; with job control (interactive shell) it is the
# leader of its own process group, which is signalled as a whole. Without job
# control that group does not exist, so its direct children (the pipeline) are
# killed first, then the subshell. SIGINT is trapped meanwhile so that Ctrl-C
# stops the job like a key press instead of aborting the caller and leaving it
# running. The wait is a polling loop of short timed reads: a blocking read is
# restarted by bash after a trapped signal and would never notice the Ctrl-C.
# Usage: _fun_stop_on_key <pid>
_fun_stop_on_key()
{
local pid=$1 old_int interrupted=0
old_int=$(trap -p INT)
trap 'interrupted=1' INT
while (( ! interrupted )); do
IFS= read -r -n 1 -s -t 0.3 _ </dev/tty && break
# Status above 128 is just the timeout; anything else (such as end of
# input on the terminal) ends the wait.
(( $? > 128 )) || break
done
kill -- -"$pid" 2>/dev/null || {
pkill -P "$pid" 2>/dev/null
kill "$pid" 2>/dev/null
}
wait "$pid" 2>/dev/null
if [[ -n "$old_int" ]]; then
eval "$old_int"
else
trap - INT
fi
return 0
}
export -f _fun_stop_on_key
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Make non-IT peoples think you're busy doing something hard: print the lines of
# a hexdump of random data that contain a pattern, until a key is pressed.
# Usage: busy [options] [pattern]
# Options:
# --delay=<ms> : add a delay between each line output (milliseconds)
# pattern : the string to search for in the hexdump output (default is "ca fe")
# -p, --pattern <hex> : the string to search for in the hexdump output
# (default is "ca fe"); may also be given as argument
# -d, --delay <ms> : add a delay between each line output (milliseconds)
busy()
{
local pattern="${BUSY_DEFAULT_PATTERN:-ca fe}" delay_ms="${BUSY_DEFAULT_DELAY:-0}"
@@ -55,9 +128,8 @@ busy()
disp E "Invalid options, use \"busy --help\" to display usage."
return 1
fi
local -a _pargs=()
readarray -td '' _pargs < <(printf '%s' "$PARSED" | xargs printf '%s\0')
set -- "${_pargs[@]}"
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
while true; do
case "$1" in
@@ -67,7 +139,8 @@ busy()
printf "Options:\n"
printf "\t-h, --help\t\tDisplay this help screen\n"
printf "\t-p, --pattern PATTERN\tHex pattern to search (default: \"ca fe\")\n"
printf "\t-d, --delay MS\t\tDelay between matches in milliseconds\n"
printf "\t-d, --delay MS\t\tDelay between matches in milliseconds\n\n"
printf "It runs until a key is pressed.\n"
return 0
;;
-p|--pattern)
@@ -76,10 +149,6 @@ busy()
;;
-d|--delay)
delay_ms="$2"
if ! [[ "$delay_ms" =~ ^[0-9]+$ ]]; then
disp E "Invalid delay: must be an integer (milliseconds)."
return 1
fi
shift 2
;;
--)
@@ -93,29 +162,34 @@ busy()
esac
done
# Convert milliseconds to seconds for 'sleep'
# A pattern given as argument (words joined by spaces) overrides -p
[[ $# -gt 0 ]] && pattern="$*"
# Checked here so that the configured default is validated as well as -d
if ! _fun_is_uint "$delay_ms"; then
disp E "Invalid delay '${delay_ms}': must be an integer (milliseconds)."
return 1
fi
local delay_s
delay_s=$(awk "BEGIN{
printf \"%.3f\", $delay_ms / 1000 }")
delay_s=$(_fun_ms_to_s "$delay_ms")
command -v hexdump >/dev/null 2>&1 || {
disp E "busy: 'hexdump' is required but not installed (util-linux or bsdmainutils)."
return 1
}
_fun_need_tty busy || return 1
# Monitor /dev/urandom
# Monitor /dev/urandom. grep -e protects a pattern starting with "-";
# IFS= read keeps the line's leading blanks.
(
hexdump -C < /dev/urandom | grep -iF --line-buffered "$pattern" | \
while read -r line; do
echo "$line"
hexdump -C < /dev/urandom | grep -iF --line-buffered -e "$pattern" | \
while IFS= read -r line; do
printf '%s\n' "$line"
[[ $delay_ms -gt 0 ]] && sleep "$delay_s"
done
) & local sub_pid=$!
IFS= read -r -n 1 -s _ </dev/tty
kill -- -"$sub_pid" 2>/dev/null || kill "$sub_pid" 2>/dev/null
wait "$sub_pid" 2>/dev/null
return 0
_fun_stop_on_key "$sub_pid"
}
export -f busy
# ------------------------------------------------------------------------------
@@ -144,9 +218,8 @@ fake_compile()
disp E "Invalid options, use \"fake_compile --help\" to display usage."
return 1
fi
local -a _pargs=()
readarray -td '' _pargs < <(printf '%s' "$PARSED" | xargs printf '%s\0')
set -- "${_pargs[@]}"
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
while true; do
case "$1" in
@@ -158,7 +231,8 @@ fake_compile()
printf "\t-n, --min-delay MS\t\tMin delay between lines in milliseconds (default: 40)\n"
printf "\t-x, --max-delay MS\t\tMax delay between lines in milliseconds (default: 150)\n"
printf "\t-l, --lang LANG\t\t\tLanguage preset: c, cpp, java, python, random (default: c)\n"
printf "\t-e, --errors\t\t\tInject fake compilation errors at the end\n"
printf "\t-e, --errors\t\t\tInject fake compilation errors at the end\n\n"
printf "It runs until a key is pressed.\n"
return 0
;;
-n|--min-delay)
@@ -203,15 +277,35 @@ fake_compile()
esac
done
# Validated here (not only in the options) so that the values coming from
# the configuration file are checked too.
if ! _fun_is_uint "$min_ms" || ! _fun_is_uint "$max_ms"; then
disp E "min-delay and max-delay must be integers (milliseconds)."
return 1
fi
case "$lang" in
c|cpp|java|python|random) ;;
*)
disp E "Invalid lang '${lang}': must be one of: c, cpp, java, python, random."
return 1
;;
esac
if [[ $# -gt 0 ]]; then
disp E "Unexpected argument: $1. Use \"fake_compile --help\" to display usage."
return 1
fi
if [[ $min_ms -gt $max_ms ]]; then
disp E "min-delay ($min_ms) must be <= max-delay ($max_ms)."
return 1
fi
_fun_need_tty fake_compile || return 1
(
# Sleep for a random duration between min_ms and max_ms (RANDOM only
# spans 0..32767, which bounds the usable range).
rand_sleep() {
local r=$(( min_ms + RANDOM % (max_ms - min_ms + 1) ))
sleep "$(awk "BEGIN{ printf \"%.3f\", $r / 1000 }")"
sleep "$(_fun_ms_to_s "$r")"
}
c_files=( "main" "utils" "parser" "lexer" "codegen" "optimizer"
@@ -298,10 +392,7 @@ fake_compile()
done
) & local sub_pid=$!
IFS= read -r -n 1 -s _ </dev/tty
kill -- -"$sub_pid" 2>/dev/null || kill "$sub_pid" 2>/dev/null
wait "$sub_pid" 2>/dev/null
return 0
_fun_stop_on_key "$sub_pid"
}
export -f fake_compile
# ------------------------------------------------------------------------------
@@ -329,9 +420,8 @@ hack()
disp E "Invalid options, use \"hack --help\" to display usage."
return 1
fi
local -a _pargs=()
readarray -td '' _pargs < <(printf '%s' "$PARSED" | xargs printf '%s\0')
set -- "${_pargs[@]}"
# Restore the getopt-normalised (shell-quoted) argument list
eval set -- "$PARSED"
while true; do
case "$1" in
@@ -342,7 +432,8 @@ hack()
printf "\t-h, --help\t\t\tDisplay this help screen\n"
printf "\t-t, --target IP\t\t\tTarget IP address (default: random)\n"
printf "\t-n, --min-delay MS\t\tMin delay between output lines in milliseconds (default: 60)\n"
printf "\t-x, --max-delay MS\t\tMax delay between output lines in milliseconds (default: 250)\n"
printf "\t-x, --max-delay MS\t\tMax delay between output lines in milliseconds (default: 250)\n\n"
printf "It runs until a key is pressed.\n"
return 0
;;
-t|--target)
@@ -376,15 +467,26 @@ hack()
esac
done
if ! _fun_is_uint "$min_ms" || ! _fun_is_uint "$max_ms"; then
disp E "min-delay and max-delay must be integers (milliseconds)."
return 1
fi
if [[ $# -gt 0 ]]; then
disp E "Unexpected argument: $1. Use \"hack --help\" to display usage."
return 1
fi
if [[ $min_ms -gt $max_ms ]]; then
disp E "min-delay ($min_ms) must be <= max-delay ($max_ms)."
return 1
fi
_fun_need_tty hack || return 1
(
# Sleep for a random duration between min_ms and max_ms (RANDOM only
# spans 0..32767, which bounds the usable range).
rand_sleep() {
local r=$(( min_ms + RANDOM % (max_ms - min_ms + 1) ))
sleep "$(awk "BEGIN{ printf \"%.3f\", $r / 1000 }")"
sleep "$(_fun_ms_to_s "$r")"
}
rand_ip() { printf '%d.%d.%d.%d\n' \
$(( RANDOM%223+1 )) $(( RANDOM%255 )) \
@@ -405,7 +507,11 @@ hack()
fixed_target="$target"
while true; do
[[ -z "$fixed_target" ]] && target="$(rand_ip)" || target="$fixed_target"
if [[ -z "$fixed_target" ]]; then
target="$(rand_ip)"
else
target="$fixed_target"
fi
printf "[*] Initializing attack sequence against %s\n" "$target"
rand_sleep
@@ -484,10 +590,7 @@ hack()
done
) & local sub_pid=$!
IFS= read -r -n 1 -s _ </dev/tty
kill -- -"$sub_pid" 2>/dev/null || kill "$sub_pid" 2>/dev/null
wait "$sub_pid" 2>/dev/null
return 0
_fun_stop_on_key "$sub_pid"
}
export -f hack
# ------------------------------------------------------------------------------