libcosmos
Linux C++ System Programming Library
Loading...
Searching...
No Matches
SignalFD.hxx
1#pragma once
2
3// Linux
4#include <sys/signalfd.h>
5
6// cosmos
7#include <cosmos/fs/FileDescriptor.hxx>
8#include <cosmos/proc/SigInfo.hxx>
9#include <cosmos/proc/SigSet.hxx>
10#include <cosmos/proc/types.hxx>
11
12namespace cosmos {
13
15
37class COSMOS_API SignalFD {
38public: // types
39
40 class Info;
41
42public: // functions
43
46
47 ~SignalFD();
48
50 explicit SignalFD(const SigSet &mask) {
51 create(mask);
52 }
53
55 explicit SignalFD(const std::initializer_list<Signal> siglist) {
56 create(SigSet{siglist});
57 }
58
60 explicit SignalFD(const Signal s) {
61 create(SigSet{{s}});
62 }
63
64 // Prevent copying due to the file descriptor ownership.
65 SignalFD(const SignalFD&) = delete;
66 SignalFD& operator=(const SignalFD&) = delete;
67
69
73 void create(const SigSet &mask);
74
75 void close() { m_fd.close(); }
76
77 auto valid() const { return m_fd.valid(); }
78
80
84 void adjustMask(const SigSet &mask);
85
87
95 void readEvent(Info &info);
96
98 auto fd() { return m_fd; }
99
100protected: // data
101
102 FileDescriptor m_fd;
103};
104
106
116class COSMOS_API SignalFD::Info {
117public: // types
118
119 /* reuse the sub-structures as is */
120
121 using Source = SigInfo::Source;
130
131public: // functions
132
135 clear();
136 }
137
139
144 Info(const no_init_t) {}
145
146 const signalfd_siginfo* raw() const {
147 return &m_raw;
148 }
149
150 signalfd_siginfo* raw() {
151 return &m_raw;
152 }
153
155 Signal sigNr() const {
156 // ssi_signo is unsigned, while in sigaction it's signed.
157 return Signal{SignalNr{static_cast<int>(m_raw.ssi_signo)}};
158 }
159
161
164 Source source() const;
165
167
170 bool isTrustedSource() const {
171 return m_raw.ssi_code >= 0;
172 }
173
175 std::optional<const UserSigData> userSigData() const;
176
178 std::optional<const QueueSigData> queueSigData() const;
179
181 std::optional<const MsgQueueData> msgQueueData() const;
182
184 std::optional<const TimerData> timerData() const;
185
187
190 std::optional<const SysData> sysData() const;
191
193
196 std::optional<const ChildData> childData() const;
197
199
202 std::optional<const PollData> pollData() const;
203
205 void clear() {
206 zero_object(m_raw);
207 }
208
209protected: // functions
210
212
215 Errno error() const {
216 return Errno{m_raw.ssi_errno};
217 }
218
219 ProcessCtx procCtx() const {
220 return ProcessCtx{pid(), uid()};
221 }
222
223 ProcessID pid() const {
224 // ssi_pid is unsigned while pid_t is signed
225 return ProcessID{static_cast<pid_t>(m_raw.ssi_pid)};
226 }
227
228 UserID uid() const {
229 return UserID{m_raw.ssi_uid};
230 }
231
232protected: // data
233
234 signalfd_siginfo m_raw;
235};
236
237} // end ns
Thin Wrapper around OS file descriptors.
Source
The source of a signal.
Definition SigInfo.hxx:83
A bit set of signal numbers for use in system calls.
Definition SigSet.hxx:25
SigInfo style data structure returned by SignalFD::readEvent().
Definition SignalFD.hxx:116
bool isTrustedSource() const
Returns whether the signal was sent from a trusted source (i.e. the kernel).
Definition SignalFD.hxx:170
Errno error() const
Returns an error code that is generally unused on Linux (always 0).
Definition SignalFD.hxx:215
Signal sigNr() const
Returns the signal number that occurred.
Definition SignalFD.hxx:155
void clear()
Zeroes out the low level siginfo_t data structure.
Definition SignalFD.hxx:205
Info(const no_init_t)
Leaves the underlying data structure uninitialized.
Definition SignalFD.hxx:144
Info()
Creates a zero-initialized Info wrapper.
Definition SignalFD.hxx:134
A file descriptor for receiving process signals.
Definition SignalFD.hxx:37
SignalFD(const SigSet &mask)
Creates a signal FD listening on the given signals.
Definition SignalFD.hxx:50
SignalFD()
Creates an invalid SignalFD object.
Definition SignalFD.hxx:45
SignalFD(const std::initializer_list< Signal > siglist)
Creates a signal FD listening on the given list of signals.
Definition SignalFD.hxx:55
auto fd()
Returns the FileDescriptor object associated with the SignalFD.
Definition SignalFD.hxx:98
SignalFD(const Signal s)
Creates a signal FD listening on exactly the given signal.
Definition SignalFD.hxx:60
Represents a POSIX signal number and offers a minimal API around it.
Definition types.hxx:96
Errno
Strong enum type representing errno error constants.
Definition errno.hxx:29
void zero_object(T &obj)
Completely overwrites the given object with zeroes.
Definition memory.hxx:23
SignalNr
A primitive signal number specification.
Definition types.hxx:50
Additional data found in SigInfo with SIGCHILD.
Definition SigInfo.hxx:321
Additional data found in SigInfo with Source::MESGQ.
Definition SigInfo.hxx:162
Additional data found in SigInfo with SIGPOLL.
Definition SigInfo.hxx:446
Information about the process a signal is from or about.
Definition SigInfo.hxx:105
Additional data found in SigInfo with Source::QUEUE.
Definition SigInfo.hxx:153
Additional data found in SigInfo delivered with SIGSYS.
Definition SigInfo.hxx:422
Additional data found in SigInfo with Source::TIMER.
Definition SigInfo.hxx:176
Additional data found in SigInfo with Source::USER.
Definition SigInfo.hxx:147
Type used to invoke constructors that explicitly don't zero-initialize low level data structures.
Definition types.hxx:37