archivist_config/
archivist-config_rust_config_lib_source.rs

1use fidl::unpersist;
2use fidl_cf_sc_internal_archivistconfig::Config as FidlConfig;
3use fuchsia_inspect::{ArrayProperty, Node};
4use fuchsia_runtime::{take_startup_handle, HandleInfo, HandleType};
5use std::convert::TryInto;
6const EXPECTED_CHECKSUM: &[u8] = &[
7    0xad, 0x2d, 0xa7, 0x3c, 0x98, 0x65, 0x93, 0x64, 0x02, 0xd1, 0x39, 0x15, 0xc7, 0x21, 0xaf, 0xe3,
8    0x63, 0xcd, 0x55, 0xb3, 0xcf, 0x36, 0x33, 0x1f, 0x99, 0xcf, 0x24, 0x43, 0x9f, 0x16, 0xe9, 0xf9,
9];
10#[derive(Debug)]
11pub struct Config {
12    pub allow_serial_logs: Vec<String>,
13    pub bind_services: Vec<String>,
14    pub component_initial_interests: Vec<String>,
15    pub deny_serial_log_tags: Vec<String>,
16    pub enable_klog: bool,
17    pub log_to_debuglog: bool,
18    pub logs_max_cached_original_bytes: u64,
19    pub maximum_concurrent_snapshots_per_reader: u64,
20    pub num_threads: u8,
21    pub per_component_batch_timeout_seconds: i64,
22    pub pipelines_path: String,
23}
24impl Config {
25    #[doc = r" Take the config startup handle and parse its contents."]
26    #[doc = r""]
27    #[doc = r" # Panics"]
28    #[doc = r""]
29    #[doc = r" If the config startup handle was already taken or if it is not valid."]
30    pub fn take_from_startup_handle() -> Self {
31        let handle_info = HandleInfo::new(HandleType::ComponentConfigVmo, 0);
32        let config_vmo: zx::Vmo =
33            take_startup_handle(handle_info).expect("Config VMO handle must be present.").into();
34        Self::from_vmo(&config_vmo).expect("Config VMO handle must be valid.")
35    }
36    #[doc = r" Parse `Self` from `vmo`."]
37    pub fn from_vmo(vmo: &zx::Vmo) -> Result<Self, Error> {
38        let config_size = vmo.get_content_size().map_err(Error::GettingContentSize)?;
39        let config_bytes = vmo.read_to_vec(0, config_size).map_err(Error::ReadingConfigBytes)?;
40        Self::from_bytes(&config_bytes)
41    }
42    #[doc = r" Parse `Self` from `bytes`."]
43    pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
44        let (checksum_len_bytes, bytes) = bytes.split_at_checked(2).ok_or(Error::TooFewBytes)?;
45        let checksum_len_bytes: [u8; 2] =
46            checksum_len_bytes.try_into().expect("previous call guaranteed 2 element slice");
47        let checksum_length = u16::from_le_bytes(checksum_len_bytes) as usize;
48        let (observed_checksum, bytes) =
49            bytes.split_at_checked(checksum_length).ok_or(Error::TooFewBytes)?;
50        if observed_checksum != EXPECTED_CHECKSUM {
51            return Err(Error::ChecksumMismatch { observed_checksum: observed_checksum.to_vec() });
52        }
53        let fidl_config: FidlConfig = unpersist(bytes).map_err(Error::Unpersist)?;
54        Ok(Self {
55            allow_serial_logs: fidl_config.allow_serial_logs,
56            bind_services: fidl_config.bind_services,
57            component_initial_interests: fidl_config.component_initial_interests,
58            deny_serial_log_tags: fidl_config.deny_serial_log_tags,
59            enable_klog: fidl_config.enable_klog,
60            log_to_debuglog: fidl_config.log_to_debuglog,
61            logs_max_cached_original_bytes: fidl_config.logs_max_cached_original_bytes,
62            maximum_concurrent_snapshots_per_reader: fidl_config
63                .maximum_concurrent_snapshots_per_reader,
64            num_threads: fidl_config.num_threads,
65            per_component_batch_timeout_seconds: fidl_config.per_component_batch_timeout_seconds,
66            pipelines_path: fidl_config.pipelines_path,
67        })
68    }
69    pub fn record_inspect(&self, inspector_node: &Node) {
70        let arr =
71            inspector_node.create_string_array("allow_serial_logs", self.allow_serial_logs.len());
72        for i in 0..self.allow_serial_logs.len() {
73            arr.set(i, &self.allow_serial_logs[i]);
74        }
75        inspector_node.record(arr);
76        let arr = inspector_node.create_string_array("bind_services", self.bind_services.len());
77        for i in 0..self.bind_services.len() {
78            arr.set(i, &self.bind_services[i]);
79        }
80        inspector_node.record(arr);
81        let arr = inspector_node.create_string_array(
82            "component_initial_interests",
83            self.component_initial_interests.len(),
84        );
85        for i in 0..self.component_initial_interests.len() {
86            arr.set(i, &self.component_initial_interests[i]);
87        }
88        inspector_node.record(arr);
89        let arr = inspector_node
90            .create_string_array("deny_serial_log_tags", self.deny_serial_log_tags.len());
91        for i in 0..self.deny_serial_log_tags.len() {
92            arr.set(i, &self.deny_serial_log_tags[i]);
93        }
94        inspector_node.record(arr);
95        inspector_node.record_bool("enable_klog", self.enable_klog);
96        inspector_node.record_bool("log_to_debuglog", self.log_to_debuglog);
97        inspector_node
98            .record_uint("logs_max_cached_original_bytes", self.logs_max_cached_original_bytes);
99        inspector_node.record_uint(
100            "maximum_concurrent_snapshots_per_reader",
101            self.maximum_concurrent_snapshots_per_reader,
102        );
103        inspector_node.record_uint("num_threads", self.num_threads as u64);
104        inspector_node.record_int(
105            "per_component_batch_timeout_seconds",
106            self.per_component_batch_timeout_seconds,
107        );
108        inspector_node.record_string("pipelines_path", &self.pipelines_path);
109    }
110}
111#[derive(Debug)]
112pub enum Error {
113    #[doc = r" Failed to read the content size of the VMO."]
114    GettingContentSize(zx::Status),
115    #[doc = r" Failed to read the content of the VMO."]
116    ReadingConfigBytes(zx::Status),
117    #[doc = r" The VMO was too small for this config library."]
118    TooFewBytes,
119    #[doc = r" The VMO's config ABI checksum did not match this library's."]
120    ChecksumMismatch { observed_checksum: Vec<u8> },
121    #[doc = r" Failed to parse the non-checksum bytes of the VMO as this library's FIDL type."]
122    Unpersist(fidl::Error),
123}
124impl std::fmt::Display for Error {
125    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
126        match self {
127            Self::GettingContentSize(status) => {
128                write!(f, "Failed to get content size: {status}")
129            }
130            Self::ReadingConfigBytes(status) => {
131                write!(f, "Failed to read VMO content: {status}")
132            }
133            Self::TooFewBytes => {
134                write!(f, "VMO content is not large enough for this config library.")
135            }
136            Self::ChecksumMismatch { observed_checksum } => {
137                write!(
138                    f,
139                    "ABI checksum mismatch, expected {:?}, got {:?}",
140                    EXPECTED_CHECKSUM, observed_checksum,
141                )
142            }
143            Self::Unpersist(fidl_error) => {
144                write!(f, "Failed to parse contents of config VMO: {fidl_error}")
145            }
146        }
147    }
148}
149impl std::error::Error for Error {
150    #[allow(unused_parens, reason = "rustfmt errors without parens here")]
151    fn source(&self) -> Option<(&'_ (dyn std::error::Error + 'static))> {
152        match self {
153            Self::GettingContentSize(ref status) | Self::ReadingConfigBytes(ref status) => {
154                Some(status)
155            }
156            Self::TooFewBytes => None,
157            Self::ChecksumMismatch { .. } => None,
158            Self::Unpersist(ref fidl_error) => Some(fidl_error),
159        }
160    }
161    fn description(&self) -> &str {
162        match self {
163            Self::GettingContentSize(_) => "getting content size",
164            Self::ReadingConfigBytes(_) => "reading VMO contents",
165            Self::TooFewBytes => "VMO contents too small",
166            Self::ChecksumMismatch { .. } => "ABI checksum mismatch",
167            Self::Unpersist(_) => "FIDL parsing error",
168        }
169    }
170}