libcosmos
Linux C++ System Programming Library
All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SigInfo.hxx
Go to the documentation of this file.
1#pragma once
2
3// C++
4#include <optional>
5
6// Linux
7// this contains SYS_SECCOMP but the header has conflicts with user space
8// headers for some time already, there seems to be no way around it:
9// https://bugzilla.kernel.org/show_bug.cgi?id=200081
10// We define the constant ourselves further below.
11//#include <linux/signal.h>
12
13// cosmos
14#include <cosmos/error/errno.hxx>
15#include <cosmos/fs/types.hxx>
16#include <cosmos/io/types.hxx>
17#include <cosmos/memory.hxx>
18#include <cosmos/proc/ptrace.hxx>
19#include <cosmos/proc/signal.hxx>
20#include <cosmos/proc/types.hxx>
21#include <cosmos/time/types.hxx>
22#include <cosmos/utils.hxx>
23
24#ifndef SYS_SECCOMP
25# define SYS_SECCOMP 1
26#endif
27
66namespace cosmos {
67
69
79class COSMOS_API SigInfo {
80public: // types
81
83 enum class Source : int {
84 USER = SI_USER,
85 KERNEL = SI_KERNEL,
86 QUEUE = SI_QUEUE,
87 TIMER = SI_TIMER,
88 MESGQ = SI_MESGQ,
89 ASYNCIO = SI_ASYNCIO,
90 QSIGIO = SI_SIGIO,
91 TKILL = SI_TKILL,
92 };
93
95
107 UserID uid;
108 };
109
111
116 struct CustomData {
117 public: // functions
118
119 explicit CustomData(union sigval val) : m_val{val} {}
120
122
126 int asInt() const {
127 return m_val.sival_int;
128 }
129
131
137 void* asPtr() const {
138 return m_val.sival_ptr;
139 }
140
141 protected: // data
142
143 union sigval m_val;
144 };
145
151
160
169
170 /*
171 * TODO: this contains CustomData which is actually the ID returned from timer_create().
172 * We need to model types for `timer_create()` first to make use of
173 * that.
174 */
176 struct TimerData {
177 public: // types
178
179 enum class TimerID : int {};
180
181 public: // functions
182
184
189 TimerID id;
190
192
197 };
198
200
209 struct FaultData {
210
212 void *addr;
213 };
214
216 struct IllData : public FaultData {
217 public: // types
218
220 enum class Reason : int {
221 OPCODE = ILL_ILLOPC,
222 OPERAND = ILL_ILLOPN,
223 ADDRESS = ILL_ILLADR,
224 TRAP = ILL_ILLTRP,
225 PRIV_OP = ILL_PRVOPC,
226 PRIV_REG = ILL_PRVREG,
227 COPROC = ILL_COPROC,
228 BAD_STACK = ILL_BADSTK,
229 BAD_IADDR = ILL_BADIADDR,
230 };
231
232 public: // data
233
236 };
237
239 struct FPEData : public FaultData {
240 public: // types
241
243 enum class Reason : int {
244 INT_DIV_ZERO = FPE_INTDIV,
245 INT_OVERFLOW = FPE_INTOVF,
246 FLOAT_DIV_ZERO = FPE_FLTDIV,
247 FLOAT_OVERFLOW = FPE_FLTOVF,
248 FLOAT_UNDERFLOW = FPE_FLTUND,
249 FLOAT_INEXACT = FPE_FLTRES,
250 FLOAT_INVALID = FPE_FLTINV,
251 FLOAT_SUB_RANGE = FPE_FLTSUB,
252 FLOAT_UNKNOWN = FPE_FLTUNK,
253 FLOAT_CONDTRAP = FPE_CONDTRAP,
254 };
255
256 public: // data
257
260 };
261
263 struct SegfaultData : public FaultData {
264 public: // types
265
267 enum class Reason : int {
268 MAP_ERROR = SEGV_MAPERR,
269 ACCESS_ERROR = SEGV_ACCERR,
270 BOUND_ERROR = SEGV_BNDERR,
271 PROT_KEY_ERROR = SEGV_PKUERR,
272 ACCESS_ADI = SEGV_ACCADI,
273 MCD_DISRUPT = SEGV_ADIDERR,
274 PRECISE_MCD = SEGV_ADIPERR,
275 ASYNC_MTE = SEGV_MTEAERR,
276 SYNC_MTE = SEGV_MTESERR,
277 CPROT_ERROR = SEGV_CPERR,
278 };
279
280 struct Bound {
281 void *lower = nullptr;
282 void *upper = nullptr;
283 };
284
285 // TODO: this is preliminary strong type that should be moved into a pkey() API wrapper.
286 enum class ProtectionKey : unsigned int {
287 };
288
289 public: // data
290
294 std::optional<Bound> bound;
296 std::optional<ProtectionKey> key;
297 };
298
300 struct BusData : public FaultData {
301 public: // types
302
304 enum class Reason : int {
305 ALIGNMENT = BUS_ADRALN,
306 NOT_EXISTING = BUS_ADRERR,
307 OBJECT_ERROR = BUS_OBJERR,
308 MCE_ACTION_REQUIRED = BUS_MCEERR_AR,
309 MCE_ACTION_OPTIONAL = BUS_MCEERR_AO,
310 };
311
312 public: // data
313
317 std::optional<short> addr_lsb;
318 };
319
321 struct ChildData {
322 public: // types
323
325 enum class Event : int {
326 INVALID = -1,
327 EXITED = CLD_EXITED,
328 KILLED = CLD_KILLED,
329 DUMPED = CLD_DUMPED,
330 TRAPPED = CLD_TRAPPED,
331 STOPPED = CLD_STOPPED,
332 CONTINUED = CLD_CONTINUED
333 };
334
335 public: // functions
336
338 bool exited() const { return event == Event::EXITED; }
339
341 bool killed() const { return event == Event::KILLED; }
342
344 bool dumped() const { return event == Event::DUMPED; }
345
347 bool trapped() const { return event == Event::TRAPPED; }
348
350 bool continued() const { return event == Event::CONTINUED; }
351
353 bool stopped() const { return event == Event::STOPPED; }
354
356 bool exitedSuccessfully() const {
357 return exited() && *status == ExitStatus::SUCCESS;
358 }
359
361 bool signaled() const {
362 return event == Event::KILLED ||
363 event == Event::DUMPED ||
364 event == Event::STOPPED ||
365 event == Event::CONTINUED;
366 }
367
369 bool valid() const {
370 return event != Event::INVALID;
371 }
372
373 void reset() {
374 event = Event::INVALID;
375 child.pid = ProcessID::INVALID;
376 status = std::nullopt;
377 signal = std::nullopt;
378 }
379
380 public: // data
381
386
388
392 std::optional<ExitStatus> status;
393
395
399 std::optional<Signal> signal;
400
402
409 std::optional<ClockTicks> user_time;
410
412
418 std::optional<ClockTicks> system_time;
419 };
420
422 struct SysData {
423 public: // types
424
426 enum class Reason : int {
427 // TODO: check what happens for non-seccomp SIGSYS
428 SECCOMP = SYS_SECCOMP,
429 };
430
431 public: // data
432
443 };
444
446 struct PollData {
447 public: // types
448
450 enum class Reason : int {
451 INPUT = POLL_IN,
452 OUTPUT = POLL_OUT,
453 MESSAGE = POLL_MSG,
454 ERROR = POLL_ERR,
455 PRIORITY = POLL_PRI,
456 HANGUP = POLL_HUP,
457 };
458
459 public: // data
460
461 Reason reason;
466 };
467
468public: // functions
469
472 clear();
473 }
474
476
482
484 Signal sigNr() const {
485 return Signal{SignalNr{m_raw.si_signo}};
486 }
487
489
507 Source source() const;
508
510
519 bool isTrustedSource() const {
520 return m_raw.si_code >= 0;
521 }
522
524 bool isFaultSignal() const;
525
527 std::optional<const UserSigData> userSigData() const;
528
530 std::optional<const QueueSigData> queueSigData() const;
531
533 std::optional<const MsgQueueData> msgQueueData() const;
534
536 std::optional<const TimerData> timerData() const;
537
539
542 std::optional<const SysData> sysData() const;
543
545
548 std::optional<const ChildData> childData() const;
549
551
554 std::optional<const PollData> pollData() const;
555
557
560 std::optional<const IllData> illData() const;
561
563
566 std::optional<const FPEData> fpeData() const;
567
569
572 std::optional<const SegfaultData> segfaultData() const;
573
575
578 std::optional<const BusData> busData() const;
579
581 void clear() {
582 zero_object(m_raw);
583 }
584
585 const siginfo_t* raw() const {
586 return &m_raw;
587 }
588
589 siginfo_t* raw() {
590 return &m_raw;
591 }
592
593protected: // functions
594
596
599 Errno error() const {
600 return Errno{m_raw.si_errno};
601 }
602
603 ProcessCtx procCtx() const {
604 return ProcessCtx{pid(), uid()};
605 }
606
607 ProcessID pid() const {
608 return ProcessID{m_raw.si_pid};
609 }
610
611 UserID uid() const {
612 return UserID{m_raw.si_uid};
613 }
614
615protected: // data
616
617 siginfo_t m_raw;
618};
619
620} // end ns
Signal information struct used when receiving signals.
Definition SigInfo.hxx:79
bool isTrustedSource() const
Returns whether the signal was sent from a trusted source (i.e. the kernel).
Definition SigInfo.hxx:519
Source
The source of a signal.
Definition SigInfo.hxx:83
Signal sigNr() const
Returns the signal number that occurred.
Definition SigInfo.hxx:484
Errno error() const
Returns an error code that is generally unused on Linux (always 0).
Definition SigInfo.hxx:599
SigInfo(const no_init_t)
Leaves the underlying data structure uninitialized.
Definition SigInfo.hxx:481
void clear()
Zeroes out the low level siginfo_t data structure.
Definition SigInfo.hxx:581
SigInfo()
Creates a zero-initialized SigInfo wrapper.
Definition SigInfo.hxx:471
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
@ TIMER
time expired
FileNum
Primitive file descriptor.
Definition types.hxx:32
@ OUTPUT
writing is possible.
@ INPUT
there is data to read.
@ ERROR
an error condition exists (also occurs on the write end of a pipe, when the read end has been closed)...
@ PRIORITY
an exceptional condition exists: out-of-band data on TCP socket, PTY master has seen a state change o...
void zero_object(T &obj)
Completely overwrites the given object with zeroes.
Definition memory.hxx:23
ProcessID
Definition types.hxx:25
SignalNr
A primitive signal number specification.
Definition types.hxx:50
Arch
System call ABI architecture.
Definition ptrace.hxx:174
Additional data delivered with SIGBUS signals.
Definition SigInfo.hxx:300
Reason reason
The reason why SIGBUS was delivered.
Definition SigInfo.hxx:315
Reason
Different reasons for delivering a SIGBUS signal.
Definition SigInfo.hxx:304
std::optional< short > addr_lsb
For Reason::MCE_ACTION_REQUIRED and Reason::MCE_ACTION_OPTIONAL this contains the least significant b...
Definition SigInfo.hxx:317
Additional data found in SigInfo with SIGCHILD.
Definition SigInfo.hxx:321
bool dumped() const
Returns whether the child dumped core due to a signal.
Definition SigInfo.hxx:344
bool continued() const
Returns whether the child continued due to a signal.
Definition SigInfo.hxx:350
std::optional< ExitStatus > status
Contains the process's exit status, if applicable.
Definition SigInfo.hxx:392
bool valid() const
Returns whether the structure contains valid information.
Definition SigInfo.hxx:369
std::optional< Signal > signal
Contains the signal number that caused the child process to change state.
Definition SigInfo.hxx:399
bool exited() const
Returns whether the child exited.
Definition SigInfo.hxx:338
bool stopped() const
Returns whether the child stopped.
Definition SigInfo.hxx:353
bool killed() const
Returns whether the child was killed by a signal.
Definition SigInfo.hxx:341
Event event
The kind of child process event that occurred.
Definition SigInfo.hxx:383
std::optional< ClockTicks > system_time
The CPU time the child spent in kernel space.
Definition SigInfo.hxx:418
bool signaled() const
Returns whether the child received a signal.
Definition SigInfo.hxx:361
bool exitedSuccessfully() const
Returns whether the child exited and had an exit status of 0.
Definition SigInfo.hxx:356
ProcessCtx child
the PID and its real user ID the signal is about.
Definition SigInfo.hxx:385
Event
Types of SIGCHLD events that can occur.
Definition SigInfo.hxx:325
bool trapped() const
Returns true if the child entered a tracing trap.
Definition SigInfo.hxx:347
std::optional< ClockTicks > user_time
The CPU time the child spent in user space.
Definition SigInfo.hxx:409
Additional custom SigInfo data.
Definition SigInfo.hxx:116
int asInt() const
Returns custom data sent with the signal.
Definition SigInfo.hxx:126
void * asPtr() const
Returns custom data sent with the signal.
Definition SigInfo.hxx:137
Extra data delivered with SIGFPE signals.
Definition SigInfo.hxx:239
Reason
Different reasons for delivering floating-point exceptions.
Definition SigInfo.hxx:243
Reason reason
The reason why SIGFPE was delivered.
Definition SigInfo.hxx:259
Additional data found in SigInfo for one of the memory fault / trap signals.
Definition SigInfo.hxx:209
void * addr
The address of the fault / trap.
Definition SigInfo.hxx:212
Additional data delivered with SIGILL signals.
Definition SigInfo.hxx:216
Reason reason
The reason why SIGILL was delivered.
Definition SigInfo.hxx:235
Reason
Different reasons for delivering a SIGILL signal.
Definition SigInfo.hxx:220
Additional data found in SigInfo with Source::MESGQ.
Definition SigInfo.hxx:162
ProcessCtx msg_sender
The PID and real user ID of the process that sent a message queue message.
Definition SigInfo.hxx:164
CustomData data
Custom data supplied via mq_notify().
Definition SigInfo.hxx:167
Additional data found in SigInfo with SIGPOLL.
Definition SigInfo.hxx:446
Reason
Different reasons for delivering SIGPOLL.
Definition SigInfo.hxx:450
PollEvents events
The I/O events that occurred for fd.
Definition SigInfo.hxx:465
FileNum fd
The file descriptor for which the event occurred.
Definition SigInfo.hxx:463
Information about the process a signal is from or about.
Definition SigInfo.hxx:105
ProcessID pid
PID of the process.
Definition SigInfo.hxx:106
UserID uid
real user ID of the process
Definition SigInfo.hxx:107
Additional data found in SigInfo with Source::QUEUE.
Definition SigInfo.hxx:153
ProcessCtx sender
The PID and real user ID of the sending process.
Definition SigInfo.hxx:155
CustomData data
Custom data supplied along with the signal.
Definition SigInfo.hxx:158
Additional data delivered with SIGSEGV signals.
Definition SigInfo.hxx:263
Reason
Different reasons for delivering a SIGSEGV signal.
Definition SigInfo.hxx:267
Reason reason
The reason why SIGSEGV was delivered.
Definition SigInfo.hxx:292
std::optional< Bound > bound
For Reason::BOUND_ERROR this contains the lower and upper bound.
Definition SigInfo.hxx:294
std::optional< ProtectionKey > key
For Reason::PROT_KEY_ERROR this contains the protection key that caused the fault.
Definition SigInfo.hxx:296
Additional data found in SigInfo delivered with SIGSYS.
Definition SigInfo.hxx:422
Reason reason
Why SIGSYS was delivered.
Definition SigInfo.hxx:434
Reason
Different reasons for delivering SIGYS.
Definition SigInfo.hxx:426
void * call_addr
The calling user space instruction.
Definition SigInfo.hxx:436
Errno error
The SECCOMP_RET_DATA portion or Errno::SUCCESS if seccomp is not involved.
Definition SigInfo.hxx:442
int call_nr
The system call number.
Definition SigInfo.hxx:438
ptrace::Arch arch
The system call ABI.
Definition SigInfo.hxx:440
Additional data found in SigInfo with Source::TIMER.
Definition SigInfo.hxx:176
int overrun
The timer overrun count.
Definition SigInfo.hxx:196
TimerID id
The ID of the timer which expired.
Definition SigInfo.hxx:189
Additional data found in SigInfo with Source::USER.
Definition SigInfo.hxx:147
ProcessCtx sender
The PID and real user ID of the sending process.
Definition SigInfo.hxx:149
Type used to invoke constructors that explicitly don't zero-initialize low level data structures.
Definition types.hxx:37