Skip to content

Commit 155852f

Browse files
committed
Refactor stdio
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
1 parent 9a7b1f2 commit 155852f

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/native_mux_impl/stdio.rs

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

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

4949
Ok(())
5050
}
5151

52+
fn set_blocking(fd: RawFd) -> Result<(), Error> {
53+
set_blocking_inner(fd).map_err(Error::ChildIo)
54+
}
55+
5256
impl Fd {
5357
pub(crate) fn as_raw_fd_or_null_fd(&self) -> Result<RawFd, Error> {
5458
use Fd::*;
@@ -66,12 +70,7 @@ impl Fd {
6670
/// the ownershipt of it.
6771
unsafe fn new_owned<T: IntoRawFd>(fd: T) -> Result<Self, Error> {
6872
let raw_fd = fd.into_raw_fd();
69-
// Create owned_fd so that the fd will be closed on error
70-
let owned_fd = OwnedFd::from_raw_fd(raw_fd);
71-
72-
set_blocking(raw_fd).map_err(Error::ChildIo)?;
73-
74-
Ok(Fd::Owned(owned_fd))
73+
Ok(Fd::Owned(OwnedFd::from_raw_fd(raw_fd)))
7574
}
7675
}
7776

@@ -106,12 +105,16 @@ impl Stdio {
106105
StdioImpl::Null => Ok((Fd::Null, None)),
107106
StdioImpl::Pipe => {
108107
let (read, write) = create_pipe()?;
108+
109+
// read end will be sent to ssh multiplex server
110+
// and it expects blocking fd.
111+
set_blocking(read.as_raw_fd())?;
109112
Ok((read.try_into()?, Some(write)))
110113
}
111114
StdioImpl::Fd(fd, owned) => {
112115
let raw_fd = fd.as_raw_fd();
113116
if *owned {
114-
set_blocking(raw_fd).map_err(Error::ChildIo)?;
117+
set_blocking(raw_fd)?;
115118
}
116119
Ok((Fd::Borrowed(raw_fd), None))
117120
}
@@ -124,12 +127,16 @@ impl Stdio {
124127
StdioImpl::Null => Ok((Fd::Null, None)),
125128
StdioImpl::Pipe => {
126129
let (read, write) = create_pipe()?;
130+
131+
// write end will be sent to ssh multiplex server
132+
// and it expects blocking fd.
133+
set_blocking(write.as_raw_fd())?;
127134
Ok((write.try_into()?, Some(read)))
128135
}
129136
StdioImpl::Fd(fd, owned) => {
130137
let raw_fd = fd.as_raw_fd();
131138
if *owned {
132-
set_blocking(raw_fd).map_err(Error::ChildIo)?;
139+
set_blocking(raw_fd)?;
133140
}
134141
Ok((Fd::Borrowed(raw_fd), None))
135142
}

0 commit comments

Comments
 (0)