Enable close-on-exec for native types

This commit is contained in:
Aode (lion) 2022-02-15 10:42:26 -06:00
parent 5d6138747d
commit b17e24103b
2 changed files with 25 additions and 28 deletions

View file

@ -58,7 +58,7 @@ pub struct NotifyToken {
}
pub fn notify_pair() -> rustix::io::Result<(Notify, NotifyToken)> {
let (registered, free) = rustix::io::pipe_with(PipeFlags::NONBLOCK)?;
let (registered, free) = rustix::io::pipe_with(PipeFlags::NONBLOCK | PipeFlags::CLOEXEC)?;
Ok((
Notify { registered },

View file

@ -27,7 +27,10 @@ pub struct TcpListenerBuilder {
impl TcpListener {
pub fn try_accept(&self) -> rustix::io::Result<Option<(TcpStream, SocketAddrAny)>> {
match rustix::net::acceptfrom_with(&self.inner, AcceptFlags::NONBLOCK) {
match rustix::net::acceptfrom_with(
&self.inner,
AcceptFlags::NONBLOCK | AcceptFlags::CLOEXEC,
) {
Ok((inner, addr)) => Ok(Some((TcpStream { inner }, addr))),
Err(e) if e == rustix::io::Error::WOULDBLOCK || e == rustix::io::Error::AGAIN => {
Ok(None)
@ -37,21 +40,18 @@ impl TcpListener {
}
pub fn bind(ip: IpAddr, port: u16) -> rustix::io::Result<TcpListenerBuilder> {
let sock = match ip {
IpAddr::V4(_) => rustix::net::socket_with(
AddressFamily::INET,
SocketType::STREAM,
SocketFlags::NONBLOCK,
Protocol::TCP,
)?,
IpAddr::V6(_) => rustix::net::socket_with(
AddressFamily::INET6,
SocketType::STREAM,
SocketFlags::NONBLOCK,
Protocol::TCP,
)?,
let family = match ip {
IpAddr::V4(_) => AddressFamily::INET,
IpAddr::V6(_) => AddressFamily::INET6,
};
let sock = rustix::net::socket_with(
family,
SocketType::STREAM,
SocketFlags::NONBLOCK | SocketFlags::CLOEXEC,
Protocol::TCP,
)?;
rustix::net::sockopt::set_socket_reuseaddr(&sock, true)?;
Ok(TcpListenerBuilder {
@ -64,21 +64,18 @@ impl TcpListener {
impl TcpStream {
pub fn connect(ip: IpAddr, port: u16) -> rustix::io::Result<TcpStreamBuilder> {
let sock = match ip {
IpAddr::V4(_) => rustix::net::socket_with(
AddressFamily::INET,
SocketType::STREAM,
SocketFlags::NONBLOCK,
Protocol::TCP,
)?,
IpAddr::V6(_) => rustix::net::socket_with(
AddressFamily::INET6,
SocketType::STREAM,
SocketFlags::NONBLOCK,
Protocol::TCP,
)?,
let family = match ip {
IpAddr::V4(_) => AddressFamily::INET,
IpAddr::V6(_) => AddressFamily::INET6,
};
let sock = rustix::net::socket_with(
family,
SocketType::STREAM,
SocketFlags::NONBLOCK | SocketFlags::CLOEXEC,
Protocol::TCP,
)?;
Ok(TcpStreamBuilder {
ip,
port,