diff --git a/library/std/src/os/unix/net/ancillary.rs b/library/std/src/os/unix/net/ancillary.rs index a29008140f784..89d93ff8ac706 100644 --- a/library/std/src/os/unix/net/ancillary.rs +++ b/library/std/src/os/unix/net/ancillary.rs @@ -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 } } @@ -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>), } @@ -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); diff --git a/library/std/src/os/unix/net/stream.rs b/library/std/src/os/unix/net/stream.rs index 583f861a92535..e8b6a792219ca 100644 --- a/library/std/src/os/unix/net/stream.rs +++ b/library/std/src/os/unix/net/stream.rs @@ -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) @@ -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 { self.0.passcred() diff --git a/library/std/src/sys/unix/net.rs b/library/std/src/sys/unix/net.rs index 15d0dbe07fe7e..711169985e3d2 100644 --- a/library/std/src/sys/unix/net.rs +++ b/library/std/src/sys/unix/net.rs @@ -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 { 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 { + 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;