[libc-commits] [libc] 8d64884 - [libc] Implement sigwait (#237572)
via libc-commits
libc-commits at lists.llvm.org
Wed Nov 11 00:11:12 PST 2026
Author: Anshitha Yeruva
Date: 2026-11-11T09:11:07+01:00
New Revision: 8d64884d5dd84e106b685d49b9dc0a25a8fb89b0
URL: https://github.com/llvm/llvm-project/commit/8d64884d5dd84e106b685d49b9dc0a25a8fb89b0
DIFF: https://github.com/llvm/llvm-project/commit/8d64884d5dd84e106b685d49b9dc0a25a8fb89b0.diff
LOG: [libc] Implement sigwait (#237572)
This patch implements the POSIX `sigwait` function in LLVM libc, along
with unit tests.
Part of #236167.
Added:
libc/src/signal/linux/sigwait.cpp
libc/src/signal/sigwait.h
libc/test/src/signal/sigwait_test.cpp
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/include/signal.yaml
libc/src/signal/CMakeLists.txt
libc/src/signal/linux/CMakeLists.txt
libc/test/src/signal/CMakeLists.txt
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 3c026f06836e59..a9941aab496cf4 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -466,7 +466,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.chown
libc.src.unistd.chroot
libc.src.unistd.close
- libc.src.unistd.close_range
+ libc.src.unistd.close_range
libc.src.unistd.copy_file_range
libc.src.unistd.dup
libc.src.unistd.dup2
@@ -1429,6 +1429,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.signal.sigprocmask
libc.src.signal.sigsuspend
libc.src.signal.sigtimedwait
+ libc.src.signal.sigwait
libc.src.signal.sigwaitinfo
# spawn.h entrypoints
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index b152fecff84e62..e79b1f0c8efb7c 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -512,7 +512,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.chown
libc.src.unistd.chroot
libc.src.unistd.close
- libc.src.unistd.close_range
+ libc.src.unistd.close_range
libc.src.unistd.copy_file_range
libc.src.unistd.dup
libc.src.unistd.dup2
@@ -1645,6 +1645,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.signal.sigprocmask
libc.src.signal.sigsuspend
libc.src.signal.sigtimedwait
+ libc.src.signal.sigwait
libc.src.signal.sigwaitinfo
# spawn.h entrypoints
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index be9b45cf21339d..57886863ece764 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -512,7 +512,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.chown
libc.src.unistd.chroot
libc.src.unistd.close
- libc.src.unistd.close_range
+ libc.src.unistd.close_range
libc.src.unistd.copy_file_range
libc.src.unistd.dup
libc.src.unistd.dup2
@@ -1649,6 +1649,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.signal.sigprocmask
libc.src.signal.sigsuspend
libc.src.signal.sigtimedwait
+ libc.src.signal.sigwait
libc.src.signal.sigwaitinfo
# spawn.h entrypoints
diff --git a/libc/include/signal.yaml b/libc/include/signal.yaml
index e172aff747d56a..8176bfd8092547 100644
--- a/libc/include/signal.yaml
+++ b/libc/include/signal.yaml
@@ -154,6 +154,13 @@ functions:
- type: const sigset_t *__restrict
- type: siginfo_t *__restrict
- type: const struct timespec *__restrict
+ - name: sigwait
+ standards:
+ - posix
+ return_type: int
+ arguments:
+ - type: const sigset_t *__restrict
+ - type: int *__restrict
- name: sigwaitinfo
standards:
- posix
diff --git a/libc/src/signal/CMakeLists.txt b/libc/src/signal/CMakeLists.txt
index 939f9fb86eb1bc..31979e04e72f23 100644
--- a/libc/src/signal/CMakeLists.txt
+++ b/libc/src/signal/CMakeLists.txt
@@ -65,6 +65,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.sigtimedwait
)
+add_entrypoint_object(
+ sigwait
+ ALIAS
+ DEPENDS
+ .${LIBC_TARGET_OS}.sigwait
+)
+
add_entrypoint_object(
sigwaitinfo
ALIAS
diff --git a/libc/src/signal/linux/CMakeLists.txt b/libc/src/signal/linux/CMakeLists.txt
index 400e7d3040173d..c5e3749b2420dd 100644
--- a/libc/src/signal/linux/CMakeLists.txt
+++ b/libc/src/signal/linux/CMakeLists.txt
@@ -152,6 +152,22 @@ add_entrypoint_object(
libc.src.errno.errno
)
+add_entrypoint_object(
+ sigwait
+ SRCS
+ sigwait.cpp
+ HDRS
+ ../sigwait.h
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.types.siginfo_t
+ libc.hdr.types.sigset_t
+ libc.src.__support.OSUtil.linux.syscall_wrappers.rt_sigtimedwait
+ libc.src.__support.common
+ libc.src.__support.macros.config
+ libc.src.__support.macros.null_check
+)
+
add_entrypoint_object(
sigwaitinfo
SRCS
diff --git a/libc/src/signal/linux/sigwait.cpp b/libc/src/signal/linux/sigwait.cpp
new file mode 100644
index 00000000000000..176ac5174fb344
--- /dev/null
+++ b/libc/src/signal/linux/sigwait.cpp
@@ -0,0 +1,43 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 sigwait.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/signal/sigwait.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/types/siginfo_t.h"
+#include "hdr/types/sigset_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/rt_sigtimedwait.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, sigwait,
+ (const sigset_t *__restrict set, int *__restrict sig)) {
+ LIBC_CRASH_ON_NULLPTR(sig);
+
+ siginfo_t info;
+ ErrorOr<int> result = 0;
+ do {
+ result = linux_syscalls::rt_sigtimedwait(set, &info, nullptr);
+ } while (!result.has_value() && result.error() == EINTR);
+
+ if (!result.has_value())
+ return result.error();
+
+ *sig = info.si_signo;
+ return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/signal/sigwait.h b/libc/src/signal/sigwait.h
new file mode 100644
index 00000000000000..677caead225c4e
--- /dev/null
+++ b/libc/src/signal/sigwait.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 sigwait function.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SIGNAL_SIGWAIT_H
+#define LLVM_LIBC_SRC_SIGNAL_SIGWAIT_H
+
+#include "hdr/types/sigset_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int sigwait(const sigset_t *__restrict set, int *__restrict sig);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_SIGNAL_SIGWAIT_H
diff --git a/libc/test/src/signal/CMakeLists.txt b/libc/test/src/signal/CMakeLists.txt
index cb94867ab42f5e..01d84603cf9526 100644
--- a/libc/test/src/signal/CMakeLists.txt
+++ b/libc/test/src/signal/CMakeLists.txt
@@ -132,6 +132,26 @@ add_libc_test(
libc.test.UnitTest.ErrnoSetterMatcher
)
+add_libc_test(
+ sigwait_test
+ SUITE
+ libc-signal-tests
+ SRCS
+ sigwait_test.cpp
+ DEPENDS
+ libc.hdr.errno_macros
+ libc.hdr.signal_macros
+ libc.hdr.sys_time_macros
+ libc.hdr.types.sigset_t
+ libc.hdr.types.struct_itimerval
+ libc.src.signal.raise
+ libc.src.signal.sigaddset
+ libc.src.signal.sigemptyset
+ libc.src.signal.sigprocmask
+ libc.src.signal.sigwait
+ libc.src.sys.time.setitimer
+)
+
add_libc_test(
sigwaitinfo_test
SUITE
diff --git a/libc/test/src/signal/sigwait_test.cpp b/libc/test/src/signal/sigwait_test.cpp
new file mode 100644
index 00000000000000..b5a5acda9a06e1
--- /dev/null
+++ b/libc/test/src/signal/sigwait_test.cpp
@@ -0,0 +1,82 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 sigwait.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/signal/sigwait.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/signal_macros.h"
+#include "hdr/sys_time_macros.h"
+#include "hdr/types/sigset_t.h"
+#include "hdr/types/struct_itimerval.h"
+#include "src/signal/raise.h"
+#include "src/signal/sigaddset.h"
+#include "src/signal/sigemptyset.h"
+#include "src/signal/sigprocmask.h"
+#include "src/sys/time/setitimer.h"
+#include "test/UnitTest/Test.h"
+
+TEST(LlvmLibcSigwaitTest, WaitPendingSignal) {
+ // Block SIGUSR1 so raising it leaves it pending without terminating.
+ sigset_t mask;
+ ASSERT_EQ(LIBC_NAMESPACE::sigemptyset(&mask), 0);
+ ASSERT_EQ(LIBC_NAMESPACE::sigaddset(&mask, SIGUSR1), 0);
+
+ sigset_t oldset;
+ ASSERT_EQ(LIBC_NAMESPACE::sigprocmask(SIG_BLOCK, &mask, &oldset), 0);
+
+ // Raise SIGUSR1. It is now queued/pending for this thread.
+ ASSERT_EQ(LIBC_NAMESPACE::raise(SIGUSR1), 0);
+
+ // sigwait should immediately catch and dequeue SIGUSR1.
+ int sig = 0;
+ ASSERT_EQ(LIBC_NAMESPACE::sigwait(&mask, &sig), 0);
+ ASSERT_EQ(sig, SIGUSR1);
+
+ // Restore the original signal mask.
+ ASSERT_EQ(LIBC_NAMESPACE::sigprocmask(SIG_SETMASK, &oldset, nullptr), 0);
+}
+
+TEST(LlvmLibcSigwaitTest, WaitForSignalDeliveredLater) {
+ // Block SIGALRM so that when setitimer fires, it is queued for this thread
+ // and does not terminate the process.
+ sigset_t mask;
+ ASSERT_EQ(LIBC_NAMESPACE::sigemptyset(&mask), 0);
+ ASSERT_EQ(LIBC_NAMESPACE::sigaddset(&mask, SIGALRM), 0);
+
+ sigset_t oldset;
+ ASSERT_EQ(LIBC_NAMESPACE::sigprocmask(SIG_BLOCK, &mask, &oldset), 0);
+
+ // Set a timer to deliver SIGALRM after 10 milliseconds.
+ struct itimerval timer = {};
+ timer.it_value.tv_sec = 0;
+ timer.it_value.tv_usec = 10000;
+
+ struct itimerval old_timer = {};
+ ASSERT_EQ(LIBC_NAMESPACE::setitimer(ITIMER_REAL, &timer, &old_timer), 0);
+
+ // SIGALRM is not yet pending at the time of this call.
+ // sigwait should block until the timer fires and delivers the signal.
+ int sig = 0;
+ ASSERT_EQ(LIBC_NAMESPACE::sigwait(&mask, &sig), 0);
+ ASSERT_EQ(sig, SIGALRM);
+
+ // Restore the original timer and signal mask.
+ LIBC_NAMESPACE::setitimer(ITIMER_REAL, &old_timer, nullptr);
+ ASSERT_EQ(LIBC_NAMESPACE::sigprocmask(SIG_SETMASK, &oldset, nullptr), 0);
+}
+
+TEST(LlvmLibcSigwaitTest, InvalidSetReturnsEfault) {
+ // We let the kernel verify the pointer value.
+ int sig = 0;
+ EXPECT_EQ(LIBC_NAMESPACE::sigwait(nullptr, &sig), EFAULT);
+}
More information about the libc-commits
mailing list