hurd: Replace threadvars with TLS
[glibc.git] / hurd / hurd / signal.h
blobeb9b0ca42370652690e64fe496428a2db6b25202
1 /* Implementing POSIX.1 signals under the Hurd.
2 Copyright (C) 1993-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
19 #ifndef _HURD_SIGNAL_H
21 #define _HURD_SIGNAL_H 1
22 #include <features.h>
24 #define __need_size_t
25 #define __need_NULL
26 #include <stddef.h>
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>
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
51 /* Full details of a signal. */
52 struct hurd_signal_detail
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;
63 /* Per-thread signal state. */
65 struct hurd_sigstate
67 spin_lock_t critical_section_lock; /* Held if in critical section. */
69 spin_lock_t lock; /* Locks most of the rest of the structure. */
71 thread_t thread;
72 struct hurd_sigstate *next; /* Linked-list of thread sigstates. */
74 sigset_t blocked; /* What signals are blocked. */
75 sigset_t pending; /* Pending signals, possibly blocked. */
76 struct sigaction actions[_NSIG];
77 stack_t sigaltstack;
79 /* Chain of thread-local signal preemptors; see <hurd/sigpreempt.h>.
80 Each element of this chain is in local stack storage, and the chain
81 parallels the stack: the head of this chain is in the innermost
82 stack frame, and each next element in an outermore frame. */
83 struct hurd_signal_preemptor *preemptors;
85 /* For each signal that may be pending, the details to deliver it with. */
86 struct hurd_signal_detail pending_data[_NSIG];
88 /* If `suspended' is set when this thread gets a signal,
89 the signal thread sends an empty message to it. */
90 mach_port_t suspended;
92 /* The following members are not locked. They are used only by this
93 thread, or by the signal thread with this thread suspended. */
95 volatile mach_port_t intr_port; /* Port interruptible RPC was sent on. */
97 /* If this is not null, the thread is in sigreturn awaiting delivery of
98 pending signals. This context (the machine-dependent portions only)
99 will be passed to sigreturn after running the handler for a pending
100 signal, instead of examining the thread state. */
101 struct sigcontext *context;
103 /* This is the head of the thread's list of active resources; see
104 <hurd/userlink.h> for details. This member is only used by the
105 thread itself, and always inside a critical section. */
106 struct hurd_userlink *active_resources;
108 /* These are locked normally. */
109 int cancel; /* Flag set by hurd_thread_cancel. */
110 void (*cancel_hook) (void); /* Called on cancellation. */
113 /* Linked list of states of all threads whose state has been asked for. */
115 extern struct hurd_sigstate *_hurd_sigstates;
117 extern struct mutex _hurd_siglock; /* Locks _hurd_sigstates. */
119 /* Get the sigstate of a given thread, taking its lock. */
121 extern struct hurd_sigstate *_hurd_thread_sigstate (thread_t);
123 /* Get the sigstate of the current thread.
124 This uses a per-thread variable to optimize the lookup. */
126 extern struct hurd_sigstate *_hurd_self_sigstate (void)
127 /* This declaration tells the compiler that the value is constant.
128 We assume this won't be called twice from the same stack frame
129 by different threads. */
130 __attribute__ ((__const__));
132 #ifndef _HURD_SIGNAL_H_EXTERN_INLINE
133 #define _HURD_SIGNAL_H_EXTERN_INLINE __extern_inline
134 #endif
136 #if defined __USE_EXTERN_INLINES && defined _LIBC
137 # if IS_IN (libc)
138 _HURD_SIGNAL_H_EXTERN_INLINE struct hurd_sigstate *
139 _hurd_self_sigstate (void)
141 if (THREAD_SELF->_hurd_sigstate == NULL)
142 THREAD_SELF->_hurd_sigstate = _hurd_thread_sigstate (__mach_thread_self ());
143 return THREAD_SELF->_hurd_sigstate;
145 # endif
146 #endif
148 /* Thread listening on our message port; also called the "signal thread". */
150 extern thread_t _hurd_msgport_thread;
152 /* Our message port. We hold the receive right and _hurd_msgport_thread
153 listens for messages on it. We also hold a send right, for convenience. */
155 extern mach_port_t _hurd_msgport;
158 /* Thread to receive process-global signals. */
160 extern thread_t _hurd_sigthread;
163 /* Resource limit on core file size. Enforced by hurdsig.c. */
164 extern int _hurd_core_limit;
166 /* Critical sections.
168 A critical section is a section of code which cannot safely be interrupted
169 to run a signal handler; for example, code that holds any lock cannot be
170 interrupted lest the signal handler try to take the same lock and
171 deadlock result. */
173 extern void *_hurd_critical_section_lock (void);
175 #if defined __USE_EXTERN_INLINES && defined _LIBC
176 # if IS_IN (libc)
177 _HURD_SIGNAL_H_EXTERN_INLINE void *
178 _hurd_critical_section_lock (void)
180 struct hurd_sigstate *ss;
182 #ifdef __LIBC_NO_TLS
183 if (__LIBC_NO_TLS())
184 /* TLS is currently initializing, no need to enter critical section. */
185 return NULL;
186 #endif
188 ss = THREAD_SELF->_hurd_sigstate;
189 if (ss == NULL)
191 /* The thread variable is unset; this must be the first time we've
192 asked for it. In this case, the critical section flag cannot
193 possible already be set. Look up our sigstate structure the slow
194 way. */
195 ss = THREAD_SELF->_hurd_sigstate = _hurd_thread_sigstate (__mach_thread_self ());
198 if (! __spin_try_lock (&ss->critical_section_lock))
199 /* We are already in a critical section, so do nothing. */
200 return NULL;
202 /* With the critical section lock held no signal handler will run.
203 Return our sigstate pointer; this will be passed to
204 _hurd_critical_section_unlock to unlock it. */
205 return ss;
207 # endif
208 #endif
210 extern void _hurd_critical_section_unlock (void *our_lock);
212 #if defined __USE_EXTERN_INLINES && defined _LIBC
213 # if IS_IN (libc)
214 _HURD_SIGNAL_H_EXTERN_INLINE void
215 _hurd_critical_section_unlock (void *our_lock)
217 if (our_lock == NULL)
218 /* The critical section lock was held when we began. Do nothing. */
219 return;
220 else
222 /* It was us who acquired the critical section lock. Unlock it. */
223 struct hurd_sigstate *ss = (struct hurd_sigstate *) our_lock;
224 sigset_t pending;
225 __spin_lock (&ss->lock);
226 __spin_unlock (&ss->critical_section_lock);
227 pending = ss->pending & ~ss->blocked;
228 __spin_unlock (&ss->lock);
229 if (! __sigisemptyset (&pending))
230 /* There are unblocked signals pending, which weren't
231 delivered because we were in the critical section.
232 Tell the signal thread to deliver them now. */
233 __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
236 # endif
237 #endif
239 /* Convenient macros for simple uses of critical sections.
240 These two must be used as a pair at the same C scoping level. */
242 #define HURD_CRITICAL_BEGIN \
243 { void *__hurd_critical__ = _hurd_critical_section_lock ()
244 #define HURD_CRITICAL_END \
245 _hurd_critical_section_unlock (__hurd_critical__); } while (0)
247 /* Initialize the signal code, and start the signal thread.
248 Arguments give the "init ints" from exec_startup. */
250 extern void _hurdsig_init (const int *intarray, size_t intarraysize);
252 /* Initialize proc server-assisted fault recovery for the signal thread. */
254 extern void _hurdsig_fault_init (void);
256 /* Raise a signal as described by SIGNO an DETAIL, on the thread whose
257 sigstate SS points to. If SS is a null pointer, this instead affects
258 the calling thread. */
260 extern int _hurd_raise_signal (struct hurd_sigstate *ss, int signo,
261 const struct hurd_signal_detail *detail);
263 /* Translate a Mach exception into a signal (machine-dependent). */
265 extern void _hurd_exception2signal (struct hurd_signal_detail *detail,
266 int *signo);
269 /* Make the thread described by SS take the signal described by SIGNO and
270 DETAIL. If the process is traced, this will in fact stop with a SIGNO
271 as the stop signal unless UNTRACED is nonzero. When the signal can be
272 considered delivered, sends a sig_post reply message on REPLY_PORT
273 indicating success. SS is not locked. */
275 extern void _hurd_internal_post_signal (struct hurd_sigstate *ss,
276 int signo,
277 struct hurd_signal_detail *detail,
278 mach_port_t reply_port,
279 mach_msg_type_name_t reply_port_type,
280 int untraced);
282 /* Set up STATE and SS to handle signal SIGNO by running HANDLER. If
283 RPC_WAIT is nonzero, the thread needs to wait for a pending RPC to
284 finish before running the signal handler. The handler is passed SIGNO,
285 SIGCODE, and the returned `struct sigcontext' (which resides on the
286 stack the handler will use, and which describes the state of the thread
287 encoded in STATE before running the handler). */
289 struct machine_thread_all_state;
290 extern struct sigcontext *
291 _hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
292 int signo, struct hurd_signal_detail *detail,
293 int rpc_wait, struct machine_thread_all_state *state);
295 /* Function run by the signal thread to receive from the signal port. */
297 extern void _hurd_msgport_receive (void);
299 /* Set up STATE with a thread state that, when resumed, is
300 like `longjmp (_hurd_sigthread_fault_env, 1)'. */
302 extern void _hurd_initialize_fault_recovery_state (void *state);
304 /* Set up STATE to do the equivalent of `longjmp (ENV, VAL);'. */
306 extern void _hurd_longjmp_thread_state (void *state, jmp_buf env, int value);
308 /* Function run for SIGINFO when its action is SIG_DFL and the current
309 process is the session leader. */
311 extern void _hurd_siginfo_handler (int);
313 /* Replacement for mach_msg used in RPCs to provide Hurd interruption
314 semantics. Args are all the same as for mach_msg. intr-rpc.h arranges
315 for this version to be used automatically by the RPC stubs the library
316 builds in place of the normal mach_msg. */
317 error_t _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
318 mach_msg_option_t option,
319 mach_msg_size_t send_size,
320 mach_msg_size_t rcv_size,
321 mach_port_t rcv_name,
322 mach_msg_timeout_t timeout,
323 mach_port_t notify);
326 /* Milliseconds to wait for an interruptible RPC to return after
327 `interrupt_operation'. */
329 extern mach_msg_timeout_t _hurd_interrupted_rpc_timeout;
332 /* Mask of signals that cannot be caught, blocked, or ignored. */
333 #define _SIG_CANT_MASK (__sigmask (SIGSTOP) | __sigmask (SIGKILL))
335 /* Do an RPC to a process's message port.
337 Each argument is an expression which returns an error code; each
338 expression may be evaluated several times. FETCH_MSGPORT_EXPR should
339 fetch the appropriate message port and store it in the local variable
340 `msgport'; it will be deallocated after use. FETCH_REFPORT_EXPR should
341 fetch the appropriate message port and store it in the local variable
342 `refport' (if no reference port is needed in the call, then
343 FETCH_REFPORT_EXPR should be simply KERN_SUCCESS or 0); if
344 DEALLOC_REFPORT evaluates to nonzero it will be deallocated after use,
345 otherwise the FETCH_REFPORT_EXPR must take care of user references to
346 `refport'. RPC_EXPR should perform the desired RPC operation using
347 `msgport' and `refport'.
349 The reason for the complexity is that a process's message port and
350 reference port may change between fetching those ports and completing an
351 RPC using them (usually they change only when a process execs). The RPC
352 will fail with MACH_SEND_INVALID_DEST if the msgport dies before we can
353 send the RPC request; or with MIG_SERVER_DIED if the msgport was
354 destroyed after we sent the RPC request but before it was serviced. In
355 either of these cases, we retry the entire operation, discarding the old
356 message and reference ports and fetch them anew. */
358 #define HURD_MSGPORT_RPC(fetch_msgport_expr, \
359 fetch_refport_expr, dealloc_refport, \
360 rpc_expr) \
361 ({ \
362 error_t __err; \
363 mach_port_t msgport, refport = MACH_PORT_NULL; \
364 do \
366 /* Get the message port. */ \
367 __err = (error_t) (fetch_msgport_expr); \
368 if (__err) \
369 break; \
370 /* Get the reference port. */ \
371 __err = (error_t) (fetch_refport_expr); \
372 if (__err) \
374 /* Couldn't get it; deallocate MSGPORT and fail. */ \
375 __mach_port_deallocate (__mach_task_self (), msgport); \
376 break; \
378 __err = (error_t) (rpc_expr); \
379 __mach_port_deallocate (__mach_task_self (), msgport); \
380 if ((dealloc_refport) && refport != MACH_PORT_NULL) \
381 __mach_port_deallocate (__mach_task_self (), refport); \
382 } while (__err == MACH_SEND_INVALID_DEST || \
383 __err == MIG_SERVER_DIED); \
384 __err; \
388 #endif /* hurd/signal.h */