From ed182b8b543053dd80972f700bf4a74bcee19de9 Mon Sep 17 00:00:00 2001 From: Amirhossein Akhlaghpour Date: Mon, 6 Apr 2026 21:19:59 +0330 Subject: [PATCH] fix: bootstrap install LaunchAgent in user session Signed-off-by: Amirhossein Akhlaghpour --- src/platform/macos.rs | 25 ++++++++++++++++++++ src/platform/privileges_scripts/install.scpt | 14 +++++++---- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/platform/macos.rs b/src/platform/macos.rs index 2e68cf5d8..35441b59f 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -312,6 +312,31 @@ fn correct_app_name(s: &str) -> String { s } +#[cfg(test)] +mod tests { + use super::{correct_app_name, PRIVILEGES_SCRIPTS_DIR}; + + #[test] + fn install_script_bootstraps_agent_into_user_domain() { + let install = PRIVILEGES_SCRIPTS_DIR + .get_file("install.scpt") + .and_then(|file| file.contents_utf8()) + .map(correct_app_name) + .expect("install.scpt should be embedded"); + + assert!( + install.contains("launchctl bootstrap gui/$uid") + || install.contains("launchctl bootstrap user/$uid"), + "install script must bootstrap the agent into a user launchd domain", + ); + assert!( + install.contains("launchctl kickstart -k gui/$uid/$agent_label") + || install.contains("launchctl kickstart -k user/$uid/$agent_label"), + "install script must kickstart the agent after bootstrapping it", + ); + } +} + pub fn uninstall_service(show_new_window: bool, sync: bool) -> bool { // to-do: do together with win/linux about refactory start/stop service if !is_installed_daemon(false) { diff --git a/src/platform/privileges_scripts/install.scpt b/src/platform/privileges_scripts/install.scpt index 797d02c9e..1ec34e345 100644 --- a/src/platform/privileges_scripts/install.scpt +++ b/src/platform/privileges_scripts/install.scpt @@ -1,16 +1,22 @@ on run {daemon_file, agent_file, user} + set daemon_plist to "/Library/LaunchDaemons/com.carriez.RustDesk_service.plist" + set agent_plist to "/Library/LaunchAgents/com.carriez.RustDesk_server.plist" - set sh1 to "echo " & quoted form of daemon_file & " > /Library/LaunchDaemons/com.carriez.RustDesk_service.plist && chown root:wheel /Library/LaunchDaemons/com.carriez.RustDesk_service.plist;" + set sh1 to "echo " & quoted form of daemon_file & " > " & daemon_plist & " && chown root:wheel " & daemon_plist & ";" - set sh2 to "echo " & quoted form of agent_file & " > /Library/LaunchAgents/com.carriez.RustDesk_server.plist && chown root:wheel /Library/LaunchAgents/com.carriez.RustDesk_server.plist;" + set sh2 to "echo " & quoted form of agent_file & " > " & agent_plist & " && chown root:wheel " & agent_plist & ";" set sh3 to "cp -rf /Users/" & user & "/Library/Preferences/com.carriez.RustDesk/RustDesk.toml /var/root/Library/Preferences/com.carriez.RustDesk/;" set sh4 to "cp -rf /Users/" & user & "/Library/Preferences/com.carriez.RustDesk/RustDesk2.toml /var/root/Library/Preferences/com.carriez.RustDesk/;" - set sh5 to "launchctl load -w /Library/LaunchDaemons/com.carriez.RustDesk_service.plist;" + set sh5 to "uid=$(id -u " & quoted form of user & " 2>/dev/null || true);" + set sh6 to "launchctl load -w " & daemon_plist & ";" + set sh7 to "agent_label=$(basename " & quoted form of agent_plist & " .plist);" + set sh8 to "if [ -n \"$uid\" ]; then launchctl bootstrap gui/$uid " & quoted form of agent_plist & " 2>/dev/null || launchctl bootstrap user/$uid " & quoted form of agent_plist & " 2>/dev/null || launchctl load -w " & quoted form of agent_plist & " || true; else launchctl load -w " & quoted form of agent_plist & " || true; fi;" + set sh9 to "if [ -n \"$uid\" ]; then launchctl kickstart -k gui/$uid/$agent_label 2>/dev/null || launchctl kickstart -k user/$uid/$agent_label 2>/dev/null || true; fi;" - set sh to sh1 & sh2 & sh3 & sh4 & sh5 + set sh to "set -e;" & sh1 & sh2 & sh3 & sh4 & sh5 & sh6 & sh7 & sh8 & sh9 do shell script sh with prompt "RustDesk wants to install daemon and agent" with administrator privileges end run