linux, wrap pkexec

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2023-05-12 16:08:15 +08:00
parent 98a5f5ef6c
commit 59987f637c
3 changed files with 37 additions and 51 deletions

View File

@@ -648,6 +648,10 @@ pub fn quit_gui() {
unsafe { gtk_main_quit() };
}
pub fn exec_privileged(args: &[&str]) -> ResultType<Child> {
Ok(Command::new("pkexec").args(args).spawn()?)
}
pub fn check_super_user_permission() -> ResultType<bool> {
let file = "/usr/share/rustdesk/files/polkit";
let arg;
@@ -656,11 +660,11 @@ pub fn check_super_user_permission() -> ResultType<bool> {
} else {
arg = "echo";
}
let status = Command::new("pkexec").arg(arg).status()?;
let status = exec_privileged(&[arg])?.wait()?;
Ok(status.success() && status.code() == Some(0))
}
pub fn elevate(args: Vec<&str>) -> ResultType<Option<Child>> {
pub fn elevate(args: Vec<&str>) -> ResultType<bool> {
let cmd = std::env::current_exe()?;
match cmd.to_str() {
Some(cmd) => {
@@ -670,8 +674,24 @@ pub fn elevate(args: Vec<&str>) -> ResultType<Option<Child>> {
if is_opensuse() {
args_with_exe.insert(0, "-E");
}
let task = Command::new("pkexec").args(args_with_exe).spawn()?;
Ok(Some(task))
let res = match exec_privileged(&args_with_exe)?.wait() {
Ok(status) => {
if status.success() {
true
} else {
log::error!(
"Failed to wait install process, process status: {:?}",
status
);
false
}
}
Err(e) => {
log::error!("Failed to wait install process, error: {}", e);
false
}
};
Ok(res)
}
None => {
hbb_common::bail!("Failed to get current exe as str");