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

RFC: Change Process to use directory fd #127

Closed
wants to merge 1 commit into from
Closed

RFC: Change Process to use directory fd #127

wants to merge 1 commit into from

Conversation

ghost
Copy link

@ghost ghost commented May 11, 2021

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 the stat 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 the nix 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 from all_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 new Stat 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

All data accessors now use openat and readlinkat when reading procfs
entries, to prevent PID reuse.
@eminence
Copy link
Owner

eminence commented Jun 7, 2021

I'm sorry I haven't gotten a chance to look at this yet. I'm going to try to take a look within a week.

@ghost ghost changed the title Change Process to use directory fd RFC: Change Process to use directory fd Jun 7, 2021
@ghost
Copy link
Author

ghost commented Jun 7, 2021

No rush. 😃 This is still a very rough draft.

@sunfishcode
Copy link
Contributor

This patch was incorporated into #171, which is now merged.

@eminence eminence closed this Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Process struct should carry an fd and use openat
2 participants