diff options
author | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-25 16:26:31 +0530 |
---|---|---|
committer | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-25 16:26:31 +0530 |
commit | df6d6cb3a70cbea9d088c7af47f3540896b98089 (patch) | |
tree | 65e99393e0a685bab9498720ec0f4c8ad37ecbeb /src | |
parent | 151deb323578d599b9358db072b52ca712d950c6 (diff) |
check for residual socket files and clean them
Diffstat (limited to 'src')
-rw-r--r-- | src/sessions.rs | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/src/sessions.rs b/src/sessions.rs index ee72a34d0..fd834e408 100644 --- a/src/sessions.rs +++ b/src/sessions.rs @@ -1,6 +1,10 @@ use std::os::unix::fs::FileTypeExt; use std::{fs, io, process}; -use zellij_utils::consts::ZELLIJ_SOCK_DIR; +use zellij_utils::{ + consts::ZELLIJ_SOCK_DIR, + interprocess::local_socket::LocalSocketStream, + ipc::{ClientToServerMsg, IpcSenderWithContext}, +}; fn get_sessions() -> Result<Vec<String>, io::ErrorKind> { match fs::read_dir(&*ZELLIJ_SOCK_DIR) { @@ -8,8 +12,9 @@ fn get_sessions() -> Result<Vec<String>, io::ErrorKind> { let mut sessions = Vec::new(); files.for_each(|file| { let file = file.unwrap(); - if file.file_type().unwrap().is_socket() { - sessions.push(file.file_name().into_string().unwrap()); + let file_name = file.file_name().into_string().unwrap(); + if file.file_type().unwrap().is_socket() && assert_socket(&file_name) { + sessions.push(file_name); } }); Ok(sessions) @@ -84,3 +89,21 @@ pub(crate) fn assert_session_ne(name: &str) { }; process::exit(exit_code); } + +fn assert_socket(name: &str) -> bool { + let path = &*ZELLIJ_SOCK_DIR.join(name); + match LocalSocketStream::connect(path) { + Ok(stream) => { + IpcSenderWithContext::new(stream).send(ClientToServerMsg::ClientExited); + true + } + Err(e) => { + if e.kind() == io::ErrorKind::ConnectionRefused { + drop(fs::remove_file(path)); + false + } else { + true + } + } + } +} |