Skip to content

Tracking Issue for linux_pidfdย #82971

Description

@voidc

View all comments

Feature gate: #![feature(linux_pidfd)]

This is a tracking issue for Linux-specific extension methods allowing to obtain process file descriptors for processes spawned with the standard Command API.

Public API

// std::os::linux::process

pub struct PidFd;

impl PidFd {
   pub fn kill(&self) -> Result<()> {...}
   pub fn wait(&self) -> Result<ExitStatus> {...}
   pub fn try_wait(&self) -> Result<Option<ExitStatus>> {...}
}

impl AsRawFd for PidFd {}
impl FromRawFd for PidFd {}
impl IntoRawFd for PidFd {}

// sealed trait, implemented for std::process::Child
pub trait ChildExt {
    fn pidfd(&self) -> Result<&PidFd>;
    fn into_pidfd(self) -> Result<PidFd, Self>;
}

// sealed trait, implemented for std::process::Command
pub trait CommandExt {
    fn create_pidfd(&mut self, val: bool) -> &mut process::Command;
}

Steps / History

Unresolved Questions

  • How can we properly open a pidfd for the new process? The current implementation using a manual clone3 means we can't safely call libc in the child: cargo 1.56 beta hang when run inside Gentoo's sandboxย #89522 (comment)
    • pidfd_open may work, but it has conditions on avoiding pid-recycling races.
  • should Child::pidfd(&self) be removed? It can lead to Child::wait returning errors instead of a saved exit status if PidFd::wait obtains the exit status first, which may be surprising behavior.

Activity

  1. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    T-libs-api[DEPRECATED; DO NOT USE]
    on Mar 10, 2021
  2. added a commit that references this issue on Mar 14, 2021
  3. the8472 commented on Mar 16, 2021

    @the8472
    Member

    The PR adds support for obtaining PidFds, but you can't actually do anything with them. Do we want additional methods on PidFd in std to wait, send signals, obtain the /proc directory etc. or should that be left to 3rd party crates?

  4. added 3 commits that reference this issue on Mar 18, 2021
  5. added a commit that references this issue on Mar 26, 2021
  6. 52 remaining items

  7. the8472 commented on Jan 5, 2026

    @the8472
    Member

    With #150412 I think we should be fairly close to be able to stabilize this for linux.

    But I recall that FreeBSD has a similar but more limited API. CC @asomers @MikaelUrankar
    Would it make sense to expand this to a common linux + freebsd feature?
    I'm not seeing a way to use posix_spawn while still obtaining one of those FDs. And no way to get the exit status from an fd.

  8. brauner commented on Jan 5, 2026

    @brauner
  9. the8472 commented on Jan 5, 2026

    @the8472
  10. asomers commented on Jan 5, 2026

    @asomers
    Contributor

    Sorry, I've not been in-the-loop on this feature. But regarding your questions, @the8472 :

    • It's not currently possible on FreeBSD to spawn a new process and get a pidfd without cloning the process's virtual address space. That is, you must do pdfork + exec. There's nothing like pdposix_spawn. Using pdfork + exec works, but it's a little less efficient.
    • There is indeed a way to wait for a child based on its pidfd. But instead of using wait4, you must use kevent with EVFILT_PROCDESC. See https://man.freebsd.org/cgi/man.cgi?kevent(2).
  11. added a commit that references this issue on Jan 5, 2026
  12. added a commit that references this issue on Jan 5, 2026
  13. added a commit that references this issue on Jan 6, 2026
  14. added 2 commits that reference this issue on Jan 6, 2026
  15. the8472 commented on Jan 6, 2026

    @the8472
    Member

    We talked about this in today's libs-api meeting. We'd be interested in having this on FreeBSD under std::os::freebsd::process too if possible so that it's a N=2 API to inform portability.

    @asomers @MikaelUrankar would you be interested in implementing it?

  16. added a commit that references this issue on Jan 6, 2026
  17. asomers commented on Jan 7, 2026

    @asomers
    Contributor

    I'll give it a shot, @the8472 . And BTW, I realized that it's not necessary to use kevent like I originally thought. The wait-like functionality can be provided by using poll or select on the process file descriptor.

  18. Zorbatron commented on Apr 15, 2026

    @Zorbatron

    How come PidFd::kill is hardcoded to use SIGKILL/PidFd::send_signal isn't public? I think it would be more valuable if you were able to send any signal (or at least valid ones from an enum).

  19. added
    T-libsRelevant to the library team, which will review and decide on the PR/issue.
    and removed
    T-libs-api[DEPRECATED; DO NOT USE]
    on Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCF-linux_pidfd`#![feature(linux_pidfd)]`T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions