The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
signal.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * signal numbers. they are ABI: the same numbers are in the libc's
5 * <signal.h>, and a process killed by a signal reports its number in its
6 * wait status (wait_status.h).
7 */
8
9#define SIGHUP 1
10#define SIGINT 2
11#define SIGQUIT 3
12#define SIGILL 4
13#define SIGTRAP 5
14#define SIGABRT 6
15#define SIGBUS 7
16#define SIGFPE 8
17#define SIGKILL 9
18#define SIGUSR1 10
19#define SIGSEGV 11
20#define SIGUSR2 12
21#define SIGPIPE 13
22#define SIGALRM 14
23#define SIGTERM 15
24#define SIGCHLD 17
25#define SIGCONT 18
26#define SIGSTOP 19
27#define SIGTSTP 20
28#define SIGTTIN 21
29#define SIGTTOU 22
30#define SIGURG 23 // urgent data on a socket (ignored by default)
31#define SIGVTALRM 26 // a virtual timer ran out (no such timers yet)
32#define SIGPROF 27 // a profiling timer ran out (no such timers yet)
33#define SIGWINCH 28 // the terminal's window size changed (ignored by default)
34
35// signals are numbered 1 to NSIG - 1
36#define NSIG 32
37
38#include "common/stdint.h"
39
40// the bit of a signal in a mask
41#define SIG_BIT(sig) (1u << (sig))
42
43// signals that cannot be caught, blocked or ignored
44#define SIG_UNBLOCKABLE (SIG_BIT(SIGKILL) | SIG_BIT(SIGSTOP))
45
46// handler values with a meaning of their own
47#define SIG_DFL 0 // the default action
48#define SIG_IGN 1 // ignore
49
50// sigaction flags (ABI, as in libc signal.h)
51#define SA_NOCLDSTOP 0x00000001 // no SIGCHLD when a child stops or continues
52#define SA_RESTORER 0x04000000 // restorer holds the return trampoline
53#define SA_RESTART 0x10000000 // restart an interrupted syscall
54#define SA_NODEFER 0x40000000 // do not block the signal in its handler
55#define SA_RESETHAND 0x80000000 // back to SIG_DFL once the handler runs
56
57// sigprocmask how values
58#define SIG_BLOCK 0
59#define SIG_UNBLOCK 1
60#define SIG_SETMASK 2
61
62/**
63 * what a process does with one signal (ABI: the sigaction syscall copies
64 * it to and from user space in this layout).
65 */
66typedef struct
67{
68 uintptr_t handler; // SIG_DFL, SIG_IGN or a user function
69 uint32_t flags; // SA_*
70 uintptr_t restorer; // user code that calls sigreturn
71 uint32_t mask; // signals blocked while the handler runs
73
74/**
75 * the signal state of a process (inside process_t).
76 */
77typedef struct
78{
79 uint32_t pending; // signals sent and not yet acted on
80 uint32_t blocked; // signals held back
81 uint32_t saved_blocked; // the mask to restore after sigsuspend
82 bool restore_blocked; // saved_blocked is set
84
85/**
86 * what a process does with each signal. the threads of a process use one
87 * together; it is freed when the last of them lets it go.
88 */
89typedef struct signal_handlers
90{
91 uint32_t users; // the processes (threads) that use it
94
95/**
96 * make a set of handlers, all SIG_DFL, with one user.
97 * @return the set, or NULL if there is no memory.
98 */
100
101/**
102 * add a user to a set of handlers (a new thread that shares it).
103 * @return handlers.
104 */
106
107/**
108 * remove a user from a set of handlers; the last user frees it.
109 * @param handlers the set, or NULL (nothing is done).
110 */
112
113struct process;
114struct registers;
115
116/**
117 * send a signal to one thread. a signal the process ignores is dropped; a
118 * deliverable one wakes the process from an interruptible sleep. SIGCONT
119 * continues a stopped process, SIGKILL wakes one. safe in any context.
120 * @param proc the process.
121 * @param sig the signal (1 to NSIG - 1).
122 */
123void signal_send(struct process *proc, int sig);
124
125/**
126 * send a signal to a process (a thread group): one of its threads takes
127 * it (signal_send), one that does not block it if there is one. SIGCONT
128 * goes to every thread.
129 * @param proc any thread of the process, or NULL (nothing is done).
130 * @param sig the signal.
131 */
132void signal_send_process(struct process *proc, int sig);
133
134/**
135 * send a signal to every process of a process group.
136 * @param pgid the group.
137 * @param sig the signal.
138 * @return the number of processes it went to.
139 */
140int signal_send_group(uint32_t pgid, int sig);
141
142/**
143 * send a signal caused by the process's own instruction (a fault). it is
144 * delivered even if blocked or ignored: blocked or ignored, it kills.
145 * @param proc the process.
146 * @param sig the signal.
147 */
148void signal_force(struct process *proc, int sig);
149
150/**
151 * true if a signal waits that the process does not block. a sleeping
152 * syscall that sees this gives up its wait (EINTR or a restart).
153 * @param proc the process (NULL: false).
154 */
155bool signal_pending(const struct process *proc);
156
157/**
158 * act on pending signals before returning to user mode: run the default
159 * action, or set up the user handler's frame. also turns an interrupted
160 * syscall's -ERESTARTSYS into EINTR or a restart. called at the end of
161 * every interrupt and syscall that came from user mode.
162 * @param regs the user registers about to be restored.
163 */
164void signal_deliver(struct registers *regs);
165
166/**
167 * the signal state of a forked child: the parent's mask, no pending
168 * signals. the child's handlers (its own set) are copied from the parent's.
169 */
170void signal_fork(struct process *child, const struct process *parent);
171
172/**
173 * the signal state after execve: handlers go back to SIG_DFL (ignored
174 * signals stay ignored); the mask and pending signals stay.
175 */
176void signal_exec(struct process *proc);
177
178/**
179 * take the lock of the signal state of all processes, with interrupts
180 * masked. it also guards the exit and exec state of each thread group
181 * (group_exiting, group_status, exec_thread) and the waitpid reports, as
182 * the signal_struct lock does in Linux. it nests outside the process
183 * table lock and sched_lock.
184 * @return the interrupt state for signal_state_unlock().
185 */
187
188/**
189 * release the lock that signal_state_lock() took.
190 * @param flags the value from signal_state_lock().
191 */
193
194/**
195 * set up signal handling. call once at boot.
196 */
197void signal_init(void);
198
199/**
200 * send SIGALRM to processes whose alarm is due. called from the timer
201 * interrupt.
202 * @param now the current tick.
203 */
uint8_t flags
Definition idt.c:3
struct signal_handlers signal_handlers_t
what a process does with each signal.
#define NSIG
Definition signal.h:36
unsigned int uint32_t
Definition fb.h:17
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:19
bool signal_pending(const process_t *proc)
Definition signal.c:240
void signal_send_process(process_t *proc, int sig)
send a signal to a process (a thread group): one of its threads takes it (signal_send),...
Definition signal.c:604
void signal_force(process_t *proc, int sig)
send a signal caused by the process's own instruction (a fault).
Definition signal.c:224
void signal_handlers_put(signal_handlers_t *handlers)
remove a user from a set of handlers; the last user frees it.
Definition signal.c:528
void signal_deliver(struct registers *regs)
act on pending signals before returning to user mode: run the default action, or set up the user hand...
Definition signal.c:409
void signal_send(process_t *proc, int sig)
send a signal to one thread.
Definition signal.c:217
void signal_exec(process_t *proc)
the signal state after execve: handlers go back to SIG_DFL (ignored signals stay ignored); the mask a...
Definition signal.c:540
uint32_t signal_state_lock(void)
take the lock of the signal state of all processes, with interrupts masked.
Definition signal.c:912
int signal_send_group(uint32_t pgid, int sig)
send a signal to every process of a process group.
Definition signal.c:593
void signal_state_unlock(uint32_t flags)
release the lock that signal_state_lock() took.
Definition signal.c:917
void signal_init(void)
set up signal handling.
Definition signal.c:922
signal_handlers_t * signal_handlers_create(void)
make a set of handlers, all SIG_DFL, with one user.
Definition signal.c:512
void signal_alarm_tick(uint64_t now)
send SIGALRM to processes whose alarm is due.
Definition signal.c:579
void signal_fork(process_t *child, const process_t *parent)
Definition signal.c:503
signal_handlers_t * signal_handlers_get(signal_handlers_t *handlers)
add a user to a set of handlers (a new thread that shares it).
Definition signal.c:522
what a process does with one signal (ABI: the sigaction syscall copies it to and from user space in t...
Definition signal.h:67
uintptr_t restorer
Definition signal.h:70
uint32_t mask
Definition signal.h:71
uintptr_t handler
Definition signal.h:68
uint32_t flags
Definition signal.h:69
process control block.
Definition process.h:69
saved register state pushed onto the stack by ISR stubs.
Definition isr.h:41
what a process does with each signal.
Definition signal.h:90
uint32_t users
Definition signal.h:91
ksigaction_t actions[NSIG]
Definition signal.h:92
the signal state of a process (inside process_t).
Definition signal.h:78
uint32_t saved_blocked
Definition signal.h:81
uint32_t pending
Definition signal.h:79
uint32_t blocked
Definition signal.h:80
bool restore_blocked
Definition signal.h:82