[libc-commits] [libc] ddd2bce - [libc] implement sys/mount.h (#244047)

via libc-commits libc-commits at lists.llvm.org
Wed Nov 18 23:21:10 PST 2026


Author: Michael Jones
Date: 2026-11-18T23:21:04-08:00
New Revision: ddd2bce7da6c1514a61dcd2d03039193e91c5c3a

URL: https://github.com/llvm/llvm-project/commit/ddd2bce7da6c1514a61dcd2d03039193e91c5c3a
DIFF: https://github.com/llvm/llvm-project/commit/ddd2bce7da6c1514a61dcd2d03039193e91c5c3a.diff

LOG: [libc] implement sys/mount.h (#244047)

The mount header is for mounting and unmounting filesystems. This PR
adds mount, umount, and umount2, plus tests. Tests are pretty minimal
since we generally can't and don't want to mount or unmount random
files.

Assisted-by: Automated tooling, human reviewed.

Added: 
    libc/src/__support/OSUtil/linux/syscall_wrappers/mount.h
    libc/src/__support/OSUtil/linux/syscall_wrappers/umount2.h
    libc/src/sys/mount/CMakeLists.txt
    libc/src/sys/mount/linux/CMakeLists.txt
    libc/src/sys/mount/linux/mount.cpp
    libc/src/sys/mount/linux/umount.cpp
    libc/src/sys/mount/linux/umount2.cpp
    libc/src/sys/mount/mount.h
    libc/src/sys/mount/umount.h
    libc/src/sys/mount/umount2.h
    libc/test/src/sys/mount/CMakeLists.txt
    libc/test/src/sys/mount/mount_test.cpp
    libc/test/src/sys/mount/umount2_test.cpp
    libc/test/src/sys/mount/umount_test.cpp

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/arm/entrypoints.txt
    libc/config/linux/arm/headers.txt
    libc/config/linux/riscv/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/include/llvm-libc-macros/linux/sys-mount-macros.h
    libc/include/sys/mount.yaml
    libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
    libc/src/sys/CMakeLists.txt
    libc/test/src/sys/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index e7130cdfd46fa..a8a6bed94f376 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -333,6 +333,11 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.mman.shm_open
     libc.src.sys.mman.shm_unlink
 
+    # sys/mount.h entrypoints
+    libc.src.sys.mount.mount
+    libc.src.sys.mount.umount
+    libc.src.sys.mount.umount2
+
     # sys/random.h entrypoints
     libc.src.sys.random.getrandom
     libc.src.sys.random.getentropy

diff  --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index f632b8af60789..51a6d29052db4 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -234,6 +234,11 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.mman.mmap
     libc.src.sys.mman.munmap
 
+    # sys/mount.h entrypoints
+    libc.src.sys.mount.mount
+    libc.src.sys.mount.umount
+    libc.src.sys.mount.umount2
+
     # sys/prctl.h entrypoints
     libc.src.sys.prctl.prctl
 

diff  --git a/libc/config/linux/arm/headers.txt b/libc/config/linux/arm/headers.txt
index b9d4a9dc6724a..bf52235cc1fab 100644
--- a/libc/config/linux/arm/headers.txt
+++ b/libc/config/linux/arm/headers.txt
@@ -29,6 +29,7 @@ set(TARGET_PUBLIC_HEADERS
     libc.include.wctype
     libc.include.sys_eventfd
     libc.include.sys_inotify
+    libc.include.sys_mount
     libc.include.sys_param
     libc.include.sys_personality
     libc.include.sys_sysinfo

diff  --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index a95b888dc99cf..940df321e93a0 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -371,6 +371,11 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.mman.shm_open
     libc.src.sys.mman.shm_unlink
 
+    # sys/mount.h entrypoints
+    libc.src.sys.mount.mount
+    libc.src.sys.mount.umount
+    libc.src.sys.mount.umount2
+
     # sys/random.h entrypoints
     libc.src.sys.random.getrandom
     libc.src.sys.random.getentropy

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index a6cd732ce793a..b2552365e8a55 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -371,6 +371,11 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.mman.shm_open
     libc.src.sys.mman.shm_unlink
 
+    # sys/mount.h entrypoints
+    libc.src.sys.mount.mount
+    libc.src.sys.mount.umount
+    libc.src.sys.mount.umount2
+
     # sys/random.h entrypoints
     libc.src.sys.random.getrandom
     libc.src.sys.random.getentropy

diff  --git a/libc/include/llvm-libc-macros/linux/sys-mount-macros.h b/libc/include/llvm-libc-macros/linux/sys-mount-macros.h
index a18cd8f811e14..6d7c5ad77c160 100644
--- a/libc/include/llvm-libc-macros/linux/sys-mount-macros.h
+++ b/libc/include/llvm-libc-macros/linux/sys-mount-macros.h
@@ -14,27 +14,13 @@
 #ifndef LLVM_LIBC_MACROS_LINUX_SYS_MOUNT_MACROS_H
 #define LLVM_LIBC_MACROS_LINUX_SYS_MOUNT_MACROS_H
 
-#define MS_RDONLY (1 << 0)
-#define MS_NOSUID (1 << 1)
-#define MS_NODEV (1 << 2)
-#define MS_NOEXEC (1 << 3)
-#define MS_SYNCHRONOUS (1 << 4)
-#define MS_REMOUNT (1 << 5)
-#define MS_MANDLOCK (1 << 6)
-#define MS_DIRSYNC (1 << 7)
-#define MS_NOSYMFOLLOW (1 << 8)
-#define MS_NOATIME (1 << 10)
-#define MS_NODIRATIME (1 << 11)
-#define MS_BIND (1 << 12)
-#define MS_MOVE (1 << 13)
-#define MS_REC (1 << 14)
-#define MS_SILENT (1 << 15)
-#define MS_UNBINDABLE (1 << 17)
-#define MS_PRIVATE (1 << 18)
-#define MS_SLAVE (1 << 19)
-#define MS_SHARED (1 << 20)
-#define MS_RELATIME (1 << 21)
-#define MS_STRICTATIME (1 << 24)
-#define MS_LAZYTIME (1 << 25)
+// The MS_* mount flags are defined by the kernel UAPI header.
+#include <linux/mount.h>
+
+// Flags for umount2. The kernel does not export these in a UAPI header.
+#define MNT_FORCE 0x1
+#define MNT_DETACH 0x2
+#define MNT_EXPIRE 0x4
+#define UMOUNT_NOFOLLOW 0x8
 
 #endif // LLVM_LIBC_MACROS_LINUX_SYS_MOUNT_MACROS_H

diff  --git a/libc/include/sys/mount.yaml b/libc/include/sys/mount.yaml
index 9c2964ff43e35..b83ad195e861a 100644
--- a/libc/include/sys/mount.yaml
+++ b/libc/include/sys/mount.yaml
@@ -9,7 +9,38 @@ macros:
   # to system-specific ones.
   - macro_name: MS_RDONLY
     macro_header: sys-mount-macros.h
+  - macro_name: MNT_FORCE
+    macro_header: sys-mount-macros.h
+  - macro_name: MNT_DETACH
+    macro_header: sys-mount-macros.h
+  - macro_name: MNT_EXPIRE
+    macro_header: sys-mount-macros.h
+  - macro_name: UMOUNT_NOFOLLOW
+    macro_header: sys-mount-macros.h
 types: []
 enums: []
 objects: []
-functions: []
+functions:
+  - name: mount
+    standards:
+      - linux
+    return_type: int
+    arguments:
+      - type: const char *
+      - type: const char *
+      - type: const char *
+      - type: unsigned long
+      - type: const void *
+  - name: umount
+    standards:
+      - linux
+    return_type: int
+    arguments:
+      - type: const char *
+  - name: umount2
+    standards:
+      - linux
+    return_type: int
+    arguments:
+      - type: const char *
+      - type: int

diff  --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index d7a75d9172e06..c7e9217e8782e 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1712,6 +1712,30 @@ add_header_library(
     libc.include.sys_syscall
 )
 
+add_header_library(
+  mount
+  HDRS
+    mount.h
+  DEPENDS
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+    libc.include.sys_syscall
+)
+
+add_header_library(
+  umount2
+  HDRS
+    umount2.h
+  DEPENDS
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+    libc.include.sys_syscall
+)
+
 add_header_library(
   fcntl
   HDRS

diff  --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/mount.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/mount.h
new file mode 100644
index 0000000000000..7669a6d508ffa
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/mount.h
@@ -0,0 +1,36 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for mount.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MOUNT_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MOUNT_H
+
+#include "src/__support/OSUtil/linux/syscall.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h>
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> mount(const char *source, const char *target,
+                               const char *filesystemtype,
+                               unsigned long mountflags, const void *data) {
+  return syscall_checked<int>(SYS_mount, source, target, filesystemtype,
+                              mountflags, data);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MOUNT_H

diff  --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/umount2.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/umount2.h
new file mode 100644
index 0000000000000..09172fcff2e51
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/umount2.h
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for umount2.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_UMOUNT2_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_UMOUNT2_H
+
+#include "src/__support/OSUtil/linux/syscall.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h>
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> umount2(const char *target, int flags) {
+  return syscall_checked<int>(SYS_umount2, target, flags);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_UMOUNT2_H

diff  --git a/libc/src/sys/CMakeLists.txt b/libc/src/sys/CMakeLists.txt
index d43daa8411cfe..d95b380a4f9ea 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -6,6 +6,7 @@ add_subdirectory(file)
 add_subdirectory(inotify)
 add_subdirectory(ipc)
 add_subdirectory(mman)
+add_subdirectory(mount)
 add_subdirectory(personality)
 add_subdirectory(random)
 add_subdirectory(resource)

diff  --git a/libc/src/sys/mount/CMakeLists.txt b/libc/src/sys/mount/CMakeLists.txt
new file mode 100644
index 0000000000000..b382b4794cf30
--- /dev/null
+++ b/libc/src/sys/mount/CMakeLists.txt
@@ -0,0 +1,24 @@
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+endif()
+
+add_entrypoint_object(
+  mount
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.mount
+)
+
+add_entrypoint_object(
+  umount
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.umount
+)
+
+add_entrypoint_object(
+  umount2
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.umount2
+)

diff  --git a/libc/src/sys/mount/linux/CMakeLists.txt b/libc/src/sys/mount/linux/CMakeLists.txt
new file mode 100644
index 0000000000000..5373d7677bfc0
--- /dev/null
+++ b/libc/src/sys/mount/linux/CMakeLists.txt
@@ -0,0 +1,44 @@
+add_entrypoint_object(
+  mount
+  SRCS
+    mount.cpp
+  HDRS
+    ../mount.h
+  DEPENDS
+    libc.src.__support.OSUtil.linux.syscall_wrappers.mount
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  umount
+  SRCS
+    umount.cpp
+  HDRS
+    ../umount.h
+  DEPENDS
+    libc.src.__support.OSUtil.linux.syscall_wrappers.umount2
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)
+
+add_entrypoint_object(
+  umount2
+  SRCS
+    umount2.cpp
+  HDRS
+    ../umount2.h
+  DEPENDS
+    libc.src.__support.OSUtil.linux.syscall_wrappers.umount2
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)

diff  --git a/libc/src/sys/mount/linux/mount.cpp b/libc/src/sys/mount/linux/mount.cpp
new file mode 100644
index 0000000000000..1f10b1032d2c0
--- /dev/null
+++ b/libc/src/sys/mount/linux/mount.cpp
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of mount.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/mount/mount.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/mount.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, mount,
+                   (const char *source, const char *target,
+                    const char *filesystemtype, unsigned long mountflags,
+                    const void *data)) {
+  ErrorOr<int> result =
+      linux_syscalls::mount(source, target, filesystemtype, mountflags, data);
+  if (!result.has_value()) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return result.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/sys/mount/linux/umount.cpp b/libc/src/sys/mount/linux/umount.cpp
new file mode 100644
index 0000000000000..ea2847d4515cc
--- /dev/null
+++ b/libc/src/sys/mount/linux/umount.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of umount.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/mount/umount.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/umount2.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, umount, (const char *target)) {
+  ErrorOr<int> result = linux_syscalls::umount2(target, 0);
+  if (!result.has_value()) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return result.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/sys/mount/linux/umount2.cpp b/libc/src/sys/mount/linux/umount2.cpp
new file mode 100644
index 0000000000000..35031b7957d65
--- /dev/null
+++ b/libc/src/sys/mount/linux/umount2.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of umount2.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/mount/umount2.h"
+
+#include "src/__support/OSUtil/linux/syscall_wrappers/umount2.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, umount2, (const char *target, int flags)) {
+  ErrorOr<int> result = linux_syscalls::umount2(target, flags);
+  if (!result.has_value()) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return result.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/sys/mount/mount.h b/libc/src/sys/mount/mount.h
new file mode 100644
index 0000000000000..fbc5a6613291f
--- /dev/null
+++ b/libc/src/sys/mount/mount.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for mount.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_MOUNT_MOUNT_H
+#define LLVM_LIBC_SRC_SYS_MOUNT_MOUNT_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int mount(const char *source, const char *target, const char *filesystemtype,
+          unsigned long mountflags, const void *data);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_MOUNT_MOUNT_H

diff  --git a/libc/src/sys/mount/umount.h b/libc/src/sys/mount/umount.h
new file mode 100644
index 0000000000000..586397e486ff7
--- /dev/null
+++ b/libc/src/sys/mount/umount.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for umount.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_MOUNT_UMOUNT_H
+#define LLVM_LIBC_SRC_SYS_MOUNT_UMOUNT_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int umount(const char *target);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_MOUNT_UMOUNT_H

diff  --git a/libc/src/sys/mount/umount2.h b/libc/src/sys/mount/umount2.h
new file mode 100644
index 0000000000000..8573a1c8a3a02
--- /dev/null
+++ b/libc/src/sys/mount/umount2.h
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for umount2.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_MOUNT_UMOUNT2_H
+#define LLVM_LIBC_SRC_SYS_MOUNT_UMOUNT2_H
+
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int umount2(const char *target, int flags);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_MOUNT_UMOUNT2_H

diff  --git a/libc/test/src/sys/CMakeLists.txt b/libc/test/src/sys/CMakeLists.txt
index 5b8642a304e3a..ee56e464cf9a8 100644
--- a/libc/test/src/sys/CMakeLists.txt
+++ b/libc/test/src/sys/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_subdirectory(mman)
+add_subdirectory(mount)
 add_subdirectory(personality)
 add_subdirectory(random)
 add_subdirectory(resource)

diff  --git a/libc/test/src/sys/mount/CMakeLists.txt b/libc/test/src/sys/mount/CMakeLists.txt
new file mode 100644
index 0000000000000..e1420337c1e4f
--- /dev/null
+++ b/libc/test/src/sys/mount/CMakeLists.txt
@@ -0,0 +1,55 @@
+add_libc_testsuite(libc-sys-mount-tests)
+
+add_libc_test(
+  mount_test
+  SUITE
+    libc-sys-mount-tests
+  SRCS
+    mount_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.sys_stat_macros
+    libc.src.__support.CPP.scope
+    libc.src.sys.mount.mount
+    libc.src.sys.mount.umount
+    libc.src.sys.stat.mkdir
+    libc.src.unistd.rmdir
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
+
+add_libc_test(
+  umount_test
+  SUITE
+    libc-sys-mount-tests
+  SRCS
+    umount_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.sys_stat_macros
+    libc.src.__support.CPP.scope
+    libc.src.sys.mount.mount
+    libc.src.sys.mount.umount
+    libc.src.sys.stat.mkdir
+    libc.src.unistd.rmdir
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
+
+add_libc_test(
+  umount2_test
+  SUITE
+    libc-sys-mount-tests
+  SRCS
+    umount2_test.cpp
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.sys_stat_macros
+    libc.src.__support.CPP.scope
+    libc.src.sys.mount.mount
+    libc.src.sys.mount.umount2
+    libc.src.sys.stat.mkdir
+    libc.src.unistd.rmdir
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)

diff  --git a/libc/test/src/sys/mount/mount_test.cpp b/libc/test/src/sys/mount/mount_test.cpp
new file mode 100644
index 0000000000000..be9d9b019651c
--- /dev/null
+++ b/libc/test/src/sys/mount/mount_test.cpp
@@ -0,0 +1,60 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for mount.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "src/__support/CPP/scope.h"
+#include "src/sys/mount/mount.h"
+#include "src/sys/mount/umount.h"
+#include "src/sys/stat/mkdir.h"
+#include "src/unistd/rmdir.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::any_of;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+using LlvmLibcMountTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+// mount needs CAP_SYS_ADMIN, so unprivileged runs see EPERM, container LSMs
+// (e.g. AppArmor/SELinux) report EACCES, and seccomp filters may report ENOSYS.
+
+TEST_F(LlvmLibcMountTest, MountTmpfs) {
+  auto TEST_DIR = libc_make_test_file_path("mount.testdir");
+  LIBC_NAMESPACE::rmdir(TEST_DIR);
+  libc_errno = 0;
+  ASSERT_THAT(LIBC_NAMESPACE::mkdir(TEST_DIR, S_IRWXU), Succeeds(0));
+  LIBC_NAMESPACE::cpp::scope_exit cleanup([&] {
+    LIBC_NAMESPACE::rmdir(TEST_DIR);
+    libc_errno = 0;
+  });
+
+  int ret = LIBC_NAMESPACE::mount("none", TEST_DIR, "tmpfs", 0, nullptr);
+  if (ret == 0) {
+    ASSERT_ERRNO_SUCCESS();
+    LIBC_NAMESPACE::umount(TEST_DIR);
+    libc_errno = 0;
+  } else {
+    ASSERT_THAT(ret, Fails(any_of(EPERM, EACCES, ENOSYS)));
+  }
+}
+
+TEST_F(LlvmLibcMountTest, MountNonExistentTarget) {
+  auto TEST_DIR = libc_make_test_file_path("mount_nonexistent.testdir");
+  LIBC_NAMESPACE::rmdir(TEST_DIR);
+  libc_errno = 0;
+
+  ASSERT_THAT(LIBC_NAMESPACE::mount("none", TEST_DIR, "tmpfs", 0, nullptr),
+              Fails(any_of(ENOENT, EPERM, EACCES, ENOSYS)));
+}

diff  --git a/libc/test/src/sys/mount/umount2_test.cpp b/libc/test/src/sys/mount/umount2_test.cpp
new file mode 100644
index 0000000000000..6aaa865585770
--- /dev/null
+++ b/libc/test/src/sys/mount/umount2_test.cpp
@@ -0,0 +1,65 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for umount2.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "src/__support/CPP/scope.h"
+#include "src/sys/mount/mount.h"
+#include "src/sys/mount/umount2.h"
+#include "src/sys/stat/mkdir.h"
+#include "src/unistd/rmdir.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::any_of;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+using LlvmLibcUmount2Test = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+// mount and umount2 need CAP_SYS_ADMIN, so unprivileged runs see EPERM,
+// container LSMs (e.g. AppArmor/SELinux) report EACCES, and seccomp filters may
+// report ENOSYS.
+
+TEST_F(LlvmLibcUmount2Test, Umount2MountedDir) {
+  auto TEST_DIR = libc_make_test_file_path("umount2.testdir");
+  LIBC_NAMESPACE::rmdir(TEST_DIR);
+  libc_errno = 0;
+  ASSERT_THAT(LIBC_NAMESPACE::mkdir(TEST_DIR, S_IRWXU), Succeeds(0));
+  LIBC_NAMESPACE::cpp::scope_exit cleanup([&] {
+    LIBC_NAMESPACE::rmdir(TEST_DIR);
+    libc_errno = 0;
+  });
+
+  int ret = LIBC_NAMESPACE::mount("none", TEST_DIR, "tmpfs", 0, nullptr);
+  if (ret == 0) {
+    ASSERT_ERRNO_SUCCESS();
+    ASSERT_THAT(LIBC_NAMESPACE::umount2(TEST_DIR, 0), Succeeds(0));
+  } else {
+    ASSERT_THAT(ret, Fails(any_of(EPERM, EACCES, ENOSYS)));
+  }
+}
+
+TEST_F(LlvmLibcUmount2Test, Umount2NotAMountPoint) {
+  auto TEST_DIR = libc_make_test_file_path("umount2_not_mounted.testdir");
+  LIBC_NAMESPACE::rmdir(TEST_DIR);
+  libc_errno = 0;
+  ASSERT_THAT(LIBC_NAMESPACE::mkdir(TEST_DIR, S_IRWXU), Succeeds(0));
+  LIBC_NAMESPACE::cpp::scope_exit cleanup([&] {
+    LIBC_NAMESPACE::rmdir(TEST_DIR);
+    libc_errno = 0;
+  });
+
+  ASSERT_THAT(LIBC_NAMESPACE::umount2(TEST_DIR, 0),
+              Fails(any_of(EINVAL, EPERM, EACCES, ENOSYS)));
+}

diff  --git a/libc/test/src/sys/mount/umount_test.cpp b/libc/test/src/sys/mount/umount_test.cpp
new file mode 100644
index 0000000000000..5c01316ffeb35
--- /dev/null
+++ b/libc/test/src/sys/mount/umount_test.cpp
@@ -0,0 +1,65 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for umount.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "src/__support/CPP/scope.h"
+#include "src/sys/mount/mount.h"
+#include "src/sys/mount/umount.h"
+#include "src/sys/stat/mkdir.h"
+#include "src/unistd/rmdir.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::any_of;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+using LlvmLibcUmountTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+// mount and umount need CAP_SYS_ADMIN, so unprivileged runs see EPERM,
+// container LSMs (e.g. AppArmor/SELinux) report EACCES, and seccomp filters may
+// report ENOSYS.
+
+TEST_F(LlvmLibcUmountTest, UmountMountedDir) {
+  auto TEST_DIR = libc_make_test_file_path("umount.testdir");
+  LIBC_NAMESPACE::rmdir(TEST_DIR);
+  libc_errno = 0;
+  ASSERT_THAT(LIBC_NAMESPACE::mkdir(TEST_DIR, S_IRWXU), Succeeds(0));
+  LIBC_NAMESPACE::cpp::scope_exit cleanup([&] {
+    LIBC_NAMESPACE::rmdir(TEST_DIR);
+    libc_errno = 0;
+  });
+
+  int ret = LIBC_NAMESPACE::mount("none", TEST_DIR, "tmpfs", 0, nullptr);
+  if (ret == 0) {
+    ASSERT_ERRNO_SUCCESS();
+    ASSERT_THAT(LIBC_NAMESPACE::umount(TEST_DIR), Succeeds(0));
+  } else {
+    ASSERT_THAT(ret, Fails(any_of(EPERM, EACCES, ENOSYS)));
+  }
+}
+
+TEST_F(LlvmLibcUmountTest, UmountNotAMountPoint) {
+  auto TEST_DIR = libc_make_test_file_path("umount_not_mounted.testdir");
+  LIBC_NAMESPACE::rmdir(TEST_DIR);
+  libc_errno = 0;
+  ASSERT_THAT(LIBC_NAMESPACE::mkdir(TEST_DIR, S_IRWXU), Succeeds(0));
+  LIBC_NAMESPACE::cpp::scope_exit cleanup([&] {
+    LIBC_NAMESPACE::rmdir(TEST_DIR);
+    libc_errno = 0;
+  });
+
+  ASSERT_THAT(LIBC_NAMESPACE::umount(TEST_DIR),
+              Fails(any_of(EINVAL, EPERM, EACCES, ENOSYS)));
+}


        


More information about the libc-commits mailing list