]>
Commit | Line | Data |
---|---|---|
dff8da6b | 1 | /* Copyright (C) 1991-2024 Free Software Foundation, Inc. |
2c6fe0bd | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
2c6fe0bd | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
5 | modify it under the terms of the GNU Lesser General Public |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
28f540f4 | 8 | |
2c6fe0bd UD |
9 | The GNU C Library is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
28f540f4 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. */ |
28f540f4 | 17 | |
dac7c640 | 18 | #include <assert.h> |
28f540f4 | 19 | #include <stdio.h> |
64659255 | 20 | #include <stdlib.h> |
28f540f4 | 21 | #include <string.h> |
64659255 | 22 | |
9446e02b | 23 | #include <lock-intern.h> /* For `struct mutex'. */ |
0f652f05 | 24 | #include <pthreadP.h> |
64659255 | 25 | #include <mach.h> |
aa19c68d | 26 | #include <mach/setup-thread.h> |
64659255 | 27 | #include <mach/thread_switch.h> |
7a8f45e3 | 28 | #include <mach/mig_support.h> |
9446e02b | 29 | #include <mach/vm_param.h> |
64659255 MK |
30 | |
31 | #include <hurd.h> | |
762a2918 | 32 | #include <hurd/id.h> |
64659255 MK |
33 | #include <hurd/signal.h> |
34 | ||
28f540f4 RM |
35 | #include "hurdfault.h" |
36 | #include "hurdmalloc.h" /* XXX */ | |
cc13edc8 | 37 | #include "../locale/localeinfo.h" |
28f540f4 | 38 | |
e8ef51b1 ST |
39 | #include <libc-diag.h> |
40 | ||
28f540f4 RM |
41 | const char *_hurdsig_getenv (const char *); |
42 | ||
43 | struct mutex _hurd_siglock; | |
44 | int _hurd_stopped; | |
45 | ||
46 | /* Port that receives signals and other miscellaneous messages. */ | |
47 | mach_port_t _hurd_msgport; | |
48 | ||
49 | /* Thread listening on it. */ | |
50 | thread_t _hurd_msgport_thread; | |
51 | ||
6bac11d9 MB |
52 | /* These are set up by _hurdsig_init. */ |
53 | unsigned long int __hurd_sigthread_stack_base; | |
54 | unsigned long int __hurd_sigthread_stack_end; | |
6bac11d9 | 55 | |
28f540f4 RM |
56 | /* Linked-list of per-thread signal state. */ |
57 | struct hurd_sigstate *_hurd_sigstates; | |
54da5be3 | 58 | |
653d74f1 JK |
59 | /* Sigstate for the task-global signals. */ |
60 | struct hurd_sigstate *_hurd_global_sigstate; | |
61 | ||
54da5be3 | 62 | /* Timeout for RPC's after interrupt_operation. */ |
46a7f24c | 63 | mach_msg_timeout_t _hurd_interrupted_rpc_timeout = 60000; |
28f540f4 RM |
64 | \f |
65 | static void | |
66 | default_sigaction (struct sigaction actions[NSIG]) | |
67 | { | |
68 | int signo; | |
69 | ||
70 | __sigemptyset (&actions[0].sa_mask); | |
71 | actions[0].sa_flags = SA_RESTART; | |
72 | actions[0].sa_handler = SIG_DFL; | |
73 | ||
74 | for (signo = 1; signo < NSIG; ++signo) | |
75 | actions[signo] = actions[0]; | |
76 | } | |
77 | ||
78 | struct hurd_sigstate * | |
79 | _hurd_thread_sigstate (thread_t thread) | |
80 | { | |
81 | struct hurd_sigstate *ss; | |
82 | __mutex_lock (&_hurd_siglock); | |
83 | for (ss = _hurd_sigstates; ss != NULL; ss = ss->next) | |
84 | if (ss->thread == thread) | |
b96bdcd7 | 85 | break; |
28f540f4 RM |
86 | if (ss == NULL) |
87 | { | |
88 | ss = malloc (sizeof (*ss)); | |
89 | if (ss == NULL) | |
653d74f1 | 90 | __libc_fatal ("hurd: Can't allocate sigstate\n"); |
61416e19 | 91 | __spin_lock_init (&ss->critical_section_lock); |
28f540f4 | 92 | __spin_lock_init (&ss->lock); |
61416e19 | 93 | ss->thread = thread; |
28f540f4 | 94 | |
6df1b247 | 95 | /* Initialize default state. */ |
28f540f4 RM |
96 | __sigemptyset (&ss->blocked); |
97 | __sigemptyset (&ss->pending); | |
98 | memset (&ss->sigaltstack, 0, sizeof (ss->sigaltstack)); | |
a0bb5abd | 99 | ss->sigaltstack.ss_flags |= SS_DISABLE; |
10dc2a90 | 100 | ss->preemptors = NULL; |
e8bceaee | 101 | ss->suspended = MACH_PORT_NULL; |
28f540f4 RM |
102 | ss->intr_port = MACH_PORT_NULL; |
103 | ss->context = NULL; | |
61416e19 ST |
104 | ss->active_resources = NULL; |
105 | ss->cancel = 0; | |
106 | ss->cancel_hook = NULL; | |
28f540f4 | 107 | |
653d74f1 JK |
108 | if (thread == MACH_PORT_NULL) |
109 | { | |
110 | /* Process-wide sigstate, use the system defaults. */ | |
111 | default_sigaction (ss->actions); | |
112 | ||
113 | /* The global sigstate is not added to the _hurd_sigstates list. | |
114 | It is created with _hurd_thread_sigstate (MACH_PORT_NULL) | |
115 | but should be accessed through _hurd_global_sigstate. */ | |
116 | } | |
28f540f4 RM |
117 | else |
118 | { | |
5c06743c RB |
119 | error_t err; |
120 | ||
653d74f1 JK |
121 | /* Use the global actions as a default for new threads. */ |
122 | struct hurd_sigstate *s = _hurd_global_sigstate; | |
28f540f4 RM |
123 | if (s) |
124 | { | |
125 | __spin_lock (&s->lock); | |
126 | memcpy (ss->actions, s->actions, sizeof (s->actions)); | |
127 | __spin_unlock (&s->lock); | |
128 | } | |
129 | else | |
130 | default_sigaction (ss->actions); | |
28f540f4 | 131 | |
653d74f1 JK |
132 | ss->next = _hurd_sigstates; |
133 | _hurd_sigstates = ss; | |
5c06743c RB |
134 | |
135 | err = __mach_port_mod_refs (__mach_task_self (), thread, | |
136 | MACH_PORT_RIGHT_SEND, 1); | |
137 | if (err) | |
138 | __libc_fatal ("hurd: Can't add reference on Mach thread\n"); | |
653d74f1 | 139 | } |
28f540f4 RM |
140 | } |
141 | __mutex_unlock (&_hurd_siglock); | |
142 | return ss; | |
143 | } | |
7a8f45e3 | 144 | libc_hidden_def (_hurd_thread_sigstate) |
653d74f1 JK |
145 | |
146 | /* Destroy a sigstate structure. Called by libpthread just before the | |
5c06743c | 147 | * corresponding thread is terminated. */ |
653d74f1 JK |
148 | void |
149 | _hurd_sigstate_delete (thread_t thread) | |
150 | { | |
151 | struct hurd_sigstate **ssp, *ss; | |
152 | ||
153 | __mutex_lock (&_hurd_siglock); | |
154 | for (ssp = &_hurd_sigstates; *ssp; ssp = &(*ssp)->next) | |
155 | if ((*ssp)->thread == thread) | |
156 | break; | |
157 | ||
158 | ss = *ssp; | |
159 | if (ss) | |
160 | *ssp = ss->next; | |
161 | ||
162 | __mutex_unlock (&_hurd_siglock); | |
163 | if (ss) | |
5c06743c RB |
164 | { |
165 | if (ss->thread != MACH_PORT_NULL) | |
166 | __mach_port_deallocate (__mach_task_self (), ss->thread); | |
167 | ||
168 | free (ss); | |
169 | } | |
653d74f1 JK |
170 | } |
171 | ||
172 | /* Make SS a global receiver, with pthread signal semantics. */ | |
173 | void | |
174 | _hurd_sigstate_set_global_rcv (struct hurd_sigstate *ss) | |
175 | { | |
176 | assert (ss->thread != MACH_PORT_NULL); | |
177 | ss->actions[0].sa_handler = SIG_IGN; | |
178 | } | |
3fd996d3 | 179 | libc_hidden_def (_hurd_sigstate_set_global_rcv) |
653d74f1 JK |
180 | |
181 | /* Check whether SS is a global receiver. */ | |
182 | static int | |
183 | sigstate_is_global_rcv (const struct hurd_sigstate *ss) | |
184 | { | |
185 | return (_hurd_global_sigstate != NULL) | |
186 | && (ss->actions[0].sa_handler == SIG_IGN); | |
187 | } | |
188 | libc_hidden_def (_hurd_sigstate_delete) | |
189 | ||
190 | /* Lock/unlock a hurd_sigstate structure. If the accessors below require | |
191 | it, the global sigstate will be locked as well. */ | |
192 | void | |
193 | _hurd_sigstate_lock (struct hurd_sigstate *ss) | |
194 | { | |
195 | if (sigstate_is_global_rcv (ss)) | |
196 | __spin_lock (&_hurd_global_sigstate->lock); | |
197 | __spin_lock (&ss->lock); | |
198 | } | |
3fd996d3 SB |
199 | libc_hidden_def (_hurd_sigstate_lock) |
200 | ||
653d74f1 JK |
201 | void |
202 | _hurd_sigstate_unlock (struct hurd_sigstate *ss) | |
203 | { | |
204 | __spin_unlock (&ss->lock); | |
205 | if (sigstate_is_global_rcv (ss)) | |
206 | __spin_unlock (&_hurd_global_sigstate->lock); | |
207 | } | |
3fd996d3 | 208 | libc_hidden_def (_hurd_sigstate_unlock) |
653d74f1 | 209 | |
0d41182e | 210 | /* Retrieve a thread's full set of pending signals, including the global |
653d74f1 JK |
211 | ones if appropriate. SS must be locked. */ |
212 | sigset_t | |
213 | _hurd_sigstate_pending (const struct hurd_sigstate *ss) | |
214 | { | |
215 | sigset_t pending = ss->pending; | |
216 | if (sigstate_is_global_rcv (ss)) | |
217 | __sigorset (&pending, &pending, &_hurd_global_sigstate->pending); | |
218 | return pending; | |
219 | } | |
3fd996d3 | 220 | libc_hidden_def (_hurd_sigstate_pending) |
653d74f1 JK |
221 | |
222 | /* Clear a pending signal and return the associated detailed | |
223 | signal information. SS must be locked, and must have signal SIGNO | |
224 | pending, either directly or through the global sigstate. */ | |
225 | static struct hurd_signal_detail | |
226 | sigstate_clear_pending (struct hurd_sigstate *ss, int signo) | |
227 | { | |
228 | if (sigstate_is_global_rcv (ss) | |
229 | && __sigismember (&_hurd_global_sigstate->pending, signo)) | |
230 | { | |
231 | __sigdelset (&_hurd_global_sigstate->pending, signo); | |
232 | return _hurd_global_sigstate->pending_data[signo]; | |
233 | } | |
234 | ||
235 | assert (__sigismember (&ss->pending, signo)); | |
236 | __sigdelset (&ss->pending, signo); | |
237 | return ss->pending_data[signo]; | |
238 | } | |
653d74f1 | 239 | |
0d41182e | 240 | /* Retrieve a thread's action vector. SS must be locked. */ |
653d74f1 JK |
241 | struct sigaction * |
242 | _hurd_sigstate_actions (struct hurd_sigstate *ss) | |
243 | { | |
244 | if (sigstate_is_global_rcv (ss)) | |
245 | return _hurd_global_sigstate->actions; | |
246 | else | |
247 | return ss->actions; | |
248 | } | |
653d74f1 | 249 | |
28f540f4 RM |
250 | \f |
251 | /* Signal delivery itself is on this page. */ | |
252 | ||
253 | #include <hurd/fd.h> | |
9fd18b6c | 254 | #include <hurd/crash.h> |
79c236ac | 255 | #include <hurd/resource.h> |
28f540f4 RM |
256 | #include <hurd/paths.h> |
257 | #include <setjmp.h> | |
258 | #include <fcntl.h> | |
259 | #include <sys/wait.h> | |
8f480b4b | 260 | #include <thread_state.h> |
28f540f4 RM |
261 | #include <hurd/msg_server.h> |
262 | #include <hurd/msg_reply.h> /* For __msg_sig_post_reply. */ | |
28f540f4 | 263 | #include <hurd/interrupt.h> |
fb8e70d6 | 264 | #include <unistd.h> |
28f540f4 | 265 | |
28f540f4 | 266 | |
9fd18b6c | 267 | /* Call the crash dump server to mummify us before we die. |
28f540f4 RM |
268 | Returns nonzero if a core file was written. */ |
269 | static int | |
0e3426bb | 270 | write_corefile (int signo, const struct hurd_signal_detail *detail) |
28f540f4 RM |
271 | { |
272 | error_t err; | |
273 | mach_port_t coreserver; | |
274 | file_t file, coredir; | |
275 | const char *name; | |
276 | ||
79c236ac RM |
277 | /* Don't bother locking since we just read the one word. */ |
278 | rlim_t corelimit = _hurd_rlimits[RLIMIT_CORE].rlim_cur; | |
279 | ||
280 | if (corelimit == 0) | |
281 | /* No core dumping, thank you very much. Note that this makes | |
282 | `ulimit -c 0' prevent crash-suspension too, which is probably | |
283 | what the user wanted. */ | |
284 | return 0; | |
285 | ||
28f540f4 RM |
286 | /* XXX RLIMIT_CORE: |
287 | When we have a protocol to make the server return an error | |
288 | for RLIMIT_FSIZE, then tell the corefile fs server the RLIMIT_CORE | |
289 | value in place of the RLIMIT_FSIZE value. */ | |
290 | ||
291 | /* First get a port to the core dumping server. */ | |
292 | coreserver = MACH_PORT_NULL; | |
9fd18b6c | 293 | name = _hurdsig_getenv ("CRASHSERVER"); |
28f540f4 RM |
294 | if (name != NULL) |
295 | coreserver = __file_name_lookup (name, 0, 0); | |
296 | if (coreserver == MACH_PORT_NULL) | |
9fd18b6c | 297 | coreserver = __file_name_lookup (_SERVERS_CRASH, 0, 0); |
28f540f4 RM |
298 | if (coreserver == MACH_PORT_NULL) |
299 | return 0; | |
300 | ||
301 | /* Get a port to the directory where the new core file will reside. */ | |
78abf563 | 302 | file = MACH_PORT_NULL; |
28f540f4 RM |
303 | name = _hurdsig_getenv ("COREFILE"); |
304 | if (name == NULL) | |
305 | name = "core"; | |
306 | coredir = __file_name_split (name, (char **) &name); | |
78abf563 TBB |
307 | if (coredir != MACH_PORT_NULL) |
308 | /* Create the new file, but don't link it into the directory yet. */ | |
309 | __dir_mkfile (coredir, O_WRONLY|O_CREAT, | |
310 | 0600 & ~_hurd_umask, /* XXX ? */ | |
0f110f41 | 311 | &file); |
28f540f4 RM |
312 | |
313 | /* Call the core dumping server to write the core file. */ | |
9fd18b6c RM |
314 | err = __crash_dump_task (coreserver, |
315 | __mach_task_self (), | |
0e3426bb RM |
316 | file, |
317 | signo, detail->code, detail->error, | |
318 | detail->exc, detail->exc_code, detail->exc_subcode, | |
319 | _hurd_ports[INIT_PORT_CTTYID].port, | |
320 | MACH_MSG_TYPE_COPY_SEND); | |
28f540f4 | 321 | __mach_port_deallocate (__mach_task_self (), coreserver); |
78abf563 TBB |
322 | |
323 | if (! err && file != MACH_PORT_NULL) | |
28f540f4 RM |
324 | /* The core dump into FILE succeeded, so now link it into the |
325 | directory. */ | |
70481be8 | 326 | err = __dir_link (coredir, file, name, 1); |
28f540f4 RM |
327 | __mach_port_deallocate (__mach_task_self (), file); |
328 | __mach_port_deallocate (__mach_task_self (), coredir); | |
78abf563 | 329 | return !err && file != MACH_PORT_NULL; |
28f540f4 RM |
330 | } |
331 | ||
332 | ||
28f540f4 RM |
333 | /* The lowest-numbered thread state flavor value is 1, |
334 | so we use bit 0 in machine_thread_all_state.set to | |
335 | record whether we have done thread_abort. */ | |
336 | #define THREAD_ABORTED 1 | |
337 | ||
1a3a58fd | 338 | /* SS->thread is suspended. Abort the thread and get its basic state. */ |
28f540f4 RM |
339 | static void |
340 | abort_thread (struct hurd_sigstate *ss, struct machine_thread_all_state *state, | |
1a3a58fd | 341 | void (*reply) (void)) |
28f540f4 | 342 | { |
653d74f1 JK |
343 | assert (ss->thread != MACH_PORT_NULL); |
344 | ||
28f540f4 RM |
345 | if (!(state->set & THREAD_ABORTED)) |
346 | { | |
b96bdcd7 RM |
347 | error_t err = __thread_abort (ss->thread); |
348 | assert_perror (err); | |
28f540f4 RM |
349 | /* Clear all thread state flavor set bits, because thread_abort may |
350 | have changed the state. */ | |
351 | state->set = THREAD_ABORTED; | |
352 | } | |
353 | ||
1a3a58fd RM |
354 | if (reply) |
355 | (*reply) (); | |
28f540f4 RM |
356 | |
357 | machine_get_basic_state (ss->thread, state); | |
358 | } | |
359 | ||
360 | /* Find the location of the MiG reply port cell in use by the thread whose | |
54da5be3 RM |
361 | state is described by THREAD_STATE. If SIGTHREAD is nonzero, make sure |
362 | that this location can be set without faulting, or else return NULL. */ | |
28f540f4 RM |
363 | |
364 | static mach_port_t * | |
dc33bef3 ST |
365 | interrupted_reply_port_location (thread_t thread, |
366 | struct machine_thread_all_state *thread_state, | |
54da5be3 | 367 | int sigthread) |
28f540f4 | 368 | { |
dc33bef3 | 369 | mach_port_t *portloc = &THREAD_TCB(thread, thread_state)->reply_port; |
28f540f4 | 370 | |
fb8e70d6 | 371 | if (sigthread && _hurdsig_catch_memory_fault (portloc)) |
dc33bef3 | 372 | /* Faulted trying to read the TCB. */ |
fb8e70d6 | 373 | return NULL; |
28f540f4 | 374 | |
e8ef51b1 ST |
375 | DIAG_PUSH_NEEDS_COMMENT; |
376 | /* GCC 6 and before seem to be confused by the setjmp call inside | |
377 | _hurdsig_catch_memory_fault and think that we may be returning a second | |
378 | time to here with portloc uninitialized (but we never do). */ | |
379 | DIAG_IGNORE_NEEDS_COMMENT (6, "-Wmaybe-uninitialized"); | |
28f540f4 RM |
380 | /* Fault now if this pointer is bogus. */ |
381 | *(volatile mach_port_t *) portloc = *portloc; | |
e8ef51b1 | 382 | DIAG_POP_NEEDS_COMMENT; |
28f540f4 | 383 | |
54da5be3 RM |
384 | if (sigthread) |
385 | _hurdsig_end_catch_fault (); | |
28f540f4 RM |
386 | |
387 | return portloc; | |
388 | } | |
3fe9de0d | 389 | \f |
6df1b247 | 390 | #include <hurd/sigpreempt.h> |
8f480b4b | 391 | #include <intr-msg.h> |
28f540f4 | 392 | |
d3dc731b MB |
393 | /* Timeout on interrupt_operation calls. */ |
394 | mach_msg_timeout_t _hurdsig_interrupt_timeout = 1000; | |
395 | ||
28f540f4 RM |
396 | /* SS->thread is suspended. |
397 | ||
398 | Abort any interruptible RPC operation the thread is doing. | |
399 | ||
400 | This uses only the constant member SS->thread and the unlocked, atomically | |
401 | set member SS->intr_port, so no locking is needed. | |
402 | ||
403 | If successfully sent an interrupt_operation and therefore the thread should | |
404 | wait for its pending RPC to return (possibly EINTR) before taking the | |
405 | incoming signal, returns the reply port to be received on. Otherwise | |
406 | returns MACH_PORT_NULL. | |
407 | ||
75914335 RM |
408 | SIGNO is used to find the applicable SA_RESTART bit. If SIGNO is zero, |
409 | the RPC fails with EINTR instead of restarting (thread_cancel). | |
410 | ||
28f540f4 RM |
411 | *STATE_CHANGE is set nonzero if STATE->basic was modified and should |
412 | be applied back to the thread if it might ever run again, else zero. */ | |
413 | ||
191abc51 | 414 | mach_port_t |
75914335 | 415 | _hurdsig_abort_rpcs (struct hurd_sigstate *ss, int signo, int sigthread, |
54da5be3 | 416 | struct machine_thread_all_state *state, int *state_change, |
1a3a58fd | 417 | void (*reply) (void)) |
28f540f4 | 418 | { |
5c81be53 | 419 | extern const void _hurd_intr_rpc_msg_about_to; |
60b21327 | 420 | extern const void _hurd_intr_rpc_msg_setup_done; |
3fe9de0d | 421 | extern const void _hurd_intr_rpc_msg_in_trap; |
54da5be3 | 422 | mach_port_t rcv_port = MACH_PORT_NULL; |
28f540f4 RM |
423 | mach_port_t intr_port; |
424 | ||
425 | *state_change = 0; | |
426 | ||
427 | intr_port = ss->intr_port; | |
428 | if (intr_port == MACH_PORT_NULL) | |
429 | /* No interruption needs done. */ | |
430 | return MACH_PORT_NULL; | |
431 | ||
432 | /* Abort the thread's kernel context, so any pending message send or | |
433 | receive completes immediately or aborts. */ | |
1a3a58fd | 434 | abort_thread (ss, state, reply); |
28f540f4 | 435 | |
32fff41b SB |
436 | if (state->basic.PC >= (uintptr_t) &_hurd_intr_rpc_msg_about_to |
437 | && state->basic.PC < (uintptr_t) &_hurd_intr_rpc_msg_in_trap) | |
28f540f4 | 438 | { |
54da5be3 | 439 | /* The thread is about to do the RPC, but hasn't yet entered |
60b21327 SB |
440 | mach_msg. Importantly, it may have already checked ss->cancel for |
441 | the last time before doing the RPC, so setting that is not enough | |
442 | to make it not enter mach_msg. Instead, mutate the thread's state | |
443 | so it knows not to try the RPC. | |
444 | ||
445 | If the thread is past _hurd_intr_rpc_msg_setup_done, just make it | |
446 | jump to after the trap, since we know it's safe to do so. Otherwise, | |
447 | we know that the thread is yet to check for the MACH_SEND_INTERRUPTED | |
448 | value we set below, and will skip the trap by itself. */ | |
449 | if (state->basic.PC >= (uintptr_t) &_hurd_intr_rpc_msg_setup_done) | |
450 | MACHINE_THREAD_STATE_SET_PC (&state->basic, | |
451 | &_hurd_intr_rpc_msg_in_trap); | |
54da5be3 RM |
452 | state->basic.SYSRETURN = MACH_SEND_INTERRUPTED; |
453 | *state_change = 1; | |
28f540f4 | 454 | } |
32fff41b | 455 | else if (state->basic.PC == (uintptr_t) &_hurd_intr_rpc_msg_in_trap |
0d41182e SB |
456 | /* The thread was blocked in the system call. After thread_abort, |
457 | the return value register indicates what state the RPC was in | |
458 | when interrupted. */ | |
459 | && state->basic.SYSRETURN == MACH_RCV_INTERRUPTED) | |
460 | { | |
461 | /* The RPC request message was sent and the thread was waiting for the | |
462 | reply message; now the message receive has been aborted, so the | |
463 | mach_msg call will return MACH_RCV_INTERRUPTED. We must tell the | |
464 | server to interrupt the pending operation. The thread must wait for | |
465 | the reply message before running the signal handler (to guarantee that | |
466 | the operation has finished being interrupted), so our nonzero return | |
467 | tells the trampoline code to finish the message receive operation | |
468 | before running the handler. */ | |
469 | ||
470 | mach_port_t *reply = interrupted_reply_port_location (ss->thread, | |
471 | state, | |
472 | sigthread); | |
473 | error_t err = __interrupt_operation (intr_port, | |
474 | _hurdsig_interrupt_timeout); | |
475 | ||
476 | if (err) | |
477 | { | |
478 | if (reply) | |
479 | { | |
480 | /* The interrupt didn't work. | |
4e506f67 SB |
481 | Destroy the receive right the thread is blocked on, and |
482 | replace it with a dead name to keep the name from reuse until | |
483 | the therad is done with it. To do this atomically, first | |
484 | insert a send right, and then destroy the receive right, | |
485 | turning the send right into a dead name. */ | |
486 | err = __mach_port_insert_right (__mach_task_self (), | |
487 | *reply, *reply, | |
488 | MACH_MSG_TYPE_MAKE_SEND); | |
489 | assert_perror (err); | |
490 | err = __mach_port_mod_refs (__mach_task_self (), *reply, | |
491 | MACH_PORT_RIGHT_RECEIVE, -1); | |
492 | assert_perror (err); | |
0d41182e SB |
493 | } |
494 | ||
495 | /* The system call return value register now contains | |
496 | MACH_RCV_INTERRUPTED; when mach_msg resumes, it will retry the | |
497 | call. Since we have just destroyed the receive right, the retry | |
498 | will fail with MACH_RCV_INVALID_NAME. Instead, just change the | |
499 | return value here to EINTR so mach_msg will not retry and the | |
500 | EINTR error code will propagate up. */ | |
501 | state->basic.SYSRETURN = EINTR; | |
502 | *state_change = 1; | |
503 | } | |
504 | else if (reply) | |
505 | rcv_port = *reply; | |
506 | ||
507 | /* All threads whose RPCs were interrupted by the interrupt_operation | |
508 | call above will retry their RPCs unless we clear SS->intr_port. So we | |
509 | clear it for the thread taking a signal when SA_RESTART is clear, so | |
510 | that its call returns EINTR. */ | |
511 | if (! signo || !(_hurd_sigstate_actions (ss) [signo].sa_flags & SA_RESTART)) | |
512 | ss->intr_port = MACH_PORT_NULL; | |
513 | } | |
28f540f4 | 514 | |
54da5be3 | 515 | return rcv_port; |
28f540f4 RM |
516 | } |
517 | ||
54da5be3 | 518 | |
28f540f4 RM |
519 | /* Abort the RPCs being run by all threads but this one; |
520 | all other threads should be suspended. If LIVE is nonzero, those | |
521 | threads may run again, so they should be adjusted as necessary to be | |
522 | happy when resumed. STATE is clobbered as a scratch area; its initial | |
523 | contents are ignored, and its contents on return are not useful. */ | |
524 | ||
525 | static void | |
526 | abort_all_rpcs (int signo, struct machine_thread_all_state *state, int live) | |
527 | { | |
528 | /* We can just loop over the sigstates. Any thread doing something | |
529 | interruptible must have one. We needn't bother locking because all | |
530 | other threads are stopped. */ | |
531 | ||
532 | struct hurd_sigstate *ss; | |
533 | size_t nthreads; | |
534 | mach_port_t *reply_ports; | |
535 | ||
536 | /* First loop over the sigstates to count them. | |
537 | We need to know how big a vector we will need for REPLY_PORTS. */ | |
538 | nthreads = 0; | |
539 | for (ss = _hurd_sigstates; ss != NULL; ss = ss->next) | |
540 | ++nthreads; | |
541 | ||
542 | reply_ports = alloca (nthreads * sizeof *reply_ports); | |
543 | ||
544 | nthreads = 0; | |
fb8e70d6 | 545 | for (ss = _hurd_sigstates; ss != NULL; ss = ss->next, ++nthreads) |
28f540f4 | 546 | if (ss->thread == _hurd_msgport_thread) |
fb8e70d6 | 547 | reply_ports[nthreads] = MACH_PORT_NULL; |
28f540f4 RM |
548 | else |
549 | { | |
550 | int state_changed; | |
551 | state->set = 0; /* Reset scratch area. */ | |
552 | ||
553 | /* Abort any operation in progress with interrupt_operation. | |
554 | Record the reply port the thread is waiting on. | |
555 | We will wait for all the replies below. */ | |
fb8e70d6 RM |
556 | reply_ports[nthreads] = _hurdsig_abort_rpcs (ss, signo, 1, |
557 | state, &state_changed, | |
558 | NULL); | |
559 | if (live) | |
560 | { | |
561 | if (reply_ports[nthreads] != MACH_PORT_NULL) | |
562 | { | |
563 | /* We will wait for the reply to this RPC below, so the | |
564 | thread must issue a new RPC rather than waiting for the | |
565 | reply to the one it sent. */ | |
566 | state->basic.SYSRETURN = EINTR; | |
567 | state_changed = 1; | |
568 | } | |
569 | if (state_changed) | |
570 | /* Aborting the RPC needed to change this thread's state, | |
571 | and it might ever run again. So write back its state. */ | |
572 | __thread_set_state (ss->thread, MACHINE_THREAD_STATE_FLAVOR, | |
573 | (natural_t *) &state->basic, | |
574 | MACHINE_THREAD_STATE_COUNT); | |
575 | } | |
28f540f4 RM |
576 | } |
577 | ||
578 | /* Wait for replies from all the successfully interrupted RPCs. */ | |
579 | while (nthreads-- > 0) | |
580 | if (reply_ports[nthreads] != MACH_PORT_NULL) | |
581 | { | |
582 | error_t err; | |
583 | mach_msg_header_t head; | |
54da5be3 | 584 | err = __mach_msg (&head, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof head, |
28f540f4 | 585 | reply_ports[nthreads], |
54da5be3 RM |
586 | _hurd_interrupted_rpc_timeout, MACH_PORT_NULL); |
587 | switch (err) | |
588 | { | |
589 | case MACH_RCV_TIMED_OUT: | |
590 | case MACH_RCV_TOO_LARGE: | |
591 | break; | |
592 | ||
593 | default: | |
594 | assert_perror (err); | |
595 | } | |
28f540f4 RM |
596 | } |
597 | } | |
598 | ||
f9011787 ST |
599 | /* Wake up any sigsuspend or pselect call that is blocking SS->thread. SS must |
600 | be locked. */ | |
4288c548 JK |
601 | static void |
602 | wake_sigsuspend (struct hurd_sigstate *ss) | |
603 | { | |
604 | error_t err; | |
605 | mach_msg_header_t msg; | |
606 | ||
607 | if (ss->suspended == MACH_PORT_NULL) | |
608 | return; | |
609 | ||
610 | /* There is a sigsuspend waiting. Tell it to wake up. */ | |
611 | msg.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_MAKE_SEND, 0); | |
612 | msg.msgh_remote_port = ss->suspended; | |
613 | msg.msgh_local_port = MACH_PORT_NULL; | |
614 | /* These values do not matter. */ | |
615 | msg.msgh_id = 8675309; /* Jenny, Jenny. */ | |
616 | ss->suspended = MACH_PORT_NULL; | |
617 | err = __mach_msg (&msg, MACH_SEND_MSG, sizeof msg, 0, | |
618 | MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, | |
619 | MACH_PORT_NULL); | |
620 | assert_perror (err); | |
621 | } | |
622 | ||
43b0e40f | 623 | struct hurd_signal_preemptor *_hurdsig_preemptors = 0; |
6df1b247 | 624 | sigset_t _hurdsig_preempted_set; |
28f540f4 | 625 | |
6bac11d9 MB |
626 | /* XXX temporary to deal with spelling fix */ |
627 | weak_alias (_hurdsig_preemptors, _hurdsig_preempters) | |
628 | ||
28f540f4 | 629 | /* Mask of stop signals. */ |
a9175662 FW |
630 | #define STOPSIGS (__sigmask (SIGTTIN) | __sigmask (SIGTTOU) \ |
631 | | __sigmask (SIGSTOP) | __sigmask (SIGTSTP)) | |
28f540f4 | 632 | |
4288c548 | 633 | /* Actual delivery of a single signal. Called with SS unlocked. When |
653d74f1 JK |
634 | the signal is delivered, return SS, locked (or, if SS was originally |
635 | _hurd_global_sigstate, the sigstate of the actual thread the signal | |
636 | was delivered to). If the signal is being traced, return NULL with | |
637 | SS unlocked. */ | |
638 | static struct hurd_sigstate * | |
4288c548 JK |
639 | post_signal (struct hurd_sigstate *ss, |
640 | int signo, struct hurd_signal_detail *detail, | |
641 | int untraced, void (*reply) (void)) | |
28f540f4 RM |
642 | { |
643 | struct machine_thread_all_state thread_state; | |
644 | enum { stop, ignore, core, term, handle } act; | |
28f540f4 RM |
645 | int ss_suspended; |
646 | ||
e42efa01 ST |
647 | /* sigaction for preemptors */ |
648 | struct sigaction preempt_sigaction = { | |
649 | .sa_flags = SA_RESTART | |
650 | }; | |
651 | ||
652 | struct sigaction *action; | |
653 | ||
28f540f4 RM |
654 | /* Mark the signal as pending. */ |
655 | void mark_pending (void) | |
656 | { | |
657 | __sigaddset (&ss->pending, signo); | |
93a470c7 | 658 | /* Save the details to be given to the handler when SIGNO is |
28f540f4 | 659 | unblocked. */ |
93a470c7 | 660 | ss->pending_data[signo] = *detail; |
28f540f4 RM |
661 | } |
662 | ||
663 | /* Suspend the process with SIGNO. */ | |
664 | void suspend (void) | |
665 | { | |
666 | /* Stop all other threads and mark ourselves stopped. */ | |
667 | __USEPORT (PROC, | |
668 | ({ | |
669 | /* Hold the siglock while stopping other threads to be | |
670 | sure it is not held by another thread afterwards. */ | |
671 | __mutex_lock (&_hurd_siglock); | |
672 | __proc_dostop (port, _hurd_msgport_thread); | |
673 | __mutex_unlock (&_hurd_siglock); | |
674 | abort_all_rpcs (signo, &thread_state, 1); | |
6a7169a5 | 675 | reply (); |
0e3426bb | 676 | __proc_mark_stop (port, signo, detail->code); |
28f540f4 RM |
677 | })); |
678 | _hurd_stopped = 1; | |
679 | } | |
75914335 RM |
680 | /* Resume the process after a suspension. */ |
681 | void resume (void) | |
682 | { | |
683 | /* Resume the process from being stopped. */ | |
684 | thread_t *threads; | |
685 | mach_msg_type_number_t nthreads, i; | |
686 | error_t err; | |
687 | ||
688 | if (! _hurd_stopped) | |
689 | return; | |
690 | ||
691 | /* Tell the proc server we are continuing. */ | |
692 | __USEPORT (PROC, __proc_mark_cont (port)); | |
693 | /* Fetch ports to all our threads and resume them. */ | |
694 | err = __task_threads (__mach_task_self (), &threads, &nthreads); | |
695 | assert_perror (err); | |
696 | for (i = 0; i < nthreads; ++i) | |
697 | { | |
653d74f1 JK |
698 | if (act == handle && threads[i] == ss->thread) |
699 | { | |
700 | /* The thread that will run the handler is kept suspended. */ | |
701 | ss_suspended = 1; | |
702 | } | |
703 | else if (threads[i] != _hurd_msgport_thread) | |
75914335 RM |
704 | { |
705 | err = __thread_resume (threads[i]); | |
706 | assert_perror (err); | |
707 | } | |
708 | err = __mach_port_deallocate (__mach_task_self (), | |
709 | threads[i]); | |
710 | assert_perror (err); | |
711 | } | |
712 | __vm_deallocate (__mach_task_self (), | |
713 | (vm_address_t) threads, | |
714 | nthreads * sizeof *threads); | |
715 | _hurd_stopped = 0; | |
75914335 RM |
716 | } |
717 | ||
4288c548 JK |
718 | error_t err; |
719 | sighandler_t handler; | |
720 | ||
75914335 RM |
721 | if (signo == 0) |
722 | { | |
723 | if (untraced) | |
4288c548 JK |
724 | { |
725 | /* This is PTRACE_CONTINUE. */ | |
726 | act = ignore; | |
727 | resume (); | |
728 | } | |
75914335 RM |
729 | |
730 | /* This call is just to check for pending signals. */ | |
653d74f1 JK |
731 | _hurd_sigstate_lock (ss); |
732 | return ss; | |
75914335 | 733 | } |
28f540f4 | 734 | |
28f540f4 RM |
735 | thread_state.set = 0; /* We know nothing. */ |
736 | ||
653d74f1 JK |
737 | _hurd_sigstate_lock (ss); |
738 | ||
739 | /* If this is a global signal, try to find a thread ready to accept | |
740 | it right away. This is especially important for untraced signals, | |
741 | since going through the global pending mask would de-untrace them. */ | |
742 | if (ss->thread == MACH_PORT_NULL) | |
743 | { | |
744 | struct hurd_sigstate *rss; | |
745 | ||
746 | __mutex_lock (&_hurd_siglock); | |
747 | for (rss = _hurd_sigstates; rss != NULL; rss = rss->next) | |
748 | { | |
749 | if (! sigstate_is_global_rcv (rss)) | |
750 | continue; | |
751 | ||
752 | /* The global sigstate is already locked. */ | |
753 | __spin_lock (&rss->lock); | |
754 | if (! __sigismember (&rss->blocked, signo)) | |
755 | { | |
756 | ss = rss; | |
757 | break; | |
758 | } | |
759 | __spin_unlock (&rss->lock); | |
760 | } | |
761 | __mutex_unlock (&_hurd_siglock); | |
762 | } | |
763 | ||
764 | /* We want the preemptors to be able to update the blocking mask | |
765 | without affecting the delivery of this signal, so we save the | |
766 | current value to test against later. */ | |
767 | sigset_t blocked = ss->blocked; | |
28f540f4 | 768 | |
6df1b247 RM |
769 | /* Check for a preempted signal. Preempted signals can arrive during |
770 | critical sections. */ | |
e26996aa RM |
771 | { |
772 | inline sighandler_t try_preemptor (struct hurd_signal_preemptor *pe) | |
773 | { /* PE cannot be null. */ | |
774 | do | |
775 | { | |
d865ff74 | 776 | if (HURD_PREEMPT_SIGNAL_P (pe, signo, detail->exc_subcode)) |
e26996aa RM |
777 | { |
778 | if (pe->preemptor) | |
779 | { | |
780 | sighandler_t handler = (*pe->preemptor) (pe, ss, | |
781 | &signo, detail); | |
782 | if (handler != SIG_ERR) | |
783 | return handler; | |
784 | } | |
785 | else | |
786 | return pe->handler; | |
787 | } | |
788 | pe = pe->next; | |
789 | } while (pe != 0); | |
790 | return SIG_ERR; | |
791 | } | |
6df1b247 | 792 | |
e26996aa | 793 | handler = ss->preemptors ? try_preemptor (ss->preemptors) : SIG_ERR; |
6df1b247 | 794 | |
e26996aa | 795 | /* If no thread-specific preemptor, check for a global one. */ |
044edf6d | 796 | if (handler == SIG_ERR && __sigismember (&_hurdsig_preempted_set, signo)) |
e26996aa RM |
797 | { |
798 | __mutex_lock (&_hurd_siglock); | |
799 | handler = try_preemptor (_hurdsig_preemptors); | |
800 | __mutex_unlock (&_hurd_siglock); | |
801 | } | |
802 | } | |
28f540f4 RM |
803 | |
804 | ss_suspended = 0; | |
805 | ||
6df1b247 RM |
806 | if (handler == SIG_IGN) |
807 | /* Ignore the signal altogether. */ | |
808 | act = ignore; | |
09d434df | 809 | else if (handler != SIG_ERR) |
e42efa01 ST |
810 | { |
811 | /* Run the preemption-provided handler. */ | |
812 | action = &preempt_sigaction; | |
813 | act = handle; | |
814 | } | |
28f540f4 RM |
815 | else |
816 | { | |
817 | /* No preemption. Do normal handling. */ | |
818 | ||
e42efa01 ST |
819 | action = & _hurd_sigstate_actions (ss) [signo]; |
820 | ||
8f0c527e | 821 | if (!untraced && __sigismember (&_hurdsig_traced, signo)) |
28f540f4 RM |
822 | { |
823 | /* We are being traced. Stop to tell the debugger of the signal. */ | |
824 | if (_hurd_stopped) | |
825 | /* Already stopped. Mark the signal as pending; | |
826 | when resumed, we will notice it and stop again. */ | |
827 | mark_pending (); | |
828 | else | |
829 | suspend (); | |
653d74f1 | 830 | _hurd_sigstate_unlock (ss); |
28f540f4 | 831 | reply (); |
653d74f1 | 832 | return NULL; |
28f540f4 RM |
833 | } |
834 | ||
e42efa01 | 835 | handler = action->sa_handler; |
75914335 | 836 | |
28f540f4 RM |
837 | if (handler == SIG_DFL) |
838 | /* Figure out the default action for this signal. */ | |
839 | switch (signo) | |
840 | { | |
841 | case 0: | |
842 | /* A sig_post msg with SIGNO==0 is sent to | |
843 | tell us to check for pending signals. */ | |
844 | act = ignore; | |
845 | break; | |
846 | ||
847 | case SIGTTIN: | |
848 | case SIGTTOU: | |
849 | case SIGSTOP: | |
850 | case SIGTSTP: | |
851 | act = stop; | |
852 | break; | |
853 | ||
854 | case SIGCONT: | |
855 | case SIGIO: | |
856 | case SIGURG: | |
857 | case SIGCHLD: | |
858 | case SIGWINCH: | |
859 | act = ignore; | |
860 | break; | |
861 | ||
862 | case SIGQUIT: | |
863 | case SIGILL: | |
864 | case SIGTRAP: | |
865 | case SIGIOT: | |
866 | case SIGEMT: | |
867 | case SIGFPE: | |
868 | case SIGBUS: | |
869 | case SIGSEGV: | |
870 | case SIGSYS: | |
871 | act = core; | |
872 | break; | |
873 | ||
874 | case SIGINFO: | |
875 | if (_hurd_pgrp == _hurd_pid) | |
876 | { | |
877 | /* We are the process group leader. Since there is no | |
878 | user-specified handler for SIGINFO, we use a default one | |
879 | which prints something interesting. We use the normal | |
880 | handler mechanism instead of just doing it here to avoid | |
881 | the signal thread faulting or blocking in this | |
882 | potentially hairy operation. */ | |
883 | act = handle; | |
884 | handler = _hurd_siginfo_handler; | |
885 | } | |
886 | else | |
887 | act = ignore; | |
888 | break; | |
889 | ||
890 | default: | |
891 | act = term; | |
892 | break; | |
893 | } | |
894 | else if (handler == SIG_IGN) | |
895 | act = ignore; | |
896 | else | |
897 | act = handle; | |
898 | ||
899 | if (__sigmask (signo) & STOPSIGS) | |
900 | /* Stop signals clear a pending SIGCONT even if they | |
901 | are handled or ignored (but not if preempted). */ | |
05dea6d1 | 902 | __sigdelset (&ss->pending, SIGCONT); |
28f540f4 RM |
903 | else |
904 | { | |
905 | if (signo == SIGCONT) | |
906 | /* Even if handled or ignored (but not preempted), SIGCONT clears | |
907 | stop signals and resumes the process. */ | |
908 | ss->pending &= ~STOPSIGS; | |
909 | ||
910 | if (_hurd_stopped && act != stop && (untraced || signo == SIGCONT)) | |
75914335 | 911 | resume (); |
28f540f4 RM |
912 | } |
913 | } | |
914 | ||
a04549c1 JM |
915 | if (_hurd_orphaned && act == stop |
916 | && (__sigmask (signo) & (__sigmask (SIGTTIN) | __sigmask (SIGTTOU) | |
917 | | __sigmask (SIGTSTP)))) | |
28f540f4 RM |
918 | { |
919 | /* If we would ordinarily stop for a job control signal, but we are | |
7f0d9e61 | 920 | orphaned so no one would ever notice and continue us again, we just |
28f540f4 | 921 | quietly die, alone and in the dark. */ |
0e3426bb | 922 | detail->code = signo; |
28f540f4 RM |
923 | signo = SIGKILL; |
924 | act = term; | |
925 | } | |
926 | ||
75914335 | 927 | /* Handle receipt of a blocked signal, or any signal while stopped. */ |
653d74f1 | 928 | if (__sigismember (&blocked, signo) || (signo != SIGKILL && _hurd_stopped)) |
28f540f4 RM |
929 | { |
930 | mark_pending (); | |
931 | act = ignore; | |
932 | } | |
933 | ||
934 | /* Perform the chosen action for the signal. */ | |
935 | switch (act) | |
936 | { | |
937 | case stop: | |
938 | if (_hurd_stopped) | |
b96bdcd7 RM |
939 | { |
940 | /* We are already stopped, but receiving an untraced stop | |
941 | signal. Instead of resuming and suspending again, just | |
942 | notify the proc server of the new stop signal. */ | |
0e3426bb RM |
943 | error_t err = __USEPORT (PROC, __proc_mark_stop |
944 | (port, signo, detail->code)); | |
b96bdcd7 RM |
945 | assert_perror (err); |
946 | } | |
28f540f4 RM |
947 | else |
948 | /* Suspend the process. */ | |
949 | suspend (); | |
950 | break; | |
951 | ||
952 | case ignore: | |
2b965f1b RM |
953 | if (detail->exc) |
954 | /* Blocking or ignoring a machine exception is fatal. | |
955 | Otherwise we could just spin on the faulting instruction. */ | |
956 | goto fatal; | |
957 | ||
4d02a5b1 RM |
958 | /* Nobody cares about this signal. If there was a call to resume |
959 | above in SIGCONT processing and we've left a thread suspended, | |
960 | now's the time to set it going. */ | |
961 | if (ss_suspended) | |
962 | { | |
653d74f1 | 963 | assert (ss->thread != MACH_PORT_NULL); |
4d02a5b1 RM |
964 | err = __thread_resume (ss->thread); |
965 | assert_perror (err); | |
966 | ss_suspended = 0; | |
967 | } | |
28f540f4 RM |
968 | break; |
969 | ||
421f82e5 RM |
970 | sigbomb: |
971 | /* We got a fault setting up the stack frame for the handler. | |
972 | Nothing to do but die; BSD gets SIGILL in this case. */ | |
0e3426bb | 973 | detail->code = signo; /* XXX ? */ |
421f82e5 | 974 | signo = SIGILL; |
2b965f1b RM |
975 | |
976 | fatal: | |
421f82e5 RM |
977 | act = core; |
978 | /* FALLTHROUGH */ | |
979 | ||
28f540f4 RM |
980 | case term: /* Time to die. */ |
981 | case core: /* And leave a rotting corpse. */ | |
28f540f4 | 982 | /* Have the proc server stop all other threads in our task. */ |
b96bdcd7 RM |
983 | err = __USEPORT (PROC, __proc_dostop (port, _hurd_msgport_thread)); |
984 | assert_perror (err); | |
28f540f4 RM |
985 | /* No more user instructions will be executed. |
986 | The signal can now be considered delivered. */ | |
987 | reply (); | |
988 | /* Abort all server operations now in progress. */ | |
989 | abort_all_rpcs (signo, &thread_state, 0); | |
990 | ||
991 | { | |
992 | int status = W_EXITCODE (0, signo); | |
993 | /* Do a core dump if desired. Only set the wait status bit saying we | |
994 | in fact dumped core if the operation was actually successful. */ | |
0e3426bb | 995 | if (act == core && write_corefile (signo, detail)) |
28f540f4 RM |
996 | status |= WCOREFLAG; |
997 | /* Tell proc how we died and then stick the saber in the gut. */ | |
998 | _hurd_exit (status); | |
999 | /* NOTREACHED */ | |
1000 | } | |
1001 | ||
1002 | case handle: | |
1003 | /* Call a handler for this signal. */ | |
1004 | { | |
421f82e5 | 1005 | struct sigcontext *scp, ocontext; |
28f540f4 RM |
1006 | int wait_for_reply, state_changed; |
1007 | ||
653d74f1 JK |
1008 | assert (ss->thread != MACH_PORT_NULL); |
1009 | ||
28f540f4 RM |
1010 | /* Stop the thread and abort its pending RPC operations. */ |
1011 | if (! ss_suspended) | |
b96bdcd7 RM |
1012 | { |
1013 | err = __thread_suspend (ss->thread); | |
1014 | assert_perror (err); | |
1015 | } | |
28f540f4 RM |
1016 | |
1017 | /* Abort the thread's kernel context, so any pending message send | |
1018 | or receive completes immediately or aborts. If an interruptible | |
1019 | RPC is in progress, abort_rpcs will do this. But we must always | |
1020 | do it before fetching the thread's state, because | |
1021 | thread_get_state is never kosher before thread_abort. */ | |
1a3a58fd | 1022 | abort_thread (ss, &thread_state, NULL); |
28f540f4 | 1023 | |
421f82e5 RM |
1024 | if (ss->context) |
1025 | { | |
1026 | /* We have a previous sigcontext that sigreturn was about | |
1027 | to restore when another signal arrived. */ | |
1028 | ||
1029 | mach_port_t *loc; | |
28f540f4 | 1030 | |
fb8e70d6 | 1031 | if (_hurdsig_catch_memory_fault (ss->context)) |
421f82e5 | 1032 | { |
421f82e5 RM |
1033 | /* We faulted reading the thread's stack. Forget that |
1034 | context and pretend it wasn't there. It almost | |
1035 | certainly crash if this handler returns, but that's it's | |
1036 | problem. */ | |
1037 | ss->context = NULL; | |
1038 | } | |
1039 | else | |
1040 | { | |
1041 | /* Copy the context from the thread's stack before | |
1042 | we start diddling the stack to set up the handler. */ | |
1043 | ocontext = *ss->context; | |
1044 | ss->context = &ocontext; | |
1045 | } | |
1046 | _hurdsig_end_catch_fault (); | |
75914335 | 1047 | |
421f82e5 RM |
1048 | if (! machine_get_basic_state (ss->thread, &thread_state)) |
1049 | goto sigbomb; | |
dc33bef3 ST |
1050 | loc = interrupted_reply_port_location (ss->thread, |
1051 | &thread_state, 1); | |
421f82e5 RM |
1052 | if (loc && *loc != MACH_PORT_NULL) |
1053 | /* This is the reply port for the context which called | |
1054 | sigreturn. Since we are abandoning that context entirely | |
1055 | and restoring SS->context instead, destroy this port. */ | |
1056 | __mach_port_destroy (__mach_task_self (), *loc); | |
1057 | ||
1058 | /* The thread was in sigreturn, not in any interruptible RPC. */ | |
1059 | wait_for_reply = 0; | |
1060 | ||
8f0c527e | 1061 | assert (! __spin_lock_locked (&ss->critical_section_lock)); |
421f82e5 RM |
1062 | } |
1063 | else | |
28f540f4 | 1064 | { |
e33b438a RM |
1065 | int crit = __spin_lock_locked (&ss->critical_section_lock); |
1066 | ||
54da5be3 | 1067 | wait_for_reply |
e33b438a RM |
1068 | = (_hurdsig_abort_rpcs (ss, |
1069 | /* In a critical section, any RPC | |
1070 | should be cancelled instead of | |
1071 | restarted, regardless of | |
3081378b | 1072 | SA_RESTART, so the entire |
e33b438a RM |
1073 | "atomic" operation can be aborted |
1074 | as a unit. */ | |
1075 | crit ? 0 : signo, 1, | |
54da5be3 | 1076 | &thread_state, &state_changed, |
4288c548 | 1077 | reply) |
54da5be3 | 1078 | != MACH_PORT_NULL); |
421f82e5 | 1079 | |
e33b438a | 1080 | if (crit) |
421f82e5 RM |
1081 | { |
1082 | /* The thread is in a critical section. Mark the signal as | |
1083 | pending. When it finishes the critical section, it will | |
1084 | check for pending signals. */ | |
1085 | mark_pending (); | |
e33b438a RM |
1086 | if (state_changed) |
1087 | /* Some cases of interrupting an RPC must change the | |
1088 | thread state to back out the call. Normally this | |
1089 | change is rolled into the warping to the handler and | |
1090 | sigreturn, but we are not running the handler now | |
1091 | because the thread is in a critical section. Instead, | |
1092 | mutate the thread right away for the RPC interruption | |
1093 | and resume it; the RPC will return early so the | |
1094 | critical section can end soon. */ | |
1095 | __thread_set_state (ss->thread, MACHINE_THREAD_STATE_FLAVOR, | |
1096 | (natural_t *) &thread_state.basic, | |
1097 | MACHINE_THREAD_STATE_COUNT); | |
1098 | /* */ | |
1099 | ss->intr_port = MACH_PORT_NULL; | |
421f82e5 RM |
1100 | __thread_resume (ss->thread); |
1101 | break; | |
1102 | } | |
28f540f4 RM |
1103 | } |
1104 | ||
1105 | /* Call the machine-dependent function to set the thread up | |
1106 | to run the signal handler, and preserve its old context. */ | |
e42efa01 | 1107 | scp = _hurd_setup_sighandler (ss, action, handler, signo, detail, |
28f540f4 RM |
1108 | wait_for_reply, &thread_state); |
1109 | if (scp == NULL) | |
421f82e5 | 1110 | goto sigbomb; |
28f540f4 RM |
1111 | |
1112 | /* Set the machine-independent parts of the signal context. */ | |
1113 | ||
28f540f4 RM |
1114 | { |
1115 | /* Fetch the thread variable for the MiG reply port, | |
1116 | and set it to MACH_PORT_NULL. */ | |
dc33bef3 ST |
1117 | mach_port_t *loc = interrupted_reply_port_location (ss->thread, |
1118 | &thread_state, | |
54da5be3 | 1119 | 1); |
28f540f4 RM |
1120 | if (loc) |
1121 | { | |
1122 | scp->sc_reply_port = *loc; | |
1123 | *loc = MACH_PORT_NULL; | |
1124 | } | |
1125 | else | |
1126 | scp->sc_reply_port = MACH_PORT_NULL; | |
421f82e5 RM |
1127 | |
1128 | /* Save the intr_port in use by the interrupted code, | |
1129 | and clear the cell before running the trampoline. */ | |
1130 | scp->sc_intr_port = ss->intr_port; | |
1131 | ss->intr_port = MACH_PORT_NULL; | |
1132 | ||
1133 | if (ss->context) | |
1134 | { | |
1135 | /* After the handler runs we will restore to the state in | |
1136 | SS->context, not the state of the thread now. So restore | |
1137 | that context's reply port and intr port. */ | |
1138 | ||
1139 | scp->sc_reply_port = ss->context->sc_reply_port; | |
1140 | scp->sc_intr_port = ss->context->sc_intr_port; | |
1141 | ||
1142 | ss->context = NULL; | |
1143 | } | |
28f540f4 RM |
1144 | } |
1145 | ||
421f82e5 | 1146 | /* Backdoor extra argument to signal handler. */ |
0e3426bb | 1147 | scp->sc_error = detail->error; |
421f82e5 | 1148 | |
ac61ed31 | 1149 | /* Block requested signals while running the handler. */ |
28f540f4 | 1150 | scp->sc_mask = ss->blocked; |
653d74f1 | 1151 | __sigorset (&ss->blocked, &ss->blocked, &action->sa_mask); |
5d83494f | 1152 | |
ac61ed31 | 1153 | /* Also block SIGNO unless we're asked not to. */ |
653d74f1 | 1154 | if (! (action->sa_flags & (SA_RESETHAND | SA_NODEFER))) |
05dea6d1 | 1155 | __sigaddset (&ss->blocked, signo); |
5d83494f | 1156 | |
ac61ed31 MK |
1157 | /* Reset to SIG_DFL if requested. SIGILL and SIGTRAP cannot |
1158 | be automatically reset when delivered; the system silently | |
1159 | enforces this restriction. */ | |
653d74f1 | 1160 | if (action->sa_flags & SA_RESETHAND |
ac61ed31 | 1161 | && signo != SIGILL && signo != SIGTRAP) |
653d74f1 | 1162 | action->sa_handler = SIG_DFL; |
28f540f4 | 1163 | |
4288c548 JK |
1164 | /* Any sigsuspend call must return after the handler does. */ |
1165 | wake_sigsuspend (ss); | |
1166 | ||
28f540f4 RM |
1167 | /* Start the thread running the handler (or possibly waiting for an |
1168 | RPC reply before running the handler). */ | |
b96bdcd7 RM |
1169 | err = __thread_set_state (ss->thread, MACHINE_THREAD_STATE_FLAVOR, |
1170 | (natural_t *) &thread_state.basic, | |
1171 | MACHINE_THREAD_STATE_COUNT); | |
1172 | assert_perror (err); | |
1173 | err = __thread_resume (ss->thread); | |
1174 | assert_perror (err); | |
28f540f4 RM |
1175 | thread_state.set = 0; /* Everything we know is now wrong. */ |
1176 | break; | |
1177 | } | |
1178 | } | |
1179 | ||
653d74f1 | 1180 | return ss; |
4288c548 | 1181 | } |
28f540f4 | 1182 | |
4288c548 JK |
1183 | /* Return the set of pending signals in SS which should be delivered. */ |
1184 | static sigset_t | |
1185 | pending_signals (struct hurd_sigstate *ss) | |
1186 | { | |
1187 | /* We don't worry about any pending signals if we are stopped, nor if | |
1188 | SS is in a critical section. We are guaranteed to get a sig_post | |
1189 | message before any of them become deliverable: either the SIGCONT | |
1190 | signal, or a sig_post with SIGNO==0 as an explicit poll when the | |
1191 | thread finishes its critical section. */ | |
1192 | if (_hurd_stopped || __spin_lock_locked (&ss->critical_section_lock)) | |
1193 | return 0; | |
7cc645ed | 1194 | |
653d74f1 | 1195 | return _hurd_sigstate_pending (ss) & ~ss->blocked; |
4288c548 | 1196 | } |
75914335 | 1197 | |
4288c548 JK |
1198 | /* Post the specified pending signals in SS and return 1. If one of |
1199 | them is traced, abort immediately and return 0. SS must be locked on | |
1200 | entry and will be unlocked in all cases. */ | |
1201 | static int | |
1202 | post_pending (struct hurd_sigstate *ss, sigset_t pending, void (*reply) (void)) | |
1203 | { | |
1204 | int signo; | |
1205 | struct hurd_signal_detail detail; | |
7cc645ed | 1206 | |
653d74f1 JK |
1207 | /* Make sure SS corresponds to an actual thread, since we assume it won't |
1208 | change in post_signal. */ | |
1209 | assert (ss->thread != MACH_PORT_NULL); | |
1210 | ||
4288c548 JK |
1211 | for (signo = 1; signo < NSIG; ++signo) |
1212 | if (__sigismember (&pending, signo)) | |
7cc645ed | 1213 | { |
653d74f1 JK |
1214 | detail = sigstate_clear_pending (ss, signo); |
1215 | _hurd_sigstate_unlock (ss); | |
4288c548 JK |
1216 | |
1217 | /* Will reacquire the lock, except if the signal is traced. */ | |
1218 | if (! post_signal (ss, signo, &detail, 0, reply)) | |
1219 | return 0; | |
7cc645ed | 1220 | } |
28f540f4 | 1221 | |
4288c548 | 1222 | /* No more signals pending; SS->lock is still locked. */ |
653d74f1 | 1223 | _hurd_sigstate_unlock (ss); |
4288c548 JK |
1224 | |
1225 | return 1; | |
1226 | } | |
1227 | ||
1228 | /* Post all the pending signals of all threads and return 1. If a traced | |
1229 | signal is encountered, abort immediately and return 0. */ | |
1230 | static int | |
1231 | post_all_pending_signals (void (*reply) (void)) | |
1232 | { | |
1233 | struct hurd_sigstate *ss; | |
1234 | sigset_t pending = 0; | |
1235 | ||
1236 | for (;;) | |
1237 | { | |
1238 | __mutex_lock (&_hurd_siglock); | |
1239 | for (ss = _hurd_sigstates; ss != NULL; ss = ss->next) | |
1240 | { | |
653d74f1 | 1241 | _hurd_sigstate_lock (ss); |
4288c548 JK |
1242 | |
1243 | pending = pending_signals (ss); | |
1244 | if (pending) | |
1245 | /* post_pending() below will unlock SS. */ | |
1246 | break; | |
1247 | ||
653d74f1 | 1248 | _hurd_sigstate_unlock (ss); |
4288c548 JK |
1249 | } |
1250 | __mutex_unlock (&_hurd_siglock); | |
1251 | ||
1252 | if (! pending) | |
1253 | return 1; | |
1254 | if (! post_pending (ss, pending, reply)) | |
1255 | return 0; | |
1256 | } | |
1257 | } | |
1258 | ||
1259 | /* Deliver a signal. SS is not locked. */ | |
1260 | void | |
1261 | _hurd_internal_post_signal (struct hurd_sigstate *ss, | |
1262 | int signo, struct hurd_signal_detail *detail, | |
1263 | mach_port_t reply_port, | |
1264 | mach_msg_type_name_t reply_port_type, | |
1265 | int untraced) | |
1266 | { | |
1267 | /* Reply to this sig_post message. */ | |
1268 | __typeof (__msg_sig_post_reply) *reply_rpc | |
1269 | = (untraced ? __msg_sig_post_untraced_reply : __msg_sig_post_reply); | |
1270 | void reply (void) | |
1271 | { | |
1272 | error_t err; | |
1273 | if (reply_port == MACH_PORT_NULL) | |
1274 | return; | |
1275 | err = (*reply_rpc) (reply_port, reply_port_type, 0); | |
1276 | reply_port = MACH_PORT_NULL; | |
1277 | if (err != MACH_SEND_INVALID_DEST) /* Ignore dead reply port. */ | |
1278 | assert_perror (err); | |
1279 | } | |
1280 | ||
653d74f1 JK |
1281 | ss = post_signal (ss, signo, detail, untraced, reply); |
1282 | if (! ss) | |
4288c548 JK |
1283 | return; |
1284 | ||
1285 | /* The signal was neither fatal nor traced. We still hold SS->lock. */ | |
653d74f1 | 1286 | if (signo != 0 && ss->thread != MACH_PORT_NULL) |
4288c548 JK |
1287 | { |
1288 | /* The signal has either been ignored or is now being handled. We can | |
1289 | consider it delivered and reply to the killer. */ | |
1290 | reply (); | |
1291 | ||
1292 | /* Post any pending signals for this thread. */ | |
1293 | if (! post_pending (ss, pending_signals (ss), reply)) | |
1294 | return; | |
1295 | } | |
1296 | else | |
1297 | { | |
653d74f1 JK |
1298 | /* If this was a process-wide signal or a poll request, we need |
1299 | to check for pending signals for all threads. */ | |
1300 | _hurd_sigstate_unlock (ss); | |
4288c548 JK |
1301 | if (! post_all_pending_signals (reply)) |
1302 | return; | |
1303 | ||
1304 | /* All pending signals delivered to all threads. | |
1305 | Now we can send the reply message even for signal 0. */ | |
1306 | reply (); | |
1307 | } | |
28f540f4 RM |
1308 | } |
1309 | \f | |
1310 | /* Decide whether REFPORT enables the sender to send us a SIGNO signal. | |
1311 | Returns zero if so, otherwise the error code to return to the sender. */ | |
1312 | ||
1313 | static error_t | |
1314 | signal_allowed (int signo, mach_port_t refport) | |
1315 | { | |
1316 | if (signo < 0 || signo >= NSIG) | |
1317 | return EINVAL; | |
1318 | ||
1319 | if (refport == __mach_task_self ()) | |
1320 | /* Can send any signal. */ | |
1321 | goto win; | |
1322 | ||
1323 | /* Avoid needing to check for this below. */ | |
1324 | if (refport == MACH_PORT_NULL) | |
1325 | return EPERM; | |
1326 | ||
1327 | switch (signo) | |
1328 | { | |
1329 | case SIGINT: | |
1330 | case SIGQUIT: | |
1331 | case SIGTSTP: | |
1332 | case SIGHUP: | |
1333 | case SIGINFO: | |
1334 | case SIGTTIN: | |
1335 | case SIGTTOU: | |
ce7f605b | 1336 | case SIGWINCH: |
28f540f4 RM |
1337 | /* Job control signals can be sent by the controlling terminal. */ |
1338 | if (__USEPORT (CTTYID, port == refport)) | |
1339 | goto win; | |
1340 | break; | |
1341 | ||
1342 | case SIGCONT: | |
1343 | { | |
1344 | /* A continue signal can be sent by anyone in the session. */ | |
1345 | mach_port_t sessport; | |
1346 | if (! __USEPORT (PROC, __proc_getsidport (port, &sessport))) | |
75914335 | 1347 | { |
28f540f4 RM |
1348 | __mach_port_deallocate (__mach_task_self (), sessport); |
1349 | if (refport == sessport) | |
1350 | goto win; | |
1351 | } | |
1352 | } | |
1353 | break; | |
1354 | ||
1355 | case SIGIO: | |
1356 | case SIGURG: | |
1357 | { | |
1358 | /* Any io object a file descriptor refers to might send us | |
1359 | one of these signals using its async ID port for REFPORT. | |
1360 | ||
1361 | This is pretty wide open; it is not unlikely that some random | |
1362 | process can at least open for reading something we have open, | |
1363 | get its async ID port, and send us a spurious SIGIO or SIGURG | |
1364 | signal. But BSD is actually wider open than that!--you can set | |
1365 | the owner of an io object to any process or process group | |
1366 | whatsoever and send them gratuitous signals. | |
1367 | ||
1368 | Someday we could implement some reasonable scheme for | |
1369 | authorizing SIGIO and SIGURG signals properly. */ | |
1370 | ||
1371 | int d; | |
e0637da1 | 1372 | int lucky = 0; /* True if we find a match for REFPORT. */ |
28f540f4 | 1373 | __mutex_lock (&_hurd_dtable_lock); |
e0637da1 | 1374 | for (d = 0; !lucky && (unsigned) d < (unsigned) _hurd_dtablesize; ++d) |
28f540f4 RM |
1375 | { |
1376 | struct hurd_userlink ulink; | |
1377 | io_t port; | |
1378 | mach_port_t asyncid; | |
1379 | if (_hurd_dtable[d] == NULL) | |
1380 | continue; | |
1381 | port = _hurd_port_get (&_hurd_dtable[d]->port, &ulink); | |
1382 | if (! __io_get_icky_async_id (port, &asyncid)) | |
1383 | { | |
1384 | if (refport == asyncid) | |
1385 | /* Break out of the loop on the next iteration. */ | |
e0637da1 | 1386 | lucky = 1; |
28f540f4 RM |
1387 | __mach_port_deallocate (__mach_task_self (), asyncid); |
1388 | } | |
1389 | _hurd_port_free (&_hurd_dtable[d]->port, &ulink, port); | |
1390 | } | |
c90c1e1c | 1391 | __mutex_unlock (&_hurd_dtable_lock); |
28f540f4 | 1392 | /* If we found a lucky winner, we've set D to -1 in the loop. */ |
e0637da1 | 1393 | if (lucky) |
28f540f4 RM |
1394 | goto win; |
1395 | } | |
1396 | } | |
1397 | ||
1398 | /* If this signal is legit, we have done `goto win' by now. | |
1399 | When we return the error, mig deallocates REFPORT. */ | |
1400 | return EPERM; | |
1401 | ||
1402 | win: | |
1403 | /* Deallocate the REFPORT send right; we are done with it. */ | |
1404 | __mach_port_deallocate (__mach_task_self (), refport); | |
1405 | ||
1406 | return 0; | |
1407 | } | |
1408 | ||
1409 | /* Implement the sig_post RPC from <hurd/msg.defs>; | |
1410 | sent when someone wants us to get a signal. */ | |
1411 | kern_return_t | |
1412 | _S_msg_sig_post (mach_port_t me, | |
1413 | mach_port_t reply_port, mach_msg_type_name_t reply_port_type, | |
8f0c527e | 1414 | int signo, natural_t sigcode, |
28f540f4 RM |
1415 | mach_port_t refport) |
1416 | { | |
1417 | error_t err; | |
93a470c7 | 1418 | struct hurd_signal_detail d; |
28f540f4 RM |
1419 | |
1420 | if (err = signal_allowed (signo, refport)) | |
1421 | return err; | |
1422 | ||
d865ff74 | 1423 | d.code = d.exc_subcode = sigcode; |
93a470c7 RM |
1424 | d.exc = 0; |
1425 | ||
653d74f1 JK |
1426 | /* Post the signal to a global receiver thread (or mark it pending in |
1427 | the global sigstate). This will reply when the signal can be | |
1428 | considered delivered. */ | |
1429 | _hurd_internal_post_signal (_hurd_global_sigstate, | |
93a470c7 | 1430 | signo, &d, reply_port, reply_port_type, |
28f540f4 RM |
1431 | 0); /* Stop if traced. */ |
1432 | ||
1433 | return MIG_NO_REPLY; /* Already replied. */ | |
1434 | } | |
1435 | ||
1436 | /* Implement the sig_post_untraced RPC from <hurd/msg.defs>; | |
1437 | sent when the debugger wants us to really get a signal | |
1438 | even if we are traced. */ | |
1439 | kern_return_t | |
1440 | _S_msg_sig_post_untraced (mach_port_t me, | |
1441 | mach_port_t reply_port, | |
1442 | mach_msg_type_name_t reply_port_type, | |
8f0c527e | 1443 | int signo, natural_t sigcode, |
28f540f4 RM |
1444 | mach_port_t refport) |
1445 | { | |
1446 | error_t err; | |
93a470c7 | 1447 | struct hurd_signal_detail d; |
28f540f4 RM |
1448 | |
1449 | if (err = signal_allowed (signo, refport)) | |
1450 | return err; | |
1451 | ||
d865ff74 | 1452 | d.code = d.exc_subcode = sigcode; |
93a470c7 RM |
1453 | d.exc = 0; |
1454 | ||
28f540f4 RM |
1455 | /* Post the signal to the designated signal-receiving thread. This will |
1456 | reply when the signal can be considered delivered. */ | |
653d74f1 | 1457 | _hurd_internal_post_signal (_hurd_global_sigstate, |
93a470c7 | 1458 | signo, &d, reply_port, reply_port_type, |
28f540f4 RM |
1459 | 1); /* Untraced flag. */ |
1460 | ||
1461 | return MIG_NO_REPLY; /* Already replied. */ | |
1462 | } | |
1463 | \f | |
1464 | extern void __mig_init (void *); | |
1465 | ||
1466 | #include <mach/task_special_ports.h> | |
1467 | ||
653d74f1 JK |
1468 | /* Initialize the message port, _hurd_global_sigstate, and start the |
1469 | signal thread. */ | |
28f540f4 RM |
1470 | |
1471 | void | |
62495816 | 1472 | _hurdsig_init (const int *intarray, size_t intarraysize) |
28f540f4 RM |
1473 | { |
1474 | error_t err; | |
1475 | vm_size_t stacksize; | |
62495816 | 1476 | struct hurd_sigstate *ss; |
28f540f4 RM |
1477 | |
1478 | __mutex_init (&_hurd_siglock); | |
1479 | ||
a5a81fec RM |
1480 | err = __mach_port_allocate (__mach_task_self (), |
1481 | MACH_PORT_RIGHT_RECEIVE, | |
1482 | &_hurd_msgport); | |
1483 | assert_perror (err); | |
75914335 | 1484 | |
28f540f4 | 1485 | /* Make a send right to the signal port. */ |
a5a81fec RM |
1486 | err = __mach_port_insert_right (__mach_task_self (), |
1487 | _hurd_msgport, | |
1488 | _hurd_msgport, | |
1489 | MACH_MSG_TYPE_MAKE_SEND); | |
1490 | assert_perror (err); | |
28f540f4 | 1491 | |
653d74f1 JK |
1492 | /* Initialize the global signal state. */ |
1493 | _hurd_global_sigstate = _hurd_thread_sigstate (MACH_PORT_NULL); | |
1494 | ||
1495 | /* We block all signals, and let actual threads pull them from the | |
1496 | pending mask. */ | |
1497 | __sigfillset(& _hurd_global_sigstate->blocked); | |
1498 | ||
62495816 RM |
1499 | /* Initialize the main thread's signal state. */ |
1500 | ss = _hurd_self_sigstate (); | |
1501 | ||
653d74f1 JK |
1502 | /* Mark it as a process-wide signal receiver. Threads in this set use |
1503 | the common action vector in _hurd_global_sigstate. */ | |
1504 | _hurd_sigstate_set_global_rcv (ss); | |
1505 | ||
1506 | /* Copy inherited signal settings from our parent (or pre-exec process | |
1507 | state) */ | |
62495816 RM |
1508 | if (intarraysize > INIT_SIGMASK) |
1509 | ss->blocked = intarray[INIT_SIGMASK]; | |
1510 | if (intarraysize > INIT_SIGPENDING) | |
653d74f1 | 1511 | _hurd_global_sigstate->pending = intarray[INIT_SIGPENDING]; |
62495816 RM |
1512 | if (intarraysize > INIT_SIGIGN && intarray[INIT_SIGIGN] != 0) |
1513 | { | |
1514 | int signo; | |
1515 | for (signo = 1; signo < NSIG; ++signo) | |
1516 | if (intarray[INIT_SIGIGN] & __sigmask(signo)) | |
653d74f1 | 1517 | _hurd_global_sigstate->actions[signo].sa_handler = SIG_IGN; |
62495816 RM |
1518 | } |
1519 | ||
28f540f4 RM |
1520 | /* Start the signal thread listening on the message port. */ |
1521 | ||
9446e02b ST |
1522 | #pragma weak __pthread_create |
1523 | if (!__pthread_create) | |
72e1a750 RM |
1524 | { |
1525 | err = __thread_create (__mach_task_self (), &_hurd_msgport_thread); | |
1526 | assert_perror (err); | |
28f540f4 | 1527 | |
72e1a750 | 1528 | stacksize = __vm_page_size * 8; /* Small stack for signal thread. */ |
aa19c68d SB |
1529 | err = __mach_setup_thread_call (__mach_task_self (), |
1530 | _hurd_msgport_thread, | |
1531 | _hurd_msgport_receive, | |
1532 | (vm_address_t *) &__hurd_sigthread_stack_base, | |
1533 | &stacksize); | |
72e1a750 | 1534 | assert_perror (err); |
42fc12ef ST |
1535 | err = __mach_setup_tls (_hurd_msgport_thread); |
1536 | assert_perror (err); | |
28f540f4 | 1537 | |
72e1a750 | 1538 | __hurd_sigthread_stack_end = __hurd_sigthread_stack_base + stacksize; |
28f540f4 | 1539 | |
72e1a750 RM |
1540 | /* Reinitialize the MiG support routines so they will use a per-thread |
1541 | variable for the cached reply port. */ | |
1542 | __mig_init ((void *) __hurd_sigthread_stack_base); | |
28f540f4 | 1543 | |
72e1a750 RM |
1544 | err = __thread_resume (_hurd_msgport_thread); |
1545 | assert_perror (err); | |
1546 | } | |
1547 | else | |
1548 | { | |
9446e02b ST |
1549 | pthread_t thread; |
1550 | pthread_attr_t attr; | |
1551 | void *addr; | |
1552 | size_t size; | |
1553 | ||
1554 | /* When pthread is being used, we need to make the signal thread a | |
1555 | proper pthread. Otherwise it cannot use mutex_lock et al, which | |
1556 | will be the pthread versions. Various of the message port RPC | |
72e1a750 | 1557 | handlers need to take locks, so we need to be able to call into |
9446e02b ST |
1558 | pthread code and meet its assumptions about how our thread and |
1559 | its stack are arranged. Since pthread puts it there anyway, | |
72e1a750 | 1560 | we'll let the signal thread's per-thread variables be found as for |
9446e02b | 1561 | any normal pthread, and just leave the magic __hurd_sigthread_* |
72e1a750 | 1562 | values all zero so they'll be ignored. */ |
9446e02b ST |
1563 | |
1564 | #pragma weak __pthread_detach | |
c2fb08c7 ST |
1565 | #pragma weak __pthread_getattr_np |
1566 | #pragma weak __pthread_attr_getstack | |
9446e02b | 1567 | __pthread_create(&thread, NULL, &_hurd_msgport_receive, NULL); |
c2fb08c7 | 1568 | |
9446e02b ST |
1569 | /* Record signal thread stack layout for fork() */ |
1570 | __pthread_getattr_np (thread, &attr); | |
1571 | __pthread_attr_getstack (&attr, &addr, &size); | |
1572 | __hurd_sigthread_stack_base = (uintptr_t) addr; | |
1573 | __hurd_sigthread_stack_end = __hurd_sigthread_stack_base + size; | |
1574 | ||
1575 | __pthread_detach(thread); | |
64659255 MK |
1576 | |
1577 | /* XXX We need the thread port for the signal thread further on | |
1578 | in this thread (see hurdfault.c:_hurdsigfault_init). | |
1579 | Therefore we block until _hurd_msgport_thread is initialized | |
1580 | by the newly created thread. This really shouldn't be | |
1581 | necessary; we should be able to fetch the thread port for a | |
9446e02b | 1582 | pthread from here. */ |
64659255 MK |
1583 | while (_hurd_msgport_thread == 0) |
1584 | __swtch_pri (0); | |
72e1a750 | 1585 | } |
75914335 | 1586 | |
28f540f4 | 1587 | /* Receive exceptions on the signal port. */ |
7595ddb8 | 1588 | #ifdef TASK_EXCEPTION_PORT |
28f540f4 RM |
1589 | __task_set_special_port (__mach_task_self (), |
1590 | TASK_EXCEPTION_PORT, _hurd_msgport); | |
7595ddb8 RM |
1591 | #elif defined (EXC_MASK_ALL) |
1592 | __task_set_exception_ports (__mach_task_self (), | |
1593 | EXC_MASK_ALL & ~(EXC_MASK_SYSCALL | |
1594 | | EXC_MASK_MACH_SYSCALL | |
1595 | | EXC_MASK_RPC_ALERT), | |
1596 | _hurd_msgport, | |
1597 | EXCEPTION_DEFAULT, MACHINE_THREAD_STATE); | |
1598 | #else | |
1599 | # error task_set_exception_port? | |
1600 | #endif | |
159d4836 RM |
1601 | |
1602 | /* Sanity check. Any pending, unblocked signals should have been | |
1603 | taken by our predecessor incarnation (i.e. parent or pre-exec state) | |
1604 | before packing up our init ints. This assert is last (not above) | |
1605 | so that signal handling is all set up to handle the abort. */ | |
1606 | assert ((ss->pending &~ ss->blocked) == 0); | |
28f540f4 RM |
1607 | } |
1608 | \f /* XXXX */ | |
1609 | /* Reauthenticate with the proc server. */ | |
1610 | ||
1611 | static void | |
1612 | reauth_proc (mach_port_t new) | |
1613 | { | |
1614 | mach_port_t ref, ignore; | |
1615 | ||
1616 | ref = __mach_reply_port (); | |
1617 | if (! HURD_PORT_USE (&_hurd_ports[INIT_PORT_PROC], | |
1618 | __proc_reauthenticate (port, ref, | |
a04549c1 JM |
1619 | MACH_MSG_TYPE_MAKE_SEND) |
1620 | || __auth_user_authenticate (new, ref, | |
1621 | MACH_MSG_TYPE_MAKE_SEND, | |
1622 | &ignore)) | |
28f540f4 RM |
1623 | && ignore != MACH_PORT_NULL) |
1624 | __mach_port_deallocate (__mach_task_self (), ignore); | |
1625 | __mach_port_destroy (__mach_task_self (), ref); | |
1626 | ||
762a2918 | 1627 | /* Set the owner of the process here too. */ |
fb4cc8a0 | 1628 | __mutex_lock (&_hurd_id.lock); |
762a2918 UD |
1629 | if (!_hurd_check_ids ()) |
1630 | HURD_PORT_USE (&_hurd_ports[INIT_PORT_PROC], | |
1631 | __proc_setowner (port, | |
1632 | (_hurd_id.gen.nuids | |
1633 | ? _hurd_id.gen.uids[0] : 0), | |
1634 | !_hurd_id.gen.nuids)); | |
fb4cc8a0 | 1635 | __mutex_unlock (&_hurd_id.lock); |
762a2918 | 1636 | |
28f540f4 RM |
1637 | (void) &reauth_proc; /* Silence compiler warning. */ |
1638 | } | |
1639 | text_set_element (_hurd_reauth_hook, reauth_proc); | |
1640 | \f | |
1641 | /* Like `getenv', but safe for the signal thread to run. | |
1642 | If the environment is trashed, this will just return NULL. */ | |
1643 | ||
1644 | const char * | |
1645 | _hurdsig_getenv (const char *variable) | |
1646 | { | |
a753ffb2 RM |
1647 | if (__libc_enable_secure) |
1648 | return NULL; | |
1649 | ||
fb8e70d6 | 1650 | if (_hurdsig_catch_memory_fault (__environ)) |
28f540f4 RM |
1651 | /* We bombed in getenv. */ |
1652 | return NULL; | |
1653 | else | |
1654 | { | |
fb8e70d6 RM |
1655 | const size_t len = strlen (variable); |
1656 | char *value = NULL; | |
1657 | char *volatile *ep = __environ; | |
1658 | while (*ep) | |
1659 | { | |
1660 | const char *p = *ep; | |
10dc2a90 UD |
1661 | _hurdsig_fault_preemptor.first = (long int) p; |
1662 | _hurdsig_fault_preemptor.last = VM_MAX_ADDRESS; | |
fb8e70d6 RM |
1663 | if (! strncmp (p, variable, len) && p[len] == '=') |
1664 | { | |
fb8e70d6 RM |
1665 | size_t valuelen; |
1666 | p += len + 1; | |
1667 | valuelen = strlen (p); | |
10dc2a90 | 1668 | _hurdsig_fault_preemptor.last = (long int) (p + valuelen); |
fb8e70d6 RM |
1669 | value = malloc (++valuelen); |
1670 | if (value) | |
1671 | memcpy (value, p, valuelen); | |
1672 | break; | |
1673 | } | |
10dc2a90 UD |
1674 | _hurdsig_fault_preemptor.first = (long int) ++ep; |
1675 | _hurdsig_fault_preemptor.last = (long int) (ep + 1); | |
fb8e70d6 | 1676 | } |
28f540f4 RM |
1677 | _hurdsig_end_catch_fault (); |
1678 | return value; | |
1679 | } | |
1680 | } |