blob: 3afdf9978a4b83d04b8f3c05b26c1e650c724b9b [file] [log] [blame]
Austin Schuh6c590f82019-09-11 19:23:12 -07001#ifndef AOS_IPC_LIB_SIGNALFD_H_
2#define AOS_IPC_LIB_SIGNALFD_H_
3
Stephan Pleines682928d2024-05-31 20:43:48 -07004#include <signal.h>
Austin Schuh6c590f82019-09-11 19:23:12 -07005#include <sys/signalfd.h>
Austin Schuh60e77942022-05-16 17:48:24 -07006
Austin Schuh6c590f82019-09-11 19:23:12 -07007#include <initializer_list>
8
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -08009namespace aos::ipc_lib {
Austin Schuh6c590f82019-09-11 19:23:12 -070010
11// Class to manage a signalfd.
12class 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 Perrycb7da4b2019-08-28 19:35:56 -070016 SignalFd(::std::initializer_list<unsigned int> signals);
Austin Schuh6c590f82019-09-11 19:23:12 -070017
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 Silverman36975282021-07-29 12:06:55 -070029 // 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 Schuh6c590f82019-09-11 19:23:12 -070034 private:
35 int fd_ = -1;
36
Brian Silverman407cc532019-11-03 11:40:56 -080037 // The signals we blocked in the constructor.
38 sigset_t blocked_mask_;
Austin Schuh6c590f82019-09-11 19:23:12 -070039};
40
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080041} // namespace aos::ipc_lib
Austin Schuh6c590f82019-09-11 19:23:12 -070042
43#endif // AOS_IPC_LIB_SIGNALFD_H_