net: add handling for abstract socket name (#6772)

This commit is contained in:
Motoyuki Kimura 2024-08-16 23:50:34 +09:00 committed by GitHub
parent f602eae499
commit b2ea40bb54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 4 deletions

View File

@ -3,8 +3,14 @@ use crate::net::unix::{SocketAddr, UnixStream};
use std::fmt;
use std::io;
#[cfg(target_os = "android")]
use std::os::android::net::SocketAddrExt;
#[cfg(target_os = "linux")]
use std::os::linux::net::SocketAddrExt;
#[cfg(any(target_os = "linux", target_os = "android"))]
use std::os::unix::ffi::OsStrExt;
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use std::os::unix::net;
use std::os::unix::net::{self, SocketAddr as StdSocketAddr};
use std::path::Path;
use std::task::{Context, Poll};
@ -70,7 +76,20 @@ impl UnixListener {
where
P: AsRef<Path>,
{
let listener = mio::net::UnixListener::bind(path)?;
// For now, we handle abstract socket paths on linux here.
#[cfg(any(target_os = "linux", target_os = "android"))]
let addr = {
let os_str_bytes = path.as_ref().as_os_str().as_bytes();
if os_str_bytes.starts_with(b"\0") {
StdSocketAddr::from_abstract_name(os_str_bytes)?
} else {
StdSocketAddr::from_pathname(path)?
}
};
#[cfg(not(any(target_os = "linux", target_os = "android")))]
let addr = StdSocketAddr::from_pathname(path)?;
let listener = mio::net::UnixListener::bind_addr(&addr)?;
let io = PollEvented::new(listener)?;
Ok(UnixListener { io })
}

View File

@ -8,8 +8,14 @@ use crate::net::unix::SocketAddr;
use std::fmt;
use std::io::{self, Read, Write};
use std::net::Shutdown;
#[cfg(target_os = "android")]
use std::os::android::net::SocketAddrExt;
#[cfg(target_os = "linux")]
use std::os::linux::net::SocketAddrExt;
#[cfg(any(target_os = "linux", target_os = "android"))]
use std::os::unix::ffi::OsStrExt;
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, RawFd};
use std::os::unix::net;
use std::os::unix::net::{self, SocketAddr as StdSocketAddr};
use std::path::Path;
use std::pin::Pin;
use std::task::{Context, Poll};
@ -66,7 +72,20 @@ impl UnixStream {
where
P: AsRef<Path>,
{
let stream = mio::net::UnixStream::connect(path)?;
// On linux, abstract socket paths need to be considered.
#[cfg(any(target_os = "linux", target_os = "android"))]
let addr = {
let os_str_bytes = path.as_ref().as_os_str().as_bytes();
if os_str_bytes.starts_with(b"\0") {
StdSocketAddr::from_abstract_name(os_str_bytes)?
} else {
StdSocketAddr::from_pathname(path)?
}
};
#[cfg(not(any(target_os = "linux", target_os = "android")))]
let addr = StdSocketAddr::from_pathname(path)?;
let stream = mio::net::UnixStream::connect_addr(&addr)?;
let stream = UnixStream::new(stream)?;
poll_fn(|cx| stream.io.registration().poll_write_ready(cx)).await?;

View File

@ -409,3 +409,16 @@ async fn epollhup() -> io::Result<()> {
assert_eq!(err.kind(), io::ErrorKind::ConnectionReset);
Ok(())
}
// test for https://github.com/tokio-rs/tokio/issues/6767
#[tokio::test]
#[cfg(any(target_os = "linux", target_os = "android"))]
async fn abstract_socket_name() {
let socket_path = "\0aaa";
let listener = UnixListener::bind(socket_path).unwrap();
let accept = listener.accept();
let connect = UnixStream::connect(&socket_path);
try_join(accept, connect).await.unwrap();
}