From 4dd3df2326c7a1346a00697f78bf58684a707b9f Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Wed, 2 Jun 2021 12:28:35 +0200 Subject: [PATCH] feat(core): prevent espanso from starting if legacy instance is running --- espanso/src/cli/daemon/mod.rs | 17 +++++++++-------- espanso/src/cli/worker/mod.rs | 15 +++++++-------- espanso/src/exit_code.rs | 4 +++- espanso/src/lock.rs | 8 ++++++-- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/espanso/src/cli/daemon/mod.rs b/espanso/src/cli/daemon/mod.rs index 6cd46d1..7864d71 100644 --- a/espanso/src/cli/daemon/mod.rs +++ b/espanso/src/cli/daemon/mod.rs @@ -27,14 +27,7 @@ use espanso_ipc::IPCClient; use espanso_path::Paths; use log::{error, info, warn}; -use crate::{ - exit_code::{ - DAEMON_ALREADY_RUNNING, DAEMON_GENERAL_ERROR, DAEMON_SUCCESS, WORKER_EXIT_ALL_PROCESSES, - WORKER_RESTART, WORKER_SUCCESS, - }, - ipc::{create_ipc_client_to_worker, IPCEvent}, - lock::{acquire_daemon_lock, acquire_worker_lock}, -}; +use crate::{exit_code::{DAEMON_ALREADY_RUNNING, DAEMON_GENERAL_ERROR, DAEMON_LEGACY_ALREADY_RUNNING, DAEMON_SUCCESS, WORKER_EXIT_ALL_PROCESSES, WORKER_RESTART, WORKER_SUCCESS}, ipc::{create_ipc_client_to_worker, IPCEvent}, lock::{acquire_daemon_lock, acquire_legacy_lock, acquire_worker_lock}}; use super::{CliModule, CliModuleArgs}; @@ -65,6 +58,14 @@ fn daemon_main(args: CliModuleArgs) -> i32 { return DAEMON_ALREADY_RUNNING; } + let legacy_lock_file = acquire_legacy_lock(&paths.runtime); + if legacy_lock_file.is_none() { + // TODO: show a (blocking) alert message using modulo + + error!("an instance of legacy espanso is running, please terminate it, otherwise the new version cannot start"); + return DAEMON_LEGACY_ALREADY_RUNNING; + } + // TODO: we might need to check preconditions: accessibility on macOS, presence of binaries on Linux, etc info!("espanso version: {}", VERSION); diff --git a/espanso/src/cli/worker/mod.rs b/espanso/src/cli/worker/mod.rs index da527d8..22855af 100644 --- a/espanso/src/cli/worker/mod.rs +++ b/espanso/src/cli/worker/mod.rs @@ -20,14 +20,7 @@ use crossbeam::channel::unbounded; use log::{error, info}; -use crate::{ - engine::event::ExitMode, - exit_code::{ - WORKER_ALREADY_RUNNING, WORKER_EXIT_ALL_PROCESSES, WORKER_GENERAL_ERROR, WORKER_RESTART, - WORKER_SUCCESS, - }, - lock::acquire_worker_lock, -}; +use crate::{engine::event::ExitMode, exit_code::{WORKER_ALREADY_RUNNING, WORKER_EXIT_ALL_PROCESSES, WORKER_GENERAL_ERROR, WORKER_LEGACY_ALREADY_RUNNING, WORKER_RESTART, WORKER_SUCCESS}, lock::{acquire_legacy_lock, acquire_worker_lock}}; use self::ui::util::convert_icon_paths_to_tray_vec; @@ -62,6 +55,12 @@ fn worker_main(args: CliModuleArgs) -> i32 { return WORKER_ALREADY_RUNNING; } + let legacy_lock_file = acquire_legacy_lock(&paths.runtime); + if legacy_lock_file.is_none() { + error!("an instance of legacy espanso is running, please terminate it, otherwise the new version cannot start"); + return WORKER_LEGACY_ALREADY_RUNNING; + } + let config_store = args .config_store .expect("missing config store in worker main"); diff --git a/espanso/src/exit_code.rs b/espanso/src/exit_code.rs index 26e0c38..261da3e 100644 --- a/espanso/src/exit_code.rs +++ b/espanso/src/exit_code.rs @@ -20,9 +20,11 @@ pub const WORKER_SUCCESS: i32 = 0; pub const WORKER_ALREADY_RUNNING: i32 = 1; pub const WORKER_GENERAL_ERROR: i32 = 2; +pub const WORKER_LEGACY_ALREADY_RUNNING: i32 = 3; pub const WORKER_EXIT_ALL_PROCESSES: i32 = 101; pub const WORKER_RESTART: i32 = 102; pub const DAEMON_SUCCESS: i32 = 0; pub const DAEMON_ALREADY_RUNNING: i32 = 1; -pub const DAEMON_GENERAL_ERROR: i32 = 2; \ No newline at end of file +pub const DAEMON_GENERAL_ERROR: i32 = 2; +pub const DAEMON_LEGACY_ALREADY_RUNNING: i32 = 3; \ No newline at end of file diff --git a/espanso/src/lock.rs b/espanso/src/lock.rs index ea85613..b901e48 100644 --- a/espanso/src/lock.rs +++ b/espanso/src/lock.rs @@ -64,9 +64,13 @@ impl Drop for Lock { } pub fn acquire_daemon_lock(runtime_dir: &Path) -> Option { - Lock::acquire(runtime_dir, "espanso-daemon.lock") + Lock::acquire(runtime_dir, "espanso-daemon") } pub fn acquire_worker_lock(runtime_dir: &Path) -> Option { - Lock::acquire(runtime_dir, "espanso-worker.lock") + Lock::acquire(runtime_dir, "espanso-worker") +} + +pub fn acquire_legacy_lock(runtime_dir: &Path) -> Option { + Lock::acquire(runtime_dir, "espanso") }