1 /* Copyright (C) 1991, 92, 93, 94, 95, 96, 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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. */
22 #include <hurd/signal.h>
23 #include <cthreads.h> /* For `struct mutex'. */
25 #include "hurdfault.h"
26 #include "hurdmalloc.h" /* XXX */
28 const char *_hurdsig_getenv (const char *);
30 struct mutex _hurd_siglock;
33 /* Port that receives signals and other miscellaneous messages. */
34 mach_port_t _hurd_msgport;
36 /* Thread listening on it. */
37 thread_t _hurd_msgport_thread;
39 /* Thread which receives task-global signals. */
40 thread_t _hurd_sigthread;
42 /* Linked-list of per-thread signal state. */
43 struct hurd_sigstate *_hurd_sigstates;
45 /* Timeout for RPC's after interrupt_operation. */
46 mach_msg_timeout_t _hurd_interrupted_rpc_timeout = 3000;
49 default_sigaction (struct sigaction actions[NSIG])
53 __sigemptyset (&actions[0].sa_mask);
54 actions[0].sa_flags = SA_RESTART;
55 actions[0].sa_handler = SIG_DFL;
57 for (signo = 1; signo < NSIG; ++signo)
58 actions[signo] = actions[0];
61 struct hurd_sigstate *
62 _hurd_thread_sigstate (thread_t thread)
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)
71 ss = malloc (sizeof (*ss));
73 __libc_fatal ("hurd: Can't allocate thread sigstate\n");
75 __spin_lock_init (&ss->lock);
77 /* Initialize default state. */
78 __sigemptyset (&ss->blocked);
79 __sigemptyset (&ss->pending);
80 memset (&ss->sigaltstack, 0, sizeof (ss->sigaltstack));
81 ss->preempters = NULL;
83 ss->intr_port = MACH_PORT_NULL;
86 /* Initialize the sigaction vector from the default signal receiving
87 thread's state, and its from the system defaults. */
88 if (thread == _hurd_sigthread)
89 default_sigaction (ss->actions);
92 struct hurd_sigstate *s;
93 for (s = _hurd_sigstates; s != NULL; s = s->next)
94 if (s->thread == _hurd_sigthread)
98 __spin_lock (&s->lock);
99 memcpy (ss->actions, s->actions, sizeof (s->actions));
100 __spin_unlock (&s->lock);
103 default_sigaction (ss->actions);
106 ss->next = _hurd_sigstates;
107 _hurd_sigstates = ss;
109 __mutex_unlock (&_hurd_siglock);
113 /* Signal delivery itself is on this page. */
116 #include <hurd/crash.h>
117 #include <hurd/paths.h>
120 #include <sys/wait.h>
121 #include "thread_state.h"
122 #include <hurd/msg_server.h>
123 #include <hurd/msg_reply.h> /* For __msg_sig_post_reply. */
124 #include <hurd/interrupt.h>
128 int _hurd_core_limit; /* XXX */
130 /* Call the crash dump server to mummify us before we die.
131 Returns nonzero if a core file was written. */
133 write_corefile (int signo, const struct hurd_signal_detail *detail)
136 mach_port_t coreserver;
137 file_t file, coredir;
141 When we have a protocol to make the server return an error
142 for RLIMIT_FSIZE, then tell the corefile fs server the RLIMIT_CORE
143 value in place of the RLIMIT_FSIZE value. */
145 /* First get a port to the core dumping server. */
146 coreserver = MACH_PORT_NULL;
147 name = _hurdsig_getenv ("CRASHSERVER");
149 coreserver = __file_name_lookup (name, 0, 0);
150 if (coreserver == MACH_PORT_NULL)
151 coreserver = __file_name_lookup (_SERVERS_CRASH, 0, 0);
152 if (coreserver == MACH_PORT_NULL)
155 /* Get a port to the directory where the new core file will reside. */
156 name = _hurdsig_getenv ("COREFILE");
159 coredir = __file_name_split (name, (char **) &name);
160 if (coredir == MACH_PORT_NULL)
162 /* Create the new file, but don't link it into the directory yet. */
163 if (err = __dir_mkfile (coredir, O_WRONLY|O_CREAT,
164 0600 & ~_hurd_umask, /* XXX ? */
168 /* Call the core dumping server to write the core file. */
169 err = __crash_dump_task (coreserver,
172 signo, detail->code, detail->error,
173 detail->exc, detail->exc_code, detail->exc_subcode,
174 _hurd_ports[INIT_PORT_CTTYID].port,
175 MACH_MSG_TYPE_COPY_SEND);
176 __mach_port_deallocate (__mach_task_self (), coreserver);
178 /* The core dump into FILE succeeded, so now link it into the
180 err = __dir_link (file, coredir, name, 1);
181 __mach_port_deallocate (__mach_task_self (), file);
182 __mach_port_deallocate (__mach_task_self (), coredir);
187 /* The lowest-numbered thread state flavor value is 1,
188 so we use bit 0 in machine_thread_all_state.set to
189 record whether we have done thread_abort. */
190 #define THREAD_ABORTED 1
192 /* SS->thread is suspended. Abort the thread and get its basic state. */
194 abort_thread (struct hurd_sigstate *ss, struct machine_thread_all_state *state,
195 void (*reply) (void))
197 if (!(state->set & THREAD_ABORTED))
199 error_t err = __thread_abort (ss->thread);
201 /* Clear all thread state flavor set bits, because thread_abort may
202 have changed the state. */
203 state->set = THREAD_ABORTED;
209 machine_get_basic_state (ss->thread, state);
212 /* Find the location of the MiG reply port cell in use by the thread whose
213 state is described by THREAD_STATE. If SIGTHREAD is nonzero, make sure
214 that this location can be set without faulting, or else return NULL. */
217 interrupted_reply_port_location (struct machine_thread_all_state *thread_state,
220 mach_port_t *portloc = (mach_port_t *) __hurd_threadvar_location_from_sp
221 (_HURD_THREADVAR_MIG_REPLY, (void *) thread_state->basic.SP);
223 if (sigthread && _hurdsig_catch_memory_fault (portloc))
224 /* Faulted trying to read the stack. */
227 /* Fault now if this pointer is bogus. */
228 *(volatile mach_port_t *) portloc = *portloc;
231 _hurdsig_end_catch_fault ();
236 #include <hurd/sigpreempt.h>
237 #include "intr-msg.h"
239 /* Timeout on interrupt_operation calls. */
240 mach_msg_timeout_t _hurdsig_interrupt_timeout = 1000;
242 /* SS->thread is suspended.
244 Abort any interruptible RPC operation the thread is doing.
246 This uses only the constant member SS->thread and the unlocked, atomically
247 set member SS->intr_port, so no locking is needed.
249 If successfully sent an interrupt_operation and therefore the thread should
250 wait for its pending RPC to return (possibly EINTR) before taking the
251 incoming signal, returns the reply port to be received on. Otherwise
252 returns MACH_PORT_NULL.
254 SIGNO is used to find the applicable SA_RESTART bit. If SIGNO is zero,
255 the RPC fails with EINTR instead of restarting (thread_cancel).
257 *STATE_CHANGE is set nonzero if STATE->basic was modified and should
258 be applied back to the thread if it might ever run again, else zero. */
261 _hurdsig_abort_rpcs (struct hurd_sigstate *ss, int signo, int sigthread,
262 struct machine_thread_all_state *state, int *state_change,
263 void (*reply) (void))
265 extern const void _hurd_intr_rpc_msg_in_trap;
266 mach_port_t rcv_port = MACH_PORT_NULL;
267 mach_port_t intr_port;
271 intr_port = ss->intr_port;
272 if (intr_port == MACH_PORT_NULL)
273 /* No interruption needs done. */
274 return MACH_PORT_NULL;
276 /* Abort the thread's kernel context, so any pending message send or
277 receive completes immediately or aborts. */
278 abort_thread (ss, state, reply);
280 if (state->basic.PC < (natural_t) &_hurd_intr_rpc_msg_in_trap)
282 /* The thread is about to do the RPC, but hasn't yet entered
283 mach_msg. Mutate the thread's state so it knows not to try
285 INTR_MSG_BACK_OUT (&state->basic);
286 MACHINE_THREAD_STATE_SET_PC (&state->basic,
287 &_hurd_intr_rpc_msg_in_trap);
288 state->basic.SYSRETURN = MACH_SEND_INTERRUPTED;
291 else if (state->basic.PC == (natural_t) &_hurd_intr_rpc_msg_in_trap &&
292 /* The thread was blocked in the system call. After thread_abort,
293 the return value register indicates what state the RPC was in
295 state->basic.SYSRETURN == MACH_RCV_INTERRUPTED)
297 /* The RPC request message was sent and the thread was waiting for
298 the reply message; now the message receive has been aborted, so
299 the mach_msg call will return MACH_RCV_INTERRUPTED. We must tell
300 the server to interrupt the pending operation. The thread must
301 wait for the reply message before running the signal handler (to
302 guarantee that the operation has finished being interrupted), so
303 our nonzero return tells the trampoline code to finish the message
304 receive operation before running the handler. */
306 mach_port_t *reply = interrupted_reply_port_location (state,
308 error_t err = __interrupt_operation (intr_port, _hurdsig_interrupt_timeout);
314 /* The interrupt didn't work.
315 Destroy the receive right the thread is blocked on. */
316 __mach_port_destroy (__mach_task_self (), *reply);
317 *reply = MACH_PORT_NULL;
320 /* The system call return value register now contains
321 MACH_RCV_INTERRUPTED; when mach_msg resumes, it will retry the
322 call. Since we have just destroyed the receive right, the
323 retry will fail with MACH_RCV_INVALID_NAME. Instead, just
324 change the return value here to EINTR so mach_msg will not
325 retry and the EINTR error code will propagate up. */
326 state->basic.SYSRETURN = EINTR;
332 /* All threads whose RPCs were interrupted by the interrupt_operation
333 call above will retry their RPCs unless we clear SS->intr_port.
334 So we clear it for the thread taking a signal when SA_RESTART is
335 clear, so that its call returns EINTR. */
336 if (! signo || !(ss->actions[signo].sa_flags & SA_RESTART))
337 ss->intr_port = MACH_PORT_NULL;
344 /* Abort the RPCs being run by all threads but this one;
345 all other threads should be suspended. If LIVE is nonzero, those
346 threads may run again, so they should be adjusted as necessary to be
347 happy when resumed. STATE is clobbered as a scratch area; its initial
348 contents are ignored, and its contents on return are not useful. */
351 abort_all_rpcs (int signo, struct machine_thread_all_state *state, int live)
353 /* We can just loop over the sigstates. Any thread doing something
354 interruptible must have one. We needn't bother locking because all
355 other threads are stopped. */
357 struct hurd_sigstate *ss;
359 mach_port_t *reply_ports;
361 /* First loop over the sigstates to count them.
362 We need to know how big a vector we will need for REPLY_PORTS. */
364 for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
367 reply_ports = alloca (nthreads * sizeof *reply_ports);
370 for (ss = _hurd_sigstates; ss != NULL; ss = ss->next, ++nthreads)
371 if (ss->thread == _hurd_msgport_thread)
372 reply_ports[nthreads] = MACH_PORT_NULL;
376 state->set = 0; /* Reset scratch area. */
378 /* Abort any operation in progress with interrupt_operation.
379 Record the reply port the thread is waiting on.
380 We will wait for all the replies below. */
381 reply_ports[nthreads] = _hurdsig_abort_rpcs (ss, signo, 1,
382 state, &state_changed,
386 if (reply_ports[nthreads] != MACH_PORT_NULL)
388 /* We will wait for the reply to this RPC below, so the
389 thread must issue a new RPC rather than waiting for the
390 reply to the one it sent. */
391 state->basic.SYSRETURN = EINTR;
395 /* Aborting the RPC needed to change this thread's state,
396 and it might ever run again. So write back its state. */
397 __thread_set_state (ss->thread, MACHINE_THREAD_STATE_FLAVOR,
398 (natural_t *) &state->basic,
399 MACHINE_THREAD_STATE_COUNT);
403 /* Wait for replies from all the successfully interrupted RPCs. */
404 while (nthreads-- > 0)
405 if (reply_ports[nthreads] != MACH_PORT_NULL)
408 mach_msg_header_t head;
409 err = __mach_msg (&head, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof head,
410 reply_ports[nthreads],
411 _hurd_interrupted_rpc_timeout, MACH_PORT_NULL);
414 case MACH_RCV_TIMED_OUT:
415 case MACH_RCV_TOO_LARGE:
424 struct hurd_signal_preempter *_hurdsig_preempters;
425 sigset_t _hurdsig_preempted_set;
427 /* Mask of stop signals. */
428 #define STOPSIGS (sigmask (SIGTTIN) | sigmask (SIGTTOU) | \
429 sigmask (SIGSTOP) | sigmask (SIGTSTP))
431 /* Deliver a signal. SS is not locked. */
433 _hurd_internal_post_signal (struct hurd_sigstate *ss,
434 int signo, struct hurd_signal_detail *detail,
435 mach_port_t reply_port,
436 mach_msg_type_name_t reply_port_type,
440 struct machine_thread_all_state thread_state;
441 enum { stop, ignore, core, term, handle } act;
442 struct hurd_signal_preempter *pe;
443 sighandler_t handler;
447 /* Reply to this sig_post message. */
448 __typeof (__msg_sig_post_reply) *reply_rpc
449 = (untraced ? __msg_sig_post_untraced_reply : __msg_sig_post_reply);
453 if (reply_port == MACH_PORT_NULL)
455 err = (*reply_rpc) (reply_port, reply_port_type, 0);
456 reply_port = MACH_PORT_NULL;
457 if (err != MACH_SEND_INVALID_DEST) /* Ignore dead reply port. */
461 /* Mark the signal as pending. */
462 void mark_pending (void)
464 __sigaddset (&ss->pending, signo);
465 /* Save the details to be given to the handler when SIGNO is
467 ss->pending_data[signo] = *detail;
470 /* Suspend the process with SIGNO. */
473 /* Stop all other threads and mark ourselves stopped. */
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);
483 __proc_mark_stop (port, signo, detail->code);
487 /* Resume the process after a suspension. */
490 /* Resume the process from being stopped. */
492 mach_msg_type_number_t nthreads, i;
498 /* Tell the proc server we are continuing. */
499 __USEPORT (PROC, __proc_mark_cont (port));
500 /* Fetch ports to all our threads and resume them. */
501 err = __task_threads (__mach_task_self (), &threads, &nthreads);
503 for (i = 0; i < nthreads; ++i)
505 if (threads[i] != _hurd_msgport_thread &&
506 (act != handle || threads[i] != ss->thread))
508 err = __thread_resume (threads[i]);
511 err = __mach_port_deallocate (__mach_task_self (),
515 __vm_deallocate (__mach_task_self (),
516 (vm_address_t) threads,
517 nthreads * sizeof *threads);
519 /* The thread that will run the handler is already suspended. */
526 /* This is PTRACE_CONTINUE. */
529 /* This call is just to check for pending signals. */
530 __spin_lock (&ss->lock);
531 goto check_pending_signals;
536 thread_state.set = 0; /* We know nothing. */
538 __spin_lock (&ss->lock);
540 /* Check for a preempted signal. Preempted signals can arrive during
541 critical sections. */
544 for (pe = ss->preempters; pe && handler == SIG_ERR; pe = pe->next)
545 if (HURD_PREEMPT_SIGNAL_P (pe, signo, detail->code))
546 handler = (*pe->preempter) (pe, ss, &signo, detail);
548 if (handler == SIG_ERR && (__sigmask (signo) & _hurdsig_preempted_set))
550 __mutex_lock (&_hurd_siglock);
551 for (pe = _hurdsig_preempters; pe && handler == SIG_ERR; pe = pe->next)
552 if (HURD_PREEMPT_SIGNAL_P (pe, signo, detail->code))
553 handler = (*pe->preempter) (pe, ss, &signo, detail);
554 __mutex_unlock (&_hurd_siglock);
559 if (handler == SIG_IGN)
560 /* Ignore the signal altogether. */
562 if (handler != SIG_ERR)
563 /* Run the preemption-provided handler. */
567 /* No preemption. Do normal handling. */
569 if (!untraced && __sigismember (&_hurdsig_traced, signo))
571 /* We are being traced. Stop to tell the debugger of the signal. */
573 /* Already stopped. Mark the signal as pending;
574 when resumed, we will notice it and stop again. */
578 __spin_unlock (&ss->lock);
583 handler = ss->actions[signo].sa_handler;
585 if (handler == SIG_DFL)
586 /* Figure out the default action for this signal. */
590 /* A sig_post msg with SIGNO==0 is sent to
591 tell us to check for pending signals. */
623 if (_hurd_pgrp == _hurd_pid)
625 /* We are the process group leader. Since there is no
626 user-specified handler for SIGINFO, we use a default one
627 which prints something interesting. We use the normal
628 handler mechanism instead of just doing it here to avoid
629 the signal thread faulting or blocking in this
630 potentially hairy operation. */
632 handler = _hurd_siginfo_handler;
642 else if (handler == SIG_IGN)
647 if (__sigmask (signo) & STOPSIGS)
648 /* Stop signals clear a pending SIGCONT even if they
649 are handled or ignored (but not if preempted). */
650 ss->pending &= ~sigmask (SIGCONT);
653 if (signo == SIGCONT)
654 /* Even if handled or ignored (but not preempted), SIGCONT clears
655 stop signals and resumes the process. */
656 ss->pending &= ~STOPSIGS;
658 if (_hurd_stopped && act != stop && (untraced || signo == SIGCONT))
663 if (_hurd_orphaned && act == stop &&
664 (__sigmask (signo) & (__sigmask (SIGTTIN) | __sigmask (SIGTTOU) |
665 __sigmask (SIGTSTP))))
667 /* If we would ordinarily stop for a job control signal, but we are
668 orphaned so noone would ever notice and continue us again, we just
669 quietly die, alone and in the dark. */
670 detail->code = signo;
675 /* Handle receipt of a blocked signal, or any signal while stopped. */
676 if (__sigismember (&ss->blocked, signo) ||
677 (signo != SIGKILL && _hurd_stopped))
680 /* If there was a call to resume above in SIGCONT processing
681 and we've left a thread suspended, now's the time to
683 if (act == handle && ss_suspended)
685 err = __thread_resume (ss->thread);
692 /* Perform the chosen action for the signal. */
698 /* We are already stopped, but receiving an untraced stop
699 signal. Instead of resuming and suspending again, just
700 notify the proc server of the new stop signal. */
701 error_t err = __USEPORT (PROC, __proc_mark_stop
702 (port, signo, detail->code));
706 /* Suspend the process. */
711 /* Nobody cares about this signal. */
715 /* We got a fault setting up the stack frame for the handler.
716 Nothing to do but die; BSD gets SIGILL in this case. */
717 detail->code = signo; /* XXX ? */
722 case term: /* Time to die. */
723 case core: /* And leave a rotting corpse. */
724 /* Have the proc server stop all other threads in our task. */
725 err = __USEPORT (PROC, __proc_dostop (port, _hurd_msgport_thread));
727 /* No more user instructions will be executed.
728 The signal can now be considered delivered. */
730 /* Abort all server operations now in progress. */
731 abort_all_rpcs (signo, &thread_state, 0);
734 int status = W_EXITCODE (0, signo);
735 /* Do a core dump if desired. Only set the wait status bit saying we
736 in fact dumped core if the operation was actually successful. */
737 if (act == core && write_corefile (signo, detail))
739 /* Tell proc how we died and then stick the saber in the gut. */
745 /* Call a handler for this signal. */
747 struct sigcontext *scp, ocontext;
748 int wait_for_reply, state_changed;
750 /* Stop the thread and abort its pending RPC operations. */
753 err = __thread_suspend (ss->thread);
757 /* Abort the thread's kernel context, so any pending message send
758 or receive completes immediately or aborts. If an interruptible
759 RPC is in progress, abort_rpcs will do this. But we must always
760 do it before fetching the thread's state, because
761 thread_get_state is never kosher before thread_abort. */
762 abort_thread (ss, &thread_state, NULL);
766 /* We have a previous sigcontext that sigreturn was about
767 to restore when another signal arrived. */
771 if (_hurdsig_catch_memory_fault (ss->context))
773 /* We faulted reading the thread's stack. Forget that
774 context and pretend it wasn't there. It almost
775 certainly crash if this handler returns, but that's it's
781 /* Copy the context from the thread's stack before
782 we start diddling the stack to set up the handler. */
783 ocontext = *ss->context;
784 ss->context = &ocontext;
786 _hurdsig_end_catch_fault ();
788 if (! machine_get_basic_state (ss->thread, &thread_state))
790 loc = interrupted_reply_port_location (&thread_state, 1);
791 if (loc && *loc != MACH_PORT_NULL)
792 /* This is the reply port for the context which called
793 sigreturn. Since we are abandoning that context entirely
794 and restoring SS->context instead, destroy this port. */
795 __mach_port_destroy (__mach_task_self (), *loc);
797 /* The thread was in sigreturn, not in any interruptible RPC. */
800 assert (! __spin_lock_locked (&ss->critical_section_lock));
805 = (_hurdsig_abort_rpcs (ss, signo, 1,
806 &thread_state, &state_changed,
810 if (__spin_lock_locked (&ss->critical_section_lock))
812 /* The thread is in a critical section. Mark the signal as
813 pending. When it finishes the critical section, it will
814 check for pending signals. */
816 assert (! state_changed);
817 __thread_resume (ss->thread);
822 /* Call the machine-dependent function to set the thread up
823 to run the signal handler, and preserve its old context. */
824 scp = _hurd_setup_sighandler (ss, handler, signo, detail,
825 wait_for_reply, &thread_state);
829 /* Set the machine-independent parts of the signal context. */
832 /* Fetch the thread variable for the MiG reply port,
833 and set it to MACH_PORT_NULL. */
834 mach_port_t *loc = interrupted_reply_port_location (&thread_state,
838 scp->sc_reply_port = *loc;
839 *loc = MACH_PORT_NULL;
842 scp->sc_reply_port = MACH_PORT_NULL;
844 /* Save the intr_port in use by the interrupted code,
845 and clear the cell before running the trampoline. */
846 scp->sc_intr_port = ss->intr_port;
847 ss->intr_port = MACH_PORT_NULL;
851 /* After the handler runs we will restore to the state in
852 SS->context, not the state of the thread now. So restore
853 that context's reply port and intr port. */
855 scp->sc_reply_port = ss->context->sc_reply_port;
856 scp->sc_intr_port = ss->context->sc_intr_port;
862 /* Backdoor extra argument to signal handler. */
863 scp->sc_error = detail->error;
865 /* Block SIGNO and requested signals while running the handler. */
866 scp->sc_mask = ss->blocked;
867 ss->blocked |= __sigmask (signo) | ss->actions[signo].sa_mask;
869 /* Start the thread running the handler (or possibly waiting for an
870 RPC reply before running the handler). */
871 err = __thread_set_state (ss->thread, MACHINE_THREAD_STATE_FLAVOR,
872 (natural_t *) &thread_state.basic,
873 MACHINE_THREAD_STATE_COUNT);
875 err = __thread_resume (ss->thread);
877 thread_state.set = 0; /* Everything we know is now wrong. */
882 /* The signal has either been ignored or is now being handled. We can
883 consider it delivered and reply to the killer. */
886 /* We get here unless the signal was fatal. We still hold SS->lock.
887 Check for pending signals, and loop to post them. */
889 /* Return nonzero if SS has any signals pending we should worry about.
890 We don't worry about any pending signals if we are stopped, nor if
891 SS is in a critical section. We are guaranteed to get a sig_post
892 message before any of them become deliverable: either the SIGCONT
893 signal, or a sig_post with SIGNO==0 as an explicit poll when the
894 thread finishes its critical section. */
895 inline int signals_pending (void)
897 if (_hurd_stopped || __spin_lock_locked (&ss->critical_section_lock))
899 return pending = ss->pending & ~ss->blocked;
902 check_pending_signals:
905 if (signals_pending ())
908 for (signo = 1; signo < NSIG; ++signo)
909 if (__sigismember (&pending, signo))
911 __sigdelset (&ss->pending, signo);
912 *detail = ss->pending_data[signo];
913 __spin_unlock (&ss->lock);
918 /* No pending signals left undelivered for this thread.
919 If we were sent signal 0, we need to check for pending
920 signals for all threads. */
923 __spin_unlock (&ss->lock);
924 __mutex_lock (&_hurd_siglock);
925 for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
927 __spin_lock (&ss->lock);
928 if (signals_pending ())
930 __spin_unlock (&ss->lock);
932 __mutex_unlock (&_hurd_siglock);
936 /* No more signals pending; SS->lock is still locked.
937 Wake up any sigsuspend call that is blocking SS->thread. */
938 if (ss->suspended != MACH_PORT_NULL)
940 /* There is a sigsuspend waiting. Tell it to wake up. */
942 mach_msg_header_t msg;
943 err = __mach_port_insert_right (__mach_task_self (),
944 ss->suspended, ss->suspended,
945 MACH_MSG_TYPE_MAKE_SEND);
947 msg.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_MOVE_SEND, 0);
948 msg.msgh_remote_port = ss->suspended;
949 msg.msgh_local_port = MACH_PORT_NULL;
950 /* These values do not matter. */
951 msg.msgh_id = 8675309; /* Jenny, Jenny. */
952 msg.msgh_seqno = 17; /* Random. */
953 ss->suspended = MACH_PORT_NULL;
954 err = __mach_msg (&msg, MACH_SEND_MSG, sizeof msg, 0,
955 MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE,
959 __spin_unlock (&ss->lock);
963 /* All pending signals delivered to all threads.
964 Now we can send the reply message even for signal 0. */
968 /* Decide whether REFPORT enables the sender to send us a SIGNO signal.
969 Returns zero if so, otherwise the error code to return to the sender. */
972 signal_allowed (int signo, mach_port_t refport)
974 if (signo < 0 || signo >= NSIG)
977 if (refport == __mach_task_self ())
978 /* Can send any signal. */
981 /* Avoid needing to check for this below. */
982 if (refport == MACH_PORT_NULL)
994 /* Job control signals can be sent by the controlling terminal. */
995 if (__USEPORT (CTTYID, port == refport))
1001 /* A continue signal can be sent by anyone in the session. */
1002 mach_port_t sessport;
1003 if (! __USEPORT (PROC, __proc_getsidport (port, &sessport)))
1005 __mach_port_deallocate (__mach_task_self (), sessport);
1006 if (refport == sessport)
1015 /* Any io object a file descriptor refers to might send us
1016 one of these signals using its async ID port for REFPORT.
1018 This is pretty wide open; it is not unlikely that some random
1019 process can at least open for reading something we have open,
1020 get its async ID port, and send us a spurious SIGIO or SIGURG
1021 signal. But BSD is actually wider open than that!--you can set
1022 the owner of an io object to any process or process group
1023 whatsoever and send them gratuitous signals.
1025 Someday we could implement some reasonable scheme for
1026 authorizing SIGIO and SIGURG signals properly. */
1029 int lucky = 0; /* True if we find a match for REFPORT. */
1030 __mutex_lock (&_hurd_dtable_lock);
1031 for (d = 0; !lucky && (unsigned) d < (unsigned) _hurd_dtablesize; ++d)
1033 struct hurd_userlink ulink;
1035 mach_port_t asyncid;
1036 if (_hurd_dtable[d] == NULL)
1038 port = _hurd_port_get (&_hurd_dtable[d]->port, &ulink);
1039 if (! __io_get_icky_async_id (port, &asyncid))
1041 if (refport == asyncid)
1042 /* Break out of the loop on the next iteration. */
1044 __mach_port_deallocate (__mach_task_self (), asyncid);
1046 _hurd_port_free (&_hurd_dtable[d]->port, &ulink, port);
1048 /* If we found a lucky winner, we've set D to -1 in the loop. */
1054 /* If this signal is legit, we have done `goto win' by now.
1055 When we return the error, mig deallocates REFPORT. */
1059 /* Deallocate the REFPORT send right; we are done with it. */
1060 __mach_port_deallocate (__mach_task_self (), refport);
1065 /* Implement the sig_post RPC from <hurd/msg.defs>;
1066 sent when someone wants us to get a signal. */
1068 _S_msg_sig_post (mach_port_t me,
1069 mach_port_t reply_port, mach_msg_type_name_t reply_port_type,
1070 int signo, natural_t sigcode,
1071 mach_port_t refport)
1074 struct hurd_signal_detail d;
1076 if (err = signal_allowed (signo, refport))
1082 /* Post the signal to the designated signal-receiving thread. This will
1083 reply when the signal can be considered delivered. */
1084 _hurd_internal_post_signal (_hurd_thread_sigstate (_hurd_sigthread),
1085 signo, &d, reply_port, reply_port_type,
1086 0); /* Stop if traced. */
1088 return MIG_NO_REPLY; /* Already replied. */
1091 /* Implement the sig_post_untraced RPC from <hurd/msg.defs>;
1092 sent when the debugger wants us to really get a signal
1093 even if we are traced. */
1095 _S_msg_sig_post_untraced (mach_port_t me,
1096 mach_port_t reply_port,
1097 mach_msg_type_name_t reply_port_type,
1098 int signo, natural_t sigcode,
1099 mach_port_t refport)
1102 struct hurd_signal_detail d;
1104 if (err = signal_allowed (signo, refport))
1110 /* Post the signal to the designated signal-receiving thread. This will
1111 reply when the signal can be considered delivered. */
1112 _hurd_internal_post_signal (_hurd_thread_sigstate (_hurd_sigthread),
1113 signo, &d, reply_port, reply_port_type,
1114 1); /* Untraced flag. */
1116 return MIG_NO_REPLY; /* Already replied. */
1119 extern void __mig_init (void *);
1121 #include <mach/task_special_ports.h>
1123 /* Initialize the message port and _hurd_sigthread and start the signal
1127 _hurdsig_init (void)
1130 vm_size_t stacksize;
1132 __mutex_init (&_hurd_siglock);
1134 err = __mach_port_allocate (__mach_task_self (),
1135 MACH_PORT_RIGHT_RECEIVE,
1137 assert_perror (err);
1139 /* Make a send right to the signal port. */
1140 err = __mach_port_insert_right (__mach_task_self (),
1143 MACH_MSG_TYPE_MAKE_SEND);
1144 assert_perror (err);
1146 /* Set the default thread to receive task-global signals
1147 to this one, the main (first) user thread. */
1148 _hurd_sigthread = __mach_thread_self ();
1150 /* Start the signal thread listening on the message port. */
1152 err = __thread_create (__mach_task_self (), &_hurd_msgport_thread);
1153 assert_perror (err);
1155 stacksize = __vm_page_size * 4; /* Small stack for signal thread. */
1156 err = __mach_setup_thread (__mach_task_self (), _hurd_msgport_thread,
1157 _hurd_msgport_receive,
1158 (vm_address_t *) &__hurd_sigthread_stack_base,
1160 assert_perror (err);
1162 __hurd_sigthread_stack_end = __hurd_sigthread_stack_base + stacksize;
1163 __hurd_sigthread_variables =
1164 malloc (__hurd_threadvar_max * sizeof (unsigned long int));
1165 if (__hurd_sigthread_variables == NULL)
1166 __libc_fatal ("hurd: Can't allocate thread variables for signal thread\n");
1168 /* Reinitialize the MiG support routines so they will use a per-thread
1169 variable for the cached reply port. */
1170 __mig_init ((void *) __hurd_sigthread_stack_base);
1172 err = __thread_resume (_hurd_msgport_thread);
1173 assert_perror (err);
1175 /* Receive exceptions on the signal port. */
1176 __task_set_special_port (__mach_task_self (),
1177 TASK_EXCEPTION_PORT, _hurd_msgport);
1180 /* Reauthenticate with the proc server. */
1183 reauth_proc (mach_port_t new)
1185 mach_port_t ref, ignore;
1187 ref = __mach_reply_port ();
1188 if (! HURD_PORT_USE (&_hurd_ports[INIT_PORT_PROC],
1189 __proc_reauthenticate (port, ref,
1190 MACH_MSG_TYPE_MAKE_SEND) ||
1191 __auth_user_authenticate (new, ref,
1192 MACH_MSG_TYPE_MAKE_SEND,
1194 && ignore != MACH_PORT_NULL)
1195 __mach_port_deallocate (__mach_task_self (), ignore);
1196 __mach_port_destroy (__mach_task_self (), ref);
1198 (void) &reauth_proc; /* Silence compiler warning. */
1200 text_set_element (_hurd_reauth_hook, reauth_proc);
1202 /* Like `getenv', but safe for the signal thread to run.
1203 If the environment is trashed, this will just return NULL. */
1206 _hurdsig_getenv (const char *variable)
1208 if (_hurdsig_catch_memory_fault (__environ))
1209 /* We bombed in getenv. */
1213 const size_t len = strlen (variable);
1215 char *volatile *ep = __environ;
1218 const char *p = *ep;
1219 _hurdsig_fault_preempter.first = (long int) p;
1220 _hurdsig_fault_preempter.last = VM_MAX_ADDRESS;
1221 if (! strncmp (p, variable, len) && p[len] == '=')
1226 valuelen = strlen (p);
1227 _hurdsig_fault_preempter.last = (long int) (p + valuelen);
1228 value = malloc (++valuelen);
1230 memcpy (value, p, valuelen);
1233 _hurdsig_fault_preempter.first = (long int) ++ep;
1234 _hurdsig_fault_preempter.last = (long int) (ep + 1);
1236 _hurdsig_end_catch_fault ();