Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions library/std/src/os/unix/net/ancillary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,46 +249,53 @@ impl SocketCred {
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
#[must_use]
pub fn new() -> SocketCred {
SocketCred(libc::cmsgcred { cmsgcred_pid: 0, cmsgcred_uid: 0, cmsgcred_gid: 0 })
SocketCred(libc::cmsgcred {
cmcred_pid: 0,
cmcred_uid: 0,
cmcred_euid: 0,
cmcred_gid: 0,
cmcred_ngroups: 0,
cmcred_groups: [0; libc::CMGROUP_MAX],
})
}

/// Set the PID.
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn set_pid(&mut self, pid: libc::pid_t) {
self.0.cmsgcred_pid = pid;
self.0.cmcred_pid = pid;
}

/// Get the current PID.
#[must_use]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn get_pid(&self) -> libc::pid_t {
self.0.cmsgcred_pid
self.0.cmcred_pid
}

/// Set the UID.
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn set_uid(&mut self, uid: libc::uid_t) {
self.0.cmsgcred_uid = uid;
self.0.cmcred_uid = uid;
}

/// Get the current UID.
#[must_use]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn get_uid(&self) -> libc::uid_t {
self.0.cmsgcred_uid
self.0.cmcred_uid
}

/// Set the GID.
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn set_gid(&mut self, gid: libc::gid_t) {
self.0.cmsgcred_gid = gid;
self.0.cmcred_gid = gid;
}

/// Get the current GID.
#[must_use]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn get_gid(&self) -> libc::gid_t {
self.0.cmsgcred_gid
self.0.cmcred_gid
}
}

Expand Down Expand Up @@ -340,7 +347,7 @@ pub enum AncillaryError {
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub enum AncillaryData<'a> {
ScmRights(ScmRights<'a>),
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "dragonfly"))]
ScmCredentials(ScmCredentials<'a>),
}

Expand All @@ -363,7 +370,7 @@ impl<'a> AncillaryData<'a> {
///
/// `data` must contain a valid control message and the control message must be type of
/// `SOL_SOCKET` and level of `SCM_CREDENTIALS` or `SCM_CREDS`.
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "dragonfly"))]
unsafe fn as_credentials(data: &'a [u8]) -> Self {
let ancillary_data_iter = AncillaryDataIter::new(data);
let scm_credentials = ScmCredentials(ancillary_data_iter);
Expand Down
4 changes: 2 additions & 2 deletions library/std/src/os/unix/net/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ impl UnixStream {
/// Ok(())
/// }
/// ```
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "dragonfly"))]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
self.0.set_passcred(passcred)
Expand All @@ -427,7 +427,7 @@ impl UnixStream {
/// Get the socket option `SO_PASSCRED`.
///
/// [`set_passcred`]: UnixStream::set_passcred
#[cfg(any(doc, target_os = "android", target_os = "linux",))]
#[cfg(any(doc, target_os = "android", target_os = "linux", target_os = "dragonfly"))]
#[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
pub fn passcred(&self) -> io::Result<bool> {
self.0.passcred()
Expand Down
17 changes: 15 additions & 2 deletions library/std/src/sys/unix/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,17 +408,30 @@ impl Socket {
Ok(raw != 0)
}

#[cfg(any(target_os = "android", target_os = "linux", target_os = "dragonfly",))]
#[cfg(any(target_os = "android", target_os = "linux"))]
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
setsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED, passcred as libc::c_int)
}

#[cfg(any(target_os = "android", target_os = "linux", target_os = "dragonfly",))]
#[cfg(target_os = "dragonfly")]
pub fn set_passcred(&self, passcred: bool) -> io::Result<()> {
const SO_PASSCRED: libc::c_int = 0x4000;
setsockopt(self, libc::SOL_SOCKET, SO_PASSCRED, passcred as libc::c_int)
}

#[cfg(any(target_os = "android", target_os = "linux"))]
pub fn passcred(&self) -> io::Result<bool> {
let passcred: libc::c_int = getsockopt(self, libc::SOL_SOCKET, libc::SO_PASSCRED)?;
Ok(passcred != 0)
}

#[cfg(target_os = "dragonfly")]
pub fn passcred(&self) -> io::Result<bool> {
const SO_PASSCRED: libc::c_int = 0x4000;
let passcred: libc::c_int = getsockopt(self, libc::SOL_SOCKET, SO_PASSCRED)?;
Ok(passcred != 0)
}

#[cfg(not(any(target_os = "solaris", target_os = "illumos")))]
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
let mut nonblocking = nonblocking as libc::c_int;
Expand Down