Record the original bound name

This commit is contained in:
Ruihan Li
2024-07-27 00:13:25 +08:00
committed by Tate, Hongliang Tian
parent 6b50d28ba1
commit f499f54cf5
6 changed files with 22 additions and 25 deletions

View File

@ -5,14 +5,14 @@ use crate::{fs::path::Dentry, net::socket::util::socket_addr::SocketAddr, prelud
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum UnixSocketAddr { pub enum UnixSocketAddr {
Unnamed, Unnamed,
Path(String), Path(Arc<str>),
Abstract(Vec<u8>), Abstract(Arc<[u8]>),
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub(super) enum UnixSocketAddrBound { pub(super) enum UnixSocketAddrBound {
Path(Arc<Dentry>), Path(Arc<str>, Arc<Dentry>),
Abstract(Vec<u8>), Abstract(Arc<[u8]>),
} }
impl TryFrom<SocketAddr> for UnixSocketAddr { impl TryFrom<SocketAddr> for UnixSocketAddr {
@ -29,10 +29,7 @@ impl TryFrom<SocketAddr> for UnixSocketAddr {
impl From<UnixSocketAddrBound> for UnixSocketAddr { impl From<UnixSocketAddrBound> for UnixSocketAddr {
fn from(value: UnixSocketAddrBound) -> Self { fn from(value: UnixSocketAddrBound) -> Self {
match value { match value {
UnixSocketAddrBound::Path(dentry) => { UnixSocketAddrBound::Path(path, _) => Self::Path(path),
let abs_path = dentry.abs_path();
Self::Path(abs_path)
}
UnixSocketAddrBound::Abstract(name) => Self::Abstract(name), UnixSocketAddrBound::Abstract(name) => Self::Abstract(name),
} }
} }

View File

@ -26,7 +26,7 @@ impl Init {
} }
} }
pub(super) fn bind(&mut self, addr_to_bind: &UnixSocketAddr) -> Result<()> { pub(super) fn bind(&mut self, addr_to_bind: UnixSocketAddr) -> Result<()> {
if self.addr.is_some() { if self.addr.is_some() {
return_errno_with_message!(Errno::EINVAL, "the socket is already bound"); return_errno_with_message!(Errno::EINVAL, "the socket is already bound");
} }
@ -35,8 +35,8 @@ impl Init {
UnixSocketAddr::Unnamed => todo!(), UnixSocketAddr::Unnamed => todo!(),
UnixSocketAddr::Abstract(_) => todo!(), UnixSocketAddr::Abstract(_) => todo!(),
UnixSocketAddr::Path(path) => { UnixSocketAddr::Path(path) => {
let dentry = create_socket_file(path)?; let dentry = create_socket_file(&path)?;
UnixSocketAddrBound::Path(dentry) UnixSocketAddrBound::Path(path, dentry)
} }
}; };
self.addr = Some(bound_addr); self.addr = Some(bound_addr);

View File

@ -82,7 +82,7 @@ impl BacklogTable {
fn add_backlog(&self, addr: &UnixSocketAddrBound, backlog: usize) -> Result<()> { fn add_backlog(&self, addr: &UnixSocketAddrBound, backlog: usize) -> Result<()> {
let inode = { let inode = {
let UnixSocketAddrBound::Path(dentry) = addr else { let UnixSocketAddrBound::Path(_, dentry) = addr else {
todo!() todo!()
}; };
create_keyable_inode(dentry) create_keyable_inode(dentry)
@ -99,7 +99,7 @@ impl BacklogTable {
fn get_backlog(&self, addr: &UnixSocketAddrBound) -> Result<Arc<Backlog>> { fn get_backlog(&self, addr: &UnixSocketAddrBound) -> Result<Arc<Backlog>> {
let inode = { let inode = {
let UnixSocketAddrBound::Path(dentry) = addr else { let UnixSocketAddrBound::Path(_, dentry) = addr else {
todo!() todo!()
}; };
create_keyable_inode(dentry) create_keyable_inode(dentry)
@ -134,7 +134,7 @@ impl BacklogTable {
} }
fn remove_backlog(&self, addr: &UnixSocketAddrBound) { fn remove_backlog(&self, addr: &UnixSocketAddrBound) {
let UnixSocketAddrBound::Path(dentry) = addr else { let UnixSocketAddrBound::Path(_, dentry) = addr else {
todo!() todo!()
}; };

View File

@ -198,7 +198,7 @@ impl Socket for UnixStreamSocket {
let addr = UnixSocketAddr::try_from(socket_addr)?; let addr = UnixSocketAddr::try_from(socket_addr)?;
match &mut *self.state.write() { match &mut *self.state.write() {
State::Init(init) => init.bind(&addr), State::Init(init) => init.bind(addr),
_ => return_errno_with_message!( _ => return_errno_with_message!(
Errno::EINVAL, Errno::EINVAL,
"cannot bind a listening or connected socket" "cannot bind a listening or connected socket"
@ -217,7 +217,7 @@ impl Socket for UnixStreamSocket {
} }
UnixSocketAddr::Path(path) => { UnixSocketAddr::Path(path) => {
let dentry = lookup_socket_file(&path)?; let dentry = lookup_socket_file(&path)?;
UnixSocketAddrBound::Path(dentry) UnixSocketAddrBound::Path(path, dentry)
} }
} }
}; };

View File

@ -91,16 +91,16 @@ pub(super) fn from_c_bytes(bytes: &[u8]) -> Result<UnixSocketAddr> {
} }
if sun_path[0] == 0 { if sun_path[0] == 0 {
return Ok(UnixSocketAddr::Abstract(Vec::from(&sun_path[1..]))); return Ok(UnixSocketAddr::Abstract(Arc::from(&sun_path[1..])));
} }
// Again, Linux always appends a null terminator to the pathname if none is supplied. So we // Again, Linux always appends a null terminator to the pathname if none is supplied. So we
// need to deal with the case where `CStr::from_bytes_until_nul` fails. // need to deal with the case where `CStr::from_bytes_until_nul` fails.
if let Ok(c_str) = CStr::from_bytes_until_nul(sun_path) { if let Ok(c_str) = CStr::from_bytes_until_nul(sun_path) {
Ok(UnixSocketAddr::Path(c_str.to_string_lossy().to_string())) Ok(UnixSocketAddr::Path(Arc::from(c_str.to_string_lossy())))
} else { } else {
Ok(UnixSocketAddr::Path( Ok(UnixSocketAddr::Path(Arc::from(String::from_utf8_lossy(
String::from_utf8_lossy(sun_path).to_string(), sun_path,
)) ))))
} }
} }

View File

@ -88,12 +88,12 @@ static int sk_accepted;
#define UNNAMED_ADDRLEN PATH_OFFSET #define UNNAMED_ADDRLEN PATH_OFFSET
#define BOUND_ADDR \ #define BOUND_ADDR \
((struct sockaddr_un){ .sun_family = AF_UNIX, .sun_path = "/tmp/B0" }) ((struct sockaddr_un){ .sun_family = AF_UNIX, .sun_path = "//tmp/B0" })
#define BOUND_ADDRLEN (PATH_OFFSET + 8) #define BOUND_ADDRLEN (PATH_OFFSET + 9)
#define LISTEN_ADDR \ #define LISTEN_ADDR \
((struct sockaddr_un){ .sun_family = AF_UNIX, .sun_path = "/tmp/L0" }) ((struct sockaddr_un){ .sun_family = AF_UNIX, .sun_path = "/tmp//L0" })
#define LISTEN_ADDRLEN (PATH_OFFSET + 8) #define LISTEN_ADDRLEN (PATH_OFFSET + 9)
FN_SETUP(unbound) FN_SETUP(unbound)
{ {