All Projects → joshtriplett → async-pidfd

joshtriplett / async-pidfd

Licence: other
Rust crate to use process file descriptors (pidfd) for Linux

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to async-pidfd

yona
Yona is a modern take on a dynamic general-purpose programming language with advanced functional programming, minimalistic ML-like syntax, strict evaluation, for GraalVM polyglot virtual machine (VM).
Stars: ✭ 113 (+169.05%)
Mutual labels:  asynchronous
memsocket
An asynchronous in-memory socket-like interface for Rust
Stars: ✭ 34 (-19.05%)
Mutual labels:  asynchronous
drone-cortexm
ARM® Cortex®-M platform crate for Drone, an Embedded Operating System.
Stars: ✭ 31 (-26.19%)
Mutual labels:  asynchronous
Post-Mortems-Template
An Incident Management Process / Post Mortem Template
Stars: ✭ 23 (-45.24%)
Mutual labels:  process
firetrap
This project is no longer maintained. Check out the fork (lib)unFTP instead.
Stars: ✭ 15 (-64.29%)
Mutual labels:  asynchronous
tgrid
TypeScript Grid Computing Framework supporting RFC (Remote Function Call)
Stars: ✭ 83 (+97.62%)
Mutual labels:  process
reactools
Create React interfaces is easy.
Stars: ✭ 14 (-66.67%)
Mutual labels:  asynchronous
AutoOED
AutoOED: Automated Optimal Experimental Design Platform
Stars: ✭ 87 (+107.14%)
Mutual labels:  asynchronous
SockNet
The easiest and fastest way to work with sockets in C#
Stars: ✭ 42 (+0%)
Mutual labels:  asynchronous
codebase
The jBPT code library is a compendium of technologies that support research on design, execution, and evaluation of business processes. The library offers a broad range of basis analysis and utility functionality and, due to its open publishing model, can easily be extended.
Stars: ✭ 26 (-38.1%)
Mutual labels:  process
kill-process
Bash script to kill high CPU process, long running process and too much consuming memory process.
Stars: ✭ 58 (+38.1%)
Mutual labels:  process
DLL-Injector
Inject and detour DLLs and program functions both managed and unmanaged in other programs, written (almost) purely in C#. [Not maintained].
Stars: ✭ 29 (-30.95%)
Mutual labels:  process
PRUNE
Logs key Windows process performance metrics. #nsacyber
Stars: ✭ 56 (+33.33%)
Mutual labels:  process
proc-maps
Read virtual memory maps from another process
Stars: ✭ 25 (-40.48%)
Mutual labels:  process
Process
Creation of Dynamic Dedicated WebWorkers, definition of dependencies, promise support.
Stars: ✭ 13 (-69.05%)
Mutual labels:  process
prox
A Scala library for working with system processes
Stars: ✭ 93 (+121.43%)
Mutual labels:  process
RepositoryHelpers
📦 Extensions for HttpClient and Custom Repository based on dapper
Stars: ✭ 22 (-47.62%)
Mutual labels:  asynchronous
ExecutionMaster
Windows utility for intercepting process creation and assigning standard actions to program startup
Stars: ✭ 54 (+28.57%)
Mutual labels:  process
futura
Asynchronous Swift made easy. The project was made by Miquido. https://www.miquido.com/
Stars: ✭ 34 (-19.05%)
Mutual labels:  asynchronous
fake-sandbox
👁‍🗨 This script will simulate fake processes of analysis sandbox/VM software that some malware will try to avoid.
Stars: ✭ 110 (+161.9%)
Mutual labels:  process

Process file descriptors (pidfd) for Linux

Process file descriptors (pidfd) provide a race-free way to manage processes on Linux, maintaining a persistent reference to a process using a file descriptor rather than a numeric process ID (PID) that could be reused after the process exits.

This crate only works on Linux; if you need support for other platforms, or for older Linux kernels, see async-process.

async-pidfd provides Rust support for pidfd, and supports managing processes both synchronously (via the PidFd type) and asynchronously (via the AsyncPidFd type).

Sync - PidFd

The PidFd type manages processes synchronously. Use PidFd::from_pid to construct a PidFd from a process ID, such as from Child::id in the standard library. (Note that the portable Child::id function returns process IDs as u32, rather than as a libc::pid_t, necessitating a cast.)

use std::os::unix::process::ExitStatusExt;
use std::process::{Command, ExitStatus};

use async_pidfd::PidFd;

fn main() -> std::io::Result<()> {
    let child = Command::new("/bin/true").spawn()?;
    let pidfd = PidFd::from_pid(child.id() as libc::pid_t)?;
    let status = pidfd.wait()?.status();
    assert_eq!(status.code(), Some(0));

    let child = Command::new("/bin/sh").arg("-c").arg("kill -9 $$").spawn()?;
    let pidfd = PidFd::from_pid(child.id() as libc::pid_t)?;
    let status = pidfd.wait()?.status();
    assert_eq!(status.signal(), Some(9));

    Ok(())
}

PidFd::wait returns information about an exited process via the ExitInfo structure. ExitInfo includes a libc::siginfo_t indicating how the process exited (including the exit code if it exited normally, or the signal if it was killed by a signal), and a libc::rusage describing the resource usage of the process and its children. libc::siginfo_t has complex semantics; to get a std::process::ExitStatus instead, you can call .status() on an ExitInfo.

Note that while opening the PID for an arbitrary process can potentially race with the exit of that process, opening the PID for a child process that you have not yet waited on is safe, as the process ID will not get reused until you wait on the process (or block SIGCHLD).

If you only want to use the synchronous PidFd type, you can use async-pidfd with default-features = false in Cargo.toml to remove async-related dependencies.

Async - AsyncPidFd

The AsyncPidFd type manages processes asynchronously, based on the async-io crate by Stjepan Glavina. async-io provides an Async wrapper that makes it easy to turn any synchronous type based on a file descriptor into an asynchronous type; the resulting asynchronous code uses epoll to wait for all the file descriptors concurrently.

AsyncPidFd wraps an Async<PidFd> and provides the same API as PidFd, but with an async version of the wait function.

use std::os::unix::process::ExitStatusExt;
use std::process::{Command, ExitStatus};

use async_pidfd::AsyncPidFd;
use futures_lite::future;

async fn async_spawn_and_status(cmd: &mut Command) -> std::io::Result<ExitStatus> {
    let child = cmd.spawn()?;
    let pidfd = AsyncPidFd::from_pid(child.id() as libc::pid_t)?;
    Ok(pidfd.wait().await?.status())
}

fn main() -> std::io::Result<()> {
    future::block_on(async {
        let (status1, status2) = future::try_join(
            async_spawn_and_status(&mut Command::new("/bin/true")),
            async_spawn_and_status(&mut Command::new("/bin/false")),
        )
        .await?;
        assert_eq!(status1.code(), Some(0));
        assert_eq!(status2.code(), Some(1));
        Ok(())
    })
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].