processargs/
lib.rs

1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4use fuchsia_runtime::{HandleInfo, HandleType};
5
6use process_builder::{NamespaceEntry, StartupHandle};
7use std::ffi::CString;
8use std::iter::once;
9
10#[derive(Default)]
11pub struct ProcessArgs {
12    /// Command-line arguments passed to the process.
13    pub args: Vec<CString>,
14
15    /// Environment variables passed to the process.
16    pub environment_variables: Vec<CString>,
17
18    /// Entries added to the process namespace.
19    ///
20    /// The paths in the namespace must be non-overlapping. See
21    /// <https://fuchsia.dev/fuchsia-src/concepts/process/namespaces> for details.
22    pub namespace_entries: Vec<NamespaceEntry>,
23
24    /// Handles passed to the process.
25    pub handles: Vec<StartupHandle>,
26}
27
28impl ProcessArgs {
29    pub fn new() -> Self {
30        ProcessArgs::default()
31    }
32
33    pub fn add_default_loader(&mut self) -> Result<&mut Self, zx::Status> {
34        let loader = fuchsia_runtime::loader_svc()?;
35        Ok(self.add_handles(once(StartupHandle {
36            handle: loader,
37            info: HandleInfo::new(HandleType::LdsvcLoader, 0),
38        })))
39    }
40
41    pub fn add_args<I, T>(&mut self, args: I) -> &mut Self
42    where
43        I: IntoIterator<Item = T>,
44        T: Into<CString>,
45    {
46        let args = args.into_iter().map(|s| s.into());
47        self.args.extend(args);
48        self
49    }
50
51    pub fn add_environment_variables<I, T>(&mut self, environment_variables: I) -> &mut Self
52    where
53        I: IntoIterator<Item = T>,
54        T: Into<CString>,
55    {
56        let environment_variables = environment_variables.into_iter().map(|s| s.into());
57        self.environment_variables.extend(environment_variables);
58        self
59    }
60
61    pub fn add_handles<I>(&mut self, handles: I) -> &mut Self
62    where
63        I: IntoIterator<Item = StartupHandle>,
64    {
65        self.handles.extend(handles);
66        self
67    }
68}