use std::io;
use std::os::fd::{AsRawFd, BorrowedFd};
pub const MOUNT_ATTR_RDONLY: u64 = 0x0000_0001;
pub const MOUNT_ATTR_NOSUID: u64 = 0x0000_0002;
pub const MOUNT_ATTR_NODEV: u64 = 0x0000_0004;
pub const MOUNT_ATTR_NOEXEC: u64 = 0x0000_0008;
pub const MOUNT_ATTR_NOSYMFOLLOW: u64 = 0x0020_0000;
pub const STATX_MNT_ID_UNIQUE: u32 = 0x0000_4000;
pub const ST_NOSYMFOLLOW: u64 = 0x2000;
pub const PROC_SUPER_MAGIC: u64 = 0x9fa0;
#[repr(C)]
#[derive(Debug, Default)]
struct MountAttr {
attr_set: u64,
attr_clr: u64,
propagation: u64,
userns_fd: u64,
}
const AT_EMPTY_PATH: libc::c_uint = 0x1000;
pub fn mount_setattr_add(fd: BorrowedFd<'_>, set: u64) -> io::Result<()> {
let attr = MountAttr {
attr_set: set,
..MountAttr::default()
};
let ret = unsafe {
libc::syscall(
libc::SYS_mount_setattr,
fd.as_raw_fd(),
c"".as_ptr(),
AT_EMPTY_PATH,
std::ptr::from_ref(&attr),
std::mem::size_of::<MountAttr>(),
)
};
if ret == 0 {
Ok(())
} else {
Err(io::Error::last_os_error())
}
}
#[must_use]
pub fn probe_mount_setattr() -> io::Error {
let attr = MountAttr::default();
let ret = unsafe {
libc::syscall(
libc::SYS_mount_setattr,
-1,
c"".as_ptr(),
0x8000_0000u32,
std::ptr::from_ref(&attr),
std::mem::size_of::<MountAttr>(),
)
};
debug_assert!(ret != 0, "mount_setattr probe unexpectedly succeeded");
io::Error::last_os_error()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mount_attr_has_kernel_layout() {
assert_eq!(std::mem::size_of::<MountAttr>(), 32);
}
#[test]
fn probe_reports_non_enosys_on_supported_kernels() {
let err = probe_mount_setattr();
assert_ne!(err.raw_os_error(), Some(libc::ENOSYS));
}
}