From 61bd3e2a8f0cefaef4799fd6049f264244752de6 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Wed, 11 Jan 2023 13:13:44 +0100 Subject: [PATCH 1/3] Added LOCAL_PEERPID/LocalPeerPid sockopt for macos --- src/sys/socket/sockopt.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/sys/socket/sockopt.rs b/src/sys/socket/sockopt.rs index dd24208f31..cea623ae15 100644 --- a/src/sys/socket/sockopt.rs +++ b/src/sys/socket/sockopt.rs @@ -492,6 +492,15 @@ sockopt_impl!( libc::LOCAL_PEERCRED, super::XuCred ); +#[cfg(any(target_os = "macos"))] +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. From 34788b179b14b5c5c8e7bb7df168089fc854c499 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sat, 14 Jan 2023 19:49:42 +0100 Subject: [PATCH 2/3] Added changelog entry and test for LocalPeerPid --- CHANGELOG.md | 1 + test/sys/test_sockopt.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7da93bbf1..68edef38df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/test/sys/test_sockopt.rs b/test/sys/test_sockopt.rs index 34bef945e1..bf791688d6 100644 --- a/test/sys/test_sockopt.rs +++ b/test/sys/test_sockopt.rs @@ -54,6 +54,22 @@ pub fn test_local_peercred_stream() { assert_eq!(Gid::from_raw(xucred.groups()[0]), Gid::current()); } +#[cfg(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() { From 960199daf9f395096b08dc24aa0337cb5b93da81 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 15 Jan 2023 22:41:33 +0100 Subject: [PATCH 3/3] Try enabling LocalPeerPid for ios --- src/sys/socket/sockopt.rs | 2 +- test/sys/test_sockopt.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sys/socket/sockopt.rs b/src/sys/socket/sockopt.rs index cea623ae15..9021664997 100644 --- a/src/sys/socket/sockopt.rs +++ b/src/sys/socket/sockopt.rs @@ -492,7 +492,7 @@ sockopt_impl!( libc::LOCAL_PEERCRED, super::XuCred ); -#[cfg(any(target_os = "macos"))] +#[cfg(any(target_os = "macos", target_os = "ios"))] sockopt_impl!( /// Get the PID of the peer process of a connected unix domain socket. LocalPeerPid, diff --git a/test/sys/test_sockopt.rs b/test/sys/test_sockopt.rs index bf791688d6..a498bdfa2c 100644 --- a/test/sys/test_sockopt.rs +++ b/test/sys/test_sockopt.rs @@ -54,7 +54,7 @@ pub fn test_local_peercred_stream() { assert_eq!(Gid::from_raw(xucred.groups()[0]), Gid::current()); } -#[cfg(target_os = "macos")] +#[cfg(any(target_os = "ios", target_os = "macos"))] #[test] pub fn test_local_peer_pid() { use nix::sys::socket::socketpair;