use std::fs::Permissions;
use std::os::unix::fs::{FileTypeExt, PermissionsExt};
use std::os::unix::net::UnixListener;
use std::path::{Path, PathBuf};
use std::sync::mpsc;
use std::{env, fs, net, thread};
pub fn start(socket_path: PathBuf, tx: mpsc::Sender<()>) -> thread::JoinHandle<()> {
thread::spawn(move || bind_listener(&socket_path, tx).unwrap())
}
fn bind_listener(socket_path: &Path, sync_tx: mpsc::Sender<()>) -> std::io::Result<()> {
if let Ok(meta) = fs::metadata(&socket_path) {
if meta.file_type().is_socket() {
fs::remove_file(&socket_path)?;
}
}
debug!("binding unix socket on {:?}", socket_path);
let listener = UnixListener::bind(&socket_path)?;
if let Ok(mode) = env::var("UNIX_LISTENER_MODE") {
let perms = Permissions::from_mode(mode.parse().expect("invalid UNIX_LISTERN_MODE"));
fs::set_permissions(&socket_path, perms).unwrap();
}
for stream in listener.incoming() {
trace!("received sync notification via unix socket");
stream.and_then(|s| s.shutdown(net::Shutdown::Both)).ok();
if sync_tx.send(()).is_err() {
break;
}
}
Ok(())
}