fix: use file locking instead of abstract socket for single instance to avoid rust null byte error
This commit is contained in:
parent
66351de4fd
commit
5242473b93
3 changed files with 35 additions and 14 deletions
|
|
@ -504,15 +504,25 @@ fn recover_display_env() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn ensure_single_instance(name: &str) {
|
||||||
|
let uid = unsafe { libc::getuid() };
|
||||||
|
let path = format!("/tmp/{}-{}.lock", name, uid);
|
||||||
|
let path_c = std::ffi::CString::new(path).unwrap();
|
||||||
|
let fd = unsafe { libc::open(path_c.as_ptr(), libc::O_CREAT | libc::O_RDWR, 0o600) };
|
||||||
|
if fd < 0 {
|
||||||
|
eprintln!("[{}] Failed to open lock file", name);
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
let res = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) };
|
||||||
|
if res < 0 {
|
||||||
|
eprintln!("[{}] Another instance is already running. Exiting.", name);
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
// Ensure single instance to avoid duplicate daemon processes
|
// Ensure single instance to avoid duplicate daemon processes
|
||||||
let _listener = match std::os::unix::net::UnixListener::bind("\0vietc-daemon-lock") {
|
ensure_single_instance("vietc-daemon");
|
||||||
Ok(l) => l,
|
|
||||||
Err(_) => {
|
|
||||||
eprintln!("[vietc] Another instance is already running. Exiting.");
|
|
||||||
std::process::exit(0);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
recover_display_env();
|
recover_display_env();
|
||||||
let config_path = config::find_config_path();
|
let config_path = config::find_config_path();
|
||||||
|
|
|
||||||
|
|
@ -14,3 +14,4 @@ toml = "0.8"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
dirs = "5"
|
dirs = "5"
|
||||||
|
libc = "0.2.186"
|
||||||
|
|
|
||||||
|
|
@ -179,15 +179,25 @@ fn config_path() -> PathBuf {
|
||||||
.join("vietc")
|
.join("vietc")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn ensure_single_instance(name: &str) {
|
||||||
|
let uid = unsafe { libc::getuid() };
|
||||||
|
let path = format!("/tmp/{}-{}.lock", name, uid);
|
||||||
|
let path_c = std::ffi::CString::new(path).unwrap();
|
||||||
|
let fd = unsafe { libc::open(path_c.as_ptr(), libc::O_CREAT | libc::O_RDWR, 0o600) };
|
||||||
|
if fd < 0 {
|
||||||
|
eprintln!("[{}] Failed to open lock file", name);
|
||||||
|
std::process::exit(1);
|
||||||
|
}
|
||||||
|
let res = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) };
|
||||||
|
if res < 0 {
|
||||||
|
eprintln!("[{}] Another instance is already running. Exiting.", name);
|
||||||
|
std::process::exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Ensure single instance to avoid duplicate tray icons
|
// Ensure single instance to avoid duplicate tray icons
|
||||||
let _listener = match std::os::unix::net::UnixListener::bind("\0vietc-tray-lock") {
|
ensure_single_instance("vietc-tray");
|
||||||
Ok(l) => l,
|
|
||||||
Err(_) => {
|
|
||||||
eprintln!("[vietc-tray] Another instance is already running. Exiting.");
|
|
||||||
std::process::exit(0);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
eprintln!("[vietc-tray] Starting");
|
eprintln!("[vietc-tray] Starting");
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue