use alloc::{boxed::Box, sync::Arc};
use ax_errno::{AxResult, ax_err_type};
use super::BindSlot;
pub trait UnixNamespace: Send + Sync {
fn resolve(&self, path: &str) -> AxResult<Arc<BindSlot>>;
fn bind(&self, path: &str) -> AxResult<Arc<BindSlot>>;
fn unbind(&self, path: &str) -> AxResult<()>;
}
static UNIX_NS: spin::Once<Box<dyn UnixNamespace>> = spin::Once::new();
pub fn register_unix_namespace(ns: impl UnixNamespace + 'static) {
UNIX_NS.call_once(|| Box::new(ns));
}
pub(crate) fn with_namespace<R>(f: impl FnOnce(&dyn UnixNamespace) -> AxResult<R>) -> AxResult<R> {
match UNIX_NS.get() {
Some(ns) => f(&**ns),
None => Err(ax_err_type!(
Unsupported,
"Unix socket path operations require filesystem support (enable 'fs-ng' feature)"
)),
}
}