Skip to content

Commit e07d499

Browse files
authored
Add unmount syscall (#512)
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com> Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
1 parent 478363a commit e07d499

File tree

9 files changed

+76
-6
lines changed

9 files changed

+76
-6
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ once_cell = { version = "1.5.2", optional = true }
3939
# libc backend can be selected via adding `--cfg=rustix_use_libc` to
4040
# `RUSTFLAGS` or enabling the `use-libc` cargo feature.
4141
[target.'cfg(all(not(rustix_use_libc), not(miri), target_os = "linux", any(target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"), all(target_endian = "little", any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "powerpc64", target_arch = "riscv64", target_arch = "mips", target_arch = "mips64")))))'.dependencies]
42-
linux-raw-sys = { version = "0.2.0", default-features = false, features = ["general", "errno", "ioctl", "no_std"] }
42+
linux-raw-sys = { version = "0.2.1", default-features = false, features = ["general", "errno", "ioctl", "no_std"] }
4343
libc_errno = { package = "errno", version = "0.2.8", default-features = false, optional = true }
4444
libc = { version = "0.2.133", features = ["extra_traits"], optional = true }
4545

@@ -56,7 +56,7 @@ libc = { version = "0.2.133", features = ["extra_traits"] }
5656
# Some syscalls do not have libc wrappers, such as in `io_uring`. For these,
5757
# the libc backend uses the linux-raw-sys ABI and `libc::syscall`.
5858
[target.'cfg(all(any(target_os = "android", target_os = "linux"), any(rustix_use_libc, miri, not(all(target_os = "linux", any(target_arch = "x86", all(target_arch = "x86_64", target_pointer_width = "64"), all(target_endian = "little", any(target_arch = "arm", all(target_arch = "aarch64", target_pointer_width = "64"), target_arch = "powerpc64", target_arch = "riscv64", target_arch = "mips", target_arch = "mips64"))))))))'.dependencies]
59-
linux-raw-sys = { version = "0.2.0", default-features = false, features = ["general", "no_std"] }
59+
linux-raw-sys = { version = "0.2.1", default-features = false, features = ["general", "no_std"] }
6060

6161
# For the libc backend on Windows, use the Winsock2 API in windows-sys.
6262
[target.'cfg(windows)'.dependencies.windows-sys]

src/backend/libc/fs/syscalls.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,3 +1818,8 @@ pub(crate) fn mount(
18181818
))
18191819
}
18201820
}
1821+
1822+
#[cfg(any(target_os = "android", target_os = "linux"))]
1823+
pub(crate) fn unmount(target: &CStr, flags: super::types::UnmountFlags) -> io::Result<()> {
1824+
unsafe { ret(c::umount2(target.as_ptr(), flags.bits())) }
1825+
}

src/backend/libc/fs/types.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,3 +1194,18 @@ bitflags! {
11941194

11951195
#[cfg(any(target_os = "android", target_os = "linux"))]
11961196
pub(crate) struct MountFlagsArg(pub(crate) c::c_ulong);
1197+
1198+
#[cfg(any(target_os = "android", target_os = "linux"))]
1199+
bitflags! {
1200+
/// `MNT_*` constants for use with [`unmount`][crate::fs::mount::unmount].
1201+
pub struct UnmountFlags: c::c_int {
1202+
/// `MNT_FORCE`
1203+
const FORCE = c::MNT_FORCE;
1204+
/// `MNT_DETACH`
1205+
const DETACH = c::MNT_DETACH;
1206+
/// `MNT_EXPIRE`
1207+
const EXPIRE = c::MNT_EXPIRE;
1208+
/// `UMOUNT_NOFOLLOW`
1209+
const NOFOLLOW = c::UMOUNT_NOFOLLOW;
1210+
}
1211+
}

src/backend/linux_raw/conv.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,15 @@ impl<'a, Num: ArgNumber> From<crate::backend::fs::types::MountFlagsArg> for ArgR
674674
}
675675
}
676676

677+
#[cfg(feature = "fs")]
678+
#[cfg(any(target_os = "android", target_os = "linux"))]
679+
impl<'a, Num: ArgNumber> From<crate::backend::fs::types::UnmountFlags> for ArgReg<'a, Num> {
680+
#[inline]
681+
fn from(flags: crate::backend::fs::types::UnmountFlags) -> Self {
682+
c_uint(flags.bits())
683+
}
684+
}
685+
677686
/// Convert a `usize` returned from a syscall that effectively returns `()` on
678687
/// success.
679688
///

src/backend/linux_raw/fs/syscalls.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,3 +1416,9 @@ pub(crate) fn mount(
14161416
))
14171417
}
14181418
}
1419+
1420+
#[inline]
1421+
#[cfg(any(target_os = "android", target_os = "linux"))]
1422+
pub(crate) fn unmount(target: &CStr, flags: super::types::UnmountFlags) -> io::Result<()> {
1423+
unsafe { ret(syscall_readonly!(__NR_umount2, target, flags)) }
1424+
}

src/backend/linux_raw/fs/types.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,3 +727,18 @@ bitflags! {
727727

728728
#[cfg(any(target_os = "android", target_os = "linux"))]
729729
pub(crate) struct MountFlagsArg(pub(crate) c::c_uint);
730+
731+
#[cfg(any(target_os = "android", target_os = "linux"))]
732+
bitflags! {
733+
/// `MNT_*` constants for use with [`unmount`][crate::fs::mount::unmount].
734+
pub struct UnmountFlags: c::c_uint {
735+
/// `MNT_FORCE`
736+
const FORCE = linux_raw_sys::general::MNT_FORCE;
737+
/// `MNT_DETACH`
738+
const DETACH = linux_raw_sys::general::MNT_DETACH;
739+
/// `MNT_EXPIRE`
740+
const EXPIRE = linux_raw_sys::general::MNT_EXPIRE;
741+
/// `UMOUNT_NOFOLLOW`
742+
const NOFOLLOW = linux_raw_sys::general::UMOUNT_NOFOLLOW;
743+
}
744+
}

src/fs/constants.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ 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::{MountFlags, MountPropagationFlags, RenameFlags, ResolveFlags};
15+
pub use backend::fs::types::{
16+
MountFlags, MountPropagationFlags, RenameFlags, ResolveFlags, UnmountFlags,
17+
};
1618

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

src/fs/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub use constants::{Access, FdFlags, Mode, Nsecs, OFlags, Secs, Timespec};
104104
#[cfg(not(target_os = "redox"))]
105105
pub use constants::{AtFlags, Dev};
106106
#[cfg(any(target_os = "android", target_os = "linux"))]
107-
pub use constants::{MountFlags, MountPropagationFlags, RenameFlags, ResolveFlags};
107+
pub use constants::{MountFlags, MountPropagationFlags, RenameFlags, ResolveFlags, UnmountFlags};
108108
#[cfg(any(target_os = "android", target_os = "linux"))]
109109
pub use copy_file_range::copy_file_range;
110110
#[cfg(not(target_os = "redox"))]
@@ -202,7 +202,9 @@ pub use makedev::{major, makedev, minor};
202202
#[cfg(any(target_os = "android", target_os = "freebsd", target_os = "linux"))]
203203
pub use memfd_create::{memfd_create, MemfdFlags};
204204
#[cfg(any(target_os = "android", target_os = "linux"))]
205-
pub use mount::{bind_mount, change_mount, mount, move_mount, recursive_bind_mount, remount};
205+
pub use mount::{
206+
bind_mount, change_mount, mount, move_mount, recursive_bind_mount, remount, unmount,
207+
};
206208
#[cfg(any(target_os = "android", target_os = "linux"))]
207209
pub use openat2::openat2;
208210
#[cfg(any(target_os = "android", target_os = "linux"))]

src/fs/mount.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Linux `mount`.
22
33
use crate::backend::fs::types::{
4-
InternalMountFlags, MountFlags, MountFlagsArg, MountPropagationFlags,
4+
InternalMountFlags, MountFlags, MountFlagsArg, MountPropagationFlags, UnmountFlags,
55
};
66
use crate::{backend, io, path};
77

@@ -43,6 +43,7 @@ pub fn mount<Source: path::Arg, Target: path::Arg, Fs: path::Arg, Data: path::Ar
4343
///
4444
/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
4545
#[inline]
46+
#[doc(alias = "mount")]
4647
pub fn remount<Target: path::Arg, Data: path::Arg>(
4748
target: Target,
4849
flags: MountFlags,
@@ -68,6 +69,7 @@ pub fn remount<Target: path::Arg, Data: path::Arg>(
6869
///
6970
/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
7071
#[inline]
72+
#[doc(alias = "mount")]
7173
pub fn bind_mount<Source: path::Arg, Target: path::Arg>(
7274
source: Source,
7375
target: Target,
@@ -92,6 +94,7 @@ pub fn bind_mount<Source: path::Arg, Target: path::Arg>(
9294
///
9395
/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
9496
#[inline]
97+
#[doc(alias = "mount")]
9598
pub fn recursive_bind_mount<Source: path::Arg, Target: path::Arg>(
9699
source: Source,
97100
target: Target,
@@ -116,6 +119,7 @@ pub fn recursive_bind_mount<Source: path::Arg, Target: path::Arg>(
116119
///
117120
/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
118121
#[inline]
122+
#[doc(alias = "mount")]
119123
pub fn change_mount<Target: path::Arg>(
120124
target: Target,
121125
flags: MountPropagationFlags,
@@ -132,6 +136,7 @@ pub fn change_mount<Target: path::Arg>(
132136
///
133137
/// [Linux]: https://man7.org/linux/man-pages/man2/mount.2.html
134138
#[inline]
139+
#[doc(alias = "mount")]
135140
pub fn move_mount<Source: path::Arg, Target: path::Arg>(
136141
source: Source,
137142
target: Target,
@@ -148,3 +153,14 @@ pub fn move_mount<Source: path::Arg, Target: path::Arg>(
148153
})
149154
})
150155
}
156+
157+
/// `umount2(target, flags)`
158+
///
159+
/// # References
160+
/// - [Linux]
161+
///
162+
/// [Linux]: https://man7.org/linux/man-pages/man2/umount.2.html
163+
#[doc(alias = "umount", alias = "umount2")]
164+
pub fn unmount<Target: path::Arg>(target: Target, flags: UnmountFlags) -> io::Result<()> {
165+
target.into_with_c_str(|target| backend::fs::syscalls::unmount(target, flags))
166+
}

0 commit comments

Comments
 (0)