@@ -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,13 @@ 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.
566
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
562
567
pub fn new_abstract ( path : & [ u8 ] ) -> Result < UnixAddr > {
563
568
unsafe {
564
569
let mut ret = libc:: sockaddr_un {
@@ -587,7 +592,7 @@ impl UnixAddr {
587
592
/// If this address represents a filesystem path, return that path.
588
593
pub fn path ( & self ) -> Option < & Path > {
589
594
if self . 1 == 0 || self . 0 . sun_path [ 0 ] == 0 {
590
- // unbound or abstract
595
+ // unnamed or abstract
591
596
None
592
597
} else {
593
598
let p = self . sun_path ( ) ;
@@ -600,6 +605,20 @@ impl UnixAddr {
600
605
Some ( Path :: new ( <OsStr as OsStrExt >:: from_bytes ( & p[ ..reallen] ) ) )
601
606
}
602
607
}
608
+
609
+ /// If this address represents an abstract socket, return its name.
610
+ ///
611
+ /// For abstract sockets only the bare name is returned, without the
612
+ /// leading null byte. `None` is returned for unnamed or path-backed sockets.
613
+ #[ cfg( any( target_os = "android" , target_os = "linux" ) ) ]
614
+ pub fn as_abstract ( & self ) -> Option < & [ u8 ] > {
615
+ if self . 1 >= 1 && self . 0 . sun_path [ 0 ] == 0 {
616
+ Some ( & self . sun_path ( ) [ 1 ..] )
617
+ } else {
618
+ // unnamed or filesystem path
619
+ None
620
+ }
621
+ }
603
622
}
604
623
605
624
impl PartialEq for UnixAddr {
0 commit comments