diff --git a/library/std/src/sys/unix/ext/net/ancillary.rs b/library/std/src/sys/unix/ext/net/ancillary.rs index 77214801e3e..7d0f43ab87f 100644 --- a/library/std/src/sys/unix/ext/net/ancillary.rs +++ b/library/std/src/sys/unix/ext/net/ancillary.rs @@ -64,6 +64,47 @@ pub(super) fn send_vectored_with_ancillary_to( } } +#[unstable(feature = "unix_socket_ancillary_data", issue = "none")] +#[derive(Clone)] +pub struct UCred(libc::ucred); + +impl UCred { + #[unstable(feature = "unix_socket_ancillary_data", issue = "none")] + pub fn new() -> UCred { + UCred(libc::ucred { pid: 0, uid: 0, gid: 0 }) + } + + #[unstable(feature = "unix_socket_ancillary_data", issue = "none")] + pub fn set_pid(&mut self, pid: i32) { + self.0.pid = pid; + } + + #[unstable(feature = "unix_socket_ancillary_data", issue = "none")] + pub fn get_pid(&self) -> i32 { + self.0.pid + } + + #[unstable(feature = "unix_socket_ancillary_data", issue = "none")] + pub fn set_uid(&mut self, uid: u32) { + self.0.uid = uid; + } + + #[unstable(feature = "unix_socket_ancillary_data", issue = "none")] + pub fn get_uid(&self) -> u32 { + self.0.uid + } + + #[unstable(feature = "unix_socket_ancillary_data", issue = "none")] + pub fn set_gid(&mut self, gid: u32) { + self.0.gid = gid; + } + + #[unstable(feature = "unix_socket_ancillary_data", issue = "none")] + pub fn get_gid(&self) -> u32 { + self.0.gid + } +} + #[cfg(any( target_os = "haiku", target_os = "solaris", @@ -139,10 +180,10 @@ pub struct ScmCredentials<'a>(AncillaryDataIter<'a, libc::ucred>); ))] #[unstable(feature = "unix_socket_ancillary_data", issue = "none")] impl<'a> Iterator for ScmCredentials<'a> { - type Item = libc::ucred; + type Item = UCred; - fn next(&mut self) -> Option { - self.0.next() + fn next(&mut self) -> Option { + Some(UCred(self.0.next()?)) } } @@ -550,7 +591,7 @@ impl<'a> SocketAncillary<'a> { target_env = "uclibc", ))] #[unstable(feature = "unix_socket_ancillary_data", issue = "none")] - pub fn add_creds(&mut self, creds: &[libc::ucred]) -> bool { + pub fn add_creds(&mut self, creds: &[UCred]) -> bool { self.truncated = false; add_to_ancillary_data( &mut self.buffer, diff --git a/library/std/src/sys/unix/ext/net/test.rs b/library/std/src/sys/unix/ext/net/test.rs index 3be9bb48583..a39b97f2c31 100644 --- a/library/std/src/sys/unix/ext/net/test.rs +++ b/library/std/src/sys/unix/ext/net/test.rs @@ -529,8 +529,11 @@ fn test_send_vectored_with_ancillary_to_unix_datagram() { let mut ancillary1_buffer = [0; 128]; let mut ancillary1 = SocketAncillary::new(&mut ancillary1_buffer[..]); - let cred1 = libc::ucred { pid: getpid(), uid: getuid(), gid: getgid() }; - assert!(ancillary1.add_creds(&[cred1][..])); + let mut cred1 = UCred::new(); + cred1.set_pid(getpid()); + cred1.set_uid(getuid()); + cred1.set_gid(getgid()); + assert!(ancillary1.add_creds(&[cred1.clone()][..])); let usize = or_panic!(bsock1.send_vectored_with_ancillary_to(&mut bufs_send, &mut ancillary1, &path2)); @@ -556,9 +559,9 @@ fn test_send_vectored_with_ancillary_to_unix_datagram() { { let cred_vec = Vec::from_iter(scm_credentials); assert_eq!(cred_vec.len(), 1); - assert_eq!(cred1.pid, cred_vec[0].pid); - assert_eq!(cred1.uid, cred_vec[0].uid); - assert_eq!(cred1.gid, cred_vec[0].gid); + assert_eq!(cred1.get_pid(), cred_vec[0].get_pid()); + assert_eq!(cred1.get_uid(), cred_vec[0].get_uid()); + assert_eq!(cred1.get_gid(), cred_vec[0].get_gid()); } else { assert!(false); }