RFC: Change Process to use directory fd #127
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
All data accessors now use openat and readlinkat when reading procfs entries, to protect against PID reuse. Also, iterators are now used instead of
Vec
in any place where a file descriptor is held open. This is an API break, see examples/netstat.rs and examples/process_hierarchy.rs for the most obvious effects on library consumers. I also removed thestat
field as suggested in #69. I haven't made any documentation changes yet, but I can if the approach here looks good. I used a few functions from thenix
library for convenience, but those are simple enough that they can potentially be re-implemented here if the dependency isn't desired.A downside of this is that simple cases using
stat
are now slower. If all you need during iteration is the information from/proc/<pid>/stat
, that now takes two additional syscalls to open and close the directory fd. We could speed that up by adding another function to iterate over the pids, and then adding more constructors to the relevant structures (Stat
,MemoryMap
,Status
...) that directly read from/proc/<pid>/<name>
.It would have to be noted in the documentation that it's only safe to use one of those constructors while iterating, because of PID recycling; for cases where two or more files need to be read from within an individual/proc/<pid>
directory, the fd-holding iterator fromall_processes()
must be used.On second thought, a better way to handle those types of iterations would be to put the pid in a separate
Pid
structure that then gets consumed by a move into a newStat
constructor. That way it can't accidentally be used twice.If this all sounds good, I'd like to use this as a base to add pidfd support as well, to support race-free waiting and signaling on procfs processes.
Fixes #125