1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//! Utility functions for exec.

use crate::child::ChildProcess;
use crate::error::UECOError;
use crate::libc_util::{libc_ret_to_result, LibcSyscall};
use crate::pipe::CatchPipes;
use crate::reader::{OutputReader, SimpleOutputReader, SimultaneousOutputReader};
use crate::OCatchStrategy;
use crate::ProcessOutput;
use std::ffi::CString;
use std::sync::{Arc, Mutex};

/// Wrapper around [`libc::execvp`].
/// * `executable` Path or name of executable without null (\0).
/// * `args` vector of args without null (\0). Remember that the
///          first real arg starts at index 1. index 0 is usually
///          the name of the executable. See:
///          https://unix.stackexchange.com/questions/315812/why-does-argv-include-the-program-name
pub fn exec(executable: &str, args: Vec<&str>) -> Result<(), UECOError> {
    // panics if the string contains a \0 (null)
    let executable = CString::new(executable).expect("Executable must not contain null!");
    let executable = executable.as_c_str();

    // Build array of null terminated C-strings array
    let args = args
        .iter()
        .map(|s| CString::new(*s).expect("Arg not contain null!"))
        .collect::<Vec<CString>>();
    // Build null terminated array with pointers null terminated c-strings
    let mut args_nl = args
        .iter()
        .map(|cs| cs.as_ptr())
        .collect::<Vec<*const libc::c_char>>();
    args_nl.push(std::ptr::null());

    let ret = unsafe { libc::execvp(executable.as_ptr(), args_nl.as_ptr()) };
    let res = libc_ret_to_result(ret, LibcSyscall::Execvp);

    res
}

/// Executes a program in a child process and returns the output of STDOUT and STDERR
/// line by line in a vector. Be aware that this is blocking and static! So if your
/// executable produces 1GB of output text, the data of the vectors of the returned structs
/// is also 1GB in size. If the program doesn't terminate, this function will neither.
///
/// This lib/function works fine for commands like "$ sysctl -a" or "$ ls -la".
///
/// ⚠️ Difference to std::process::Command 🚨
/// `std::process::Command` does the same in the standard library but **with one exception**:
/// My library gives you access to stdout, stderr, **and "stdcombined"**. This way you get all output
/// lines in the order they appeared. That's the unique feature of this crate.
///
///
/// * `executable` Path or name of executable without null (\0). Lookup in $PATH happens automatically.
/// * `args` vector of args, each without null (\0). Remember that the
///          first real arg starts at index 1. index 0 is usually
///          the name of the executable. See:
///          https://unix.stackexchange.com/questions/315812/why-does-argv-include-the-program-name
/// * `strategy` Specify how accurate the `"STDCOMBINED` vecor is. See [`crate::OCatchStrategy`] for
///              more information.
pub fn fork_exec_and_catch(
    executable: &str,
    args: Vec<&str>,
    strategy: OCatchStrategy,
) -> Result<ProcessOutput, UECOError> {
    let cp = CatchPipes::new(strategy)?;
    let child = match strategy {
        OCatchStrategy::StdCombined => setup_and_execute_strategy_combined(executable, args, cp),
        OCatchStrategy::StdSeparately => {
            setup_and_execute_strategy_separately(executable, args, cp)
        }
    };
    let mut child = child?;
    child.dispatch()?;
    let output = match strategy {
        OCatchStrategy::StdCombined => SimpleOutputReader::new(&mut child).read_all_bl(),
        OCatchStrategy::StdSeparately => {
            SimultaneousOutputReader::new(Arc::new(Mutex::new(child))).read_all_bl()
        }
    };
    output
}

/// Setups up parent and child process and executes everything. Obtains the output
/// using the [`crate::OCatchStrategy::StdCombined`]-strategy.
fn setup_and_execute_strategy_combined(
    executable: &str,
    args: Vec<&str>,
    cp: CatchPipes,
) -> Result<ChildProcess, UECOError> {
    let pipe = if let CatchPipes::Combined(pipe) = cp {
        pipe
    } else {
        panic!("Wrong CatchPipe-variant")
    };
    let pipe = Arc::new(Mutex::new(pipe));
    let pipe_closure = pipe.clone();
    // gets called after fork() after
    let child_setup = move || {
        let mut pipe_closure = pipe_closure.lock().unwrap();
        pipe_closure.mark_as_child_process()?;
        pipe_closure.connect_to_stdout()?;
        pipe_closure.connect_to_stderr()?;
        Ok(())
    };
    let pipe_closure = pipe.clone();
    let parent_setup = move || {
        let mut pipe_closure = pipe_closure.lock().unwrap();
        pipe_closure.mark_as_parent_process()?;
        Ok(())
    };
    let child = ChildProcess::new(
        executable,
        args,
        Box::new(child_setup),
        Box::new(parent_setup),
        pipe.clone(),
        pipe,
    );
    Ok(child)
}

/// Setups up parent and child process and executes everything. Obtains the output
/// using the [`crate::OCatchStrategy::StdSeparately`]-strategy.
fn setup_and_execute_strategy_separately(
    executable: &str,
    args: Vec<&str>,
    cp: CatchPipes,
) -> Result<ChildProcess, UECOError> {
    let (stdout_pipe, stderr_pipe) = if let CatchPipes::Separately { stdout, stderr } = cp {
        (stdout, stderr)
    } else {
        panic!("Wrong CatchPipe-variant")
    };
    let stdout_pipe = Arc::new(Mutex::new(stdout_pipe));
    let stderr_pipe = Arc::new(Mutex::new(stderr_pipe));
    let stdout_pipe_closure = stdout_pipe.clone();
    let stderr_pipe_closure = stderr_pipe.clone();
    // gets called after fork() after
    let child_setup = move || {
        let mut stdout_pipe_closure = stdout_pipe_closure.lock().unwrap();
        let mut stderr_pipe_closure = stderr_pipe_closure.lock().unwrap();
        stdout_pipe_closure.mark_as_child_process()?;
        stderr_pipe_closure.mark_as_child_process()?;
        stdout_pipe_closure.connect_to_stdout()?;
        stderr_pipe_closure.connect_to_stderr()?;
        Ok(())
    };
    let stdout_pipe_closure = stdout_pipe.clone();
    let stderr_pipe_closure = stderr_pipe.clone();
    let parent_setup = move || {
        let mut stdout_pipe_closure = stdout_pipe_closure.lock().unwrap();
        let mut stderr_pipe_closure = stderr_pipe_closure.lock().unwrap();
        stdout_pipe_closure.mark_as_parent_process()?;
        stderr_pipe_closure.mark_as_parent_process()?;
        Ok(())
    };
    let child = ChildProcess::new(
        executable,
        args,
        Box::new(child_setup),
        Box::new(parent_setup),
        stdout_pipe,
        stderr_pipe,
    );
    Ok(child)
}