Skip to content

Commit

Permalink
Merge #1967
Browse files Browse the repository at this point in the history
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 <armin.ronacher@active-4.com>
  • Loading branch information
bors[bot] and mitsuhiko authored Feb 9, 2023
2 parents 348e238 + 960199d commit c42b649
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
([#1912](https://github.com/nix-rust/nix/pull/1912))
- Added `mq_timedreceive` to `::nix::mqueue`.
([#1966])(https://github.com/nix-rust/nix/pull/1966)
- Added `LocalPeerPid` to `nix::sys::socket::sockopt` for macOS. ([#1967](https://github.com/nix-rust/nix/pull/1967))

### Changed

Expand Down
9 changes: 9 additions & 0 deletions src/sys/socket/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,15 @@ sockopt_impl!(
libc::LOCAL_PEERCRED,
super::XuCred
);
#[cfg(any(target_os = "macos", target_os = "ios"))]
sockopt_impl!(
/// Get the PID of the peer process of a connected unix domain socket.
LocalPeerPid,
GetOnly,
0,
libc::LOCAL_PEERPID,
libc::c_int
);
#[cfg(any(target_os = "android", target_os = "linux"))]
sockopt_impl!(
/// Return the credentials of the foreign process connected to this socket.
Expand Down
16 changes: 16 additions & 0 deletions test/sys/test_sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ pub fn test_local_peercred_stream() {
assert_eq!(Gid::from_raw(xucred.groups()[0]), Gid::current());
}

#[cfg(any(target_os = "ios", target_os = "macos"))]
#[test]
pub fn test_local_peer_pid() {
use nix::sys::socket::socketpair;

let (fd1, _fd2) = socketpair(
AddressFamily::Unix,
SockType::Stream,
None,
SockFlag::empty(),
)
.unwrap();
let pid = getsockopt(fd1, sockopt::LocalPeerPid).unwrap();
assert_eq!(pid, std::process::id() as _);
}

#[cfg(target_os = "linux")]
#[test]
fn is_so_mark_functional() {
Expand Down

0 comments on commit c42b649

Please sign in to comment.