blob: a6991de8d83631413daa24ab35dfa0f16ae0442a [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
4#include <sys/signalfd.h>
5#include <sys/types.h>
Austin Schuh60e77942022-05-16 17:48:24 -07006
Austin Schuh6c590f82019-09-11 19:23:12 -07007#include <initializer_list>
8
9namespace aos {
10namespace ipc_lib {
11
12// Class to manage a signalfd.
13class SignalFd {
14 public:
15 // Constructs a SignalFd for the provided list of signals.
16 // Blocks the signals at the same time in this thread.
Alex Perrycb7da4b2019-08-28 19:35:56 -070017 SignalFd(::std::initializer_list<unsigned int> signals);
Austin Schuh6c590f82019-09-11 19:23:12 -070018
19 SignalFd(const SignalFd &) = delete;
20 SignalFd &operator=(const SignalFd &) = delete;
21 ~SignalFd();
22
23 // Returns the file descriptor for the signalfd.
24 int fd() { return fd_; }
25
26 // Reads a signalfd_siginfo. If there was an error, the resulting ssi_signo
27 // will be 0.
28 signalfd_siginfo Read();
29
30 private:
31 int fd_ = -1;
32
Brian Silverman407cc532019-11-03 11:40:56 -080033 // The signals we blocked in the constructor.
34 sigset_t blocked_mask_;
Austin Schuh6c590f82019-09-11 19:23:12 -070035};
36
37} // namespace ipc_lib
38} // namespace aos
39
40#endif // AOS_IPC_LIB_SIGNALFD_H_