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

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

Change-Id: Id160384bf624a4eb0f419cb8ba07d8b69bb693f3
This commit is contained in:
arehbein 2022-11-13 14:24:30 +01:00
parent 579055bc21
commit 103dff282d
2 changed files with 24 additions and 13 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,18 +5,17 @@ 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