Skip to content

Commit cd7a868

Browse files
authored
bugfix: Add support for abstract sockets
In #146, I introduced a bug that prevented abstract sockets from working. I passed the path straight into rustix::net::SocketAddrUnix::new, which fails if it receives an abstract socket. This commit fixes this issue by explicitly checking for abstract sockets. If it sees that the path it's receiving is abstract, it will pass the path's bytes to new_abstract_socket() instead. This should fix the issue that is occurring in dbus2/zbus#517 Signed-off-by: John Nunley <[email protected]>
1 parent ccdb956 commit cd7a868

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

src/lib.rs

+31-5
Original file line numberDiff line numberDiff line change
@@ -1847,12 +1847,10 @@ impl Async<UnixStream> {
18471847
/// # std::io::Result::Ok(()) });
18481848
/// ```
18491849
pub async fn connect<P: AsRef<Path>>(path: P) -> io::Result<Async<UnixStream>> {
1850+
let address = convert_path_to_socket_address(path.as_ref())?;
1851+
18501852
// Begin async connect.
1851-
let socket = connect(
1852-
rn::SocketAddrUnix::new(path.as_ref())?.into(),
1853-
rn::AddressFamily::UNIX,
1854-
None,
1855-
)?;
1853+
let socket = connect(address.into(), rn::AddressFamily::UNIX, None)?;
18561854
// Use new_nonblocking because connect already sets socket to non-blocking mode.
18571855
let stream = Async::new_nonblocking(UnixStream::from(socket))?;
18581856

@@ -2195,3 +2193,31 @@ fn set_nonblocking(
21952193

21962194
Ok(())
21972195
}
2196+
2197+
/// Converts a `Path` to its socket address representation.
2198+
///
2199+
/// This function is abstract socket-aware.
2200+
#[cfg(unix)]
2201+
#[inline]
2202+
fn convert_path_to_socket_address(path: &Path) -> io::Result<rn::SocketAddrUnix> {
2203+
// SocketAddrUnix::new() will throw EINVAL when a path with a zero in it is passed in.
2204+
// However, some users expect to be able to pass in paths to abstract sockets, which
2205+
// triggers this error as it has a zero in it. Therefore, if a path starts with a zero,
2206+
// make it an abstract socket.
2207+
#[cfg(any(target_os = "linux", target_os = "android"))]
2208+
let address = {
2209+
use std::os::unix::ffi::OsStrExt;
2210+
2211+
let path = path.as_os_str();
2212+
match path.as_bytes().first() {
2213+
Some(0) => rn::SocketAddrUnix::new_abstract_name(path.as_bytes().get(1..).unwrap())?,
2214+
_ => rn::SocketAddrUnix::new(path)?,
2215+
}
2216+
};
2217+
2218+
// Only Linux and Android support abstract sockets.
2219+
#[cfg(not(any(target_os = "linux", target_os = "android")))]
2220+
let address = rn::SocketAddrUnix::new(path)?;
2221+
2222+
Ok(address)
2223+
}

tests/async.rs

+56
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,59 @@ fn duplicate_socket_insert() -> io::Result<()> {
383383
Ok(())
384384
})
385385
}
386+
387+
#[cfg(any(target_os = "linux", target_os = "android"))]
388+
#[test]
389+
fn abstract_socket() -> io::Result<()> {
390+
use std::ffi::OsStr;
391+
#[cfg(target_os = "android")]
392+
use std::os::android::net::SocketAddrExt;
393+
#[cfg(target_os = "linux")]
394+
use std::os::linux::net::SocketAddrExt;
395+
use std::os::unix::ffi::OsStrExt;
396+
use std::os::unix::net::{SocketAddr, UnixListener, UnixStream};
397+
398+
future::block_on(async {
399+
// Bind a listener to a socket.
400+
let path = OsStr::from_bytes(b"\0smolabstract");
401+
let addr = SocketAddr::from_abstract_name(b"smolabstract")?;
402+
let listener = Async::new(UnixListener::bind_addr(&addr)?)?;
403+
404+
// Future that connects to the listener.
405+
let connector = async {
406+
// Connect to the socket.
407+
let mut stream = Async::<UnixStream>::connect(path).await?;
408+
409+
// Write some bytes to the stream.
410+
stream.write_all(LOREM_IPSUM).await?;
411+
412+
// Read some bytes from the stream.
413+
let mut buf = vec![0; LOREM_IPSUM.len()];
414+
stream.read_exact(&mut buf).await?;
415+
assert_eq!(buf.as_slice(), LOREM_IPSUM);
416+
417+
io::Result::Ok(())
418+
};
419+
420+
// Future that drives the listener.
421+
let driver = async {
422+
// Wait for a new connection.
423+
let (mut stream, _) = listener.accept().await?;
424+
425+
// Read some bytes from the stream.
426+
let mut buf = vec![0; LOREM_IPSUM.len()];
427+
stream.read_exact(&mut buf).await?;
428+
assert_eq!(buf.as_slice(), LOREM_IPSUM);
429+
430+
// Write some bytes to the stream.
431+
stream.write_all(LOREM_IPSUM).await?;
432+
433+
io::Result::Ok(())
434+
};
435+
436+
// Run both in parallel.
437+
future::try_zip(connector, driver).await?;
438+
439+
Ok(())
440+
})
441+
}

0 commit comments

Comments
 (0)