#child-process #sub-processes #fd #command

command-fds

A library for passing arbitrary file descriptors when spawning child processes

7 releases

Uses new Rust 2024

new 0.3.1 May 8, 2025
0.3.0 Dec 12, 2023
0.2.3 Oct 31, 2023
0.2.2 Apr 1, 2022
0.1.0 May 4, 2021

#63 in Unix APIs

Download history 13561/week @ 2025-01-19 15459/week @ 2025-01-26 16768/week @ 2025-02-02 19695/week @ 2025-02-09 14663/week @ 2025-02-16 25968/week @ 2025-02-23 17925/week @ 2025-03-02 26612/week @ 2025-03-09 18910/week @ 2025-03-16 17989/week @ 2025-03-23 17077/week @ 2025-03-30 21632/week @ 2025-04-06 32328/week @ 2025-04-13 47334/week @ 2025-04-20 26588/week @ 2025-04-27 11960/week @ 2025-05-04

118,673 downloads per month
Used in 22 crates (11 directly)

Apache-2.0

19KB
290 lines

command-fds

crates.io page docs.rs page

A library for passing arbitrary file descriptors when spawning child processes.

Example

use command_fds::{CommandFdExt, FdMapping};
use std::fs::File;
use std::io::stdin;
use std::os::fd::AsFd;
use std::os::unix::io::AsRawFd;
use std::process::Command;

// Open a file.
let file = File::open("Cargo.toml").unwrap();

// Prepare to run `ls -l /proc/self/fd` with some FDs mapped.
let mut command = Command::new("ls");
command.arg("-l").arg("/proc/self/fd");
command
    .fd_mappings(vec![
        // Map `file` as FD 3 in the child process.
        FdMapping {
            parent_fd: file.into(),
            child_fd: 3,
        },
        // Map this process's stdin as FD 5 in the child process.
        FdMapping {
            parent_fd: stdin().as_fd().try_clone_to_owned().unwrap(),
            child_fd: 5,
        },
    ])
    .unwrap();

// Spawn the child process.
let mut child = command.spawn().unwrap();
child.wait().unwrap();

License

Licensed under the Apache License, Version 2.0.

Contributing

If you want to contribute to the project, see details of how we accept contributions.

Dependencies

~2–11MB
~113K SLoC