use std::io::{self, Read, Write};
use std::path::Path;
use std::time::Duration;
pub struct Rendezvous(backend::Rendezvous);
pub enum Joined {
Owner(Ownership),
Peer(Connection),
}
pub struct Ownership(backend::Ownership);
pub struct Listener(backend::Listener);
pub struct Connection(backend::Connection);
impl Rendezvous {
pub fn beside(resource: &Path) -> io::Result<Self> {
backend::Rendezvous::beside(resource).map(Self)
}
pub fn join(
&self,
timeout: Duration,
mut greet: impl FnMut(&Connection) -> io::Result<()>,
) -> io::Result<Joined> {
let started = tilo_clock::now();
loop {
if let Ok(connection) = self.0.connect().map(Connection)
&& greet(&connection).is_ok()
{
return Ok(Joined::Peer(connection));
}
if let Some(ownership) = self.0.try_own()? {
return Ok(Joined::Owner(Ownership(ownership)));
}
if started.elapsed() >= timeout {
return Err(io::Error::new(
io::ErrorKind::TimedOut,
"the process that owns this resource did not answer",
));
}
std::thread::sleep(Duration::from_millis(25));
}
}
}
impl Ownership {
pub fn listen(&self) -> io::Result<Listener> {
self.0.listen().map(Listener)
}
pub fn hold_with(&self, command: &mut std::process::Command) {
self.0.hold_with(command);
}
}
impl Listener {
pub fn accept_within(&self, wait: Duration) -> io::Result<Option<Connection>> {
Ok(self.0.accept_within(wait)?.map(Connection))
}
pub fn close(&self) {
self.0.close();
}
}
impl Connection {
pub fn try_clone(&self) -> io::Result<Self> {
self.0.try_clone().map(Self)
}
pub fn close(&self) {
self.0.close();
}
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
self.0.set_read_timeout(timeout)
}
}
impl Read for &Connection {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.0.read(buf)
}
}
impl Write for &Connection {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
}
impl Read for Connection {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let mut shared: &Self = self;
shared.read(buf)
}
}
impl Write for Connection {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let mut shared: &Self = self;
shared.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
let mut shared: &Self = self;
shared.flush()
}
}
#[cfg(unix)]
mod backend {
use std::io::{self, Read, Write};
use std::net::Shutdown;
use std::os::fd::AsRawFd;
use std::os::unix::fs::{MetadataExt, OpenOptionsExt, PermissionsExt};
use std::os::unix::net::{SocketAddr, UnixListener, UnixStream};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::Duration;
pub struct Rendezvous {
socket: PathBuf,
address: Arc<Address>,
lock: PathBuf,
}
pub struct Ownership {
socket: PathBuf,
address: Arc<Address>,
lock: advisory_lock::FileLock,
}
enum Address {
Path(PathBuf),
Beside {
directory: relative::Directory,
name: PathBuf,
},
}
impl Address {
fn new(socket: &Path) -> io::Result<Self> {
if SocketAddr::from_pathname(socket).is_ok() {
return Ok(Self::Path(socket.to_owned()));
}
let too_long = || {
io::Error::new(
io::ErrorKind::InvalidInput,
format!(
"the socket path {} is too long for a local socket",
socket.display()
),
)
};
let (Some(parent), Some(name)) = (socket.parent(), socket.file_name()) else {
return Err(too_long());
};
let name = PathBuf::from(name);
if SocketAddr::from_pathname(&name).is_err() {
return Err(too_long());
}
Ok(Self::Beside {
directory: relative::Directory::open(parent)?,
name,
})
}
fn bind(&self) -> io::Result<UnixListener> {
match self {
Self::Path(path) => tilo_files::native::socket::bind(path),
Self::Beside { directory, name } => relative::bind(directory, name),
}
}
fn connect(&self) -> io::Result<UnixStream> {
match self {
Self::Path(path) => tilo_files::native::socket::connect(path),
Self::Beside { directory, name } => relative::connect(directory, name),
}
}
}
#[cfg(not(target_vendor = "apple"))]
mod relative {
use std::io;
use std::os::fd::{AsRawFd, OwnedFd};
use std::os::unix::fs::OpenOptionsExt;
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::{Path, PathBuf};
pub struct Directory(OwnedFd);
impl Directory {
pub fn open(path: &Path) -> io::Result<Self> {
let directory = std::fs::OpenOptions::new()
.read(true)
.custom_flags(libc::O_DIRECTORY | libc::O_CLOEXEC)
.open(path)?;
Ok(Self(directory.into()))
}
fn through(&self, name: &Path) -> PathBuf {
PathBuf::from(format!("/proc/self/fd/{}", self.0.as_raw_fd())).join(name)
}
}
pub fn bind(directory: &Directory, name: &Path) -> io::Result<UnixListener> {
tilo_files::native::socket::bind(&directory.through(name))
}
pub fn connect(directory: &Directory, name: &Path) -> io::Result<UnixStream> {
tilo_files::native::socket::connect(&directory.through(name))
}
}
#[cfg(target_vendor = "apple")]
mod relative {
use std::ffi::{CString, OsString};
use std::io;
use std::os::unix::ffi::OsStringExt;
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::{Path, PathBuf};
pub struct Directory(PathBuf);
impl Directory {
pub fn open(path: &Path) -> io::Result<Self> {
path.canonicalize().map(Self)
}
fn through<T>(
&self,
name: &Path,
call: impl FnOnce(PathBuf) -> io::Result<T>,
) -> io::Result<T> {
let template = CString::new("/tmp/tilo-socket-XXXXXX").map_err(io::Error::other)?;
let mut template = template.into_bytes_with_nul();
let made = unsafe { libc::mkdtemp(template.as_mut_ptr().cast()) };
if made.is_null() {
return Err(io::Error::last_os_error());
}
template.pop();
let short = PathBuf::from(OsString::from_vec(template));
let link = short.join("d");
let result =
std::os::unix::fs::symlink(&self.0, &link).and_then(|()| call(link.join(name)));
let _ = std::fs::remove_file(&link);
let _ = std::fs::remove_dir(&short);
result
}
}
pub fn bind(directory: &Directory, name: &Path) -> io::Result<UnixListener> {
directory.through(name, |short| tilo_files::native::socket::bind(&short))
}
pub fn connect(directory: &Directory, name: &Path) -> io::Result<UnixStream> {
directory.through(name, |short| tilo_files::native::socket::connect(&short))
}
}
pub struct Listener {
socket: PathBuf,
identity: (u64, u64),
bound: UnixListener,
}
pub struct Connection(UnixStream);
fn identity(metadata: &std::fs::Metadata) -> (u64, u64) {
(metadata.dev(), metadata.ino())
}
impl Rendezvous {
pub fn beside(resource: &Path) -> io::Result<Self> {
let resource = resource.canonicalize()?;
let (Some(parent), Some(name)) = (resource.parent(), resource.file_name()) else {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"a resource at the filesystem root has no place beside it",
));
};
let sibling = |suffix: &str| {
let mut file = name.to_os_string();
file.push(suffix);
parent.join(file)
};
let socket = sibling(".sock");
let address = Arc::new(Address::new(&socket)?);
Ok(Self {
socket,
address,
lock: sibling(".lock"),
})
}
pub fn connect(&self) -> io::Result<Connection> {
self.address.connect().map(Connection)
}
pub fn try_own(&self) -> io::Result<Option<Ownership>> {
let file = std::fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(false)
.mode(0o600)
.open(&self.lock)?;
let lock = match advisory_lock::FileLock::try_exclusive(file) {
Ok(lock) => lock,
Err(std::fs::TryLockError::WouldBlock) => return Ok(None),
Err(std::fs::TryLockError::Error(error)) => return Err(error),
};
let held = lock.metadata()?;
if !std::fs::metadata(&self.lock).is_ok_and(|path| identity(&path) == identity(&held)) {
return Ok(None);
}
match std::fs::remove_file(&self.socket) {
Ok(()) => {}
Err(error) if error.kind() == io::ErrorKind::NotFound => {}
Err(error) => return Err(error),
}
Ok(Some(Ownership {
socket: self.socket.clone(),
address: self.address.clone(),
lock,
}))
}
}
impl Ownership {
pub fn listen(&self) -> io::Result<Listener> {
let listener = self.address.bind()?;
let secured = listener
.set_nonblocking(true)
.and_then(|()| {
std::fs::set_permissions(&self.socket, std::fs::Permissions::from_mode(0o600))
})
.and_then(|()| std::fs::symlink_metadata(&self.socket));
match secured {
Ok(metadata) => Ok(Listener {
socket: self.socket.clone(),
identity: identity(&metadata),
bound: listener,
}),
Err(error) => {
let _ = std::fs::remove_file(&self.socket);
Err(error)
}
}
}
pub fn hold_with(&self, command: &mut std::process::Command) {
use std::os::unix::process::CommandExt;
let fd = self.lock.as_raw_fd();
unsafe {
command.pre_exec(move || {
if libc::fcntl(fd, libc::F_SETFD, 0) == -1 {
return Err(io::Error::last_os_error());
}
Ok(())
});
}
}
}
impl Listener {
pub fn accept_within(&self, wait: Duration) -> io::Result<Option<Connection>> {
let mut poll = libc::pollfd {
fd: self.bound.as_raw_fd(),
events: libc::POLLIN,
revents: 0,
};
let millis = libc::c_int::try_from(wait.as_millis()).unwrap_or(libc::c_int::MAX);
let ready = unsafe { libc::poll(&raw mut poll, 1, millis) };
if ready < 0 {
let error = io::Error::last_os_error();
return if error.kind() == io::ErrorKind::Interrupted {
Ok(None)
} else {
Err(error)
};
}
if ready == 0 {
return Ok(None);
}
match self.bound.accept() {
Ok((stream, _)) => {
stream.set_nonblocking(false)?;
Ok(Some(Connection(stream)))
}
Err(error) if error.kind() == io::ErrorKind::WouldBlock => Ok(None),
Err(error) => Err(error),
}
}
pub fn close(&self) {
if std::fs::symlink_metadata(&self.socket)
.is_ok_and(|metadata| identity(&metadata) == self.identity)
{
let _ = std::fs::remove_file(&self.socket);
}
}
}
impl Drop for Listener {
fn drop(&mut self) {
self.close();
}
}
impl Connection {
pub fn try_clone(&self) -> io::Result<Self> {
self.0.try_clone().map(Self)
}
pub fn close(&self) {
let _ = self.0.shutdown(Shutdown::Both);
}
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
self.0.set_read_timeout(timeout)
}
pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
(&self.0).read(buf)
}
pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
(&self.0).write(buf)
}
pub fn flush(&self) -> io::Result<()> {
(&self.0).flush()
}
}
}
#[cfg(not(unix))]
mod backend {
use std::io;
use std::path::Path;
use std::time::Duration;
pub enum Never {}
pub struct Rendezvous(Never);
pub struct Ownership(Never);
pub struct Listener(Never);
pub struct Connection(Never);
impl Rendezvous {
pub fn beside(_: &Path) -> io::Result<Self> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"local sockets are not available on this platform",
))
}
pub fn connect(&self) -> io::Result<Connection> {
match self.0 {}
}
pub fn try_own(&self) -> io::Result<Option<Ownership>> {
match self.0 {}
}
}
impl Ownership {
pub fn listen(&self) -> io::Result<Listener> {
match self.0 {}
}
pub fn hold_with(&self, _: &mut std::process::Command) {
match self.0 {}
}
}
impl Listener {
pub fn accept_within(&self, _: Duration) -> io::Result<Option<Connection>> {
match self.0 {}
}
pub fn close(&self) {
match self.0 {}
}
}
impl Connection {
pub fn try_clone(&self) -> io::Result<Self> {
match self.0 {}
}
pub fn close(&self) {
match self.0 {}
}
pub fn set_read_timeout(&self, _: Option<Duration>) -> io::Result<()> {
match self.0 {}
}
pub fn read(&self, _: &mut [u8]) -> io::Result<usize> {
match self.0 {}
}
pub fn write(&self, _: &[u8]) -> io::Result<usize> {
match self.0 {}
}
pub fn flush(&self) -> io::Result<()> {
match self.0 {}
}
}
}
#[cfg(test)]
#[cfg(unix)]
mod tests {
use super::*;
#[allow(
clippy::unnecessary_wraps,
reason = "the signature `Rendezvous::join` requires of a greeting"
)]
fn greet(_: &Connection) -> io::Result<()> {
Ok(())
}
#[test]
fn the_first_process_owns_and_the_next_connects() {
let dir = tempfile::tempdir().unwrap();
let resource = dir.path().join("profile");
std::fs::create_dir(&resource).unwrap();
let rendezvous = Rendezvous::beside(&resource).unwrap();
let Joined::Owner(owner) = rendezvous.join(tilo_test_env::hang_bound(), greet).unwrap()
else {
panic!("nobody owned the resource");
};
let listener = owner.listen().unwrap();
let socket = dir.path().canonicalize().unwrap().join("profile.sock");
assert!(socket.exists());
let Joined::Peer(peer) = rendezvous.join(tilo_test_env::hang_bound(), greet).unwrap()
else {
panic!("the owner was not reached");
};
let accepted = listener
.accept_within(tilo_test_env::hang_bound())
.unwrap()
.unwrap();
(&peer).write_all(b"hello").unwrap();
let mut read = [0; 5];
(&accepted).read_exact(&mut read).unwrap();
assert_eq!(&read, b"hello");
assert!(
listener
.accept_within(Duration::from_millis(10))
.unwrap()
.is_none()
);
listener.close();
assert!(!socket.exists());
drop(owner);
}
tilo_test_env::scenario! {
fn a_rendezvous_replaces_a_dead_owner_meets_deep_resources_and_refuses_long_paths;
phase a_dead_owners_socket_is_replaced_and_a_busy_lock_times_out;
{
let dir = tempfile::tempdir().unwrap();
let resource = dir.path().join("profile");
std::fs::create_dir(&resource).unwrap();
let socket = dir.path().canonicalize().unwrap().join("profile.sock");
drop(std::os::unix::net::UnixListener::bind(&socket).unwrap());
let rendezvous = Rendezvous::beside(&resource).unwrap();
let Joined::Owner(owner) = rendezvous.join(tilo_test_env::hang_bound(), greet).unwrap()
else {
panic!("a dead owner's socket answered");
};
assert!(!socket.exists(), "the stale socket is removed");
let error = rendezvous
.join(Duration::from_millis(100), greet)
.err()
.unwrap();
assert_eq!(error.kind(), io::ErrorKind::TimedOut);
drop(owner);
assert!(matches!(
rendezvous.join(tilo_test_env::hang_bound(), greet).unwrap(),
Joined::Owner(_)
));
}
phase a_resource_deeper_than_a_socket_address_still_meets;
{
let dir = tempfile::tempdir().unwrap();
let deep = dir.path().join("d".repeat(60)).join("e".repeat(60));
std::fs::create_dir_all(&deep).unwrap();
let resource = deep.join("profile");
std::fs::create_dir(&resource).unwrap();
let socket = deep.canonicalize().unwrap().join("profile.sock");
assert!(std::os::unix::net::SocketAddr::from_pathname(&socket).is_err());
let rendezvous = Rendezvous::beside(&resource).unwrap();
let Joined::Owner(owner) = rendezvous.join(tilo_test_env::hang_bound(), greet).unwrap()
else {
panic!("nobody owned the resource");
};
let listener = owner.listen().unwrap();
assert!(socket.exists(), "the socket is bound beside the resource");
let Joined::Peer(peer) = rendezvous.join(tilo_test_env::hang_bound(), greet).unwrap()
else {
panic!("the owner was not reached");
};
let accepted = listener
.accept_within(tilo_test_env::hang_bound())
.unwrap()
.unwrap();
(&peer).write_all(b"hello").unwrap();
let mut read = [0; 5];
(&accepted).read_exact(&mut read).unwrap();
assert_eq!(&read, b"hello");
listener.close();
assert!(!socket.exists());
drop(owner);
}
phase a_socket_path_too_long_is_refused;
{
let dir = tempfile::tempdir().unwrap();
let resource = dir.path().join("x".repeat(120));
std::fs::create_dir(&resource).unwrap();
let error = Rendezvous::beside(&resource).err().unwrap();
assert_eq!(error.kind(), io::ErrorKind::InvalidInput);
}
}
}