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
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};
pub fn exec(executable: &str, args: Vec<&str>) -> Result<(), UECOError> {
let executable = CString::new(executable).expect("Executable must not contain null!");
let executable = executable.as_c_str();
let args = args
.iter()
.map(|s| CString::new(*s).expect("Arg not contain null!"))
.collect::<Vec<CString>>();
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
}
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
}
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();
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)
}
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();
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)
}