Skip to main content

std/os/net/linux_ext/
socket.rs

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