-
Notifications
You must be signed in to change notification settings - Fork 675
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
pty: Add forkpty #1042
pty: Add forkpty #1042
Conversation
Note: |
94bf05f
to
e0f1a90
Compare
Err... failed CI for OSX but it passed all the tests? |
It hung on OSX. Probably a bug in |
@asomers yeah... i think i relied on linux kernel for how it handles a |
648b81d
to
67688aa
Compare
@asomers Got CI passing. Can you take a look |
Child => { | ||
write(0, string.as_bytes()).unwrap(); | ||
pause(); // we need the child to stay alive until the parent calls read | ||
}, |
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.
In a Rust test, you cannot return from a forked child. That can cause deadlocks. Instead, you must _exit(0)
. The SIGTERM
will probably suffice, but I would _exit(0)
just in case.
src/pty.rs
Outdated
/// the values in `winsize`. If `termios` is not `None`, the pseudoterminal's | ||
/// terminal settings of the slave will be set to the values in `termios`. | ||
#[inline] | ||
pub fn forkpty<'a, 'b, T: Into<Option<&'a Winsize>>, U: Into<Option<&'b Termios>>>(winsize: T, termios: U) -> Result<ForkptyResult> { |
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.
80 cols, please.
src/pty.rs
Outdated
/// If `winsize` is not `None`, the window size of the slave will be set to | ||
/// the values in `winsize`. If `termios` is not `None`, the pseudoterminal's | ||
/// terminal settings of the slave will be set to the values in `termios`. | ||
#[inline] |
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.
Why inline such a big function? In fact, it's rarely a good idea to use #[inline]
at all. Better to let the compiler decide.
src/pty.rs
Outdated
|
||
let mut master: libc::c_int = unsafe { mem::uninitialized() }; | ||
let res = { | ||
match (termios.into(), winsize.into()) { |
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 think you could considerably shorten this block by using something like termios.into().unwrap_or(ptr::null_mut())
.
src/pty.rs
Outdated
/// This is returned by `forkpty`. Note that this type does *not* implement `Drop`, so the user | ||
/// must manually close the file descriptors. | ||
#[derive(Clone, Copy)] | ||
#[allow(missing_debug_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.
Why not derive Debug
?
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.
Now that you've derived Debug
, you should remove the #[allow(missing_debug_implementations)]
test/test_pty.rs
Outdated
let pty = forkpty(None, None).unwrap(); | ||
match pty.fork_result { | ||
Child => { | ||
write(0, string.as_bytes()).unwrap(); |
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.
Yikes! Can you eliminate that magic number?
@asomers Made the suggested changes. |
src/pty.rs
Outdated
/// This is returned by `forkpty`. Note that this type does *not* implement `Drop`, so the user | ||
/// must manually close the file descriptors. | ||
#[derive(Clone, Copy)] | ||
#[allow(missing_debug_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.
Now that you've derived Debug
, you should remove the #[allow(missing_debug_implementations)]
src/pty.rs
Outdated
None => ptr::null_mut(), | ||
}; | ||
|
||
let win = match winsize.into() { |
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.
FYI, instead of match winsize.into()
you could equivalently do winsize.into().map(|ws| ws as *const Winsize).unwrap_or(ptr::null_mut())
. It's just a matter of which style you prefer. Either is acceptable.
@asomers Thanks for the comments. I made the changes. Since the initial code was based largely off of |
@asomers anything left to fix for this? |
|
||
Ok(ForkptyResult { | ||
master: master, | ||
fork_result: fork_result, |
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.
If libc::forkpty
fails, then master
will be uninitialized here. For that matter, the return value will be wrong, too. You should ensure that it returns Err
if libc::forkpty
fails.
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.
Errno::result(res)
should convert to an Err
if forkpty
fails, and ?
on line 320 should propagate the error.
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.
Ahh, I didn't see that little ?
.
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.
Ok, this looks good. All you need to do is add a CHANGELOG entry and squash.
|
||
Ok(ForkptyResult { | ||
master: master, | ||
fork_result: fork_result, |
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.
Ahh, I didn't see that little ?
.
@asomers done |
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.
Thanks for your contribution!
bors r+
Build succeeded
|
No description provided.