blob: 9f3c97267c4ce37335041c78e369c0fdb1e2dcc2 [file] [log] [blame]
Austin Schuh6c590f82019-09-11 19:23:12 -07001#include "aos/ipc_lib/signalfd.h"
2
Stephan Pleines682928d2024-05-31 20:43:48 -07003#include <string.h>
Austin Schuh6c590f82019-09-11 19:23:12 -07004#include <sys/types.h>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07005
6#include <csignal>
Stephan Pleines682928d2024-05-31 20:43:48 -07007#include <ostream>
Austin Schuh8d2e3fa2020-09-10 23:01:55 -07008#if __has_feature(memory_sanitizer)
9#include <sanitizer/msan_interface.h>
10#endif
Austin Schuh6c590f82019-09-11 19:23:12 -070011#include <unistd.h>
Tyler Chatow85c32c92021-07-31 11:58:51 -070012
Austin Schuh6c590f82019-09-11 19:23:12 -070013#include <initializer_list>
14
Austin Schuh99f7c6a2024-06-25 22:07:44 -070015#include "absl/log/check.h"
16#include "absl/log/log.h"
Austin Schuh6c590f82019-09-11 19:23:12 -070017
Stephan Pleinesf63bde82024-01-13 15:59:33 -080018namespace aos::ipc_lib {
Austin Schuh8d2e3fa2020-09-10 23:01:55 -070019namespace {
20
21// Wrapper which propagates msan information.
22// TODO(Brian): Drop this once we have <https://reviews.llvm.org/D82411> to
23// intercept this function natively.
24int wrapped_sigandset(sigset_t *dest, const sigset_t *left,
25 const sigset_t *right) {
26#if __has_feature(memory_sanitizer)
27 if (left) {
28 __msan_check_mem_is_initialized(left, sizeof(*left));
29 }
30 if (right) {
31 __msan_check_mem_is_initialized(right, sizeof(*right));
32 }
33#endif
34 const int r = sigandset(dest, left, right);
35#if __has_feature(memory_sanitizer)
36 if (!r && dest) {
37 __msan_unpoison(dest, sizeof(*dest));
38 }
39#endif
40 return r;
41}
42
43// Wrapper which propagates msan information.
44// TODO(Brian): Drop this once we have
45// <https://reviews.llvm.org/rG89ae290b58e20fc5f56b7bfae4b34e7fef06e1b1> to
46// intercept this function natively.
47int wrapped_pthread_sigmask(int how, const sigset_t *set, sigset_t *oldset) {
48#if __has_feature(memory_sanitizer)
49 if (set) {
50 __msan_check_mem_is_initialized(set, sizeof(*set));
51 }
52#endif
53 const int r = pthread_sigmask(how, set, oldset);
54#if __has_feature(memory_sanitizer)
55 if (!r && oldset) {
56 __msan_unpoison(oldset, sizeof(*oldset));
57 }
58#endif
59 return r;
60}
61
62} // namespace
Austin Schuh6c590f82019-09-11 19:23:12 -070063
Alex Perrycb7da4b2019-08-28 19:35:56 -070064SignalFd::SignalFd(::std::initializer_list<unsigned int> signals) {
Austin Schuh6c590f82019-09-11 19:23:12 -070065 // Build up the mask with the provided signals.
Brian Silverman407cc532019-11-03 11:40:56 -080066 CHECK_EQ(0, sigemptyset(&blocked_mask_));
Austin Schuh6c590f82019-09-11 19:23:12 -070067 for (int signal : signals) {
Brian Silverman407cc532019-11-03 11:40:56 -080068 CHECK_EQ(0, sigaddset(&blocked_mask_, signal));
Austin Schuh6c590f82019-09-11 19:23:12 -070069 }
70 // Then build a signalfd. Make it nonblocking so it works well with an epoll
71 // loop, and have it close on exec.
Brian Silverman407cc532019-11-03 11:40:56 -080072 PCHECK((fd_ = signalfd(-1, &blocked_mask_, SFD_NONBLOCK | SFD_CLOEXEC)) != 0);
Austin Schuh6c590f82019-09-11 19:23:12 -070073 // Now that we have a consumer of the signal, block the signals so the
Brian Silverman407cc532019-11-03 11:40:56 -080074 // signalfd gets them. Record which ones we actually blocked, so we can
75 // unblock just those later.
76 sigset_t old_mask;
Austin Schuh8d2e3fa2020-09-10 23:01:55 -070077 CHECK_EQ(0, wrapped_pthread_sigmask(SIG_BLOCK, &blocked_mask_, &old_mask));
Brian Silverman407cc532019-11-03 11:40:56 -080078 for (int signal : signals) {
79 if (sigismember(&old_mask, signal)) {
Brian Silverman36975282021-07-29 12:06:55 -070080 LeaveSignalBlocked(signal);
Brian Silverman407cc532019-11-03 11:40:56 -080081 }
82 }
Austin Schuh6c590f82019-09-11 19:23:12 -070083}
84
Tyler Chatow85c32c92021-07-31 11:58:51 -070085namespace {
86// sizeof(sigset_t) is larger than the bytes actually used to represent all
87// signals. This size is only the bytes initialized. _NSIG is 1-indexed.
88static constexpr size_t kSigSetSize = (_NSIG - 1) / 8;
89
90// If the size of the mask changes, we should check that we still have
91// correct behavior.
92static_assert(kSigSetSize == 8 && kSigSetSize <= sizeof(sigset_t));
93} // namespace
94
Austin Schuh6c590f82019-09-11 19:23:12 -070095SignalFd::~SignalFd() {
Brian Silverman407cc532019-11-03 11:40:56 -080096 // Unwind the constructor. Unblock the signals and close the fd. Verify nobody
97 // else unblocked the signals we're supposed to unblock in the meantime.
98 sigset_t old_mask;
Austin Schuh8d2e3fa2020-09-10 23:01:55 -070099 CHECK_EQ(0, wrapped_pthread_sigmask(SIG_UNBLOCK, &blocked_mask_, &old_mask));
Brian Silverman407cc532019-11-03 11:40:56 -0800100 sigset_t unblocked_mask;
Austin Schuh8d2e3fa2020-09-10 23:01:55 -0700101 CHECK_EQ(0, wrapped_sigandset(&unblocked_mask, &blocked_mask_, &old_mask));
Tyler Chatow85c32c92021-07-31 11:58:51 -0700102 if (memcmp(&unblocked_mask, &blocked_mask_, kSigSetSize) != 0) {
Brian Silverman407cc532019-11-03 11:40:56 -0800103 LOG(FATAL) << "Some other code unblocked one or more of our signals";
104 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700105 PCHECK(close(fd_) == 0);
Austin Schuh6c590f82019-09-11 19:23:12 -0700106}
107
108signalfd_siginfo SignalFd::Read() {
109 signalfd_siginfo result;
110
111 const int ret =
112 read(fd_, static_cast<void *>(&result), sizeof(signalfd_siginfo));
113 // If we didn't get the right amount of data, signal that there was a problem
114 // by setting the signal number to 0.
115 if (ret != static_cast<int>(sizeof(signalfd_siginfo))) {
116 result.ssi_signo = 0;
Brian Silverman407cc532019-11-03 11:40:56 -0800117 } else {
118 CHECK_NE(0u, result.ssi_signo);
Austin Schuh6c590f82019-09-11 19:23:12 -0700119 }
120 return result;
121}
122
Brian Silverman36975282021-07-29 12:06:55 -0700123void SignalFd::LeaveSignalBlocked(unsigned int signal) {
124 CHECK_EQ(0, sigdelset(&blocked_mask_, signal));
125}
126
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800127} // namespace aos::ipc_lib