pub struct UnixListener { /* private fields */ }local only.Expand description
A Unix domain socket listener. Dropping it removes its socket file, but only while the path still names the file this listener created.
let listener = net_io::UnixListener::bind("/run/myapp/control.sock")?;
let (mut conn, info) = listener.accept()?;
println!("uid {:?}", info.peer_credentials.and_then(|c| c.uid));
conn.send_all(b"ready\n")?;Implementations§
Source§impl UnixListener
impl UnixListener
Sourcepub fn bind<P: AsRef<Path>>(path: P) -> Result<UnixListener, NetError>
pub fn bind<P: AsRef<Path>>(path: P) -> Result<UnixListener, NetError>
Binds path with LocalOptions::new: mode 0600, a stale socket file removed.
§Errors
PathTooLong, StaleSocketInUse, PermissionDenied, NotFound (no such
directory), InvalidConfig { field: "path" } (a NUL byte, a path that
ends in /, ., or .., or reason: InsecureDirectory, see
UnixListener::bind_with), Os.
Sourcepub fn bind_with<P: AsRef<Path>>(
path: P,
options: LocalOptions,
) -> Result<UnixListener, NetError>
pub fn bind_with<P: AsRef<Path>>( path: P, options: LocalOptions, ) -> Result<UnixListener, NetError>
Binds path with options: the socket is bound, given its mode and owner,
and only then listens, so no connection is accepted under the umask’s
permissions.
The mode and owner are never set through a path another user can change. The socket’s directory is opened once and checked:
- writable by every user without the sticky bit: refused with
InvalidConfig { field: "path", reason: InsecureDirectory }, as anyone could swap the socket file for a link to another file; - owned by this user or root, and writable by nobody else (or sticky, as
/tmp): the socket is bound atpathand its mode and owner are set by name relative to the open directory; - otherwise (writable by its group, or owned by another user): the socket
is bound in a new private directory next to
path(.nioand 8 hex digits, mode0700), given its mode and owner there, and linked topathonce it listens; the private directory is then removed. The directory’s path plus 15 bytes must then also fit the platform limit, or the bind fails withPathTooLong.
In the last case the name the OS reports for the socket (getsockname,
and so for a listener adopted from it) is the private one, as with
UnixListener::bind_atomic; UnixListener::path and
ListenerInfo::local are always path.
§Errors
Sourcepub fn bind_atomic<P: AsRef<Path>>(
path: P,
options: LocalOptions,
) -> Result<UnixListener, NetError>
pub fn bind_atomic<P: AsRef<Path>>( path: P, options: LocalOptions, ) -> Result<UnixListener, NetError>
As UnixListener::bind_with, but bound under a temporary name in the
same directory and renamed into place once it listens with its final
mode: the path never names a socket that refuses or is not ready, and a
stale socket at it is replaced in one step. The directory is checked as
for bind_with; where others can change it, the temporary name is the
socket in the private directory, and the rename moves it from there.
§Errors
Sourcepub fn accept(&self) -> Result<(UnixConnection, ConnectionInfo), NetError>
pub fn accept(&self) -> Result<(UnixConnection, ConnectionInfo), NetError>
Blocks until a connection arrives. Peer credentials are read once and
stored in ConnectionInfo; with require_uid, other users’ connections
are closed before any read and accepting continues.
§Errors
Limit { which: Descriptors } when exhaustion persists past one backoff step; Os.
Sourcepub fn accept_timeout(
&self,
timeout: Duration,
) -> Result<Option<(UnixConnection, ConnectionInfo)>, NetError>
pub fn accept_timeout( &self, timeout: Duration, ) -> Result<Option<(UnixConnection, ConnectionInfo)>, NetError>
As UnixListener::accept, but Ok(None) when nothing arrived within timeout.
§Errors
Sourcepub fn info(&self) -> &ListenerInfo
pub fn info(&self) -> &ListenerInfo
Facts about this listener.