add timeout 120 when executing external binary
This commit is contained in:
parent
c0b171a5c1
commit
85684b39ad
@ -2,7 +2,7 @@
|
|||||||
# ID: execBin
|
# ID: execBin
|
||||||
# Author: Carlos Polop
|
# Author: Carlos Polop
|
||||||
# Last Update: 22-08-2023
|
# Last Update: 22-08-2023
|
||||||
# Description: Write and exected an embedded binary
|
# Description: Write and execute an embedded binary
|
||||||
# License: GNU GPL
|
# License: GNU GPL
|
||||||
# Version: 1.0
|
# Version: 1.0
|
||||||
# Functions Used: print_3title, print_info
|
# Functions Used: print_3title, print_info
|
||||||
@ -14,18 +14,41 @@
|
|||||||
|
|
||||||
|
|
||||||
execBin() {
|
execBin() {
|
||||||
TOOL_NAME=$1
|
TOOL_NAME=$1 # Display name
|
||||||
TOOL_LINK=$2
|
TOOL_LINK=$2 # Reference URL
|
||||||
B64_BIN=$3
|
B64_BIN=$3 # base64‑encoded executable
|
||||||
PARAMS=$4
|
PARAMS=$4 # Arguments to the tool
|
||||||
if [ "$B64_BIN" ]; then
|
|
||||||
echo ""
|
[ -z "$B64_BIN" ] && return 0 # nothing to do
|
||||||
|
|
||||||
|
echo
|
||||||
print_3title "Running $TOOL_NAME"
|
print_3title "Running $TOOL_NAME"
|
||||||
print_info "$TOOL_LINK"
|
print_info "$TOOL_LINK"
|
||||||
echo "$B64_BIN" | base64 -d > $Wfolder/bin
|
|
||||||
chmod +x $Wfolder/bin
|
TMP_BIN=$(mktemp "${Wfolder:-/tmp}/bin.XXXXXX") || { echo "mktemp failed"; return 1; }
|
||||||
eval "$Wfolder/bin $PARAMS"
|
printf '%s' "$B64_BIN" | base64 -d > "$TMP_BIN" || { echo "decode failed"; rm -f "$TMP_BIN"; return 1; }
|
||||||
rm -f $Wfolder/bin
|
chmod +x "$TMP_BIN"
|
||||||
echo ""
|
|
||||||
|
# ---------------- 120‑second wall‑clock timeout ----------------
|
||||||
|
if command -v timeout >/dev/null 2>&1; then # GNU/BSD timeout
|
||||||
|
timeout --preserve-status 120 "$TMP_BIN" $PARAMS
|
||||||
|
elif command -v gtimeout >/dev/null 2>&1; then # Homebrew coreutils (macOS)
|
||||||
|
gtimeout --preserve-status 120 "$TMP_BIN" $PARAMS
|
||||||
|
else # POSIX fall‑back
|
||||||
|
(
|
||||||
|
"$TMP_BIN" $PARAMS & # run in background
|
||||||
|
cmdpid=$!
|
||||||
|
( sleep 120 && kill -0 "$cmdpid" 2>/dev/null && kill -TERM "$cmdpid" ) &
|
||||||
|
watcher=$!
|
||||||
|
wait "$cmdpid"
|
||||||
|
rc=$?
|
||||||
|
kill -9 "$watcher" 2>/dev/null
|
||||||
|
exit $rc
|
||||||
|
)
|
||||||
fi
|
fi
|
||||||
|
rc=$?
|
||||||
|
|
||||||
|
rm -f "$TMP_BIN"
|
||||||
|
echo
|
||||||
|
return $rc
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user