Update copyright notices with scripts/update-copyrights
[glibc.git] / hurd / hurd / signal.h
blob8355d67ff3215ac1df8b169022f78034cad048bd
1 /* Implementing POSIX.1 signals under the Hurd.
2 Copyright (C) 1993-2014 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>
23 /* Make sure <signal.h> is going to define NSIG. */
24 #ifndef __USE_GNU
25 #error "Must have `_GNU_SOURCE' feature test macro to use this file"
26 #endif
28 #define __need_size_t
29 #define __need_NULL
30 #include <stddef.h>
32 #include <mach/mach_types.h>
33 #include <mach/port.h>
34 #include <mach/message.h>
35 #include <hurd/hurd_types.h>
36 #include <signal.h>
37 #include <errno.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 #include <hurd/threadvar.h> /* We cache sigstate in a threadvar. */
44 struct hurd_signal_preemptor; /* <hurd/sigpreempt.h> */
47 /* Full details of a signal. */
48 struct hurd_signal_detail
50 /* Codes from origination Mach exception_raise message. */
51 integer_t exc, exc_code, exc_subcode;
52 /* Sigcode as passed or computed from exception codes. */
53 integer_t code;
54 /* Error code as passed or extracted from exception codes. */
55 error_t error;
59 /* Per-thread signal state. */
61 struct hurd_sigstate
63 spin_lock_t critical_section_lock; /* Held if in critical section. */
65 spin_lock_t lock; /* Locks most of the rest of the structure. */
67 thread_t thread;
68 struct hurd_sigstate *next; /* Linked-list of thread sigstates. */
70 sigset_t blocked; /* What signals are blocked. */
71 sigset_t pending; /* Pending signals, possibly blocked. */
72 struct sigaction actions[NSIG];
73 struct sigaltstack sigaltstack;
75 /* Chain of thread-local signal preemptors; see <hurd/sigpreempt.h>.
76 Each element of this chain is in local stack storage, and the chain
77 parallels the stack: the head of this chain is in the innermost
78 stack frame, and each next element in an outermore frame. */
79 struct hurd_signal_preemptor *preemptors;
81 /* For each signal that may be pending, the details to deliver it with. */
82 struct hurd_signal_detail pending_data[NSIG];
84 /* If `suspended' is set when this thread gets a signal,
85 the signal thread sends an empty message to it. */
86 mach_port_t suspended;
88 /* The following members are not locked. They are used only by this
89 thread, or by the signal thread with this thread suspended. */
91 volatile mach_port_t intr_port; /* Port interruptible RPC was sent on. */
93 /* If this is not null, the thread is in sigreturn awaiting delivery of
94 pending signals. This context (the machine-dependent portions only)
95 will be passed to sigreturn after running the handler for a pending
96 signal, instead of examining the thread state. */
97 struct sigcontext *context;
99 /* This is the head of the thread's list of active resources; see
100 <hurd/userlink.h> for details. This member is only used by the
101 thread itself, and always inside a critical section. */
102 struct hurd_userlink *active_resources;
104 /* These are locked normally. */
105 int cancel; /* Flag set by hurd_thread_cancel. */
106 void (*cancel_hook) (void); /* Called on cancellation. */
109 /* Linked list of states of all threads whose state has been asked for. */
111 extern struct hurd_sigstate *_hurd_sigstates;
113 extern struct mutex _hurd_siglock; /* Locks _hurd_sigstates. */
115 /* Get the sigstate of a given thread, taking its lock. */
117 extern struct hurd_sigstate *_hurd_thread_sigstate (thread_t);
119 /* Get the sigstate of the current thread.
120 This uses a per-thread variable to optimize the lookup. */
122 extern struct hurd_sigstate *_hurd_self_sigstate (void)
123 /* This declaration tells the compiler that the value is constant.
124 We assume this won't be called twice from the same stack frame
125 by different threads. */
126 __attribute__ ((__const__));
128 #ifndef _HURD_SIGNAL_H_EXTERN_INLINE
129 #define _HURD_SIGNAL_H_EXTERN_INLINE __extern_inline
130 #endif
132 _HURD_SIGNAL_H_EXTERN_INLINE struct hurd_sigstate *
133 _hurd_self_sigstate (void)
135 struct hurd_sigstate **location =
136 (void *) __hurd_threadvar_location (_HURD_THREADVAR_SIGSTATE);
137 if (*location == NULL)
138 *location = _hurd_thread_sigstate (__mach_thread_self ());
139 return *location;
142 /* Thread listening on our message port; also called the "signal thread". */
144 extern thread_t _hurd_msgport_thread;
146 /* Our message port. We hold the receive right and _hurd_msgport_thread
147 listens for messages on it. We also hold a send right, for convenience. */
149 extern mach_port_t _hurd_msgport;
152 /* Thread to receive process-global signals. */
154 extern thread_t _hurd_sigthread;
157 /* Resource limit on core file size. Enforced by hurdsig.c. */
158 extern int _hurd_core_limit;
160 /* Critical sections.
162 A critical section is a section of code which cannot safely be interrupted
163 to run a signal handler; for example, code that holds any lock cannot be
164 interrupted lest the signal handler try to take the same lock and
165 deadlock result. */
167 _HURD_SIGNAL_H_EXTERN_INLINE void *
168 _hurd_critical_section_lock (void)
170 struct hurd_sigstate **location =
171 (void *) __hurd_threadvar_location (_HURD_THREADVAR_SIGSTATE);
172 struct hurd_sigstate *ss = *location;
173 if (ss == NULL)
175 /* The thread variable is unset; this must be the first time we've
176 asked for it. In this case, the critical section flag cannot
177 possible already be set. Look up our sigstate structure the slow
178 way; this locks the sigstate lock. */
179 ss = *location = _hurd_thread_sigstate (__mach_thread_self ());
180 __spin_unlock (&ss->lock);
183 if (! __spin_try_lock (&ss->critical_section_lock))
184 /* We are already in a critical section, so do nothing. */
185 return NULL;
187 /* With the critical section lock held no signal handler will run.
188 Return our sigstate pointer; this will be passed to
189 _hurd_critical_section_unlock to unlock it. */
190 return ss;
193 _HURD_SIGNAL_H_EXTERN_INLINE void
194 _hurd_critical_section_unlock (void *our_lock)
196 if (our_lock == NULL)
197 /* The critical section lock was held when we began. Do nothing. */
198 return;
199 else
201 /* It was us who acquired the critical section lock. Unlock it. */
202 struct hurd_sigstate *ss = our_lock;
203 sigset_t pending;
204 __spin_lock (&ss->lock);
205 __spin_unlock (&ss->critical_section_lock);
206 pending = ss->pending & ~ss->blocked;
207 __spin_unlock (&ss->lock);
208 if (! __sigisemptyset (&pending))
209 /* There are unblocked signals pending, which weren't
210 delivered because we were in the critical section.
211 Tell the signal thread to deliver them now. */
212 __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
216 /* Convenient macros for simple uses of critical sections.
217 These two must be used as a pair at the same C scoping level. */
219 #define HURD_CRITICAL_BEGIN \
220 { void *__hurd_critical__ = _hurd_critical_section_lock ()
221 #define HURD_CRITICAL_END \
222 _hurd_critical_section_unlock (__hurd_critical__); } while (0)
224 /* Initialize the signal code, and start the signal thread.
225 Arguments give the "init ints" from exec_startup. */
227 extern void _hurdsig_init (const int *intarray, size_t intarraysize);
229 /* Initialize proc server-assisted fault recovery for the signal thread. */
231 extern void _hurdsig_fault_init (void);
233 /* Raise a signal as described by SIGNO an DETAIL, on the thread whose
234 sigstate SS points to. If SS is a null pointer, this instead affects
235 the calling thread. */
237 extern void _hurd_raise_signal (struct hurd_sigstate *ss, int signo,
238 const struct hurd_signal_detail *detail);
240 /* Translate a Mach exception into a signal (machine-dependent). */
242 extern void _hurd_exception2signal (struct hurd_signal_detail *detail,
243 int *signo);
246 /* Make the thread described by SS take the signal described by SIGNO and
247 DETAIL. If the process is traced, this will in fact stop with a SIGNO
248 as the stop signal unless UNTRACED is nonzero. When the signal can be
249 considered delivered, sends a sig_post reply message on REPLY_PORT
250 indicating success. SS is not locked. */
252 extern void _hurd_internal_post_signal (struct hurd_sigstate *ss,
253 int signo,
254 struct hurd_signal_detail *detail,
255 mach_port_t reply_port,
256 mach_msg_type_name_t reply_port_type,
257 int untraced);
259 /* Set up STATE and SS to handle signal SIGNO by running HANDLER. If
260 RPC_WAIT is nonzero, the thread needs to wait for a pending RPC to
261 finish before running the signal handler. The handler is passed SIGNO,
262 SIGCODE, and the returned `struct sigcontext' (which resides on the
263 stack the handler will use, and which describes the state of the thread
264 encoded in STATE before running the handler). */
266 struct machine_thread_all_state;
267 extern struct sigcontext *
268 _hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
269 int signo, struct hurd_signal_detail *detail,
270 int rpc_wait, struct machine_thread_all_state *state);
272 /* Function run by the signal thread to receive from the signal port. */
274 extern void _hurd_msgport_receive (void);
276 /* Set up STATE with a thread state that, when resumed, is
277 like `longjmp (_hurd_sigthread_fault_env, 1)'. */
279 extern void _hurd_initialize_fault_recovery_state (void *state);
281 /* Set up STATE to do the equivalent of `longjmp (ENV, VAL);'. */
283 extern void _hurd_longjmp_thread_state (void *state, jmp_buf env, int value);
285 /* Function run for SIGINFO when its action is SIG_DFL and the current
286 process is the session leader. */
288 extern void _hurd_siginfo_handler (int);
290 /* Replacement for mach_msg used in RPCs to provide Hurd interruption
291 semantics. Args are all the same as for mach_msg. intr-rpc.h arranges
292 for this version to be used automatically by the RPC stubs the library
293 builds in place of the normal mach_msg. */
294 error_t _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
295 mach_msg_option_t option,
296 mach_msg_size_t send_size,
297 mach_msg_size_t rcv_size,
298 mach_port_t rcv_name,
299 mach_msg_timeout_t timeout,
300 mach_port_t notify);
303 /* Milliseconds to wait for an interruptible RPC to return after
304 `interrupt_operation'. */
306 extern mach_msg_timeout_t _hurd_interrupted_rpc_timeout;
309 /* Mask of signals that cannot be caught, blocked, or ignored. */
310 #define _SIG_CANT_MASK (__sigmask (SIGSTOP) | __sigmask (SIGKILL))
312 /* Do an RPC to a process's message port.
314 Each argument is an expression which returns an error code; each
315 expression may be evaluated several times. FETCH_MSGPORT_EXPR should
316 fetch the appropriate message port and store it in the local variable
317 `msgport'; it will be deallocated after use. FETCH_REFPORT_EXPR should
318 fetch the appropriate message port and store it in the local variable
319 `refport' (if no reference port is needed in the call, then
320 FETCH_REFPORT_EXPR should be simply KERN_SUCCESS or 0); if
321 DEALLOC_REFPORT evaluates to nonzero it will be deallocated after use,
322 otherwise the FETCH_REFPORT_EXPR must take care of user references to
323 `refport'. RPC_EXPR should perform the desired RPC operation using
324 `msgport' and `refport'.
326 The reason for the complexity is that a process's message port and
327 reference port may change between fetching those ports and completing an
328 RPC using them (usually they change only when a process execs). The RPC
329 will fail with MACH_SEND_INVALID_DEST if the msgport dies before we can
330 send the RPC request; or with MIG_SERVER_DIED if the msgport was
331 destroyed after we sent the RPC request but before it was serviced. In
332 either of these cases, we retry the entire operation, discarding the old
333 message and reference ports and fetch them anew. */
335 #define HURD_MSGPORT_RPC(fetch_msgport_expr, \
336 fetch_refport_expr, dealloc_refport, \
337 rpc_expr) \
338 ({ \
339 error_t __err; \
340 mach_port_t msgport, refport = MACH_PORT_NULL; \
341 do \
343 /* Get the message port. */ \
344 __err = (fetch_msgport_expr); \
345 if (__err) \
346 break; \
347 /* Get the reference port. */ \
348 __err = (fetch_refport_expr); \
349 if (__err) \
351 /* Couldn't get it; deallocate MSGPORT and fail. */ \
352 __mach_port_deallocate (__mach_task_self (), msgport); \
353 break; \
355 __err = (rpc_expr); \
356 __mach_port_deallocate (__mach_task_self (), msgport); \
357 if ((dealloc_refport) && refport != MACH_PORT_NULL) \
358 __mach_port_deallocate (__mach_task_self (), refport); \
359 } while (__err == MACH_SEND_INVALID_DEST || \
360 __err == MIG_SERVER_DIED); \
361 __err; \
365 #endif /* hurd/signal.h */