-
Notifications
You must be signed in to change notification settings - Fork 597
Filesystem: get_pids() "safe": scan /proc only once #1978
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 \ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -fprint is GNU findutils only. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.