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