ksud: fix issues found by clippy (#167)

These issues are mostly found by `cargo clippy -- -W clippy::pedantic`.
This commit is contained in:
skbeh
2023-02-03 09:45:07 +08:00
committed by GitHub
parent bea93f6ad7
commit 219ea1c458
13 changed files with 217 additions and 245 deletions

View File

@@ -1,9 +1,11 @@
#![allow(dead_code, unused_mut, unused_variables, unused_imports)]
use anyhow::Result;
use anyhow::{ensure, Result};
#[cfg(unix)]
use anyhow::ensure;
#[cfg(unix)]
use std::os::unix::process::CommandExt;
pub const KERNEL_SU_OPTION: u32 = 0xDEADBEEF;
pub const KERNEL_SU_OPTION: u32 = 0xDEAD_BEEF;
const CMD_GRANT_ROOT: u64 = 0;
// const CMD_BECOME_MANAGER: u64 = 1;
@@ -18,45 +20,52 @@ pub const CMD_SET_SEPOLICY: u64 = 8;
const EVENT_POST_FS_DATA: u64 = 1;
const EVENT_BOOT_COMPLETED: u64 = 2;
#[cfg(target_os = "android")]
#[cfg(unix)]
pub fn grant_root() -> Result<()> {
let mut result: u32 = 0;
unsafe {
#[allow(clippy::cast_possible_wrap)]
libc::prctl(
KERNEL_SU_OPTION as i32,
KERNEL_SU_OPTION as i32, // supposed to overflow
CMD_GRANT_ROOT,
0,
0,
&mut result as *mut _ as *mut libc::c_void,
std::ptr::addr_of_mut!(result).cast::<libc::c_void>(),
);
}
ensure!(result == KERNEL_SU_OPTION, "grant root failed");
return Err(std::process::Command::new("sh").exec().into());
Err(std::process::Command::new("sh").exec().into())
}
#[cfg(not(target_os = "android"))]
#[cfg(not(unix))]
pub fn grant_root() -> Result<()> {
unimplemented!("grant_root is only available on android");
}
pub fn get_version() -> i32 {
let mut result: i32 = 0;
#[cfg(target_os = "android")]
#[cfg(unix)]
unsafe {
#[allow(clippy::cast_possible_wrap)]
libc::prctl(
KERNEL_SU_OPTION as i32,
KERNEL_SU_OPTION as i32, // supposed to overflow
CMD_GET_VERSION,
&mut result as *mut _ as *mut libc::c_void,
std::ptr::addr_of_mut!(result).cast::<libc::c_void>(),
);
}
result
}
fn report_event(event: u64) {
#[cfg(target_os = "android")]
#[cfg(unix)]
unsafe {
libc::prctl(KERNEL_SU_OPTION as i32, CMD_REPORT_EVENT, event);
#[allow(clippy::cast_possible_wrap)]
libc::prctl(
KERNEL_SU_OPTION as i32, // supposed to overflow
CMD_REPORT_EVENT,
event,
);
}
}