1 | //! Linux and Android-specific socket functionality. |
2 | |
3 | use crate::io; |
4 | use crate::os::unix::net; |
5 | use crate::sealed::Sealed; |
6 | use crate::sys_common::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" )] |
14 | pub trait UnixSocketExt: Sealed { |
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 | /// ```no_run |
29 | /// #![feature(unix_socket_ancillary_data)] |
30 | /// #[cfg(target_os = "linux" )] |
31 | /// use std::os::linux::net::UnixSocketExt; |
32 | /// #[cfg(target_os = "android" )] |
33 | /// use std::os::android::net::UnixSocketExt; |
34 | /// use std::os::unix::net::UnixDatagram; |
35 | /// |
36 | /// fn main() -> std::io::Result<()> { |
37 | /// let sock = UnixDatagram::unbound()?; |
38 | /// sock.set_passcred(true).expect("set_passcred failed" ); |
39 | /// Ok(()) |
40 | /// } |
41 | /// ``` |
42 | #[unstable (feature = "unix_socket_ancillary_data" , issue = "76915" )] |
43 | fn set_passcred(&self, passcred: bool) -> io::Result<()>; |
44 | } |
45 | |
46 | #[unstable (feature = "unix_socket_ancillary_data" , issue = "76915" )] |
47 | impl UnixSocketExt for net::UnixDatagram { |
48 | fn passcred(&self) -> io::Result<bool> { |
49 | self.as_inner().passcred() |
50 | } |
51 | |
52 | fn set_passcred(&self, passcred: bool) -> io::Result<()> { |
53 | self.as_inner().set_passcred(passcred) |
54 | } |
55 | } |
56 | |
57 | #[unstable (feature = "unix_socket_ancillary_data" , issue = "76915" )] |
58 | impl UnixSocketExt for net::UnixStream { |
59 | fn passcred(&self) -> io::Result<bool> { |
60 | self.as_inner().passcred() |
61 | } |
62 | |
63 | fn set_passcred(&self, passcred: bool) -> io::Result<()> { |
64 | self.as_inner().set_passcred(passcred) |
65 | } |
66 | } |
67 | |