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