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
use std::ffi::OsStr;
use std::default::Default;
use std::collections::HashMap;
use std::env;
use std::path::Path;
use libc::{uid_t, gid_t};
use ffi_util::ToCString;
use {Command, Stdio, Fd};
impl Command {
pub fn new<S: AsRef<OsStr>>(program: S) -> Command {
Command {
filename: program.to_cstring(),
args: vec![program.to_cstring()],
environ: None,
config: Default::default(),
chroot_dir: None,
pivot_root: None,
fds: vec![
(0, Fd::inherit()),
(1, Fd::inherit()),
(2, Fd::inherit()),
].into_iter().collect(),
close_fds: Vec::new(),
id_map_commands: None,
}
}
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command {
self.args.push(arg.to_cstring());
self
}
pub fn args<S: AsRef<OsStr>>(&mut self, args: &[S]) -> &mut Command {
self.args.extend(args.iter().map(ToCString::to_cstring));
self
}
#[doc(hidden)]
pub fn init_env_map(&mut self) {
if self.environ.is_none() {
self.environ = Some(env::vars_os().collect());
}
}
pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
where K: AsRef<OsStr>, V: AsRef<OsStr>
{
self.init_env_map();
self.environ.as_mut().unwrap().insert(
key.as_ref().to_os_string(),
val.as_ref().to_os_string());
self
}
pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command {
self.init_env_map();
self.environ.as_mut().unwrap().remove(key.as_ref());
self
}
pub fn env_clear(&mut self) -> &mut Command {
self.environ = Some(HashMap::new());
self
}
pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Command
{
self.config.work_dir = Some(dir.as_ref().to_cstring());
self
}
pub fn stdin(&mut self, cfg: Stdio) -> &mut Command {
self.fds.insert(0, cfg.to_fd(false));
self
}
pub fn stdout(&mut self, cfg: Stdio) -> &mut Command {
self.fds.insert(1, cfg.to_fd(true));
self
}
pub fn stderr(&mut self, cfg: Stdio) -> &mut Command {
self.fds.insert(2, cfg.to_fd(true));
self
}
pub fn uid(&mut self, id: uid_t) -> &mut Command {
self.config.uid = Some(id);
self
}
pub fn gid(&mut self, id: gid_t) -> &mut Command {
self.config.gid = Some(id);
self
}
pub fn groups(&mut self, ids: Vec<gid_t>) -> &mut Command {
self.config.supplementary_gids = Some(ids);
self
}
}