Skip to content

Commit 2d2cafa

Browse files
committed
refactor(main): Enforce root-only execution with fail-fast logic
Refactor the application's startup sequence to enforce a strict root-only requirement, removing the previous non-root fallback behavior. The new logic in main() is as follows: - On startup, check if the effective UID is 0. - If not root, check for the availability of an 'su' binary. - If 'su' is present, attempt self-elevation via `try_elevate_privileges()`. If this function returns (i.e., fails), exit the app with an error. - If 'su' is not present, exit immediately with an error. This change ensures that the application will not proceed to the TUI unless it has successfully acquired root privileges, preventing it from running in an unsupported and non-functional state.
1 parent a6d4700 commit 2d2cafa

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

src/main.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,25 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4141
log_info!("Android Ver: {}", device_info.android_ver);
4242
log_info!("Is Root: {}, Can SU: {}", device_info.is_root, device_info.can_su);
4343

44-
if !device_info.is_root && device_info.can_su {
45-
log_info!("Attempting self-elevation...");
46-
try_elevate_privileges();
47-
log_info!("Failed to elevate. Continuing as non-root (PRoot mode only).");
44+
if !device_info.is_root {
45+
if device_info.can_su {
46+
log_info!("Not root. Attempting self-elevation...");
47+
try_elevate_privileges();
48+
49+
let err_msg = "Failed to gain root access. Please grant permission to the 'su' request.";
50+
log_error!("{}", err_msg);
51+
eprintln!("{}", err_msg);
52+
exit(1);
53+
} else {
54+
let err_msg = "Root access is required, but 'su' binary not found.";
55+
log_error!("{}", err_msg);
56+
eprintln!("{}", err_msg);
57+
exit(1);
58+
}
4859
}
4960

61+
log_info!("Root access confirmed. Initializing TUI...");
62+
5063
enable_raw_mode()?;
5164
let mut stdout = io::stdout();
5265
execute!(stdout, EnterAlternateScreen)?;

0 commit comments

Comments
 (0)