3 # This script parses /proc/locks and finds the processes that are holding
4 # locks on CTDB databases. For all those processes the script dumps a
7 # This script can be used only if Samba is configured to use fcntl locks
8 # rather than mutex locks.
10 [ -n "$CTDB_BASE" ] || \
11 CTDB_BASE=$(d=$(dirname "$0") ; cd -P "$d" ; echo "$PWD")
13 . "${CTDB_BASE}/functions"
15 # type is at least mentioned in POSIX and more is portable than which(1)
16 # shellcheck disable=SC2039
17 if ! type gstack >/dev/null 2>&1 ; then
22 gdb -batch --quiet -nx "/proc/${_pid}/exe" "$_pid" \
23 -ex "thread apply all bt" 2>/dev/null |
24 grep '^\(#\|Thread \)'
28 # Load/cache database options from configuration file
35 echo "----- Stack trace for PID=${_pid} -----"
36 _state=$(ps -p "$_pid" -o state= | cut -c 1)
37 if [ "$_state" = "D" ] ; then
38 # Don't run gstack on a process in D state since
39 # gstack will hang until the process exits D state.
40 # Although it is possible for a process to transition
41 # to D state after this check, it is unlikely because
42 # if a process is stuck in D state then it is probably
43 # the reason why this script was called. Note that a
44 # kernel stack almost certainly won't help diagnose a
45 # deadlock... but it will probably give us someone to
47 echo "----- Process in D state, printing kernel stack only"
48 get_proc "${_pid}/stack"
58 # Use word splitting to squash whitespace
59 # shellcheck disable=SC2086
60 _pids=$(echo $_pids | tr ' ' '\n' | sort -u)
62 for _pid in $_pids; do
70 echo "===== Start of debug locks PID=$$ ====="
72 # Create sed expression to convert inodes to names.
73 # Filenames don't contain dashes and we want basenames
74 # shellcheck disable=SC2035
75 sed_cmd=$(cd "$CTDB_DBDIR" &&
76 stat -c "s#[0-9a-f]*:[0-9a-f]*:%i #%n #" *.tdb.* 2>/dev/null ;
77 cd "$CTDB_DBDIR_PERSISTENT" &&
78 stat -c "s#[0-9a-f]*:[0-9a-f]*:%i #%n #" *.tdb.* 2>/dev/null)
80 # Parse /proc/locks and extract following information
81 # pid process_name tdb_name offsets [W]
82 out=$( get_proc "locks" |
83 grep -F "POSIX ADVISORY WRITE" |
84 awk '{ if($2 == "->") { print $6, $7, $8, $9, "W" } else { print $5, $6, $7, $8 } }' |
85 while read pid rest ; do
86 pname=$(ps -p "$pid" -o comm=)
87 echo "$pid $pname $rest"
88 done | sed -e "$sed_cmd" | grep '\.tdb' )
90 if [ -n "$out" ]; then
91 # Log information about locks
94 # Find processes that are waiting for locks
95 dbs=$(echo "$out" | grep "W$" | awk '{print $3}')
98 pids=$(echo "$out" | grep -v "W$" | grep "$db" | grep -v ctdbd | awk '{print $1}')
99 all_pids="$all_pids $pids"
102 dump_stacks "$all_pids"
105 echo "===== End of debug locks PID=$$ ====="
106 )9>"${CTDB_SCRIPT_VARDIR}/debug_locks.lock" | script_log "ctdbd-lock"