Skip to content

Commit 45c4696

Browse files
authored
Add mount syscall (#494)
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
1 parent d8afee9 commit 45c4696

File tree

8 files changed

+369
-7
lines changed

8 files changed

+369
-7
lines changed

src/backend/libc/fs/syscalls.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,3 +1799,22 @@ struct Attrlist {
17991799
fileattr: Attrgroup,
18001800
forkattr: Attrgroup,
18011801
}
1802+
1803+
#[cfg(any(target_os = "android", target_os = "linux"))]
1804+
pub(crate) fn mount(
1805+
source: Option<&CStr>,
1806+
target: &CStr,
1807+
file_system_type: Option<&CStr>,
1808+
flags: super::types::MountFlagsArg,
1809+
data: Option<&CStr>,
1810+
) -> io::Result<()> {
1811+
unsafe {
1812+
ret(c::mount(
1813+
source.map_or_else(null, CStr::as_ptr),
1814+
target.as_ptr(),
1815+
file_system_type.map_or_else(null, CStr::as_ptr),
1816+
flags.0,
1817+
data.map_or_else(null, CStr::as_ptr).cast(),
1818+
))
1819+
}
1820+
}

src/backend/libc/fs/types.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,3 +1114,83 @@ pub const NFS_SUPER_MAGIC: FsWord = 0x0000_6969;
11141114
#[repr(transparent)]
11151115
#[derive(Copy, Clone)]
11161116
pub struct copyfile_state_t(pub(crate) *mut c::c_void);
1117+
1118+
#[cfg(any(target_os = "android", target_os = "linux"))]
1119+
bitflags! {
1120+
/// `MS_*` constants for use with [`mount`][crate::fs::mount].
1121+
pub struct MountFlags: c::c_ulong {
1122+
/// `MS_BIND`
1123+
const BIND = c::MS_BIND;
1124+
1125+
/// `MS_DIRSYNC`
1126+
const DIRSYNC = c::MS_DIRSYNC;
1127+
1128+
/// `MS_LAZYTIME`
1129+
const LAZYTIME = c::MS_LAZYTIME;
1130+
1131+
/// `MS_MANDLOCK`
1132+
#[doc(alias = "MANDLOCK")]
1133+
const PERMIT_MANDATORY_FILE_LOCKING = c::MS_MANDLOCK;
1134+
1135+
/// `MS_NOATIME`
1136+
const NOATIME = c::MS_NOATIME;
1137+
1138+
/// `MS_NODEV`
1139+
const NODEV = c::MS_NODEV;
1140+
1141+
/// `MS_NODIRATIME`
1142+
const NODIRATIME = c::MS_NODIRATIME;
1143+
1144+
/// `MS_NOEXEC`
1145+
const NOEXEC = c::MS_NOEXEC;
1146+
1147+
/// `MS_NOSUID`
1148+
const NOSUID = c::MS_NOSUID;
1149+
1150+
/// `MS_RDONLY`
1151+
const RDONLY = c::MS_RDONLY;
1152+
1153+
/// `MS_REC`
1154+
const REC = c::MS_REC;
1155+
1156+
/// `MS_RELATIME`
1157+
const RELATIME = c::MS_RELATIME;
1158+
1159+
/// `MS_SILENT`
1160+
const SILENT = c::MS_SILENT;
1161+
1162+
/// `MS_STRICTATIME`
1163+
const STRICTATIME = c::MS_STRICTATIME;
1164+
1165+
/// `MS_SYNCHRONOUS`
1166+
const SYNCHRONOUS = c::MS_SYNCHRONOUS;
1167+
}
1168+
}
1169+
1170+
#[cfg(any(target_os = "android", target_os = "linux"))]
1171+
bitflags! {
1172+
/// `MS_*` constants for use with [`change_mount`][crate::fs::mount::change_mount].
1173+
pub struct MountPropagationFlags: c::c_ulong {
1174+
/// `MS_SHARED`
1175+
const SHARED = c::MS_SHARED;
1176+
/// `MS_PRIVATE`
1177+
const PRIVATE = c::MS_PRIVATE;
1178+
/// `MS_SLAVE`
1179+
const SLAVE = c::MS_SLAVE;
1180+
/// `MS_UNBINDABLE`
1181+
const UNBINDABLE = c::MS_UNBINDABLE;
1182+
/// `MS_REC`
1183+
const REC = c::MS_REC;
1184+
}
1185+
}
1186+
1187+
#[cfg(any(target_os = "android", target_os = "linux"))]
1188+
bitflags! {
1189+
pub(crate) struct InternalMountFlags: c::c_ulong {
1190+
const REMOUNT = c::MS_REMOUNT;
1191+
const MOVE = c::MS_MOVE;
1192+
}
1193+
}
1194+
1195+
#[cfg(any(target_os = "android", target_os = "linux"))]
1196+
pub(crate) struct MountFlagsArg(pub(crate) c::c_ulong);

src/backend/linux_raw/conv.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,15 @@ impl<'a, Num: ArgNumber, T> From<&'a mut MaybeUninit<T>> for ArgReg<'a, Num> {
665665
}
666666
}
667667

668+
#[cfg(feature = "fs")]
669+
#[cfg(any(target_os = "android", target_os = "linux"))]
670+
impl<'a, Num: ArgNumber> From<crate::backend::fs::types::MountFlagsArg> for ArgReg<'a, Num> {
671+
#[inline]
672+
fn from(flags: crate::backend::fs::types::MountFlagsArg) -> Self {
673+
c_uint(flags.0)
674+
}
675+
}
676+
668677
/// Convert a `usize` returned from a syscall that effectively returns `()` on
669678
/// success.
670679
///

src/backend/linux_raw/fs/syscalls.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,3 +1395,24 @@ pub(crate) fn sendfile(
13951395
))
13961396
}
13971397
}
1398+
1399+
#[inline]
1400+
#[cfg(any(target_os = "android", target_os = "linux"))]
1401+
pub(crate) fn mount(
1402+
source: Option<&CStr>,
1403+
target: &CStr,
1404+
file_system_type: Option<&CStr>,
1405+
flags: super::types::MountFlagsArg,
1406+
data: Option<&CStr>,
1407+
) -> io::Result<()> {
1408+
unsafe {
1409+
ret(syscall_readonly!(
1410+
__NR_mount,
1411+
source,
1412+
target,
1413+
file_system_type,
1414+
flags,
1415+
data
1416+
))
1417+
}
1418+
}

src/backend/linux_raw/fs/types.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,3 +644,86 @@ pub const PROC_SUPER_MAGIC: FsWord = linux_raw_sys::general::PROC_SUPER_MAGIC as
644644

645645
/// `NFS_SUPER_MAGIC`—The magic number for the NFS filesystem.
646646
pub const NFS_SUPER_MAGIC: FsWord = linux_raw_sys::general::NFS_SUPER_MAGIC as FsWord;
647+
648+
#[cfg(any(target_os = "android", target_os = "linux"))]
649+
bitflags! {
650+
/// `MS_*` constants for use with [`mount`][crate::fs::mount].
651+
pub struct MountFlags: c::c_uint {
652+
/// `MS_BIND`
653+
const BIND = linux_raw_sys::general::MS_BIND;
654+
655+
/// `MS_DIRSYNC`
656+
const DIRSYNC = linux_raw_sys::general::MS_DIRSYNC;
657+
658+
/// `MS_LAZYTIME`
659+
const LAZYTIME = linux_raw_sys::general::MS_LAZYTIME;
660+
661+
/// `MS_MANDLOCK`
662+
#[doc(alias = "MANDLOCK")]
663+
const PERMIT_MANDATORY_FILE_LOCKING = linux_raw_sys::general::MS_MANDLOCK;
664+
665+
/// `MS_NOATIME`
666+
const NOATIME = linux_raw_sys::general::MS_NOATIME;
667+
668+
/// `MS_NODEV`
669+
const NODEV = linux_raw_sys::general::MS_NODEV;
670+
671+
/// `MS_NODIRATIME`
672+
const NODIRATIME = linux_raw_sys::general::MS_NODIRATIME;
673+
674+
/// `MS_NOEXEC`
675+
const NOEXEC = linux_raw_sys::general::MS_NOEXEC;
676+
677+
/// `MS_NOSUID`
678+
const NOSUID = linux_raw_sys::general::MS_NOSUID;
679+
680+
/// `MS_RDONLY`
681+
const RDONLY = linux_raw_sys::general::MS_RDONLY;
682+
683+
/// `MS_REC`
684+
const REC = linux_raw_sys::general::MS_REC;
685+
686+
/// `MS_RELATIME`
687+
const RELATIME = linux_raw_sys::general::MS_RELATIME;
688+
689+
/// `MS_SILENT`
690+
const SILENT = linux_raw_sys::general::MS_SILENT;
691+
692+
/// `MS_STRICTATIME`
693+
const STRICTATIME = linux_raw_sys::general::MS_STRICTATIME;
694+
695+
/// `MS_SYNCHRONOUS`
696+
const SYNCHRONOUS = linux_raw_sys::general::MS_SYNCHRONOUS;
697+
698+
/// `MS_NOSYMFOLLOW`
699+
const NOSYMFOLLOW = linux_raw_sys::general::MS_NOSYMFOLLOW;
700+
}
701+
}
702+
703+
#[cfg(any(target_os = "android", target_os = "linux"))]
704+
bitflags! {
705+
/// `MS_*` constants for use with [`change_mount`][crate::fs::mount::change_mount].
706+
pub struct MountPropagationFlags: c::c_uint {
707+
/// `MS_SHARED`
708+
const SHARED = linux_raw_sys::general::MS_SHARED;
709+
/// `MS_PRIVATE`
710+
const PRIVATE = linux_raw_sys::general::MS_PRIVATE;
711+
/// `MS_SLAVE`
712+
const SLAVE = linux_raw_sys::general::MS_SLAVE;
713+
/// `MS_UNBINDABLE`
714+
const UNBINDABLE = linux_raw_sys::general::MS_UNBINDABLE;
715+
/// `MS_REC`
716+
const REC = linux_raw_sys::general::MS_REC;
717+
}
718+
}
719+
720+
#[cfg(any(target_os = "android", target_os = "linux"))]
721+
bitflags! {
722+
pub(crate) struct InternalMountFlags: c::c_uint {
723+
const REMOUNT = linux_raw_sys::general::MS_REMOUNT;
724+
const MOVE = linux_raw_sys::general::MS_MOVE;
725+
}
726+
}
727+
728+
#[cfg(any(target_os = "android", target_os = "linux"))]
729+
pub(crate) struct MountFlagsArg(pub(crate) c::c_uint);

src/fs/constants.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub use backend::fs::types::AtFlags;
1212
pub use backend::fs::types::{CloneFlags, CopyfileFlags};
1313

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

1717
#[cfg(not(target_os = "redox"))]
1818
pub use backend::fs::types::Dev;

src/fs/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ mod makedev;
4848
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
4949
mod memfd_create;
5050
#[cfg(any(target_os = "android", target_os = "linux"))]
51+
mod mount;
52+
#[cfg(any(target_os = "android", target_os = "linux"))]
5153
mod openat2;
52-
#[cfg(feature = "fs")]
5354
#[cfg(any(target_os = "android", target_os = "linux"))]
5455
mod raw_dir;
5556
#[cfg(target_os = "linux")]
@@ -99,14 +100,12 @@ pub use constants::CloneFlags;
99100
/// `copyfile_flags_t`
100101
#[cfg(any(target_os = "ios", target_os = "macos"))]
101102
pub use constants::CopyfileFlags;
102-
#[cfg(any(target_os = "android", target_os = "linux"))]
103-
pub use constants::RenameFlags;
104-
#[cfg(any(target_os = "android", target_os = "linux"))]
105-
pub use constants::ResolveFlags;
106103
pub use constants::{Access, FdFlags, Mode, Nsecs, OFlags, Secs, Timespec};
107104
#[cfg(not(target_os = "redox"))]
108105
pub use constants::{AtFlags, Dev};
109106
#[cfg(any(target_os = "android", target_os = "linux"))]
107+
pub use constants::{MountFlags, MountPropagationFlags, RenameFlags, ResolveFlags};
108+
#[cfg(any(target_os = "android", target_os = "linux"))]
110109
pub use copy_file_range::copy_file_range;
111110
#[cfg(not(target_os = "redox"))]
112111
pub use cwd::cwd;
@@ -203,8 +202,9 @@ pub use makedev::{major, makedev, minor};
203202
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
204203
pub use memfd_create::{memfd_create, MemfdFlags};
205204
#[cfg(any(target_os = "android", target_os = "linux"))]
205+
pub use mount::{bind_mount, change_mount, mount, move_mount, recursive_bind_mount, remount};
206+
#[cfg(any(target_os = "android", target_os = "linux"))]
206207
pub use openat2::openat2;
207-
#[cfg(feature = "fs")]
208208
#[cfg(any(target_os = "android", target_os = "linux"))]
209209
pub use raw_dir::{RawDir, RawDirEntry};
210210
#[cfg(target_os = "linux")]

0 commit comments

Comments
 (0)