LocalSocket

Struct LocalSocket 

Source
pub struct LocalSocket {
Show 16 fields socket_type: SocketType, self_weak: RwLock<Weak<LocalSocket>>, protocol: SocketProtocol, state: RwLock<SocketState>, local_addr: RwLock<Option<String>>, peer_addr: RwLock<Option<String>>, read_buffer: RwLock<Arc<SocketBuffer>>, peer_read_buffer: RwLock<Option<Arc<SocketBuffer>>>, peer_socket: RwLock<Option<Weak<LocalSocket>>>, backlog: RwLock<Vec<Arc<LocalSocket>>>, max_backlog: RwLock<usize>, accept_waker: Waker, read_waker: Waker, handle_waker: Waker, handle_queue: RwLock<VecDeque<KernelObject>>, nonblocking: RwLock<bool>,
}
Expand description

Local Socket Implementation

This socket type provides local (Unix-like) domain socket functionality. It uses VecDeque buffers internally for data transfer and integrates with the NetworkManager for socket registry.

Fields§

§socket_type: SocketType

Socket type (Stream, Datagram, etc.)

§self_weak: RwLock<Weak<LocalSocket>>

Weak self reference (initialized when wrapped in Arc)

This is used to establish peer relationships in methods that only have &self (e.g., connect()), where we still need an Arc<Self>.

§protocol: SocketProtocol

Socket protocol

§state: RwLock<SocketState>

Current socket state

§local_addr: RwLock<Option<String>>

Local address (if bound)

§peer_addr: RwLock<Option<String>>

Peer address (if connected)

§read_buffer: RwLock<Arc<SocketBuffer>>

Read buffer: data received from peer (shared with peer for writing)

§peer_read_buffer: RwLock<Option<Arc<SocketBuffer>>>

Write buffer reference: shared with peer socket for writing When we write, we push to peer’s read_buffer

§peer_socket: RwLock<Option<Weak<LocalSocket>>>

Peer socket reference (for waking read waiters)

§backlog: RwLock<Vec<Arc<LocalSocket>>>

Backlog queue for listening sockets Contains pending connections waiting to be accepted

§max_backlog: RwLock<usize>

Maximum backlog size (set by listen())

§accept_waker: Waker

Waker for blocking accept() operations

§read_waker: Waker

Waker for blocking read() operations

§handle_waker: Waker

Waker for blocking recv_handle() operations

§handle_queue: RwLock<VecDeque<KernelObject>>

Queue of handles (KernelObjects) received from peer This allows passing file descriptors / kernel objects between tasks

§nonblocking: RwLock<bool>

Nonblocking I/O flag

Implementations§

Source§

impl LocalSocket

Source

pub(crate) fn init_self_weak(this: &Arc<Self>)

Source

pub fn from_socket_object(socket: &Arc<dyn SocketObject>) -> Option<&Self>

Safely downcast a SocketObject to LocalSocket using Any trait

Returns None if the socket is not a LocalSocket. This is completely safe and does not use any unsafe code.

Source

pub fn new(socket_type: SocketType, protocol: SocketProtocol) -> Self

Create a new local socket

§Arguments
  • socket_type - Socket type (Stream, Datagram, etc.)
  • protocol - Socket protocol
§Returns

A new socket in the Unconnected state

Source

pub fn send_handle(&self, object: KernelObject) -> Result<(), IpcError>

Send a KernelObject handle through this socket

This is LocalSocket-only (SCM_RIGHTS equivalent) and uses dup() semantics.

Source

pub fn send_handle_and_data( &self, object: KernelObject, data: &[u8], ) -> Result<(), IpcError>

Send a handle and data together atomically for Wayland protocol

This method ensures that both the handle and data are available before waking the peer, preventing race conditions where recvmsg might get the handle but not the data (or vice versa).

This is needed for Wayland protocol messages with file descriptors, where the client expects both the FD and message data in a single recvmsg call.

Source

pub fn recv_handle_and_data( &self, max_data_len: usize, ) -> Result<(KernelObject, Vec<u8>), IpcError>

Receive a handle and data together atomically for Wayland protocol

Returns both a handle and data in a single atomic operation. This is the counterpart to send_handle_and_data().

§Arguments
  • max_data_len - Maximum amount of data to read
§Returns
  • (KernelObject, Vec<u8>) - Handle and data on success
  • IpcError - Error if no handle/data available or other error
Source

pub fn recv_handle(&self) -> Result<KernelObject, IpcError>

Receive a KernelObject handle from this socket (non-blocking)

Source

pub fn accept_blocking( &self, task_id: usize, trapframe: &mut Trapframe, ) -> Result<Arc<dyn SocketObject>, SocketError>

Accept a connection with blocking behavior

This method blocks the calling task until a connection is available in the backlog. It uses the waker mechanism to properly suspend and wake the task.

§Arguments
  • task_id - ID of the task calling accept
  • trapframe - Trapframe for scheduler context switching
§Returns

Arc to the accepted socket connection

Source

pub fn create_connected_pair( local_addr: String, peer_addr: String, ) -> (Arc<Self>, Arc<Self>)

Create a connected socket pair (for internal use)

This creates two connected sockets, useful for accept() implementation.

§Arguments
  • local_addr - Local address for the first socket
  • peer_addr - Peer address for the second socket
§Returns

A tuple of (local_socket, peer_socket) that are connected

Source

pub fn recv_handle_blocking( &self, task_id: usize, trapframe: &mut Trapframe, ) -> Result<KernelObject, IpcError>

Blocking handle receive operation

This method blocks the calling task until a handle is available in the handle queue, or the peer is closed.

Trait Implementations§

Source§

impl ControlOps for LocalSocket

Source§

fn control(&self, command: u32, arg: usize) -> Result<i32, &'static str>

Perform a control operation Read more
Source§

fn supported_control_commands(&self) -> Vec<(u32, &'static str)>

Get a list of supported control commands Read more
Source§

impl Selectable for LocalSocket

Source§

fn current_ready(&self, interest: ReadyInterest) -> ReadySet

Return current readiness for the given interest set.
Source§

fn wait_until_ready( &self, interest: ReadyInterest, trapframe: &mut Trapframe, timeout_ticks: Option<u64>, ) -> SelectWaitOutcome

Block the current task using the provided trapframe until the interest becomes ready or the optional timeout (in ticks) expires. Read more
Source§

fn set_nonblocking(&self, enabled: bool)

Enable or disable non-blocking I/O semantics on this object. Read more
Source§

fn is_nonblocking(&self) -> bool

Query whether non-blocking I/O semantics are enabled on this object.
Source§

impl SocketControl for LocalSocket

Source§

fn bind(&self, address: &SocketAddress) -> Result<(), SocketError>

Bind socket to an address
Source§

fn listen(&self, backlog: usize) -> Result<(), SocketError>

Listen for incoming connections (for stream sockets)
Source§

fn accept(&self) -> Result<Arc<dyn SocketObject>, SocketError>

Accept an incoming connection (for listening sockets) Returns a new socket for the accepted connection
Source§

fn connect(&self, address: &SocketAddress) -> Result<(), SocketError>

Connect to a remote address
Source§

fn shutdown(&self, how: ShutdownHow) -> Result<(), SocketError>

Shutdown socket for reading, writing, or both
Source§

fn is_connected(&self) -> bool

Check if socket is connected
Source§

fn state(&self) -> SocketState

Get socket state
Source§

fn getpeername(&self) -> Result<SocketAddress, SocketError>

Get socket peer address
Source§

fn getsockname(&self) -> Result<SocketAddress, SocketError>

Get socket local address
Source§

impl SocketObject for LocalSocket

Source§

fn socket_type(&self) -> SocketType

Get socket type (Stream, Datagram, etc.)
Source§

fn socket_domain(&self) -> SocketDomain

Get socket domain (Local, Inet, Inet6, etc.)
Source§

fn socket_protocol(&self) -> SocketProtocol

Get socket protocol
Source§

fn as_any(&self) -> &dyn Any

Cast to Any for safe downcasting
Source§

fn as_selectable(&self) -> Option<&dyn Selectable>

Optional capability: expose select/pselect readiness/wait interface
Source§

fn as_control_ops(&self) -> Option<&dyn ControlOps>

Optional capability: expose control operations interface
Source§

fn sendto( &self, data: &[u8], address: &SocketAddress, flags: u32, ) -> Result<usize, SocketError>

Send data to a specific address (for datagram sockets) For stream sockets, address is ignored and data is sent to connected peer
Source§

fn recvfrom( &self, buffer: &mut [u8], flags: u32, ) -> Result<(usize, SocketAddress), SocketError>

Receive data with source address (for datagram sockets) For stream sockets, returns Unspecified address
Source§

impl StreamIpcOps for LocalSocket

Source§

fn is_connected(&self) -> bool

Check if the stream IPC object is still connected/valid
Source§

fn peer_count(&self) -> usize

Get the number of active peers (readers/writers/endpoints)
Source§

fn description(&self) -> String

Get a human-readable description of this IPC object
Source§

impl StreamOps for LocalSocket

Source§

fn read(&self, buffer: &mut [u8]) -> Result<usize, StreamError>

Read data from the stream
Source§

fn write(&self, data: &[u8]) -> Result<usize, StreamError>

Write data to the stream

Auto Trait Implementations§

§

impl !Freeze for LocalSocket

§

impl !RefUnwindSafe for LocalSocket

§

impl Send for LocalSocket

§

impl Sync for LocalSocket

§

impl Unpin for LocalSocket

§

impl !UnwindSafe for LocalSocket

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.