Enable close-on-exec for native types
This commit is contained in:
parent
5d6138747d
commit
b17e24103b
|
@ -58,7 +58,7 @@ pub struct NotifyToken {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn notify_pair() -> rustix::io::Result<(Notify, 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((
|
Ok((
|
||||||
Notify { registered },
|
Notify { registered },
|
||||||
|
|
51
src/net.rs
51
src/net.rs
|
@ -27,7 +27,10 @@ pub struct TcpListenerBuilder {
|
||||||
|
|
||||||
impl TcpListener {
|
impl TcpListener {
|
||||||
pub fn try_accept(&self) -> rustix::io::Result<Option<(TcpStream, SocketAddrAny)>> {
|
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))),
|
Ok((inner, addr)) => Ok(Some((TcpStream { inner }, addr))),
|
||||||
Err(e) if e == rustix::io::Error::WOULDBLOCK || e == rustix::io::Error::AGAIN => {
|
Err(e) if e == rustix::io::Error::WOULDBLOCK || e == rustix::io::Error::AGAIN => {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
|
@ -37,21 +40,18 @@ impl TcpListener {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn bind(ip: IpAddr, port: u16) -> rustix::io::Result<TcpListenerBuilder> {
|
pub fn bind(ip: IpAddr, port: u16) -> rustix::io::Result<TcpListenerBuilder> {
|
||||||
let sock = match ip {
|
let family = match ip {
|
||||||
IpAddr::V4(_) => rustix::net::socket_with(
|
IpAddr::V4(_) => AddressFamily::INET,
|
||||||
AddressFamily::INET,
|
IpAddr::V6(_) => AddressFamily::INET6,
|
||||||
SocketType::STREAM,
|
|
||||||
SocketFlags::NONBLOCK,
|
|
||||||
Protocol::TCP,
|
|
||||||
)?,
|
|
||||||
IpAddr::V6(_) => rustix::net::socket_with(
|
|
||||||
AddressFamily::INET6,
|
|
||||||
SocketType::STREAM,
|
|
||||||
SocketFlags::NONBLOCK,
|
|
||||||
Protocol::TCP,
|
|
||||||
)?,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let sock = rustix::net::socket_with(
|
||||||
|
family,
|
||||||
|
SocketType::STREAM,
|
||||||
|
SocketFlags::NONBLOCK | SocketFlags::CLOEXEC,
|
||||||
|
Protocol::TCP,
|
||||||
|
)?;
|
||||||
|
|
||||||
rustix::net::sockopt::set_socket_reuseaddr(&sock, true)?;
|
rustix::net::sockopt::set_socket_reuseaddr(&sock, true)?;
|
||||||
|
|
||||||
Ok(TcpListenerBuilder {
|
Ok(TcpListenerBuilder {
|
||||||
|
@ -64,21 +64,18 @@ impl TcpListener {
|
||||||
|
|
||||||
impl TcpStream {
|
impl TcpStream {
|
||||||
pub fn connect(ip: IpAddr, port: u16) -> rustix::io::Result<TcpStreamBuilder> {
|
pub fn connect(ip: IpAddr, port: u16) -> rustix::io::Result<TcpStreamBuilder> {
|
||||||
let sock = match ip {
|
let family = match ip {
|
||||||
IpAddr::V4(_) => rustix::net::socket_with(
|
IpAddr::V4(_) => AddressFamily::INET,
|
||||||
AddressFamily::INET,
|
IpAddr::V6(_) => AddressFamily::INET6,
|
||||||
SocketType::STREAM,
|
|
||||||
SocketFlags::NONBLOCK,
|
|
||||||
Protocol::TCP,
|
|
||||||
)?,
|
|
||||||
IpAddr::V6(_) => rustix::net::socket_with(
|
|
||||||
AddressFamily::INET6,
|
|
||||||
SocketType::STREAM,
|
|
||||||
SocketFlags::NONBLOCK,
|
|
||||||
Protocol::TCP,
|
|
||||||
)?,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let sock = rustix::net::socket_with(
|
||||||
|
family,
|
||||||
|
SocketType::STREAM,
|
||||||
|
SocketFlags::NONBLOCK | SocketFlags::CLOEXEC,
|
||||||
|
Protocol::TCP,
|
||||||
|
)?;
|
||||||
|
|
||||||
Ok(TcpStreamBuilder {
|
Ok(TcpStreamBuilder {
|
||||||
ip,
|
ip,
|
||||||
port,
|
port,
|
||||||
|
|
Loading…
Reference in a new issue