Skip to content

Commit 82a3487

Browse files
committed
Refactor NetFileDesc.
1 parent 77a7aee commit 82a3487

File tree

2 files changed

+22
-24
lines changed

2 files changed

+22
-24
lines changed

src/libstd/sys/unix/net.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,6 @@ impl Socket {
129129
let fd = cvt(netc::socket(fam, ty, 0))?;
130130
let fd = NetFileDesc::new(fd);
131131

132-
// Setting CLOEXEC is not supported on FreeRTOS since
133-
// there is no file system.
134-
#[cfg(not(target_os = "freertos"))]
135132
fd.set_cloexec()?;
136133

137134
let socket = Socket(fd);
@@ -271,9 +268,6 @@ impl Socket {
271268
let fd = cvt_r(|| unsafe { netc::accept(self.0.raw(), storage, len) })?;
272269
let fd = NetFileDesc::new(fd);
273270

274-
// Setting CLOEXEC is not supported on FreeRTOS since
275-
// there is no file system.
276-
#[cfg(not(target_os = "freertos"))]
277271
fd.set_cloexec()?;
278272

279273
Ok(Socket(fd))
@@ -411,8 +405,7 @@ impl Socket {
411405
#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
412406
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
413407
let mut nonblocking = nonblocking as libc::c_int;
414-
cvt(unsafe { netc::ioctl(*self.as_inner(), netc::FIONBIO, &mut nonblocking) })
415-
.map(drop)
408+
cvt(unsafe { netc::ioctl(*self.as_inner(), netc::FIONBIO, &mut nonblocking) }).map(drop)
416409
}
417410

418411
#[cfg(any(target_os = "solaris", target_os = "illumos"))]

src/libstd/sys/unix/net_fd.rs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
use crate::cmp;
44
use crate::io::{self, Read, Initializer, IoSlice, IoSliceMut};
55
use crate::mem;
6-
use crate::sys::{cvt, net::netc};
6+
use crate::sys::{cvt, net::netc::{self, c_int, c_void, ssize_t}};
77
use crate::sys_common::AsInner;
88

9-
use libc::{c_int, c_void, ssize_t};
10-
119
#[derive(Debug)]
1210
pub struct NetFileDesc {
1311
fd: c_int,
@@ -55,7 +53,7 @@ impl NetFileDesc {
5553
pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
5654
let ret = cvt(unsafe {
5755
netc::readv(self.fd,
58-
bufs.as_ptr() as *const libc::iovec,
56+
bufs.as_ptr() as *const netc::iovec,
5957
cmp::min(bufs.len(), c_int::max_value() as usize) as c_int)
6058
})?;
6159
Ok(ret as usize)
@@ -78,7 +76,7 @@ impl NetFileDesc {
7876
pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
7977
let ret = cvt(unsafe {
8078
netc::writev(self.fd,
81-
bufs.as_ptr() as *const libc::iovec,
79+
bufs.as_ptr() as *const netc::iovec,
8280
cmp::min(bufs.len(), c_int::max_value() as usize) as c_int)
8381
})?;
8482
Ok(ret as usize)
@@ -87,7 +85,7 @@ impl NetFileDesc {
8785
#[cfg(target_os = "linux")]
8886
pub fn get_cloexec(&self) -> io::Result<bool> {
8987
unsafe {
90-
Ok((cvt(libc::fcntl(self.fd, libc::F_GETFD))? & libc::FD_CLOEXEC) != 0)
88+
Ok((cvt(netc::fcntl(self.fd, netc::F_GETFD))? & netc::FD_CLOEXEC) != 0)
9189
}
9290
}
9391

@@ -101,29 +99,36 @@ impl NetFileDesc {
10199
target_os = "redox")))]
102100
pub fn set_cloexec(&self) -> io::Result<()> {
103101
unsafe {
104-
cvt(libc::ioctl(self.fd, libc::FIOCLEX))?;
102+
cvt(netc::ioctl(self.fd, netc::FIOCLEX))?;
105103
Ok(())
106104
}
107105
}
108-
#[cfg(any(target_env = "newlib",
106+
#[cfg(all(any(target_env = "newlib",
109107
target_os = "solaris",
110108
target_os = "emscripten",
111109
target_os = "fuchsia",
112110
target_os = "l4re",
113111
target_os = "linux",
114112
target_os = "haiku",
115-
target_os = "redox"))]
113+
target_os = "redox"), not(target_os = "freertos")))]
116114
pub fn set_cloexec(&self) -> io::Result<()> {
117115
unsafe {
118-
let previous = cvt(libc::fcntl(self.fd, libc::F_GETFD))?;
119-
let new = previous | libc::FD_CLOEXEC;
116+
let previous = cvt(netc::fcntl(self.fd, netc::F_GETFD))?;
117+
let new = previous | netc::FD_CLOEXEC;
120118
if new != previous {
121-
cvt(libc::fcntl(self.fd, libc::F_SETFD, new))?;
119+
cvt(netc::fcntl(self.fd, netc::F_SETFD, new))?;
122120
}
123121
Ok(())
124122
}
125123
}
126124

125+
// Setting `FD_CLOEXEC` is not supported on FreeRTOS
126+
// since there is no `exec` functionality.
127+
#[cfg(target_os = "freertos")]
128+
pub fn set_cloexec(&self) -> io::Result<()> {
129+
Ok(())
130+
}
131+
127132
pub fn duplicate(&self) -> io::Result<NetFileDesc> {
128133
use crate::sync::atomic::{AtomicBool, Ordering};
129134

@@ -143,9 +148,9 @@ impl NetFileDesc {
143148
//
144149
// [1]: http://comments.gmane.org/gmane.linux.lib.musl.general/2963
145150
#[cfg(any(target_os = "android", target_os = "haiku"))]
146-
use libc::F_DUPFD as F_DUPFD_CLOEXEC;
151+
use netc::F_DUPFD as F_DUPFD_CLOEXEC;
147152
#[cfg(not(any(target_os = "android", target_os="haiku")))]
148-
use libc::F_DUPFD_CLOEXEC;
153+
use netc::F_DUPFD_CLOEXEC;
149154

150155
let make_filedesc = |fd| {
151156
let fd = NetFileDesc::new(fd);
@@ -167,13 +172,13 @@ impl NetFileDesc {
167172
NetFileDesc::new(fd)
168173
})
169174
}
170-
Err(ref e) if e.raw_os_error() == Some(libc::EINVAL) => {
175+
Err(ref e) if e.raw_os_error() == Some(netc::EINVAL) => {
171176
TRY_CLOEXEC.store(false, Ordering::Relaxed);
172177
}
173178
Err(e) => return Err(e),
174179
}
175180
}
176-
cvt(unsafe { netc::fcntl(fd, libc::F_DUPFD, 0) }).and_then(make_filedesc)
181+
cvt(unsafe { netc::fcntl(fd, netc::F_DUPFD, 0) }).and_then(make_filedesc)
177182
}
178183
}
179184

0 commit comments

Comments
 (0)