Struct PidFd

Source
pub struct PidFd { /* private fields */ }
Expand description

PidFd for async and efficient method of reaping children process and race-free signal sending.

§Example

use libc::fork;
use async_linux_spec_fd::*;

#[tokio::main(flavor = "current_thread")]
async fn f() {
    let pid = unsafe { fork() };
    assert!(pid >= 0);
    if pid != 0 { // parent
        let pidfd = PidFd::open(pid).unwrap();
        let exitinfo = pidfd.waitpid().await.unwrap();

        match exitinfo.get_code() {
            ExitCode::Killed(_) => panic!("Children killed by signal!"),
            ExitCode::Exited(code) => assert_eq!(code, 0),
        }
    }
}

f();

Implementations§

Source§

impl PidFd

Source

pub fn open(pid: pid_t) -> Result<Self>

§Creating PidFd from the pid of children

Make sure that:

  • the disposition of SIGCHLD has not been explicitly set to SIG_IGN;
  • the SA_NOCLDWAIT flag was not specified while establishing a handler for SIGCHLD or while setting the disposition of that signal to SIG_DFL;
  • the zombie process was not reaped elsewhere in the program (e.g., either by an asynchronously executed signal handler or by wait(2) or similar in another thread).

If any of these conditions does not hold, then the child process (along with a PID file descriptor that refers to it) should instead be created using clone with the CLONE_PIDFD flag and uses the from_raw function to create PidFd.

§Creating PidFd from arbitary pid

Make sure to verify that the process pointed to by this pid is the one you want.

Source

pub const unsafe fn from_raw(fd: RawFd) -> Self

§Safety

Make sure fd is actually created via clone with the CLONE_PIDFD flag or by using pidfd_open.

Source

pub fn send_signal( &self, signal: Signal, info: Option<&siginfo_t>, ) -> Result<()>

  • self - The calling process must either be in the same PID namespace as the process referred to by self, or be in an ancestor of that namespace.
  • info - If equals to Some(buffer), then buffer should be populated as described in rt_sigqueueinfo. Or, it is equivalent to specifing to a buffer whose fields are implicily filled in as follows:
    • si_signo is set to the signal;
    • si_errno is set to 0;
    • si_code is set to SI_USER;
    • si_pid is set to the caller’s PID;
    • si_uid is set to the caller’s real user ID.
Source

pub async fn wait_for_terminate(&self) -> Result<()>

Asynchronously wait for the process to terminate.

Source

pub async fn waitpid(&self) -> Result<ExitInfo>

Asynchronously wait for the child process to terminate and reap it using waitid.

Auto Trait Implementations§

§

impl Freeze for PidFd

§

impl RefUnwindSafe for PidFd

§

impl Send for PidFd

§

impl Sync for PidFd

§

impl Unpin for PidFd

§

impl UnwindSafe for PidFd

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.