#child-process #guard #termination #ensure #ctrl-c #proper #strategies

proc_guard

A process guard that ensures the proper destruction of a child process in various scenarios

2 unstable releases

0.2.0 Jul 30, 2024
0.1.0 Jul 19, 2024

#316 in Operating systems

Download history 26/week @ 2024-09-13 13/week @ 2024-09-20 2/week @ 2024-09-27

70 downloads per month

MIT license

25KB
174 lines

proc_guard

proc_guard is a Rust crate designed to manage and ensure the proper termination of child processes using various termination strategies. This crate provides a guard for a child process that ensures the process is terminated according to a specified strategy when the guard is dropped.

Features

  • Different termination strategies such as waiting, sending Ctrl+C, and killing the process.
  • Ensures the proper cleanup of child processes when the guard goes out of scope.
  • Supports both blocking and timeout-based termination methods.

Installation

Add the following to your Cargo.toml:

[dependencies]
proc_guard = "0.2.0"

Usage

Below are some examples to illustrate how to use the ProcGuard crate.

Example 1: Basic Usage

use std::process::Command;
use proc_guard::{ProcGuard, ProcessTermination};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let child = if cfg!(target_os = "windows") {
        Command::new("timeout").args(["/t", "2"]).spawn()?
    } else {
        Command::new("sleep").arg("2").spawn()?
    };

    let guard = ProcGuard::new(child, ProcessTermination::Wait);
    // The child process will be waited upon until it exits.
    Ok(())
}

Example 2: Using Ctrl+C and Wait

use std::process::Command;
use proc_guard::{ProcGuard, ProcessTermination};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let child = if cfg!(target_os = "windows") {
        Command::new("timeout").args(["/t", "2"]).spawn()?
    } else {
        Command::new("sleep").arg("2").spawn()?
    };

    let guard = ProcGuard::new(child, ProcessTermination::CtrlCWait);
    // The child process will receive a Ctrl+C signal and will be waited upon until it exits.
    Ok(())
}

Example 3: Spawn a New Guarded Process

use std::process::Command;
use proc_guard::{ProcGuard, ProcessTermination};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let guard = if cfg!(target_os = "windows") {
        ProcGuard::spawn(Command::new("timeout").args(["/t", "2"]), ProcessTermination::CtrlCWait)?
    } else {
        ProcGuard::spawn(Command::new("sleep").arg("2"), ProcessTermination::CtrlCWait)?
    };
    // The child process will be managed and terminated as specified.
    Ok(())
}

Example 4: Releasing the Guard

use std::process::Command;
use proc_guard::{ProcGuard, ProcessTermination};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let child = if cfg!(target_os = "windows") {
        Command::new("timeout").args(["/t", "2"]).spawn()?
    } else {
        Command::new("sleep").arg("2").spawn()?
    };

    let guard = ProcGuard::new(child, ProcessTermination::Wait);
    let child = guard.release();
    // The child process is now unmanaged and must be manually handled.
    Ok(())
}

Termination Strategies

The ProcessTermination enum provides various strategies for terminating a process:

  • Wait: Wait indefinitely for the process to exit.
  • WaitTimeout(Duration): Wait for a specified duration for the process to exit.
  • WaitTimeoutKill(Duration): Wait for a specified duration, then kill the process if it hasn't exited.
  • CtrlC: Send a Ctrl+C signal to the process and does not wait.
  • CtrlCWait: Send a Ctrl+C signal and wait indefinitely for the process to exit.
  • CtrlCWaitTimeout(Duration): Send a Ctrl+C signal and wait for a specified duration for the process to exit.
  • CtrlCWaitTimeoutKill(Duration): Send a Ctrl+C signal, wait for a specified duration, and then kill the process if it hasn't exited.
  • Kill: Kill the process immediately and does not wait.
  • KillWait: Kill the process immediately and wait indefinitely for the process to exit.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Dependencies

~0.3–1.2MB
~24K SLoC