]> sourceware.org Git - glibc.git/blob - hurd/hurdsig.c
13ffb71822e5103c35dab93c119610b7e6d6a7cc
[glibc.git] / hurd / hurdsig.c
1 /* Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, 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
28 const char *_hurdsig_getenv (const char *);
29
30 struct mutex _hurd_siglock;
31 int _hurd_stopped;
32
33 /* Port that receives signals and other miscellaneous messages. */
34 mach_port_t _hurd_msgport;
35
36 /* Thread listening on it. */
37 thread_t _hurd_msgport_thread;
38
39 /* Thread which receives task-global signals. */
40 thread_t _hurd_sigthread;
41
42 /* Linked-list of per-thread signal state. */
43 struct hurd_sigstate *_hurd_sigstates;
44
45 /* Timeout for RPC's after interrupt_operation. */
46 mach_msg_timeout_t _hurd_interrupted_rpc_timeout = 3000;
47 \f
48 static void
49 default_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
61 struct 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)
68 break;
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;
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/crash.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
126 int _hurd_core_limit; /* XXX */
127
128 /* Call the crash dump server to mummify us before we die.
129 Returns nonzero if a core file was written. */
130 static int
131 write_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 ("CRASHSERVER");
146 if (name != NULL)
147 coreserver = __file_name_lookup (name, 0, 0);
148 if (coreserver == MACH_PORT_NULL)
149 coreserver = __file_name_lookup (_SERVERS_CRASH, 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 = __crash_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. */
183 static inline void
184 post_reply (mach_port_t *reply_port, mach_msg_type_name_t reply_port_type,
185 int untraced,
186 error_t result)
187 {
188 error_t err;
189 if (reply_port == NULL || *reply_port == MACH_PORT_NULL)
190 return;
191 err = (untraced ? __msg_sig_post_untraced_reply : __msg_sig_post_reply)
192 (*reply_port, reply_port_type, result);
193 *reply_port = MACH_PORT_NULL;
194 if (err != MACH_SEND_INVALID_DEST) /* Ignore dead reply port. */
195 assert_perror (err);
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. */
207 static void
208 abort_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 {
214 error_t err = __thread_abort (ss->thread);
215 assert_perror (err);
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
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. */
230
231 static mach_port_t *
232 interrupted_reply_port_location (struct machine_thread_all_state *thread_state,
233 int sigthread)
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
238 if (sigthread && _hurdsig_catch_fault (SIGSEGV))
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
248 if (sigthread)
249 _hurdsig_end_catch_fault ();
250
251 return portloc;
252 }
253 \f
254 #include "intr-msg.h"
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 SIGNO is used to find the applicable SA_RESTART bit. If SIGNO is zero,
269 the RPC fails with EINTR instead of restarting (thread_cancel).
270
271 *STATE_CHANGE is set nonzero if STATE->basic was modified and should
272 be applied back to the thread if it might ever run again, else zero. */
273
274 mach_port_t
275 _hurdsig_abort_rpcs (struct hurd_sigstate *ss, int signo, int sigthread,
276 struct machine_thread_all_state *state, int *state_change,
277 mach_port_t *reply_port,
278 mach_msg_type_name_t reply_port_type,
279 int untraced)
280 {
281 extern const void _hurd_intr_rpc_msg_in_trap;
282 mach_port_t rcv_port = MACH_PORT_NULL;
283 mach_port_t intr_port;
284
285 *state_change = 0;
286
287 intr_port = ss->intr_port;
288 if (intr_port == MACH_PORT_NULL)
289 /* No interruption needs done. */
290 return MACH_PORT_NULL;
291
292 /* Abort the thread's kernel context, so any pending message send or
293 receive completes immediately or aborts. */
294 abort_thread (ss, state, reply_port, reply_port_type, untraced);
295
296 if (state->basic.PC < (natural_t) &_hurd_intr_rpc_msg_in_trap)
297 {
298 /* The thread is about to do the RPC, but hasn't yet entered
299 mach_msg. Mutate the thread's state so it knows not to try
300 the RPC. */
301 INTR_MSG_BACK_OUT (&state->basic);
302 MACHINE_THREAD_STATE_SET_PC (&state->basic,
303 &_hurd_intr_rpc_msg_in_trap);
304 state->basic.SYSRETURN = MACH_SEND_INTERRUPTED;
305 *state_change = 1;
306 }
307 else if (state->basic.PC == (natural_t) &_hurd_intr_rpc_msg_in_trap &&
308 /* The thread was blocked in the system call. After thread_abort,
309 the return value register indicates what state the RPC was in
310 when interrupted. */
311 state->basic.SYSRETURN == MACH_RCV_INTERRUPTED)
312 {
313 /* The RPC request message was sent and the thread was waiting for
314 the reply message; now the message receive has been aborted, so
315 the mach_msg call will return MACH_RCV_INTERRUPTED. We must tell
316 the server to interrupt the pending operation. The thread must
317 wait for the reply message before running the signal handler (to
318 guarantee that the operation has finished being interrupted), so
319 our nonzero return tells the trampoline code to finish the message
320 receive operation before running the handler. */
321
322 mach_port_t *reply = interrupted_reply_port_location (state,
323 sigthread);
324 error_t err = __interrupt_operation (intr_port);
325
326 if (err)
327 {
328 if (reply)
329 {
330 /* The interrupt didn't work.
331 Destroy the receive right the thread is blocked on. */
332 __mach_port_destroy (__mach_task_self (), *reply);
333 *reply = MACH_PORT_NULL;
334 }
335
336 /* The system call return value register now contains
337 MACH_RCV_INTERRUPTED; when mach_msg resumes, it will retry the
338 call. Since we have just destroyed the receive right, the
339 retry will fail with MACH_RCV_INVALID_NAME. Instead, just
340 change the return value here to EINTR so mach_msg will not
341 retry and the EINTR error code will propagate up. */
342 state->basic.SYSRETURN = EINTR;
343 *state_change = 1;
344 }
345 else if (reply)
346 rcv_port = *reply;
347
348 /* All threads whose RPCs were interrupted by the interrupt_operation
349 call above will retry their RPCs unless we clear SS->intr_port.
350 So we clear it for the thread taking a signal when SA_RESTART is
351 clear, so that its call returns EINTR. */
352 if (! signo || !(ss->actions[signo].sa_flags & SA_RESTART))
353 ss->intr_port = MACH_PORT_NULL;
354 }
355
356 return rcv_port;
357 }
358
359
360 /* Abort the RPCs being run by all threads but this one;
361 all other threads should be suspended. If LIVE is nonzero, those
362 threads may run again, so they should be adjusted as necessary to be
363 happy when resumed. STATE is clobbered as a scratch area; its initial
364 contents are ignored, and its contents on return are not useful. */
365
366 static void
367 abort_all_rpcs (int signo, struct machine_thread_all_state *state, int live)
368 {
369 /* We can just loop over the sigstates. Any thread doing something
370 interruptible must have one. We needn't bother locking because all
371 other threads are stopped. */
372
373 struct hurd_sigstate *ss;
374 size_t nthreads;
375 mach_port_t *reply_ports;
376
377 /* First loop over the sigstates to count them.
378 We need to know how big a vector we will need for REPLY_PORTS. */
379 nthreads = 0;
380 for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
381 ++nthreads;
382
383 reply_ports = alloca (nthreads * sizeof *reply_ports);
384
385 nthreads = 0;
386 for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
387 if (ss->thread == _hurd_msgport_thread)
388 reply_ports[nthreads++] = MACH_PORT_NULL;
389 else
390 {
391 int state_changed;
392 state->set = 0; /* Reset scratch area. */
393
394 /* Abort any operation in progress with interrupt_operation.
395 Record the reply port the thread is waiting on.
396 We will wait for all the replies below. */
397 reply_ports[nthreads++] = _hurdsig_abort_rpcs (ss, signo, 1,
398 state, &state_changed,
399 NULL, 0, 0);
400 if (state_changed && live)
401 /* Aborting the RPC needed to change this thread's state,
402 and it might ever run again. So write back its state. */
403 __thread_set_state (ss->thread, MACHINE_THREAD_STATE_FLAVOR,
404 (natural_t *) &state->basic,
405 MACHINE_THREAD_STATE_COUNT);
406 }
407
408 /* Wait for replies from all the successfully interrupted RPCs. */
409 while (nthreads-- > 0)
410 if (reply_ports[nthreads] != MACH_PORT_NULL)
411 {
412 error_t err;
413 mach_msg_header_t head;
414 err = __mach_msg (&head, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof head,
415 reply_ports[nthreads],
416 _hurd_interrupted_rpc_timeout, MACH_PORT_NULL);
417 switch (err)
418 {
419 case MACH_RCV_TIMED_OUT:
420 case MACH_RCV_TOO_LARGE:
421 break;
422
423 default:
424 assert_perror (err);
425 }
426 }
427 }
428
429
430 struct hurd_signal_preempt *_hurd_signal_preempt[NSIG];
431 struct mutex _hurd_signal_preempt_lock;
432
433 /* Mask of stop signals. */
434 #define STOPSIGS (sigmask (SIGTTIN) | sigmask (SIGTTOU) | \
435 sigmask (SIGSTOP) | sigmask (SIGTSTP))
436
437 /* Deliver a signal. SS is not locked. */
438 void
439 _hurd_internal_post_signal (struct hurd_sigstate *ss,
440 int signo, long int sigcode, int sigerror,
441 mach_port_t reply_port,
442 mach_msg_type_name_t reply_port_type,
443 int untraced)
444 {
445 error_t err;
446 struct machine_thread_all_state thread_state;
447 enum { stop, ignore, core, term, handle } act;
448 sighandler_t handler;
449 struct hurd_signal_preempt *pe;
450 sighandler_t (*preempt) (thread_t, int, long int, int) = NULL;
451 sigset_t pending;
452 int ss_suspended;
453
454 /* Reply to this sig_post message. */
455 inline void reply (void)
456 {
457 post_reply (&reply_port, reply_port_type, untraced, 0);
458 }
459
460 /* Mark the signal as pending. */
461 void mark_pending (void)
462 {
463 __sigaddset (&ss->pending, signo);
464 /* Save the code to be given to the handler when SIGNO is
465 unblocked. */
466 ss->pending_data[signo].code = sigcode;
467 ss->pending_data[signo].error = sigerror;
468 }
469
470 /* Suspend the process with SIGNO. */
471 void suspend (void)
472 {
473 /* Stop all other threads and mark ourselves stopped. */
474 __USEPORT (PROC,
475 ({
476 /* Hold the siglock while stopping other threads to be
477 sure it is not held by another thread afterwards. */
478 __mutex_lock (&_hurd_siglock);
479 __proc_dostop (port, _hurd_msgport_thread);
480 __mutex_unlock (&_hurd_siglock);
481 abort_all_rpcs (signo, &thread_state, 1);
482 __proc_mark_stop (port, signo);
483 }));
484 _hurd_stopped = 1;
485 }
486 /* Resume the process after a suspension. */
487 void resume (void)
488 {
489 /* Resume the process from being stopped. */
490 thread_t *threads;
491 mach_msg_type_number_t nthreads, i;
492 error_t err;
493
494 if (! _hurd_stopped)
495 return;
496
497 /* Tell the proc server we are continuing. */
498 __USEPORT (PROC, __proc_mark_cont (port));
499 /* Fetch ports to all our threads and resume them. */
500 err = __task_threads (__mach_task_self (), &threads, &nthreads);
501 assert_perror (err);
502 for (i = 0; i < nthreads; ++i)
503 {
504 if (threads[i] != _hurd_msgport_thread &&
505 (act != handle || threads[i] != ss->thread))
506 {
507 err = __thread_resume (threads[i]);
508 assert_perror (err);
509 }
510 err = __mach_port_deallocate (__mach_task_self (),
511 threads[i]);
512 assert_perror (err);
513 }
514 __vm_deallocate (__mach_task_self (),
515 (vm_address_t) threads,
516 nthreads * sizeof *threads);
517 _hurd_stopped = 0;
518 /* The thread that will run the handler is already suspended. */
519 ss_suspended = 1;
520 }
521
522 if (signo == 0)
523 {
524 if (untraced)
525 /* This is PTRACE_CONTINUE. */
526 resume ();
527
528 /* This call is just to check for pending signals. */
529 __spin_lock (&ss->lock);
530 goto check_pending_signals;
531 }
532
533 post_signal:
534
535 thread_state.set = 0; /* We know nothing. */
536
537 /* Check for a preempted signal. Preempted signals
538 can arrive during critical sections. */
539 __mutex_lock (&_hurd_signal_preempt_lock);
540 for (pe = _hurd_signal_preempt[signo]; pe != NULL; pe = pe->next)
541 if (pe->handler && sigcode >= pe->first && sigcode <= pe->last)
542 {
543 preempt = pe->handler;
544 break;
545 }
546 __mutex_unlock (&_hurd_signal_preempt_lock);
547
548 handler = SIG_DFL;
549 if (preempt)
550 /* Let the preempting handler examine the thread.
551 If it returns SIG_DFL, we run the normal handler;
552 otherwise we use the handler it returns. */
553 handler = (*preempt) (ss->thread, signo, sigcode, sigerror);
554
555 ss_suspended = 0;
556
557 if (handler != SIG_DFL)
558 /* Run the preemption-provided handler. */
559 act = handle;
560 else
561 {
562 /* No preemption. Do normal handling. */
563
564 __spin_lock (&ss->lock);
565
566 if (!untraced && (_hurd_exec_flags & EXEC_TRACED))
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
580 handler = ss->actions[signo].sa_handler;
581
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))
656 resume ();
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
672 /* Handle receipt of a blocked signal, or any signal while stopped. */
673 if (__sigismember (&ss->blocked, signo) ||
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)
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 }
692 else
693 /* Suspend the process. */
694 suspend ();
695 break;
696
697 case ignore:
698 /* Nobody cares about this signal. */
699 break;
700
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
709 case term: /* Time to die. */
710 case core: /* And leave a rotting corpse. */
711 /* Have the proc server stop all other threads in our task. */
712 err = __USEPORT (PROC, __proc_dostop (port, _hurd_msgport_thread));
713 assert_perror (err);
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 {
734 struct sigcontext *scp, ocontext;
735 int wait_for_reply, state_changed;
736
737 /* Stop the thread and abort its pending RPC operations. */
738 if (! ss_suspended)
739 {
740 err = __thread_suspend (ss->thread);
741 assert_perror (err);
742 }
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. */
749 abort_thread (ss, &thread_state, NULL, 0, 0);
750
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;
757
758 if (_hurdsig_catch_fault (SIGSEGV))
759 {
760 assert (_hurdsig_fault_sigcode >= (long int) ss->context &&
761 _hurdsig_fault_sigcode < (long int) (ss->context + 1));
762 /* We faulted reading the thread's stack. Forget that
763 context and pretend it wasn't there. It almost
764 certainly crash if this handler returns, but that's it's
765 problem. */
766 ss->context = NULL;
767 }
768 else
769 {
770 /* Copy the context from the thread's stack before
771 we start diddling the stack to set up the handler. */
772 ocontext = *ss->context;
773 ss->context = &ocontext;
774 }
775 _hurdsig_end_catch_fault ();
776
777 if (! machine_get_basic_state (ss->thread, &thread_state))
778 goto sigbomb;
779 loc = interrupted_reply_port_location (&thread_state, 1);
780 if (loc && *loc != MACH_PORT_NULL)
781 /* This is the reply port for the context which called
782 sigreturn. Since we are abandoning that context entirely
783 and restoring SS->context instead, destroy this port. */
784 __mach_port_destroy (__mach_task_self (), *loc);
785
786 /* The thread was in sigreturn, not in any interruptible RPC. */
787 wait_for_reply = 0;
788
789 assert (! ss->critical_section);
790 }
791 else
792 {
793 wait_for_reply
794 = (_hurdsig_abort_rpcs (ss, signo, 1,
795 &thread_state, &state_changed,
796 &reply_port, reply_port_type, untraced)
797 != MACH_PORT_NULL);
798
799 if (ss->critical_section)
800 {
801 /* The thread is in a critical section. Mark the signal as
802 pending. When it finishes the critical section, it will
803 check for pending signals. */
804 mark_pending ();
805 assert (! state_changed);
806 __thread_resume (ss->thread);
807 break;
808 }
809 }
810
811 /* Call the machine-dependent function to set the thread up
812 to run the signal handler, and preserve its old context. */
813 scp = _hurd_setup_sighandler (ss, handler,
814 signo, sigcode,
815 wait_for_reply, &thread_state);
816 if (scp == NULL)
817 goto sigbomb;
818
819 /* Set the machine-independent parts of the signal context. */
820
821 {
822 /* Fetch the thread variable for the MiG reply port,
823 and set it to MACH_PORT_NULL. */
824 mach_port_t *loc = interrupted_reply_port_location (&thread_state,
825 1);
826 if (loc)
827 {
828 scp->sc_reply_port = *loc;
829 *loc = MACH_PORT_NULL;
830 }
831 else
832 scp->sc_reply_port = MACH_PORT_NULL;
833
834 /* Save the intr_port in use by the interrupted code,
835 and clear the cell before running the trampoline. */
836 scp->sc_intr_port = ss->intr_port;
837 ss->intr_port = MACH_PORT_NULL;
838
839 if (ss->context)
840 {
841 /* After the handler runs we will restore to the state in
842 SS->context, not the state of the thread now. So restore
843 that context's reply port and intr port. */
844
845 scp->sc_reply_port = ss->context->sc_reply_port;
846 scp->sc_intr_port = ss->context->sc_intr_port;
847
848 ss->context = NULL;
849 }
850 }
851
852 /* Backdoor extra argument to signal handler. */
853 scp->sc_error = sigerror;
854
855 /* Block SIGNO and requested signals while running the handler. */
856 scp->sc_mask = ss->blocked;
857 ss->blocked |= __sigmask (signo) | ss->actions[signo].sa_mask;
858
859 /* Start the thread running the handler (or possibly waiting for an
860 RPC reply before running the handler). */
861 err = __thread_set_state (ss->thread, MACHINE_THREAD_STATE_FLAVOR,
862 (natural_t *) &thread_state.basic,
863 MACHINE_THREAD_STATE_COUNT);
864 assert_perror (err);
865 err = __thread_resume (ss->thread);
866 assert_perror (err);
867 thread_state.set = 0; /* Everything we know is now wrong. */
868 break;
869 }
870 }
871
872 /* The signal has either been ignored or is now being handled. We can
873 consider it delivered and reply to the killer. */
874 reply ();
875
876 /* We get here unless the signal was fatal. We still hold SS->lock.
877 Check for pending signals, and loop to post them. */
878 {
879 /* Return nonzero if SS has any signals pending we should worry about.
880 We don't worry about any pending signals if we are stopped, nor if
881 SS is in a critical section. We are guaranteed to get a sig_post
882 message before any of them become deliverable: either the SIGCONT
883 signal, or a sig_post with SIGNO==0 as an explicit poll when the
884 thread finishes its critical section. */
885 inline int signals_pending (void)
886 {
887 if (_hurd_stopped || ss->critical_section)
888 return 0;
889 return pending = ss->pending & ~ss->blocked;
890 }
891
892 check_pending_signals:
893 untraced = 0;
894
895 if (signals_pending ())
896 {
897 pending:
898 for (signo = 1; signo < NSIG; ++signo)
899 if (__sigismember (&pending, signo))
900 {
901 __sigdelset (&ss->pending, signo);
902 sigcode = ss->pending_data[signo].code;
903 sigerror = ss->pending_data[signo].error;
904 __spin_unlock (&ss->lock);
905 goto post_signal;
906 }
907 }
908
909 /* No pending signals left undelivered for this thread.
910 If we were sent signal 0, we need to check for pending
911 signals for all threads. */
912 if (signo == 0)
913 {
914 __spin_unlock (&ss->lock);
915 __mutex_lock (&_hurd_siglock);
916 for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
917 {
918 __spin_lock (&ss->lock);
919 if (signals_pending ())
920 goto pending;
921 __spin_unlock (&ss->lock);
922 }
923 __mutex_unlock (&_hurd_siglock);
924 }
925 else
926 {
927 /* No more signals pending; SS->lock is still locked.
928 Wake up any sigsuspend call that is blocking SS->thread. */
929 if (ss->suspended != MACH_PORT_NULL)
930 {
931 /* There is a sigsuspend waiting. Tell it to wake up. */
932 error_t err;
933 mach_msg_header_t msg;
934 err = __mach_port_insert_right (__mach_task_self (),
935 ss->suspended, ss->suspended,
936 MACH_MSG_TYPE_MAKE_SEND);
937 assert_perror (err);
938 msg.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_MOVE_SEND, 0);
939 msg.msgh_remote_port = ss->suspended;
940 msg.msgh_local_port = MACH_PORT_NULL;
941 /* These values do not matter. */
942 msg.msgh_id = 8675309; /* Jenny, Jenny. */
943 msg.msgh_seqno = 17; /* Random. */
944 ss->suspended = MACH_PORT_NULL;
945 err = __mach_msg (&msg, MACH_SEND_MSG, sizeof msg, 0,
946 MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE,
947 MACH_PORT_NULL);
948 assert_perror (err);
949 }
950 __spin_unlock (&ss->lock);
951 }
952 }
953
954 /* All pending signals delivered to all threads.
955 Now we can send the reply message even for signal 0. */
956 reply ();
957 }
958 \f
959 /* Decide whether REFPORT enables the sender to send us a SIGNO signal.
960 Returns zero if so, otherwise the error code to return to the sender. */
961
962 static error_t
963 signal_allowed (int signo, mach_port_t refport)
964 {
965 if (signo < 0 || signo >= NSIG)
966 return EINVAL;
967
968 if (refport == __mach_task_self ())
969 /* Can send any signal. */
970 goto win;
971
972 /* Avoid needing to check for this below. */
973 if (refport == MACH_PORT_NULL)
974 return EPERM;
975
976 switch (signo)
977 {
978 case SIGINT:
979 case SIGQUIT:
980 case SIGTSTP:
981 case SIGHUP:
982 case SIGINFO:
983 case SIGTTIN:
984 case SIGTTOU:
985 /* Job control signals can be sent by the controlling terminal. */
986 if (__USEPORT (CTTYID, port == refport))
987 goto win;
988 break;
989
990 case SIGCONT:
991 {
992 /* A continue signal can be sent by anyone in the session. */
993 mach_port_t sessport;
994 if (! __USEPORT (PROC, __proc_getsidport (port, &sessport)))
995 {
996 __mach_port_deallocate (__mach_task_self (), sessport);
997 if (refport == sessport)
998 goto win;
999 }
1000 }
1001 break;
1002
1003 case SIGIO:
1004 case SIGURG:
1005 {
1006 /* Any io object a file descriptor refers to might send us
1007 one of these signals using its async ID port for REFPORT.
1008
1009 This is pretty wide open; it is not unlikely that some random
1010 process can at least open for reading something we have open,
1011 get its async ID port, and send us a spurious SIGIO or SIGURG
1012 signal. But BSD is actually wider open than that!--you can set
1013 the owner of an io object to any process or process group
1014 whatsoever and send them gratuitous signals.
1015
1016 Someday we could implement some reasonable scheme for
1017 authorizing SIGIO and SIGURG signals properly. */
1018
1019 int d;
1020 __mutex_lock (&_hurd_dtable_lock);
1021 for (d = 0; (unsigned int) d < (unsigned int) _hurd_dtablesize; ++d)
1022 {
1023 struct hurd_userlink ulink;
1024 io_t port;
1025 mach_port_t asyncid;
1026 if (_hurd_dtable[d] == NULL)
1027 continue;
1028 port = _hurd_port_get (&_hurd_dtable[d]->port, &ulink);
1029 if (! __io_get_icky_async_id (port, &asyncid))
1030 {
1031 if (refport == asyncid)
1032 /* Break out of the loop on the next iteration. */
1033 d = -1;
1034 __mach_port_deallocate (__mach_task_self (), asyncid);
1035 }
1036 _hurd_port_free (&_hurd_dtable[d]->port, &ulink, port);
1037 }
1038 /* If we found a lucky winner, we've set D to -1 in the loop. */
1039 if (d < 0)
1040 goto win;
1041 }
1042 }
1043
1044 /* If this signal is legit, we have done `goto win' by now.
1045 When we return the error, mig deallocates REFPORT. */
1046 return EPERM;
1047
1048 win:
1049 /* Deallocate the REFPORT send right; we are done with it. */
1050 __mach_port_deallocate (__mach_task_self (), refport);
1051
1052 return 0;
1053 }
1054
1055 /* Implement the sig_post RPC from <hurd/msg.defs>;
1056 sent when someone wants us to get a signal. */
1057 kern_return_t
1058 _S_msg_sig_post (mach_port_t me,
1059 mach_port_t reply_port, mach_msg_type_name_t reply_port_type,
1060 int signo,
1061 mach_port_t refport)
1062 {
1063 error_t err;
1064
1065 if (err = signal_allowed (signo, refport))
1066 return err;
1067
1068 /* Post the signal to the designated signal-receiving thread. This will
1069 reply when the signal can be considered delivered. */
1070 _hurd_internal_post_signal (_hurd_thread_sigstate (_hurd_sigthread),
1071 signo, 0, 0, reply_port, reply_port_type,
1072 0); /* Stop if traced. */
1073
1074 return MIG_NO_REPLY; /* Already replied. */
1075 }
1076
1077 /* Implement the sig_post_untraced RPC from <hurd/msg.defs>;
1078 sent when the debugger wants us to really get a signal
1079 even if we are traced. */
1080 kern_return_t
1081 _S_msg_sig_post_untraced (mach_port_t me,
1082 mach_port_t reply_port,
1083 mach_msg_type_name_t reply_port_type,
1084 int signo,
1085 mach_port_t refport)
1086 {
1087 error_t err;
1088
1089 if (err = signal_allowed (signo, refport))
1090 return err;
1091
1092 /* Post the signal to the designated signal-receiving thread. This will
1093 reply when the signal can be considered delivered. */
1094 _hurd_internal_post_signal (_hurd_thread_sigstate (_hurd_sigthread),
1095 signo, 0, 0, reply_port, reply_port_type,
1096 1); /* Untraced flag. */
1097
1098 return MIG_NO_REPLY; /* Already replied. */
1099 }
1100 \f
1101 extern void __mig_init (void *);
1102
1103 #include <mach/task_special_ports.h>
1104
1105 /* Initialize the message port and _hurd_sigthread and start the signal
1106 thread. */
1107
1108 void
1109 _hurdsig_init (void)
1110 {
1111 error_t err;
1112 vm_size_t stacksize;
1113
1114 __mutex_init (&_hurd_siglock);
1115
1116 err = __mach_port_allocate (__mach_task_self (),
1117 MACH_PORT_RIGHT_RECEIVE,
1118 &_hurd_msgport);
1119 assert_perror (err);
1120
1121 /* Make a send right to the signal port. */
1122 err = __mach_port_insert_right (__mach_task_self (),
1123 _hurd_msgport,
1124 _hurd_msgport,
1125 MACH_MSG_TYPE_MAKE_SEND);
1126 assert_perror (err);
1127
1128 /* Set the default thread to receive task-global signals
1129 to this one, the main (first) user thread. */
1130 _hurd_sigthread = __mach_thread_self ();
1131
1132 /* Start the signal thread listening on the message port. */
1133
1134 err = __thread_create (__mach_task_self (), &_hurd_msgport_thread);
1135 assert_perror (err);
1136
1137 stacksize = __vm_page_size * 4; /* Small stack for signal thread. */
1138 err = __mach_setup_thread (__mach_task_self (), _hurd_msgport_thread,
1139 _hurd_msgport_receive,
1140 (vm_address_t *) &__hurd_sigthread_stack_base,
1141 &stacksize);
1142 assert_perror (err);
1143
1144 __hurd_sigthread_stack_end = __hurd_sigthread_stack_base + stacksize;
1145 __hurd_sigthread_variables =
1146 malloc (__hurd_threadvar_max * sizeof (unsigned long int));
1147 if (__hurd_sigthread_variables == NULL)
1148 __libc_fatal ("hurd: Can't allocate thread variables for signal thread\n");
1149
1150 /* Reinitialize the MiG support routines so they will use a per-thread
1151 variable for the cached reply port. */
1152 __mig_init ((void *) __hurd_sigthread_stack_base);
1153
1154 err = __thread_resume (_hurd_msgport_thread);
1155 assert_perror (err);
1156
1157 /* Receive exceptions on the signal port. */
1158 __task_set_special_port (__mach_task_self (),
1159 TASK_EXCEPTION_PORT, _hurd_msgport);
1160 }
1161 \f /* XXXX */
1162 /* Reauthenticate with the proc server. */
1163
1164 static void
1165 reauth_proc (mach_port_t new)
1166 {
1167 mach_port_t ref, ignore;
1168
1169 ref = __mach_reply_port ();
1170 if (! HURD_PORT_USE (&_hurd_ports[INIT_PORT_PROC],
1171 __proc_reauthenticate (port, ref,
1172 MACH_MSG_TYPE_MAKE_SEND) ||
1173 __auth_user_authenticate (new, port, ref,
1174 MACH_MSG_TYPE_MAKE_SEND,
1175 &ignore))
1176 && ignore != MACH_PORT_NULL)
1177 __mach_port_deallocate (__mach_task_self (), ignore);
1178 __mach_port_destroy (__mach_task_self (), ref);
1179
1180 (void) &reauth_proc; /* Silence compiler warning. */
1181 }
1182 text_set_element (_hurd_reauth_hook, reauth_proc);
1183 \f
1184 /* Like `getenv', but safe for the signal thread to run.
1185 If the environment is trashed, this will just return NULL. */
1186
1187 const char *
1188 _hurdsig_getenv (const char *variable)
1189 {
1190 if (_hurdsig_catch_fault (SIGSEGV))
1191 /* We bombed in getenv. */
1192 return NULL;
1193 else
1194 {
1195 const char *value = getenv (variable);
1196 /* Fault now if VALUE is a bogus string. */
1197 (void) strlen (value);
1198 _hurdsig_end_catch_fault ();
1199 return value;
1200 }
1201 }
This page took 0.091745 seconds and 4 git commands to generate.