-
Notifications
You must be signed in to change notification settings - Fork 679
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
Safe fd wrappers #1184
Safe fd wrappers #1184
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you intending to modify all of Nix's functions to take a FileDescriptor
?
|
||
impl Drop for FileDescriptor { | ||
fn drop(&mut self) { | ||
unistd::close(self.0).expect("You've already closed this file descriptor before me =("); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
close
can also fail for reasons like EINTR
. You shouldn't panic in such cases.
@@ -12,7 +12,7 @@ | |||
#![recursion_limit = "500"] | |||
#![deny(unused)] | |||
#![deny(unstable_features)] | |||
#![deny(missing_copy_implementations)] | |||
// #![deny(missing_copy_implementations)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Presumably you're planning to fix this before merging?
let this_fd = self.as_raw_fd(); | ||
let other_fd = other.as_raw_fd(); | ||
|
||
std::mem::forget(other); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about a comment explaining why it's ok to use mem::forget
?
unsafe { Ok(T::from_raw_fd(ret_fd)) } | ||
} | ||
|
||
/// You must guarantee the RawFd isn't owned by any of fd safe wrappers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment belongs in a # Safety
section.
std::mem::forget(other); | ||
|
||
// TODO: Make this call unsafe | ||
let ret_fd = dup2(this_fd, other_fd)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to call dup2
before calling mem::forget
, so that you don't deallocate other
if dup2
fails. In that event, you should probably return other
to the caller. That would make the error type a std::result::Result<T, (nix::Error, T)>
let this_fd = self.as_raw_fd(); | ||
let other_fd = other.as_raw_fd(); | ||
|
||
std::mem::forget(other); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if a user implements FdOps
for his own type, and Drop
does something more than just close
? In that case, this code would be wrong. Perhaps you should make FdOps
a private trait. If not, at least document the strict requirement of drop
's behavior.
|
||
impl FromRawFd for StdHandle { | ||
unsafe fn from_raw_fd(fd: RawFd) -> Self { | ||
assert!(fd < 3); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-1 to magic numbers. Instead of hard-coding 3, you should compare to the libc::STDERR_FILENO
etc
Here I'm trying to resolve #594 #678. Wish me luck!