libcosmos
Linux C++ System Programming Library
Loading...
Searching...
No Matches
signal.hxx
Go to the documentation of this file.
1#pragma once
2
9// C++
10#include <variant>
11
12// cosmos
13#include <cosmos/BitMask.hxx>
14#include <cosmos/memory.hxx>
15#include <cosmos/proc/PidFD.hxx>
16#include <cosmos/proc/types.hxx>
17#include <cosmos/thread/thread.hxx>
18#include <cosmos/time/types.hxx>
19#include <cosmos/utils.hxx>
20
21namespace cosmos {
22 class SigSet;
23 class SigInfo;
24 class SigAction;
25}
26
27namespace cosmos::signal {
28
29/* constants for all well known signal numbers */
30
31constexpr Signal NONE = Signal{SignalNr::NONE};
32constexpr Signal HANGUP = Signal{SignalNr::HANGUP};
33constexpr Signal INTERRUPT = Signal{SignalNr::INTERRUPT};
34constexpr Signal QUIT = Signal{SignalNr::QUIT};
35constexpr Signal ILL = Signal{SignalNr::ILL};
36constexpr Signal TRAP = Signal{SignalNr::TRAP};
37constexpr Signal SYS_TRAP = Signal{SignalNr::SYS_TRAP};
38constexpr Signal ABORT = Signal{SignalNr::ABORT};
39constexpr Signal IOT = Signal{SignalNr::IOT};
40constexpr Signal BUS = Signal{SignalNr::BUS};
41constexpr Signal FPE = Signal{SignalNr::FPE};
42constexpr Signal KILL = Signal{SignalNr::KILL};
43constexpr Signal USR1 = Signal{SignalNr::USR1};
44constexpr Signal SEGV = Signal{SignalNr::SEGV};
45constexpr Signal USR2 = Signal{SignalNr::USR2};
46constexpr Signal PIPE = Signal{SignalNr::PIPE};
47constexpr Signal ALARM = Signal{SignalNr::ALARM};
48constexpr Signal TERMINATE = Signal{SignalNr::TERMINATE};
49constexpr Signal STACK_FAULT = Signal{SignalNr::STACK_FAULT};
50constexpr Signal CHILD = Signal{SignalNr::CHILD};
51constexpr Signal CONT = Signal{SignalNr::CONT};
52constexpr Signal STOP = Signal{SignalNr::STOP};
53constexpr Signal TERM_STOP = Signal{SignalNr::TERM_STOP};
54constexpr Signal TERM_INPUT = Signal{SignalNr::TERM_INPUT};
55constexpr Signal TERM_OUTPUT = Signal{SignalNr::TERM_OUTPUT};
56constexpr Signal URGENT = Signal{SignalNr::URGENT};
57constexpr Signal CPU_EXCEEDED = Signal{SignalNr::CPU_EXCEEDED};
58constexpr Signal FS_EXCEEDED = Signal{SignalNr::FS_EXCEEDED};
59constexpr Signal VIRTUAL_ALARM = Signal{SignalNr::VIRTUAL_ALARM};
60constexpr Signal PROFILING = Signal{SignalNr::PROFILING};
61constexpr Signal WIN_CHANGED = Signal{SignalNr::WIN_CHANGED};
62constexpr Signal IO_EVENT = Signal{SignalNr::IO_EVENT};
63constexpr Signal POLL = Signal{SignalNr::POLL};
64constexpr Signal POWER = Signal{SignalNr::POWER};
65constexpr Signal BAD_SYS = Signal{SignalNr::BAD_SYS};
66constexpr Signal MAXIMUM = Signal{SignalNr::MAXIMUM};
67
69
75inline Signal rt_min() {
76 return Signal{SignalNr{SIGRTMIN}};
77}
78
80inline Signal rt_max() {
81 return Signal{SignalNr{SIGRTMAX}};
82}
83
85inline size_t num_rt_sigs() {
86 return to_integral(rt_max().raw()) - to_integral(rt_min().raw());
87}
88
90
96COSMOS_API void raise(const Signal s);
97
99
102COSMOS_API void send(const ProcessID proc, const Signal s);
103
105
115COSMOS_API void send(const ProcessID proc, const Signal s, std::variant<void*, int> data);
116
118
125COSMOS_API void send(const PidFD pidfd, const Signal s);
126
128
134COSMOS_API void send(const ProcessID proc, const ThreadID thread, const Signal s);
135
137
142COSMOS_API void pause();
143
145
150COSMOS_API void suspend(const SigSet &mask);
151
153
166COSMOS_API Signal wait(const SigSet &set);
167
169
176COSMOS_API void wait_info(const SigSet &set, SigInfo &info);
177
179enum class WaitRes {
180 SIGNALED,
181 NO_RESULT
182};
183
185
197COSMOS_API WaitRes timed_wait(const SigSet &set, SigInfo &info, const IntervalTime timeout);
198
200
208inline WaitRes poll_info(const SigSet &set, SigInfo &info) {
209 return timed_wait(set, info, IntervalTime{0});
210}
211
213
260COSMOS_API void set_action(const Signal sig, const SigAction &action, SigAction *old = nullptr);
261
263
274COSMOS_API void get_action(const Signal sig, SigAction &action);
275
277
287COSMOS_API void block(const SigSet &s, SigSet *old = nullptr);
288
290COSMOS_API void unblock(const SigSet &s, SigSet *old = nullptr);
291
293COSMOS_API void set_sigmask(const SigSet &s, SigSet *old = nullptr);
294
296COSMOS_API void get_sigmask(SigSet &old);
297
299inline void restore(const Signal sig) {
300 ::signal(to_integral(sig.raw()), SIG_DFL);
301}
302
304
309inline void ignore(const Signal sig) {
310 ::signal(to_integral(sig.raw()), SIG_IGN);
311}
312
314class COSMOS_API Stack {
315public: // types
316
318 enum class Flag : int {
319 ON_STACK = SS_ONSTACK,
320 DISABLE = SS_DISABLE,
321 /* SS_AUTODISARM not yet in userspace headers? */
322 AUTO_DISARM = 1 << 31,
323 };
324
325 using Flags = BitMask<Flag>;
326
328 static size_t MIN_SIZE;
329
330public: // functions
331
332 Stack() {
333 clear();
334 }
335
337 Stack (const no_init_t) {}
338
339 void setFlags(const Flags flags) {
340 m_raw.ss_flags = flags.raw();
341 }
342
343 Flags getFlags() const {
344 return Flags{m_raw.ss_flags};
345 }
346
348 void setBase(void *base) {
349 m_raw.ss_sp = base;
350 }
351
352 void* getBase() const {
353 return m_raw.ss_sp;
354 }
355
357 void setSize(const size_t size) {
358 m_raw.ss_size = size;
359 }
360
361 size_t getSize() const {
362 return m_raw.ss_size;
363 }
364
365 void clear() {
366 zero_object(m_raw);
367 }
368
369 auto raw() {
370 return &m_raw;
371 }
372
373 auto raw() const {
374 return &m_raw;
375 }
376
377protected: // data
378
379 stack_t m_raw;
380};
381
383
400COSMOS_API void set_altstack(const Stack &stack, Stack *old = nullptr);
401
403
407COSMOS_API void get_altstack(Stack &old);
408
409} // end ns
A typesafe bit mask representation using class enums.
Definition BitMask.hxx:19
A specialized FileDescriptor for pidfds.
Definition PidFD.hxx:36
Data type used with signal::set_action() for controlling asynchronous signal delivery.
Definition SigAction.hxx:28
Signal information struct used when receiving signals.
Definition SigInfo.hxx:79
A bit set of signal numbers for use in system calls.
Definition SigSet.hxx:25
Represents a POSIX signal number and offers a minimal API around it.
Definition types.hxx:96
SignalNr raw() const
Returns the primitive signal number stored in this object.
Definition types.hxx:115
A C++ wrapper around the POSIX struct timespec coupled to a specific CLOCK type.
Definition types.hxx:57
Data structure used for defining an alternate signal stack.
Definition signal.hxx:314
void setBase(void *base)
Sets the base pointer for the alternate signal stack.
Definition signal.hxx:348
static size_t MIN_SIZE
Minimum size an alternate signal stack needs to have.
Definition signal.hxx:328
void setSize(const size_t size)
Sets the size of the alternate signal stack found at getBase().
Definition signal.hxx:357
Flag
Settings for alternate stack setup.
Definition signal.hxx:318
Stack(const no_init_t)
Leaves underlying data uninitialized.
Definition signal.hxx:337
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
void restore(const Signal sig)
Restores the default signal handling behaviour for the given signal.
Definition signal.hxx:299
size_t num_rt_sigs()
Returns the number of available real-time signals.
Definition signal.hxx:85
WaitRes
Strong type to express timed_wait() and poll_info() results.
Definition signal.hxx:179
@ SIGNALED
a signal has been caught.
@ NO_RESULT
no signal was caught / timeout occurred.
Signal rt_min()
Returns the first real-time signal available.
Definition signal.hxx:75
void ignore(const Signal sig)
Ignore signal delivery for the given signal.
Definition signal.hxx:309
WaitRes poll_info(const SigSet &set, SigInfo &info)
Check for a pending signal from set and fill in info with signal details.
Definition signal.hxx:208
Signal rt_max()
Returns the last real-time signal available.
Definition signal.hxx:80
Type used to invoke constructors that explicitly don't zero-initialize low level data structures.
Definition types.hxx:37