From: Martin Schwenke Date: Mon, 4 Jan 2021 00:54:38 +0000 (+1100) Subject: ctdb-scripts: Avoid direct /proc access X-Git-Tag: tevent-0.11.0~710 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ffb56c9143d1d4b9a2dfaa8300be6ce5d6881ab1;p=thirdparty%2Fsamba.git ctdb-scripts: Avoid direct /proc access The main reason for this is to facilitate testing. Avoid some /proc accesses entirely by using ps(1) (which can be replaced by a stub when testing) because this script might as well be more portable in case anyone wants to add lock debugging for a non-Linux platform. While the "state" format specification isn't POSIX-compliant, it works on both Linux and FreeBSD so it is a reasonable improvement. Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs --- diff --git a/ctdb/config/debug_locks.sh b/ctdb/config/debug_locks.sh index 6f012bd3355..7c5fd50d8aa 100755 --- a/ctdb/config/debug_locks.sh +++ b/ctdb/config/debug_locks.sh @@ -33,10 +33,8 @@ dump_stack () _pid="$1" echo "----- Stack trace for PID=${_pid} -----" - # x is intentionally ignored - # shellcheck disable=SC2034 - read x x state x <"/proc/${_pid}/stat" - if [ "$state" = "D" ] ; then + _state=$(ps -p "$_pid" -o state= | cut -c 1) + if [ "$_state" = "D" ] ; then # Don't run gstack on a process in D state since # gstack will hang until the process exits D state. # Although it is possible for a process to transition @@ -47,7 +45,7 @@ dump_stack () # deadlock... but it will probably give us someone to # blame! echo "----- Process in D state, printing kernel stack only" - cat "/proc/${_pid}/stack" + get_proc "${_pid}/stack" else gstack "$_pid" fi @@ -81,10 +79,11 @@ dump_stacks () # Parse /proc/locks and extract following information # pid process_name tdb_name offsets [W] - out=$( grep -F "POSIX ADVISORY WRITE" /proc/locks | + out=$( get_proc "locks" | + grep -F "POSIX ADVISORY WRITE" | awk '{ if($2 == "->") { print $6, $7, $8, $9, "W" } else { print $5, $6, $7, $8 } }' | while read pid rest ; do - pname=$(readlink "/proc/${pid}/exe") + pname=$(ps -p "$pid" -o comm=) echo "$pid $pname $rest" done | sed -e "$sed_cmd" | grep '\.tdb' )