Skip to content

Commit c42b649

Browse files
bors[bot]mitsuhiko
andauthored
Merge #1967
1967: Added LOCAL_PEERPID/LocalPeerPid sockopt for macos r=asomers a=mitsuhiko macOS has a badly documented `LOCAL_PEERPID` sockopt that can be used to retrieve the PID of the connected peer. I intentionally only added this for macOS because I know it exists there, and I'm not sure about ios yet even if it exists there, it's doubtful that the PID information gets any use there. Co-authored-by: Armin Ronacher <[email protected]>
2 parents 348e238 + 960199d commit c42b649

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
1313
([#1912](https://github.com/nix-rust/nix/pull/1912))
1414
- Added `mq_timedreceive` to `::nix::mqueue`.
1515
([#1966])(https://github.com/nix-rust/nix/pull/1966)
16+
- Added `LocalPeerPid` to `nix::sys::socket::sockopt` for macOS. ([#1967](https://github.com/nix-rust/nix/pull/1967))
1617

1718
### Changed
1819

src/sys/socket/sockopt.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,15 @@ sockopt_impl!(
492492
libc::LOCAL_PEERCRED,
493493
super::XuCred
494494
);
495+
#[cfg(any(target_os = "macos", target_os = "ios"))]
496+
sockopt_impl!(
497+
/// Get the PID of the peer process of a connected unix domain socket.
498+
LocalPeerPid,
499+
GetOnly,
500+
0,
501+
libc::LOCAL_PEERPID,
502+
libc::c_int
503+
);
495504
#[cfg(any(target_os = "android", target_os = "linux"))]
496505
sockopt_impl!(
497506
/// Return the credentials of the foreign process connected to this socket.

test/sys/test_sockopt.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ pub fn test_local_peercred_stream() {
5454
assert_eq!(Gid::from_raw(xucred.groups()[0]), Gid::current());
5555
}
5656

57+
#[cfg(any(target_os = "ios", target_os = "macos"))]
58+
#[test]
59+
pub fn test_local_peer_pid() {
60+
use nix::sys::socket::socketpair;
61+
62+
let (fd1, _fd2) = socketpair(
63+
AddressFamily::Unix,
64+
SockType::Stream,
65+
None,
66+
SockFlag::empty(),
67+
)
68+
.unwrap();
69+
let pid = getsockopt(fd1, sockopt::LocalPeerPid).unwrap();
70+
assert_eq!(pid, std::process::id() as _);
71+
}
72+
5773
#[cfg(target_os = "linux")]
5874
#[test]
5975
fn is_so_mark_functional() {

0 commit comments

Comments
 (0)