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
impl PidFd
Sourcepub fn open(pid: pid_t) -> Result<Self>
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 toSIG_IGN
; - the
SA_NOCLDWAIT
flag was not specified while establishing a handler forSIGCHLD
or while setting the disposition of that signal toSIG_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.
Sourcepub const unsafe fn from_raw(fd: RawFd) -> Self
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
.
Sourcepub fn send_signal(
&self,
signal: Signal,
info: Option<&siginfo_t>,
) -> Result<()>
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 byself
, or be in an ancestor of that namespace.info
- If equals toSome(buffer)
, thenbuffer
should be populated as described inrt_sigqueueinfo
. Or, it is equivalent to specifing to a buffer whose fields are implicily filled in as follows:si_signo
is set to thesignal
;si_errno
is set to0
;si_code
is set toSI_USER
;si_pid
is set to the caller’s PID;si_uid
is set to the caller’s real user ID.
Sourcepub async fn wait_for_terminate(&self) -> Result<()>
pub async fn wait_for_terminate(&self) -> Result<()>
Asynchronously wait for the process to terminate.
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more