Add root check to aflags

aflags scans the apex/ dir, which requires root access to read.
Currently the command fails with "Error: Permission denied", which
doesn't tell the user how to fix the problem. This CL adds a more
descriptive error message.

Test: adb unroot && adb shell aflags list
Bug: 347692127
Change-Id: I98a7a1ba10ef52ec47035816fa66119ea84f281d
This commit is contained in:
Ted Bauer
2024-06-17 14:48:55 +00:00
parent e7a2e5994f
commit 5223a70887

View File

@@ -233,8 +233,6 @@ fn format_flag_row(flag: &Flag, info: &PaddingInfo) -> String {
}
fn set_flag(qualified_name: &str, value: &str) -> Result<()> {
ensure!(nix::unistd::Uid::current().is_root(), "must be root to mutate flags");
let flags_binding = DeviceConfigSource::list_flags()?;
let flag = flags_binding.iter().find(|f| f.qualified_name() == qualified_name).ok_or(
anyhow!("no aconfig flag '{qualified_name}'. Does the flag have an .aconfig definition?"),
@@ -282,7 +280,9 @@ fn list(source_type: FlagSourceType, container: Option<String>) -> Result<String
Ok(result)
}
fn main() {
fn main() -> Result<()> {
ensure!(nix::unistd::Uid::current().is_root(), "must be root");
let cli = Cli::parse();
let output = match cli.command {
Command::List { use_new_storage: true, container } => {
@@ -299,6 +299,8 @@ fn main() {
Ok(None) => (),
Err(message) => println!("Error: {message}"),
}
Ok(())
}
#[cfg(test)]