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

Michael Jones via libc-commits libc-commits at lists.llvm.org
Wed Nov 18 15:59:36 PST 2026


https://github.com/michaelrj-google updated https://github.com/llvm/llvm-project/pull/244047

>From 0a357eb4a42f3421f87085285e27b6c1ab3571d7 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Wed, 18 Nov 2026 23:25:03 +0000
Subject: [PATCH 1/2] [libc] implement sys/mount.h

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.
---
 libc/config/linux/aarch64/entrypoints.txt     |  5 ++
 libc/config/linux/arm/entrypoints.txt         |  5 ++
 libc/config/linux/arm/headers.txt             |  1 +
 libc/config/linux/riscv/entrypoints.txt       |  5 ++
 libc/config/linux/x86_64/entrypoints.txt      |  5 ++
 .../llvm-libc-macros/linux/sys-mount-macros.h | 30 +++------
 libc/include/sys/mount.yaml                   | 33 +++++++++-
 .../linux/syscall_wrappers/CMakeLists.txt     | 24 +++++++
 .../OSUtil/linux/syscall_wrappers/mount.h     | 36 +++++++++++
 .../OSUtil/linux/syscall_wrappers/umount2.h   | 33 ++++++++++
 libc/src/sys/CMakeLists.txt                   |  1 +
 libc/src/sys/mount/CMakeLists.txt             | 24 +++++++
 libc/src/sys/mount/linux/CMakeLists.txt       | 44 +++++++++++++
 libc/src/sys/mount/linux/mount.cpp            | 37 +++++++++++
 libc/src/sys/mount/linux/umount.cpp           | 33 ++++++++++
 libc/src/sys/mount/linux/umount2.cpp          | 33 ++++++++++
 libc/src/sys/mount/mount.h                    | 26 ++++++++
 libc/src/sys/mount/umount.h                   | 25 ++++++++
 libc/src/sys/mount/umount2.h                  | 25 ++++++++
 libc/test/src/sys/CMakeLists.txt              |  1 +
 libc/test/src/sys/mount/CMakeLists.txt        | 55 ++++++++++++++++
 libc/test/src/sys/mount/mount_test.cpp        | 60 +++++++++++++++++
 libc/test/src/sys/mount/umount2_test.cpp      | 64 +++++++++++++++++++
 libc/test/src/sys/mount/umount_test.cpp       | 64 +++++++++++++++++++
 24 files changed, 646 insertions(+), 23 deletions(-)
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/mount.h
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/umount2.h
 create mode 100644 libc/src/sys/mount/CMakeLists.txt
 create mode 100644 libc/src/sys/mount/linux/CMakeLists.txt
 create mode 100644 libc/src/sys/mount/linux/mount.cpp
 create mode 100644 libc/src/sys/mount/linux/umount.cpp
 create mode 100644 libc/src/sys/mount/linux/umount2.cpp
 create mode 100644 libc/src/sys/mount/mount.h
 create mode 100644 libc/src/sys/mount/umount.h
 create mode 100644 libc/src/sys/mount/umount2.h
 create mode 100644 libc/test/src/sys/mount/CMakeLists.txt
 create mode 100644 libc/test/src/sys/mount/mount_test.cpp
 create mode 100644 libc/test/src/sys/mount/umount2_test.cpp
 create mode 100644 libc/test/src/sys/mount/umount_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index b43c7c670db822..512dcf5deb809f 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -329,6 +329,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 956a154cf0d5ec..a21c38634a9ba3 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -230,6 +230,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 b9d4a9dc6724a7..bf52235cc1fab0 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 6354889fa0edd5..2fc9427f0ba745 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -367,6 +367,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 0e7ae76c11457c..24f29f90784b23 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -367,6 +367,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 a18cd8f811e146..6d7c5ad77c1604 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 9c2964ff43e355..b83ad195e861a6 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 bb9d3a1846bef6..17f5b8b0344bf5 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 00000000000000..7669a6d508ffaf
--- /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 00000000000000..09172fcff2e51c
--- /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 c891c471ed4f13..6a7214410bd283 100644
--- a/libc/src/sys/CMakeLists.txt
+++ b/libc/src/sys/CMakeLists.txt
@@ -5,6 +5,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 00000000000000..b382b4794cf304
--- /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 00000000000000..5373d7677bfc06
--- /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 00000000000000..1f10b1032d2c03
--- /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 00000000000000..ea2847d4515ccd
--- /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 00000000000000..35031b7957d659
--- /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 00000000000000..fbc5a6613291f0
--- /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 00000000000000..586397e486ff7f
--- /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 00000000000000..8573a1c8a3a025
--- /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 6650f0842f3ae9..1317385d29a73f 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 00000000000000..e1420337c1e4f3
--- /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 00000000000000..5ac228576e1d46
--- /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.
+// Sandboxes that filter this syscall with seccomp report ENOSYS instead.
+
+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, 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, 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 00000000000000..c431f53548ddec
--- /dev/null
+++ b/libc/test/src/sys/mount/umount2_test.cpp
@@ -0,0 +1,64 @@
+//===----------------------------------------------------------------------===//
+//
+// 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.
+// Sandboxes that filter these syscalls with seccomp report ENOSYS instead.
+
+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, 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, 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 00000000000000..572c38bdb6c158
--- /dev/null
+++ b/libc/test/src/sys/mount/umount_test.cpp
@@ -0,0 +1,64 @@
+//===----------------------------------------------------------------------===//
+//
+// 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.
+// Sandboxes that filter these syscalls with seccomp report ENOSYS instead.
+
+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, 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, ENOSYS)));
+}

>From f04eacb784edd4a19a87d3aecdc263abde5d6939 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Wed, 18 Nov 2026 23:59:05 +0000
Subject: [PATCH 2/2] add EACCES for the buildbots.

---
 libc/test/src/sys/mount/mount_test.cpp   | 8 ++++----
 libc/test/src/sys/mount/umount2_test.cpp | 9 +++++----
 libc/test/src/sys/mount/umount_test.cpp  | 9 +++++----
 3 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/libc/test/src/sys/mount/mount_test.cpp b/libc/test/src/sys/mount/mount_test.cpp
index 5ac228576e1d46..be9d9b019651cb 100644
--- a/libc/test/src/sys/mount/mount_test.cpp
+++ b/libc/test/src/sys/mount/mount_test.cpp
@@ -27,8 +27,8 @@ 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.
-// Sandboxes that filter this syscall with seccomp report ENOSYS instead.
+// 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");
@@ -46,7 +46,7 @@ TEST_F(LlvmLibcMountTest, MountTmpfs) {
     LIBC_NAMESPACE::umount(TEST_DIR);
     libc_errno = 0;
   } else {
-    ASSERT_THAT(ret, Fails(any_of(EPERM, ENOSYS)));
+    ASSERT_THAT(ret, Fails(any_of(EPERM, EACCES, ENOSYS)));
   }
 }
 
@@ -56,5 +56,5 @@ TEST_F(LlvmLibcMountTest, MountNonExistentTarget) {
   libc_errno = 0;
 
   ASSERT_THAT(LIBC_NAMESPACE::mount("none", TEST_DIR, "tmpfs", 0, nullptr),
-              Fails(any_of(ENOENT, EPERM, ENOSYS)));
+              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
index c431f53548ddec..6aaa865585770b 100644
--- a/libc/test/src/sys/mount/umount2_test.cpp
+++ b/libc/test/src/sys/mount/umount2_test.cpp
@@ -27,8 +27,9 @@ 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.
-// Sandboxes that filter these syscalls with seccomp report ENOSYS instead.
+// 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");
@@ -45,7 +46,7 @@ TEST_F(LlvmLibcUmount2Test, Umount2MountedDir) {
     ASSERT_ERRNO_SUCCESS();
     ASSERT_THAT(LIBC_NAMESPACE::umount2(TEST_DIR, 0), Succeeds(0));
   } else {
-    ASSERT_THAT(ret, Fails(any_of(EPERM, ENOSYS)));
+    ASSERT_THAT(ret, Fails(any_of(EPERM, EACCES, ENOSYS)));
   }
 }
 
@@ -60,5 +61,5 @@ TEST_F(LlvmLibcUmount2Test, Umount2NotAMountPoint) {
   });
 
   ASSERT_THAT(LIBC_NAMESPACE::umount2(TEST_DIR, 0),
-              Fails(any_of(EINVAL, EPERM, ENOSYS)));
+              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
index 572c38bdb6c158..5c01316ffeb350 100644
--- a/libc/test/src/sys/mount/umount_test.cpp
+++ b/libc/test/src/sys/mount/umount_test.cpp
@@ -27,8 +27,9 @@ 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.
-// Sandboxes that filter these syscalls with seccomp report ENOSYS instead.
+// 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");
@@ -45,7 +46,7 @@ TEST_F(LlvmLibcUmountTest, UmountMountedDir) {
     ASSERT_ERRNO_SUCCESS();
     ASSERT_THAT(LIBC_NAMESPACE::umount(TEST_DIR), Succeeds(0));
   } else {
-    ASSERT_THAT(ret, Fails(any_of(EPERM, ENOSYS)));
+    ASSERT_THAT(ret, Fails(any_of(EPERM, EACCES, ENOSYS)));
   }
 }
 
@@ -60,5 +61,5 @@ TEST_F(LlvmLibcUmountTest, UmountNotAMountPoint) {
   });
 
   ASSERT_THAT(LIBC_NAMESPACE::umount(TEST_DIR),
-              Fails(any_of(EINVAL, EPERM, ENOSYS)));
+              Fails(any_of(EINVAL, EPERM, EACCES, ENOSYS)));
 }



More information about the libc-commits mailing list