blob: 88fdea9d45f9c8cf23e7d65db9ab513eba7b64a2 [file] [log] [blame]
Mårten Kongstad433fab92023-09-22 11:08:33 +02001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! `printflags` is a device binary to print feature flags.
18
Mårten Kongstad3bb79882023-09-26 13:06:22 +020019use aconfig_protos::aconfig::Flag_state as State;
Mårten Kongstad433fab92023-09-22 11:08:33 +020020use aconfig_protos::aconfig::Parsed_flags as ProtoParsedFlags;
Mårten Kongstad3bb79882023-09-26 13:06:22 +020021use anyhow::{bail, Result};
22use regex::Regex;
Mårten Kongstad433fab92023-09-22 11:08:33 +020023use std::collections::HashMap;
Mårten Kongstad3bb79882023-09-26 13:06:22 +020024use std::process::Command;
25use std::{fs, str};
26
27fn parse_device_config(raw: &str) -> HashMap<String, String> {
28 let mut flags = HashMap::new();
29 let regex = Regex::new(r"(?m)^([[[:alnum:]]_]+/[[[:alnum:]]_\.]+)=(true|false)$").unwrap();
30 for capture in regex.captures_iter(raw) {
31 let key = capture.get(1).unwrap().as_str().to_string();
32 let value = match capture.get(2).unwrap().as_str() {
33 "true" => format!("{:?} (device_config)", State::ENABLED),
34 "false" => format!("{:?} (device_config)", State::DISABLED),
35 _ => panic!(),
36 };
37 flags.insert(key, value);
38 }
39 flags
40}
Mårten Kongstad433fab92023-09-22 11:08:33 +020041
42fn main() -> Result<()> {
Mårten Kongstad3bb79882023-09-26 13:06:22 +020043 // read device_config
44 let output = Command::new("/system/bin/device_config").arg("list").output()?;
45 if !output.status.success() {
46 let reason = match output.status.code() {
47 Some(code) => format!("exit code {}", code),
48 None => "terminated by signal".to_string(),
49 };
50 bail!("failed to execute device_config: {}", reason);
51 }
52 let dc_stdout = str::from_utf8(&output.stdout)?;
53 let device_config_flags = parse_device_config(dc_stdout);
54
55 // read aconfig_flags.pb files
Mårten Kongstad433fab92023-09-22 11:08:33 +020056 let mut flags: HashMap<String, Vec<String>> = HashMap::new();
57 for partition in ["system", "system_ext", "product", "vendor"] {
58 let path = format!("/{}/etc/aconfig_flags.pb", partition);
59 let Ok(bytes) = fs::read(&path) else {
60 eprintln!("warning: failed to read {}", path);
61 continue;
62 };
63 let parsed_flags: ProtoParsedFlags = protobuf::Message::parse_from_bytes(&bytes)?;
64 for flag in parsed_flags.parsed_flag {
Mårten Kongstad3bb79882023-09-26 13:06:22 +020065 let key = format!("{}/{}.{}", flag.namespace(), flag.package(), flag.name());
Mårten Kongstad433fab92023-09-22 11:08:33 +020066 let value = format!("{:?} + {:?} ({})", flag.permission(), flag.state(), partition);
67 flags.entry(key).or_default().push(value);
68 }
69 }
Mårten Kongstad3bb79882023-09-26 13:06:22 +020070
71 // print flags
72 for (key, mut value) in flags {
73 let (_, package_and_name) = key.split_once('/').unwrap();
74 if let Some(dc_value) = device_config_flags.get(&key) {
75 value.push(dc_value.to_string());
76 }
77 println!("{}: {}", package_and_name, value.join(", "));
Mårten Kongstad433fab92023-09-22 11:08:33 +020078 }
Mårten Kongstad3bb79882023-09-26 13:06:22 +020079
Mårten Kongstad433fab92023-09-22 11:08:33 +020080 Ok(())
81}
Mårten Kongstad3bb79882023-09-26 13:06:22 +020082
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn test_foo() {
89 let input = r#"
90namespace_one/com.foo.bar.flag_one=true
91namespace_one/com.foo.bar.flag_two=false
92random_noise;
93namespace_two/android.flag_one=true
94namespace_two/android.flag_two=nonsense
95"#;
96 let expected = HashMap::from([
97 (
98 "namespace_one/com.foo.bar.flag_one".to_string(),
99 "ENABLED (device_config)".to_string(),
100 ),
101 (
102 "namespace_one/com.foo.bar.flag_two".to_string(),
103 "DISABLED (device_config)".to_string(),
104 ),
105 ("namespace_two/android.flag_one".to_string(), "ENABLED (device_config)".to_string()),
106 ]);
107 let actual = parse_device_config(input);
108 assert_eq!(expected, actual);
109 }
110}