2.9
[glibc/nacl-glibc.git] / hurd / hurd / signal.h
blob21e30c5729914a55f12805ed77a606f76406168e
1 /* Implementing POSIX.1 signals under the Hurd.
2 Copyright (C) 1993,94,95,96,98,99,2002,2007,2008
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #ifndef _HURD_SIGNAL_H
23 #define _HURD_SIGNAL_H 1
24 #include <features.h>
25 /* Make sure <signal.h> is going to define NSIG. */
26 #ifndef __USE_GNU
27 #error "Must have `_GNU_SOURCE' feature test macro to use this file"
28 #endif
30 #define __need_size_t
31 #define __need_NULL
32 #include <stddef.h>
34 #include <mach/mach_types.h>
35 #include <mach/port.h>
36 #include <mach/message.h>
37 #include <hurd/hurd_types.h>
38 #include <signal.h>
39 #include <errno.h>
40 #include <hurd/msg.h>
42 #include <cthreads.h> /* For `struct mutex'. */
43 #include <setjmp.h> /* For `jmp_buf'. */
44 #include <spin-lock.h>
45 #include <hurd/threadvar.h> /* We cache sigstate in a threadvar. */
46 struct hurd_signal_preemptor; /* <hurd/sigpreempt.h> */
49 /* Full details of a signal. */
50 struct hurd_signal_detail
52 /* Codes from origination Mach exception_raise message. */
53 integer_t exc, exc_code, exc_subcode;
54 /* Sigcode as passed or computed from exception codes. */
55 integer_t code;
56 /* Error code as passed or extracted from exception codes. */
57 error_t error;
61 /* Per-thread signal state. */
63 struct hurd_sigstate
65 spin_lock_t critical_section_lock; /* Held if in critical section. */
67 spin_lock_t lock; /* Locks most of the rest of the structure. */
69 thread_t thread;
70 struct hurd_sigstate *next; /* Linked-list of thread sigstates. */
72 sigset_t blocked; /* What signals are blocked. */
73 sigset_t pending; /* Pending signals, possibly blocked. */
74 struct sigaction actions[NSIG];
75 struct sigaltstack sigaltstack;
77 /* Chain of thread-local signal preemptors; see <hurd/sigpreempt.h>.
78 Each element of this chain is in local stack storage, and the chain
79 parallels the stack: the head of this chain is in the innermost
80 stack frame, and each next element in an outermore frame. */
81 struct hurd_signal_preemptor *preemptors;
83 /* For each signal that may be pending, the details to deliver it with. */
84 struct hurd_signal_detail pending_data[NSIG];
86 /* If `suspended' is set when this thread gets a signal,
87 the signal thread sends an empty message to it. */
88 mach_port_t suspended;
90 /* The following members are not locked. They are used only by this
91 thread, or by the signal thread with this thread suspended. */
93 volatile mach_port_t intr_port; /* Port interruptible RPC was sent on. */
95 /* If this is not null, the thread is in sigreturn awaiting delivery of
96 pending signals. This context (the machine-dependent portions only)
97 will be passed to sigreturn after running the handler for a pending
98 signal, instead of examining the thread state. */
99 struct sigcontext *context;
101 /* This is the head of the thread's list of active resources; see
102 <hurd/userlink.h> for details. This member is only used by the
103 thread itself, and always inside a critical section. */
104 struct hurd_userlink *active_resources;
106 /* These are locked normally. */
107 int cancel; /* Flag set by hurd_thread_cancel. */
108 void (*cancel_hook) (void); /* Called on cancellation. */
111 /* Linked list of states of all threads whose state has been asked for. */
113 extern struct hurd_sigstate *_hurd_sigstates;
115 extern struct mutex _hurd_siglock; /* Locks _hurd_sigstates. */
117 /* Get the sigstate of a given thread, taking its lock. */
119 extern struct hurd_sigstate *_hurd_thread_sigstate (thread_t);
121 /* Get the sigstate of the current thread.
122 This uses a per-thread variable to optimize the lookup. */
124 extern struct hurd_sigstate *_hurd_self_sigstate (void)
125 /* This declaration tells the compiler that the value is constant.
126 We assume this won't be called twice from the same stack frame
127 by different threads. */
128 __attribute__ ((__const__));
130 #ifndef _HURD_SIGNAL_H_EXTERN_INLINE
131 #define _HURD_SIGNAL_H_EXTERN_INLINE __extern_inline
132 #endif
134 _HURD_SIGNAL_H_EXTERN_INLINE struct hurd_sigstate *
135 _hurd_self_sigstate (void)
137 struct hurd_sigstate **location =
138 (void *) __hurd_threadvar_location (_HURD_THREADVAR_SIGSTATE);
139 if (*location == NULL)
140 *location = _hurd_thread_sigstate (__mach_thread_self ());
141 return *location;
144 /* Thread listening on our message port; also called the "signal thread". */
146 extern thread_t _hurd_msgport_thread;
148 /* Our message port. We hold the receive right and _hurd_msgport_thread
149 listens for messages on it. We also hold a send right, for convenience. */
151 extern mach_port_t _hurd_msgport;
154 /* Thread to receive process-global signals. */
156 extern thread_t _hurd_sigthread;
159 /* Resource limit on core file size. Enforced by hurdsig.c. */
160 extern int _hurd_core_limit;
162 /* Critical sections.
164 A critical section is a section of code which cannot safely be interrupted
165 to run a signal handler; for example, code that holds any lock cannot be
166 interrupted lest the signal handler try to take the same lock and
167 deadlock result. */
169 _HURD_SIGNAL_H_EXTERN_INLINE void *
170 _hurd_critical_section_lock (void)
172 struct hurd_sigstate **location =
173 (void *) __hurd_threadvar_location (_HURD_THREADVAR_SIGSTATE);
174 struct hurd_sigstate *ss = *location;
175 if (ss == NULL)
177 /* The thread variable is unset; this must be the first time we've
178 asked for it. In this case, the critical section flag cannot
179 possible already be set. Look up our sigstate structure the slow
180 way; this locks the sigstate lock. */
181 ss = *location = _hurd_thread_sigstate (__mach_thread_self ());
182 __spin_unlock (&ss->lock);
185 if (! __spin_try_lock (&ss->critical_section_lock))
186 /* We are already in a critical section, so do nothing. */
187 return NULL;
189 /* With the critical section lock held no signal handler will run.
190 Return our sigstate pointer; this will be passed to
191 _hurd_critical_section_unlock to unlock it. */
192 return ss;
195 _HURD_SIGNAL_H_EXTERN_INLINE void
196 _hurd_critical_section_unlock (void *our_lock)
198 if (our_lock == NULL)
199 /* The critical section lock was held when we began. Do nothing. */
200 return;
201 else
203 /* It was us who acquired the critical section lock. Unlock it. */
204 struct hurd_sigstate *ss = our_lock;
205 sigset_t pending;
206 __spin_lock (&ss->lock);
207 __spin_unlock (&ss->critical_section_lock);
208 pending = ss->pending & ~ss->blocked;
209 __spin_unlock (&ss->lock);
210 if (! __sigisemptyset (&pending))
211 /* There are unblocked signals pending, which weren't
212 delivered because we were in the critical section.
213 Tell the signal thread to deliver them now. */
214 __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
218 /* Convenient macros for simple uses of critical sections.
219 These two must be used as a pair at the same C scoping level. */
221 #define HURD_CRITICAL_BEGIN \
222 { void *__hurd_critical__ = _hurd_critical_section_lock ()
223 #define HURD_CRITICAL_END \
224 _hurd_critical_section_unlock (__hurd_critical__); } while (0)
226 /* Initialize the signal code, and start the signal thread.
227 Arguments give the "init ints" from exec_startup. */
229 extern void _hurdsig_init (const int *intarray, size_t intarraysize);
231 /* Initialize proc server-assisted fault recovery for the signal thread. */
233 extern void _hurdsig_fault_init (void);
235 /* Raise a signal as described by SIGNO an DETAIL, on the thread whose
236 sigstate SS points to. If SS is a null pointer, this instead affects
237 the calling thread. */
239 extern void _hurd_raise_signal (struct hurd_sigstate *ss, int signo,
240 const struct hurd_signal_detail *detail);
242 /* Translate a Mach exception into a signal (machine-dependent). */
244 extern void _hurd_exception2signal (struct hurd_signal_detail *detail,
245 int *signo);
248 /* Make the thread described by SS take the signal described by SIGNO and
249 DETAIL. If the process is traced, this will in fact stop with a SIGNO
250 as the stop signal unless UNTRACED is nonzero. When the signal can be
251 considered delivered, sends a sig_post reply message on REPLY_PORT
252 indicating success. SS is not locked. */
254 extern void _hurd_internal_post_signal (struct hurd_sigstate *ss,
255 int signo,
256 struct hurd_signal_detail *detail,
257 mach_port_t reply_port,
258 mach_msg_type_name_t reply_port_type,
259 int untraced);
261 /* Set up STATE and SS to handle signal SIGNO by running HANDLER. If
262 RPC_WAIT is nonzero, the thread needs to wait for a pending RPC to
263 finish before running the signal handler. The handler is passed SIGNO,
264 SIGCODE, and the returned `struct sigcontext' (which resides on the
265 stack the handler will use, and which describes the state of the thread
266 encoded in STATE before running the handler). */
268 struct machine_thread_all_state;
269 extern struct sigcontext *
270 _hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
271 int signo, struct hurd_signal_detail *detail,
272 int rpc_wait, struct machine_thread_all_state *state);
274 /* Function run by the signal thread to receive from the signal port. */
276 extern void _hurd_msgport_receive (void);
278 /* Set up STATE with a thread state that, when resumed, is
279 like `longjmp (_hurd_sigthread_fault_env, 1)'. */
281 extern void _hurd_initialize_fault_recovery_state (void *state);
283 /* Set up STATE to do the equivalent of `longjmp (ENV, VAL);'. */
285 extern void _hurd_longjmp_thread_state (void *state, jmp_buf env, int value);
287 /* Function run for SIGINFO when its action is SIG_DFL and the current
288 process is the session leader. */
290 extern void _hurd_siginfo_handler (int);
292 /* Replacement for mach_msg used in RPCs to provide Hurd interruption
293 semantics. Args are all the same as for mach_msg. intr-rpc.h arranges
294 for this version to be used automatically by the RPC stubs the library
295 builds in place of the normal mach_msg. */
296 error_t _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
297 mach_msg_option_t option,
298 mach_msg_size_t send_size,
299 mach_msg_size_t rcv_size,
300 mach_port_t rcv_name,
301 mach_msg_timeout_t timeout,
302 mach_port_t notify);
305 /* Milliseconds to wait for an interruptible RPC to return after
306 `interrupt_operation'. */
308 extern mach_msg_timeout_t _hurd_interrupted_rpc_timeout;
311 /* Mask of signals that cannot be caught, blocked, or ignored. */
312 #define _SIG_CANT_MASK (__sigmask (SIGSTOP) | __sigmask (SIGKILL))
314 /* Do an RPC to a process's message port.
316 Each argument is an expression which returns an error code; each
317 expression may be evaluated several times. FETCH_MSGPORT_EXPR should
318 fetch the appropriate message port and store it in the local variable
319 `msgport'; it will be deallocated after use. FETCH_REFPORT_EXPR should
320 fetch the appropriate message port and store it in the local variable
321 `refport' (if no reference port is needed in the call, then
322 FETCH_REFPORT_EXPR should be simply KERN_SUCCESS or 0); if
323 DEALLOC_REFPORT evaluates to nonzero it will be deallocated after use,
324 otherwise the FETCH_REFPORT_EXPR must take care of user references to
325 `refport'. RPC_EXPR should perform the desired RPC operation using
326 `msgport' and `refport'.
328 The reason for the complexity is that a process's message port and
329 reference port may change between fetching those ports and completing an
330 RPC using them (usually they change only when a process execs). The RPC
331 will fail with MACH_SEND_INVALID_DEST if the msgport dies before we can
332 send the RPC request; or with MIG_SERVER_DIED if the msgport was
333 destroyed after we sent the RPC request but before it was serviced. In
334 either of these cases, we retry the entire operation, discarding the old
335 message and reference ports and fetch them anew. */
337 #define HURD_MSGPORT_RPC(fetch_msgport_expr, \
338 fetch_refport_expr, dealloc_refport, \
339 rpc_expr) \
340 ({ \
341 error_t __err; \
342 mach_port_t msgport, refport = MACH_PORT_NULL; \
343 do \
345 /* Get the message port. */ \
346 __err = (fetch_msgport_expr); \
347 if (__err) \
348 break; \
349 /* Get the reference port. */ \
350 __err = (fetch_refport_expr); \
351 if (__err) \
353 /* Couldn't get it; deallocate MSGPORT and fail. */ \
354 __mach_port_deallocate (__mach_task_self (), msgport); \
355 break; \
357 __err = (rpc_expr); \
358 __mach_port_deallocate (__mach_task_self (), msgport); \
359 if ((dealloc_refport) && refport != MACH_PORT_NULL) \
360 __mach_port_deallocate (__mach_task_self (), refport); \
361 } while (__err == MACH_SEND_INVALID_DEST || \
362 __err == MIG_SERVER_DIED); \
363 __err; \
367 #endif /* hurd/signal.h */