Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 1 | #include "aos/ipc_lib/signalfd.h" |
| 2 | |
| 3 | #include <signal.h> |
| 4 | #include <sys/types.h> |
Austin Schuh | 8d2e3fa | 2020-09-10 23:01:55 -0700 | [diff] [blame] | 5 | #if __has_feature(memory_sanitizer) |
| 6 | #include <sanitizer/msan_interface.h> |
| 7 | #endif |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 8 | #include <unistd.h> |
| 9 | #include <initializer_list> |
| 10 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 11 | #include "glog/logging.h" |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 12 | |
| 13 | namespace aos { |
| 14 | namespace ipc_lib { |
Austin Schuh | 8d2e3fa | 2020-09-10 23:01:55 -0700 | [diff] [blame] | 15 | namespace { |
| 16 | |
| 17 | // Wrapper which propagates msan information. |
| 18 | // TODO(Brian): Drop this once we have <https://reviews.llvm.org/D82411> to |
| 19 | // intercept this function natively. |
| 20 | int wrapped_sigandset(sigset_t *dest, const sigset_t *left, |
| 21 | const sigset_t *right) { |
| 22 | #if __has_feature(memory_sanitizer) |
| 23 | if (left) { |
| 24 | __msan_check_mem_is_initialized(left, sizeof(*left)); |
| 25 | } |
| 26 | if (right) { |
| 27 | __msan_check_mem_is_initialized(right, sizeof(*right)); |
| 28 | } |
| 29 | #endif |
| 30 | const int r = sigandset(dest, left, right); |
| 31 | #if __has_feature(memory_sanitizer) |
| 32 | if (!r && dest) { |
| 33 | __msan_unpoison(dest, sizeof(*dest)); |
| 34 | } |
| 35 | #endif |
| 36 | return r; |
| 37 | } |
| 38 | |
| 39 | // Wrapper which propagates msan information. |
| 40 | // TODO(Brian): Drop this once we have |
| 41 | // <https://reviews.llvm.org/rG89ae290b58e20fc5f56b7bfae4b34e7fef06e1b1> to |
| 42 | // intercept this function natively. |
| 43 | int wrapped_pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset) { |
| 44 | #if __has_feature(memory_sanitizer) |
| 45 | if (set) { |
| 46 | __msan_check_mem_is_initialized(set, sizeof(*set)); |
| 47 | } |
| 48 | #endif |
| 49 | const int r = pthread_sigmask(how, set, oldset); |
| 50 | #if __has_feature(memory_sanitizer) |
| 51 | if (!r && oldset) { |
| 52 | __msan_unpoison(oldset, sizeof(*oldset)); |
| 53 | } |
| 54 | #endif |
| 55 | return r; |
| 56 | } |
| 57 | |
| 58 | } // namespace |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 59 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 60 | SignalFd::SignalFd(::std::initializer_list<unsigned int> signals) { |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 61 | // Build up the mask with the provided signals. |
Brian Silverman | 407cc53 | 2019-11-03 11:40:56 -0800 | [diff] [blame] | 62 | CHECK_EQ(0, sigemptyset(&blocked_mask_)); |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 63 | for (int signal : signals) { |
Brian Silverman | 407cc53 | 2019-11-03 11:40:56 -0800 | [diff] [blame] | 64 | CHECK_EQ(0, sigaddset(&blocked_mask_, signal)); |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 65 | } |
| 66 | // Then build a signalfd. Make it nonblocking so it works well with an epoll |
| 67 | // loop, and have it close on exec. |
Brian Silverman | 407cc53 | 2019-11-03 11:40:56 -0800 | [diff] [blame] | 68 | PCHECK((fd_ = signalfd(-1, &blocked_mask_, SFD_NONBLOCK | SFD_CLOEXEC)) != 0); |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 69 | // Now that we have a consumer of the signal, block the signals so the |
Brian Silverman | 407cc53 | 2019-11-03 11:40:56 -0800 | [diff] [blame] | 70 | // signalfd gets them. Record which ones we actually blocked, so we can |
| 71 | // unblock just those later. |
| 72 | sigset_t old_mask; |
Austin Schuh | 8d2e3fa | 2020-09-10 23:01:55 -0700 | [diff] [blame] | 73 | CHECK_EQ(0, wrapped_pthread_sigmask(SIG_BLOCK, &blocked_mask_, &old_mask)); |
Brian Silverman | 407cc53 | 2019-11-03 11:40:56 -0800 | [diff] [blame] | 74 | for (int signal : signals) { |
| 75 | if (sigismember(&old_mask, signal)) { |
| 76 | CHECK_EQ(0, sigdelset(&blocked_mask_, signal)); |
| 77 | } |
| 78 | } |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | SignalFd::~SignalFd() { |
Brian Silverman | 407cc53 | 2019-11-03 11:40:56 -0800 | [diff] [blame] | 82 | // Unwind the constructor. Unblock the signals and close the fd. Verify nobody |
| 83 | // else unblocked the signals we're supposed to unblock in the meantime. |
| 84 | sigset_t old_mask; |
Austin Schuh | 8d2e3fa | 2020-09-10 23:01:55 -0700 | [diff] [blame] | 85 | CHECK_EQ(0, wrapped_pthread_sigmask(SIG_UNBLOCK, &blocked_mask_, &old_mask)); |
Brian Silverman | 407cc53 | 2019-11-03 11:40:56 -0800 | [diff] [blame] | 86 | sigset_t unblocked_mask; |
Austin Schuh | 8d2e3fa | 2020-09-10 23:01:55 -0700 | [diff] [blame] | 87 | CHECK_EQ(0, wrapped_sigandset(&unblocked_mask, &blocked_mask_, &old_mask)); |
Brian Silverman | 407cc53 | 2019-11-03 11:40:56 -0800 | [diff] [blame] | 88 | if (memcmp(&unblocked_mask, &blocked_mask_, sizeof(unblocked_mask)) != 0) { |
| 89 | LOG(FATAL) << "Some other code unblocked one or more of our signals"; |
| 90 | } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 91 | PCHECK(close(fd_) == 0); |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | signalfd_siginfo SignalFd::Read() { |
| 95 | signalfd_siginfo result; |
| 96 | |
| 97 | const int ret = |
| 98 | read(fd_, static_cast<void *>(&result), sizeof(signalfd_siginfo)); |
| 99 | // If we didn't get the right amount of data, signal that there was a problem |
| 100 | // by setting the signal number to 0. |
| 101 | if (ret != static_cast<int>(sizeof(signalfd_siginfo))) { |
| 102 | result.ssi_signo = 0; |
Brian Silverman | 407cc53 | 2019-11-03 11:40:56 -0800 | [diff] [blame] | 103 | } else { |
| 104 | CHECK_NE(0u, result.ssi_signo); |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 105 | } |
| 106 | return result; |
| 107 | } |
| 108 | |
| 109 | } // namespace ipc_lib |
| 110 | } // namespace aos |