Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024 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 | //! `aflags` is a device binary to read and write aconfig flags. |
| 18 | |
Ted Bauer | a98448f | 2024-03-06 14:01:19 -0500 | [diff] [blame] | 19 | use anyhow::{anyhow, ensure, Result}; |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 20 | use clap::Parser; |
| 21 | |
| 22 | mod device_config_source; |
| 23 | use device_config_source::DeviceConfigSource; |
| 24 | |
Ted Bauer | 6d4db66 | 2024-03-06 18:08:32 -0500 | [diff] [blame] | 25 | mod aconfig_storage_source; |
| 26 | use aconfig_storage_source::AconfigStorageSource; |
| 27 | |
Ted Bauer | 206d44a | 2024-03-15 17:04:06 +0000 | [diff] [blame] | 28 | mod load_protos; |
| 29 | |
Ted Bauer | 46d758b | 2024-03-12 19:28:58 +0000 | [diff] [blame] | 30 | #[derive(Clone, PartialEq, Debug)] |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 31 | enum FlagPermission { |
| 32 | ReadOnly, |
| 33 | ReadWrite, |
| 34 | } |
| 35 | |
Chris Wailes | e6bb2e9 | 2024-05-09 15:14:22 -0700 | [diff] [blame] | 36 | impl std::fmt::Display for FlagPermission { |
| 37 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
Ted Bauer | aeb9609 | 2024-05-15 09:23:43 -0400 | [diff] [blame] | 38 | write!( |
| 39 | f, |
| 40 | "{}", |
| 41 | match &self { |
| 42 | Self::ReadOnly => "read-only", |
| 43 | Self::ReadWrite => "read-write", |
| 44 | } |
| 45 | ) |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
Ted Bauer | 46d758b | 2024-03-12 19:28:58 +0000 | [diff] [blame] | 49 | #[derive(Clone, Debug)] |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 50 | enum ValuePickedFrom { |
| 51 | Default, |
| 52 | Server, |
| 53 | } |
| 54 | |
Chris Wailes | e6bb2e9 | 2024-05-09 15:14:22 -0700 | [diff] [blame] | 55 | impl std::fmt::Display for ValuePickedFrom { |
| 56 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
Ted Bauer | aeb9609 | 2024-05-15 09:23:43 -0400 | [diff] [blame] | 57 | write!( |
| 58 | f, |
| 59 | "{}", |
| 60 | match &self { |
| 61 | Self::Default => "default", |
| 62 | Self::Server => "server", |
| 63 | } |
| 64 | ) |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Mårten Kongstad | f0c594d | 2024-03-08 10:20:32 +0100 | [diff] [blame] | 68 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 69 | enum FlagValue { |
| 70 | Enabled, |
| 71 | Disabled, |
| 72 | } |
| 73 | |
| 74 | impl TryFrom<&str> for FlagValue { |
| 75 | type Error = anyhow::Error; |
| 76 | |
| 77 | fn try_from(value: &str) -> std::result::Result<Self, Self::Error> { |
| 78 | match value { |
| 79 | "true" | "enabled" => Ok(Self::Enabled), |
| 80 | "false" | "disabled" => Ok(Self::Disabled), |
| 81 | _ => Err(anyhow!("cannot convert string '{}' to FlagValue", value)), |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
Chris Wailes | e6bb2e9 | 2024-05-09 15:14:22 -0700 | [diff] [blame] | 86 | impl std::fmt::Display for FlagValue { |
| 87 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
Ted Bauer | aeb9609 | 2024-05-15 09:23:43 -0400 | [diff] [blame] | 88 | write!( |
| 89 | f, |
| 90 | "{}", |
| 91 | match &self { |
| 92 | Self::Enabled => "enabled", |
| 93 | Self::Disabled => "disabled", |
| 94 | } |
| 95 | ) |
Mårten Kongstad | f0c594d | 2024-03-08 10:20:32 +0100 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Ted Bauer | 46d758b | 2024-03-12 19:28:58 +0000 | [diff] [blame] | 99 | #[derive(Clone, Debug)] |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 100 | struct Flag { |
| 101 | namespace: String, |
| 102 | name: String, |
| 103 | package: String, |
| 104 | container: String, |
Mårten Kongstad | f0c594d | 2024-03-08 10:20:32 +0100 | [diff] [blame] | 105 | value: FlagValue, |
Ted Bauer | 46d758b | 2024-03-12 19:28:58 +0000 | [diff] [blame] | 106 | staged_value: Option<FlagValue>, |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 107 | permission: FlagPermission, |
| 108 | value_picked_from: ValuePickedFrom, |
| 109 | } |
| 110 | |
Ted Bauer | 84883bd | 2024-03-04 22:45:29 +0000 | [diff] [blame] | 111 | impl Flag { |
| 112 | fn qualified_name(&self) -> String { |
| 113 | format!("{}.{}", self.package, self.name) |
| 114 | } |
Ted Bauer | 46d758b | 2024-03-12 19:28:58 +0000 | [diff] [blame] | 115 | |
| 116 | fn display_staged_value(&self) -> String { |
| 117 | match self.staged_value { |
Chris Wailes | e6bb2e9 | 2024-05-09 15:14:22 -0700 | [diff] [blame] | 118 | Some(v) => format!("(->{})", v), |
Ted Bauer | 46d758b | 2024-03-12 19:28:58 +0000 | [diff] [blame] | 119 | None => "-".to_string(), |
| 120 | } |
| 121 | } |
Ted Bauer | 84883bd | 2024-03-04 22:45:29 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 124 | trait FlagSource { |
| 125 | fn list_flags() -> Result<Vec<Flag>>; |
Ted Bauer | 84883bd | 2024-03-04 22:45:29 +0000 | [diff] [blame] | 126 | fn override_flag(namespace: &str, qualified_name: &str, value: &str) -> Result<()>; |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Ted Bauer | 6d4db66 | 2024-03-06 18:08:32 -0500 | [diff] [blame] | 129 | enum FlagSourceType { |
| 130 | DeviceConfig, |
| 131 | AconfigStorage, |
| 132 | } |
| 133 | |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 134 | const ABOUT_TEXT: &str = "Tool for reading and writing flags. |
| 135 | |
| 136 | Rows in the table from the `list` command follow this format: |
| 137 | |
| 138 | package flag_name value provenance permission container |
| 139 | |
| 140 | * `package`: package set for this flag in its .aconfig definition. |
| 141 | * `flag_name`: flag name, also set in definition. |
| 142 | * `value`: the value read from the flag. |
Ted Bauer | 46d758b | 2024-03-12 19:28:58 +0000 | [diff] [blame] | 143 | * `staged_value`: the value on next boot: |
| 144 | + `-`: same as current value |
| 145 | + `(->enabled) flipped to enabled on boot. |
| 146 | + `(->disabled) flipped to disabled on boot. |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 147 | * `provenance`: one of: |
| 148 | + `default`: the flag value comes from its build-time default. |
| 149 | + `server`: the flag value comes from a server override. |
| 150 | * `permission`: read-write or read-only. |
| 151 | * `container`: the container for the flag, configured in its definition. |
| 152 | "; |
| 153 | |
| 154 | #[derive(Parser, Debug)] |
| 155 | #[clap(long_about=ABOUT_TEXT)] |
| 156 | struct Cli { |
| 157 | #[clap(subcommand)] |
| 158 | command: Command, |
| 159 | } |
| 160 | |
| 161 | #[derive(Parser, Debug)] |
| 162 | enum Command { |
| 163 | /// List all aconfig flags on this device. |
Ted Bauer | 6d4db66 | 2024-03-06 18:08:32 -0500 | [diff] [blame] | 164 | List { |
| 165 | /// Read from the new flag storage. |
| 166 | #[clap(long)] |
| 167 | use_new_storage: bool, |
Ted Bauer | aeb9609 | 2024-05-15 09:23:43 -0400 | [diff] [blame] | 168 | |
| 169 | /// Optionally filter by container name. |
| 170 | #[clap(short = 'c', long = "container")] |
| 171 | container: Option<String>, |
Ted Bauer | 6d4db66 | 2024-03-06 18:08:32 -0500 | [diff] [blame] | 172 | }, |
Ted Bauer | 84883bd | 2024-03-04 22:45:29 +0000 | [diff] [blame] | 173 | |
| 174 | /// Enable an aconfig flag on this device, on the next boot. |
| 175 | Enable { |
| 176 | /// <package>.<flag_name> |
| 177 | qualified_name: String, |
| 178 | }, |
| 179 | |
| 180 | /// Disable an aconfig flag on this device, on the next boot. |
| 181 | Disable { |
| 182 | /// <package>.<flag_name> |
| 183 | qualified_name: String, |
| 184 | }, |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | struct PaddingInfo { |
Mårten Kongstad | d408e96 | 2024-03-07 13:56:30 +0100 | [diff] [blame] | 188 | longest_flag_col: usize, |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 189 | longest_val_col: usize, |
Ted Bauer | 46d758b | 2024-03-12 19:28:58 +0000 | [diff] [blame] | 190 | longest_staged_val_col: usize, |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 191 | longest_value_picked_from_col: usize, |
| 192 | longest_permission_col: usize, |
| 193 | } |
| 194 | |
Ted Bauer | aeb9609 | 2024-05-15 09:23:43 -0400 | [diff] [blame] | 195 | struct Filter { |
| 196 | container: Option<String>, |
| 197 | } |
| 198 | |
| 199 | impl Filter { |
| 200 | fn apply(&self, flags: &[Flag]) -> Vec<Flag> { |
| 201 | flags |
| 202 | .iter() |
| 203 | .filter(|flag| match &self.container { |
| 204 | Some(c) => flag.container == *c, |
| 205 | None => true, |
| 206 | }) |
| 207 | .cloned() |
| 208 | .collect() |
| 209 | } |
| 210 | } |
| 211 | |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 212 | fn format_flag_row(flag: &Flag, info: &PaddingInfo) -> String { |
Mårten Kongstad | d408e96 | 2024-03-07 13:56:30 +0100 | [diff] [blame] | 213 | let full_name = flag.qualified_name(); |
| 214 | let p0 = info.longest_flag_col + 1; |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 215 | |
Mårten Kongstad | f0c594d | 2024-03-08 10:20:32 +0100 | [diff] [blame] | 216 | let val = flag.value.to_string(); |
Mårten Kongstad | d408e96 | 2024-03-07 13:56:30 +0100 | [diff] [blame] | 217 | let p1 = info.longest_val_col + 1; |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 218 | |
Ted Bauer | 46d758b | 2024-03-12 19:28:58 +0000 | [diff] [blame] | 219 | let staged_val = flag.display_staged_value(); |
| 220 | let p2 = info.longest_staged_val_col + 1; |
| 221 | |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 222 | let value_picked_from = flag.value_picked_from.to_string(); |
Ted Bauer | 46d758b | 2024-03-12 19:28:58 +0000 | [diff] [blame] | 223 | let p3 = info.longest_value_picked_from_col + 1; |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 224 | |
| 225 | let perm = flag.permission.to_string(); |
Ted Bauer | 46d758b | 2024-03-12 19:28:58 +0000 | [diff] [blame] | 226 | let p4 = info.longest_permission_col + 1; |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 227 | |
| 228 | let container = &flag.container; |
| 229 | |
Ted Bauer | 46d758b | 2024-03-12 19:28:58 +0000 | [diff] [blame] | 230 | format!( |
| 231 | "{full_name:p0$}{val:p1$}{staged_val:p2$}{value_picked_from:p3$}{perm:p4$}{container}\n" |
| 232 | ) |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Ted Bauer | 84883bd | 2024-03-04 22:45:29 +0000 | [diff] [blame] | 235 | fn set_flag(qualified_name: &str, value: &str) -> Result<()> { |
| 236 | let flags_binding = DeviceConfigSource::list_flags()?; |
| 237 | let flag = flags_binding.iter().find(|f| f.qualified_name() == qualified_name).ok_or( |
| 238 | anyhow!("no aconfig flag '{qualified_name}'. Does the flag have an .aconfig definition?"), |
| 239 | )?; |
| 240 | |
Ted Bauer | a98448f | 2024-03-06 14:01:19 -0500 | [diff] [blame] | 241 | ensure!(flag.permission == FlagPermission::ReadWrite, |
| 242 | format!("could not write flag '{qualified_name}', it is read-only for the current release configuration.")); |
Ted Bauer | 84883bd | 2024-03-04 22:45:29 +0000 | [diff] [blame] | 243 | |
| 244 | DeviceConfigSource::override_flag(&flag.namespace, qualified_name, value)?; |
| 245 | |
| 246 | Ok(()) |
| 247 | } |
| 248 | |
Ted Bauer | aeb9609 | 2024-05-15 09:23:43 -0400 | [diff] [blame] | 249 | fn list(source_type: FlagSourceType, container: Option<String>) -> Result<String> { |
| 250 | let flags_unfiltered = match source_type { |
Ted Bauer | 6d4db66 | 2024-03-06 18:08:32 -0500 | [diff] [blame] | 251 | FlagSourceType::DeviceConfig => DeviceConfigSource::list_flags()?, |
| 252 | FlagSourceType::AconfigStorage => AconfigStorageSource::list_flags()?, |
| 253 | }; |
Ted Bauer | aeb9609 | 2024-05-15 09:23:43 -0400 | [diff] [blame] | 254 | let flags = (Filter { container }).apply(&flags_unfiltered); |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 255 | let padding_info = PaddingInfo { |
Mårten Kongstad | d408e96 | 2024-03-07 13:56:30 +0100 | [diff] [blame] | 256 | longest_flag_col: flags.iter().map(|f| f.qualified_name().len()).max().unwrap_or(0), |
Mårten Kongstad | f0c594d | 2024-03-08 10:20:32 +0100 | [diff] [blame] | 257 | longest_val_col: flags.iter().map(|f| f.value.to_string().len()).max().unwrap_or(0), |
Ted Bauer | 46d758b | 2024-03-12 19:28:58 +0000 | [diff] [blame] | 258 | longest_staged_val_col: flags |
| 259 | .iter() |
| 260 | .map(|f| f.display_staged_value().len()) |
| 261 | .max() |
| 262 | .unwrap_or(0), |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 263 | longest_value_picked_from_col: flags |
| 264 | .iter() |
| 265 | .map(|f| f.value_picked_from.to_string().len()) |
| 266 | .max() |
| 267 | .unwrap_or(0), |
| 268 | longest_permission_col: flags |
| 269 | .iter() |
| 270 | .map(|f| f.permission.to_string().len()) |
| 271 | .max() |
| 272 | .unwrap_or(0), |
| 273 | }; |
| 274 | |
| 275 | let mut result = String::from(""); |
| 276 | for flag in flags { |
| 277 | let row = format_flag_row(&flag, &padding_info); |
| 278 | result.push_str(&row); |
| 279 | } |
| 280 | Ok(result) |
| 281 | } |
| 282 | |
Ted Bauer | 5223a70 | 2024-06-17 14:48:55 +0000 | [diff] [blame] | 283 | fn main() -> Result<()> { |
| 284 | ensure!(nix::unistd::Uid::current().is_root(), "must be root"); |
| 285 | |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 286 | let cli = Cli::parse(); |
| 287 | let output = match cli.command { |
Ted Bauer | aeb9609 | 2024-05-15 09:23:43 -0400 | [diff] [blame] | 288 | Command::List { use_new_storage: true, container } => { |
| 289 | list(FlagSourceType::AconfigStorage, container).map(Some) |
| 290 | } |
| 291 | Command::List { use_new_storage: false, container } => { |
| 292 | list(FlagSourceType::DeviceConfig, container).map(Some) |
| 293 | } |
Ted Bauer | 84883bd | 2024-03-04 22:45:29 +0000 | [diff] [blame] | 294 | Command::Enable { qualified_name } => set_flag(&qualified_name, "true").map(|_| None), |
| 295 | Command::Disable { qualified_name } => set_flag(&qualified_name, "false").map(|_| None), |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 296 | }; |
| 297 | match output { |
Ted Bauer | 84883bd | 2024-03-04 22:45:29 +0000 | [diff] [blame] | 298 | Ok(Some(text)) => println!("{text}"), |
| 299 | Ok(None) => (), |
| 300 | Err(message) => println!("Error: {message}"), |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 301 | } |
Ted Bauer | 5223a70 | 2024-06-17 14:48:55 +0000 | [diff] [blame] | 302 | |
| 303 | Ok(()) |
Ted Bauer | 4dbf58a | 2024-02-08 18:46:52 +0000 | [diff] [blame] | 304 | } |
Ted Bauer | aeb9609 | 2024-05-15 09:23:43 -0400 | [diff] [blame] | 305 | |
| 306 | #[cfg(test)] |
| 307 | mod tests { |
| 308 | use super::*; |
| 309 | |
| 310 | #[test] |
| 311 | fn test_filter_container() { |
| 312 | let flags = vec![ |
| 313 | Flag { |
| 314 | namespace: "namespace".to_string(), |
| 315 | name: "test1".to_string(), |
| 316 | package: "package".to_string(), |
| 317 | value: FlagValue::Disabled, |
| 318 | staged_value: None, |
| 319 | permission: FlagPermission::ReadWrite, |
| 320 | value_picked_from: ValuePickedFrom::Default, |
| 321 | container: "system".to_string(), |
| 322 | }, |
| 323 | Flag { |
| 324 | namespace: "namespace".to_string(), |
| 325 | name: "test2".to_string(), |
| 326 | package: "package".to_string(), |
| 327 | value: FlagValue::Disabled, |
| 328 | staged_value: None, |
| 329 | permission: FlagPermission::ReadWrite, |
| 330 | value_picked_from: ValuePickedFrom::Default, |
| 331 | container: "not_system".to_string(), |
| 332 | }, |
| 333 | Flag { |
| 334 | namespace: "namespace".to_string(), |
| 335 | name: "test3".to_string(), |
| 336 | package: "package".to_string(), |
| 337 | value: FlagValue::Disabled, |
| 338 | staged_value: None, |
| 339 | permission: FlagPermission::ReadWrite, |
| 340 | value_picked_from: ValuePickedFrom::Default, |
| 341 | container: "system".to_string(), |
| 342 | }, |
| 343 | ]; |
| 344 | |
| 345 | assert_eq!((Filter { container: Some("system".to_string()) }).apply(&flags).len(), 2); |
| 346 | } |
| 347 | |
| 348 | #[test] |
| 349 | fn test_filter_no_container() { |
| 350 | let flags = vec![ |
| 351 | Flag { |
| 352 | namespace: "namespace".to_string(), |
| 353 | name: "test1".to_string(), |
| 354 | package: "package".to_string(), |
| 355 | value: FlagValue::Disabled, |
| 356 | staged_value: None, |
| 357 | permission: FlagPermission::ReadWrite, |
| 358 | value_picked_from: ValuePickedFrom::Default, |
| 359 | container: "system".to_string(), |
| 360 | }, |
| 361 | Flag { |
| 362 | namespace: "namespace".to_string(), |
| 363 | name: "test2".to_string(), |
| 364 | package: "package".to_string(), |
| 365 | value: FlagValue::Disabled, |
| 366 | staged_value: None, |
| 367 | permission: FlagPermission::ReadWrite, |
| 368 | value_picked_from: ValuePickedFrom::Default, |
| 369 | container: "not_system".to_string(), |
| 370 | }, |
| 371 | Flag { |
| 372 | namespace: "namespace".to_string(), |
| 373 | name: "test3".to_string(), |
| 374 | package: "package".to_string(), |
| 375 | value: FlagValue::Disabled, |
| 376 | staged_value: None, |
| 377 | permission: FlagPermission::ReadWrite, |
| 378 | value_picked_from: ValuePickedFrom::Default, |
| 379 | container: "system".to_string(), |
| 380 | }, |
| 381 | ]; |
| 382 | |
| 383 | assert_eq!((Filter { container: None }).apply(&flags).len(), 3); |
| 384 | } |
| 385 | } |