[libc-commits] [libc] [libc] Implement sigpending (Part of #236167) (PR #237568)
Anshitha Yeruva via libc-commits
libc-commits at lists.llvm.org
Sat Oct 31 07:03:07 PDT 2026
https://github.com/anshithayeruva created https://github.com/llvm/llvm-project/pull/237568
This patch implements the POSIX sigpending function in LLVM libc, along with unit tests.
Part of #236167.
>From ab503583aea5474cd413d9a27a216a5c5b124ca0 Mon Sep 17 00:00:00 2001
From: anshithayeruva <anshithayeruva18 at gmail.com>
Date: Sat, 31 Oct 2026 19:31:46 +0530
Subject: [PATCH] [libc] Implement sigpending (Part of #236167)
---
libc/config/linux/aarch64/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 3 +-
libc/include/signal.yaml | 6 +++
.../linux/syscall_wrappers/CMakeLists.txt | 12 +++++
.../linux/syscall_wrappers/rt_sigpending.h | 34 ++++++++++++++
libc/src/signal/CMakeLists.txt | 6 +++
libc/src/signal/linux/CMakeLists.txt | 11 +++++
libc/src/signal/linux/sigpending.cpp | 28 +++++++++++
libc/src/signal/sigpending.h | 21 +++++++++
libc/test/src/signal/CMakeLists.txt | 18 ++++++-
libc/test/src/signal/sigpending_test.cpp | 47 +++++++++++++++++++
12 files changed, 186 insertions(+), 2 deletions(-)
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/rt_sigpending.h
create mode 100644 libc/src/signal/linux/sigpending.cpp
create mode 100644 libc/src/signal/sigpending.h
create mode 100644 libc/test/src/signal/sigpending_test.cpp
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 09b1b9080e0874..1c712d38a35adc 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1394,6 +1394,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.signal.sigfillset
libc.src.signal.sigismember
libc.src.signal.signal
+ libc.src.signal.sigpending
libc.src.signal.sigprocmask
# spawn.h entrypoints
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 69189b37ddddd4..6f9cffa65986c1 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1580,6 +1580,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.signal.sigfillset
libc.src.signal.sigismember
libc.src.signal.signal
+ libc.src.signal.sigpending
libc.src.signal.sigprocmask
# spawn.h entrypoints
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 746e712ec160e6..a9651ae91a3968 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1606,8 +1606,9 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.signal.sigfillset
libc.src.signal.sigismember
libc.src.signal.signal
+ libc.src.signal.sigpending
libc.src.signal.sigprocmask
-
+
# spawn.h entrypoints
libc.src.spawn.posix_spawn
libc.src.spawn.posix_spawn_file_actions_addclose
diff --git a/libc/include/signal.yaml b/libc/include/signal.yaml
index b763da7eeb6025..e95959d9f9c05a 100644
--- a/libc/include/signal.yaml
+++ b/libc/include/signal.yaml
@@ -119,6 +119,12 @@ functions:
arguments:
- type: int
- type: sighandler_t
+ - name: sigpending
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: sigset_t *
- name: sigprocmask
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 05eb6f0fa6f776..06e45d23c1e3cd 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -647,6 +647,18 @@ add_header_library(
libc.hdr.types.sigset_t
libc.include.sys_syscall
)
+add_header_library(
+ rt_sigpending
+ HDRS
+ rt_sigpending.h
+ DEPENDS
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+ libc.hdr.types.sigset_t
+ libc.include.sys_syscall
+)
add_header_library(
raise
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/rt_sigpending.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/rt_sigpending.h
new file mode 100644
index 00000000000000..d41a1ab386bc99
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/rt_sigpending.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
+/// Implementation header for rt_sigpending.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RT_SIGPENDING_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RT_SIGPENDING_H
+
+#include "hdr/types/sigset_t.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> rt_sigpending(sigset_t *set) {
+ return syscall_checked<int>(SYS_rt_sigpending, set, sizeof(sigset_t));
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_RT_SIGPENDING_H
\ No newline at end of file
diff --git a/libc/src/signal/CMakeLists.txt b/libc/src/signal/CMakeLists.txt
index 8da94d1eea85fd..529815444f31ad 100644
--- a/libc/src/signal/CMakeLists.txt
+++ b/libc/src/signal/CMakeLists.txt
@@ -22,6 +22,12 @@ add_entrypoint_object(
DEPENDS
.${LIBC_TARGET_OS}.sigaction
)
+add_entrypoint_object(
+ sigpending
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.sigpending
+)
add_entrypoint_object(
sigaltstack
diff --git a/libc/src/signal/linux/CMakeLists.txt b/libc/src/signal/linux/CMakeLists.txt
index 77898cf9aab636..99aaec14c3805c 100644
--- a/libc/src/signal/linux/CMakeLists.txt
+++ b/libc/src/signal/linux/CMakeLists.txt
@@ -94,6 +94,17 @@ add_entrypoint_object(
libc.src.__support.OSUtil.linux.syscall_wrappers.rt_sigprocmask
libc.src.errno.errno
)
+add_entrypoint_object(
+ sigpending
+ SRCS
+ sigpending.cpp
+ HDRS
+ ../sigpending.h
+ DEPENDS
+ libc.hdr.types.sigset_t
+ libc.src.__support.OSUtil.linux.syscall_wrappers.rt_sigpending
+ libc.src.errno.errno
+)
add_entrypoint_object(
pthread_sigmask
diff --git a/libc/src/signal/linux/sigpending.cpp b/libc/src/signal/linux/sigpending.cpp
new file mode 100644
index 00000000000000..4c72d2d672ecf5
--- /dev/null
+++ b/libc/src/signal/linux/sigpending.cpp
@@ -0,0 +1,28 @@
+//===-- Linux implementation of sigpending -------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/signal/sigpending.h"
+
+#include "hdr/types/sigset_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/rt_sigpending.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, sigpending, (sigset_t *set)) {
+ auto result = linux_syscalls::rt_sigpending(set);
+ if (result.has_value())
+ return 0;
+
+ libc_errno = result.error();
+ return -1;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
\ No newline at end of file
diff --git a/libc/src/signal/sigpending.h b/libc/src/signal/sigpending.h
new file mode 100644
index 00000000000000..ef13a778eaaa83
--- /dev/null
+++ b/libc/src/signal/sigpending.h
@@ -0,0 +1,21 @@
+//===-- Implementation header for sigpending function -----------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SIGNAL_SIGPENDING_H
+#define LLVM_LIBC_SRC_SIGNAL_SIGPENDING_H
+
+#include "hdr/types/sigset_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int sigpending(sigset_t *set);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SIGNAL_SIGPENDING_H
\ No newline at end of file
diff --git a/libc/test/src/signal/CMakeLists.txt b/libc/test/src/signal/CMakeLists.txt
index 287977e2762236..9270f33b96f035 100644
--- a/libc/test/src/signal/CMakeLists.txt
+++ b/libc/test/src/signal/CMakeLists.txt
@@ -53,7 +53,23 @@ add_libc_test(
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
)
-
+add_libc_test(
+ sigpending_test
+ SUITE
+ libc-signal-tests
+ SRCS
+ sigpending_test.cpp
+ DEPENDS
+ libc.hdr.signal_macros
+ libc.hdr.types.sigset_t
+ libc.src.signal.raise
+ libc.src.signal.sigaddset
+ libc.src.signal.sigemptyset
+ libc.src.signal.sigismember
+ libc.src.signal.sigpending
+ libc.src.signal.sigprocmask
+ libc.test.UnitTest.ErrnoSetterMatcher
+)
add_libc_test(
pthread_sigmask_test
SUITE
diff --git a/libc/test/src/signal/sigpending_test.cpp b/libc/test/src/signal/sigpending_test.cpp
new file mode 100644
index 00000000000000..70a052c9ea250b
--- /dev/null
+++ b/libc/test/src/signal/sigpending_test.cpp
@@ -0,0 +1,47 @@
+//===-- Unittests for sigpending ------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/signal/sigpending.h"
+
+#include "hdr/signal_macros.h"
+#include "hdr/types/sigset_t.h"
+#include "src/signal/raise.h"
+#include "src/signal/sigaddset.h"
+#include "src/signal/sigemptyset.h"
+#include "src/signal/sigismember.h"
+#include "src/signal/sigprocmask.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+
+TEST(LlvmLibcSigpendingTest, NoBlockedSignals) {
+ sigset_t pending;
+ ASSERT_THAT(LIBC_NAMESPACE::sigpending(&pending), Succeeds(0));
+}
+
+TEST(LlvmLibcSigpendingTest, BlockAndRaise) {
+ // Block SIGUSR1.
+ sigset_t blockset;
+ ASSERT_EQ(LIBC_NAMESPACE::sigemptyset(&blockset), 0);
+ ASSERT_EQ(LIBC_NAMESPACE::sigaddset(&blockset, SIGUSR1), 0);
+
+ sigset_t oldset;
+ ASSERT_EQ(LIBC_NAMESPACE::sigprocmask(SIG_BLOCK, &blockset, &oldset), 0);
+
+ // Raise SIGUSR1; it should become pending since it is blocked.
+ ASSERT_EQ(LIBC_NAMESPACE::raise(SIGUSR1), 0);
+
+ // Check that SIGUSR1 is now pending.
+ sigset_t pending;
+ ASSERT_THAT(LIBC_NAMESPACE::sigpending(&pending), Succeeds(0));
+ ASSERT_EQ(LIBC_NAMESPACE::sigismember(&pending, SIGUSR1), 1);
+
+ // Restore the old signal mask.
+ ASSERT_EQ(LIBC_NAMESPACE::sigprocmask(SIG_SETMASK, &oldset, nullptr), 0);
+}
\ No newline at end of file
More information about the libc-commits
mailing list