1 /* Implementing POSIX.1 signals under the Hurd.
2 Copyright (C) 1993, 94, 95, 96, 98, 99 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #ifndef _HURD_SIGNAL_H
22 #define _HURD_SIGNAL_H 1
24 /* Make sure <signal.h> is going to define NSIG. */
26 #error "Must have `_GNU_SOURCE' feature test macro to use this file"
32 #include <mach/mach_types.h>
33 #include <mach/port.h>
34 #include <mach/message.h>
35 #include <hurd/hurd_types.h>
40 #include <cthreads.h> /* For `struct mutex'. */
41 #include <spin-lock.h>
42 #include <hurd/threadvar.h> /* We cache sigstate in a threadvar. */
43 struct hurd_signal_preemptor; /* <hurd/sigpreempt.h> */
46 /* Full details of a signal. */
47 struct hurd_signal_detail
49 /* Codes from origination Mach exception_raise message. */
50 integer_t exc, exc_code, exc_subcode;
51 /* Sigcode as passed or computed from exception codes. */
53 /* Error code as passed or extracted from exception codes. */
58 /* Per-thread signal state. */
62 spin_lock_t critical_section_lock; /* Held if in critical section. */
64 spin_lock_t lock; /* Locks most of the rest of the structure. */
67 struct hurd_sigstate *next; /* Linked-list of thread sigstates. */
69 sigset_t blocked; /* What signals are blocked. */
70 sigset_t pending; /* Pending signals, possibly blocked. */
71 struct sigaction actions[NSIG];
72 struct sigaltstack sigaltstack;
74 /* Chain of thread-local signal preemptors; see <hurd/sigpreempt.h>.
75 Each element of this chain is in local stack storage, and the chain
76 parallels the stack: the head of this chain is in the innermost
77 stack frame, and each next element in an outermore frame. */
78 struct hurd_signal_preemptor *preemptors;
80 /* For each signal that may be pending, the details to deliver it with. */
81 struct hurd_signal_detail pending_data[NSIG];
83 /* If `suspended' is set when this thread gets a signal,
84 the signal thread sends an empty message to it. */
85 mach_port_t suspended;
87 /* The following members are not locked. They are used only by this
88 thread, or by the signal thread with this thread suspended. */
90 volatile mach_port_t intr_port; /* Port interruptible RPC was sent on. */
92 /* If this is not null, the thread is in sigreturn awaiting delivery of
93 pending signals. This context (the machine-dependent portions only)
94 will be passed to sigreturn after running the handler for a pending
95 signal, instead of examining the thread state. */
96 struct sigcontext *context;
98 /* This is the head of the thread's list of active resources; see
99 <hurd/userlink.h> for details. This member is only used by the
100 thread itself, and always inside a critical section. */
101 struct hurd_userlink *active_resources;
103 /* These are locked normally. */
104 int cancel; /* Flag set by hurd_thread_cancel. */
105 void (*cancel_hook) (void); /* Called on cancellation. */
108 /* Linked list of states of all threads whose state has been asked for. */
110 extern struct hurd_sigstate *_hurd_sigstates;
112 extern struct mutex _hurd_siglock; /* Locks _hurd_sigstates. */
114 /* Get the sigstate of a given thread, taking its lock. */
116 extern struct hurd_sigstate *_hurd_thread_sigstate (thread_t);
118 /* Get the sigstate of the current thread.
119 This uses a per-thread variable to optimize the lookup. */
121 extern struct hurd_sigstate *_hurd_self_sigstate (void)
122 /* This declaration tells the compiler that the value is constant.
123 We assume this won't be called twice from the same stack frame
124 by different threads. */
125 __attribute__ ((__const__));
127 #ifndef _HURD_SIGNAL_H_EXTERN_INLINE
128 #define _HURD_SIGNAL_H_EXTERN_INLINE extern __inline
131 _HURD_SIGNAL_H_EXTERN_INLINE struct hurd_sigstate *
132 _hurd_self_sigstate (void)
134 struct hurd_sigstate **location =
135 (void *) __hurd_threadvar_location (_HURD_THREADVAR_SIGSTATE);
136 if (*location == NULL)
137 *location = _hurd_thread_sigstate (__mach_thread_self ());
141 /* Thread listening on our message port; also called the "signal thread". */
143 extern thread_t _hurd_msgport_thread;
145 /* Our message port. We hold the receive right and _hurd_msgport_thread
146 listens for messages on it. We also hold a send right, for convenience. */
148 extern mach_port_t _hurd_msgport;
151 /* Thread to receive process-global signals. */
153 extern thread_t _hurd_sigthread;
156 /* Resource limit on core file size. Enforced by hurdsig.c. */
157 extern int _hurd_core_limit;
159 /* Critical sections.
161 A critical section is a section of code which cannot safely be interrupted
162 to run a signal handler; for example, code that holds any lock cannot be
163 interrupted lest the signal handler try to take the same lock and
166 _HURD_SIGNAL_H_EXTERN_INLINE void *
167 _hurd_critical_section_lock (void)
169 struct hurd_sigstate **location =
170 (void *) __hurd_threadvar_location (_HURD_THREADVAR_SIGSTATE);
171 struct hurd_sigstate *ss = *location;
174 /* The thread variable is unset; this must be the first time we've
175 asked for it. In this case, the critical section flag cannot
176 possible already be set. Look up our sigstate structure the slow
177 way; this locks the sigstate lock. */
178 ss = *location = _hurd_thread_sigstate (__mach_thread_self ());
179 __spin_unlock (&ss->lock);
182 if (! __spin_try_lock (&ss->critical_section_lock))
183 /* We are already in a critical section, so do nothing. */
186 /* With the critical section lock held no signal handler will run.
187 Return our sigstate pointer; this will be passed to
188 _hurd_critical_section_unlock to unlock it. */
192 _HURD_SIGNAL_H_EXTERN_INLINE void
193 _hurd_critical_section_unlock (void *our_lock)
195 if (our_lock == NULL)
196 /* The critical section lock was held when we began. Do nothing. */
200 /* It was us who acquired the critical section lock. Unlock it. */
201 struct hurd_sigstate *ss = our_lock;
203 __spin_lock (&ss->lock);
204 __spin_unlock (&ss->critical_section_lock);
205 pending = ss->pending & ~ss->blocked;
206 __spin_unlock (&ss->lock);
208 /* There are unblocked signals pending, which weren't
209 delivered because we were in the critical section.
210 Tell the signal thread to deliver them now. */
211 __msg_sig_post (_hurd_msgport, 0, 0, __mach_task_self ());
215 /* Convenient macros for simple uses of critical sections.
216 These two must be used as a pair at the same C scoping level. */
218 #define HURD_CRITICAL_BEGIN \
219 { void *__hurd_critical__ = _hurd_critical_section_lock ()
220 #define HURD_CRITICAL_END \
221 _hurd_critical_section_unlock (__hurd_critical__); } while (0)
223 /* Initialize the signal code, and start the signal thread.
224 Arguments give the "init ints" from exec_startup. */
226 extern void _hurdsig_init (const int *intarray, size_t intarraysize);
228 /* Initialize proc server-assisted fault recovery for the signal thread. */
230 extern void _hurdsig_fault_init (void);
232 /* Raise a signal as described by SIGNO an DETAIL, on the thread whose
233 sigstate SS points to. If SS is a null pointer, this instead affects
234 the calling thread. */
236 extern void _hurd_raise_signal (struct hurd_sigstate *ss, int signo,
237 const struct hurd_signal_detail *detail);
239 /* Translate a Mach exception into a signal (machine-dependent). */
241 extern void _hurd_exception2signal (struct hurd_signal_detail *detail,
245 /* Make the thread described by SS take the signal described by SIGNO and
246 DETAIL. If the process is traced, this will in fact stop with a SIGNO
247 as the stop signal unless UNTRACED is nonzero. When the signal can be
248 considered delivered, sends a sig_post reply message on REPLY_PORT
249 indicating success. SS is not locked. */
251 extern void _hurd_internal_post_signal (struct hurd_sigstate *ss,
253 struct hurd_signal_detail *detail,
254 mach_port_t reply_port,
255 mach_msg_type_name_t reply_port_type,
258 /* Set up STATE and SS to handle signal SIGNO by running HANDLER. If
259 RPC_WAIT is nonzero, the thread needs to wait for a pending RPC to
260 finish before running the signal handler. The handler is passed SIGNO,
261 SIGCODE, and the returned `struct sigcontext' (which resides on the
262 stack the handler will use, and which describes the state of the thread
263 encoded in STATE before running the handler). */
265 struct machine_thread_all_state;
266 extern struct sigcontext *
267 _hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
268 int signo, struct hurd_signal_detail *detail,
269 int rpc_wait, struct machine_thread_all_state *state);
271 /* Function run by the signal thread to receive from the signal port. */
273 extern void _hurd_msgport_receive (void);
275 /* Set up STATE with a thread state that, when resumed, is
276 like `longjmp (_hurd_sigthread_fault_env, 1)'. */
278 extern void _hurd_initialize_fault_recovery_state (void *state);
280 /* Set up STATE to do the equivalent of `longjmp (ENV, VAL);'. */
282 extern void _hurd_longjmp_thread_state (void *state, jmp_buf env, int value);
284 /* Function run for SIGINFO when its action is SIG_DFL and the current
285 process is the session leader. */
287 extern void _hurd_siginfo_handler (int);
289 /* Replacement for mach_msg used in RPCs to provide Hurd interruption
290 semantics. Args are all the same as for mach_msg. intr-rpc.h arranges
291 for this version to be used automatically by the RPC stubs the library
292 builds in place of the normal mach_msg. */
293 error_t _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
294 mach_msg_option_t option,
295 mach_msg_size_t send_size,
296 mach_msg_size_t rcv_size,
297 mach_port_t rcv_name,
298 mach_msg_timeout_t timeout,
302 /* Milliseconds to wait for an interruptible RPC to return after
303 `interrupt_operation'. */
305 extern mach_msg_timeout_t _hurd_interrupted_rpc_timeout;
308 /* Mask of signals that cannot be caught, blocked, or ignored. */
309 #define _SIG_CANT_MASK (__sigmask (SIGSTOP) | __sigmask (SIGKILL))
311 /* Do an RPC to a process's message port.
313 Each argument is an expression which returns an error code; each
314 expression may be evaluated several times. FETCH_MSGPORT_EXPR should
315 fetch the appropriate message port and store it in the local variable
316 `msgport'; it will be deallocated after use. FETCH_REFPORT_EXPR should
317 fetch the appropriate message port and store it in the local variable
318 `refport' (if no reference port is needed in the call, then
319 FETCH_REFPORT_EXPR should be simply KERN_SUCCESS or 0); if
320 DEALLOC_REFPORT evaluates to nonzero it will be deallocated after use,
321 otherwise the FETCH_REFPORT_EXPR must take care of user references to
322 `refport'. RPC_EXPR should perform the desired RPC operation using
323 `msgport' and `refport'.
325 The reason for the complexity is that a process's message port and
326 reference port may change between fetching those ports and completing an
327 RPC using them (usually they change only when a process execs). The RPC
328 will fail with MACH_SEND_INVALID_DEST if the msgport dies before we can
329 send the RPC request; or with MIG_SERVER_DIED if the msgport was
330 destroyed after we sent the RPC request but before it was serviced. In
331 either of these cases, we retry the entire operation, discarding the old
332 message and reference ports and fetch them anew. */
334 #define HURD_MSGPORT_RPC(fetch_msgport_expr, \
335 fetch_refport_expr, dealloc_refport, \
339 mach_port_t msgport, refport = MACH_PORT_NULL; \
342 /* Get the message port. */ \
343 if (__err = (fetch_msgport_expr)) \
345 /* Get the reference port. */ \
346 if (__err = (fetch_refport_expr)) \
348 /* Couldn't get it; deallocate MSGPORT and fail. */ \
349 __mach_port_deallocate (__mach_task_self (), msgport); \
352 __err = (rpc_expr); \
353 __mach_port_deallocate (__mach_task_self (), msgport); \
354 if ((dealloc_refport) && refport != MACH_PORT_NULL) \
355 __mach_port_deallocate (__mach_task_self (), refport); \
356 } while (__err == MACH_SEND_INVALID_DEST || \
357 __err == MIG_SERVER_DIED); \
362 #endif /* hurd/signal.h */