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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use std::ffi::{OsStr, OsString};
use std::fmt::{self, Display, Formatter};
use std::path::{Path, PathBuf};
use std::time::Duration;

#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum ShareNet {
    Share,
    Unshare,
}

impl Default for ShareNet {
    fn default() -> Self {
        ShareNet::Unshare
    }
}

#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum SwapRedirects {
    Yes,
    No,
}

impl Default for SwapRedirects {
    fn default() -> Self {
        SwapRedirects::No
    }
}

#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum ClearUsage {
    Yes,
    No,
}

impl Default for ClearUsage {
    fn default() -> Self {
        ClearUsage::Yes
    }
}

#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum Interactive {
    Yes,
    No,
}

impl Default for Interactive {
    fn default() -> Self {
        Interactive::No
    }
}

#[derive(Debug, Eq, PartialEq, Copy, Clone, PartialOrd, Ord, Serialize, Deserialize)]
pub struct SpaceUsage(u64);

impl SpaceUsage {
    pub fn from_bytes(bytes: u64) -> Self {
        SpaceUsage(bytes)
    }

    pub fn from_kilobytes(kilobytes: u64) -> Self {
        Self::from_bytes(kilobytes * 1_000)
    }

    pub fn from_megabytes(megabytes: u64) -> Self {
        Self::from_kilobytes(megabytes * 1_000)
    }

    pub fn from_gigabytes(gigabytes: u64) -> Self {
        Self::from_megabytes(gigabytes * 1_000)
    }

    pub fn from_kibibytes(kibibytes: u64) -> Self {
        Self::from_bytes(kibibytes * 1_024)
    }

    pub fn from_mebibytes(mebibytes: u64) -> Self {
        Self::from_kibibytes(mebibytes * 1_024)
    }

    pub fn from_gibibytes(gibibytes: u64) -> Self {
        Self::from_mebibytes(gibibytes * 1_024)
    }

    pub fn as_bytes(self) -> u64 {
        self.0
    }

    pub fn as_kilobytes(self) -> u64 {
        self.0 / 1_000
    }
}

impl Display for SpaceUsage {
    fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
        if self.0 % (1 << 30) == 0 {
            write!(fmt, "{} gibibytes", self.0 >> 30)
        } else if self.0 % (1 << 20) == 0 {
            write!(fmt, "{} mebibytes", self.0 >> 20)
        } else if self.0 % (1 << 10) == 0 {
            write!(fmt, "{} kibibytes", self.0 >> 10)
        } else if self.0 % 1_000_000_000 == 0 {
            write!(fmt, "{} gigabytes", self.0 / 1_000_000_000)
        } else if self.0 % 1_000_000 == 0 {
            write!(fmt, "{} megabytes", self.0 / 1_000_000)
        } else if self.0 % 1_000 == 0 {
            write!(fmt, "{} kilobytes", self.0 / 1_000)
        } else {
            write!(fmt, "{} bytes", self.0)
        }
    }
}

/// Limits for memory/time
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub struct Limits {
    wall_time: Option<Duration>,
    user_time: Option<Duration>,
    memory: Option<SpaceUsage>,
    stack: Option<SpaceUsage>,
    pids: Option<usize>,
}

impl Limits {
    pub fn new(
        wall_time: Option<Duration>,
        user_time: Option<Duration>,
        memory: Option<SpaceUsage>,
        stack: Option<SpaceUsage>,
        pids: Option<usize>,
    ) -> Self {
        Self {
            wall_time,
            user_time,
            memory,
            stack,
            pids,
        }
    }

    pub fn wall_time(&self) -> Option<Duration> {
        self.wall_time
    }

    pub fn user_time(&self) -> Option<Duration> {
        self.user_time
    }

    pub fn memory(&self) -> Option<SpaceUsage> {
        self.memory
    }

    pub fn stack(&self) -> Option<SpaceUsage> {
        self.stack
    }

    pub fn pids(&self) -> Option<usize> {
        self.pids
    }
}

impl Default for Limits {
    fn default() -> Self {
        Self::new(None, None, None, None, None)
    }
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub struct ControllerPath {
    cpuacct: Option<PathBuf>,
    memory: Option<PathBuf>,
    pids: Option<PathBuf>,
}

impl ControllerPath {
    pub fn new(cpuacct: Option<PathBuf>, memory: Option<PathBuf>, pids: Option<PathBuf>) -> Self {
        Self {
            cpuacct,
            memory,
            pids,
        }
    }

    pub fn cpuacct(&self) -> Option<&Path> {
        self.cpuacct.as_ref().map(|path_buf| path_buf.as_path())
    }

    pub fn memory(&self) -> Option<&Path> {
        self.memory.as_ref().map(|path_buf| path_buf.as_path())
    }

    pub fn pids(&self) -> Option<&Path> {
        self.pids.as_ref().map(|path_buf| path_buf.as_path())
    }
}

impl Default for ControllerPath {
    fn default() -> Self {
        Self::new(None, None, None)
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
pub struct MountOptions {
    read_only: bool,
    dev: bool,
    exec: bool,
}

impl MountOptions {
    pub fn read_only(self) -> bool {
        self.read_only
    }

    pub fn dev(self) -> bool {
        self.dev
    }

    pub fn exec(self) -> bool {
        self.exec
    }

    pub fn set_read_only(&mut self, value: bool) {
        self.read_only = value;
    }

    pub fn set_dev(&mut self, value: bool) {
        self.dev = value;
    }

    pub fn set_exec(&mut self, value: bool) {
        self.exec = value;
    }
}

impl Default for MountOptions {
    fn default() -> Self {
        Self {
            read_only: true,
            dev: false,
            exec: false,
        }
    }
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub struct Mount {
    source: PathBuf,
    destination: PathBuf,
    mount_options: MountOptions,
}

impl Mount {
    pub fn new(source: PathBuf, destination: PathBuf, mount_options: MountOptions) -> Self {
        Self {
            source,
            destination,
            mount_options,
        }
    }

    pub fn source(&self) -> &Path {
        &self.source
    }

    pub fn destination(&self) -> &Path {
        &self.destination
    }

    pub fn mount_options(&self) -> MountOptions {
        self.mount_options
    }
}

#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Environment {
    Forward,
    EnvList(Vec<(String, String)>),
}

impl Default for Environment {
    fn default() -> Self {
        Environment::EnvList(Vec::new())
    }
}

#[derive(Debug, Eq, PartialEq)]
pub struct Config {
    command: PathBuf,
    args: Vec<OsString>,
    new_root: Option<PathBuf>,
    share_net: ShareNet,
    redirect_stdin: Option<PathBuf>,
    redirect_stdout: Option<PathBuf>,
    redirect_stderr: Option<PathBuf>,
    limits: Limits,
    instance_name: Option<OsString>,
    controller_path: ControllerPath,
    mounts: Vec<Mount>,
    swap_redirects: SwapRedirects,
    clear_usage: ClearUsage,
    interactive: Interactive,
    environment: Environment,
}

impl Config {
    #![cfg_attr(feature = "cargo-clippy", allow(too_many_arguments))]
    pub fn new(
        command: PathBuf,
        args: Vec<OsString>,
        new_root: Option<PathBuf>,
        share_net: ShareNet,
        redirect_stdin: Option<PathBuf>,
        redirect_stdout: Option<PathBuf>,
        redirect_stderr: Option<PathBuf>,
        limits: Limits,
        instance_name: Option<OsString>,
        controller_path: ControllerPath,
        mounts: Vec<Mount>,
        swap_redirects: SwapRedirects,
        clear_usage: ClearUsage,
        interactive: Interactive,
        environment: Environment,
    ) -> Self {
        Self {
            command,
            args,
            new_root,
            share_net,
            redirect_stdin,
            redirect_stdout,
            redirect_stderr,
            limits,
            instance_name,
            controller_path,
            mounts,
            swap_redirects,
            clear_usage,
            interactive,
            environment,
        }
    }

    pub fn command(&self) -> &Path {
        &self.command
    }

    pub fn args<'a>(&'a self) -> Vec<&'a OsStr> {
        self.args
            .iter()
            .map(|os_string| os_string.as_os_str())
            .collect()
    }

    pub fn new_root(&self) -> Option<&Path> {
        self.new_root.as_ref().map(|path_buf| path_buf.as_path())
    }

    pub fn share_net(&self) -> ShareNet {
        self.share_net
    }

    pub fn redirect_stdin(&self) -> Option<&Path> {
        self.redirect_stdin
            .as_ref()
            .map(|path_buf| path_buf.as_path())
    }

    pub fn redirect_stdout(&self) -> Option<&Path> {
        self.redirect_stdout
            .as_ref()
            .map(|path_buf| path_buf.as_path())
    }

    pub fn redirect_stderr(&self) -> Option<&Path> {
        self.redirect_stderr
            .as_ref()
            .map(|path_buf| path_buf.as_path())
    }

    pub fn limits(&self) -> Limits {
        self.limits
    }

    pub fn instance_name(&self) -> Option<&OsStr> {
        self.instance_name
            .as_ref()
            .map(|os_string| os_string.as_os_str())
    }

    pub fn controller_path(&self) -> &ControllerPath {
        &self.controller_path
    }

    pub fn mounts(&self) -> &[Mount] {
        self.mounts.as_ref()
    }

    pub fn swap_redirects(&self) -> SwapRedirects {
        self.swap_redirects
    }

    pub fn clear_usage(&self) -> ClearUsage {
        self.clear_usage
    }

    pub fn interactive(&self) -> Interactive {
        self.interactive
    }

    pub fn environment(&self) -> &Environment {
        &self.environment
    }
}