Skip to content

Commit 0d3626a

Browse files
committed
sys/socket: add UnixAddr abstract name getter
This introduces an `as_abstract()` getter to `UnixAddr` in order to retrieve the name of an abstract unix socket. This also adds tests around abstract addresses and clarify docs, adding explicit semantics.
1 parent 1b9d205 commit 0d3626a

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

src/sys/socket/addr.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,9 @@ impl fmt::Display for Ipv6Addr {
521521
*
522522
*/
523523

524-
/// A wrapper around `sockaddr_un`. We track the length of `sun_path` (excluding
524+
/// A wrapper around `sockaddr_un`.
525+
///
526+
/// This also tracks the length of `sun_path` address (excluding
525527
/// a terminating null), because it may not be null-terminated. For example,
526528
/// unconnected and Linux abstract sockets are never null-terminated, and POSIX
527529
/// does not require that `sun_len` include the terminating null even for normal
@@ -555,10 +557,12 @@ impl UnixAddr {
555557
}))
556558
}
557559

558-
/// Create a new sockaddr_un representing an address in the
559-
/// "abstract namespace". This is a Linux-specific extension,
560-
/// primarily used to allow chrooted processes to communicate with
561-
/// specific daemons.
560+
/// Create a new `sockaddr_un` representing an address in the "abstract namespace".
561+
///
562+
/// The leading null byte for the abstract namespace is automatically added;
563+
/// thus the input `path` is expected to be the bare name, not null-prefixed.
564+
/// This is a Linux-specific extension, primarily used to allow chrooted
565+
/// processes to communicate with processes having a different filesystem view.
562566
pub fn new_abstract(path: &[u8]) -> Result<UnixAddr> {
563567
unsafe {
564568
let mut ret = libc::sockaddr_un {
@@ -587,7 +591,7 @@ impl UnixAddr {
587591
/// If this address represents a filesystem path, return that path.
588592
pub fn path(&self) -> Option<&Path> {
589593
if self.1 == 0 || self.0.sun_path[0] == 0 {
590-
// unbound or abstract
594+
// unnamed or abstract
591595
None
592596
} else {
593597
let p = self.sun_path();
@@ -600,6 +604,19 @@ impl UnixAddr {
600604
Some(Path::new(<OsStr as OsStrExt>::from_bytes(&p[..reallen])))
601605
}
602606
}
607+
608+
/// If this address represents an abstract socket, return its name.
609+
///
610+
/// For abstract sockets only the bare name is returned, without the
611+
/// leading null byte. `None` is returned for unnamed or path-backed sockets.
612+
pub fn as_abstract(&self) -> Option<&[u8]> {
613+
if self.1 >= 1 && self.0.sun_path[0] == 0 {
614+
Some(&self.sun_path()[1..])
615+
} else {
616+
// unnamed or filesystem path
617+
None
618+
}
619+
}
603620
}
604621

605622
impl PartialEq for UnixAddr {

test/sys/test_socket.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ pub fn test_path_to_sock_addr() {
6464
assert_eq!(addr.path(), Some(actual));
6565
}
6666

67+
// Test getting/setting abstract addresses (without unix socket creation)
68+
#[test]
69+
pub fn test_abstract_uds_addr() {
70+
let empty = String::new();
71+
let addr = UnixAddr::new_abstract(empty.as_bytes()).unwrap();
72+
assert_eq!(addr.as_abstract(), Some(empty.as_bytes()));
73+
74+
let name = String::from("nix\0abstract\0test");
75+
let addr = UnixAddr::new_abstract(name.as_bytes()).unwrap();
76+
assert_eq!(addr.as_abstract(), Some(name.as_bytes()));
77+
assert_eq!(addr.path(), None);
78+
79+
// Internally, name is null-prefixed (abstract namespace)
80+
let internal: &[u8] = unsafe {
81+
slice::from_raw_parts(addr.0.sun_path.as_ptr() as *const u8, addr.1)
82+
};
83+
let mut abstract_name = name.clone();
84+
abstract_name.insert(0, '\0');
85+
assert_eq!(internal, abstract_name.as_bytes());
86+
}
87+
6788
#[test]
6889
pub fn test_getsockname() {
6990
use nix::sys::socket::{socket, AddressFamily, SockType, SockFlag};

0 commit comments

Comments
 (0)