]> git.ipfire.org Git - thirdparty/glibc.git/blob - hurd/hurd/signal.h
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / hurd / hurd / signal.h
1 /* Implementing POSIX.1 signals under the Hurd.
2 Copyright (C) 1993-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #ifndef _HURD_SIGNAL_H
20
21 #define _HURD_SIGNAL_H 1
22 #include <features.h>
23
24 #define __need_size_t
25 #define __need_NULL
26 #include <stddef.h>
27
28 #include <mach/mach_types.h>
29 #include <mach/port.h>
30 #include <mach/message.h>
31 #include <hurd/hurd_types.h>
32 #include <signal.h>
33 #include <errno.h>
34 #include <bits/types/error_t.h>
35 #include <bits/types/stack_t.h>
36 #include <bits/types/sigset_t.h>
37 #include <bits/sigaction.h>
38 #include <hurd/msg.h>
39
40 #include <cthreads.h> /* For `struct mutex'. */
41 #include <setjmp.h> /* For `jmp_buf'. */
42 #include <spin-lock.h>
43 struct hurd_signal_preemptor; /* <hurd/sigpreempt.h> */
44 #if defined __USE_EXTERN_INLINES && defined _LIBC
45 # if IS_IN (libc) || IS_IN (libpthread)
46 # include <sigsetops.h>
47 # endif
48 #endif
49
50
51 /* Full details of a signal. */
52 struct hurd_signal_detail
53 {
54 /* Codes from origination Mach exception_raise message. */
55 integer_t exc, exc_code, exc_subcode;
56 /* Sigcode as passed or computed from exception codes. */
57 integer_t code;
58 /* Error code as passed or extracted from exception codes. */
59 error_t error;
60 };
61
62
63 /* Per-thread signal state. */
64
65 struct hurd_sigstate
66 {
67 spin_lock_t critical_section_lock; /* Held if in critical section. */
68
69 spin_lock_t lock; /* Locks most of the rest of the structure. */
70
71 thread_t thread;
72 struct hurd_sigstate *next; /* Linked-list of thread sigstates. */
73
74 sigset_t blocked; /* What signals are blocked. */
75 sigset_t pending; /* Pending signals, possibly blocked. */
76
77 /* Signal handlers. ACTIONS[0] is used to mark the threads with POSIX
78 semantics: if sa_handler is SIG_IGN instead of SIG_DFL, this thread
79 will receive global signals and use the process-wide action vector
80 instead of this one. */
81 struct sigaction actions[_NSIG];
82
83 stack_t sigaltstack;
84
85 /* Chain of thread-local signal preemptors; see <hurd/sigpreempt.h>.
86 Each element of this chain is in local stack storage, and the chain
87 parallels the stack: the head of this chain is in the innermost
88 stack frame, and each next element in an outermore frame. */
89 struct hurd_signal_preemptor *preemptors;
90
91 /* For each signal that may be pending, the details to deliver it with. */
92 struct hurd_signal_detail pending_data[_NSIG];
93
94 /* If `suspended' is set when this thread gets a signal,
95 the signal thread sends an empty message to it. */
96 mach_port_t suspended;
97
98 /* The following members are not locked. They are used only by this
99 thread, or by the signal thread with this thread suspended. */
100
101 volatile mach_port_t intr_port; /* Port interruptible RPC was sent on. */
102
103 /* If this is not null, the thread is in sigreturn awaiting delivery of
104 pending signals. This context (the machine-dependent portions only)
105 will be passed to sigreturn after running the handler for a pending
106 signal, instead of examining the thread state. */
107 struct sigcontext *context;
108
109 /* This is the head of the thread's list of active resources; see
110 <hurd/userlink.h> for details. This member is only used by the
111 thread itself, and always inside a critical section. */
112 struct hurd_userlink *active_resources;
113
114 /* These are locked normally. */
115 int cancel; /* Flag set by hurd_thread_cancel. */
116 void (*cancel_hook) (void); /* Called on cancellation. */
117 };
118
119 /* Linked list of states of all threads whose state has been asked for. */
120
121 extern struct hurd_sigstate *_hurd_sigstates;
122
123 extern struct mutex _hurd_siglock; /* Locks _hurd_sigstates. */
124
125 /* Get the sigstate of a given thread, taking its lock. */
126
127 extern struct hurd_sigstate *_hurd_thread_sigstate (thread_t);
128
129 /* Get the sigstate of the current thread.
130 This uses a per-thread variable to optimize the lookup. */
131
132 extern struct hurd_sigstate *_hurd_self_sigstate (void)
133 /* This declaration tells the compiler that the value is constant.
134 We assume this won't be called twice from the same stack frame
135 by different threads. */
136 __attribute__ ((__const__));
137
138 /* Process-wide signal state. */
139
140 extern struct hurd_sigstate *_hurd_global_sigstate;
141
142 /* Mark the given thread as a process-wide signal receiver. */
143
144 extern void _hurd_sigstate_set_global_rcv (struct hurd_sigstate *ss);
145
146 /* A thread can either use its own action vector and pending signal set
147 or use the global ones, depending on wether it has been marked as a
148 global receiver. The accessors below take that into account. */
149
150 extern void _hurd_sigstate_lock (struct hurd_sigstate *ss);
151 extern struct sigaction *_hurd_sigstate_actions (struct hurd_sigstate *ss);
152 extern sigset_t _hurd_sigstate_pending (const struct hurd_sigstate *ss);
153 extern void _hurd_sigstate_unlock (struct hurd_sigstate *ss);
154
155 /* Used by libpthread to remove stale sigstate structures. */
156 extern void _hurd_sigstate_delete (thread_t thread);
157
158 #ifndef _HURD_SIGNAL_H_EXTERN_INLINE
159 #define _HURD_SIGNAL_H_EXTERN_INLINE __extern_inline
160 #endif
161
162 #if defined __USE_EXTERN_INLINES && defined _LIBC
163 # if IS_IN (libc)
164 _HURD_SIGNAL_H_EXTERN_INLINE struct hurd_sigstate *
165 _hurd_self_sigstate (void)
166 {
167 if (THREAD_SELF->_hurd_sigstate == NULL)
168 THREAD_SELF->_hurd_sigstate = _hurd_thread_sigstate (__mach_thread_self ());
169 return THREAD_SELF->_hurd_sigstate;
170 }
171 # endif
172 #endif
173 \f
174 /* Thread listening on our message port; also called the "signal thread". */
175
176 extern thread_t _hurd_msgport_thread;
177
178 /* Our message port. We hold the receive right and _hurd_msgport_thread
179 listens for messages on it. We also hold a send right, for convenience. */
180
181 extern mach_port_t _hurd_msgport;
182
183 /* Resource limit on core file size. Enforced by hurdsig.c. */
184 extern int _hurd_core_limit;
185 \f
186 /* Critical sections.
187
188 A critical section is a section of code which cannot safely be interrupted
189 to run a signal handler; for example, code that holds any lock cannot be
190 interrupted lest the signal handler try to take the same lock and
191 deadlock result.
192
193 As a consequence, a critical section will see its RPCs return EINTR, even if
194 SA_RESTART is set! In that case, the critical section should be left, so
195 that the handler can run, and the whole critical section be tried again, to
196 avoid unexpectingly exposing EINTR to the application. */
197
198 extern void *_hurd_critical_section_lock (void);
199
200 #if defined __USE_EXTERN_INLINES && defined _LIBC
201 # if IS_IN (libc)
202 _HURD_SIGNAL_H_EXTERN_INLINE void *
203 _hurd_critical_section_lock (void)
204 {
205 struct hurd_sigstate *ss;
206
207 #ifdef __LIBC_NO_TLS
208 if (__LIBC_NO_TLS ())
209 /* TLS is currently initializing, no need to enter critical section. */
210 return NULL;
211 #endif
212
213 ss = THREAD_SELF->_hurd_sigstate;
214 if (ss == NULL)
215 {
216 /* The thread variable is unset; this must be the first time we've
217 asked for it. In this case, the critical section flag cannot
218 possible already be set. Look up our sigstate structure the slow
219 way. */
220 ss = THREAD_SELF->_hurd_sigstate = _hurd_thread_sigstate (__mach_thread_self ());
221 }
222
223 if (! __spin_try_lock (&ss->critical_section_lock))
224 /* We are already in a critical section, so do nothing. */
225 return NULL;
226
227 /* With the critical section lock held no signal handler will run.
228 Return our sigstate pointer; this will be passed to
229 _hurd_critical_section_unlock to unlock it. */
230 return ss;
231 }
232 # endif
233 #endif
234
235 extern void _hurd_critical_section_unlock (void *our_lock);
236
237 #if defined __USE_EXTERN_INLINES && defined _LIBC
238 # if IS_IN (libc)
239 _HURD_SIGNAL_H_EXTERN_INLINE void
240 _hurd_critical_section_unlock (void *our_lock)
241 {
242 if (our_lock == NULL)
243 /* The critical section lock was held when we began. Do nothing. */
244 return;
245 else
246 {
247 /* It was us who acquired the critical section lock. Unlock it. */
248 struct hurd_sigstate *ss = (struct hurd_sigstate *) our_lock;
249 sigset_t pending;
250 _hurd_sigstate_lock (ss);
251 __spin_unlock (&ss->critical_section_lock);
252 pending = _hurd_sigstate_pending(ss) & ~ss->blocked;
253 _hurd_sigstate_unlock (ss);
254 if (! __sigisemptyset (&pending))
255 /* There are unblocked signals pending, which weren't
256 delivered because we were in the critical section.
257 Tell the signal thread to deliver them now. */
258 __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
259 }
260 }
261 # endif
262 #endif
263
264 /* Convenient macros for simple uses of critical sections.
265 These two must be used as a pair at the same C scoping level. */
266
267 #define HURD_CRITICAL_BEGIN \
268 { void *__hurd_critical__ = _hurd_critical_section_lock ()
269 #define HURD_CRITICAL_END \
270 _hurd_critical_section_unlock (__hurd_critical__); } while (0)
271 \f
272 /* Initialize the signal code, and start the signal thread.
273 Arguments give the "init ints" from exec_startup. */
274
275 extern void _hurdsig_init (const int *intarray, size_t intarraysize);
276
277 /* Initialize proc server-assisted fault recovery for the signal thread. */
278
279 extern void _hurdsig_fault_init (void);
280
281 /* Raise a signal as described by SIGNO an DETAIL, on the thread whose
282 sigstate SS points to. If SS is a null pointer, this instead affects
283 the calling thread. */
284
285 extern int _hurd_raise_signal (struct hurd_sigstate *ss, int signo,
286 const struct hurd_signal_detail *detail);
287
288 /* Translate a Mach exception into a signal (machine-dependent). */
289
290 extern void _hurd_exception2signal (struct hurd_signal_detail *detail,
291 int *signo);
292
293
294 /* Make the thread described by SS take the signal described by SIGNO and
295 DETAIL. If the process is traced, this will in fact stop with a SIGNO
296 as the stop signal unless UNTRACED is nonzero. When the signal can be
297 considered delivered, sends a sig_post reply message on REPLY_PORT
298 indicating success. SS is not locked. */
299
300 extern void _hurd_internal_post_signal (struct hurd_sigstate *ss,
301 int signo,
302 struct hurd_signal_detail *detail,
303 mach_port_t reply_port,
304 mach_msg_type_name_t reply_port_type,
305 int untraced);
306
307 /* Set up STATE and SS to handle signal SIGNO by running HANDLER. If
308 RPC_WAIT is nonzero, the thread needs to wait for a pending RPC to
309 finish before running the signal handler. The handler is passed SIGNO,
310 SIGCODE, and the returned `struct sigcontext' (which resides on the
311 stack the handler will use, and which describes the state of the thread
312 encoded in STATE before running the handler). */
313
314 struct machine_thread_all_state;
315 extern struct sigcontext *
316 _hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
317 int signo, struct hurd_signal_detail *detail,
318 int rpc_wait, struct machine_thread_all_state *state);
319
320 /* Function run by the signal thread to receive from the signal port. */
321
322 extern void _hurd_msgport_receive (void);
323
324 /* Set up STATE with a thread state that, when resumed, is
325 like `longjmp (_hurd_sigthread_fault_env, 1)'. */
326
327 extern void _hurd_initialize_fault_recovery_state (void *state);
328
329 /* Set up STATE to do the equivalent of `longjmp (ENV, VAL);'. */
330
331 extern void _hurd_longjmp_thread_state (void *state, jmp_buf env, int value);
332
333 /* Function run for SIGINFO when its action is SIG_DFL and the current
334 process is the session leader. */
335
336 extern void _hurd_siginfo_handler (int);
337
338 /* Replacement for mach_msg used in RPCs to provide Hurd interruption
339 semantics. Args are all the same as for mach_msg. intr-rpc.h arranges
340 for this version to be used automatically by the RPC stubs the library
341 builds in place of the normal mach_msg. */
342 error_t _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
343 mach_msg_option_t option,
344 mach_msg_size_t send_size,
345 mach_msg_size_t rcv_size,
346 mach_port_t rcv_name,
347 mach_msg_timeout_t timeout,
348 mach_port_t notify);
349
350
351 /* Milliseconds to wait for an interruptible RPC to return after
352 `interrupt_operation'. */
353
354 extern mach_msg_timeout_t _hurd_interrupted_rpc_timeout;
355
356
357 /* Mask of signals that cannot be caught, blocked, or ignored. */
358 #define _SIG_CANT_MASK (__sigmask (SIGSTOP) | __sigmask (SIGKILL))
359
360 /* Do an RPC to a process's message port.
361
362 Each argument is an expression which returns an error code; each
363 expression may be evaluated several times. FETCH_MSGPORT_EXPR should
364 fetch the appropriate message port and store it in the local variable
365 `msgport'; it will be deallocated after use. FETCH_REFPORT_EXPR should
366 fetch the appropriate message port and store it in the local variable
367 `refport' (if no reference port is needed in the call, then
368 FETCH_REFPORT_EXPR should be simply KERN_SUCCESS or 0); if
369 DEALLOC_REFPORT evaluates to nonzero it will be deallocated after use,
370 otherwise the FETCH_REFPORT_EXPR must take care of user references to
371 `refport'. RPC_EXPR should perform the desired RPC operation using
372 `msgport' and `refport'.
373
374 The reason for the complexity is that a process's message port and
375 reference port may change between fetching those ports and completing an
376 RPC using them (usually they change only when a process execs). The RPC
377 will fail with MACH_SEND_INVALID_DEST if the msgport dies before we can
378 send the RPC request; or with MIG_SERVER_DIED if the msgport was
379 destroyed after we sent the RPC request but before it was serviced. In
380 either of these cases, we retry the entire operation, discarding the old
381 message and reference ports and fetch them anew. */
382
383 #define HURD_MSGPORT_RPC(fetch_msgport_expr, \
384 fetch_refport_expr, dealloc_refport, \
385 rpc_expr) \
386 ({ \
387 error_t __err; \
388 mach_port_t msgport, refport = MACH_PORT_NULL; \
389 do \
390 { \
391 /* Get the message port. */ \
392 __err = (error_t) (fetch_msgport_expr); \
393 if (__err) \
394 break; \
395 /* Get the reference port. */ \
396 __err = (error_t) (fetch_refport_expr); \
397 if (__err) \
398 { \
399 /* Couldn't get it; deallocate MSGPORT and fail. */ \
400 __mach_port_deallocate (__mach_task_self (), msgport); \
401 break; \
402 } \
403 __err = (error_t) (rpc_expr); \
404 __mach_port_deallocate (__mach_task_self (), msgport); \
405 if ((dealloc_refport) && refport != MACH_PORT_NULL) \
406 __mach_port_deallocate (__mach_task_self (), refport); \
407 } while (__err == MACH_SEND_INVALID_DEST \
408 || __err == MIG_SERVER_DIED); \
409 __err; \
410 })
411
412
413 #endif /* hurd/signal.h */