Skip to content

Commit 139e070

Browse files
committed
Refactor: Invoke set_blocking in Command::spawn
Signed-off-by: Jiahao XU <[email protected]>
1 parent 527843e commit 139e070

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

src/native_mux_impl/command.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::Error;
22
use super::RemoteChild;
3-
use super::{ChildStderr, ChildStdin, ChildStdout, Stdio};
3+
use super::{stdio::set_blocking, ChildStderr, ChildStdin, ChildStdout, Stdio};
44

55
use std::borrow::Cow;
66
use std::ffi::OsStr;
@@ -71,6 +71,10 @@ impl Command {
7171
stderr.as_raw_fd_or_null_fd()?,
7272
];
7373

74+
for stdio in stdios {
75+
set_blocking(stdio).map_err(Error::ChildIo)?;
76+
}
77+
7478
let cmd = NonZeroByteSlice::new(&self.cmd).ok_or(Error::InvalidCommand)?;
7579

7680
let session = Session::builder()

src/native_mux_impl/stdio.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn cvt(ret: c_int) -> io::Result<c_int> {
4242
}
4343
}
4444

45-
fn set_blocking(fd: RawFd) -> io::Result<()> {
45+
pub(super) fn set_blocking(fd: RawFd) -> io::Result<()> {
4646
let flags = cvt(unsafe { fcntl(fd, F_GETFL) })?;
4747
cvt(unsafe { fcntl(fd, F_SETFL, flags & (!O_NONBLOCK)) })?;
4848

@@ -53,18 +53,10 @@ impl Fd {
5353
pub(crate) fn as_raw_fd_or_null_fd(&self) -> Result<RawFd, Error> {
5454
use Fd::*;
5555

56-
let fd = match self {
57-
Owned(owned_fd) => Some(owned_fd.as_raw_fd()),
58-
Borrowed(rawfd) => Some(*rawfd),
59-
Null => None,
60-
};
61-
62-
if let Some(fd) = fd {
63-
set_blocking(fd).map_err(Error::ChildIo)?;
64-
65-
Ok(fd)
66-
} else {
67-
get_null_fd()
56+
match self {
57+
Owned(owned_fd) => Ok(owned_fd.as_raw_fd()),
58+
Borrowed(rawfd) => Ok(*rawfd),
59+
Null => get_null_fd(),
6860
}
6961
}
7062

0 commit comments

Comments
 (0)