signal: Add idempotent block function
This makes it trivial to block individual signals from being handled by
a thread. Useful when tryng to set up signal handling in event loops.
Tested:
Builds and passes unit tests.
Change-Id: I61739debe2a47ec0ec3e767cf138125c6f59165f
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/test/signal.cpp b/test/signal.cpp
new file mode 100644
index 0000000..6389cd8
--- /dev/null
+++ b/test/signal.cpp
@@ -0,0 +1,62 @@
+#include <cstring>
+#include <gtest/gtest.h>
+#include <signal.h>
+#include <stdplus/signal.hpp>
+
+namespace stdplus
+{
+namespace signal
+{
+namespace
+{
+
+TEST(SignalTest, BlockSignal)
+{
+ constexpr int s = SIGINT;
+ constexpr int otherS = SIGTERM;
+ constexpr int notBlocked = SIGPROF;
+
+ sigset_t expectedSet;
+ EXPECT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &expectedSet));
+ EXPECT_EQ(0, sigaddset(&expectedSet, otherS));
+ EXPECT_EQ(0, sigprocmask(SIG_BLOCK, &expectedSet, nullptr));
+ EXPECT_EQ(0, sigismember(&expectedSet, notBlocked));
+ EXPECT_EQ(0, sigismember(&expectedSet, s));
+ EXPECT_EQ(0, sigaddset(&expectedSet, s));
+
+ block(s);
+
+ sigset_t newSet;
+ EXPECT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &newSet));
+ EXPECT_EQ(sigismember(&expectedSet, s), sigismember(&newSet, s));
+ EXPECT_EQ(sigismember(&expectedSet, otherS), sigismember(&newSet, otherS));
+ EXPECT_EQ(sigismember(&expectedSet, notBlocked),
+ sigismember(&newSet, notBlocked));
+}
+
+TEST(SignalTest, KeepBlockSignal)
+{
+ constexpr int s = SIGINT;
+ constexpr int otherS = SIGTERM;
+ constexpr int notBlocked = SIGPROF;
+
+ sigset_t expectedSet;
+ EXPECT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &expectedSet));
+ EXPECT_EQ(0, sigaddset(&expectedSet, s));
+ EXPECT_EQ(0, sigaddset(&expectedSet, otherS));
+ EXPECT_EQ(0, sigismember(&expectedSet, notBlocked));
+ EXPECT_EQ(0, sigprocmask(SIG_BLOCK, &expectedSet, nullptr));
+
+ block(s);
+
+ sigset_t newSet;
+ EXPECT_EQ(0, sigprocmask(SIG_BLOCK, nullptr, &newSet));
+ EXPECT_EQ(sigismember(&expectedSet, s), sigismember(&newSet, s));
+ EXPECT_EQ(sigismember(&expectedSet, otherS), sigismember(&newSet, otherS));
+ EXPECT_EQ(sigismember(&expectedSet, notBlocked),
+ sigismember(&newSet, notBlocked));
+}
+
+} // namespace
+} // namespace signal
+} // namespace stdplus