fix(linux): retry with sudo when pkexec execution failed (#5469)

This commit is contained in:
Su Guoyu
2025-11-16 14:52:13 +08:00
committed by GitHub
parent dbb4877be6
commit 29d8df40b9

View File

@@ -136,11 +136,28 @@ async fn uninstall_service() -> Result<()> {
let status = if linux_running_as_root() { let status = if linux_running_as_root() {
StdCommand::new(&uninstall_path).status()? StdCommand::new(&uninstall_path).status()?
} else { } else {
StdCommand::new(elevator) let result = StdCommand::new(&elevator)
.arg("sh") .arg("sh")
.arg("-c") .arg("-c")
.arg(uninstall_shell) .arg(&uninstall_shell)
.status()? .status()?;
// 如果 pkexec 执行失败,回退到 sudo
if !result.success() && elevator.contains("pkexec") {
logging!(
warn,
Type::Service,
"pkexec failed with code {}, falling back to sudo",
result.code().unwrap_or(-1)
);
StdCommand::new("sudo")
.arg("sh")
.arg("-c")
.arg(&uninstall_shell)
.status()?
} else {
result
}
}; };
logging!( logging!(
info, info,
@@ -177,11 +194,28 @@ async fn install_service() -> Result<()> {
let status = if linux_running_as_root() { let status = if linux_running_as_root() {
StdCommand::new(&install_path).status()? StdCommand::new(&install_path).status()?
} else { } else {
StdCommand::new(elevator) let result = StdCommand::new(&elevator)
.arg("sh") .arg("sh")
.arg("-c") .arg("-c")
.arg(install_shell) .arg(&install_shell)
.status()? .status()?;
// 如果 pkexec 执行失败,回退到 sudo
if !result.success() && elevator.contains("pkexec") {
logging!(
warn,
Type::Service,
"pkexec failed with code {}, falling back to sudo",
result.code().unwrap_or(-1)
);
StdCommand::new("sudo")
.arg("sh")
.arg("-c")
.arg(&install_shell)
.status()?
} else {
result
}
}; };
logging!( logging!(
info, info,