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