osmith has uploaded this change for review. (
https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/40164?usp=email )
Change subject: tcpdump/dumpcap: move kill_rm_pidfile to inc file
......................................................................
tcpdump/dumpcap: move kill_rm_pidfile to inc file
Move kill_rm_pidfile out of the 4 ttcn3 tcpdump/dumpcap scripts into a
shared include file. Use the version of the function that only tries to
kill the command with sudo if it was started with sudo.
This fixes dumpcap not stopping if:
* it was started with ttcn3-tcpdump-start.sh (despite the name it will
start dumpcap instead of tcpdump if dumpcap was found), where it gets
started without sudo, and
* no rule is in the user's sudoers file is set to run kill as root with
NOPASSWD.
Change-Id: Ia63c37b54fb42a8c64a676334cef6f9df4152e18
---
A _scripts/tcpdump-dumpcap.inc.sh
M ttcn3-dumpcap-start.sh
M ttcn3-dumpcap-stop.sh
M ttcn3-tcpdump-start.sh
M ttcn3-tcpdump-stop.sh
5 files changed, 23 insertions(+), 43 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-ttcn3-hacks refs/changes/64/40164/1
diff --git a/_scripts/tcpdump-dumpcap.inc.sh b/_scripts/tcpdump-dumpcap.inc.sh
new file mode 100644
index 0000000..d033998
--- /dev/null
+++ b/_scripts/tcpdump-dumpcap.inc.sh
@@ -0,0 +1,15 @@
+# This file gets included by ttcn3-{tcpdump,dumpcap}-{start,stop} scripts
+
+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
+ fi
+}
diff --git a/ttcn3-dumpcap-start.sh b/ttcn3-dumpcap-start.sh
index a1e64fc..ddd4eb4 100755
--- a/ttcn3-dumpcap-start.sh
+++ b/ttcn3-dumpcap-start.sh
@@ -4,6 +4,8 @@
# needed when we want to capture from interfaces of different link
# types. It will also store the results as pcap-ng, not plain old pcap.
+. "$(dirname "$0")/_scripts/tcpdump-dumpcap.inc.sh"
+
PIDFILE_PCAP=/tmp/pcap.pid
DUMPCAP=/usr/bin/dumpcap
@@ -13,13 +15,6 @@
TESTCASE=$1
-kill_rm_pidfile() {
- if [ -e $1 ]; then
- kill "$(cat "$1")"
- rm $1
- fi
-}
-
echo "------ $TESTCASE ------"
date
diff --git a/ttcn3-dumpcap-stop.sh b/ttcn3-dumpcap-stop.sh
index 95bcdb6..1ac8e7b 100755
--- a/ttcn3-dumpcap-stop.sh
+++ b/ttcn3-dumpcap-stop.sh
@@ -1,24 +1,12 @@
#!/bin/sh
+. "$(dirname "$0")/_scripts/tcpdump-dumpcap.inc.sh"
+
PIDFILE_PCAP=/tmp/pcap.pid
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
-fi
-}
-
date
if [ x"$VERDICT" = x"pass" ]; then
diff --git a/ttcn3-tcpdump-start.sh b/ttcn3-tcpdump-start.sh
index 9575a68..2c2f8c5 100755
--- a/ttcn3-tcpdump-start.sh
+++ b/ttcn3-tcpdump-start.sh
@@ -1,5 +1,7 @@
#!/bin/sh
+. "$(dirname "$0")/_scripts/tcpdump-dumpcap.inc.sh"
+
PIDFILE_PCAP=/tmp/pcap.pid
TCPDUMP=$(command -v tcpdump)
DUMPCAP=$(command -v dumpcap)
@@ -18,17 +20,6 @@
# and this script will hang indefinitely
fi
-kill_rm_pidfile() {
- # NOTE: This requires you to be root or something like
- # "laforge ALL=NOPASSWD: /usr/sbin/tcpdump, /bin/kill" in your sudoers file
- if [ -e "$1" ]; then
- if [ -s "$1" ]; then
- $SUDOSTR kill "$(cat "$1")" 2>&1 | grep -v "No such
process"
- fi
- rm $1
- fi
-}
-
echo "------ $TESTCASE ------"
date
diff --git a/ttcn3-tcpdump-stop.sh b/ttcn3-tcpdump-stop.sh
index def10d8..65aeb2a 100755
--- a/ttcn3-tcpdump-stop.sh
+++ b/ttcn3-tcpdump-stop.sh
@@ -1,5 +1,7 @@
#!/bin/sh
+. "$(dirname "$0")/_scripts/tcpdump-dumpcap.inc.sh"
+
PIDFILE_PCAP=/tmp/pcap.pid
PIDFILE_NETCAT=/tmp/netcat.pid
FIFO=/tmp/cmderr
@@ -13,17 +15,6 @@
# and this script will hang indefinitely
fi
-kill_rm_pidfile() {
- # NOTE: This requires you to be root or something like
- # "laforge ALL=NOPASSWD: /usr/sbin/tcpdump, /bin/kill" in your sudoers file
- if [ -e "$1" ]; then
- if [ -s "$1" ]; then
- $SUDOSTR kill "$(cat "$1")" 2>&1 | grep -v "No such
process"
- fi
- rm $1
- fi
-}
-
date
if [ x"$VERDICT" = x"pass" ]; then
--
To view, visit
https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/40164?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: osmo-ttcn3-hacks
Gerrit-Branch: master
Gerrit-Change-Id: Ia63c37b54fb42a8c64a676334cef6f9df4152e18
Gerrit-Change-Number: 40164
Gerrit-PatchSet: 1
Gerrit-Owner: osmith <osmith(a)sysmocom.de>