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