Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 1 | #ifndef AOS_IPC_LIB_SIGNALFD_H_ |
| 2 | #define AOS_IPC_LIB_SIGNALFD_H_ |
| 3 | |
Stephan Pleines | 682928d | 2024-05-31 20:43:48 -0700 | [diff] [blame] | 4 | #include <signal.h> |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 5 | #include <sys/signalfd.h> |
Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 6 | |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 7 | #include <initializer_list> |
| 8 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 9 | namespace aos::ipc_lib { |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 10 | |
| 11 | // Class to manage a signalfd. |
| 12 | class SignalFd { |
| 13 | public: |
| 14 | // Constructs a SignalFd for the provided list of signals. |
| 15 | // Blocks the signals at the same time in this thread. |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 16 | SignalFd(::std::initializer_list<unsigned int> signals); |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 17 | |
| 18 | SignalFd(const SignalFd &) = delete; |
| 19 | SignalFd &operator=(const SignalFd &) = delete; |
| 20 | ~SignalFd(); |
| 21 | |
| 22 | // Returns the file descriptor for the signalfd. |
| 23 | int fd() { return fd_; } |
| 24 | |
| 25 | // Reads a signalfd_siginfo. If there was an error, the resulting ssi_signo |
| 26 | // will be 0. |
| 27 | signalfd_siginfo Read(); |
| 28 | |
Brian Silverman | 3697528 | 2021-07-29 12:06:55 -0700 | [diff] [blame] | 29 | // Ensures the destructor will leave the specific signal blocked. This can be |
| 30 | // helpful if the signal is sent asynchronously, such that it may arrive after |
| 31 | // this object is destroyed, to ensure that doesn't kill the process. |
| 32 | void LeaveSignalBlocked(unsigned int signal); |
| 33 | |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 34 | private: |
| 35 | int fd_ = -1; |
| 36 | |
Brian Silverman | 407cc53 | 2019-11-03 11:40:56 -0800 | [diff] [blame] | 37 | // The signals we blocked in the constructor. |
| 38 | sigset_t blocked_mask_; |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 41 | } // namespace aos::ipc_lib |
Austin Schuh | 6c590f8 | 2019-09-11 19:23:12 -0700 | [diff] [blame] | 42 | |
| 43 | #endif // AOS_IPC_LIB_SIGNALFD_H_ |