Skip to content
Myr

Unify execute_command across platforms

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Matthias Reitinger · committed by David Peter · parent 51aea57 · Browse files · 1 file+3 added−73 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+3 added−73 removed

  • other:+3 added−73 removed

1 file+3 added−73 removed

modified:src/exec/command.rs+3 added-73 removed
7 unmodified lines
89use std::process::Command;10use std::sync::{Arc, Mutex};11use std::io;12use std::io::Write;1314/// 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;1816 // Spawn the supplied command.20 let output = cmd.stdout(Stdio::piped()).stderr(Stdio::piped()).output();17 let output = cmd.output();1819 // Then wait for the command to exit, if it was spawned.20 match output {21 Ok(output) => {22 // While this lock is active, this thread will be the only thread allowed26 // to write it's outputs.23 // to write its outputs.24 let _lock = out_perm.lock().unwrap();2526 let stdout = io::stdout();27 let stderr = io::stderr();
9 unmodified lines
37 }38 }39 }40}4445/// Executes a command.46#[cfg(all(unix))]47pub fn execute_command(mut cmd: Command, out_perm: Arc<Mutex<()>>) {48 use libc::{close, dup2, pipe, STDERR_FILENO, STDOUT_FILENO};49 use std::fs::File;50 use std::os::unix::process::CommandExt;51 use std::os::unix::io::FromRawFd;5253 // Initial a pair of pipes that will be used to54 // pipe the std{out,err} of the spawned process.55 let mut stdout_fds = [0; 2];56 let mut stderr_fds = [0; 2];5758 unsafe {59 pipe(stdout_fds.as_mut_ptr());60 pipe(stderr_fds.as_mut_ptr());61 }6263 // Configure the pipes accordingly in the child.64 let child = cmd.before_exec(move || unsafe {65 // Redirect the child's std{out,err} to the write ends of our pipe.66 dup2(stdout_fds[1], STDOUT_FILENO);67 dup2(stderr_fds[1], STDERR_FILENO);6869 // Close all the fds we created here, so EOF will be sent when the program exits.70 close(stdout_fds[0]);71 close(stdout_fds[1]);72 close(stderr_fds[0]);73 close(stderr_fds[1]);74 Ok(())75 }).spawn();7677 // Open the read end of the pipes as `File`s.78 let (mut pout, mut perr) = unsafe {79 // Close the write ends of the pipes in the parent80 close(stdout_fds[1]);81 close(stderr_fds[1]);82 (83 // But create files from the read ends.84 File::from_raw_fd(stdout_fds[0]),85 File::from_raw_fd(stderr_fds[0]),86 )87 };8889 match child {90 Ok(mut child) => {91 let _ = child.wait();9293 // Create a lock to ensure that this thread has exclusive access to writing.94 let _lock = out_perm.lock().unwrap();9596 // And then write the outputs of the process until EOF is sent to each file.97 let stdout = io::stdout();98 let stderr = io::stderr();99 let _ = io::copy(&mut pout, &mut stdout.lock());100 let _ = io::copy(&mut perr, &mut stderr.lock());101 }102 Err(why) => {103 if why.kind() == io::ErrorKind::NotFound {104 eprintln!("fd: execution error: command not found");105 } else {106 eprintln!("fd: execution error: {}", why);107 }108 }109 }110}