]> 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-2017 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 <http://www.gnu.org/licenses/>. */
18
19 #ifndef _HURD_SIGNAL_H
20
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
27
28 #define __need_size_t
29 #define __need_NULL
30 #include <stddef.h>
31
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>
39
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> */
45
46
47 /* Full details of a signal. */
48 struct hurd_signal_detail
49 {
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;
56 };
57
58
59 /* Per-thread signal state. */
60
61 struct hurd_sigstate
62 {
63 spin_lock_t critical_section_lock; /* Held if in critical section. */
64
65 spin_lock_t lock; /* Locks most of the rest of the structure. */
66
67 thread_t thread;
68 struct hurd_sigstate *next; /* Linked-list of thread sigstates. */
69
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;
74
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;
80
81 /* For each signal that may be pending, the details to deliver it with. */
82 struct hurd_signal_detail pending_data[NSIG];
83
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;
87
88 /* The following members are not locked. They are used only by this
89 thread, or by the signal thread with this thread suspended. */
90
91 volatile mach_port_t intr_port; /* Port interruptible RPC was sent on. */
92
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;
98
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;
103
104 /* These are locked normally. */
105 int cancel; /* Flag set by hurd_thread_cancel. */
106 void (*cancel_hook) (void); /* Called on cancellation. */
107 };
108
109 /* Linked list of states of all threads whose state has been asked for. */
110
111 extern struct hurd_sigstate *_hurd_sigstates;
112
113 extern struct mutex _hurd_siglock; /* Locks _hurd_sigstates. */
114
115 /* Get the sigstate of a given thread, taking its lock. */
116
117 extern struct hurd_sigstate *_hurd_thread_sigstate (thread_t);
118
119 /* Get the sigstate of the current thread.
120 This uses a per-thread variable to optimize the lookup. */
121
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__));
127
128 #ifndef _HURD_SIGNAL_H_EXTERN_INLINE
129 #define _HURD_SIGNAL_H_EXTERN_INLINE __extern_inline
130 #endif
131
132 _HURD_SIGNAL_H_EXTERN_INLINE struct hurd_sigstate *
133 _hurd_self_sigstate (void)
134 {
135 struct hurd_sigstate **location = (struct hurd_sigstate **)
136 (void *) __hurd_threadvar_location (_HURD_THREADVAR_SIGSTATE);
137 if (*location == NULL)
138 *location = _hurd_thread_sigstate (__mach_thread_self ());
139 return *location;
140 }
141 \f
142 /* Thread listening on our message port; also called the "signal thread". */
143
144 extern thread_t _hurd_msgport_thread;
145
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. */
148
149 extern mach_port_t _hurd_msgport;
150
151
152 /* Thread to receive process-global signals. */
153
154 extern thread_t _hurd_sigthread;
155
156
157 /* Resource limit on core file size. Enforced by hurdsig.c. */
158 extern int _hurd_core_limit;
159 \f
160 /* Critical sections.
161
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. */
166
167 _HURD_SIGNAL_H_EXTERN_INLINE void *
168 _hurd_critical_section_lock (void)
169 {
170 struct hurd_sigstate **location = (struct hurd_sigstate **)
171 (void *) __hurd_threadvar_location (_HURD_THREADVAR_SIGSTATE);
172 struct hurd_sigstate *ss = *location;
173 if (ss == NULL)
174 {
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. */
179 ss = *location = _hurd_thread_sigstate (__mach_thread_self ());
180 }
181
182 if (! __spin_try_lock (&ss->critical_section_lock))
183 /* We are already in a critical section, so do nothing. */
184 return NULL;
185
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. */
189 return ss;
190 }
191
192 _HURD_SIGNAL_H_EXTERN_INLINE void
193 _hurd_critical_section_unlock (void *our_lock)
194 {
195 if (our_lock == NULL)
196 /* The critical section lock was held when we began. Do nothing. */
197 return;
198 else
199 {
200 /* It was us who acquired the critical section lock. Unlock it. */
201 struct hurd_sigstate *ss = (struct hurd_sigstate *) our_lock;
202 sigset_t pending;
203 __spin_lock (&ss->lock);
204 __spin_unlock (&ss->critical_section_lock);
205 pending = ss->pending & ~ss->blocked;
206 __spin_unlock (&ss->lock);
207 if (! __sigisemptyset (&pending))
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 ());
212 }
213 }
214
215 /* Convenient macros for simple uses of critical sections.
216 These two must be used as a pair at the same C scoping level. */
217
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)
222 \f
223 /* Initialize the signal code, and start the signal thread.
224 Arguments give the "init ints" from exec_startup. */
225
226 extern void _hurdsig_init (const int *intarray, size_t intarraysize);
227
228 /* Initialize proc server-assisted fault recovery for the signal thread. */
229
230 extern void _hurdsig_fault_init (void);
231
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. */
235
236 extern int _hurd_raise_signal (struct hurd_sigstate *ss, int signo,
237 const struct hurd_signal_detail *detail);
238
239 /* Translate a Mach exception into a signal (machine-dependent). */
240
241 extern void _hurd_exception2signal (struct hurd_signal_detail *detail,
242 int *signo);
243
244
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. */
250
251 extern void _hurd_internal_post_signal (struct hurd_sigstate *ss,
252 int signo,
253 struct hurd_signal_detail *detail,
254 mach_port_t reply_port,
255 mach_msg_type_name_t reply_port_type,
256 int untraced);
257
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). */
264
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);
270
271 /* Function run by the signal thread to receive from the signal port. */
272
273 extern void _hurd_msgport_receive (void);
274
275 /* Set up STATE with a thread state that, when resumed, is
276 like `longjmp (_hurd_sigthread_fault_env, 1)'. */
277
278 extern void _hurd_initialize_fault_recovery_state (void *state);
279
280 /* Set up STATE to do the equivalent of `longjmp (ENV, VAL);'. */
281
282 extern void _hurd_longjmp_thread_state (void *state, jmp_buf env, int value);
283
284 /* Function run for SIGINFO when its action is SIG_DFL and the current
285 process is the session leader. */
286
287 extern void _hurd_siginfo_handler (int);
288
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,
299 mach_port_t notify);
300
301
302 /* Milliseconds to wait for an interruptible RPC to return after
303 `interrupt_operation'. */
304
305 extern mach_msg_timeout_t _hurd_interrupted_rpc_timeout;
306
307
308 /* Mask of signals that cannot be caught, blocked, or ignored. */
309 #define _SIG_CANT_MASK (__sigmask (SIGSTOP) | __sigmask (SIGKILL))
310
311 /* Do an RPC to a process's message port.
312
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'.
324
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. */
333
334 #define HURD_MSGPORT_RPC(fetch_msgport_expr, \
335 fetch_refport_expr, dealloc_refport, \
336 rpc_expr) \
337 ({ \
338 error_t __err; \
339 mach_port_t msgport, refport = MACH_PORT_NULL; \
340 do \
341 { \
342 /* Get the message port. */ \
343 __err = (error_t) (fetch_msgport_expr); \
344 if (__err) \
345 break; \
346 /* Get the reference port. */ \
347 __err = (error_t) (fetch_refport_expr); \
348 if (__err) \
349 { \
350 /* Couldn't get it; deallocate MSGPORT and fail. */ \
351 __mach_port_deallocate (__mach_task_self (), msgport); \
352 break; \
353 } \
354 __err = (error_t) (rpc_expr); \
355 __mach_port_deallocate (__mach_task_self (), msgport); \
356 if ((dealloc_refport) && refport != MACH_PORT_NULL) \
357 __mach_port_deallocate (__mach_task_self (), refport); \
358 } while (__err == MACH_SEND_INVALID_DEST || \
359 __err == MIG_SERVER_DIED); \
360 __err; \
361 })
362
363
364 #endif /* hurd/signal.h */