Skip to main content

coreshift_core/
unix_socket.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/
4
5//! Low-level Unix domain socket primitives.
6//!
7//! This module exposes Linux/Android `AF_UNIX` stream socket mechanics only:
8//! bind, listen, accept, connect, chmod for filesystem sockets, peer
9//! credentials, and byte I/O through [`Fd`]. Callers own all protocol, message
10//! framing, authentication policy, daemon behavior, and socket naming.
11//!
12//! Abstract socket names are Linux/Android-only. They are encoded with a
13//! leading NUL byte in `sun_path`; interior NUL bytes in the caller-provided
14//! abstract name are preserved because the kernel uses the explicit sockaddr
15//! length, not C string termination.
16
17use crate::CoreError;
18use crate::error::syscall_ret;
19use crate::reactor::Fd;
20use std::io::Error as IoError;
21use std::os::unix::ffi::OsStrExt;
22use std::os::unix::fs::FileTypeExt;
23use std::os::unix::io::AsRawFd;
24use std::path::Path;
25
26#[inline(always)]
27fn errno() -> i32 {
28    IoError::last_os_error().raw_os_error().unwrap_or(0)
29}
30
31/// Owned non-blocking Unix listener descriptor.
32pub struct UnixListenerFd {
33    /// Underlying descriptor for reactor registration and raw byte helpers.
34    pub fd: Fd,
35}
36
37/// Owned non-blocking Unix stream descriptor.
38pub struct UnixStreamFd {
39    /// Underlying descriptor for reactor registration and raw byte helpers.
40    pub fd: Fd,
41}
42
43/// Result of starting a non-blocking Unix stream connection.
44pub enum UnixConnectResult {
45    /// The socket connected immediately.
46    Connected(UnixStreamFd),
47    /// The socket connection is in progress; register for writability and call
48    /// [`UnixStreamFd::finish_connect`] or [`UnixStreamFd::check_connect_error`].
49    InProgress(UnixStreamFd),
50}
51
52/// Unix socket address.
53#[derive(Clone, Copy, Debug)]
54pub enum UnixSocketAddr<'a> {
55    /// Filesystem pathname socket.
56    Path(&'a Path),
57    /// Linux/Android abstract namespace socket name, without the leading NUL.
58    Abstract(&'a [u8]),
59}
60
61/// Explicit stale pathname behavior for filesystem socket binds.
62#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
63pub enum StaleSocketPolicy {
64    /// Preserve any existing path and let `bind` report the conflict.
65    #[default]
66    Preserve,
67    /// Unlink only if the existing path is itself a socket.
68    UnlinkSocketOnly,
69    /// Unlink any existing filesystem path.
70    ///
71    /// This may delete non-socket files and should only be used when the caller
72    /// owns the path namespace.
73    UnlinkAnyPath,
74}
75
76/// Bind options for a Unix stream listener.
77#[derive(Clone, Copy, Debug, Default)]
78pub struct UnixSocketBindOptions {
79    /// Explicit stale pathname handling for filesystem socket binds.
80    pub stale_socket_policy: StaleSocketPolicy,
81    /// Optional filesystem socket path mode applied after a successful bind.
82    pub mode: Option<u32>,
83}
84
85/// Peer process credentials when the platform exposes them.
86#[derive(Debug, Clone, Copy, PartialEq, Eq)]
87pub struct PeerCred {
88    /// Peer process id when available.
89    pub pid: Option<i32>,
90    /// Peer user id.
91    pub uid: u32,
92    /// Peer group id.
93    pub gid: u32,
94}
95
96impl UnixListenerFd {
97    /// Accept one non-blocking client.
98    ///
99    /// ### Fork Safety
100    /// The listener's file descriptor is `O_CLOEXEC` and will be closed in the
101    /// child after `exec`.
102    ///
103    /// ### Errors
104    /// - `EAGAIN`/`EWOULDBLOCK`: No connection is pending.
105    /// - `ECONNABORTED`: A connection was aborted before it could be accepted.
106    /// - `EMFILE`: Process limit on open file descriptors hit.
107    /// - `ENFILE`: System-wide limit on open files hit.
108    ///
109    /// Returns `Ok(None)` if no client is ready.
110    pub fn accept(&self) -> Result<Option<UnixStreamFd>, CoreError> {
111        self.accept_timeout(0)
112    }
113
114    /// Accept a client with a raw timeout in milliseconds.
115    ///
116    /// - `-1`: Block indefinitely until a client connects.
117    /// - `0`: Return immediately (equivalent to [`Self::accept`]).
118    /// - `> 0`: Wait up to the specified milliseconds.
119    ///
120    /// ### Errors
121    /// Returns the same errors as [`Self::accept`], or `poll(2)` errors.
122    ///
123    /// # Example
124    /// ```no_run
125    /// # use coreshift_core::unix_socket::{self, UnixListenerFd, UnixSocketAddr, UnixSocketBindOptions};
126    /// # let listener = unix_socket::bind_unix_listener(UnixSocketAddr::Abstract(b"test"), UnixSocketBindOptions::default()).unwrap();
127    /// let stream = listener.accept_timeout(1000).unwrap();
128    /// ```
129    pub fn accept_timeout(&self, timeout_ms: i32) -> Result<Option<UnixStreamFd>, CoreError> {
130        if timeout_ms != 0 {
131            let mut pollfd = libc::pollfd {
132                fd: self.fd.as_raw_fd(),
133                events: libc::POLLIN,
134                revents: 0,
135            };
136            let ret = unsafe { libc::poll(&mut pollfd, 1, timeout_ms) };
137            if ret < 0 {
138                let e = errno();
139                if e == libc::EINTR {
140                    return Ok(None);
141                }
142                return Err(CoreError::sys(e, "poll(accept)"));
143            }
144            if ret == 0 {
145                return Ok(None);
146            }
147        }
148
149        loop {
150            let fd = unsafe {
151                libc::accept4(
152                    self.fd.as_raw_fd(),
153                    std::ptr::null_mut(),
154                    std::ptr::null_mut(),
155                    libc::SOCK_CLOEXEC | libc::SOCK_NONBLOCK,
156                )
157            };
158            if fd >= 0 {
159                return Ok(Some(UnixStreamFd {
160                    fd: Fd::new(fd, "accept4")?,
161                }));
162            }
163
164            let e = errno();
165            if e == libc::EINTR {
166                continue;
167            }
168            if e == libc::EAGAIN || e == libc::EWOULDBLOCK {
169                return Ok(None);
170            }
171            return Err(CoreError::sys(e, "accept4"));
172        }
173    }
174}
175
176impl UnixStreamFd {
177    /// Return peer credentials when the platform supports `SO_PEERCRED`.
178    ///
179    /// ### Errors
180    /// - `EBADF`: The file descriptor is invalid.
181    /// - `ENOPROTOOPT`: `SO_PEERCRED` is not supported by the socket.
182    ///
183    /// # Example
184    /// ```no_run
185    /// # use coreshift_core::unix_socket::UnixStreamFd;
186    /// # fn example(stream: UnixStreamFd) {
187    /// let creds = stream.peer_cred().unwrap();
188    /// if let Some(c) = creds {
189    ///     println!("Peer UID: {}", c.uid);
190    /// }
191    /// # }
192    /// ```
193    pub fn peer_cred(&self) -> Result<Option<PeerCred>, CoreError> {
194        peer_cred_raw(&self.fd)
195    }
196
197    /// Return the pending `SO_ERROR` connect status.
198    ///
199    /// `Ok(None)` means no pending socket error was reported. `Ok(Some(code))`
200    /// returns the raw connect error without making a policy decision.
201    ///
202    /// ### Errors
203    /// - `EBADF`: The file descriptor is invalid.
204    pub fn check_connect_error(&self) -> Result<Option<i32>, CoreError> {
205        let mut code: libc::c_int = 0;
206        let mut len = std::mem::size_of::<libc::c_int>() as libc::socklen_t;
207        let ret = unsafe {
208            libc::getsockopt(
209                self.fd.as_raw_fd(),
210                libc::SOL_SOCKET,
211                libc::SO_ERROR,
212                (&mut code as *mut libc::c_int).cast(),
213                &mut len,
214            )
215        };
216        syscall_ret(ret, "getsockopt(SO_ERROR)")?;
217        if code == 0 { Ok(None) } else { Ok(Some(code)) }
218    }
219
220    /// Finish a non-blocking connect after the socket becomes writable.
221    ///
222    /// Returns the stream when `SO_ERROR` is clear; otherwise returns the raw
223    /// socket error as [`CoreError`].
224    ///
225    /// ### Errors
226    /// Returns the same errors as [`Self::check_connect_error`], or the
227    /// pending connection error itself.
228    pub fn finish_connect(self) -> Result<Self, CoreError> {
229        match self.check_connect_error()? {
230            None => Ok(self),
231            Some(code) => Err(CoreError::sys(code, "connect(SO_ERROR)")),
232        }
233    }
234}
235
236/// Bind and listen on a non-blocking Unix stream socket.
237/// Bind a new Unix domain stream listener.
238///
239/// The socket is created with `SOCK_CLOEXEC` set.
240///
241/// ### Fork Safety
242/// The socket is `O_CLOEXEC` and will be closed in the child after `exec`.
243///
244/// ### Errors
245/// - `EACCES`: Permission denied for a component of the path.
246/// - `EADDRINUSE`: The address is already in use.
247/// - `EINVAL`: Invalid address.
248/// - `ELOOP`: Too many symbolic links encountered.
249/// - `ENAMETOOLONG`: Path is too long.
250/// - `ENOENT`: A component of the path prefix does not exist.
251pub fn bind_unix_listener(
252    addr: UnixSocketAddr<'_>,
253    opts: UnixSocketBindOptions,
254) -> Result<UnixListenerFd, CoreError> {
255    let encoded = UnixSockAddr::new(addr, "unix bind address")?;
256
257    match addr {
258        UnixSocketAddr::Path(path) => {
259            apply_stale_socket_policy(path, opts.stale_socket_policy)?;
260        }
261        UnixSocketAddr::Abstract(_) => {
262            if opts.stale_socket_policy != StaleSocketPolicy::Preserve || opts.mode.is_some() {
263                return Err(CoreError::sys(libc::EINVAL, "abstract unix bind options"));
264            }
265        }
266    }
267
268    let fd = new_unix_stream_socket()?;
269    let ret = unsafe { libc::bind(fd.as_raw_fd(), encoded.as_ptr(), encoded.len()) };
270    syscall_ret(ret, "bind")?;
271
272    if let (UnixSocketAddr::Path(path), Some(mode)) = (addr, opts.mode) {
273        if let Err(err) = chmod_unix_socket(UnixSocketAddr::Path(path), mode) {
274            cleanup_created_path(addr);
275            return Err(err);
276        }
277    }
278
279    let ret = unsafe { libc::listen(fd.as_raw_fd(), libc::SOMAXCONN) };
280    if let Err(err) = syscall_ret(ret, "listen") {
281        cleanup_created_path(addr);
282        return Err(err);
283    }
284
285    Ok(UnixListenerFd { fd })
286}
287
288/// Connect a non-blocking Unix stream socket.
289///
290/// ### Fork Safety
291/// The socket is `O_CLOEXEC` and will be closed in the child after `exec`.
292///
293/// ### Errors
294/// - `EACCES`: Permission denied.
295/// - `ECONNREFUSED`: No one listening on the remote address.
296/// - `EINPROGRESS`: Connection is in progress.
297/// - `ENOENT`: The socket path does not exist.
298pub fn connect_unix_stream(addr: UnixSocketAddr<'_>) -> Result<UnixConnectResult, CoreError> {
299    let encoded = UnixSockAddr::new(addr, "unix connect address")?;
300    let fd = new_unix_stream_socket()?;
301
302    loop {
303        let ret = unsafe { libc::connect(fd.as_raw_fd(), encoded.as_ptr(), encoded.len()) };
304        if ret == 0 {
305            return Ok(UnixConnectResult::Connected(UnixStreamFd { fd }));
306        }
307
308        let e = errno();
309        if e == libc::EINTR {
310            continue;
311        }
312        if e == libc::EINPROGRESS || e == libc::EALREADY {
313            return Ok(UnixConnectResult::InProgress(UnixStreamFd { fd }));
314        }
315        if e == libc::EISCONN {
316            return Ok(UnixConnectResult::Connected(UnixStreamFd { fd }));
317        }
318        return Err(CoreError::sys(e, "connect"));
319    }
320}
321
322/// Change mode bits on a Unix socket filesystem path.
323///
324/// ### Errors
325/// - `EACCES`: Permission denied.
326/// - `ENOENT`: The socket path does not exist.
327/// - `EPERM`: The caller does not own the file.
328pub fn chmod_unix_socket(addr: UnixSocketAddr<'_>, mode: u32) -> Result<(), CoreError> {
329    match addr {
330        UnixSocketAddr::Path(path) => {
331            let metadata = std::fs::symlink_metadata(path).map_err(|err| {
332                CoreError::sys(
333                    err.raw_os_error().unwrap_or(libc::EIO),
334                    "lstat unix socket path",
335                )
336            })?;
337            if !metadata.file_type().is_socket() {
338                return Err(CoreError::sys(libc::EINVAL, "chmod unix socket path"));
339            }
340            let c_path = path_cstring(path, "chmod unix socket path")?;
341            let ret = unsafe { libc::chmod(c_path.as_ptr(), mode as libc::mode_t) };
342            syscall_ret(ret, "chmod")
343        }
344        UnixSocketAddr::Abstract(_) => Err(CoreError::sys(libc::EINVAL, "chmod abstract socket")),
345    }
346}
347
348/// Change mode bits on a Unix socket filesystem path.
349pub fn chmod_socket_path(path: impl AsRef<Path>, mode: u32) -> Result<(), CoreError> {
350    chmod_unix_socket(UnixSocketAddr::Path(path.as_ref()), mode)
351}
352
353/// Connect to a Unix domain stream socket.
354///
355/// The socket is created with `SOCK_CLOEXEC` and `SOCK_NONBLOCK` set.
356fn new_unix_stream_socket() -> Result<Fd, CoreError> {
357    let fd = unsafe {
358        libc::socket(
359            libc::AF_UNIX,
360            libc::SOCK_STREAM | libc::SOCK_CLOEXEC | libc::SOCK_NONBLOCK,
361            0,
362        )
363    };
364    syscall_ret(fd, "socket(AF_UNIX)")?;
365    Fd::new(fd, "socket(AF_UNIX)")
366}
367
368fn apply_stale_socket_policy(path: &Path, policy: StaleSocketPolicy) -> Result<(), CoreError> {
369    match policy {
370        StaleSocketPolicy::Preserve => Ok(()),
371        StaleSocketPolicy::UnlinkSocketOnly => {
372            let metadata = match std::fs::symlink_metadata(path) {
373                Ok(metadata) => metadata,
374                Err(err) if err.raw_os_error() == Some(libc::ENOENT) => return Ok(()),
375                Err(err) => {
376                    return Err(CoreError::sys(
377                        err.raw_os_error().unwrap_or(libc::EIO),
378                        "lstat unix socket path",
379                    ));
380                }
381            };
382            if !metadata.file_type().is_socket() {
383                return Err(CoreError::sys(libc::EEXIST, "stale unix socket path"));
384            }
385            unlink_path(path, "unlink stale unix socket")
386        }
387        StaleSocketPolicy::UnlinkAnyPath => unlink_path(path, "unlink unix socket path"),
388    }
389}
390
391fn unlink_path(path: &Path, op: &'static str) -> Result<(), CoreError> {
392    match std::fs::remove_file(path) {
393        Ok(()) => Ok(()),
394        Err(err) if err.raw_os_error() == Some(libc::ENOENT) => Ok(()),
395        Err(err) => Err(CoreError::sys(err.raw_os_error().unwrap_or(libc::EIO), op)),
396    }
397}
398
399fn cleanup_created_path(addr: UnixSocketAddr<'_>) {
400    if let UnixSocketAddr::Path(path) = addr {
401        let _ = std::fs::remove_file(path);
402    }
403}
404
405struct UnixSockAddr {
406    inner: libc::sockaddr_un,
407    len: libc::socklen_t,
408}
409
410impl UnixSockAddr {
411    fn new(addr: UnixSocketAddr<'_>, op: &'static str) -> Result<Self, CoreError> {
412        let mut inner: libc::sockaddr_un = unsafe { std::mem::zeroed() };
413        inner.sun_family = libc::AF_UNIX as libc::sa_family_t;
414        let sun_path_offset = std::mem::offset_of!(libc::sockaddr_un, sun_path);
415
416        let len = match addr {
417            UnixSocketAddr::Path(path) => {
418                let bytes = path.as_os_str().as_bytes();
419                if bytes.is_empty() {
420                    return Err(CoreError::sys(libc::EINVAL, op));
421                }
422                if bytes.contains(&0) {
423                    return Err(CoreError::sys(libc::EINVAL, op));
424                }
425                if bytes.len() >= inner.sun_path.len() {
426                    return Err(CoreError::sys(libc::ENAMETOOLONG, op));
427                }
428
429                for (slot, byte) in inner.sun_path.iter_mut().zip(bytes.iter().copied()) {
430                    *slot = byte as libc::c_char;
431                }
432                sun_path_offset + bytes.len() + 1
433            }
434            UnixSocketAddr::Abstract(name) => {
435                validate_abstract_supported()?;
436                if name.is_empty() {
437                    return Err(CoreError::sys(libc::EINVAL, op));
438                }
439                if name.len() + 1 > inner.sun_path.len() {
440                    return Err(CoreError::sys(libc::ENAMETOOLONG, op));
441                }
442
443                inner.sun_path[0] = 0;
444                for (slot, byte) in inner.sun_path[1..].iter_mut().zip(name.iter().copied()) {
445                    *slot = byte as libc::c_char;
446                }
447                sun_path_offset + 1 + name.len()
448            }
449        };
450        let len = libc::socklen_t::try_from(len).map_err(|_| CoreError::sys(libc::EINVAL, op))?;
451
452        Ok(Self { inner, len })
453    }
454
455    fn len(&self) -> libc::socklen_t {
456        self.len
457    }
458
459    fn as_ptr(&self) -> *const libc::sockaddr {
460        (&self.inner as *const libc::sockaddr_un).cast()
461    }
462}
463
464fn validate_abstract_supported() -> Result<(), CoreError> {
465    if cfg!(any(target_os = "linux", target_os = "android")) {
466        Ok(())
467    } else {
468        Err(CoreError::sys(libc::ENOSYS, "abstract unix socket"))
469    }
470}
471
472fn path_cstring(path: &Path, op: &'static str) -> Result<std::ffi::CString, CoreError> {
473    std::ffi::CString::new(path.as_os_str().as_bytes())
474        .map_err(|_| CoreError::sys(libc::EINVAL, op))
475}
476
477#[cfg(any(target_os = "linux", target_os = "android"))]
478fn peer_cred_raw(fd: &Fd) -> Result<Option<PeerCred>, CoreError> {
479    let mut cred: libc::ucred = unsafe { std::mem::zeroed() };
480    let mut len = std::mem::size_of::<libc::ucred>() as libc::socklen_t;
481    let ret = unsafe {
482        libc::getsockopt(
483            fd.as_raw_fd(),
484            libc::SOL_SOCKET,
485            libc::SO_PEERCRED,
486            (&mut cred as *mut libc::ucred).cast(),
487            &mut len,
488        )
489    };
490    syscall_ret(ret, "getsockopt(SO_PEERCRED)")?;
491
492    Ok(Some(PeerCred {
493        pid: Some(cred.pid),
494        uid: cred.uid,
495        gid: cred.gid,
496    }))
497}
498
499#[cfg(not(any(target_os = "linux", target_os = "android")))]
500fn peer_cred_raw(_fd: &Fd) -> Result<Option<PeerCred>, CoreError> {
501    Ok(None)
502}