Skip to content

Commit fe0bea2

Browse files
committed
ext/ucred: Support PID in peer creds on macOS
1 parent 53d19b3 commit fe0bea2

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

library/std/src/sys/unix/ext/ucred.rs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ pub use self::impl_linux::peer_cred;
3131
#[cfg(any(
3232
target_os = "dragonfly",
3333
target_os = "freebsd",
34-
target_os = "ios",
35-
target_os = "macos",
3634
target_os = "openbsd"
3735
))]
3836
pub use self::impl_bsd::peer_cred;
3937

38+
#[cfg(any(
39+
target_os = "macos",
40+
target_os = "ios",
41+
))]
42+
pub use self::impl_mac::peer_cred;
43+
4044
#[cfg(any(target_os = "linux", target_os = "android"))]
4145
pub mod impl_linux {
4246
use super::UCred;
@@ -75,8 +79,6 @@ pub mod impl_linux {
7579

7680
#[cfg(any(
7781
target_os = "dragonfly",
78-
target_os = "macos",
79-
target_os = "ios",
8082
target_os = "freebsd",
8183
target_os = "openbsd"
8284
))]
@@ -95,3 +97,44 @@ pub mod impl_bsd {
9597
}
9698
}
9799
}
100+
101+
#[cfg(any(
102+
target_os = "macos",
103+
target_os = "ios",
104+
))]
105+
pub mod impl_mac {
106+
use super::UCred;
107+
use crate::{io, mem};
108+
use crate::os::unix::io::AsRawFd;
109+
use crate::os::unix::net::UnixStream;
110+
use libc::{c_void, getpeereid, getsockopt, pid_t, socklen_t, SOL_LOCAL, LOCAL_PEERPID};
111+
112+
pub fn peer_cred(socket: &UnixStream) -> io::Result<UCred> {
113+
let mut cred = UCred { uid: 1, gid: 1, pid: None };
114+
unsafe {
115+
let ret = getpeereid(socket.as_raw_fd(), &mut cred.uid, &mut cred.gid);
116+
117+
if ret != 0 {
118+
return Err(io::Error::last_os_error());
119+
}
120+
121+
let mut pid: pid_t = 1;
122+
let mut pid_size = mem::size_of::<pid_t>() as socklen_t;
123+
124+
let ret = getsockopt(
125+
socket.as_raw_fd(),
126+
SOL_LOCAL,
127+
LOCAL_PEERPID,
128+
&mut pid as *mut pid_t as *mut c_void,
129+
&mut pid_size
130+
);
131+
132+
if ret == 0 && pid_size as usize == mem::size_of::<pid_t>() {
133+
cred.pid = Some(pid);
134+
Ok(cred)
135+
} else {
136+
Err(io::Error::last_os_error())
137+
}
138+
}
139+
}
140+
}

library/std/src/sys/unix/ext/ucred/tests.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::os::unix::net::UnixStream;
2-
use libc::{getegid, geteuid};
2+
use libc::{getegid, geteuid, getpid};
33

44
#[test]
55
#[cfg(any(
@@ -23,3 +23,20 @@ fn test_socket_pair() {
2323
assert_eq!(cred_a.uid, uid);
2424
assert_eq!(cred_a.gid, gid);
2525
}
26+
27+
#[test]
28+
#[cfg(any(
29+
target_os = "linux",
30+
target_os = "ios",
31+
target_os = "macos",
32+
))]
33+
fn test_socket_pair_pids(arg: Type) -> RetType {
34+
// Create two connected sockets and get their peer credentials.
35+
let (sock_a, sock_b) = UnixStream::pair().unwrap();
36+
let (cred_a, cred_b) = (sock_a.peer_cred().unwrap(), sock_b.peer_cred().unwrap());
37+
38+
// On supported platforms (see the cfg above), the credentials should always include the PID.
39+
let pid = unsafe { getpid() };
40+
assert_eq!(cred_a.pid, Some(pid));
41+
assert_eq!(cred_b.pid, Some(pid));
42+
}

0 commit comments

Comments
 (0)