Skip to content

Commit

Permalink
Add mount syscall (#494)
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>

Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
  • Loading branch information
SUPERCILEX committed Jan 12, 2023
1 parent d8afee9 commit 45c4696
Show file tree
Hide file tree
Showing 8 changed files with 369 additions and 7 deletions.
19 changes: 19 additions & 0 deletions src/backend/libc/fs/syscalls.rs
Expand Up @@ -1799,3 +1799,22 @@ struct Attrlist {
fileattr: Attrgroup,
forkattr: Attrgroup,
}

#[cfg(any(target_os = "android", target_os = "linux"))]
pub(crate) fn mount(
source: Option<&CStr>,
target: &CStr,
file_system_type: Option<&CStr>,
flags: super::types::MountFlagsArg,
data: Option<&CStr>,
) -> io::Result<()> {
unsafe {
ret(c::mount(
source.map_or_else(null, CStr::as_ptr),
target.as_ptr(),
file_system_type.map_or_else(null, CStr::as_ptr),
flags.0,
data.map_or_else(null, CStr::as_ptr).cast(),
))
}
}
80 changes: 80 additions & 0 deletions src/backend/libc/fs/types.rs
Expand Up @@ -1114,3 +1114,83 @@ pub const NFS_SUPER_MAGIC: FsWord = 0x0000_6969;
#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct copyfile_state_t(pub(crate) *mut c::c_void);

#[cfg(any(target_os = "android", target_os = "linux"))]
bitflags! {
/// `MS_*` constants for use with [`mount`][crate::fs::mount].
pub struct MountFlags: c::c_ulong {
/// `MS_BIND`
const BIND = c::MS_BIND;

/// `MS_DIRSYNC`
const DIRSYNC = c::MS_DIRSYNC;

/// `MS_LAZYTIME`
const LAZYTIME = c::MS_LAZYTIME;

/// `MS_MANDLOCK`
#[doc(alias = "MANDLOCK")]
const PERMIT_MANDATORY_FILE_LOCKING = c::MS_MANDLOCK;

/// `MS_NOATIME`
const NOATIME = c::MS_NOATIME;

/// `MS_NODEV`
const NODEV = c::MS_NODEV;

/// `MS_NODIRATIME`
const NODIRATIME = c::MS_NODIRATIME;

/// `MS_NOEXEC`
const NOEXEC = c::MS_NOEXEC;

/// `MS_NOSUID`
const NOSUID = c::MS_NOSUID;

/// `MS_RDONLY`
const RDONLY = c::MS_RDONLY;

/// `MS_REC`
const REC = c::MS_REC;

/// `MS_RELATIME`
const RELATIME = c::MS_RELATIME;

/// `MS_SILENT`
const SILENT = c::MS_SILENT;

/// `MS_STRICTATIME`
const STRICTATIME = c::MS_STRICTATIME;

/// `MS_SYNCHRONOUS`
const SYNCHRONOUS = c::MS_SYNCHRONOUS;
}
}

#[cfg(any(target_os = "android", target_os = "linux"))]
bitflags! {
/// `MS_*` constants for use with [`change_mount`][crate::fs::mount::change_mount].
pub struct MountPropagationFlags: c::c_ulong {
/// `MS_SHARED`
const SHARED = c::MS_SHARED;
/// `MS_PRIVATE`
const PRIVATE = c::MS_PRIVATE;
/// `MS_SLAVE`
const SLAVE = c::MS_SLAVE;
/// `MS_UNBINDABLE`
const UNBINDABLE = c::MS_UNBINDABLE;
/// `MS_REC`
const REC = c::MS_REC;
}
}

#[cfg(any(target_os = "android", target_os = "linux"))]
bitflags! {
pub(crate) struct InternalMountFlags: c::c_ulong {
const REMOUNT = c::MS_REMOUNT;
const MOVE = c::MS_MOVE;
}
}

#[cfg(any(target_os = "android", target_os = "linux"))]
pub(crate) struct MountFlagsArg(pub(crate) c::c_ulong);
9 changes: 9 additions & 0 deletions src/backend/linux_raw/conv.rs
Expand Up @@ -665,6 +665,15 @@ impl<'a, Num: ArgNumber, T> From<&'a mut MaybeUninit<T>> for ArgReg<'a, Num> {
}
}

#[cfg(feature = "fs")]
#[cfg(any(target_os = "android", target_os = "linux"))]
impl<'a, Num: ArgNumber> From<crate::backend::fs::types::MountFlagsArg> for ArgReg<'a, Num> {
#[inline]
fn from(flags: crate::backend::fs::types::MountFlagsArg) -> Self {
c_uint(flags.0)
}
}

/// Convert a `usize` returned from a syscall that effectively returns `()` on
/// success.
///
Expand Down
21 changes: 21 additions & 0 deletions src/backend/linux_raw/fs/syscalls.rs
Expand Up @@ -1395,3 +1395,24 @@ pub(crate) fn sendfile(
))
}
}

#[inline]
#[cfg(any(target_os = "android", target_os = "linux"))]
pub(crate) fn mount(
source: Option<&CStr>,
target: &CStr,
file_system_type: Option<&CStr>,
flags: super::types::MountFlagsArg,
data: Option<&CStr>,
) -> io::Result<()> {
unsafe {
ret(syscall_readonly!(
__NR_mount,
source,
target,
file_system_type,
flags,
data
))
}
}
83 changes: 83 additions & 0 deletions src/backend/linux_raw/fs/types.rs
Expand Up @@ -644,3 +644,86 @@ pub const PROC_SUPER_MAGIC: FsWord = linux_raw_sys::general::PROC_SUPER_MAGIC as

/// `NFS_SUPER_MAGIC`—The magic number for the NFS filesystem.
pub const NFS_SUPER_MAGIC: FsWord = linux_raw_sys::general::NFS_SUPER_MAGIC as FsWord;

#[cfg(any(target_os = "android", target_os = "linux"))]
bitflags! {
/// `MS_*` constants for use with [`mount`][crate::fs::mount].
pub struct MountFlags: c::c_uint {
/// `MS_BIND`
const BIND = linux_raw_sys::general::MS_BIND;

/// `MS_DIRSYNC`
const DIRSYNC = linux_raw_sys::general::MS_DIRSYNC;

/// `MS_LAZYTIME`
const LAZYTIME = linux_raw_sys::general::MS_LAZYTIME;

/// `MS_MANDLOCK`
#[doc(alias = "MANDLOCK")]
const PERMIT_MANDATORY_FILE_LOCKING = linux_raw_sys::general::MS_MANDLOCK;

/// `MS_NOATIME`
const NOATIME = linux_raw_sys::general::MS_NOATIME;

/// `MS_NODEV`
const NODEV = linux_raw_sys::general::MS_NODEV;

/// `MS_NODIRATIME`
const NODIRATIME = linux_raw_sys::general::MS_NODIRATIME;

/// `MS_NOEXEC`
const NOEXEC = linux_raw_sys::general::MS_NOEXEC;

/// `MS_NOSUID`
const NOSUID = linux_raw_sys::general::MS_NOSUID;

/// `MS_RDONLY`
const RDONLY = linux_raw_sys::general::MS_RDONLY;

/// `MS_REC`
const REC = linux_raw_sys::general::MS_REC;

/// `MS_RELATIME`
const RELATIME = linux_raw_sys::general::MS_RELATIME;

/// `MS_SILENT`
const SILENT = linux_raw_sys::general::MS_SILENT;

/// `MS_STRICTATIME`
const STRICTATIME = linux_raw_sys::general::MS_STRICTATIME;

/// `MS_SYNCHRONOUS`
const SYNCHRONOUS = linux_raw_sys::general::MS_SYNCHRONOUS;

/// `MS_NOSYMFOLLOW`
const NOSYMFOLLOW = linux_raw_sys::general::MS_NOSYMFOLLOW;
}
}

#[cfg(any(target_os = "android", target_os = "linux"))]
bitflags! {
/// `MS_*` constants for use with [`change_mount`][crate::fs::mount::change_mount].
pub struct MountPropagationFlags: c::c_uint {
/// `MS_SHARED`
const SHARED = linux_raw_sys::general::MS_SHARED;
/// `MS_PRIVATE`
const PRIVATE = linux_raw_sys::general::MS_PRIVATE;
/// `MS_SLAVE`
const SLAVE = linux_raw_sys::general::MS_SLAVE;
/// `MS_UNBINDABLE`
const UNBINDABLE = linux_raw_sys::general::MS_UNBINDABLE;
/// `MS_REC`
const REC = linux_raw_sys::general::MS_REC;
}
}

#[cfg(any(target_os = "android", target_os = "linux"))]
bitflags! {
pub(crate) struct InternalMountFlags: c::c_uint {
const REMOUNT = linux_raw_sys::general::MS_REMOUNT;
const MOVE = linux_raw_sys::general::MS_MOVE;
}
}

#[cfg(any(target_os = "android", target_os = "linux"))]
pub(crate) struct MountFlagsArg(pub(crate) c::c_uint);
2 changes: 1 addition & 1 deletion src/fs/constants.rs
Expand Up @@ -12,7 +12,7 @@ pub use backend::fs::types::AtFlags;
pub use backend::fs::types::{CloneFlags, CopyfileFlags};

#[cfg(any(target_os = "android", target_os = "linux"))]
pub use backend::fs::types::{RenameFlags, ResolveFlags};
pub use backend::fs::types::{MountFlags, MountPropagationFlags, RenameFlags, ResolveFlags};

#[cfg(not(target_os = "redox"))]
pub use backend::fs::types::Dev;
Expand Down
12 changes: 6 additions & 6 deletions src/fs/mod.rs
Expand Up @@ -48,8 +48,9 @@ mod makedev;
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
mod memfd_create;
#[cfg(any(target_os = "android", target_os = "linux"))]
mod mount;
#[cfg(any(target_os = "android", target_os = "linux"))]
mod openat2;
#[cfg(feature = "fs")]
#[cfg(any(target_os = "android", target_os = "linux"))]
mod raw_dir;
#[cfg(target_os = "linux")]
Expand Down Expand Up @@ -99,14 +100,12 @@ pub use constants::CloneFlags;
/// `copyfile_flags_t`
#[cfg(any(target_os = "ios", target_os = "macos"))]
pub use constants::CopyfileFlags;
#[cfg(any(target_os = "android", target_os = "linux"))]
pub use constants::RenameFlags;
#[cfg(any(target_os = "android", target_os = "linux"))]
pub use constants::ResolveFlags;
pub use constants::{Access, FdFlags, Mode, Nsecs, OFlags, Secs, Timespec};
#[cfg(not(target_os = "redox"))]
pub use constants::{AtFlags, Dev};
#[cfg(any(target_os = "android", target_os = "linux"))]
pub use constants::{MountFlags, MountPropagationFlags, RenameFlags, ResolveFlags};
#[cfg(any(target_os = "android", target_os = "linux"))]
pub use copy_file_range::copy_file_range;
#[cfg(not(target_os = "redox"))]
pub use cwd::cwd;
Expand Down Expand Up @@ -203,8 +202,9 @@ pub use makedev::{major, makedev, minor};
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
pub use memfd_create::{memfd_create, MemfdFlags};
#[cfg(any(target_os = "android", target_os = "linux"))]
pub use mount::{bind_mount, change_mount, mount, move_mount, recursive_bind_mount, remount};
#[cfg(any(target_os = "android", target_os = "linux"))]
pub use openat2::openat2;
#[cfg(feature = "fs")]
#[cfg(any(target_os = "android", target_os = "linux"))]
pub use raw_dir::{RawDir, RawDirEntry};
#[cfg(target_os = "linux")]
Expand Down

0 comments on commit 45c4696

Please sign in to comment.