Skip to content

Commit

Permalink
fixup! Added better support for unnamed unix socket addrs
Browse files Browse the repository at this point in the history
Make Linux-only
  • Loading branch information
stevenengler committed Nov 7, 2022
1 parent fb9f0b7 commit 60f17d7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
17 changes: 5 additions & 12 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,25 +830,16 @@ impl UnixAddr {
}

/// Create a new `sockaddr_un` representing an "unnamed" unix socket address.
#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg_attr(docsrs, doc(cfg(all())))]
pub fn new_unnamed() -> UnixAddr {
#[allow(unused)]
let mut ret = libc::sockaddr_un {
let ret = libc::sockaddr_un {
sun_family: AddressFamily::Unix as sa_family_t,
.. unsafe { mem::zeroed() }
};

let sun_len: u8 = offset_of!(libc::sockaddr_un, sun_path).try_into().unwrap();

#[cfg(any(target_os = "dragonfly",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
target_os = "netbsd",
target_os = "openbsd"))]
{
ret.sun_len = sun_len;
}

unsafe { UnixAddr::from_raw_parts(ret, sun_len) }
}

Expand Down Expand Up @@ -906,6 +897,8 @@ impl UnixAddr {
}

/// Check if this address is an "unnamed" unix socket address.
#[cfg(any(target_os = "android", target_os = "linux"))]
#[cfg_attr(docsrs, doc(cfg(all())))]
#[inline]
pub fn is_unnamed(&self) -> bool {
matches!(self.kind(), UnixAddrKind::Unnamed)
Expand Down
5 changes: 3 additions & 2 deletions test/sys/test_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub fn test_addr_equality_abstract() {
}

// Test getting/setting abstract addresses (without unix socket creation)
#[cfg(target_os = "linux")]
#[cfg(any(target_os = "android", target_os = "linux"))]
#[test]
pub fn test_abstract_uds_addr() {
let empty = String::new();
Expand All @@ -245,6 +245,7 @@ pub fn test_abstract_uds_addr() {
}

// Test getting an unnamed address (without unix socket creation)
#[cfg(any(target_os = "android", target_os = "linux"))]
#[test]
pub fn test_unnamed_uds_addr() {
use crate::nix::sys::socket::SockaddrLike;
Expand All @@ -256,7 +257,6 @@ pub fn test_unnamed_uds_addr() {
assert!(addr.path().is_none());
assert_eq!(addr.path_len(), 0);

#[cfg(target_os = "linux")]
assert!(addr.as_abstract().is_none());
}

Expand Down Expand Up @@ -1544,6 +1544,7 @@ pub fn test_named_unixdomain() {
}

// Test using unnamed unix domain addresses
#[cfg(any(target_os = "android", target_os = "linux"))]
#[test]
pub fn test_unnamed_unixdomain() {
use nix::sys::socket::{getsockname, socketpair};
Expand Down

0 comments on commit 60f17d7

Please sign in to comment.