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