WIP: dumpcap-start/stop: Make script fail, not hang on sudo prompt

- Also: Fix output of '-e' option in pure POSIX shell

Related: OS#5743

Change-Id: Id160384bf624a4eb0f419cb8ba07d8b69bb693f3
This commit is contained in:
arehbein 2022-11-13 14:24:30 +01:00
parent 579055bc21
commit 6f278c456b
2 changed files with 34 additions and 16 deletions

View File

@ -13,9 +13,21 @@ GSMTAP_PORT=4729
TESTCASE=$1
if ! [ "$(id -u)" = "0" ]; then
SUDOSTR="sudo -n"
# Check if sudo /usr/bin/kill, sudo /usr/bin/tcpdump can always be run without password prompt (otherwise this script may hang)
sudo -k
if ! (sudo -n echo test >/dev/null 2>&1); then
echo "Error: Make sure 'sudo /usr/bin/kill' and 'sudo /usr/bin/tcpdump' can be executed without a password (NOPASSWD in sudoers file)" >&2
exit 1
fi
else
SUDOSTR=""
fi
kill_rm_pidfile() {
if [ -e $1 ]; then
kill "$(cat "$1")"
if ! [ -e "$1" ] && [ -s "$1" ]; then
$SUDOSTR kill "$(cat "$1")" 2>&1 || grep -v "No such process"
rm $1
fi
}

View File

@ -5,26 +5,32 @@ PIDFILE_NETCAT=/tmp/netcat.pid
TESTCASE=$1
VERDICT="$2"
kill_rm_pidfile() {
if [ -e $1 ]; then
PSNAME="$(ps -q "$(cat "$1")" -o comm=)"
if [ "$PSNAME" != "sudo" ]; then
kill "$(cat "$1")"
else
# NOTE: This requires you to be root or something like
# "laforge ALL=NOPASSWD: /usr/sbin/tcpdump, /bin/kill" in your sudoers file
sudo kill "$(cat "$1")"
fi
rm $1
if ! [ "$(id -u)" = "0" ]; then
SUDOSTR="sudo -n"
else
SUDOSTR=""
fi
kill_rm_pidfile() {
if ! [ -e "$1" ] && [ -s "$1" ]; then
$SUDOSTR kill "$(cat "$1")" 2>&1 || grep -v "No such process"
rm $1
fi
}
date
if [ x"$VERDICT" = x"pass" ]; then
echo -e "\033[1;32m====== $TESTCASE $VERDICT ======\033[0m"
# -e only works/is required only in Bash; in dash/POSIX shells it isn't required
if (lsof -p $$ | grep -q /usr/bin/bash); then
ESCAPE_OPT="-e"
else
echo -e "\033[1;31m------ $TESTCASE $VERDICT ------\033[0m"
ESCAPE_OPT=""
fi
if [ x"$VERDICT" = x"pass" ]; then
echo $ESCAPE_OPT "\033[1;32m====== $TESTCASE $VERDICT ======\033[0m"
else
echo $ESCAPE_OPT "\033[1;31m------ $TESTCASE $VERDICT ------\033[0m"
fi
echo