Skip to content

Add socket_peerpidfd #1474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/backend/libc/net/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::ext::{in6_addr_new, in_addr_new};
use crate::backend::c;
use crate::backend::conv::{borrowed_fd, ret};
use crate::fd::BorrowedFd;
use crate::fd::{BorrowedFd, FromRawFd, OwnedFd, RawFd};
#[cfg(feature = "alloc")]
#[cfg(any(
linux_like,
Expand Down Expand Up @@ -1060,6 +1060,13 @@ pub(crate) fn socket_peercred(fd: BorrowedFd<'_>) -> io::Result<UCred> {
getsockopt(fd, c::SOL_SOCKET, c::SO_PEERCRED)
}

#[cfg(linux_kernel)]
#[inline]
pub(crate) fn socket_peerpidfd(fd: BorrowedFd<'_>) -> io::Result<OwnedFd> {
let raw = getsockopt::<RawFd>(fd, c::SOL_SOCKET, c::SO_PEERPIDFD)?;
Ok(unsafe { OwnedFd::from_raw_fd(raw) })
}

#[cfg(target_os = "linux")]
#[inline]
pub(crate) fn set_xdp_umem_reg(fd: BorrowedFd<'_>, value: XdpUmemReg) -> io::Result<()> {
Expand Down
8 changes: 7 additions & 1 deletion src/backend/linux_raw/net/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use crate::backend::c;
use crate::backend::conv::{by_mut, c_uint, ret, socklen_t};
use crate::fd::BorrowedFd;
use crate::fd::{BorrowedFd, FromRawFd, OwnedFd, RawFd};
#[cfg(feature = "alloc")]
use crate::ffi::CStr;
use crate::io;
Expand Down Expand Up @@ -848,6 +848,12 @@ pub(crate) fn socket_peercred(fd: BorrowedFd<'_>) -> io::Result<UCred> {
getsockopt(fd, c::SOL_SOCKET, linux_raw_sys::net::SO_PEERCRED)
}

#[inline]
pub(crate) fn socket_peerpidfd(fd: BorrowedFd<'_>) -> io::Result<OwnedFd> {
let raw = getsockopt::<RawFd>(fd, c::SOL_SOCKET, linux_raw_sys::net::SO_PEERPIDFD)?;
Ok(unsafe { OwnedFd::from_raw_fd(raw) })
}

#[cfg(target_os = "linux")]
#[inline]
pub(crate) fn set_xdp_umem_reg(fd: BorrowedFd<'_>, value: XdpUmemReg) -> io::Result<()> {
Expand Down
11 changes: 10 additions & 1 deletion src/net/sockopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ use crate::{backend, io};
))]
use alloc::string::String;
use backend::c;
use backend::fd::AsFd;
use backend::fd::{AsFd, OwnedFd};
use core::time::Duration;

/// Timeout identifier for use with [`set_socket_timeout`] and
Expand Down Expand Up @@ -1536,6 +1536,15 @@ pub fn socket_peercred<Fd: AsFd>(fd: Fd) -> io::Result<super::UCred> {
backend::net::sockopt::socket_peercred(fd.as_fd())
}

///`getsockopt(fd, SOL_SOCKET, SO_PEERPIDFD)`—Get pidfd of Unix domain peer
///
/// Added in Linux 6.5.
#[cfg(linux_kernel)]
#[doc(alias = "SO_PEERPIDFD")]
pub fn socket_peerpidfd<Fd: AsFd>(fd: Fd) -> io::Result<OwnedFd> {
backend::net::sockopt::socket_peerpidfd(fd.as_fd())
}

/// `setsockopt(fd, SOL_XDP, XDP_UMEM_REG, value)`
///
/// On kernel versions only supporting v1, the flags are ignored.
Expand Down
2 changes: 2 additions & 0 deletions tests/event/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ fn test_select_with_pipes() {
#[serial] // for `setrlimit` usage
fn test_select_with_great_fds() {
use core::cmp::max;
use rustix::fd::{FromRawFd as _, OwnedFd};
use rustix::io::{read, write};
use rustix::pipe::pipe;
use rustix::process::{getrlimit, setrlimit, Resource};
Expand Down Expand Up @@ -277,6 +278,7 @@ fn test_select_with_sockets() {
#[test]
#[serial] // for `setrlimit` usage, and `crate::init`
fn test_select_with_maxfd_sockets() {
use rustix::fd::{FromRawFd as _, OwnedFd};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious why these lines were needed in the tests, that weren't otherwise changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That fixes a compile error with cargo test --features net,process,event. Though it also works to use --features=all-apis like CI uses. (I suppose ideally it shouldn't error for any combination of features; but doesn't matter that much for tests.)

use rustix::net::{recv, send, AddressFamily, RecvFlags, SendFlags, SocketType};
use rustix::process::{getrlimit, setrlimit, Resource};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
Expand Down
23 changes: 23 additions & 0 deletions tests/net/unix_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,29 @@ fn test_unix_peercred() {
};
}

#[cfg(all(feature = "process", feature = "net", linux_kernel))]
#[test]
fn test_unix_peerpidfd() {
use rustix::net::{sockopt, AddressFamily, SocketFlags, SocketType};
use rustix::process::PidfdFlags;

let (send_sock, _recv_sock) = rustix::net::socketpair(
AddressFamily::UNIX,
SocketType::STREAM,
SocketFlags::CLOEXEC,
None,
)
.unwrap();
let pidfd = sockopt::socket_peerpidfd(&send_sock).unwrap();
let own_pidfd =
rustix::process::pidfd_open(rustix::process::getpid(), PidfdFlags::empty()).unwrap();
// Two pidfds refer to the same process iff their `st_ino` values are the same.
assert_eq!(
rustix::fs::fstat(pidfd).unwrap().st_ino,
rustix::fs::fstat(own_pidfd).unwrap().st_ino
)
}

/// Like `test_unix_msg_with_scm_rights`, but with multiple file descriptors
/// over multiple control messages.
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
Expand Down
Loading