Apply pseudo `Path` to sockets

This commit is contained in:
Wang Siyuan 2025-12-26 10:47:53 +00:00
parent 8e993e3400
commit 647b7dfc77
8 changed files with 57 additions and 62 deletions

View File

@ -136,6 +136,20 @@ impl SockFs {
PseudoFs::singleton(&SOCKFS, "sockfs", SOCKFS_MAGIC)
}
/// Creates a pseudo `Path` for a socket.
pub fn new_path() -> Path {
let socket_inode = Arc::new(Self::singleton().alloc_inode(
InodeType::Socket,
mkmod!(a+rwx),
Uid::new_root(),
Gid::new_root(),
));
Path::new_pseudo(Self::mount_node().clone(), socket_inode, |inode| {
format!("socket:[{}]", inode.ino())
})
}
/// Returns the pseudo mount node of the socket file system.
pub fn mount_node() -> &'static Arc<Mount> {
static SOCKFS_MOUNT: Once<Arc<Mount>> = Once::new();

View File

@ -9,13 +9,12 @@ use unbound::{BindOptions, UnboundDatagram};
use super::addr::UNSPECIFIED_LOCAL_ENDPOINT;
use crate::{
events::IoEvents,
fs::utils::Inode,
fs::{path::Path, pseudofs::SockFs},
net::{
iface::is_broadcast_endpoint,
socket::{
Socket,
ip::options::{IpOptionSet, SetIpLevelOption},
new_pseudo_inode,
options::{Error as SocketError, SocketOption, macros::sock_option_mut},
private::SocketPrivate,
util::{
@ -41,7 +40,7 @@ pub struct DatagramSocket {
is_nonblocking: AtomicBool,
pollee: Pollee,
pseudo_inode: Arc<dyn Inode>,
pseudo_path: Path,
}
#[derive(Debug, Clone)]
@ -67,7 +66,7 @@ impl DatagramSocket {
options: RwLock::new(OptionSet::new()),
is_nonblocking: AtomicBool::new(is_nonblocking),
pollee: Pollee::new(),
pseudo_inode: new_pseudo_inode(),
pseudo_path: SockFs::new_path(),
})
}
@ -293,8 +292,8 @@ impl Socket for DatagramSocket {
Ok(())
}
fn pseudo_inode(&self) -> &Arc<dyn Inode> {
&self.pseudo_inode
fn pseudo_path(&self) -> &Path {
&self.pseudo_path
}
}

View File

@ -25,11 +25,11 @@ use super::{
};
use crate::{
events::IoEvents,
fs::{file_handle::FileLike, utils::Inode},
fs::{file_handle::FileLike, path::Path, pseudofs::SockFs},
net::{
iface::Iface,
socket::{
Socket, new_pseudo_inode,
Socket,
options::{
Error as SocketError, SocketOption,
macros::{sock_option_mut, sock_option_ref},
@ -63,7 +63,7 @@ pub struct StreamSocket {
is_nonblocking: AtomicBool,
pollee: Pollee,
pseudo_inode: Arc<dyn Inode>,
pseudo_path: Path,
}
enum State {
@ -108,7 +108,7 @@ impl StreamSocket {
options: RwLock::new(OptionSet::new()),
is_nonblocking: AtomicBool::new(is_nonblocking),
pollee: Pollee::new(),
pseudo_inode: new_pseudo_inode(),
pseudo_path: SockFs::new_path(),
})
}
@ -137,7 +137,7 @@ impl StreamSocket {
state: RwLock::new(Takeable::new(State::Connected(connected_stream))),
is_nonblocking: AtomicBool::new(false),
pollee,
pseudo_inode: new_pseudo_inode(),
pseudo_path: SockFs::new_path(),
})
}
@ -706,8 +706,8 @@ impl Socket for StreamSocket {
Ok(())
}
fn pseudo_inode(&self) -> &Arc<dyn Inode> {
&self.pseudo_inode
fn pseudo_path(&self) -> &Path {
&self.pseudo_path
}
}

View File

@ -9,12 +9,11 @@ use crate::{
fs::{
file_handle::FileLike,
file_table::FdFlags,
path::RESERVED_MOUNT_ID,
path::Path,
pseudofs::SockFs,
utils::{CreationFlags, Inode, InodeType, StatusFlags, mkmod},
utils::{CreationFlags, Inode, StatusFlags},
},
prelude::*,
process::{Gid, Uid},
util::{MultiRead, MultiWrite},
};
@ -128,8 +127,8 @@ pub trait Socket: private::SocketPrivate + Send + Sync {
flags: SendRecvFlags,
) -> Result<(usize, MessageHeader)>;
/// Returns a reference to the pseudo inode associated with this socket.
fn pseudo_inode(&self) -> &Arc<dyn Inode>;
/// Returns a reference to the pseudo path associated with this socket.
fn pseudo_path(&self) -> &Path;
}
impl<T: Socket + 'static> FileLike for T {
@ -177,7 +176,7 @@ impl<T: Socket + 'static> FileLike for T {
}
fn inode(&self) -> &Arc<dyn Inode> {
self.pseudo_inode()
self.pseudo_path().inode()
}
fn dump_proc_fdinfo(self: Arc<Self>, fd_flags: FdFlags) -> Box<dyn Display> {
@ -190,8 +189,7 @@ impl<T: Socket + 'static> FileLike for T {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
writeln!(f, "pos:\t{}", 0)?;
writeln!(f, "flags:\t0{:o}", self.flags)?;
// TODO: This should be the mount ID of the pseudo filesystem.
writeln!(f, "mnt_id:\t{}", RESERVED_MOUNT_ID)?;
writeln!(f, "mnt_id:\t{}", SockFs::mount_node().id())?;
writeln!(f, "ino:\t{}", self.ino)
}
}
@ -207,15 +205,3 @@ impl<T: Socket + 'static> FileLike for T {
})
}
}
/// Creates a new pseudo inode for a socket.
fn new_pseudo_inode() -> Arc<dyn Inode> {
let pseudo_inode = SockFs::singleton().alloc_inode(
InodeType::Socket,
mkmod!(a+rwx),
Uid::new_root(),
Gid::new_root(),
);
Arc::new(pseudo_inode)
}

View File

@ -8,11 +8,10 @@ use unbound::UnboundNetlink;
use super::{GroupIdSet, NetlinkSocketAddr};
use crate::{
events::IoEvents,
fs::utils::Inode,
fs::{path::Path, pseudofs::SockFs},
net::socket::{
Socket,
netlink::{AddMembership, DropMembership, table::SupportedNetlinkProtocol},
new_pseudo_inode,
options::{
Error as SocketError, SocketOption,
macros::{sock_option_mut, sock_option_ref},
@ -38,7 +37,7 @@ pub struct NetlinkSocket<P: SupportedNetlinkProtocol> {
is_nonblocking: AtomicBool,
pollee: Pollee,
pseudo_inode: Arc<dyn Inode>,
pseudo_path: Path,
}
#[derive(Debug, Clone)]
@ -65,7 +64,7 @@ where
options: RwLock::new(OptionSet::new()),
is_nonblocking: AtomicBool::new(is_nonblocking),
pollee: Pollee::new(),
pseudo_inode: new_pseudo_inode(),
pseudo_path: SockFs::new_path(),
})
}
@ -222,8 +221,8 @@ where
do_netlink_setsockopt(option, &mut inner)
}
fn pseudo_inode(&self) -> &Arc<dyn Inode> {
&self.pseudo_inode
fn pseudo_path(&self) -> &Path {
&self.pseudo_path
}
}

View File

@ -5,9 +5,9 @@ use core::sync::atomic::{AtomicBool, Ordering};
use super::message::{MessageQueue, MessageReceiver};
use crate::{
events::IoEvents,
fs::utils::Inode,
fs::{path::Path, pseudofs::SockFs},
net::socket::{
Socket, new_pseudo_inode,
Socket,
options::{Error as SocketError, SocketOption, macros::sock_option_mut},
private::SocketPrivate,
unix::{UnixSocketAddr, ctrl_msg::AuxiliaryData},
@ -28,7 +28,7 @@ pub struct UnixDatagramSocket {
is_nonblocking: AtomicBool,
is_write_shutdown: AtomicBool,
pseudo_inode: Arc<dyn Inode>,
pseudo_path: Path,
}
#[derive(Clone, Debug)]
@ -69,7 +69,7 @@ impl UnixDatagramSocket {
options: RwLock::new(OptionSet::new()),
is_nonblocking: AtomicBool::new(is_nonblocking),
is_write_shutdown: AtomicBool::new(false),
pseudo_inode: new_pseudo_inode(),
pseudo_path: SockFs::new_path(),
}
}
@ -281,8 +281,8 @@ impl Socket for UnixDatagramSocket {
Ok((received_bytes, message_header))
}
fn pseudo_inode(&self) -> &Arc<dyn Inode> {
&self.pseudo_inode
fn pseudo_path(&self) -> &Path {
&self.pseudo_path
}
}

View File

@ -12,12 +12,9 @@ use super::{
};
use crate::{
events::IoEvents,
fs::{
file_handle::FileLike,
utils::{EndpointState, Inode},
},
fs::{file_handle::FileLike, path::Path, pseudofs::SockFs, utils::EndpointState},
net::socket::{
Socket, new_pseudo_inode,
Socket,
options::{
Error as SocketError, PeerCred, PeerGroups, SocketOption, macros::sock_option_mut,
},
@ -45,7 +42,7 @@ pub struct UnixStreamSocket {
is_nonblocking: AtomicBool,
is_seqpacket: bool,
pseudo_inode: Arc<dyn Inode>,
pseudo_path: Path,
}
enum State {
@ -148,7 +145,7 @@ impl UnixStreamSocket {
pollee: Pollee::new(),
is_nonblocking: AtomicBool::new(is_nonblocking),
is_seqpacket,
pseudo_inode: new_pseudo_inode(),
pseudo_path: SockFs::new_path(),
})
}
@ -184,7 +181,7 @@ impl UnixStreamSocket {
pollee: cloned_pollee,
is_nonblocking: AtomicBool::new(is_nonblocking),
is_seqpacket,
pseudo_inode: new_pseudo_inode(),
pseudo_path: SockFs::new_path(),
})
}
@ -490,8 +487,8 @@ impl Socket for UnixStreamSocket {
Ok((received_bytes, message_header))
}
fn pseudo_inode(&self) -> &Arc<dyn Inode> {
&self.pseudo_inode
fn pseudo_path(&self) -> &Path {
&self.pseudo_path
}
}

View File

@ -5,9 +5,9 @@ use core::sync::atomic::{AtomicBool, Ordering};
use super::{connected::Connected, connecting::Connecting, init::Init, listen::Listen};
use crate::{
events::IoEvents,
fs::{file_handle::FileLike, utils::Inode},
fs::{file_handle::FileLike, path::Path, pseudofs::SockFs},
net::socket::{
Socket, new_pseudo_inode,
Socket,
private::SocketPrivate,
util::{MessageHeader, SendRecvFlags, SockShutdownCmd, SocketAddr},
vsock::{VSOCK_GLOBAL, addr::VsockSocketAddr},
@ -20,7 +20,7 @@ use crate::{
pub struct VsockStreamSocket {
status: RwLock<Status>,
is_nonblocking: AtomicBool,
pseudo_inode: Arc<dyn Inode>,
pseudo_path: Path,
}
pub enum Status {
@ -42,7 +42,7 @@ impl VsockStreamSocket {
Ok(Self {
status: RwLock::new(Status::Init(init)),
is_nonblocking: AtomicBool::new(nonblocking),
pseudo_inode: new_pseudo_inode(),
pseudo_path: SockFs::new_path(),
})
}
@ -50,7 +50,7 @@ impl VsockStreamSocket {
Self {
status: RwLock::new(Status::Connected(connected)),
is_nonblocking: AtomicBool::new(false),
pseudo_inode: new_pseudo_inode(),
pseudo_path: SockFs::new_path(),
}
}
@ -310,8 +310,8 @@ impl Socket for VsockStreamSocket {
}
}
fn pseudo_inode(&self) -> &Arc<dyn Inode> {
&self.pseudo_inode
fn pseudo_path(&self) -> &Path {
&self.pseudo_path
}
}