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