[libc-commits] [libc] 94a0cfb - [libc][sys/shm] Implement shmctl and add remaining constants. (#239839)
via libc-commits
libc-commits at lists.llvm.org
Mon Nov 9 12:09:39 PST 2026
Author: Alexey Samsonov
Date: 2026-11-09T20:09:33Z
New Revision: 94a0cfbc43131e2b85760c32065e1913af872a31
URL: https://github.com/llvm/llvm-project/commit/94a0cfbc43131e2b85760c32065e1913af872a31
DIFF: https://github.com/llvm/llvm-project/commit/94a0cfbc43131e2b85760c32065e1913af872a31.diff
LOG: [libc][sys/shm] Implement shmctl and add remaining constants. (#239839)
`shmctl` is a POSIX function (see
[docs](https://pubs.opengroup.org/onlinepubs/9799919799/functions/shmctl.html#))
that provides a variety of shared memory control operations.
On Linux, it can be implemented as a simple wrapper around SYS_shmctl
syscall.
Provide the implementation as a simple syscall wrapper, define
(Linux-specific) `struct shmid_ds` definition,
and provide the remaining Linux-specific macro definitions used as flag
values for `shmctl` and
other shared memory functions from `<sys/shm.h>`.
This concludes adding the basic `<sys/shm.h>` functionality.
Added:
libc/hdr/types/struct_shmid_ds.h
libc/include/llvm-libc-types/struct_shmid_ds.h
libc/src/__support/OSUtil/linux/syscall_wrappers/shmctl.h
libc/src/sys/shm/linux/shmctl.cpp
libc/src/sys/shm/shmctl.h
libc/test/src/sys/shm/linux/shmctl_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/hdr/types/CMakeLists.txt
libc/include/CMakeLists.txt
libc/include/llvm-libc-macros/linux/sys-shm-macros.h
libc/include/llvm-libc-types/CMakeLists.txt
libc/include/sys/shm.yaml
libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
libc/src/sys/shm/CMakeLists.txt
libc/src/sys/shm/linux/CMakeLists.txt
libc/test/src/sys/shm/linux/CMakeLists.txt
libc/test/src/sys/shm/linux/shmat_test.cpp
libc/test/src/sys/shm/linux/shmdt_test.cpp
libc/test/src/sys/shm/linux/shmget_test.cpp
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index d0d15b0a35ebcb..3d8281a3014db4 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -346,6 +346,7 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/shm.h entrypoints
libc.src.sys.shm.shmat
+ libc.src.sys.shm.shmctl
libc.src.sys.shm.shmdt
libc.src.sys.shm.shmget
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 9ba8eaad40bd16..a4846ba0712659 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -384,6 +384,7 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/shm.h entrypoints
libc.src.sys.shm.shmat
+ libc.src.sys.shm.shmctl
libc.src.sys.shm.shmdt
libc.src.sys.shm.shmget
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 39efe8b75af9ad..d97f363a06b9ae 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -389,6 +389,7 @@ set(TARGET_LIBC_ENTRYPOINTS
# sys/shm.h entrypoints
libc.src.sys.shm.shmat
+ libc.src.sys.shm.shmctl
libc.src.sys.shm.shmdt
libc.src.sys.shm.shmget
diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index f28639d9d5623f..f038bcfb380004 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -926,6 +926,14 @@ add_proxy_header_library(
libc.include.llvm-libc-types.struct_seminfo
)
+add_proxy_header_library(
+ struct_shmid_ds
+ HDRS
+ struct_shmid_ds.h
+ FULL_BUILD_DEPENDS
+ libc.include.llvm-libc-types.struct_shmid_ds
+)
+
add_proxy_header_library(
gid_t
HDRS
diff --git a/libc/hdr/types/struct_shmid_ds.h b/libc/hdr/types/struct_shmid_ds.h
new file mode 100644
index 00000000000000..45b40673569884
--- /dev/null
+++ b/libc/hdr/types/struct_shmid_ds.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Proxy for struct shmid_ds.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_SHMID_DS_H
+#define LLVM_LIBC_HDR_TYPES_STRUCT_SHMID_DS_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/struct_shmid_ds.h"
+
+#else // Overlay mode
+
+#include <sys/shm.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_STRUCT_SHMID_DS_H
diff --git a/libc/include/CMakeLists.txt b/libc/include/CMakeLists.txt
index 02e7617af536c9..c0e518cabc6745 100644
--- a/libc/include/CMakeLists.txt
+++ b/libc/include/CMakeLists.txt
@@ -953,6 +953,7 @@ add_header_macro(
.llvm-libc-types.pid_t
.llvm-libc-types.shmatt_t
.llvm-libc-types.size_t
+ .llvm-libc-types.struct_shmid_ds
.llvm-libc-types.time_t
.stdint
.sys_ipc
diff --git a/libc/include/llvm-libc-macros/linux/sys-shm-macros.h b/libc/include/llvm-libc-macros/linux/sys-shm-macros.h
index 55f98da9301caa..9ed875d7246dd1 100644
--- a/libc/include/llvm-libc-macros/linux/sys-shm-macros.h
+++ b/libc/include/llvm-libc-macros/linux/sys-shm-macros.h
@@ -17,6 +17,10 @@
#include "../../__llvm-libc-common.h"
#include "../stdint-macros.h"
+// shmget permission flags
+#define SHM_R 0400
+#define SHM_W 0200
+
// shmat flags
#define SHM_RDONLY 010000
#define SHM_RND 020000
@@ -28,4 +32,36 @@
__LLVM_LIBC_CAST(reinterpret_cast, void *, \
__LLVM_LIBC_CAST(static_cast, intptr_t, -1))
+// shmctl commands
+#define SHM_LOCK 11
+#define SHM_UNLOCK 12
+#define SHM_STAT 13
+#define SHM_INFO 14
+#define SHM_STAT_ANY 15
+
+// shm_mode upper byte flags in ipc_perm.
+#define SHM_DEST 01000
+#define SHM_LOCKED 02000
+
+// shmflg bits for shmget
+#define SHM_HUGETLB 04000
+#define SHM_NORESERVE 010000
+
+// Huge page size encoding when SHM_HUGETLB is set.
+#define SHM_HUGE_SHIFT 26
+#define SHM_HUGE_MASK 0x3f
+#define SHM_HUGE_16KB (14U << SHM_HUGE_SHIFT)
+#define SHM_HUGE_64KB (16U << SHM_HUGE_SHIFT)
+#define SHM_HUGE_512KB (19U << SHM_HUGE_SHIFT)
+#define SHM_HUGE_1MB (20U << SHM_HUGE_SHIFT)
+#define SHM_HUGE_2MB (21U << SHM_HUGE_SHIFT)
+#define SHM_HUGE_8MB (23U << SHM_HUGE_SHIFT)
+#define SHM_HUGE_16MB (24U << SHM_HUGE_SHIFT)
+#define SHM_HUGE_32MB (25U << SHM_HUGE_SHIFT)
+#define SHM_HUGE_256MB (28U << SHM_HUGE_SHIFT)
+#define SHM_HUGE_512MB (29U << SHM_HUGE_SHIFT)
+#define SHM_HUGE_1GB (30U << SHM_HUGE_SHIFT)
+#define SHM_HUGE_2GB (31U << SHM_HUGE_SHIFT)
+#define SHM_HUGE_16GB (34U << SHM_HUGE_SHIFT)
+
#endif // LLVM_LIBC_MACROS_LINUX_SYS_SHM_MACROS_H
diff --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 548d1cc92e5033..cbd4594d463055 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -208,6 +208,16 @@ add_header(
.struct_ipc_perm
.time_t
)
+add_header(
+ struct_shmid_ds
+ HDR struct_shmid_ds.h
+ DEPENDS
+ .pid_t
+ .shmatt_t
+ .size_t
+ .struct_ipc_perm
+ .time_t
+)
add_header(struct_timeval HDR struct_timeval.h DEPENDS .suseconds_t .time_t)
add_header(struct_itimerval HDR struct_itimerval.h DEPENDS .struct_timeval)
add_header(struct_rusage HDR struct_rusage.h DEPENDS .struct_timeval)
diff --git a/libc/include/llvm-libc-types/struct_shmid_ds.h b/libc/include/llvm-libc-types/struct_shmid_ds.h
new file mode 100644
index 00000000000000..4388c980d31767
--- /dev/null
+++ b/libc/include/llvm-libc-types/struct_shmid_ds.h
@@ -0,0 +1,40 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Definition of struct shmid_ds.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_TYPES_STRUCT_SHMID_DS_H
+#define LLVM_LIBC_TYPES_STRUCT_SHMID_DS_H
+
+#include "pid_t.h"
+#include "shmatt_t.h"
+#include "size_t.h"
+#include "struct_ipc_perm.h"
+#include "time_t.h"
+
+#ifdef __linux__
+struct shmid_ds {
+ struct ipc_perm shm_perm;
+ size_t shm_segsz;
+ time_t shm_atime;
+ time_t shm_dtime;
+ time_t shm_ctime;
+ pid_t shm_cpid;
+ pid_t shm_lpid;
+ shmatt_t shm_nattch;
+ unsigned long __unused4;
+ unsigned long __unused5;
+};
+#else
+#error "shmid_ds type not defined for the target platform."
+#endif
+
+#endif // LLVM_LIBC_TYPES_STRUCT_SHMID_DS_H
diff --git a/libc/include/sys/shm.yaml b/libc/include/sys/shm.yaml
index 53bd5dc90c3791..46624261e0b2d8 100644
--- a/libc/include/sys/shm.yaml
+++ b/libc/include/sys/shm.yaml
@@ -12,6 +12,14 @@ macros:
macro_header: sys-shm-macros.h
- macro_name: SHM_FAILED
macro_header: sys-shm-macros.h
+ - macro_name: SHM_R
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_W
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
- macro_name: SHM_REMAP
macro_header: sys-shm-macros.h
standards:
@@ -20,11 +28,108 @@ macros:
macro_header: sys-shm-macros.h
standards:
- linux
+ - macro_name: SHM_LOCK
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_UNLOCK
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_STAT
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_INFO
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_STAT_ANY
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_DEST
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_LOCKED
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_HUGETLB
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_NORESERVE
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_HUGE_SHIFT
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_HUGE_MASK
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_HUGE_16KB
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_HUGE_64KB
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_HUGE_512KB
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_HUGE_1MB
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_HUGE_2MB
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_HUGE_8MB
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_HUGE_16MB
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_HUGE_32MB
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_HUGE_256MB
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_HUGE_512MB
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_HUGE_1GB
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_HUGE_2GB
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
+ - macro_name: SHM_HUGE_16GB
+ macro_header: sys-shm-macros.h
+ standards:
+ - linux
types:
- type_name: intptr_t
- type_name: pid_t
- type_name: shmatt_t
- type_name: size_t
+ - type_name: struct_shmid_ds
- type_name: time_t
enums: []
objects: []
@@ -37,6 +142,14 @@ functions:
- type: int
- type: const void *
- type: int
+ - name: shmctl
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: int
+ - type: int
+ - type: struct shmid_ds *
- name: shmdt
standards:
- posix
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index d9736316ef4a8d..1d465d5721194b 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -234,6 +234,19 @@ add_header_library(
libc.src.__support.macros.config
)
+add_header_library(
+ shmctl
+ HDRS
+ shmctl.h
+ DEPENDS
+ libc.hdr.types.struct_shmid_ds
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+)
+
add_header_library(
shmdt
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/shmctl.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/shmctl.h
new file mode 100644
index 00000000000000..28746ceb004a41
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/shmctl.h
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 shmctl.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SHMCTL_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SHMCTL_H
+
+#include "hdr/types/struct_shmid_ds.h"
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> shmctl(int shmid, int cmd, struct shmid_ds *buf) {
+ return syscall_checked<int>(SYS_shmctl, shmid, cmd, buf);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_SHMCTL_H
diff --git a/libc/src/sys/shm/CMakeLists.txt b/libc/src/sys/shm/CMakeLists.txt
index ec80e690a283cb..89e0241605b57f 100644
--- a/libc/src/sys/shm/CMakeLists.txt
+++ b/libc/src/sys/shm/CMakeLists.txt
@@ -9,6 +9,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.shmat
)
+add_entrypoint_object(
+ shmctl
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.shmctl
+)
+
add_entrypoint_object(
shmdt
ALIAS
diff --git a/libc/src/sys/shm/linux/CMakeLists.txt b/libc/src/sys/shm/linux/CMakeLists.txt
index 6d337006c97183..8bce3774f57fad 100644
--- a/libc/src/sys/shm/linux/CMakeLists.txt
+++ b/libc/src/sys/shm/linux/CMakeLists.txt
@@ -13,6 +13,21 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ shmctl
+ SRCS
+ shmctl.cpp
+ HDRS
+ ../shmctl.h
+ DEPENDS
+ libc.hdr.types.struct_shmid_ds
+ libc.src.__support.OSUtil.linux.syscall_wrappers.shmctl
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
+ libc.src.errno.errno
+)
+
add_entrypoint_object(
shmdt
SRCS
diff --git a/libc/src/sys/shm/linux/shmctl.cpp b/libc/src/sys/shm/linux/shmctl.cpp
new file mode 100644
index 00000000000000..17a4dbf7d1af79
--- /dev/null
+++ b/libc/src/sys/shm/linux/shmctl.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 shmctl.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/shm/shmctl.h"
+
+#include "hdr/types/struct_shmid_ds.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/shmctl.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, shmctl, (int shmid, int cmd, struct shmid_ds *buf)) {
+ ErrorOr<int> ret = linux_syscalls::shmctl(shmid, cmd, buf);
+ if (!ret) {
+ libc_errno = ret.error();
+ return -1;
+ }
+ return ret.value();
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/shm/shmctl.h b/libc/src/sys/shm/shmctl.h
new file mode 100644
index 00000000000000..72a7fc55bb280f
--- /dev/null
+++ b/libc/src/sys/shm/shmctl.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 shmctl.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_SHM_SHMCTL_H
+#define LLVM_LIBC_SRC_SYS_SHM_SHMCTL_H
+
+#include "hdr/types/struct_shmid_ds.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int shmctl(int shmid, int cmd, struct shmid_ds *buf);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SYS_SHM_SHMCTL_H
diff --git a/libc/test/src/sys/shm/linux/CMakeLists.txt b/libc/test/src/sys/shm/linux/CMakeLists.txt
index f0df5c05ed642e..4b6e9c16d7af08 100644
--- a/libc/test/src/sys/shm/linux/CMakeLists.txt
+++ b/libc/test/src/sys/shm/linux/CMakeLists.txt
@@ -12,13 +12,33 @@ add_libc_test(
libc.hdr.sys_ipc_macros
libc.hdr.sys_shm_macros
libc.hdr.sys_stat_macros
+ libc.src.__support.CPP.scope
libc.src.sys.shm.shmat
+ libc.src.sys.shm.shmctl
libc.src.sys.shm.shmdt
libc.src.sys.shm.shmget
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
)
+add_libc_test(
+ shmctl_test
+ SUITE
+ libc-sys-shm-tests
+ SRCS
+ shmctl_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.sys_ipc_macros
+ libc.hdr.sys_stat_macros
+ libc.hdr.types.struct_shmid_ds
+ libc.src.__support.CPP.scope
+ libc.src.sys.shm.shmctl
+ libc.src.sys.shm.shmget
+ libc.test.UnitTest.ErrnoCheckingTest
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
+
add_libc_test(
shmdt_test
SUITE
@@ -30,7 +50,9 @@ add_libc_test(
libc.hdr.sys_ipc_macros
libc.hdr.sys_shm_macros
libc.hdr.sys_stat_macros
+ libc.src.__support.CPP.scope
libc.src.sys.shm.shmat
+ libc.src.sys.shm.shmctl
libc.src.sys.shm.shmdt
libc.src.sys.shm.shmget
libc.test.UnitTest.ErrnoCheckingTest
@@ -47,6 +69,8 @@ add_libc_test(
libc.hdr.errno_macros
libc.hdr.sys_ipc_macros
libc.hdr.sys_stat_macros
+ libc.src.__support.CPP.scope
+ libc.src.sys.shm.shmctl
libc.src.sys.shm.shmget
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
diff --git a/libc/test/src/sys/shm/linux/shmat_test.cpp b/libc/test/src/sys/shm/linux/shmat_test.cpp
index 93a7299d967ea8..d9e208867bbea7 100644
--- a/libc/test/src/sys/shm/linux/shmat_test.cpp
+++ b/libc/test/src/sys/shm/linux/shmat_test.cpp
@@ -16,7 +16,9 @@
#include "hdr/sys_ipc_macros.h"
#include "hdr/sys_shm_macros.h"
#include "hdr/sys_stat_macros.h"
+#include "src/__support/CPP/scope.h"
#include "src/sys/shm/shmat.h"
+#include "src/sys/shm/shmctl.h"
#include "src/sys/shm/shmdt.h"
#include "src/sys/shm/shmget.h"
#include "test/UnitTest/ErrnoCheckingTest.h"
@@ -24,6 +26,7 @@
#include "test/UnitTest/Test.h"
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LIBC_NAMESPACE::cpp::scope_exit;
using LlvmLibcShmAtTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
TEST_F(LlvmLibcShmAtTest, AttachReadWriteAndReadOnly) {
@@ -31,6 +34,9 @@ TEST_F(LlvmLibcShmAtTest, AttachReadWriteAndReadOnly) {
ASSERT_THAT(shmid = LIBC_NAMESPACE::shmget(
IPC_PRIVATE, 256, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR),
returns(GE(0)).with_errno(EQ(0)));
+ scope_exit cleanup([&] {
+ EXPECT_THAT(LIBC_NAMESPACE::shmctl(shmid, IPC_RMID, nullptr), Succeeds(0));
+ });
// Attach the newly created segment for read-write access.
void *rw_addr = LIBC_NAMESPACE::shmat(shmid, nullptr, 0);
@@ -52,7 +58,6 @@ TEST_F(LlvmLibcShmAtTest, AttachReadWriteAndReadOnly) {
EXPECT_THAT(LIBC_NAMESPACE::shmdt(rw_addr), Succeeds(0));
EXPECT_THAT(LIBC_NAMESPACE::shmdt(ro_addr), Succeeds(0));
- // TODO: Remove the segment with shmctl once shmctl is implemented.
}
TEST_F(LlvmLibcShmAtTest, UnalignedAddrWithoutShmRndFails) {
@@ -60,6 +65,9 @@ TEST_F(LlvmLibcShmAtTest, UnalignedAddrWithoutShmRndFails) {
ASSERT_THAT(shmid = LIBC_NAMESPACE::shmget(
IPC_PRIVATE, 256, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR),
returns(GE(0)).with_errno(EQ(0)));
+ scope_exit cleanup([&] {
+ EXPECT_THAT(LIBC_NAMESPACE::shmctl(shmid, IPC_RMID, nullptr), Succeeds(0));
+ });
void *addr = LIBC_NAMESPACE::shmat(shmid, nullptr, 0);
ASSERT_ERRNO_SUCCESS();
@@ -72,7 +80,6 @@ TEST_F(LlvmLibcShmAtTest, UnalignedAddrWithoutShmRndFails) {
Fails(EINVAL, SHM_FAILED));
EXPECT_THAT(LIBC_NAMESPACE::shmdt(addr), Succeeds(0));
- // TODO: Remove the segment with shmctl once shmctl is implemented.
}
TEST_F(LlvmLibcShmAtTest, UnalignedAddrWithShmRndRoundsDown) {
@@ -80,6 +87,9 @@ TEST_F(LlvmLibcShmAtTest, UnalignedAddrWithShmRndRoundsDown) {
ASSERT_THAT(shmid = LIBC_NAMESPACE::shmget(
IPC_PRIVATE, 256, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR),
returns(GE(0)).with_errno(EQ(0)));
+ scope_exit cleanup([&] {
+ EXPECT_THAT(LIBC_NAMESPACE::shmctl(shmid, IPC_RMID, nullptr), Succeeds(0));
+ });
void *addr = LIBC_NAMESPACE::shmat(shmid, nullptr, 0);
ASSERT_ERRNO_SUCCESS();
@@ -95,7 +105,6 @@ TEST_F(LlvmLibcShmAtTest, UnalignedAddrWithShmRndRoundsDown) {
EXPECT_EQ(rounded_addr, addr);
EXPECT_THAT(LIBC_NAMESPACE::shmdt(rounded_addr), Succeeds(0));
- // TODO: Remove the segment with shmctl once shmctl is implemented.
}
TEST_F(LlvmLibcShmAtTest, InvalidShmidReturnsEinval) {
diff --git a/libc/test/src/sys/shm/linux/shmctl_test.cpp b/libc/test/src/sys/shm/linux/shmctl_test.cpp
new file mode 100644
index 00000000000000..474ffdc66d7715
--- /dev/null
+++ b/libc/test/src/sys/shm/linux/shmctl_test.cpp
@@ -0,0 +1,63 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 shmctl.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/errno_macros.h"
+#include "hdr/sys_ipc_macros.h"
+#include "hdr/sys_stat_macros.h"
+#include "hdr/types/struct_shmid_ds.h"
+#include "src/__support/CPP/scope.h"
+#include "src/sys/shm/shmctl.h"
+#include "src/sys/shm/shmget.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LIBC_NAMESPACE::cpp::scope_exit;
+using LlvmLibcShmCtlTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcShmCtlTest, StatSetAndRemove) {
+ int shmid;
+ ASSERT_THAT(shmid = LIBC_NAMESPACE::shmget(
+ IPC_PRIVATE, 256, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR),
+ returns(GE(0)).with_errno(EQ(0)));
+ scope_exit cleanup([&] {
+ EXPECT_THAT(LIBC_NAMESPACE::shmctl(shmid, IPC_RMID, nullptr), Succeeds(0));
+ });
+
+ struct shmid_ds ds = {};
+ ASSERT_THAT(LIBC_NAMESPACE::shmctl(shmid, IPC_STAT, &ds), Succeeds(0));
+ EXPECT_EQ(ds.shm_segsz, size_t(256));
+ EXPECT_EQ(ds.shm_nattch, shmatt_t(0));
+ EXPECT_EQ(ds.shm_perm.mode & 0777, mode_t(S_IRUSR | S_IWUSR));
+
+ ds.shm_perm.mode = S_IRUSR;
+ ASSERT_THAT(LIBC_NAMESPACE::shmctl(shmid, IPC_SET, &ds), Succeeds(0));
+
+ struct shmid_ds updated_ds = {};
+ ASSERT_THAT(LIBC_NAMESPACE::shmctl(shmid, IPC_STAT, &updated_ds),
+ Succeeds(0));
+ EXPECT_EQ(updated_ds.shm_perm.mode & 0777, mode_t(S_IRUSR));
+
+ ASSERT_THAT(LIBC_NAMESPACE::shmctl(shmid, IPC_RMID, nullptr), Succeeds(0));
+ cleanup.release();
+
+ // Verify that once shmid is deleted, we can no longer query stat.
+ EXPECT_THAT(LIBC_NAMESPACE::shmctl(shmid, IPC_STAT, &ds), Fails(EINVAL));
+}
+
+TEST_F(LlvmLibcShmCtlTest, InvalidArgsReturnEinval) {
+ struct shmid_ds ds = {};
+ EXPECT_THAT(LIBC_NAMESPACE::shmctl(-1, IPC_STAT, &ds), Fails(EINVAL));
+ EXPECT_THAT(LIBC_NAMESPACE::shmctl(-1, -1, &ds), Fails(EINVAL));
+}
diff --git a/libc/test/src/sys/shm/linux/shmdt_test.cpp b/libc/test/src/sys/shm/linux/shmdt_test.cpp
index c716c0e674f5bd..35a3d176a9548a 100644
--- a/libc/test/src/sys/shm/linux/shmdt_test.cpp
+++ b/libc/test/src/sys/shm/linux/shmdt_test.cpp
@@ -15,7 +15,9 @@
#include "hdr/sys_ipc_macros.h"
#include "hdr/sys_shm_macros.h"
#include "hdr/sys_stat_macros.h"
+#include "src/__support/CPP/scope.h"
#include "src/sys/shm/shmat.h"
+#include "src/sys/shm/shmctl.h"
#include "src/sys/shm/shmdt.h"
#include "src/sys/shm/shmget.h"
#include "test/UnitTest/ErrnoCheckingTest.h"
@@ -23,6 +25,7 @@
#include "test/UnitTest/Test.h"
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LIBC_NAMESPACE::cpp::scope_exit;
using LlvmLibcShmDtTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
TEST_F(LlvmLibcShmDtTest, DetachSegment) {
@@ -30,6 +33,9 @@ TEST_F(LlvmLibcShmDtTest, DetachSegment) {
ASSERT_THAT(shmid = LIBC_NAMESPACE::shmget(
IPC_PRIVATE, 256, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR),
returns(GE(0)).with_errno(EQ(0)));
+ scope_exit cleanup([&] {
+ EXPECT_THAT(LIBC_NAMESPACE::shmctl(shmid, IPC_RMID, nullptr), Succeeds(0));
+ });
void *addr = LIBC_NAMESPACE::shmat(shmid, nullptr, 0);
ASSERT_ERRNO_SUCCESS();
@@ -44,8 +50,6 @@ TEST_F(LlvmLibcShmDtTest, DetachSegment) {
// Detaching an already detached address must fail with EINVAL.
EXPECT_THAT(LIBC_NAMESPACE::shmdt(addr), Fails(EINVAL));
-
- // TODO: Remove the segment with shmctl once shmctl is implemented.
}
TEST_F(LlvmLibcShmDtTest, InvalidAddrReturnsEinval) {
diff --git a/libc/test/src/sys/shm/linux/shmget_test.cpp b/libc/test/src/sys/shm/linux/shmget_test.cpp
index 8846205d5728a5..d0244d6df38ece 100644
--- a/libc/test/src/sys/shm/linux/shmget_test.cpp
+++ b/libc/test/src/sys/shm/linux/shmget_test.cpp
@@ -14,12 +14,15 @@
#include "hdr/errno_macros.h"
#include "hdr/sys_ipc_macros.h"
#include "hdr/sys_stat_macros.h"
+#include "src/__support/CPP/scope.h"
+#include "src/sys/shm/shmctl.h"
#include "src/sys/shm/shmget.h"
#include "test/UnitTest/ErrnoCheckingTest.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LIBC_NAMESPACE::cpp::scope_exit;
using LlvmLibcShmGetTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
TEST_F(LlvmLibcShmGetTest, AllocatesSegment) {
@@ -27,8 +30,9 @@ TEST_F(LlvmLibcShmGetTest, AllocatesSegment) {
ASSERT_THAT(shmid = LIBC_NAMESPACE::shmget(
IPC_PRIVATE, 1, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR),
returns(GE(0)).with_errno(EQ(0)));
- // TODO: Clean up the allocated segment with shmctl(shmid, IPC_RMID, nullptr)
- // once shmctl is implemented.
+ scope_exit cleanup([&] {
+ EXPECT_THAT(LIBC_NAMESPACE::shmctl(shmid, IPC_RMID, nullptr), Succeeds(0));
+ });
}
TEST_F(LlvmLibcShmGetTest, InvalidSizeReturnsEinval) {
More information about the libc-commits
mailing list