Skip to content
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

feature(process): Use pidfd on Linux for tokio::process::Child::wait #6152

Merged
merged 17 commits into from
Jan 11, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Optimization: Skip pidfd_open if last call failed with ENOSYS
And fixed `test::is_pidfd_available`

Signed-off-by: Jiahao XU <[email protected]>
  • Loading branch information
NobodyXu committed Dec 31, 2023
commit c38facaff58e62d5e5825cedd9e373a712af7ea3
20 changes: 17 additions & 3 deletions tokio/src/process/unix/pidfd_reaper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
runtime::Handle,
};

use libc::{syscall, SYS_pidfd_open, PIDFD_NONBLOCK};
use libc::{syscall, SYS_pidfd_open, __errno_location, ENOSYS, PIDFD_NONBLOCK};
use mio::{event::Source, unix::SourceFd};
use std::{
fs::File,
Expand All @@ -15,6 +15,7 @@ use std::{
os::unix::io::{AsRawFd, FromRawFd, RawFd},
pin::Pin,
process::ExitStatus,
sync::atomic::{AtomicBool, Ordering::Relaxed},
task::{Context, Poll},
};

Expand All @@ -25,9 +26,22 @@ struct Pidfd {

impl Pidfd {
fn open(pid: u32) -> Option<Pidfd> {
// Store false (0) to reduce executable size
static NO_PIDFD_SUPPORT: AtomicBool = AtomicBool::new(false);

if NO_PIDFD_SUPPORT.load(Relaxed) {
return None;
}

unsafe {
let fd = syscall(SYS_pidfd_open, pid, PIDFD_NONBLOCK);
if fd == -1 {
let errno = *__errno_location();

if errno == ENOSYS {
NO_PIDFD_SUPPORT.store(true, Relaxed)
}

None
} else {
Some(Pidfd {
Expand Down Expand Up @@ -196,8 +210,8 @@ mod test {
let stdout = String::from_utf8_lossy(&stdout);

let mut kernel_version_iter = stdout.split_once('-').unwrap().0.split('.');
let major = kernel_version_iter.next().unwrap();
let minor = kernel_version_iter.next().unwrap();
let major: u32 = kernel_version_iter.next().unwrap().parse().unwrap();
let minor: u32 = kernel_version_iter.next().unwrap().parse().unwrap();

major >= 6 || (major == 5 && minor >= 10)
}
Expand Down