]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ctdb-scripts: Avoid direct /proc access
authorMartin Schwenke <martin@meltin.net>
Mon, 4 Jan 2021 00:54:38 +0000 (11:54 +1100)
committerAmitay Isaacs <amitay@samba.org>
Fri, 28 May 2021 06:46:29 +0000 (06:46 +0000)
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 <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/config/debug_locks.sh

index 6f012bd335539b7054857f65a944c1800924171c..7c5fd50d8aacc6bd24aa987aa589d91dded2898e 100755 (executable)
@@ -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' )