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
extern crate byteorder;
#[macro_use]
extern crate failure;
extern crate libc;
#[macro_use]
extern crate log;
extern crate sysctl;
#[macro_use]
mod sys;
#[macro_use]
extern crate bitflags;
extern crate nix;
extern crate rctl;
extern crate strum;
#[macro_use]
extern crate strum_macros;
#[cfg(feature = "serialize")]
extern crate serde;
#[cfg(feature = "serialize")]
extern crate serde_json;
use std::collections::HashMap;
use std::convert;
use std::net;
use std::path;
mod error;
pub use error::JailError;
mod running;
pub use running::RunningJail;
pub use running::RunningJails as RunningJailIter;
mod stopped;
pub use stopped::StoppedJail;
pub mod param;
pub mod process;
#[cfg(test)]
mod tests;
#[cfg(target_os = "freebsd")]
#[derive(Debug, PartialEq, Clone)]
pub enum Jail {
Stopped(StoppedJail),
Running(RunningJail),
}
impl convert::From<RunningJail> for Jail {
fn from(running: RunningJail) -> Self {
Jail::Running(running)
}
}
impl convert::From<StoppedJail> for Jail {
fn from(stopped: StoppedJail) -> Self {
trace!("Jail::from({:?})", stopped);
Jail::Stopped(stopped)
}
}
impl Jail {
pub fn is_started(&self) -> bool {
trace!("Jail::is_started({:?})", self);
match self {
Jail::Running(_) => true,
Jail::Stopped(_) => false,
}
}
pub fn start(self) -> Result<Self, JailError> {
trace!("Jail::start({:?})", self);
match self {
Jail::Running(r) => Ok(Jail::Running(r)),
Jail::Stopped(s) => Ok(Jail::Running(s.start()?)),
}
}
pub fn stop(self) -> Result<Self, JailError> {
trace!("Jail::stop({:?})", self);
match self {
Jail::Running(r) => Ok(Jail::Stopped(r.stop()?)),
Jail::Stopped(s) => Ok(Jail::Stopped(s)),
}
}
pub fn name(&self) -> Result<String, JailError> {
trace!("Jail::name({:?})", self);
match self {
Jail::Running(r) => r.name(),
Jail::Stopped(s) => s
.name
.clone()
.ok_or_else(|| JailError::NoSuchParameter("name".into())),
}
}
pub fn path(&self) -> Result<path::PathBuf, JailError> {
trace!("Jail::path({:?})", self);
match self {
Jail::Running(r) => r.path(),
Jail::Stopped(s) => s
.path
.clone()
.ok_or_else(|| JailError::NoSuchParameter("path".into())),
}
}
pub fn hostname(&self) -> Result<String, JailError> {
trace!("Jail::hostname({:?})", self);
match self {
Jail::Running(r) => r.hostname(),
Jail::Stopped(s) => s
.hostname
.clone()
.ok_or_else(|| JailError::NoSuchParameter("hostname".into())),
}
}
pub fn ips(&self) -> Result<Vec<net::IpAddr>, JailError> {
trace!("Jail::ips({:?})", self);
match self {
Jail::Running(r) => r.ips(),
Jail::Stopped(s) => Ok(s.ips.clone()),
}
}
pub fn param(&self, name: &str) -> Result<param::Value, JailError> {
trace!("Jail::param({:?})", self);
match self {
Jail::Running(r) => r.param(name),
Jail::Stopped(s) => s
.params
.get(name)
.ok_or_else(|| JailError::NoSuchParameter(name.into()))
.map(|x| x.clone()),
}
}
pub fn params(&self) -> Result<HashMap<String, param::Value>, JailError> {
trace!("Jail::params({:?})", self);
match self {
Jail::Running(r) => r.params(),
Jail::Stopped(s) => Ok(s.params.clone()),
}
}
}