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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
pub use crate::data::{CmdChain, BasicCmd, CmdChainBuilder, BasicCmdBuilder, Builder, ProcessState};
pub use crate::pipe::Pipe;
mod libc_util;
mod data;
mod pipe;
pub fn execute_piped_cmd_chain(cmds: &CmdChain) -> Vec<ProcessState> {
let mut pids: Vec<libc::pid_t> = vec![];
let mut pipe_to_current: Option<Pipe> = Option::None;
let mut pipe_to_next: Option<Pipe> = Option::None;
for i in 0..cmds.length() {
let cmd = &cmds.cmds()[i];
if pipe_to_next.is_some() {
pipe_to_current.replace(
pipe_to_next.take().unwrap()
);
}
if !cmd.is_last() {
pipe_to_next.replace(Pipe::new());
}
let pid = unsafe { libc::fork() };
if pid == -1 {
panic!("Fork failed! {}", errno::errno());
}
if pid > 0 {
pids.push(pid);
if pipe_to_current.is_some() {
pipe_to_current.as_mut().unwrap().parent_close_all();
}
}
else {
if cmd.is_first() && cmd.in_red_path().is_some() {
initial_ir(cmd);
}
if cmd.is_last() && cmd.out_red_path().is_some() {
final_or(cmd);
}
if pipe_to_current.is_some() {
pipe_to_current.as_mut().unwrap().as_read_end();
}
if pipe_to_next.is_some() {
pipe_to_next.as_mut().unwrap().as_write_end();
}
let _res = unsafe {
libc::execvp(
cmd.executable_cstring().as_ptr(),
cmd.args_to_c_argv()
)
};
panic!("Exec failed! {}", errno::errno());
}
}
let mut i = 0;
let mut process_states: Vec<ProcessState> = pids.into_iter()
.map(|pid| {
let executable = cmds.cmds()[i].executable().to_owned();
i += 1;
ProcessState::new(executable, pid)
})
.collect();
update_process_states(&mut process_states, cmds.background());
process_states
}
pub fn update_process_states(states: &mut Vec<ProcessState>, wnohang: bool) -> bool {
let wait_flags: libc::c_int = if wnohang { libc::WNOHANG } else { 0 };
let mut all_finished = true;
states.into_iter()
.filter(|state| !state.finished())
.for_each(|state| {
let mut status_code: libc::c_int = 0;
let status_code_ptr = &mut status_code as * mut libc::c_int;
let res = unsafe { libc::waitpid(state.pid(), status_code_ptr, wait_flags) };
let exited_normally: bool = unsafe { libc::WIFEXITED(status_code) };
if wait_flags == libc::WNOHANG && res == 0 {
all_finished = false;
} else if res == -1 {
panic!("Failure during waitpid! {}", errno::errno());
} else {
if !exited_normally {
eprintln!("Process did not exited normally! {:#?}", state);
}
let exit_code: libc::c_int = unsafe { libc::WEXITSTATUS(status_code) };
state.finish(exit_code);
println!("Process {} finished with status code {}", state.pid(), status_code);
}
});
all_finished
}
fn initial_ir(cmd: &BasicCmd) {
let fd = unsafe {
libc::open(
cmd.in_red_path_cstring().unwrap().as_ptr(),
libc::O_RDONLY,
)
};
if fd == -1 {
panic!("Input redirect path {} can't be opened/read! {}", cmd.in_red_path().as_ref().unwrap(), errno::errno());
}
let ret = unsafe { libc::dup2(fd, libc::STDIN_FILENO) };
if ret == -1 {
panic!("Error dup2() input redirect! {}", errno::errno());
}
}
fn final_or(cmd: &BasicCmd) {
let fd = unsafe {
let file = libc::fopen(
cmd.out_red_path_cstring().unwrap().as_ptr(),
"w".as_ptr() as * const i8
);
libc::fileno(file)
};
if fd == -1 {
panic!("Output redirect path {} can't be opened/written! {}", cmd.out_red_path().as_ref().unwrap(), errno::errno());
}
let ret = unsafe { libc::dup2(fd, libc::STDOUT_FILENO) };
if ret == -1 {
panic!("Error dup2() output redirect! {}", errno::errno());
}
}
#[cfg(test)]
mod tests {
use crate::data::{CmdChainBuilder, BasicCmdBuilder, Builder};
use crate::execute_piped_cmd_chain;
#[test]
fn test_execute_chain() {
let cmd_chain = CmdChainBuilder::new()
.add_cmd(
BasicCmdBuilder::new()
.set_executable("echo")
.add_arg("echo")
.add_arg("Hallo\nAbc\n123\nAbc123")
).add_cmd(
BasicCmdBuilder::new()
.set_executable("grep")
.add_arg("grep")
.add_arg("-i")
.add_arg("abc"))
.add_cmd(
BasicCmdBuilder::new()
.set_executable("wc")
.add_arg("wc")
.add_arg("-l")
).build();
execute_piped_cmd_chain(&cmd_chain);
}
}