Use PID in socket name

This commit is contained in:
Ottatop 2024-05-07 20:46:02 -05:00
parent 37b728203c
commit bb33602724
2 changed files with 16 additions and 41 deletions

View file

@ -1,2 +1 @@
- Use compositor PID to uniquely ID gRPC socket
- Log git commit on startup and add to --help/-V - Log git commit on startup and add to --help/-V

View file

@ -295,6 +295,15 @@ pub struct Config {
pub config_dir: PathBuf, pub config_dir: PathBuf,
pub cli: Option<Cli>, pub cli: Option<Cli>,
socket_path: Option<PathBuf>,
}
impl Drop for Config {
fn drop(&mut self) {
if let Some(socket_path) = self.socket_path.as_ref() {
let _ = std::fs::remove_file(socket_path);
}
}
} }
impl Config { impl Config {
@ -307,6 +316,7 @@ impl Config {
shutdown_sender: None, shutdown_sender: None,
config_dir, config_dir,
cli, cli,
socket_path: None,
} }
} }
@ -556,51 +566,15 @@ impl Pinnacle {
self.system_processes self.system_processes
.refresh_processes_specifics(ProcessRefreshKind::new()); .refresh_processes_specifics(ProcessRefreshKind::new());
let multiple_instances = self
.system_processes
.processes_by_exact_name("pinnacle")
.filter(|proc| proc.thread_kind().is_none())
.count()
> 1;
std::fs::create_dir_all(socket_dir)?; std::fs::create_dir_all(socket_dir)?;
let socket_name = if multiple_instances { let socket_name = format!("pinnacle-grpc-{}.sock", std::process::id());
let mut suffix: u8 = 1;
while let Ok(true) = socket_dir
.join(format!("pinnacle-grpc-{suffix}.sock"))
.try_exists()
{
suffix += 1;
}
format!("pinnacle-grpc-{suffix}.sock")
} else {
"pinnacle-grpc.sock".to_string()
};
let socket_path = socket_dir.join(socket_name); let socket_path = socket_dir.join(socket_name);
// If there are multiple instances, don't touch other sockets if let Ok(true) = socket_path.try_exists() {
if multiple_instances { std::fs::remove_file(&socket_path)
if let Ok(true) = socket_path.try_exists() { .context(format!("Failed to remove old socket at {socket_path:?}"))?;
std::fs::remove_file(&socket_path)
.context(format!("Failed to remove old socket at {socket_path:?}"))?;
}
} else {
// If there aren't, remove them all
for file in std::fs::read_dir(socket_dir)?
.filter_map(|entry| entry.ok())
.filter(|entry| {
entry
.file_name()
.to_string_lossy()
.starts_with("pinnacle-grpc")
})
{
debug!("Removing socket at {:?}", file.path());
std::fs::remove_file(file.path())
.context(format!("Failed to remove old socket at {:?}", file.path()))?;
}
} }
std::env::set_var( std::env::set_var(
@ -657,6 +631,8 @@ impl Pinnacle {
info!("gRPC server started at {}", socket_path.display()); info!("gRPC server started at {}", socket_path.display());
self.config.socket_path = Some(socket_path);
Ok(()) Ok(())
} }
} }