-
Notifications
You must be signed in to change notification settings - Fork 194
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
libc: retry open when interrupted #252
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.
Nice catch here!!
src/util_libc.rs
Outdated
loop { | ||
let fd = open(path.as_ptr() as *const _, libc::O_RDONLY | libc::O_CLOEXEC); | ||
if fd < 0 { | ||
let err = last_os_error(); | ||
// We should try again if the call was interrupted. | ||
if err.raw_os_error() != Some(libc::EINTR) { | ||
return Err(err); | ||
} | ||
} else { | ||
return Ok(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.
I'm wondering if it's more idiomatic to do:
let mut fd;
while { fd = open(...); fd < 0 } {
let err = last_os_error();
// We should try again if open() was interrupted.
if err.raw_os_error() != Some(libc::EINTR) {
return Err(err);
}
}
Ok(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.
Alternatively:
loop {
let fd = open(path.as_ptr() as *const _, libc::O_RDONLY | libc::O_CLOEXEC);
if fd >= 0 {
return Ok(fd);
}
let err = las_os_error();
// We should try again if open() was interrupted.
if err.raw_os_error() != Some(libc::EINTR) {
return Err(err);
}
}
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.
I have taken the second version, which received a thumbs up. :)
The open call can be interrupted. Since sys_fill_exact covers this for read operation already, it should be done here as well. Signed-off-by: Tobias Stoeckmann <[email protected]>
I'll give @newpavlov a little time to take a look, and then I'll merge this. |
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.
I wonder if it could be worth to limit number of retries to be extra safe by replacing loop { .. }
with something like this:
for _ in 0..256 {
// ..
}
Err(Error::TOO_MANY_ITERATIONS) // find a better name
It looks like Rust's |
The open call can be interrupted. Since sys_fill_exact covers this for read operation already, it should be done here as well. Signed-off-by: Tobias Stoeckmann <[email protected]>
The open call can be interrupted. Since sys_fill_exact covers this for
read operation already, it should be done here as well.
Signed-off-by: Tobias Stoeckmann [email protected]
Shoutout to c3h2-ctf.