Closed
Description
So apparently the crate is transitioning Epoll
to a new safer (and better documented!) abstraction. But this has yet to be released. Fine I thought, since I'm starting a new project, I will just use the latest master for now and switch to the next release that contains this.
However, it seems that Inotify
(which I also need) has lost AsRawFd
, but not gained a replacement to allow it to be used together with Epoll
? For my use case I need to epoll a few FDs, one of which is an inotify FD (that monitors a few files). Thus I need a way to combine these. I would prefer to avoid threads for this.
Is there no way to do this currently or am I missing something obvious?
Metadata
Metadata
Assignees
Labels
No labels
Activity
VorpalBlade commentedon Feb 6, 2023
Note: This is probably related to issue #1750 and may be an oversight in how to combine things.
asomers commentedon Feb 6, 2023
I think you're asking for a way to use
AsFd
withEpoll
?VorpalBlade commentedon Feb 6, 2023
I would want to use
AsFd
onInotify
such that I can monitor for newInotify
events withEpoll
while also monitoring for other things. This lets me keep my program single threaded.I have this workaround for now (horrible mess, and probably not sound, but it works):
VorpalBlade commentedon Feb 6, 2023
The way to fix this would be to add
impl AsFd for Inotify
.EDIT: Oh and that lifetime on
create_inotify
in my code above is very wrong. I don't quite understand why the compiler accepts it in fact! I would of course like to tie the lifetime toInotify
, but I can't figure out how to do it (I'm relatively new to rust). NeitherInotify<'a>
norInotify + 'a
is accepted (with varying errors),VorpalBlade commentedon Feb 6, 2023
Side note: I find it weird that the
epoll
API takes a pre-allocated array (nice, no allocations) whileInotify
returns a Vec. Consistency there would be nice. I prefer the pre-allocated array (since it can be allocated on the stack and avoid heap allocations).Also the docs for
Inotify::read_events
appears to be incorrect: The docs read "Returns as many events as available.". This does not seem to be the case when reading the code. It looks to me like it will read as many events as are available and fit into a 4096 byte buffer. If the buffer gets filled it will not read more. Though that may be complicated in blocking mode (what if there was exactly 4096 bytes available?) etc. Consider changing the documentation instead.inotify: Add AsFd to allow using with epoll (issue nix-rust#1998)
inotify: Add AsFd to allow using with epoll (issue nix-rust#1998)
ids1024 commentedon Feb 22, 2023
This is incorrect, in general. If the fd is used after it has been close, the same fd number may be reallocated to something else, which may be mmaped. So it could in principle cause soundness issues in combination with other code that is correctly using mmap. Though I don't believe it's normal to
write
to an inotify fd, so to get undefined behavior here would require somewhat convoluted code.BorrowedFd::borrow_raw
returns aBorrowedFd
with a generic lifetime. It's up to the caller to make sure it is sound. It is possible (but not necessarily sound) to have your function calling return an arbitrary generic lifetime as well.asomers commentedon Apr 2, 2023
I think
impl AsFd for Inotify
should definitely work. Would you care to submit a PR?Merge #1999