-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #117156 - jmillikin:os-unix-socket-ext, r=Amanieu,dtolnay
Convert `Unix{Datagram,Stream}::{set_}passcred()` to per-OS traits These methods are the pre-stabilized API for obtaining peer credentials from an `AF_UNIX` socket, part of the `unix_socket_ancillary_data` feature. Their current behavior is to get/set one of the `SO_PASSCRED` (Linux), `LOCAL_CREDS_PERSISTENT` (FreeBSD), or `LOCAL_CREDS` (NetBSD) socket options. On other targets the `{set_}passcred()` methods do not exist. There are two problems with this approach: 1. Having public methods only exist for certain targets isn't permitted in a stable `std` API. 2. These options have generally similar purposes, but they are non-POSIX and their details can differ in subtle and surprising ways (such as whether they continue to be set after the next call to `recvmsg()`). Splitting into OS-specific extension traits is the preferred solution to both problems.
- Loading branch information
Showing
12 changed files
with
246 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
#![stable(feature = "raw_ext", since = "1.1.0")] | ||
|
||
pub mod fs; | ||
pub mod net; | ||
pub mod raw; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//! FreeBSD-specific networking functionality. | ||
|
||
#![unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
|
||
use crate::io; | ||
use crate::os::unix::net; | ||
use crate::sealed::Sealed; | ||
use crate::sys_common::AsInner; | ||
|
||
/// FreeBSD-specific functionality for `AF_UNIX` sockets [`UnixDatagram`] | ||
/// and [`UnixStream`]. | ||
/// | ||
/// [`UnixDatagram`]: net::UnixDatagram | ||
/// [`UnixStream`]: net::UnixStream | ||
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
pub trait UnixSocketExt: Sealed { | ||
/// Query the current setting of socket option `LOCAL_CREDS_PERSISTENT`. | ||
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
fn local_creds_persistent(&self) -> io::Result<bool>; | ||
|
||
/// Enable or disable socket option `LOCAL_CREDS_PERSISTENT`. | ||
/// | ||
/// This option enables the credentials of the sending process to be | ||
/// received as a control message in [`AncillaryData`]. | ||
/// | ||
/// [`AncillaryData`]: net::AncillaryData | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #![feature(unix_socket_ancillary_data)] | ||
/// use std::os::freebsd::net::UnixSocketExt; | ||
/// use std::os::unix::net::UnixDatagram; | ||
/// | ||
/// fn main() -> std::io::Result<()> { | ||
/// let sock = UnixDatagram::unbound()?; | ||
/// sock.set_local_creds_persistent(true).expect("set_local_creds_persistent failed"); | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
fn set_local_creds_persistent(&self, local_creds_persistent: bool) -> io::Result<()>; | ||
} | ||
|
||
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
impl UnixSocketExt for net::UnixDatagram { | ||
fn local_creds_persistent(&self) -> io::Result<bool> { | ||
self.as_inner().local_creds_persistent() | ||
} | ||
|
||
fn set_local_creds_persistent(&self, local_creds_persistent: bool) -> io::Result<()> { | ||
self.as_inner().set_local_creds_persistent(local_creds_persistent) | ||
} | ||
} | ||
|
||
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
impl UnixSocketExt for net::UnixStream { | ||
fn local_creds_persistent(&self) -> io::Result<bool> { | ||
self.as_inner().local_creds_persistent() | ||
} | ||
|
||
fn set_local_creds_persistent(&self, local_creds_persistent: bool) -> io::Result<()> { | ||
self.as_inner().set_local_creds_persistent(local_creds_persistent) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//! Linux and Android-specific socket functionality. | ||
|
||
use crate::io; | ||
use crate::os::unix::net; | ||
use crate::sealed::Sealed; | ||
use crate::sys_common::AsInner; | ||
|
||
/// Linux-specific functionality for `AF_UNIX` sockets [`UnixDatagram`] | ||
/// and [`UnixStream`]. | ||
/// | ||
/// [`UnixDatagram`]: net::UnixDatagram | ||
/// [`UnixStream`]: net::UnixStream | ||
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
pub trait UnixSocketExt: Sealed { | ||
/// Query the current setting of socket option `SO_PASSCRED`. | ||
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
fn passcred(&self) -> io::Result<bool>; | ||
|
||
/// Enable or disable socket option `SO_PASSCRED`. | ||
/// | ||
/// This option enables the credentials of the sending process to be | ||
/// received as a control message in [`AncillaryData`]. | ||
/// | ||
/// [`AncillaryData`]: net::AncillaryData | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #![feature(unix_socket_ancillary_data)] | ||
/// use std::os::linux::net::UnixSocketExt; | ||
/// use std::os::unix::net::UnixDatagram; | ||
/// | ||
/// fn main() -> std::io::Result<()> { | ||
/// let sock = UnixDatagram::unbound()?; | ||
/// sock.set_passcred(true).expect("set_passcred failed"); | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
fn set_passcred(&self, passcred: bool) -> io::Result<()>; | ||
} | ||
|
||
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
impl UnixSocketExt for net::UnixDatagram { | ||
fn passcred(&self) -> io::Result<bool> { | ||
self.as_inner().passcred() | ||
} | ||
|
||
fn set_passcred(&self, passcred: bool) -> io::Result<()> { | ||
self.as_inner().set_passcred(passcred) | ||
} | ||
} | ||
|
||
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
impl UnixSocketExt for net::UnixStream { | ||
fn passcred(&self) -> io::Result<bool> { | ||
self.as_inner().passcred() | ||
} | ||
|
||
fn set_passcred(&self, passcred: bool) -> io::Result<()> { | ||
self.as_inner().set_passcred(passcred) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
#![stable(feature = "raw_ext", since = "1.1.0")] | ||
|
||
pub mod fs; | ||
pub mod net; | ||
pub mod raw; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//! NetBSD-specific networking functionality. | ||
|
||
#![unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
|
||
use crate::io; | ||
use crate::os::unix::net; | ||
use crate::sealed::Sealed; | ||
use crate::sys_common::AsInner; | ||
|
||
/// NetBSD-specific functionality for `AF_UNIX` sockets [`UnixDatagram`] | ||
/// and [`UnixStream`]. | ||
/// | ||
/// [`UnixDatagram`]: net::UnixDatagram | ||
/// [`UnixStream`]: net::UnixStream | ||
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
pub trait UnixSocketExt: Sealed { | ||
/// Query the current setting of socket option `LOCAL_CREDS`. | ||
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
fn local_creds(&self) -> io::Result<bool>; | ||
|
||
/// Enable or disable socket option `LOCAL_CREDS`. | ||
/// | ||
/// This option enables the credentials of the sending process to be | ||
/// received as a control message in [`AncillaryData`]. | ||
/// | ||
/// [`AncillaryData`]: net::AncillaryData | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```no_run | ||
/// #![feature(unix_socket_ancillary_data)] | ||
/// use std::os::netbsd::net::UnixSocketExt; | ||
/// use std::os::unix::net::UnixDatagram; | ||
/// | ||
/// fn main() -> std::io::Result<()> { | ||
/// let sock = UnixDatagram::unbound()?; | ||
/// sock.set_local_creds(true).expect("set_local_creds failed"); | ||
/// Ok(()) | ||
/// } | ||
/// ``` | ||
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
fn set_local_creds(&self, local_creds: bool) -> io::Result<()>; | ||
} | ||
|
||
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
impl UnixSocketExt for net::UnixDatagram { | ||
fn local_creds(&self) -> io::Result<bool> { | ||
self.as_inner().local_creds() | ||
} | ||
|
||
fn set_local_creds(&self, local_creds: bool) -> io::Result<()> { | ||
self.as_inner().set_local_creds(local_creds) | ||
} | ||
} | ||
|
||
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")] | ||
impl UnixSocketExt for net::UnixStream { | ||
fn local_creds(&self) -> io::Result<bool> { | ||
self.as_inner().local_creds() | ||
} | ||
|
||
fn set_local_creds(&self, local_creds: bool) -> io::Result<()> { | ||
self.as_inner().set_local_creds(local_creds) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.