Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl Socket {
)))]
{
let (socket, addr) = self.accept_raw()?;
let socket = set_common_flags(socket)?;
let socket = set_common_accept_flags(socket)?;
// `set_common_flags` does not disable inheritance on Windows because `Socket::new`
// unlike `accept` is able to create the socket with inheritance disabled.
#[cfg(windows)]
Expand Down Expand Up @@ -762,8 +762,8 @@ const fn set_common_type(ty: Type) -> Type {
}

/// Set `FD_CLOEXEC` and `NOSIGPIPE` on the `socket` for platforms that need it.
#[inline(always)]
#[allow(clippy::unnecessary_wraps)]
///
/// Sockets created via `accept` should use `set_common_accept_flags` instead.
fn set_common_flags(socket: Socket) -> io::Result<Socket> {
// On platforms that don't have `SOCK_CLOEXEC` use `FD_CLOEXEC`.
#[cfg(all(
Expand Down Expand Up @@ -798,6 +798,46 @@ fn set_common_flags(socket: Socket) -> io::Result<Socket> {
Ok(socket)
}

/// Set `FD_CLOEXEC` on the `socket` for platforms that need it.
///
/// Unlike `set_common_flags` we don't set `NOSIGPIPE` as that is inherited from
/// the listener. Furthermore, attempts to set it on a unix socket domain
/// results in an error.
#[cfg(not(any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "illumos",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "cygwin",
)))]
fn set_common_accept_flags(socket: Socket) -> io::Result<Socket> {
// On platforms that don't have `SOCK_CLOEXEC` use `FD_CLOEXEC`.
#[cfg(all(
unix,
not(any(
target_os = "android",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "fuchsia",
target_os = "hurd",
target_os = "illumos",
target_os = "linux",
target_os = "netbsd",
target_os = "openbsd",
target_os = "espidf",
target_os = "vita",
target_os = "cygwin",
))
))]
socket._set_cloexec(true)?;

Ok(socket)
}

/// A local interface specified by its index or an address assigned to it.
///
/// `Index(0)` and `Address(Ipv4Addr::UNSPECIFIED)` are equivalent and indicate
Expand Down
28 changes: 26 additions & 2 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ where
panic!("unexpected error: {}", io::Error::last_os_error());
}
assert_eq!(length as usize, size_of::<libc::c_int>());
assert_eq!(flags, want as _, "non-blocking option");
assert_eq!(flags, want as _);
}

const DATA: &[u8] = b"hello world";
Expand Down Expand Up @@ -605,7 +605,7 @@ fn unix() {
return;
}
let mut path = env::temp_dir();
path.push("socket2");
path.push("socket2.unix");
let _ = fs::remove_dir_all(&path);
fs::create_dir_all(&path).unwrap();
path.push("unix");
Expand All @@ -631,6 +631,30 @@ fn unix() {
assert_eq!(&buf[..n], DATA);
}

#[test]
fn unix_accept() {
if !unix_sockets_supported() {
return;
}
let mut path = env::temp_dir();
path.push("socket2.unix_accept");
let _ = fs::remove_dir_all(&path);
fs::create_dir_all(&path).unwrap();
path.push("unix_accept");

let listener = Socket::new(Domain::UNIX, Type::STREAM, None).unwrap();
listener.bind(&SockAddr::unix(&path).unwrap()).unwrap();
listener.listen(1).unwrap();

Socket::new(Domain::UNIX, Type::STREAM, None)
.unwrap()
.connect(&SockAddr::unix(path).unwrap())
.unwrap();

let (socket, _) = listener.accept().unwrap();
assert_common_flags(&socket, true);
}

#[test]
#[cfg(all(feature = "all", any(target_os = "android", target_os = "linux")))]
#[ignore = "using VSOCK family requires optional kernel support (works when enabled)"]
Expand Down