Crate process_control

source ·
Expand description

This crate allows running a process with resource limits, such as a running time, and the option to terminate it automatically afterward. The latter is surprisingly difficult to achieve on Unix, since process identifiers can be arbitrarily reassigned when no longer used. Thus, it would be extremely easy to inadvertently terminate an unexpected process. This crate protects against that possibility.

Methods for setting limits are available on ChildExt, which is implemented for Child. They each return a builder of options to configure how the limit should be applied.

This crate should not be used for security. There are many ways that a process can bypass resource limits. The limits are only intended for simple restriction of harmless processes.

§Features

These features are optional and can be enabled or disabled in a “Cargo.toml” file.

§Optional Features

  • parking_lot - Changes the implementation to use crate parking_lot on targets missing some syscalls. This feature will reduce the likelihood of resource starvation for those targets.

§Implementation

All traits are sealed, meaning that they can only be implemented by this crate. Otherwise, backward compatibility would be more difficult to maintain for new features.

§Comparable Crates

  • wait-timeout - Made for a related purpose but does not provide the same functionality. Processes cannot be terminated automatically, and there is no counterpart of ChildExt::controlled_with_output to read output while setting a timeout. This crate aims to fill in those gaps and simplify the implementation, now that Receiver::recv_timeout exists.

§Examples

use std::io;
use std::process::Command;
use std::process::Stdio;
use std::time::Duration;

use process_control::ChildExt;
use process_control::Control;

let message = "hello world";
let process = Command::new("echo")
    .arg(message)
    .stdout(Stdio::piped())
    .spawn()?;

let output = process
    .controlled_with_output()
    .time_limit(Duration::from_secs(1))
    .terminate_for_timeout()
    .wait()?
    .ok_or_else(|| {
        io::Error::new(io::ErrorKind::TimedOut, "Process timed out")
    })?;
assert!(output.status.success());
assert_eq!(message.as_bytes(), &output.stdout[..message.len()]);

Structs§

Traits§

  • Extensions to Child for easily terminating processes.
  • A temporary wrapper for process limits.