aboutsummaryrefslogtreecommitdiff
path: root/tools/aconfig/printflags/src/main.rs
blob: 7838b51e62d5f624b5025709058f3b8324e4527f (plain)
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
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//! `printflags` is a device binary to print feature flags.

use aconfig_protos::ProtoFlagState as State;
use aconfig_protos::ProtoParsedFlags;
use anyhow::{bail, Context, Result};
use regex::Regex;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::process::Command;
use std::{fs, str};

fn parse_device_config(raw: &str) -> HashMap<String, String> {
    let mut flags = HashMap::new();
    let regex = Regex::new(r"(?m)^([[[:alnum:]]_]+/[[[:alnum:]]_\.]+)=(true|false)$").unwrap();
    for capture in regex.captures_iter(raw) {
        let key = capture.get(1).unwrap().as_str().to_string();
        let value = match capture.get(2).unwrap().as_str() {
            "true" => format!("{:?} (device_config)", State::ENABLED),
            "false" => format!("{:?} (device_config)", State::DISABLED),
            _ => panic!(),
        };
        flags.insert(key, value);
    }
    flags
}

fn xxd(bytes: &[u8]) -> String {
    let n = 8.min(bytes.len());
    let mut v = Vec::with_capacity(n);
    for byte in bytes.iter().take(n) {
        v.push(format!("{:02x}", byte));
    }
    let trailer = match bytes.len() {
        0..=8 => "",
        _ => " ..",
    };
    format!("[{}{}]", v.join(" "), trailer)
}

fn main() -> Result<()> {
    // read device_config
    let output = Command::new("/system/bin/device_config").arg("list").output()?;
    if !output.status.success() {
        let reason = match output.status.code() {
            Some(code) => format!("exit code {}", code),
            None => "terminated by signal".to_string(),
        };
        bail!("failed to execute device_config: {}", reason);
    }
    let dc_stdout = str::from_utf8(&output.stdout)?;
    let device_config_flags = parse_device_config(dc_stdout);

    // read aconfig_flags.pb files
    let apex_pattern = Regex::new(r"^/apex/[^@]+\.[^@]+$").unwrap();
    let mut mount_points = vec![
        "system".to_string(),
        "system_ext".to_string(),
        "product".to_string(),
        "vendor".to_string(),
    ];
    for apex in fs::read_dir("/apex")? {
        let path_name = apex?.path().display().to_string();
        if let Some(canonical_path) = apex_pattern.captures(&path_name) {
            mount_points.push(canonical_path.get(0).unwrap().as_str().to_owned());
        }
    }

    let mut flags: BTreeMap<String, Vec<String>> = BTreeMap::new();
    for mount_point in mount_points {
        let path = format!("/{}/etc/aconfig_flags.pb", mount_point);
        let Ok(bytes) = fs::read(&path) else {
            eprintln!("warning: failed to read {}", path);
            continue;
        };
        let parsed_flags: ProtoParsedFlags = protobuf::Message::parse_from_bytes(&bytes)
            .with_context(|| {
                format!("failed to parse {} ({}, {} byte(s))", path, xxd(&bytes), bytes.len())
            })?;
        for flag in parsed_flags.parsed_flag {
            let key = format!("{}/{}.{}", flag.namespace(), flag.package(), flag.name());
            let value = format!("{:?} + {:?} ({})", flag.permission(), flag.state(), mount_point);
            flags.entry(key).or_default().push(value);
        }
    }

    // print flags
    for (key, mut value) in flags {
        if let Some(dc_value) = device_config_flags.get(&key) {
            value.push(dc_value.to_string());
        }
        println!("{}: {}", key, value.join(", "));
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_device_config() {
        let input = r#"
namespace_one/com.foo.bar.flag_one=true
namespace_one/com.foo.bar.flag_two=false
random_noise;
namespace_two/android.flag_one=true
namespace_two/android.flag_two=nonsense
"#;
        let expected = HashMap::from([
            (
                "namespace_one/com.foo.bar.flag_one".to_string(),
                "ENABLED (device_config)".to_string(),
            ),
            (
                "namespace_one/com.foo.bar.flag_two".to_string(),
                "DISABLED (device_config)".to_string(),
            ),
            ("namespace_two/android.flag_one".to_string(), "ENABLED (device_config)".to_string()),
        ]);
        let actual = parse_device_config(input);
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_xxd() {
        let input = [0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9];
        assert_eq!("[]", &xxd(&input[0..0]));
        assert_eq!("[00]", &xxd(&input[0..1]));
        assert_eq!("[00 01]", &xxd(&input[0..2]));
        assert_eq!("[00 01 02 03 04 05 06]", &xxd(&input[0..7]));
        assert_eq!("[00 01 02 03 04 05 06 07]", &xxd(&input[0..8]));
        assert_eq!("[00 01 02 03 04 05 06 07 ..]", &xxd(&input[0..9]));
        assert_eq!("[00 01 02 03 04 05 06 07 ..]", &xxd(&input));
    }
}