1 /* Copyright (C) 1991-2018 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
22 #include <cthreads.h> /* For `struct mutex'. */
25 #include <mach/thread_switch.h>
29 #include <hurd/signal.h>
31 #include "hurdfault.h"
32 #include "hurdmalloc.h" /* XXX */
33 #include "../locale/localeinfo.h"
35 const char *_hurdsig_getenv (const char *);
37 struct mutex _hurd_siglock
;
40 /* Port that receives signals and other miscellaneous messages. */
41 mach_port_t _hurd_msgport
;
43 /* Thread listening on it. */
44 thread_t _hurd_msgport_thread
;
46 /* Thread which receives task-global signals. */
47 thread_t _hurd_sigthread
;
49 /* These are set up by _hurdsig_init. */
50 unsigned long int __hurd_sigthread_stack_base
;
51 unsigned long int __hurd_sigthread_stack_end
;
53 /* Linked-list of per-thread signal state. */
54 struct hurd_sigstate
*_hurd_sigstates
;
56 /* Timeout for RPC's after interrupt_operation. */
57 mach_msg_timeout_t _hurd_interrupted_rpc_timeout
= 3000;
60 default_sigaction (struct sigaction actions
[NSIG
])
64 __sigemptyset (&actions
[0].sa_mask
);
65 actions
[0].sa_flags
= SA_RESTART
;
66 actions
[0].sa_handler
= SIG_DFL
;
68 for (signo
= 1; signo
< NSIG
; ++signo
)
69 actions
[signo
] = actions
[0];
72 struct hurd_sigstate
*
73 _hurd_thread_sigstate (thread_t thread
)
75 struct hurd_sigstate
*ss
;
76 __mutex_lock (&_hurd_siglock
);
77 for (ss
= _hurd_sigstates
; ss
!= NULL
; ss
= ss
->next
)
78 if (ss
->thread
== thread
)
82 ss
= malloc (sizeof (*ss
));
84 __libc_fatal ("hurd: Can't allocate thread sigstate\n");
86 __spin_lock_init (&ss
->lock
);
88 /* Initialize default state. */
89 __sigemptyset (&ss
->blocked
);
90 __sigemptyset (&ss
->pending
);
91 memset (&ss
->sigaltstack
, 0, sizeof (ss
->sigaltstack
));
92 ss
->preemptors
= NULL
;
93 ss
->suspended
= MACH_PORT_NULL
;
94 ss
->intr_port
= MACH_PORT_NULL
;
97 /* Initialize the sigaction vector from the default signal receiving
98 thread's state, and its from the system defaults. */
99 if (thread
== _hurd_sigthread
)
100 default_sigaction (ss
->actions
);
103 struct hurd_sigstate
*s
;
104 for (s
= _hurd_sigstates
; s
!= NULL
; s
= s
->next
)
105 if (s
->thread
== _hurd_sigthread
)
109 __spin_lock (&s
->lock
);
110 memcpy (ss
->actions
, s
->actions
, sizeof (s
->actions
));
111 __spin_unlock (&s
->lock
);
114 default_sigaction (ss
->actions
);
117 ss
->next
= _hurd_sigstates
;
118 _hurd_sigstates
= ss
;
120 __mutex_unlock (&_hurd_siglock
);
124 /* Signal delivery itself is on this page. */
127 #include <hurd/crash.h>
128 #include <hurd/resource.h>
129 #include <hurd/paths.h>
132 #include <sys/wait.h>
133 #include <thread_state.h>
134 #include <hurd/msg_server.h>
135 #include <hurd/msg_reply.h> /* For __msg_sig_post_reply. */
136 #include <hurd/interrupt.h>
141 /* Call the crash dump server to mummify us before we die.
142 Returns nonzero if a core file was written. */
144 write_corefile (int signo
, const struct hurd_signal_detail
*detail
)
147 mach_port_t coreserver
;
148 file_t file
, coredir
;
151 /* Don't bother locking since we just read the one word. */
152 rlim_t corelimit
= _hurd_rlimits
[RLIMIT_CORE
].rlim_cur
;
155 /* No core dumping, thank you very much. Note that this makes
156 `ulimit -c 0' prevent crash-suspension too, which is probably
157 what the user wanted. */
161 When we have a protocol to make the server return an error
162 for RLIMIT_FSIZE, then tell the corefile fs server the RLIMIT_CORE
163 value in place of the RLIMIT_FSIZE value. */
165 /* First get a port to the core dumping server. */
166 coreserver
= MACH_PORT_NULL
;
167 name
= _hurdsig_getenv ("CRASHSERVER");
169 coreserver
= __file_name_lookup (name
, 0, 0);
170 if (coreserver
== MACH_PORT_NULL
)
171 coreserver
= __file_name_lookup (_SERVERS_CRASH
, 0, 0);
172 if (coreserver
== MACH_PORT_NULL
)
175 /* Get a port to the directory where the new core file will reside. */
176 file
= MACH_PORT_NULL
;
177 name
= _hurdsig_getenv ("COREFILE");
180 coredir
= __file_name_split (name
, (char **) &name
);
181 if (coredir
!= MACH_PORT_NULL
)
182 /* Create the new file, but don't link it into the directory yet. */
183 __dir_mkfile (coredir
, O_WRONLY
|O_CREAT
,
184 0600 & ~_hurd_umask
, /* XXX ? */
187 /* Call the core dumping server to write the core file. */
188 err
= __crash_dump_task (coreserver
,
191 signo
, detail
->code
, detail
->error
,
192 detail
->exc
, detail
->exc_code
, detail
->exc_subcode
,
193 _hurd_ports
[INIT_PORT_CTTYID
].port
,
194 MACH_MSG_TYPE_COPY_SEND
);
195 __mach_port_deallocate (__mach_task_self (), coreserver
);
197 if (! err
&& file
!= MACH_PORT_NULL
)
198 /* The core dump into FILE succeeded, so now link it into the
200 err
= __dir_link (coredir
, file
, name
, 1);
201 __mach_port_deallocate (__mach_task_self (), file
);
202 __mach_port_deallocate (__mach_task_self (), coredir
);
203 return !err
&& file
!= MACH_PORT_NULL
;
207 /* The lowest-numbered thread state flavor value is 1,
208 so we use bit 0 in machine_thread_all_state.set to
209 record whether we have done thread_abort. */
210 #define THREAD_ABORTED 1
212 /* SS->thread is suspended. Abort the thread and get its basic state. */
214 abort_thread (struct hurd_sigstate
*ss
, struct machine_thread_all_state
*state
,
215 void (*reply
) (void))
217 if (!(state
->set
& THREAD_ABORTED
))
219 error_t err
= __thread_abort (ss
->thread
);
221 /* Clear all thread state flavor set bits, because thread_abort may
222 have changed the state. */
223 state
->set
= THREAD_ABORTED
;
229 machine_get_basic_state (ss
->thread
, state
);
232 /* Find the location of the MiG reply port cell in use by the thread whose
233 state is described by THREAD_STATE. If SIGTHREAD is nonzero, make sure
234 that this location can be set without faulting, or else return NULL. */
237 interrupted_reply_port_location (thread_t thread
,
238 struct machine_thread_all_state
*thread_state
,
241 mach_port_t
*portloc
= &THREAD_TCB(thread
, thread_state
)->reply_port
;
243 if (sigthread
&& _hurdsig_catch_memory_fault (portloc
))
244 /* Faulted trying to read the TCB. */
247 /* Fault now if this pointer is bogus. */
248 *(volatile mach_port_t
*) portloc
= *portloc
;
251 _hurdsig_end_catch_fault ();
256 #include <hurd/sigpreempt.h>
257 #include <intr-msg.h>
259 /* Timeout on interrupt_operation calls. */
260 mach_msg_timeout_t _hurdsig_interrupt_timeout
= 1000;
262 /* SS->thread is suspended.
264 Abort any interruptible RPC operation the thread is doing.
266 This uses only the constant member SS->thread and the unlocked, atomically
267 set member SS->intr_port, so no locking is needed.
269 If successfully sent an interrupt_operation and therefore the thread should
270 wait for its pending RPC to return (possibly EINTR) before taking the
271 incoming signal, returns the reply port to be received on. Otherwise
272 returns MACH_PORT_NULL.
274 SIGNO is used to find the applicable SA_RESTART bit. If SIGNO is zero,
275 the RPC fails with EINTR instead of restarting (thread_cancel).
277 *STATE_CHANGE is set nonzero if STATE->basic was modified and should
278 be applied back to the thread if it might ever run again, else zero. */
281 _hurdsig_abort_rpcs (struct hurd_sigstate
*ss
, int signo
, int sigthread
,
282 struct machine_thread_all_state
*state
, int *state_change
,
283 void (*reply
) (void))
285 extern const void _hurd_intr_rpc_msg_in_trap
;
286 mach_port_t rcv_port
= MACH_PORT_NULL
;
287 mach_port_t intr_port
;
291 intr_port
= ss
->intr_port
;
292 if (intr_port
== MACH_PORT_NULL
)
293 /* No interruption needs done. */
294 return MACH_PORT_NULL
;
296 /* Abort the thread's kernel context, so any pending message send or
297 receive completes immediately or aborts. */
298 abort_thread (ss
, state
, reply
);
300 if (state
->basic
.PC
< (natural_t
) &_hurd_intr_rpc_msg_in_trap
)
302 /* The thread is about to do the RPC, but hasn't yet entered
303 mach_msg. Mutate the thread's state so it knows not to try
305 INTR_MSG_BACK_OUT (&state
->basic
);
306 MACHINE_THREAD_STATE_SET_PC (&state
->basic
,
307 &_hurd_intr_rpc_msg_in_trap
);
308 state
->basic
.SYSRETURN
= MACH_SEND_INTERRUPTED
;
311 else if (state
->basic
.PC
== (natural_t
) &_hurd_intr_rpc_msg_in_trap
&&
312 /* The thread was blocked in the system call. After thread_abort,
313 the return value register indicates what state the RPC was in
315 state
->basic
.SYSRETURN
== MACH_RCV_INTERRUPTED
)
317 /* The RPC request message was sent and the thread was waiting for
318 the reply message; now the message receive has been aborted, so
319 the mach_msg call will return MACH_RCV_INTERRUPTED. We must tell
320 the server to interrupt the pending operation. The thread must
321 wait for the reply message before running the signal handler (to
322 guarantee that the operation has finished being interrupted), so
323 our nonzero return tells the trampoline code to finish the message
324 receive operation before running the handler. */
326 mach_port_t
*reply
= interrupted_reply_port_location (ss
->thread
,
329 error_t err
= __interrupt_operation (intr_port
, _hurdsig_interrupt_timeout
);
335 /* The interrupt didn't work.
336 Destroy the receive right the thread is blocked on. */
337 __mach_port_destroy (__mach_task_self (), *reply
);
338 *reply
= MACH_PORT_NULL
;
341 /* The system call return value register now contains
342 MACH_RCV_INTERRUPTED; when mach_msg resumes, it will retry the
343 call. Since we have just destroyed the receive right, the
344 retry will fail with MACH_RCV_INVALID_NAME. Instead, just
345 change the return value here to EINTR so mach_msg will not
346 retry and the EINTR error code will propagate up. */
347 state
->basic
.SYSRETURN
= EINTR
;
353 /* All threads whose RPCs were interrupted by the interrupt_operation
354 call above will retry their RPCs unless we clear SS->intr_port.
355 So we clear it for the thread taking a signal when SA_RESTART is
356 clear, so that its call returns EINTR. */
357 if (! signo
|| !(ss
->actions
[signo
].sa_flags
& SA_RESTART
))
358 ss
->intr_port
= MACH_PORT_NULL
;
365 /* Abort the RPCs being run by all threads but this one;
366 all other threads should be suspended. If LIVE is nonzero, those
367 threads may run again, so they should be adjusted as necessary to be
368 happy when resumed. STATE is clobbered as a scratch area; its initial
369 contents are ignored, and its contents on return are not useful. */
372 abort_all_rpcs (int signo
, struct machine_thread_all_state
*state
, int live
)
374 /* We can just loop over the sigstates. Any thread doing something
375 interruptible must have one. We needn't bother locking because all
376 other threads are stopped. */
378 struct hurd_sigstate
*ss
;
380 mach_port_t
*reply_ports
;
382 /* First loop over the sigstates to count them.
383 We need to know how big a vector we will need for REPLY_PORTS. */
385 for (ss
= _hurd_sigstates
; ss
!= NULL
; ss
= ss
->next
)
388 reply_ports
= alloca (nthreads
* sizeof *reply_ports
);
391 for (ss
= _hurd_sigstates
; ss
!= NULL
; ss
= ss
->next
, ++nthreads
)
392 if (ss
->thread
== _hurd_msgport_thread
)
393 reply_ports
[nthreads
] = MACH_PORT_NULL
;
397 state
->set
= 0; /* Reset scratch area. */
399 /* Abort any operation in progress with interrupt_operation.
400 Record the reply port the thread is waiting on.
401 We will wait for all the replies below. */
402 reply_ports
[nthreads
] = _hurdsig_abort_rpcs (ss
, signo
, 1,
403 state
, &state_changed
,
407 if (reply_ports
[nthreads
] != MACH_PORT_NULL
)
409 /* We will wait for the reply to this RPC below, so the
410 thread must issue a new RPC rather than waiting for the
411 reply to the one it sent. */
412 state
->basic
.SYSRETURN
= EINTR
;
416 /* Aborting the RPC needed to change this thread's state,
417 and it might ever run again. So write back its state. */
418 __thread_set_state (ss
->thread
, MACHINE_THREAD_STATE_FLAVOR
,
419 (natural_t
*) &state
->basic
,
420 MACHINE_THREAD_STATE_COUNT
);
424 /* Wait for replies from all the successfully interrupted RPCs. */
425 while (nthreads
-- > 0)
426 if (reply_ports
[nthreads
] != MACH_PORT_NULL
)
429 mach_msg_header_t head
;
430 err
= __mach_msg (&head
, MACH_RCV_MSG
|MACH_RCV_TIMEOUT
, 0, sizeof head
,
431 reply_ports
[nthreads
],
432 _hurd_interrupted_rpc_timeout
, MACH_PORT_NULL
);
435 case MACH_RCV_TIMED_OUT
:
436 case MACH_RCV_TOO_LARGE
:
445 struct hurd_signal_preemptor
*_hurdsig_preemptors
= 0;
446 sigset_t _hurdsig_preempted_set
;
448 /* XXX temporary to deal with spelling fix */
449 weak_alias (_hurdsig_preemptors
, _hurdsig_preempters
)
451 /* Mask of stop signals. */
452 #define STOPSIGS (sigmask (SIGTTIN) | sigmask (SIGTTOU) | \
453 sigmask (SIGSTOP) | sigmask (SIGTSTP))
455 /* Deliver a signal. SS is not locked. */
457 _hurd_internal_post_signal (struct hurd_sigstate
*ss
,
458 int signo
, struct hurd_signal_detail
*detail
,
459 mach_port_t reply_port
,
460 mach_msg_type_name_t reply_port_type
,
464 struct machine_thread_all_state thread_state
;
465 enum { stop
, ignore
, core
, term
, handle
} act
;
466 sighandler_t handler
;
470 /* Reply to this sig_post message. */
471 __typeof (__msg_sig_post_reply
) *reply_rpc
472 = (untraced
? __msg_sig_post_untraced_reply
: __msg_sig_post_reply
);
476 if (reply_port
== MACH_PORT_NULL
)
478 err
= (*reply_rpc
) (reply_port
, reply_port_type
, 0);
479 reply_port
= MACH_PORT_NULL
;
480 if (err
!= MACH_SEND_INVALID_DEST
) /* Ignore dead reply port. */
484 /* Mark the signal as pending. */
485 void mark_pending (void)
487 __sigaddset (&ss
->pending
, signo
);
488 /* Save the details to be given to the handler when SIGNO is
490 ss
->pending_data
[signo
] = *detail
;
493 /* Suspend the process with SIGNO. */
496 /* Stop all other threads and mark ourselves stopped. */
499 /* Hold the siglock while stopping other threads to be
500 sure it is not held by another thread afterwards. */
501 __mutex_lock (&_hurd_siglock
);
502 __proc_dostop (port
, _hurd_msgport_thread
);
503 __mutex_unlock (&_hurd_siglock
);
504 abort_all_rpcs (signo
, &thread_state
, 1);
506 __proc_mark_stop (port
, signo
, detail
->code
);
510 /* Resume the process after a suspension. */
513 /* Resume the process from being stopped. */
515 mach_msg_type_number_t nthreads
, i
;
521 /* Tell the proc server we are continuing. */
522 __USEPORT (PROC
, __proc_mark_cont (port
));
523 /* Fetch ports to all our threads and resume them. */
524 err
= __task_threads (__mach_task_self (), &threads
, &nthreads
);
526 for (i
= 0; i
< nthreads
; ++i
)
528 if (threads
[i
] != _hurd_msgport_thread
&&
529 (act
!= handle
|| threads
[i
] != ss
->thread
))
531 err
= __thread_resume (threads
[i
]);
534 err
= __mach_port_deallocate (__mach_task_self (),
538 __vm_deallocate (__mach_task_self (),
539 (vm_address_t
) threads
,
540 nthreads
* sizeof *threads
);
543 /* The thread that will run the handler is already suspended. */
550 /* This is PTRACE_CONTINUE. */
553 /* This call is just to check for pending signals. */
554 __spin_lock (&ss
->lock
);
555 goto check_pending_signals
;
560 thread_state
.set
= 0; /* We know nothing. */
562 __spin_lock (&ss
->lock
);
564 /* Check for a preempted signal. Preempted signals can arrive during
565 critical sections. */
567 inline sighandler_t
try_preemptor (struct hurd_signal_preemptor
*pe
)
568 { /* PE cannot be null. */
571 if (HURD_PREEMPT_SIGNAL_P (pe
, signo
, detail
->code
))
575 sighandler_t handler
= (*pe
->preemptor
) (pe
, ss
,
577 if (handler
!= SIG_ERR
)
588 handler
= ss
->preemptors
? try_preemptor (ss
->preemptors
) : SIG_ERR
;
590 /* If no thread-specific preemptor, check for a global one. */
591 if (handler
== SIG_ERR
&& __sigismember (&_hurdsig_preempted_set
, signo
))
593 __mutex_lock (&_hurd_siglock
);
594 handler
= try_preemptor (_hurdsig_preemptors
);
595 __mutex_unlock (&_hurd_siglock
);
601 if (handler
== SIG_IGN
)
602 /* Ignore the signal altogether. */
604 else if (handler
!= SIG_ERR
)
605 /* Run the preemption-provided handler. */
609 /* No preemption. Do normal handling. */
611 if (!untraced
&& __sigismember (&_hurdsig_traced
, signo
))
613 /* We are being traced. Stop to tell the debugger of the signal. */
615 /* Already stopped. Mark the signal as pending;
616 when resumed, we will notice it and stop again. */
620 __spin_unlock (&ss
->lock
);
625 handler
= ss
->actions
[signo
].sa_handler
;
627 if (handler
== SIG_DFL
)
628 /* Figure out the default action for this signal. */
632 /* A sig_post msg with SIGNO==0 is sent to
633 tell us to check for pending signals. */
665 if (_hurd_pgrp
== _hurd_pid
)
667 /* We are the process group leader. Since there is no
668 user-specified handler for SIGINFO, we use a default one
669 which prints something interesting. We use the normal
670 handler mechanism instead of just doing it here to avoid
671 the signal thread faulting or blocking in this
672 potentially hairy operation. */
674 handler
= _hurd_siginfo_handler
;
684 else if (handler
== SIG_IGN
)
689 if (__sigmask (signo
) & STOPSIGS
)
690 /* Stop signals clear a pending SIGCONT even if they
691 are handled or ignored (but not if preempted). */
692 __sigdelset (&ss
->pending
, SIGCONT
);
695 if (signo
== SIGCONT
)
696 /* Even if handled or ignored (but not preempted), SIGCONT clears
697 stop signals and resumes the process. */
698 ss
->pending
&= ~STOPSIGS
;
700 if (_hurd_stopped
&& act
!= stop
&& (untraced
|| signo
== SIGCONT
))
705 if (_hurd_orphaned
&& act
== stop
&&
706 (__sigmask (signo
) & (__sigmask (SIGTTIN
) | __sigmask (SIGTTOU
) |
707 __sigmask (SIGTSTP
))))
709 /* If we would ordinarily stop for a job control signal, but we are
710 orphaned so noone would ever notice and continue us again, we just
711 quietly die, alone and in the dark. */
712 detail
->code
= signo
;
717 /* Handle receipt of a blocked signal, or any signal while stopped. */
718 if (act
!= ignore
&& /* Signals ignored now are forgotten now. */
719 __sigismember (&ss
->blocked
, signo
) ||
720 (signo
!= SIGKILL
&& _hurd_stopped
))
726 /* Perform the chosen action for the signal. */
732 /* We are already stopped, but receiving an untraced stop
733 signal. Instead of resuming and suspending again, just
734 notify the proc server of the new stop signal. */
735 error_t err
= __USEPORT (PROC
, __proc_mark_stop
736 (port
, signo
, detail
->code
));
740 /* Suspend the process. */
746 /* Blocking or ignoring a machine exception is fatal.
747 Otherwise we could just spin on the faulting instruction. */
750 /* Nobody cares about this signal. If there was a call to resume
751 above in SIGCONT processing and we've left a thread suspended,
752 now's the time to set it going. */
755 err
= __thread_resume (ss
->thread
);
762 /* We got a fault setting up the stack frame for the handler.
763 Nothing to do but die; BSD gets SIGILL in this case. */
764 detail
->code
= signo
; /* XXX ? */
771 case term
: /* Time to die. */
772 case core
: /* And leave a rotting corpse. */
773 /* Have the proc server stop all other threads in our task. */
774 err
= __USEPORT (PROC
, __proc_dostop (port
, _hurd_msgport_thread
));
776 /* No more user instructions will be executed.
777 The signal can now be considered delivered. */
779 /* Abort all server operations now in progress. */
780 abort_all_rpcs (signo
, &thread_state
, 0);
783 int status
= W_EXITCODE (0, signo
);
784 /* Do a core dump if desired. Only set the wait status bit saying we
785 in fact dumped core if the operation was actually successful. */
786 if (act
== core
&& write_corefile (signo
, detail
))
788 /* Tell proc how we died and then stick the saber in the gut. */
794 /* Call a handler for this signal. */
796 struct sigcontext
*scp
, ocontext
;
797 int wait_for_reply
, state_changed
;
799 /* Stop the thread and abort its pending RPC operations. */
802 err
= __thread_suspend (ss
->thread
);
806 /* Abort the thread's kernel context, so any pending message send
807 or receive completes immediately or aborts. If an interruptible
808 RPC is in progress, abort_rpcs will do this. But we must always
809 do it before fetching the thread's state, because
810 thread_get_state is never kosher before thread_abort. */
811 abort_thread (ss
, &thread_state
, NULL
);
815 /* We have a previous sigcontext that sigreturn was about
816 to restore when another signal arrived. */
820 if (_hurdsig_catch_memory_fault (ss
->context
))
822 /* We faulted reading the thread's stack. Forget that
823 context and pretend it wasn't there. It almost
824 certainly crash if this handler returns, but that's it's
830 /* Copy the context from the thread's stack before
831 we start diddling the stack to set up the handler. */
832 ocontext
= *ss
->context
;
833 ss
->context
= &ocontext
;
835 _hurdsig_end_catch_fault ();
837 if (! machine_get_basic_state (ss
->thread
, &thread_state
))
839 loc
= interrupted_reply_port_location (ss
->thread
,
841 if (loc
&& *loc
!= MACH_PORT_NULL
)
842 /* This is the reply port for the context which called
843 sigreturn. Since we are abandoning that context entirely
844 and restoring SS->context instead, destroy this port. */
845 __mach_port_destroy (__mach_task_self (), *loc
);
847 /* The thread was in sigreturn, not in any interruptible RPC. */
850 assert (! __spin_lock_locked (&ss
->critical_section_lock
));
854 int crit
= __spin_lock_locked (&ss
->critical_section_lock
);
857 = (_hurdsig_abort_rpcs (ss
,
858 /* In a critical section, any RPC
859 should be cancelled instead of
860 restarted, regardless of
861 SA_RESTART, so the entire
862 "atomic" operation can be aborted
865 &thread_state
, &state_changed
,
871 /* The thread is in a critical section. Mark the signal as
872 pending. When it finishes the critical section, it will
873 check for pending signals. */
876 /* Some cases of interrupting an RPC must change the
877 thread state to back out the call. Normally this
878 change is rolled into the warping to the handler and
879 sigreturn, but we are not running the handler now
880 because the thread is in a critical section. Instead,
881 mutate the thread right away for the RPC interruption
882 and resume it; the RPC will return early so the
883 critical section can end soon. */
884 __thread_set_state (ss
->thread
, MACHINE_THREAD_STATE_FLAVOR
,
885 (natural_t
*) &thread_state
.basic
,
886 MACHINE_THREAD_STATE_COUNT
);
888 ss
->intr_port
= MACH_PORT_NULL
;
889 __thread_resume (ss
->thread
);
894 /* Call the machine-dependent function to set the thread up
895 to run the signal handler, and preserve its old context. */
896 scp
= _hurd_setup_sighandler (ss
, handler
, signo
, detail
,
897 wait_for_reply
, &thread_state
);
901 /* Set the machine-independent parts of the signal context. */
904 /* Fetch the thread variable for the MiG reply port,
905 and set it to MACH_PORT_NULL. */
906 mach_port_t
*loc
= interrupted_reply_port_location (ss
->thread
,
911 scp
->sc_reply_port
= *loc
;
912 *loc
= MACH_PORT_NULL
;
915 scp
->sc_reply_port
= MACH_PORT_NULL
;
917 /* Save the intr_port in use by the interrupted code,
918 and clear the cell before running the trampoline. */
919 scp
->sc_intr_port
= ss
->intr_port
;
920 ss
->intr_port
= MACH_PORT_NULL
;
924 /* After the handler runs we will restore to the state in
925 SS->context, not the state of the thread now. So restore
926 that context's reply port and intr port. */
928 scp
->sc_reply_port
= ss
->context
->sc_reply_port
;
929 scp
->sc_intr_port
= ss
->context
->sc_intr_port
;
935 /* Backdoor extra argument to signal handler. */
936 scp
->sc_error
= detail
->error
;
938 /* Block requested signals while running the handler. */
939 scp
->sc_mask
= ss
->blocked
;
940 __sigorset (&ss
->blocked
, &ss
->blocked
, &ss
->actions
[signo
].sa_mask
);
942 /* Also block SIGNO unless we're asked not to. */
943 if (! (ss
->actions
[signo
].sa_flags
& (SA_RESETHAND
| SA_NODEFER
)))
944 __sigaddset (&ss
->blocked
, signo
);
946 /* Reset to SIG_DFL if requested. SIGILL and SIGTRAP cannot
947 be automatically reset when delivered; the system silently
948 enforces this restriction. */
949 if (ss
->actions
[signo
].sa_flags
& SA_RESETHAND
950 && signo
!= SIGILL
&& signo
!= SIGTRAP
)
951 ss
->actions
[signo
].sa_handler
= SIG_DFL
;
953 /* Start the thread running the handler (or possibly waiting for an
954 RPC reply before running the handler). */
955 err
= __thread_set_state (ss
->thread
, MACHINE_THREAD_STATE_FLAVOR
,
956 (natural_t
*) &thread_state
.basic
,
957 MACHINE_THREAD_STATE_COUNT
);
959 err
= __thread_resume (ss
->thread
);
961 thread_state
.set
= 0; /* Everything we know is now wrong. */
966 /* The signal has either been ignored or is now being handled. We can
967 consider it delivered and reply to the killer. */
970 /* We get here unless the signal was fatal. We still hold SS->lock.
971 Check for pending signals, and loop to post them. */
973 /* Return nonzero if SS has any signals pending we should worry about.
974 We don't worry about any pending signals if we are stopped, nor if
975 SS is in a critical section. We are guaranteed to get a sig_post
976 message before any of them become deliverable: either the SIGCONT
977 signal, or a sig_post with SIGNO==0 as an explicit poll when the
978 thread finishes its critical section. */
979 inline int signals_pending (void)
981 if (_hurd_stopped
|| __spin_lock_locked (&ss
->critical_section_lock
))
983 return pending
= ss
->pending
& ~ss
->blocked
;
986 check_pending_signals
:
989 if (signals_pending ())
991 for (signo
= 1; signo
< NSIG
; ++signo
)
992 if (__sigismember (&pending
, signo
))
995 __sigdelset (&ss
->pending
, signo
);
996 *detail
= ss
->pending_data
[signo
];
997 __spin_unlock (&ss
->lock
);
1002 /* No pending signals left undelivered for this thread.
1003 If we were sent signal 0, we need to check for pending
1004 signals for all threads. */
1007 __spin_unlock (&ss
->lock
);
1008 __mutex_lock (&_hurd_siglock
);
1009 for (ss
= _hurd_sigstates
; ss
!= NULL
; ss
= ss
->next
)
1011 __spin_lock (&ss
->lock
);
1012 for (signo
= 1; signo
< NSIG
; ++signo
)
1013 if (__sigismember (&ss
->pending
, signo
)
1014 && (!__sigismember (&ss
->blocked
, signo
)
1015 /* We "deliver" immediately pending blocked signals whose
1016 action might be to ignore, so that if ignored they are
1017 dropped right away. */
1018 || ss
->actions
[signo
].sa_handler
== SIG_IGN
1019 || ss
->actions
[signo
].sa_handler
== SIG_DFL
))
1021 mutex_unlock (&_hurd_siglock
);
1022 goto deliver_pending
;
1024 __spin_unlock (&ss
->lock
);
1026 __mutex_unlock (&_hurd_siglock
);
1030 /* No more signals pending; SS->lock is still locked.
1031 Wake up any sigsuspend call that is blocking SS->thread. */
1032 if (ss
->suspended
!= MACH_PORT_NULL
)
1034 /* There is a sigsuspend waiting. Tell it to wake up. */
1036 mach_msg_header_t msg
;
1037 msg
.msgh_bits
= MACH_MSGH_BITS (MACH_MSG_TYPE_MAKE_SEND
, 0);
1038 msg
.msgh_remote_port
= ss
->suspended
;
1039 msg
.msgh_local_port
= MACH_PORT_NULL
;
1040 /* These values do not matter. */
1041 msg
.msgh_id
= 8675309; /* Jenny, Jenny. */
1042 ss
->suspended
= MACH_PORT_NULL
;
1043 err
= __mach_msg (&msg
, MACH_SEND_MSG
, sizeof msg
, 0,
1044 MACH_PORT_NULL
, MACH_MSG_TIMEOUT_NONE
,
1046 assert_perror (err
);
1048 __spin_unlock (&ss
->lock
);
1052 /* All pending signals delivered to all threads.
1053 Now we can send the reply message even for signal 0. */
1057 /* Decide whether REFPORT enables the sender to send us a SIGNO signal.
1058 Returns zero if so, otherwise the error code to return to the sender. */
1061 signal_allowed (int signo
, mach_port_t refport
)
1063 if (signo
< 0 || signo
>= NSIG
)
1066 if (refport
== __mach_task_self ())
1067 /* Can send any signal. */
1070 /* Avoid needing to check for this below. */
1071 if (refport
== MACH_PORT_NULL
)
1084 /* Job control signals can be sent by the controlling terminal. */
1085 if (__USEPORT (CTTYID
, port
== refport
))
1091 /* A continue signal can be sent by anyone in the session. */
1092 mach_port_t sessport
;
1093 if (! __USEPORT (PROC
, __proc_getsidport (port
, &sessport
)))
1095 __mach_port_deallocate (__mach_task_self (), sessport
);
1096 if (refport
== sessport
)
1105 /* Any io object a file descriptor refers to might send us
1106 one of these signals using its async ID port for REFPORT.
1108 This is pretty wide open; it is not unlikely that some random
1109 process can at least open for reading something we have open,
1110 get its async ID port, and send us a spurious SIGIO or SIGURG
1111 signal. But BSD is actually wider open than that!--you can set
1112 the owner of an io object to any process or process group
1113 whatsoever and send them gratuitous signals.
1115 Someday we could implement some reasonable scheme for
1116 authorizing SIGIO and SIGURG signals properly. */
1119 int lucky
= 0; /* True if we find a match for REFPORT. */
1120 __mutex_lock (&_hurd_dtable_lock
);
1121 for (d
= 0; !lucky
&& (unsigned) d
< (unsigned) _hurd_dtablesize
; ++d
)
1123 struct hurd_userlink ulink
;
1125 mach_port_t asyncid
;
1126 if (_hurd_dtable
[d
] == NULL
)
1128 port
= _hurd_port_get (&_hurd_dtable
[d
]->port
, &ulink
);
1129 if (! __io_get_icky_async_id (port
, &asyncid
))
1131 if (refport
== asyncid
)
1132 /* Break out of the loop on the next iteration. */
1134 __mach_port_deallocate (__mach_task_self (), asyncid
);
1136 _hurd_port_free (&_hurd_dtable
[d
]->port
, &ulink
, port
);
1138 __mutex_unlock (&_hurd_dtable_lock
);
1139 /* If we found a lucky winner, we've set D to -1 in the loop. */
1145 /* If this signal is legit, we have done `goto win' by now.
1146 When we return the error, mig deallocates REFPORT. */
1150 /* Deallocate the REFPORT send right; we are done with it. */
1151 __mach_port_deallocate (__mach_task_self (), refport
);
1156 /* Implement the sig_post RPC from <hurd/msg.defs>;
1157 sent when someone wants us to get a signal. */
1159 _S_msg_sig_post (mach_port_t me
,
1160 mach_port_t reply_port
, mach_msg_type_name_t reply_port_type
,
1161 int signo
, natural_t sigcode
,
1162 mach_port_t refport
)
1165 struct hurd_signal_detail d
;
1167 if (err
= signal_allowed (signo
, refport
))
1173 /* Post the signal to the designated signal-receiving thread. This will
1174 reply when the signal can be considered delivered. */
1175 _hurd_internal_post_signal (_hurd_thread_sigstate (_hurd_sigthread
),
1176 signo
, &d
, reply_port
, reply_port_type
,
1177 0); /* Stop if traced. */
1179 return MIG_NO_REPLY
; /* Already replied. */
1182 /* Implement the sig_post_untraced RPC from <hurd/msg.defs>;
1183 sent when the debugger wants us to really get a signal
1184 even if we are traced. */
1186 _S_msg_sig_post_untraced (mach_port_t me
,
1187 mach_port_t reply_port
,
1188 mach_msg_type_name_t reply_port_type
,
1189 int signo
, natural_t sigcode
,
1190 mach_port_t refport
)
1193 struct hurd_signal_detail d
;
1195 if (err
= signal_allowed (signo
, refport
))
1201 /* Post the signal to the designated signal-receiving thread. This will
1202 reply when the signal can be considered delivered. */
1203 _hurd_internal_post_signal (_hurd_thread_sigstate (_hurd_sigthread
),
1204 signo
, &d
, reply_port
, reply_port_type
,
1205 1); /* Untraced flag. */
1207 return MIG_NO_REPLY
; /* Already replied. */
1210 extern void __mig_init (void *);
1212 #include <mach/task_special_ports.h>
1214 /* Initialize the message port and _hurd_sigthread and start the signal
1218 _hurdsig_init (const int *intarray
, size_t intarraysize
)
1221 vm_size_t stacksize
;
1222 struct hurd_sigstate
*ss
;
1224 __mutex_init (&_hurd_siglock
);
1226 err
= __mach_port_allocate (__mach_task_self (),
1227 MACH_PORT_RIGHT_RECEIVE
,
1229 assert_perror (err
);
1231 /* Make a send right to the signal port. */
1232 err
= __mach_port_insert_right (__mach_task_self (),
1235 MACH_MSG_TYPE_MAKE_SEND
);
1236 assert_perror (err
);
1238 /* Initialize the main thread's signal state. */
1239 ss
= _hurd_self_sigstate ();
1241 /* Copy inherited values from our parent (or pre-exec process state)
1242 into the signal settings of the main thread. */
1243 if (intarraysize
> INIT_SIGMASK
)
1244 ss
->blocked
= intarray
[INIT_SIGMASK
];
1245 if (intarraysize
> INIT_SIGPENDING
)
1246 ss
->pending
= intarray
[INIT_SIGPENDING
];
1247 if (intarraysize
> INIT_SIGIGN
&& intarray
[INIT_SIGIGN
] != 0)
1250 for (signo
= 1; signo
< NSIG
; ++signo
)
1251 if (intarray
[INIT_SIGIGN
] & __sigmask(signo
))
1252 ss
->actions
[signo
].sa_handler
= SIG_IGN
;
1255 /* Set the default thread to receive task-global signals
1256 to this one, the main (first) user thread. */
1257 _hurd_sigthread
= ss
->thread
;
1259 /* Start the signal thread listening on the message port. */
1261 #pragma weak __cthread_fork
1262 if (!__cthread_fork
)
1264 err
= __thread_create (__mach_task_self (), &_hurd_msgport_thread
);
1265 assert_perror (err
);
1267 stacksize
= __vm_page_size
* 8; /* Small stack for signal thread. */
1268 err
= __mach_setup_thread (__mach_task_self (), _hurd_msgport_thread
,
1269 _hurd_msgport_receive
,
1270 (vm_address_t
*) &__hurd_sigthread_stack_base
,
1272 assert_perror (err
);
1274 __hurd_sigthread_stack_end
= __hurd_sigthread_stack_base
+ stacksize
;
1276 /* Reinitialize the MiG support routines so they will use a per-thread
1277 variable for the cached reply port. */
1278 __mig_init ((void *) __hurd_sigthread_stack_base
);
1280 err
= __thread_resume (_hurd_msgport_thread
);
1281 assert_perror (err
);
1285 /* When cthreads is being used, we need to make the signal thread a
1286 proper cthread. Otherwise it cannot use mutex_lock et al, which
1287 will be the cthreads versions. Various of the message port RPC
1288 handlers need to take locks, so we need to be able to call into
1289 cthreads code and meet its assumptions about how our thread and
1290 its stack are arranged. Since cthreads puts it there anyway,
1291 we'll let the signal thread's per-thread variables be found as for
1292 any normal cthread, and just leave the magic __hurd_sigthread_*
1293 values all zero so they'll be ignored. */
1294 #pragma weak __cthread_detach
1295 #pragma weak __pthread_getattr_np
1296 #pragma weak __pthread_attr_getstack
1297 __cthread_t thread
= __cthread_fork (
1298 (cthread_fn_t
) &_hurd_msgport_receive
, 0);
1299 __cthread_detach (thread
);
1301 if (__pthread_getattr_np
)
1303 /* Record signal thread stack layout for fork() */
1304 pthread_attr_t attr
;
1308 __pthread_getattr_np ((pthread_t
) thread
, &attr
);
1309 __pthread_attr_getstack (&attr
, &addr
, &size
);
1310 __hurd_sigthread_stack_base
= (uintptr_t) addr
;
1311 __hurd_sigthread_stack_end
= __hurd_sigthread_stack_base
+ size
;
1314 /* XXX We need the thread port for the signal thread further on
1315 in this thread (see hurdfault.c:_hurdsigfault_init).
1316 Therefore we block until _hurd_msgport_thread is initialized
1317 by the newly created thread. This really shouldn't be
1318 necessary; we should be able to fetch the thread port for a
1319 cthread from here. */
1320 while (_hurd_msgport_thread
== 0)
1324 /* Receive exceptions on the signal port. */
1325 #ifdef TASK_EXCEPTION_PORT
1326 __task_set_special_port (__mach_task_self (),
1327 TASK_EXCEPTION_PORT
, _hurd_msgport
);
1328 #elif defined (EXC_MASK_ALL)
1329 __task_set_exception_ports (__mach_task_self (),
1330 EXC_MASK_ALL
& ~(EXC_MASK_SYSCALL
1331 | EXC_MASK_MACH_SYSCALL
1332 | EXC_MASK_RPC_ALERT
),
1334 EXCEPTION_DEFAULT
, MACHINE_THREAD_STATE
);
1336 # error task_set_exception_port?
1339 /* Sanity check. Any pending, unblocked signals should have been
1340 taken by our predecessor incarnation (i.e. parent or pre-exec state)
1341 before packing up our init ints. This assert is last (not above)
1342 so that signal handling is all set up to handle the abort. */
1343 assert ((ss
->pending
&~ ss
->blocked
) == 0);
1346 /* Reauthenticate with the proc server. */
1349 reauth_proc (mach_port_t
new)
1351 mach_port_t ref
, ignore
;
1353 ref
= __mach_reply_port ();
1354 if (! HURD_PORT_USE (&_hurd_ports
[INIT_PORT_PROC
],
1355 __proc_reauthenticate (port
, ref
,
1356 MACH_MSG_TYPE_MAKE_SEND
) ||
1357 __auth_user_authenticate (new, ref
,
1358 MACH_MSG_TYPE_MAKE_SEND
,
1360 && ignore
!= MACH_PORT_NULL
)
1361 __mach_port_deallocate (__mach_task_self (), ignore
);
1362 __mach_port_destroy (__mach_task_self (), ref
);
1364 /* Set the owner of the process here too. */
1365 mutex_lock (&_hurd_id
.lock
);
1366 if (!_hurd_check_ids ())
1367 HURD_PORT_USE (&_hurd_ports
[INIT_PORT_PROC
],
1368 __proc_setowner (port
,
1370 ? _hurd_id
.gen
.uids
[0] : 0),
1371 !_hurd_id
.gen
.nuids
));
1372 mutex_unlock (&_hurd_id
.lock
);
1374 (void) &reauth_proc
; /* Silence compiler warning. */
1376 text_set_element (_hurd_reauth_hook
, reauth_proc
);
1378 /* Like `getenv', but safe for the signal thread to run.
1379 If the environment is trashed, this will just return NULL. */
1382 _hurdsig_getenv (const char *variable
)
1384 if (__libc_enable_secure
)
1387 if (_hurdsig_catch_memory_fault (__environ
))
1388 /* We bombed in getenv. */
1392 const size_t len
= strlen (variable
);
1394 char *volatile *ep
= __environ
;
1397 const char *p
= *ep
;
1398 _hurdsig_fault_preemptor
.first
= (long int) p
;
1399 _hurdsig_fault_preemptor
.last
= VM_MAX_ADDRESS
;
1400 if (! strncmp (p
, variable
, len
) && p
[len
] == '=')
1404 valuelen
= strlen (p
);
1405 _hurdsig_fault_preemptor
.last
= (long int) (p
+ valuelen
);
1406 value
= malloc (++valuelen
);
1408 memcpy (value
, p
, valuelen
);
1411 _hurdsig_fault_preemptor
.first
= (long int) ++ep
;
1412 _hurdsig_fault_preemptor
.last
= (long int) (ep
+ 1);
1414 _hurdsig_end_catch_fault ();