rustix icon indicating copy to clipboard operation
rustix copied to clipboard

Add `socket_peerpidfd`

Open ids1024 opened this issue 7 months ago • 8 comments

Supported on Linux since 6.5.

I see socket_peercred is only provided on Linux, while OpenBSD (https://man.openbsd.org/getsockopt.2) has some version of SO_PEERCRED, and FreeBSD (https://man.freebsd.org/cgi/man.cgi?unix(4)) has a LOCAL_PEERCRED (libwayland for instance has code to use those on their respective platforms). These APIs do seem to be slightly different though. In any case, SO_PEERPIDFD is definitely Linux-specific for the time being.

ids1024 avatar Jun 11 '25 03:06 ids1024

pidfd_getpid would also be good to add. Apparently that's not a system call, so for linux-raw we'd need to parse /proc/self/fdinfo/$pidfd for the Pid: field. But that should be fine, and not too hard.

ids1024 avatar Jun 11 '25 03:06 ids1024

Those imports are unfortunately evoking warnings, which are currently configured to be errors in CI. Could you add conditionals to the imports so that they're not imported if they're not needed?

sunfishcode avatar Sep 09 '25 01:09 sunfishcode

Huh, looking at CI, I guess libc doesn't define SO_PEERPIDFD in https://github.com/rust-lang/libc/blob/main/src/unix/linux_like/android/mod.rs.

Not sure why those are defined separately for Android; I'd assume unless the C standard library is doing something weird, those particular definitions should be the same for anything with a Linux kernel.

ids1024 avatar Sep 12 '25 22:09 ids1024

@ids1024 Thanks so much for doing this. Do you intend to finish this PR?

zeenix avatar Oct 16 '25 16:10 zeenix

I guess to pass CI this could just be disabled on Android. Presumably libc should be updated to have things like SO_PEERPIDFD. Changed that for now.

Tests seem to be broken with a recent compile, so I rebased this on https://github.com/bytecodealliance/rustix/pull/1523. Looks like there are a couple other unrelated CI errors? But I think this should be mergeable.

ids1024 avatar Oct 16 '25 19:10 ids1024

Or well, one of the errors was related to this change. Fixed that, and added a commit for another CI error. So now everything is passing.

ids1024 avatar Oct 16 '25 19:10 ids1024

I haven't investigated this yet myself, but CI is failing on a missing SO_PEERPIDFD on nightly.

sunfishcode avatar Nov 09 '25 15:11 sunfishcode

I guess the last time I rebased this I did it on a different computer without pulling the latest version of the branch; so it lacked a fix to not build enable the feature on Android.

Presumably Android should support this too, but that will require a PR to libc to add the necessary constants.

This should be mergeable now.

ids1024 avatar Nov 14 '25 20:11 ids1024