Make signalfd wrapper more robust

I realized afterwards it's only used in tests, but whatever.

Change-Id: I23f654a0119faefec6a3888dc7d7c8ba38cf1b4f
diff --git a/aos/ipc_lib/signalfd.cc b/aos/ipc_lib/signalfd.cc
index af95598..363bf4a 100644
--- a/aos/ipc_lib/signalfd.cc
+++ b/aos/ipc_lib/signalfd.cc
@@ -12,21 +12,35 @@
 
 SignalFd::SignalFd(::std::initializer_list<unsigned int> signals) {
   // Build up the mask with the provided signals.
-  sigemptyset(&mask_);
+  CHECK_EQ(0, sigemptyset(&blocked_mask_));
   for (int signal : signals) {
-    sigaddset(&mask_, signal);
+    CHECK_EQ(0, sigaddset(&blocked_mask_, signal));
   }
   // Then build a signalfd.  Make it nonblocking so it works well with an epoll
   // loop, and have it close on exec.
-  PCHECK((fd_ = signalfd(-1, &mask_, SFD_NONBLOCK | SFD_CLOEXEC)) != 0);
+  PCHECK((fd_ = signalfd(-1, &blocked_mask_, SFD_NONBLOCK | SFD_CLOEXEC)) != 0);
   // Now that we have a consumer of the signal, block the signals so the
-  // signalfd gets them.
-  pthread_sigmask(SIG_BLOCK, &mask_, nullptr);
+  // signalfd gets them. Record which ones we actually blocked, so we can
+  // unblock just those later.
+  sigset_t old_mask;
+  CHECK_EQ(0, pthread_sigmask(SIG_BLOCK, &blocked_mask_, &old_mask));
+  for (int signal : signals) {
+    if (sigismember(&old_mask, signal)) {
+      CHECK_EQ(0, sigdelset(&blocked_mask_, signal));
+    }
+  }
 }
 
 SignalFd::~SignalFd() {
-  // Unwind the constructor.  Unblock the signals and close the fd.
-  pthread_sigmask(SIG_UNBLOCK, &mask_, nullptr);
+  // Unwind the constructor. Unblock the signals and close the fd. Verify nobody
+  // else unblocked the signals we're supposed to unblock in the meantime.
+  sigset_t old_mask;
+  CHECK_EQ(0, pthread_sigmask(SIG_UNBLOCK, &blocked_mask_, &old_mask));
+  sigset_t unblocked_mask;
+  CHECK_EQ(0, sigandset(&unblocked_mask, &blocked_mask_, &old_mask));
+  if (memcmp(&unblocked_mask, &blocked_mask_, sizeof(unblocked_mask)) != 0) {
+    LOG(FATAL) << "Some other code unblocked one or more of our signals";
+  }
   PCHECK(close(fd_) == 0);
 }
 
@@ -39,6 +53,8 @@
   // by setting the signal number to 0.
   if (ret != static_cast<int>(sizeof(signalfd_siginfo))) {
     result.ssi_signo = 0;
+  } else {
+    CHECK_NE(0u, result.ssi_signo);
   }
   return result;
 }