1
Fork 0

Add UCred struct

This commit is contained in:
LinkTed 2020-08-23 14:34:12 +02:00
parent 6f82ddf18e
commit e6984eee6f
2 changed files with 53 additions and 9 deletions

View file

@ -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( #[cfg(any(
target_os = "haiku", target_os = "haiku",
target_os = "solaris", target_os = "solaris",
@ -139,10 +180,10 @@ pub struct ScmCredentials<'a>(AncillaryDataIter<'a, libc::ucred>);
))] ))]
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")] #[unstable(feature = "unix_socket_ancillary_data", issue = "none")]
impl<'a> Iterator for ScmCredentials<'a> { impl<'a> Iterator for ScmCredentials<'a> {
type Item = libc::ucred; type Item = UCred;
fn next(&mut self) -> Option<libc::ucred> { fn next(&mut self) -> Option<UCred> {
self.0.next() Some(UCred(self.0.next()?))
} }
} }
@ -550,7 +591,7 @@ impl<'a> SocketAncillary<'a> {
target_env = "uclibc", target_env = "uclibc",
))] ))]
#[unstable(feature = "unix_socket_ancillary_data", issue = "none")] #[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; self.truncated = false;
add_to_ancillary_data( add_to_ancillary_data(
&mut self.buffer, &mut self.buffer,

View file

@ -529,8 +529,11 @@ fn test_send_vectored_with_ancillary_to_unix_datagram() {
let mut ancillary1_buffer = [0; 128]; let mut ancillary1_buffer = [0; 128];
let mut ancillary1 = SocketAncillary::new(&mut ancillary1_buffer[..]); let mut ancillary1 = SocketAncillary::new(&mut ancillary1_buffer[..]);
let cred1 = libc::ucred { pid: getpid(), uid: getuid(), gid: getgid() }; let mut cred1 = UCred::new();
assert!(ancillary1.add_creds(&[cred1][..])); cred1.set_pid(getpid());
cred1.set_uid(getuid());
cred1.set_gid(getgid());
assert!(ancillary1.add_creds(&[cred1.clone()][..]));
let usize = let usize =
or_panic!(bsock1.send_vectored_with_ancillary_to(&mut bufs_send, &mut ancillary1, &path2)); 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); let cred_vec = Vec::from_iter(scm_credentials);
assert_eq!(cred_vec.len(), 1); assert_eq!(cred_vec.len(), 1);
assert_eq!(cred1.pid, cred_vec[0].pid); assert_eq!(cred1.get_pid(), cred_vec[0].get_pid());
assert_eq!(cred1.uid, cred_vec[0].uid); assert_eq!(cred1.get_uid(), cred_vec[0].get_uid());
assert_eq!(cred1.gid, cred_vec[0].gid); assert_eq!(cred1.get_gid(), cred_vec[0].get_gid());
} else { } else {
assert!(false); assert!(false);
} }