Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys/socket: add UnixAddr abstract name getter #785

Merged
merged 1 commit into from
Nov 19, 2017
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Added `nix::sys::uio::{process_vm_readv, process_vm_writev}` on Linux
([#568](https://github.com/nix-rust/nix/pull/568))
- Added `nix::unistd::{getgroups, setgroups, getgrouplist, initgroups}`. ([#733](https://github.com/nix-rust/nix/pull/733))
- Added `nix::sys::socket::UnixAddr::as_abstract` on Linux and Android.
([#785](https://github.com/nix-rust/nix/pull/785))

### Changed
- Renamed existing `ptrace` wrappers to encourage namespacing ([#692](https://github.com/nix-rust/nix/pull/692))
Expand Down Expand Up @@ -69,6 +71,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Moved constants ptrace request, event and options to enums and updated ptrace functions and argument types accordingly.
([#749](https://github.com/nix-rust/nix/pull/749))
- `AioCb::Drop` will now panic if the `AioCb` is still in-progress ([#715](https://github.com/nix-rust/nix/pull/715))
- Restricted `nix::sys::socket::UnixAddr::new_abstract` to Linux and Android only.
([#785](https://github.com/nix-rust/nix/pull/785))


### Fixed
- Fix compilation and tests for OpenBSD targets
Expand Down
31 changes: 25 additions & 6 deletions src/sys/socket/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,9 @@ impl fmt::Display for Ipv6Addr {
*
*/

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

/// Create a new sockaddr_un representing an address in the
/// "abstract namespace". This is a Linux-specific extension,
/// primarily used to allow chrooted processes to communicate with
/// specific daemons.
/// Create a new `sockaddr_un` representing an address in the "abstract namespace".
///
/// The leading null byte for the abstract namespace is automatically added;
/// thus the input `path` is expected to be the bare name, not null-prefixed.
/// This is a Linux-specific extension, primarily used to allow chrooted
/// processes to communicate with processes having a different filesystem view.
#[cfg(any(target_os = "android", target_os = "linux"))]
pub fn new_abstract(path: &[u8]) -> Result<UnixAddr> {
unsafe {
let mut ret = libc::sockaddr_un {
Expand Down Expand Up @@ -587,7 +592,7 @@ impl UnixAddr {
/// If this address represents a filesystem path, return that path.
pub fn path(&self) -> Option<&Path> {
if self.1 == 0 || self.0.sun_path[0] == 0 {
// unbound or abstract
// unnamed or abstract
None
} else {
let p = self.sun_path();
Expand All @@ -600,6 +605,20 @@ impl UnixAddr {
Some(Path::new(<OsStr as OsStrExt>::from_bytes(&p[..reallen])))
}
}

/// If this address represents an abstract socket, return its name.
///
/// For abstract sockets only the bare name is returned, without the
/// leading null byte. `None` is returned for unnamed or path-backed sockets.
#[cfg(any(target_os = "android", target_os = "linux"))]
pub fn as_abstract(&self) -> Option<&[u8]> {
if self.1 >= 1 && self.0.sun_path[0] == 0 {
Some(&self.sun_path()[1..])
} else {
// unnamed or filesystem path
None
}
}
}

impl PartialEq for UnixAddr {
Expand Down
22 changes: 22 additions & 0 deletions test/sys/test_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ pub fn test_path_to_sock_addr() {
assert_eq!(addr.path(), Some(actual));
}

// Test getting/setting abstract addresses (without unix socket creation)
#[cfg(target_os = "linux")]
#[test]
pub fn test_abstract_uds_addr() {
let empty = String::new();
let addr = UnixAddr::new_abstract(empty.as_bytes()).unwrap();
assert_eq!(addr.as_abstract(), Some(empty.as_bytes()));

let name = String::from("nix\0abstract\0test");
let addr = UnixAddr::new_abstract(name.as_bytes()).unwrap();
assert_eq!(addr.as_abstract(), Some(name.as_bytes()));
assert_eq!(addr.path(), None);

// Internally, name is null-prefixed (abstract namespace)
let internal: &[u8] = unsafe {
slice::from_raw_parts(addr.0.sun_path.as_ptr() as *const u8, addr.1)
};
let mut abstract_name = name.clone();
abstract_name.insert(0, '\0');
assert_eq!(internal, abstract_name.as_bytes());
}

#[test]
pub fn test_getsockname() {
use nix::sys::socket::{socket, AddressFamily, SockType, SockFlag};
Expand Down