arehbein has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/30127 )
Change subject: WIP: dumpcap-start/stop: Make script fail, not hang on sudo prompt ......................................................................
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 --- M ttcn3-dumpcap-start.sh M ttcn3-dumpcap-stop.sh 2 files changed, 34 insertions(+), 16 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-ttcn3-hacks refs/changes/27/30127/1
diff --git a/ttcn3-dumpcap-start.sh b/ttcn3-dumpcap-start.sh index eb390fd..c4ead8c 100755 --- a/ttcn3-dumpcap-start.sh +++ b/ttcn3-dumpcap-start.sh @@ -13,9 +13,21 @@
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 e13fbc0..6746727 100755 --- a/ttcn3-dumpcap-stop.sh +++ b/ttcn3-dumpcap-stop.sh @@ -5,26 +5,32 @@ 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