Add a wrapper for signalfd

The wrapper blocks (and unblock) signals for the thread as well.
This will get tested by the wakeup for the IPC.

Change-Id: Ica701034e8890fc0689b8528b4d7448fd630ed64
diff --git a/aos/ipc_lib/BUILD b/aos/ipc_lib/BUILD
index 03a4752..85f6dcd 100644
--- a/aos/ipc_lib/BUILD
+++ b/aos/ipc_lib/BUILD
@@ -148,3 +148,16 @@
         "@com_github_gflags_gflags//:gflags",
     ],
 )
+
+cc_library(
+    name = "signalfd",
+    srcs = [
+        "signalfd.cc",
+    ],
+    hdrs = [
+        "signalfd.h",
+    ],
+    deps = [
+        "//aos/logging",
+    ],
+)
diff --git a/aos/ipc_lib/signalfd.cc b/aos/ipc_lib/signalfd.cc
new file mode 100644
index 0000000..1679b46
--- /dev/null
+++ b/aos/ipc_lib/signalfd.cc
@@ -0,0 +1,47 @@
+#include "aos/ipc_lib/signalfd.h"
+
+#include <signal.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <initializer_list>
+
+#include "aos/logging/logging.h"
+
+namespace aos {
+namespace ipc_lib {
+
+SignalFd::SignalFd(::std::initializer_list<int> signals) {
+  // Build up the mask with the provided signals.
+  sigemptyset(&mask_);
+  for (int signal : signals) {
+    sigaddset(&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));
+  // Now that we have a consumer of the signal, block the signals so the
+  // signalfd gets them.
+  pthread_sigmask(SIG_BLOCK, &mask_, nullptr);
+}
+
+SignalFd::~SignalFd() {
+  // Unwind the constructor.  Unblock the signals and close the fd.
+  pthread_sigmask(SIG_UNBLOCK, &mask_, nullptr);
+  PCHECK(close(fd_));
+}
+
+signalfd_siginfo SignalFd::Read() {
+  signalfd_siginfo result;
+
+  const int ret =
+      read(fd_, static_cast<void *>(&result), sizeof(signalfd_siginfo));
+  // If we didn't get the right amount of data, signal that there was a problem
+  // by setting the signal number to 0.
+  if (ret != static_cast<int>(sizeof(signalfd_siginfo))) {
+    result.ssi_signo = 0;
+  }
+  return result;
+}
+
+}  // namespace ipc_lib
+}  // namespace aos
diff --git a/aos/ipc_lib/signalfd.h b/aos/ipc_lib/signalfd.h
new file mode 100644
index 0000000..a545a80
--- /dev/null
+++ b/aos/ipc_lib/signalfd.h
@@ -0,0 +1,38 @@
+#ifndef AOS_IPC_LIB_SIGNALFD_H_
+#define AOS_IPC_LIB_SIGNALFD_H_
+
+#include <sys/signalfd.h>
+#include <sys/types.h>
+#include <initializer_list>
+
+namespace aos {
+namespace ipc_lib {
+
+// Class to manage a signalfd.
+class SignalFd {
+ public:
+  // Constructs a SignalFd for the provided list of signals.
+  // Blocks the signals at the same time in this thread.
+  SignalFd(::std::initializer_list<int> signals);
+
+  SignalFd(const SignalFd &) = delete;
+  SignalFd &operator=(const SignalFd &) = delete;
+  ~SignalFd();
+
+  // Returns the file descriptor for the signalfd.
+  int fd() { return fd_; }
+
+  // Reads a signalfd_siginfo.  If there was an error, the resulting ssi_signo
+  // will be 0.
+  signalfd_siginfo Read();
+
+ private:
+  int fd_ = -1;
+
+  sigset_t mask_;
+};
+
+}  // namespace ipc_lib
+}  // namespace aos
+
+#endif  // AOS_IPC_LIB_SIGNALFD_H_