use crate::capability::CapabilitySet;
use crate::error::Result;
#[cfg(target_os = "linux")]
mod linux;
#[cfg(target_os = "macos")]
mod macos;
#[cfg(target_os = "macos")]
pub use macos::{extension_consume, extension_issue_file, extension_release};
#[cfg(target_os = "linux")]
pub use linux::{detect_abi, DetectedAbi};
#[cfg(target_os = "linux")]
pub use linux::is_wsl2;
#[cfg(target_os = "linux")]
pub use linux::{
classify_access_from_flags, classify_af_unix, continue_notif, deny_notif, inject_fd,
install_seccomp_notify, install_seccomp_proxy_filter, notif_id_valid,
probe_seccomp_block_network_support, read_notif_path, read_notif_sockaddr, read_open_how,
recv_notif, resolve_notif_path, respond_notif_errno, validate_openat2_size, OpenHow,
SeccompData, SeccompNetFallback, SeccompNotif, SockaddrInfo, UnixSocketKind, SYS_BIND,
SYS_CONNECT, SYS_OPENAT, SYS_OPENAT2,
};
#[derive(Debug, Clone)]
pub struct SupportInfo {
pub is_supported: bool,
pub platform: &'static str,
pub details: String,
}
pub struct Sandbox;
impl Sandbox {
#[cfg(target_os = "linux")]
#[must_use = "ABI detection result should be checked"]
pub fn detect_abi() -> Result<DetectedAbi> {
linux::detect_abi()
}
#[cfg(target_os = "linux")]
#[must_use = "sandbox application result should be checked"]
pub fn apply(caps: &CapabilitySet) -> Result<linux::SeccompNetFallback> {
linux::apply(caps)
}
#[cfg(target_os = "macos")]
#[must_use = "sandbox application result should be checked"]
pub fn apply(caps: &CapabilitySet) -> Result<()> {
macos::apply(caps)
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
#[must_use = "sandbox application result should be checked"]
pub fn apply(caps: &CapabilitySet) -> Result<()> {
let _ = caps;
#[cfg(target_arch = "wasm32")]
{
Err(crate::error::NonoError::UnsupportedPlatform(
"WASM: Browser sandboxing requires different approach (CSP, iframe sandbox)".into(),
))
}
#[cfg(not(target_arch = "wasm32"))]
{
Err(crate::error::NonoError::UnsupportedPlatform(
std::env::consts::OS.to_string(),
))
}
}
#[cfg(target_os = "linux")]
#[must_use = "sandbox application result should be checked"]
pub fn apply_with_abi(
caps: &CapabilitySet,
abi: &DetectedAbi,
) -> Result<linux::SeccompNetFallback> {
linux::apply_with_abi(caps, abi)
}
#[must_use]
pub fn is_supported() -> bool {
#[cfg(target_os = "linux")]
{
linux::is_supported()
}
#[cfg(target_os = "macos")]
{
macos::is_supported()
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
false
}
}
#[must_use]
pub fn support_info() -> SupportInfo {
#[cfg(target_os = "linux")]
{
linux::support_info()
}
#[cfg(target_os = "macos")]
{
macos::support_info()
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
SupportInfo {
is_supported: false,
platform: std::env::consts::OS,
details: format!("Platform '{}' is not supported", std::env::consts::OS),
}
}
}
}