refactor linux install/uninstall service as windows

This commit is contained in:
rustdesk
2023-06-07 23:08:50 +08:00
parent 209e460f3f
commit 7f51f8d976
8 changed files with 309 additions and 195 deletions

View File

@@ -3,6 +3,7 @@ use desktop::Desktop;
pub use hbb_common::platform::linux::*;
use hbb_common::{
allow_err, bail,
config::Config,
libc::{c_char, c_int, c_long, c_void},
log,
message_proto::Resolution,
@@ -306,10 +307,15 @@ fn force_stop_server() {
}
pub fn start_os_service() {
check_if_stop_service();
stop_rustdesk_servers();
stop_subprocess();
start_uinput_service();
std::thread::spawn(|| {
allow_err!(crate::ipc::start(crate::POSTFIX_SERVICE));
});
let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
let mut desktop = Desktop::default();
@@ -1075,3 +1081,86 @@ impl WakeLock {
)
}
}
fn has_cmd(cmd: &str) -> bool {
std::process::Command::new("which")
.arg(cmd)
.status()
.map(|x| x.success())
.unwrap_or_default()
}
pub fn run_cmds_pkexec(cmds: &str) -> bool {
const DONE: &str = "RUN_CMDS_PKEXEC_DONE";
if let Ok(output) = std::process::Command::new("pkexec")
.arg("sh")
.arg("-c")
.arg(&format!("{cmds};echo {DONE}"))
.output()
{
let out = String::from_utf8_lossy(&output.stdout);
log::debug!("cmds: {cmds}");
log::debug!("output: {out}");
out.contains(DONE)
} else {
false
}
}
pub fn run_me_with(secs: u32) {
let exe = std::env::current_exe()
.unwrap_or("".into())
.to_string_lossy()
.to_string();
std::process::Command::new("sh")
.arg("-c")
.arg(&format!("sleep {secs}; {exe}"))
.spawn()
.ok();
}
pub fn uninstall_service(show_new_window: bool) -> bool {
if !has_cmd("systemctl") {
return false;
}
log::info!("Uninstalling service...");
Config::set_option("stop-service".into(), "Y".into());
if !run_cmds_pkexec("systemctl disable rustdesk; systemctl stop rustdesk") {
Config::set_option("stop-service".into(), "".into());
return true;
}
if show_new_window {
run_me_with(2);
}
std::process::exit(0);
}
pub fn install_service() -> bool {
if !has_cmd("systemctl") {
return false;
}
log::info!("Installing service...");
let home = std::env::var("HOME").unwrap_or_default();
let cp = if home != "/root" {
format!("cp -f {home}/.config/rustdesk/RustDesk.toml /root/.config/rustdesk/; cp -f {home}/.config/rustdesk/RustDesk2.toml /root/.config/rustdesk/;")
} else {
"".to_owned()
};
Config::set_option("stop-service".into(), "".into());
if !run_cmds_pkexec(&format!(
"{cp}systemctl enable rustdesk; systemctl start rustdesk"
)) {
Config::set_option("stop-service".into(), "Y".into());
return true;
}
run_me_with(2);
std::process::exit(0);
}
fn check_if_stop_service() {
if Config::get_option("stop-service".into()) == "Y" {
allow_err!(run_cmds(
"systemctl disable rustdesk; systemctl stop rustdesk"
));
}
}

View File

@@ -2151,7 +2151,7 @@ impl Drop for WakeLock {
}
}
pub fn uninstall_service(show_new_window: bool) -> ResultType<()> {
pub fn uninstall_service(show_new_window: bool) -> bool {
log::info!("Uninstalling service...");
let filter = format!(" /FI \"PID ne {}\"", get_current_pid());
Config::set_option("stop-service".into(), "Y".into());
@@ -2167,13 +2167,14 @@ pub fn uninstall_service(show_new_window: bool) -> ResultType<()> {
);
if let Err(err) = run_cmds(cmds, false, "uninstall") {
Config::set_option("stop-service".into(), "".into());
bail!(err);
log::debug!("{err}");
return true;
}
run_after_run_cmds(!show_new_window);
std::process::exit(0);
}
pub fn install_service() -> ResultType<()> {
pub fn install_service() -> bool {
log::info!("Installing service...");
let (_, _, _, exe) = get_install_info();
let tmp_path = std::env::temp_dir().to_string_lossy().to_string();
@@ -2198,7 +2199,8 @@ if exist \"{tray_shortcut}\" del /f /q \"{tray_shortcut}\"
if let Err(err) = run_cmds(cmds, false, "install") {
Config::set_option("stop-service".into(), "Y".into());
crate::ipc::EXIT_RECV_CLOSE.store(true, Ordering::Relaxed);
bail!(err);
log::debug!("{err}");
return true;
}
run_after_run_cmds(false);
std::process::exit(0);