Skip to content

Commit b42d698

Browse files
committed
Filesystem: stop/get_pids to be signaled
The "safe" way to get process ids that may be using a particular filesystem currently uses shell globs ("find /proc/[0-9]*"). With a million processes (and/or a less capable shell), that may result in "Argument list too long". Replace with find /proc -path "/proc/[0-9]*" instead. While at it, also fix the non-posix -or to be -o, and add explicit grouping parentheses \( \) and explicit -print. Add a comment to not include "interesting" characters in mount point names.
1 parent f02afd0 commit b42d698

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

heartbeat/Filesystem

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -669,9 +669,26 @@ get_pids()
669669
$FUSER -Mm $dir 2>/dev/null
670670
fi
671671
elif [ "$FORCE_UNMOUNT" = "safe" ]; then
672-
procs=$(find /proc/[0-9]*/ -type l -lname "${dir}/*" -or -lname "${dir}" 2>/dev/null | awk -F/ '{print $3}')
673-
mmap_procs=$(grep " ${dir}/" /proc/[0-9]*/maps | awk -F/ '{print $3}')
674-
printf "${procs}\n${mmap_procs}" | sort | uniq
672+
# Yes, in theory, ${dir} could contain "intersting" characters
673+
# and would need to be quoted for glob (find) and regex (grep).
674+
# Don't do that, then.
675+
676+
# Avoid /proc/[0-9]*, it may cause "Argument list too long".
677+
# There are several ways to filter for /proc/<pid>
678+
# -mindepth 1 -not -path "/proc/[0-9]*" -prune -o ...
679+
# -path "/proc/[!0-9]*" -prune -o ...
680+
# -path "/proc/[0-9]*" -a ...
681+
# the latter seemd to be significantly faster for this one in my naive test.
682+
procs=$(exec 2>/dev/null;
683+
find /proc -path "/proc/[0-9]*" -type l \( -lname "${dir}/*" -o -lname "${dir}" \) -print |
684+
awk -F/ '{print $3}' | uniq)
685+
686+
# This finds both /proc/<pid>/maps and /proc/<pid>/task/<tid>/maps;
687+
# if you don't want the latter, add -maxdepth.
688+
mmap_procs=$(exec 2>/dev/null;
689+
find /proc -path "/proc/[0-9]*/maps" -print |
690+
xargs -r grep -l " ${dir}/" | awk -F/ '{print $3}' | uniq)
691+
printf "${procs}\n${mmap_procs}" | sort -u
675692
fi
676693
}
677694

0 commit comments

Comments
 (0)