Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filesystem: get_pids() "safe": scan /proc only once #1978

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions heartbeat/Filesystem
Original file line number Diff line number Diff line change
Expand Up @@ -678,17 +678,26 @@ get_pids()
# -mindepth 1 -not -path "/proc/[0-9]*" -prune -o ...
# -path "/proc/[!0-9]*" -prune -o ...
# -path "/proc/[0-9]*" -a ...
# the latter seemd to be significantly faster for this one in my naive test.
procs=$(exec 2>/dev/null;
find /proc -path "/proc/[0-9]*" -type l \( -lname "${dir}/*" -o -lname "${dir}" \) -print |
awk -F/ '{print $3}' | uniq)

# This finds both /proc/<pid>/maps and /proc/<pid>/task/<tid>/maps;
# if you don't want the latter, add -maxdepth.
mmap_procs=$(exec 2>/dev/null;
find /proc -path "/proc/[0-9]*/maps" -print |
xargs -r grep -l " ${dir}/" | awk -F/ '{print $3}' | uniq)
printf "${procs}\n${mmap_procs}" | sort -u

# find can detect relevant symlinks directly using -lname,
# but we need to grep the content of maps files.
# With many processes, scanning proc may be costly already.
# Do that only once, feed the "maps" files to grep via fifo and
# xargs, and output the matching symlinks directly.
local D
D=$(mktemp -d "$HA_RSCTMP/Filesystem.get_pids.safe.XXXXXX")
mkfifo "$D/maps"
(
< "$D/maps" xargs -r grep -l " $dir/" | cut -d/ -f3 | uniq &
exec 3> "$D/maps"; # reference for find via /dev/fd/3
# both xargs and find now have an open fd,
# we can remove the tmpdir and fifo early
rm -rf "$D"
find /proc -path '/proc/[!0-9]*' -prune -o \
-type f -name maps -fprint /dev/fd/3 -o \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-fprint is GNU findutils only.
POSIX manpage: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/find.html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-lname is not supported by POSIX, but available on *BSD as compatibility option, so no need to worry about that parameter.

-type l \( -lname "$dir/*" -o -lname "$dir" \) -print |
cut -d/ -f3 | uniq
) 2>/dev/null | sort -u
fi
}

Expand Down