Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/bin/ctl/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,8 @@ impl std::fmt::Display for IpcError {
impl std::error::Error for IpcError {}

pub fn get_default_socket() -> String {
if let Ok(val) = std::env::var("INSTANTWM_SOCKET") {
return val;
}
format!("/tmp/instantwm-{}.sock", unsafe { libc::geteuid() })
}
29 changes: 20 additions & 9 deletions src/ipc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ pub struct IpcServer {

impl IpcServer {
pub fn bind() -> std::io::Result<Self> {
let path = socket_path();
if path.exists() {
let _ = fs::remove_file(&path);
}
let path = get_available_socket_path();
let listener = UnixListener::bind(&path)?;
listener.set_nonblocking(true)?;
unsafe { std::env::set_var("INSTANTWM_SOCKET", &path) };
Expand Down Expand Up @@ -112,12 +109,26 @@ impl std::os::unix::io::AsRawFd for IpcServer {
}
}

fn socket_path() -> PathBuf {
if let Ok(p) = std::env::var("INSTANTWM_SOCKET") {
return PathBuf::from(p);
}
fn get_available_socket_path() -> PathBuf {
let uid = unsafe { libc::geteuid() };
PathBuf::from(format!("/tmp/instantwm-{}.sock", uid))
let mut i = 0;
loop {
let path = if i == 0 {
PathBuf::from(format!("/tmp/instantwm-{}.sock", uid))
} else {
PathBuf::from(format!("/tmp/instantwm-{}-{}.sock", uid, i))
};

if path.exists() {
if UnixStream::connect(&path).is_ok() {
i += 1;
continue;
} else {
let _ = fs::remove_file(&path);
}
}
return path;
}
}

fn send_response(stream: &mut UnixStream, response: &Response) -> std::io::Result<()> {
Expand Down
Loading