Skip to main content

std/os/net/linux_ext/
socket.rs

1//! Linux and Android-specific socket functionality.
2
3use crate::io;
4use crate::os::unix::net;
5use crate::sys::AsInner;
6
7/// Linux-specific functionality for `AF_UNIX` sockets [`UnixDatagram`]
8/// and [`UnixStream`].
9///
10/// [`UnixDatagram`]: net::UnixDatagram
11/// [`UnixStream`]: net::UnixStream
12#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
13pub impl(self) trait UnixSocketExt {
14    /// Query the current setting of socket option `SO_PASSCRED`.
15    #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
16    fn passcred(&self) -> io::Result<bool>;
17
18    /// Enable or disable socket option `SO_PASSCRED`.
19    ///
20    /// This option enables the credentials of the sending process to be
21    /// received as a control message in [`AncillaryData`].
22    ///
23    /// [`AncillaryData`]: net::AncillaryData
24    ///
25    /// # Examples
26    ///
27    #[cfg_attr(
28        any(target_os = "linux", target_os = "android", target_os = "cygwin"),
29        doc = "```no_run"
30    )]
31    #[cfg_attr(
32        not(any(target_os = "linux", target_os = "android", target_os = "cygwin")),
33        doc = "```ignore (needs linux)"
34    )]
35    /// #![feature(unix_socket_ancillary_data)]
36    /// #[cfg(target_os = "linux")]
37    /// use std::os::linux::net::UnixSocketExt;
38    /// #[cfg(target_os = "android")]
39    /// use std::os::android::net::UnixSocketExt;
40    /// use std::os::unix::net::UnixDatagram;
41    ///
42    /// fn main() -> std::io::Result<()> {
43    ///     let sock = UnixDatagram::unbound()?;
44    ///     sock.set_passcred(true).expect("set_passcred failed");
45    ///     Ok(())
46    /// }
47    /// ```
48    #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
49    fn set_passcred(&self, passcred: bool) -> io::Result<()>;
50}
51
52#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
53impl UnixSocketExt for net::UnixDatagram {
54    fn passcred(&self) -> io::Result<bool> {
55        self.as_inner().passcred()
56    }
57
58    fn set_passcred(&self, passcred: bool) -> io::Result<()> {
59        self.as_inner().set_passcred(passcred)
60    }
61}
62
63#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
64impl UnixSocketExt for net::UnixStream {
65    fn passcred(&self) -> io::Result<bool> {
66        self.as_inner().passcred()
67    }
68
69    fn set_passcred(&self, passcred: bool) -> io::Result<()> {
70        self.as_inner().set_passcred(passcred)
71    }
72}