Update build-many-glibcs.py for binutils ia64 obsoletion.
[glibc.git] / hurd / hurdsig.c
blob3150091e8476e9729d2facdbda861eeb02af66bc
1 /* Copyright (C) 1991-2020 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 <https://www.gnu.org/licenses/>. */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
22 #include <cthreads.h> /* For `struct mutex'. */
23 #include <pthreadP.h>
24 #include <mach.h>
25 #include <mach/thread_switch.h>
26 #include <mach/mig_support.h>
28 #include <hurd.h>
29 #include <hurd/id.h>
30 #include <hurd/signal.h>
32 #include "hurdfault.h"
33 #include "hurdmalloc.h" /* XXX */
34 #include "../locale/localeinfo.h"
36 #include <libc-diag.h>
38 const char *_hurdsig_getenv (const char *);
40 struct mutex _hurd_siglock;
41 int _hurd_stopped;
43 /* Port that receives signals and other miscellaneous messages. */
44 mach_port_t _hurd_msgport;
46 /* Thread listening on it. */
47 thread_t _hurd_msgport_thread;
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 /* Sigstate for the task-global signals. */
57 struct hurd_sigstate *_hurd_global_sigstate;
59 /* Timeout for RPC's after interrupt_operation. */
60 mach_msg_timeout_t _hurd_interrupted_rpc_timeout = 60000;
62 static void
63 default_sigaction (struct sigaction actions[NSIG])
65 int signo;
67 __sigemptyset (&actions[0].sa_mask);
68 actions[0].sa_flags = SA_RESTART;
69 actions[0].sa_handler = SIG_DFL;
71 for (signo = 1; signo < NSIG; ++signo)
72 actions[signo] = actions[0];
75 struct hurd_sigstate *
76 _hurd_thread_sigstate (thread_t thread)
78 struct hurd_sigstate *ss;
79 __mutex_lock (&_hurd_siglock);
80 for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
81 if (ss->thread == thread)
82 break;
83 if (ss == NULL)
85 ss = malloc (sizeof (*ss));
86 if (ss == NULL)
87 __libc_fatal ("hurd: Can't allocate sigstate\n");
88 __spin_lock_init (&ss->critical_section_lock);
89 __spin_lock_init (&ss->lock);
90 ss->thread = thread;
92 /* Initialize default state. */
93 __sigemptyset (&ss->blocked);
94 __sigemptyset (&ss->pending);
95 memset (&ss->sigaltstack, 0, sizeof (ss->sigaltstack));
96 ss->sigaltstack.ss_flags |= SS_DISABLE;
97 ss->preemptors = NULL;
98 ss->suspended = MACH_PORT_NULL;
99 ss->intr_port = MACH_PORT_NULL;
100 ss->context = NULL;
101 ss->active_resources = NULL;
102 ss->cancel = 0;
103 ss->cancel_hook = NULL;
105 if (thread == MACH_PORT_NULL)
107 /* Process-wide sigstate, use the system defaults. */
108 default_sigaction (ss->actions);
110 /* The global sigstate is not added to the _hurd_sigstates list.
111 It is created with _hurd_thread_sigstate (MACH_PORT_NULL)
112 but should be accessed through _hurd_global_sigstate. */
114 else
116 /* Use the global actions as a default for new threads. */
117 struct hurd_sigstate *s = _hurd_global_sigstate;
118 if (s)
120 __spin_lock (&s->lock);
121 memcpy (ss->actions, s->actions, sizeof (s->actions));
122 __spin_unlock (&s->lock);
124 else
125 default_sigaction (ss->actions);
127 ss->next = _hurd_sigstates;
128 _hurd_sigstates = ss;
131 __mutex_unlock (&_hurd_siglock);
132 return ss;
134 libc_hidden_def (_hurd_thread_sigstate)
136 /* Destroy a sigstate structure. Called by libpthread just before the
137 * corresponding thread is terminated (the kernel thread port must remain valid
138 * until this function is called.) */
139 void
140 _hurd_sigstate_delete (thread_t thread)
142 struct hurd_sigstate **ssp, *ss;
144 __mutex_lock (&_hurd_siglock);
145 for (ssp = &_hurd_sigstates; *ssp; ssp = &(*ssp)->next)
146 if ((*ssp)->thread == thread)
147 break;
149 ss = *ssp;
150 if (ss)
151 *ssp = ss->next;
153 __mutex_unlock (&_hurd_siglock);
154 if (ss)
155 free (ss);
158 /* Make SS a global receiver, with pthread signal semantics. */
159 void
160 _hurd_sigstate_set_global_rcv (struct hurd_sigstate *ss)
162 assert (ss->thread != MACH_PORT_NULL);
163 ss->actions[0].sa_handler = SIG_IGN;
166 /* Check whether SS is a global receiver. */
167 static int
168 sigstate_is_global_rcv (const struct hurd_sigstate *ss)
170 return (_hurd_global_sigstate != NULL)
171 && (ss->actions[0].sa_handler == SIG_IGN);
173 libc_hidden_def (_hurd_sigstate_delete)
175 /* Lock/unlock a hurd_sigstate structure. If the accessors below require
176 it, the global sigstate will be locked as well. */
177 void
178 _hurd_sigstate_lock (struct hurd_sigstate *ss)
180 if (sigstate_is_global_rcv (ss))
181 __spin_lock (&_hurd_global_sigstate->lock);
182 __spin_lock (&ss->lock);
184 void
185 _hurd_sigstate_unlock (struct hurd_sigstate *ss)
187 __spin_unlock (&ss->lock);
188 if (sigstate_is_global_rcv (ss))
189 __spin_unlock (&_hurd_global_sigstate->lock);
191 libc_hidden_def (_hurd_sigstate_set_global_rcv)
193 /* Retreive a thread's full set of pending signals, including the global
194 ones if appropriate. SS must be locked. */
195 sigset_t
196 _hurd_sigstate_pending (const struct hurd_sigstate *ss)
198 sigset_t pending = ss->pending;
199 if (sigstate_is_global_rcv (ss))
200 __sigorset (&pending, &pending, &_hurd_global_sigstate->pending);
201 return pending;
204 /* Clear a pending signal and return the associated detailed
205 signal information. SS must be locked, and must have signal SIGNO
206 pending, either directly or through the global sigstate. */
207 static struct hurd_signal_detail
208 sigstate_clear_pending (struct hurd_sigstate *ss, int signo)
210 if (sigstate_is_global_rcv (ss)
211 && __sigismember (&_hurd_global_sigstate->pending, signo))
213 __sigdelset (&_hurd_global_sigstate->pending, signo);
214 return _hurd_global_sigstate->pending_data[signo];
217 assert (__sigismember (&ss->pending, signo));
218 __sigdelset (&ss->pending, signo);
219 return ss->pending_data[signo];
221 libc_hidden_def (_hurd_sigstate_lock)
222 libc_hidden_def (_hurd_sigstate_unlock)
224 /* Retreive a thread's action vector. SS must be locked. */
225 struct sigaction *
226 _hurd_sigstate_actions (struct hurd_sigstate *ss)
228 if (sigstate_is_global_rcv (ss))
229 return _hurd_global_sigstate->actions;
230 else
231 return ss->actions;
233 libc_hidden_def (_hurd_sigstate_pending)
236 /* Signal delivery itself is on this page. */
238 #include <hurd/fd.h>
239 #include <hurd/crash.h>
240 #include <hurd/resource.h>
241 #include <hurd/paths.h>
242 #include <setjmp.h>
243 #include <fcntl.h>
244 #include <sys/wait.h>
245 #include <thread_state.h>
246 #include <hurd/msg_server.h>
247 #include <hurd/msg_reply.h> /* For __msg_sig_post_reply. */
248 #include <hurd/interrupt.h>
249 #include <assert.h>
250 #include <unistd.h>
253 /* Call the crash dump server to mummify us before we die.
254 Returns nonzero if a core file was written. */
255 static int
256 write_corefile (int signo, const struct hurd_signal_detail *detail)
258 error_t err;
259 mach_port_t coreserver;
260 file_t file, coredir;
261 const char *name;
263 /* Don't bother locking since we just read the one word. */
264 rlim_t corelimit = _hurd_rlimits[RLIMIT_CORE].rlim_cur;
266 if (corelimit == 0)
267 /* No core dumping, thank you very much. Note that this makes
268 `ulimit -c 0' prevent crash-suspension too, which is probably
269 what the user wanted. */
270 return 0;
272 /* XXX RLIMIT_CORE:
273 When we have a protocol to make the server return an error
274 for RLIMIT_FSIZE, then tell the corefile fs server the RLIMIT_CORE
275 value in place of the RLIMIT_FSIZE value. */
277 /* First get a port to the core dumping server. */
278 coreserver = MACH_PORT_NULL;
279 name = _hurdsig_getenv ("CRASHSERVER");
280 if (name != NULL)
281 coreserver = __file_name_lookup (name, 0, 0);
282 if (coreserver == MACH_PORT_NULL)
283 coreserver = __file_name_lookup (_SERVERS_CRASH, 0, 0);
284 if (coreserver == MACH_PORT_NULL)
285 return 0;
287 /* Get a port to the directory where the new core file will reside. */
288 file = MACH_PORT_NULL;
289 name = _hurdsig_getenv ("COREFILE");
290 if (name == NULL)
291 name = "core";
292 coredir = __file_name_split (name, (char **) &name);
293 if (coredir != MACH_PORT_NULL)
294 /* Create the new file, but don't link it into the directory yet. */
295 __dir_mkfile (coredir, O_WRONLY|O_CREAT,
296 0600 & ~_hurd_umask, /* XXX ? */
297 &file);
299 /* Call the core dumping server to write the core file. */
300 err = __crash_dump_task (coreserver,
301 __mach_task_self (),
302 file,
303 signo, detail->code, detail->error,
304 detail->exc, detail->exc_code, detail->exc_subcode,
305 _hurd_ports[INIT_PORT_CTTYID].port,
306 MACH_MSG_TYPE_COPY_SEND);
307 __mach_port_deallocate (__mach_task_self (), coreserver);
309 if (! err && file != MACH_PORT_NULL)
310 /* The core dump into FILE succeeded, so now link it into the
311 directory. */
312 err = __dir_link (coredir, file, name, 1);
313 __mach_port_deallocate (__mach_task_self (), file);
314 __mach_port_deallocate (__mach_task_self (), coredir);
315 return !err && file != MACH_PORT_NULL;
319 /* The lowest-numbered thread state flavor value is 1,
320 so we use bit 0 in machine_thread_all_state.set to
321 record whether we have done thread_abort. */
322 #define THREAD_ABORTED 1
324 /* SS->thread is suspended. Abort the thread and get its basic state. */
325 static void
326 abort_thread (struct hurd_sigstate *ss, struct machine_thread_all_state *state,
327 void (*reply) (void))
329 assert (ss->thread != MACH_PORT_NULL);
331 if (!(state->set & THREAD_ABORTED))
333 error_t err = __thread_abort (ss->thread);
334 assert_perror (err);
335 /* Clear all thread state flavor set bits, because thread_abort may
336 have changed the state. */
337 state->set = THREAD_ABORTED;
340 if (reply)
341 (*reply) ();
343 machine_get_basic_state (ss->thread, state);
346 /* Find the location of the MiG reply port cell in use by the thread whose
347 state is described by THREAD_STATE. If SIGTHREAD is nonzero, make sure
348 that this location can be set without faulting, or else return NULL. */
350 static mach_port_t *
351 interrupted_reply_port_location (thread_t thread,
352 struct machine_thread_all_state *thread_state,
353 int sigthread)
355 mach_port_t *portloc = &THREAD_TCB(thread, thread_state)->reply_port;
357 if (sigthread && _hurdsig_catch_memory_fault (portloc))
358 /* Faulted trying to read the TCB. */
359 return NULL;
361 DIAG_PUSH_NEEDS_COMMENT;
362 /* GCC 6 and before seem to be confused by the setjmp call inside
363 _hurdsig_catch_memory_fault and think that we may be returning a second
364 time to here with portloc uninitialized (but we never do). */
365 DIAG_IGNORE_NEEDS_COMMENT (6, "-Wmaybe-uninitialized");
366 /* Fault now if this pointer is bogus. */
367 *(volatile mach_port_t *) portloc = *portloc;
368 DIAG_POP_NEEDS_COMMENT;
370 if (sigthread)
371 _hurdsig_end_catch_fault ();
373 return portloc;
376 #include <hurd/sigpreempt.h>
377 #include <intr-msg.h>
379 /* Timeout on interrupt_operation calls. */
380 mach_msg_timeout_t _hurdsig_interrupt_timeout = 1000;
382 /* SS->thread is suspended.
384 Abort any interruptible RPC operation the thread is doing.
386 This uses only the constant member SS->thread and the unlocked, atomically
387 set member SS->intr_port, so no locking is needed.
389 If successfully sent an interrupt_operation and therefore the thread should
390 wait for its pending RPC to return (possibly EINTR) before taking the
391 incoming signal, returns the reply port to be received on. Otherwise
392 returns MACH_PORT_NULL.
394 SIGNO is used to find the applicable SA_RESTART bit. If SIGNO is zero,
395 the RPC fails with EINTR instead of restarting (thread_cancel).
397 *STATE_CHANGE is set nonzero if STATE->basic was modified and should
398 be applied back to the thread if it might ever run again, else zero. */
400 mach_port_t
401 _hurdsig_abort_rpcs (struct hurd_sigstate *ss, int signo, int sigthread,
402 struct machine_thread_all_state *state, int *state_change,
403 void (*reply) (void))
405 extern const void _hurd_intr_rpc_msg_about_to;
406 extern const void _hurd_intr_rpc_msg_in_trap;
407 mach_port_t rcv_port = MACH_PORT_NULL;
408 mach_port_t intr_port;
410 *state_change = 0;
412 intr_port = ss->intr_port;
413 if (intr_port == MACH_PORT_NULL)
414 /* No interruption needs done. */
415 return MACH_PORT_NULL;
417 /* Abort the thread's kernel context, so any pending message send or
418 receive completes immediately or aborts. */
419 abort_thread (ss, state, reply);
421 if (state->basic.PC >= (natural_t) &_hurd_intr_rpc_msg_about_to
422 && state->basic.PC < (natural_t) &_hurd_intr_rpc_msg_in_trap)
424 /* The thread is about to do the RPC, but hasn't yet entered
425 mach_msg. Mutate the thread's state so it knows not to try
426 the RPC. */
427 INTR_MSG_BACK_OUT (&state->basic);
428 MACHINE_THREAD_STATE_SET_PC (&state->basic,
429 &_hurd_intr_rpc_msg_in_trap);
430 state->basic.SYSRETURN = MACH_SEND_INTERRUPTED;
431 *state_change = 1;
433 else if (state->basic.PC == (natural_t) &_hurd_intr_rpc_msg_in_trap
434 /* The thread was blocked in the system call. After thread_abort,
435 the return value register indicates what state the RPC was in
436 when interrupted. */
437 && state->basic.SYSRETURN == MACH_RCV_INTERRUPTED)
439 /* The RPC request message was sent and the thread was waiting for
440 the reply message; now the message receive has been aborted, so
441 the mach_msg call will return MACH_RCV_INTERRUPTED. We must tell
442 the server to interrupt the pending operation. The thread must
443 wait for the reply message before running the signal handler (to
444 guarantee that the operation has finished being interrupted), so
445 our nonzero return tells the trampoline code to finish the message
446 receive operation before running the handler. */
448 mach_port_t *reply = interrupted_reply_port_location (ss->thread,
449 state,
450 sigthread);
451 error_t err = __interrupt_operation (intr_port, _hurdsig_interrupt_timeout);
453 if (err)
455 if (reply)
457 /* The interrupt didn't work.
458 Destroy the receive right the thread is blocked on. */
459 __mach_port_destroy (__mach_task_self (), *reply);
460 *reply = MACH_PORT_NULL;
463 /* The system call return value register now contains
464 MACH_RCV_INTERRUPTED; when mach_msg resumes, it will retry the
465 call. Since we have just destroyed the receive right, the
466 retry will fail with MACH_RCV_INVALID_NAME. Instead, just
467 change the return value here to EINTR so mach_msg will not
468 retry and the EINTR error code will propagate up. */
469 state->basic.SYSRETURN = EINTR;
470 *state_change = 1;
472 else if (reply)
473 rcv_port = *reply;
475 /* All threads whose RPCs were interrupted by the interrupt_operation
476 call above will retry their RPCs unless we clear SS->intr_port.
477 So we clear it for the thread taking a signal when SA_RESTART is
478 clear, so that its call returns EINTR. */
479 if (! signo || !(_hurd_sigstate_actions (ss) [signo].sa_flags & SA_RESTART))
480 ss->intr_port = MACH_PORT_NULL;
483 return rcv_port;
487 /* Abort the RPCs being run by all threads but this one;
488 all other threads should be suspended. If LIVE is nonzero, those
489 threads may run again, so they should be adjusted as necessary to be
490 happy when resumed. STATE is clobbered as a scratch area; its initial
491 contents are ignored, and its contents on return are not useful. */
493 static void
494 abort_all_rpcs (int signo, struct machine_thread_all_state *state, int live)
496 /* We can just loop over the sigstates. Any thread doing something
497 interruptible must have one. We needn't bother locking because all
498 other threads are stopped. */
500 struct hurd_sigstate *ss;
501 size_t nthreads;
502 mach_port_t *reply_ports;
504 /* First loop over the sigstates to count them.
505 We need to know how big a vector we will need for REPLY_PORTS. */
506 nthreads = 0;
507 for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
508 ++nthreads;
510 reply_ports = alloca (nthreads * sizeof *reply_ports);
512 nthreads = 0;
513 for (ss = _hurd_sigstates; ss != NULL; ss = ss->next, ++nthreads)
514 if (ss->thread == _hurd_msgport_thread)
515 reply_ports[nthreads] = MACH_PORT_NULL;
516 else
518 int state_changed;
519 state->set = 0; /* Reset scratch area. */
521 /* Abort any operation in progress with interrupt_operation.
522 Record the reply port the thread is waiting on.
523 We will wait for all the replies below. */
524 reply_ports[nthreads] = _hurdsig_abort_rpcs (ss, signo, 1,
525 state, &state_changed,
526 NULL);
527 if (live)
529 if (reply_ports[nthreads] != MACH_PORT_NULL)
531 /* We will wait for the reply to this RPC below, so the
532 thread must issue a new RPC rather than waiting for the
533 reply to the one it sent. */
534 state->basic.SYSRETURN = EINTR;
535 state_changed = 1;
537 if (state_changed)
538 /* Aborting the RPC needed to change this thread's state,
539 and it might ever run again. So write back its state. */
540 __thread_set_state (ss->thread, MACHINE_THREAD_STATE_FLAVOR,
541 (natural_t *) &state->basic,
542 MACHINE_THREAD_STATE_COUNT);
546 /* Wait for replies from all the successfully interrupted RPCs. */
547 while (nthreads-- > 0)
548 if (reply_ports[nthreads] != MACH_PORT_NULL)
550 error_t err;
551 mach_msg_header_t head;
552 err = __mach_msg (&head, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof head,
553 reply_ports[nthreads],
554 _hurd_interrupted_rpc_timeout, MACH_PORT_NULL);
555 switch (err)
557 case MACH_RCV_TIMED_OUT:
558 case MACH_RCV_TOO_LARGE:
559 break;
561 default:
562 assert_perror (err);
567 /* Wake up any sigsuspend or pselect call that is blocking SS->thread. SS must
568 be locked. */
569 static void
570 wake_sigsuspend (struct hurd_sigstate *ss)
572 error_t err;
573 mach_msg_header_t msg;
575 if (ss->suspended == MACH_PORT_NULL)
576 return;
578 /* There is a sigsuspend waiting. Tell it to wake up. */
579 msg.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_MAKE_SEND, 0);
580 msg.msgh_remote_port = ss->suspended;
581 msg.msgh_local_port = MACH_PORT_NULL;
582 /* These values do not matter. */
583 msg.msgh_id = 8675309; /* Jenny, Jenny. */
584 ss->suspended = MACH_PORT_NULL;
585 err = __mach_msg (&msg, MACH_SEND_MSG, sizeof msg, 0,
586 MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE,
587 MACH_PORT_NULL);
588 assert_perror (err);
591 struct hurd_signal_preemptor *_hurdsig_preemptors = 0;
592 sigset_t _hurdsig_preempted_set;
594 /* XXX temporary to deal with spelling fix */
595 weak_alias (_hurdsig_preemptors, _hurdsig_preempters)
597 /* Mask of stop signals. */
598 #define STOPSIGS (__sigmask (SIGTTIN) | __sigmask (SIGTTOU) \
599 | __sigmask (SIGSTOP) | __sigmask (SIGTSTP))
601 /* Actual delivery of a single signal. Called with SS unlocked. When
602 the signal is delivered, return SS, locked (or, if SS was originally
603 _hurd_global_sigstate, the sigstate of the actual thread the signal
604 was delivered to). If the signal is being traced, return NULL with
605 SS unlocked. */
606 static struct hurd_sigstate *
607 post_signal (struct hurd_sigstate *ss,
608 int signo, struct hurd_signal_detail *detail,
609 int untraced, void (*reply) (void))
611 struct machine_thread_all_state thread_state;
612 enum { stop, ignore, core, term, handle } act;
613 int ss_suspended;
615 /* Mark the signal as pending. */
616 void mark_pending (void)
618 __sigaddset (&ss->pending, signo);
619 /* Save the details to be given to the handler when SIGNO is
620 unblocked. */
621 ss->pending_data[signo] = *detail;
624 /* Suspend the process with SIGNO. */
625 void suspend (void)
627 /* Stop all other threads and mark ourselves stopped. */
628 __USEPORT (PROC,
630 /* Hold the siglock while stopping other threads to be
631 sure it is not held by another thread afterwards. */
632 __mutex_lock (&_hurd_siglock);
633 __proc_dostop (port, _hurd_msgport_thread);
634 __mutex_unlock (&_hurd_siglock);
635 abort_all_rpcs (signo, &thread_state, 1);
636 reply ();
637 __proc_mark_stop (port, signo, detail->code);
638 }));
639 _hurd_stopped = 1;
641 /* Resume the process after a suspension. */
642 void resume (void)
644 /* Resume the process from being stopped. */
645 thread_t *threads;
646 mach_msg_type_number_t nthreads, i;
647 error_t err;
649 if (! _hurd_stopped)
650 return;
652 /* Tell the proc server we are continuing. */
653 __USEPORT (PROC, __proc_mark_cont (port));
654 /* Fetch ports to all our threads and resume them. */
655 err = __task_threads (__mach_task_self (), &threads, &nthreads);
656 assert_perror (err);
657 for (i = 0; i < nthreads; ++i)
659 if (act == handle && threads[i] == ss->thread)
661 /* The thread that will run the handler is kept suspended. */
662 ss_suspended = 1;
664 else if (threads[i] != _hurd_msgport_thread)
666 err = __thread_resume (threads[i]);
667 assert_perror (err);
669 err = __mach_port_deallocate (__mach_task_self (),
670 threads[i]);
671 assert_perror (err);
673 __vm_deallocate (__mach_task_self (),
674 (vm_address_t) threads,
675 nthreads * sizeof *threads);
676 _hurd_stopped = 0;
679 error_t err;
680 sighandler_t handler;
682 if (signo == 0)
684 if (untraced)
686 /* This is PTRACE_CONTINUE. */
687 act = ignore;
688 resume ();
691 /* This call is just to check for pending signals. */
692 _hurd_sigstate_lock (ss);
693 return ss;
696 thread_state.set = 0; /* We know nothing. */
698 _hurd_sigstate_lock (ss);
700 /* If this is a global signal, try to find a thread ready to accept
701 it right away. This is especially important for untraced signals,
702 since going through the global pending mask would de-untrace them. */
703 if (ss->thread == MACH_PORT_NULL)
705 struct hurd_sigstate *rss;
707 __mutex_lock (&_hurd_siglock);
708 for (rss = _hurd_sigstates; rss != NULL; rss = rss->next)
710 if (! sigstate_is_global_rcv (rss))
711 continue;
713 /* The global sigstate is already locked. */
714 __spin_lock (&rss->lock);
715 if (! __sigismember (&rss->blocked, signo))
717 ss = rss;
718 break;
720 __spin_unlock (&rss->lock);
722 __mutex_unlock (&_hurd_siglock);
725 /* We want the preemptors to be able to update the blocking mask
726 without affecting the delivery of this signal, so we save the
727 current value to test against later. */
728 sigset_t blocked = ss->blocked;
730 /* Check for a preempted signal. Preempted signals can arrive during
731 critical sections. */
733 inline sighandler_t try_preemptor (struct hurd_signal_preemptor *pe)
734 { /* PE cannot be null. */
737 if (HURD_PREEMPT_SIGNAL_P (pe, signo, detail->code))
739 if (pe->preemptor)
741 sighandler_t handler = (*pe->preemptor) (pe, ss,
742 &signo, detail);
743 if (handler != SIG_ERR)
744 return handler;
746 else
747 return pe->handler;
749 pe = pe->next;
750 } while (pe != 0);
751 return SIG_ERR;
754 handler = ss->preemptors ? try_preemptor (ss->preemptors) : SIG_ERR;
756 /* If no thread-specific preemptor, check for a global one. */
757 if (handler == SIG_ERR && __sigismember (&_hurdsig_preempted_set, signo))
759 __mutex_lock (&_hurd_siglock);
760 handler = try_preemptor (_hurdsig_preemptors);
761 __mutex_unlock (&_hurd_siglock);
765 ss_suspended = 0;
767 if (handler == SIG_IGN)
768 /* Ignore the signal altogether. */
769 act = ignore;
770 else if (handler != SIG_ERR)
771 /* Run the preemption-provided handler. */
772 act = handle;
773 else
775 /* No preemption. Do normal handling. */
777 if (!untraced && __sigismember (&_hurdsig_traced, signo))
779 /* We are being traced. Stop to tell the debugger of the signal. */
780 if (_hurd_stopped)
781 /* Already stopped. Mark the signal as pending;
782 when resumed, we will notice it and stop again. */
783 mark_pending ();
784 else
785 suspend ();
786 _hurd_sigstate_unlock (ss);
787 reply ();
788 return NULL;
791 handler = _hurd_sigstate_actions (ss) [signo].sa_handler;
793 if (handler == SIG_DFL)
794 /* Figure out the default action for this signal. */
795 switch (signo)
797 case 0:
798 /* A sig_post msg with SIGNO==0 is sent to
799 tell us to check for pending signals. */
800 act = ignore;
801 break;
803 case SIGTTIN:
804 case SIGTTOU:
805 case SIGSTOP:
806 case SIGTSTP:
807 act = stop;
808 break;
810 case SIGCONT:
811 case SIGIO:
812 case SIGURG:
813 case SIGCHLD:
814 case SIGWINCH:
815 act = ignore;
816 break;
818 case SIGQUIT:
819 case SIGILL:
820 case SIGTRAP:
821 case SIGIOT:
822 case SIGEMT:
823 case SIGFPE:
824 case SIGBUS:
825 case SIGSEGV:
826 case SIGSYS:
827 act = core;
828 break;
830 case SIGINFO:
831 if (_hurd_pgrp == _hurd_pid)
833 /* We are the process group leader. Since there is no
834 user-specified handler for SIGINFO, we use a default one
835 which prints something interesting. We use the normal
836 handler mechanism instead of just doing it here to avoid
837 the signal thread faulting or blocking in this
838 potentially hairy operation. */
839 act = handle;
840 handler = _hurd_siginfo_handler;
842 else
843 act = ignore;
844 break;
846 default:
847 act = term;
848 break;
850 else if (handler == SIG_IGN)
851 act = ignore;
852 else
853 act = handle;
855 if (__sigmask (signo) & STOPSIGS)
856 /* Stop signals clear a pending SIGCONT even if they
857 are handled or ignored (but not if preempted). */
858 __sigdelset (&ss->pending, SIGCONT);
859 else
861 if (signo == SIGCONT)
862 /* Even if handled or ignored (but not preempted), SIGCONT clears
863 stop signals and resumes the process. */
864 ss->pending &= ~STOPSIGS;
866 if (_hurd_stopped && act != stop && (untraced || signo == SIGCONT))
867 resume ();
871 if (_hurd_orphaned && act == stop
872 && (__sigmask (signo) & (__sigmask (SIGTTIN) | __sigmask (SIGTTOU)
873 | __sigmask (SIGTSTP))))
875 /* If we would ordinarily stop for a job control signal, but we are
876 orphaned so noone would ever notice and continue us again, we just
877 quietly die, alone and in the dark. */
878 detail->code = signo;
879 signo = SIGKILL;
880 act = term;
883 /* Handle receipt of a blocked signal, or any signal while stopped. */
884 if (__sigismember (&blocked, signo) || (signo != SIGKILL && _hurd_stopped))
886 mark_pending ();
887 act = ignore;
890 /* Perform the chosen action for the signal. */
891 switch (act)
893 case stop:
894 if (_hurd_stopped)
896 /* We are already stopped, but receiving an untraced stop
897 signal. Instead of resuming and suspending again, just
898 notify the proc server of the new stop signal. */
899 error_t err = __USEPORT (PROC, __proc_mark_stop
900 (port, signo, detail->code));
901 assert_perror (err);
903 else
904 /* Suspend the process. */
905 suspend ();
906 break;
908 case ignore:
909 if (detail->exc)
910 /* Blocking or ignoring a machine exception is fatal.
911 Otherwise we could just spin on the faulting instruction. */
912 goto fatal;
914 /* Nobody cares about this signal. If there was a call to resume
915 above in SIGCONT processing and we've left a thread suspended,
916 now's the time to set it going. */
917 if (ss_suspended)
919 assert (ss->thread != MACH_PORT_NULL);
920 err = __thread_resume (ss->thread);
921 assert_perror (err);
922 ss_suspended = 0;
924 break;
926 sigbomb:
927 /* We got a fault setting up the stack frame for the handler.
928 Nothing to do but die; BSD gets SIGILL in this case. */
929 detail->code = signo; /* XXX ? */
930 signo = SIGILL;
932 fatal:
933 act = core;
934 /* FALLTHROUGH */
936 case term: /* Time to die. */
937 case core: /* And leave a rotting corpse. */
938 /* Have the proc server stop all other threads in our task. */
939 err = __USEPORT (PROC, __proc_dostop (port, _hurd_msgport_thread));
940 assert_perror (err);
941 /* No more user instructions will be executed.
942 The signal can now be considered delivered. */
943 reply ();
944 /* Abort all server operations now in progress. */
945 abort_all_rpcs (signo, &thread_state, 0);
948 int status = W_EXITCODE (0, signo);
949 /* Do a core dump if desired. Only set the wait status bit saying we
950 in fact dumped core if the operation was actually successful. */
951 if (act == core && write_corefile (signo, detail))
952 status |= WCOREFLAG;
953 /* Tell proc how we died and then stick the saber in the gut. */
954 _hurd_exit (status);
955 /* NOTREACHED */
958 case handle:
959 /* Call a handler for this signal. */
961 struct sigcontext *scp, ocontext;
962 int wait_for_reply, state_changed;
964 assert (ss->thread != MACH_PORT_NULL);
966 /* Stop the thread and abort its pending RPC operations. */
967 if (! ss_suspended)
969 err = __thread_suspend (ss->thread);
970 assert_perror (err);
973 /* Abort the thread's kernel context, so any pending message send
974 or receive completes immediately or aborts. If an interruptible
975 RPC is in progress, abort_rpcs will do this. But we must always
976 do it before fetching the thread's state, because
977 thread_get_state is never kosher before thread_abort. */
978 abort_thread (ss, &thread_state, NULL);
980 if (ss->context)
982 /* We have a previous sigcontext that sigreturn was about
983 to restore when another signal arrived. */
985 mach_port_t *loc;
987 if (_hurdsig_catch_memory_fault (ss->context))
989 /* We faulted reading the thread's stack. Forget that
990 context and pretend it wasn't there. It almost
991 certainly crash if this handler returns, but that's it's
992 problem. */
993 ss->context = NULL;
995 else
997 /* Copy the context from the thread's stack before
998 we start diddling the stack to set up the handler. */
999 ocontext = *ss->context;
1000 ss->context = &ocontext;
1002 _hurdsig_end_catch_fault ();
1004 if (! machine_get_basic_state (ss->thread, &thread_state))
1005 goto sigbomb;
1006 loc = interrupted_reply_port_location (ss->thread,
1007 &thread_state, 1);
1008 if (loc && *loc != MACH_PORT_NULL)
1009 /* This is the reply port for the context which called
1010 sigreturn. Since we are abandoning that context entirely
1011 and restoring SS->context instead, destroy this port. */
1012 __mach_port_destroy (__mach_task_self (), *loc);
1014 /* The thread was in sigreturn, not in any interruptible RPC. */
1015 wait_for_reply = 0;
1017 assert (! __spin_lock_locked (&ss->critical_section_lock));
1019 else
1021 int crit = __spin_lock_locked (&ss->critical_section_lock);
1023 wait_for_reply
1024 = (_hurdsig_abort_rpcs (ss,
1025 /* In a critical section, any RPC
1026 should be cancelled instead of
1027 restarted, regardless of
1028 SA_RESTART, so the entire
1029 "atomic" operation can be aborted
1030 as a unit. */
1031 crit ? 0 : signo, 1,
1032 &thread_state, &state_changed,
1033 reply)
1034 != MACH_PORT_NULL);
1036 if (crit)
1038 /* The thread is in a critical section. Mark the signal as
1039 pending. When it finishes the critical section, it will
1040 check for pending signals. */
1041 mark_pending ();
1042 if (state_changed)
1043 /* Some cases of interrupting an RPC must change the
1044 thread state to back out the call. Normally this
1045 change is rolled into the warping to the handler and
1046 sigreturn, but we are not running the handler now
1047 because the thread is in a critical section. Instead,
1048 mutate the thread right away for the RPC interruption
1049 and resume it; the RPC will return early so the
1050 critical section can end soon. */
1051 __thread_set_state (ss->thread, MACHINE_THREAD_STATE_FLAVOR,
1052 (natural_t *) &thread_state.basic,
1053 MACHINE_THREAD_STATE_COUNT);
1054 /* */
1055 ss->intr_port = MACH_PORT_NULL;
1056 __thread_resume (ss->thread);
1057 break;
1061 /* Call the machine-dependent function to set the thread up
1062 to run the signal handler, and preserve its old context. */
1063 scp = _hurd_setup_sighandler (ss, handler, signo, detail,
1064 wait_for_reply, &thread_state);
1065 if (scp == NULL)
1066 goto sigbomb;
1068 /* Set the machine-independent parts of the signal context. */
1071 /* Fetch the thread variable for the MiG reply port,
1072 and set it to MACH_PORT_NULL. */
1073 mach_port_t *loc = interrupted_reply_port_location (ss->thread,
1074 &thread_state,
1076 if (loc)
1078 scp->sc_reply_port = *loc;
1079 *loc = MACH_PORT_NULL;
1081 else
1082 scp->sc_reply_port = MACH_PORT_NULL;
1084 /* Save the intr_port in use by the interrupted code,
1085 and clear the cell before running the trampoline. */
1086 scp->sc_intr_port = ss->intr_port;
1087 ss->intr_port = MACH_PORT_NULL;
1089 if (ss->context)
1091 /* After the handler runs we will restore to the state in
1092 SS->context, not the state of the thread now. So restore
1093 that context's reply port and intr port. */
1095 scp->sc_reply_port = ss->context->sc_reply_port;
1096 scp->sc_intr_port = ss->context->sc_intr_port;
1098 ss->context = NULL;
1102 struct sigaction *action = & _hurd_sigstate_actions (ss) [signo];
1104 /* Backdoor extra argument to signal handler. */
1105 scp->sc_error = detail->error;
1107 /* Block requested signals while running the handler. */
1108 scp->sc_mask = ss->blocked;
1109 __sigorset (&ss->blocked, &ss->blocked, &action->sa_mask);
1111 /* Also block SIGNO unless we're asked not to. */
1112 if (! (action->sa_flags & (SA_RESETHAND | SA_NODEFER)))
1113 __sigaddset (&ss->blocked, signo);
1115 /* Reset to SIG_DFL if requested. SIGILL and SIGTRAP cannot
1116 be automatically reset when delivered; the system silently
1117 enforces this restriction. */
1118 if (action->sa_flags & SA_RESETHAND
1119 && signo != SIGILL && signo != SIGTRAP)
1120 action->sa_handler = SIG_DFL;
1122 /* Any sigsuspend call must return after the handler does. */
1123 wake_sigsuspend (ss);
1125 /* Start the thread running the handler (or possibly waiting for an
1126 RPC reply before running the handler). */
1127 err = __thread_set_state (ss->thread, MACHINE_THREAD_STATE_FLAVOR,
1128 (natural_t *) &thread_state.basic,
1129 MACHINE_THREAD_STATE_COUNT);
1130 assert_perror (err);
1131 err = __thread_resume (ss->thread);
1132 assert_perror (err);
1133 thread_state.set = 0; /* Everything we know is now wrong. */
1134 break;
1138 return ss;
1141 /* Return the set of pending signals in SS which should be delivered. */
1142 static sigset_t
1143 pending_signals (struct hurd_sigstate *ss)
1145 /* We don't worry about any pending signals if we are stopped, nor if
1146 SS is in a critical section. We are guaranteed to get a sig_post
1147 message before any of them become deliverable: either the SIGCONT
1148 signal, or a sig_post with SIGNO==0 as an explicit poll when the
1149 thread finishes its critical section. */
1150 if (_hurd_stopped || __spin_lock_locked (&ss->critical_section_lock))
1151 return 0;
1153 return _hurd_sigstate_pending (ss) & ~ss->blocked;
1156 /* Post the specified pending signals in SS and return 1. If one of
1157 them is traced, abort immediately and return 0. SS must be locked on
1158 entry and will be unlocked in all cases. */
1159 static int
1160 post_pending (struct hurd_sigstate *ss, sigset_t pending, void (*reply) (void))
1162 int signo;
1163 struct hurd_signal_detail detail;
1165 /* Make sure SS corresponds to an actual thread, since we assume it won't
1166 change in post_signal. */
1167 assert (ss->thread != MACH_PORT_NULL);
1169 for (signo = 1; signo < NSIG; ++signo)
1170 if (__sigismember (&pending, signo))
1172 detail = sigstate_clear_pending (ss, signo);
1173 _hurd_sigstate_unlock (ss);
1175 /* Will reacquire the lock, except if the signal is traced. */
1176 if (! post_signal (ss, signo, &detail, 0, reply))
1177 return 0;
1180 /* No more signals pending; SS->lock is still locked. */
1181 _hurd_sigstate_unlock (ss);
1183 return 1;
1186 /* Post all the pending signals of all threads and return 1. If a traced
1187 signal is encountered, abort immediately and return 0. */
1188 static int
1189 post_all_pending_signals (void (*reply) (void))
1191 struct hurd_sigstate *ss;
1192 sigset_t pending = 0;
1194 for (;;)
1196 __mutex_lock (&_hurd_siglock);
1197 for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
1199 _hurd_sigstate_lock (ss);
1201 pending = pending_signals (ss);
1202 if (pending)
1203 /* post_pending() below will unlock SS. */
1204 break;
1206 _hurd_sigstate_unlock (ss);
1208 __mutex_unlock (&_hurd_siglock);
1210 if (! pending)
1211 return 1;
1212 if (! post_pending (ss, pending, reply))
1213 return 0;
1217 /* Deliver a signal. SS is not locked. */
1218 void
1219 _hurd_internal_post_signal (struct hurd_sigstate *ss,
1220 int signo, struct hurd_signal_detail *detail,
1221 mach_port_t reply_port,
1222 mach_msg_type_name_t reply_port_type,
1223 int untraced)
1225 /* Reply to this sig_post message. */
1226 __typeof (__msg_sig_post_reply) *reply_rpc
1227 = (untraced ? __msg_sig_post_untraced_reply : __msg_sig_post_reply);
1228 void reply (void)
1230 error_t err;
1231 if (reply_port == MACH_PORT_NULL)
1232 return;
1233 err = (*reply_rpc) (reply_port, reply_port_type, 0);
1234 reply_port = MACH_PORT_NULL;
1235 if (err != MACH_SEND_INVALID_DEST) /* Ignore dead reply port. */
1236 assert_perror (err);
1239 ss = post_signal (ss, signo, detail, untraced, reply);
1240 if (! ss)
1241 return;
1243 /* The signal was neither fatal nor traced. We still hold SS->lock. */
1244 if (signo != 0 && ss->thread != MACH_PORT_NULL)
1246 /* The signal has either been ignored or is now being handled. We can
1247 consider it delivered and reply to the killer. */
1248 reply ();
1250 /* Post any pending signals for this thread. */
1251 if (! post_pending (ss, pending_signals (ss), reply))
1252 return;
1254 else
1256 /* If this was a process-wide signal or a poll request, we need
1257 to check for pending signals for all threads. */
1258 _hurd_sigstate_unlock (ss);
1259 if (! post_all_pending_signals (reply))
1260 return;
1262 /* All pending signals delivered to all threads.
1263 Now we can send the reply message even for signal 0. */
1264 reply ();
1268 /* Decide whether REFPORT enables the sender to send us a SIGNO signal.
1269 Returns zero if so, otherwise the error code to return to the sender. */
1271 static error_t
1272 signal_allowed (int signo, mach_port_t refport)
1274 if (signo < 0 || signo >= NSIG)
1275 return EINVAL;
1277 if (refport == __mach_task_self ())
1278 /* Can send any signal. */
1279 goto win;
1281 /* Avoid needing to check for this below. */
1282 if (refport == MACH_PORT_NULL)
1283 return EPERM;
1285 switch (signo)
1287 case SIGINT:
1288 case SIGQUIT:
1289 case SIGTSTP:
1290 case SIGHUP:
1291 case SIGINFO:
1292 case SIGTTIN:
1293 case SIGTTOU:
1294 case SIGWINCH:
1295 /* Job control signals can be sent by the controlling terminal. */
1296 if (__USEPORT (CTTYID, port == refport))
1297 goto win;
1298 break;
1300 case SIGCONT:
1302 /* A continue signal can be sent by anyone in the session. */
1303 mach_port_t sessport;
1304 if (! __USEPORT (PROC, __proc_getsidport (port, &sessport)))
1306 __mach_port_deallocate (__mach_task_self (), sessport);
1307 if (refport == sessport)
1308 goto win;
1311 break;
1313 case SIGIO:
1314 case SIGURG:
1316 /* Any io object a file descriptor refers to might send us
1317 one of these signals using its async ID port for REFPORT.
1319 This is pretty wide open; it is not unlikely that some random
1320 process can at least open for reading something we have open,
1321 get its async ID port, and send us a spurious SIGIO or SIGURG
1322 signal. But BSD is actually wider open than that!--you can set
1323 the owner of an io object to any process or process group
1324 whatsoever and send them gratuitous signals.
1326 Someday we could implement some reasonable scheme for
1327 authorizing SIGIO and SIGURG signals properly. */
1329 int d;
1330 int lucky = 0; /* True if we find a match for REFPORT. */
1331 __mutex_lock (&_hurd_dtable_lock);
1332 for (d = 0; !lucky && (unsigned) d < (unsigned) _hurd_dtablesize; ++d)
1334 struct hurd_userlink ulink;
1335 io_t port;
1336 mach_port_t asyncid;
1337 if (_hurd_dtable[d] == NULL)
1338 continue;
1339 port = _hurd_port_get (&_hurd_dtable[d]->port, &ulink);
1340 if (! __io_get_icky_async_id (port, &asyncid))
1342 if (refport == asyncid)
1343 /* Break out of the loop on the next iteration. */
1344 lucky = 1;
1345 __mach_port_deallocate (__mach_task_self (), asyncid);
1347 _hurd_port_free (&_hurd_dtable[d]->port, &ulink, port);
1349 __mutex_unlock (&_hurd_dtable_lock);
1350 /* If we found a lucky winner, we've set D to -1 in the loop. */
1351 if (lucky)
1352 goto win;
1356 /* If this signal is legit, we have done `goto win' by now.
1357 When we return the error, mig deallocates REFPORT. */
1358 return EPERM;
1360 win:
1361 /* Deallocate the REFPORT send right; we are done with it. */
1362 __mach_port_deallocate (__mach_task_self (), refport);
1364 return 0;
1367 /* Implement the sig_post RPC from <hurd/msg.defs>;
1368 sent when someone wants us to get a signal. */
1369 kern_return_t
1370 _S_msg_sig_post (mach_port_t me,
1371 mach_port_t reply_port, mach_msg_type_name_t reply_port_type,
1372 int signo, natural_t sigcode,
1373 mach_port_t refport)
1375 error_t err;
1376 struct hurd_signal_detail d;
1378 if (err = signal_allowed (signo, refport))
1379 return err;
1381 d.code = sigcode;
1382 d.exc = 0;
1384 /* Post the signal to a global receiver thread (or mark it pending in
1385 the global sigstate). This will reply when the signal can be
1386 considered delivered. */
1387 _hurd_internal_post_signal (_hurd_global_sigstate,
1388 signo, &d, reply_port, reply_port_type,
1389 0); /* Stop if traced. */
1391 return MIG_NO_REPLY; /* Already replied. */
1394 /* Implement the sig_post_untraced RPC from <hurd/msg.defs>;
1395 sent when the debugger wants us to really get a signal
1396 even if we are traced. */
1397 kern_return_t
1398 _S_msg_sig_post_untraced (mach_port_t me,
1399 mach_port_t reply_port,
1400 mach_msg_type_name_t reply_port_type,
1401 int signo, natural_t sigcode,
1402 mach_port_t refport)
1404 error_t err;
1405 struct hurd_signal_detail d;
1407 if (err = signal_allowed (signo, refport))
1408 return err;
1410 d.code = sigcode;
1411 d.exc = 0;
1413 /* Post the signal to the designated signal-receiving thread. This will
1414 reply when the signal can be considered delivered. */
1415 _hurd_internal_post_signal (_hurd_global_sigstate,
1416 signo, &d, reply_port, reply_port_type,
1417 1); /* Untraced flag. */
1419 return MIG_NO_REPLY; /* Already replied. */
1422 extern void __mig_init (void *);
1424 #include <mach/task_special_ports.h>
1426 /* Initialize the message port, _hurd_global_sigstate, and start the
1427 signal thread. */
1429 void
1430 _hurdsig_init (const int *intarray, size_t intarraysize)
1432 error_t err;
1433 vm_size_t stacksize;
1434 struct hurd_sigstate *ss;
1436 __mutex_init (&_hurd_siglock);
1438 err = __mach_port_allocate (__mach_task_self (),
1439 MACH_PORT_RIGHT_RECEIVE,
1440 &_hurd_msgport);
1441 assert_perror (err);
1443 /* Make a send right to the signal port. */
1444 err = __mach_port_insert_right (__mach_task_self (),
1445 _hurd_msgport,
1446 _hurd_msgport,
1447 MACH_MSG_TYPE_MAKE_SEND);
1448 assert_perror (err);
1450 /* Initialize the global signal state. */
1451 _hurd_global_sigstate = _hurd_thread_sigstate (MACH_PORT_NULL);
1453 /* We block all signals, and let actual threads pull them from the
1454 pending mask. */
1455 __sigfillset(& _hurd_global_sigstate->blocked);
1457 /* Initialize the main thread's signal state. */
1458 ss = _hurd_self_sigstate ();
1460 /* Mark it as a process-wide signal receiver. Threads in this set use
1461 the common action vector in _hurd_global_sigstate. */
1462 _hurd_sigstate_set_global_rcv (ss);
1464 /* Copy inherited signal settings from our parent (or pre-exec process
1465 state) */
1466 if (intarraysize > INIT_SIGMASK)
1467 ss->blocked = intarray[INIT_SIGMASK];
1468 if (intarraysize > INIT_SIGPENDING)
1469 _hurd_global_sigstate->pending = intarray[INIT_SIGPENDING];
1470 if (intarraysize > INIT_SIGIGN && intarray[INIT_SIGIGN] != 0)
1472 int signo;
1473 for (signo = 1; signo < NSIG; ++signo)
1474 if (intarray[INIT_SIGIGN] & __sigmask(signo))
1475 _hurd_global_sigstate->actions[signo].sa_handler = SIG_IGN;
1478 /* Start the signal thread listening on the message port. */
1480 #pragma weak __cthread_fork
1481 if (!__cthread_fork)
1483 err = __thread_create (__mach_task_self (), &_hurd_msgport_thread);
1484 assert_perror (err);
1486 stacksize = __vm_page_size * 8; /* Small stack for signal thread. */
1487 err = __mach_setup_thread (__mach_task_self (), _hurd_msgport_thread,
1488 _hurd_msgport_receive,
1489 (vm_address_t *) &__hurd_sigthread_stack_base,
1490 &stacksize);
1491 assert_perror (err);
1492 err = __mach_setup_tls (_hurd_msgport_thread);
1493 assert_perror (err);
1495 __hurd_sigthread_stack_end = __hurd_sigthread_stack_base + stacksize;
1497 /* Reinitialize the MiG support routines so they will use a per-thread
1498 variable for the cached reply port. */
1499 __mig_init ((void *) __hurd_sigthread_stack_base);
1501 err = __thread_resume (_hurd_msgport_thread);
1502 assert_perror (err);
1504 else
1506 /* When cthreads is being used, we need to make the signal thread a
1507 proper cthread. Otherwise it cannot use mutex_lock et al, which
1508 will be the cthreads versions. Various of the message port RPC
1509 handlers need to take locks, so we need to be able to call into
1510 cthreads code and meet its assumptions about how our thread and
1511 its stack are arranged. Since cthreads puts it there anyway,
1512 we'll let the signal thread's per-thread variables be found as for
1513 any normal cthread, and just leave the magic __hurd_sigthread_*
1514 values all zero so they'll be ignored. */
1515 #pragma weak __cthread_detach
1516 #pragma weak __pthread_getattr_np
1517 #pragma weak __pthread_attr_getstack
1518 __cthread_t thread = __cthread_fork (
1519 (cthread_fn_t) &_hurd_msgport_receive, 0);
1520 __cthread_detach (thread);
1522 if (__pthread_getattr_np)
1524 /* Record signal thread stack layout for fork() */
1525 pthread_attr_t attr;
1526 void *addr;
1527 size_t size;
1529 __pthread_getattr_np ((pthread_t) thread, &attr);
1530 __pthread_attr_getstack (&attr, &addr, &size);
1531 __hurd_sigthread_stack_base = (uintptr_t) addr;
1532 __hurd_sigthread_stack_end = __hurd_sigthread_stack_base + size;
1535 /* XXX We need the thread port for the signal thread further on
1536 in this thread (see hurdfault.c:_hurdsigfault_init).
1537 Therefore we block until _hurd_msgport_thread is initialized
1538 by the newly created thread. This really shouldn't be
1539 necessary; we should be able to fetch the thread port for a
1540 cthread from here. */
1541 while (_hurd_msgport_thread == 0)
1542 __swtch_pri (0);
1545 /* Receive exceptions on the signal port. */
1546 #ifdef TASK_EXCEPTION_PORT
1547 __task_set_special_port (__mach_task_self (),
1548 TASK_EXCEPTION_PORT, _hurd_msgport);
1549 #elif defined (EXC_MASK_ALL)
1550 __task_set_exception_ports (__mach_task_self (),
1551 EXC_MASK_ALL & ~(EXC_MASK_SYSCALL
1552 | EXC_MASK_MACH_SYSCALL
1553 | EXC_MASK_RPC_ALERT),
1554 _hurd_msgport,
1555 EXCEPTION_DEFAULT, MACHINE_THREAD_STATE);
1556 #else
1557 # error task_set_exception_port?
1558 #endif
1560 /* Sanity check. Any pending, unblocked signals should have been
1561 taken by our predecessor incarnation (i.e. parent or pre-exec state)
1562 before packing up our init ints. This assert is last (not above)
1563 so that signal handling is all set up to handle the abort. */
1564 assert ((ss->pending &~ ss->blocked) == 0);
1566 \f /* XXXX */
1567 /* Reauthenticate with the proc server. */
1569 static void
1570 reauth_proc (mach_port_t new)
1572 mach_port_t ref, ignore;
1574 ref = __mach_reply_port ();
1575 if (! HURD_PORT_USE (&_hurd_ports[INIT_PORT_PROC],
1576 __proc_reauthenticate (port, ref,
1577 MACH_MSG_TYPE_MAKE_SEND)
1578 || __auth_user_authenticate (new, ref,
1579 MACH_MSG_TYPE_MAKE_SEND,
1580 &ignore))
1581 && ignore != MACH_PORT_NULL)
1582 __mach_port_deallocate (__mach_task_self (), ignore);
1583 __mach_port_destroy (__mach_task_self (), ref);
1585 /* Set the owner of the process here too. */
1586 __mutex_lock (&_hurd_id.lock);
1587 if (!_hurd_check_ids ())
1588 HURD_PORT_USE (&_hurd_ports[INIT_PORT_PROC],
1589 __proc_setowner (port,
1590 (_hurd_id.gen.nuids
1591 ? _hurd_id.gen.uids[0] : 0),
1592 !_hurd_id.gen.nuids));
1593 __mutex_unlock (&_hurd_id.lock);
1595 (void) &reauth_proc; /* Silence compiler warning. */
1597 text_set_element (_hurd_reauth_hook, reauth_proc);
1599 /* Like `getenv', but safe for the signal thread to run.
1600 If the environment is trashed, this will just return NULL. */
1602 const char *
1603 _hurdsig_getenv (const char *variable)
1605 if (__libc_enable_secure)
1606 return NULL;
1608 if (_hurdsig_catch_memory_fault (__environ))
1609 /* We bombed in getenv. */
1610 return NULL;
1611 else
1613 const size_t len = strlen (variable);
1614 char *value = NULL;
1615 char *volatile *ep = __environ;
1616 while (*ep)
1618 const char *p = *ep;
1619 _hurdsig_fault_preemptor.first = (long int) p;
1620 _hurdsig_fault_preemptor.last = VM_MAX_ADDRESS;
1621 if (! strncmp (p, variable, len) && p[len] == '=')
1623 size_t valuelen;
1624 p += len + 1;
1625 valuelen = strlen (p);
1626 _hurdsig_fault_preemptor.last = (long int) (p + valuelen);
1627 value = malloc (++valuelen);
1628 if (value)
1629 memcpy (value, p, valuelen);
1630 break;
1632 _hurdsig_fault_preemptor.first = (long int) ++ep;
1633 _hurdsig_fault_preemptor.last = (long int) (ep + 1);
1635 _hurdsig_end_catch_fault ();
1636 return value;