Skip to content
Myr

command.rs

This file

Path

src/exec/command.rs

98 lines · 3.2 KiB · Rust

Last changed by

Remove CommandTicket to avoid Vec allocationMatthias Reitingerbefore adoptionc18caaa

History of this file

Take it

1// Copyright (c) 2017 fd developers2// Licensed under the Apache License, Version 2.03// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>4// or the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>,5// at your option. All files in the project carrying such6// notice may not be copied, modified, or distributed except7// according to those terms.89use std::process::Command;10use std::sync::{Arc, Mutex};11use std::io;1213/// Executes a command.14#[cfg(not(unix))]15pub fn execute_command(mut cmd: Command, out_perm: Arc<Mutex<()>>) {16    use std::process::Stdio;17    use std::io::Write;1819    // Spawn the supplied command.20    let output = cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).output();2122    // Then wait for the command to exit, if it was spawned.23    match output {24        Ok(output) => {25            // While this lock is active, this thread will be the only thread allowed26            // to write it's outputs.27            let _lock = out_perm.lock().unwrap();2829            let stdout = io::stdout();30            let stderr = io::stderr();3132            let _ = stdout.lock().write_all(&output.stdout);33            let _ = stderr.lock().write_all(&output.stderr);34        }35        Err(why) => eprintln!("fd: exec error: {}", why),36    }37}3839/// Executes a command.40#[cfg(all(unix))]41pub fn execute_command(mut cmd: Command, out_perm: Arc<Mutex<()>>) {42    use libc::{close, dup2, pipe, STDERR_FILENO, STDOUT_FILENO};43    use std::fs::File;44    use std::os::unix::process::CommandExt;45    use std::os::unix::io::FromRawFd;4647    // Initial a pair of pipes that will be used to48    // pipe the std{out,err} of the spawned process.49    let mut stdout_fds = [0; 2];50    let mut stderr_fds = [0; 2];5152    unsafe {53        pipe(stdout_fds.as_mut_ptr());54        pipe(stderr_fds.as_mut_ptr());55    }5657    // Configure the pipes accordingly in the child.58    let child = cmd.before_exec(move || unsafe {59        // Redirect the child's std{out,err} to the write ends of our pipe.60        dup2(stdout_fds[1], STDOUT_FILENO);61        dup2(stderr_fds[1], STDERR_FILENO);6263        // Close all the fds we created here, so EOF will be sent when the program exits.64        close(stdout_fds[0]);65        close(stdout_fds[1]);66        close(stderr_fds[0]);67        close(stderr_fds[1]);68        Ok(())69    }).spawn();7071    // Open the read end of the pipes as `File`s.72    let (mut pout, mut perr) = unsafe {73        // Close the write ends of the pipes in the parent74        close(stdout_fds[1]);75        close(stderr_fds[1]);76        (77            // But create files from the read ends.78            File::from_raw_fd(stdout_fds[0]),79            File::from_raw_fd(stderr_fds[0]),80        )81    };8283    match child {84        Ok(mut child) => {85            let _ = child.wait();8687            // Create a lock to ensure that this thread has exclusive access to writing.88            let _lock = self.out_perm.lock().unwrap();8990            // And then write the outputs of the process until EOF is sent to each file.91            let stdout = io::stdout();92            let stderr = io::stderr();93            let _ = io::copy(&mut pout, &mut stdout.lock());94            let _ = io::copy(&mut perr, &mut stderr.lock());95        }96        Err(why) => eprintln!("fd: exec error: {}", why),97    }98}