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
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use {
    fuchsia_runtime::{HandleInfo, HandleType},
    fuchsia_zircon as zx,
    process_builder::{NamespaceEntry, StartupHandle},
    std::ffi::CString,
    std::iter::once,
};

#[derive(Default)]
pub struct ProcessArgs {
    /// Command-line arguments passed to the process.
    pub args: Vec<CString>,

    /// Environment variables passed to the process.
    pub environment_variables: Vec<CString>,

    /// Entries added to the process namespace.
    ///
    /// The paths in the namespace must be non-overlapping. See
    /// <https://fuchsia.dev/fuchsia-src/concepts/process/namespaces> for details.
    pub namespace_entries: Vec<NamespaceEntry>,

    /// Handles passed to the process.
    pub handles: Vec<StartupHandle>,
}

impl ProcessArgs {
    pub fn new() -> Self {
        ProcessArgs::default()
    }

    pub fn add_default_loader(&mut self) -> Result<&mut Self, zx::Status> {
        let loader = fuchsia_runtime::loader_svc()?;
        Ok(self.add_handles(once(StartupHandle {
            handle: loader,
            info: HandleInfo::new(HandleType::LdsvcLoader, 0),
        })))
    }

    pub fn add_args<I, T>(&mut self, args: I) -> &mut Self
    where
        I: IntoIterator<Item = T>,
        T: Into<CString>,
    {
        let args = args.into_iter().map(|s| s.into());
        self.args.extend(args);
        self
    }

    pub fn add_environment_variables<I, T>(&mut self, environment_variables: I) -> &mut Self
    where
        I: IntoIterator<Item = T>,
        T: Into<CString>,
    {
        let environment_variables = environment_variables.into_iter().map(|s| s.into());
        self.environment_variables.extend(environment_variables);
        self
    }

    pub fn add_handles<I>(&mut self, handles: I) -> &mut Self
    where
        I: IntoIterator<Item = StartupHandle>,
    {
        self.handles.extend(handles);
        self
    }
}