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