@@ -521,7 +521,9 @@ impl fmt::Display for Ipv6Addr {
521
521
*
522
522
*/
523
523
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
525
527
/// a terminating null), because it may not be null-terminated. For example,
526
528
/// unconnected and Linux abstract sockets are never null-terminated, and POSIX
527
529
/// does not require that `sun_len` include the terminating null even for normal
@@ -555,10 +557,12 @@ impl UnixAddr {
555
557
} ) )
556
558
}
557
559
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.
562
566
pub fn new_abstract ( path : & [ u8 ] ) -> Result < UnixAddr > {
563
567
unsafe {
564
568
let mut ret = libc:: sockaddr_un {
@@ -587,7 +591,7 @@ impl UnixAddr {
587
591
/// If this address represents a filesystem path, return that path.
588
592
pub fn path ( & self ) -> Option < & Path > {
589
593
if self . 1 == 0 || self . 0 . sun_path [ 0 ] == 0 {
590
- // unbound or abstract
594
+ // unnamed or abstract
591
595
None
592
596
} else {
593
597
let p = self . sun_path ( ) ;
@@ -600,6 +604,19 @@ impl UnixAddr {
600
604
Some ( Path :: new ( <OsStr as OsStrExt >:: from_bytes ( & p[ ..reallen] ) ) )
601
605
}
602
606
}
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
+ }
603
620
}
604
621
605
622
impl PartialEq for UnixAddr {
0 commit comments