From 103dff282db44ea6b492c5b175b81688fb5e09c9 Mon Sep 17 00:00:00 2001 From: arehbein Date: Sun, 13 Nov 2022 14:24:30 +0100 Subject: [PATCH] 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 --- ttcn3-dumpcap-start.sh | 16 ++++++++++++++-- ttcn3-dumpcap-stop.sh | 21 ++++++++++----------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/ttcn3-dumpcap-start.sh b/ttcn3-dumpcap-start.sh index eb390fddb..c4ead8c31 100755 --- a/ttcn3-dumpcap-start.sh +++ b/ttcn3-dumpcap-start.sh @@ -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 } diff --git a/ttcn3-dumpcap-stop.sh b/ttcn3-dumpcap-stop.sh index e13fbc0f4..64f261a3a 100755 --- a/ttcn3-dumpcap-stop.sh +++ b/ttcn3-dumpcap-stop.sh @@ -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