]> git.ipfire.org Git - thirdparty/glibc.git/blame - hurd/hurdsig.c
Update.
[thirdparty/glibc.git] / hurd / hurdsig.c
CommitLineData
6bac11d9 1/* Copyright (C) 1991, 92, 93, 94, 95, 96, 97 Free Software Foundation, Inc.
2c6fe0bd 2 This file is part of the GNU C Library.
28f540f4 3
2c6fe0bd
UD
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.
28f540f4 8
2c6fe0bd
UD
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.
28f540f4 13
2c6fe0bd
UD
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 not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
28f540f4
RM
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <hurd.h>
22#include <hurd/signal.h>
23#include <cthreads.h> /* For `struct mutex'. */
24#include <string.h>
762a2918 25#include <hurd/id.h>
28f540f4
RM
26#include "hurdfault.h"
27#include "hurdmalloc.h" /* XXX */
28
29const char *_hurdsig_getenv (const char *);
30
31struct mutex _hurd_siglock;
32int _hurd_stopped;
33
34/* Port that receives signals and other miscellaneous messages. */
35mach_port_t _hurd_msgport;
36
37/* Thread listening on it. */
38thread_t _hurd_msgport_thread;
39
40/* Thread which receives task-global signals. */
41thread_t _hurd_sigthread;
42
6bac11d9
MB
43/* These are set up by _hurdsig_init. */
44unsigned long int __hurd_sigthread_stack_base;
45unsigned long int __hurd_sigthread_stack_end;
46unsigned long int *__hurd_sigthread_variables;
47
28f540f4
RM
48/* Linked-list of per-thread signal state. */
49struct hurd_sigstate *_hurd_sigstates;
54da5be3
RM
50
51/* Timeout for RPC's after interrupt_operation. */
52mach_msg_timeout_t _hurd_interrupted_rpc_timeout = 3000;
28f540f4
RM
53\f
54static void
55default_sigaction (struct sigaction actions[NSIG])
56{
57 int signo;
58
59 __sigemptyset (&actions[0].sa_mask);
60 actions[0].sa_flags = SA_RESTART;
61 actions[0].sa_handler = SIG_DFL;
62
63 for (signo = 1; signo < NSIG; ++signo)
64 actions[signo] = actions[0];
65}
66
67struct hurd_sigstate *
68_hurd_thread_sigstate (thread_t thread)
69{
70 struct hurd_sigstate *ss;
71 __mutex_lock (&_hurd_siglock);
72 for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
73 if (ss->thread == thread)
b96bdcd7 74 break;
28f540f4
RM
75 if (ss == NULL)
76 {
77 ss = malloc (sizeof (*ss));
78 if (ss == NULL)
79 __libc_fatal ("hurd: Can't allocate thread sigstate\n");
80 ss->thread = thread;
81 __spin_lock_init (&ss->lock);
82
6df1b247 83 /* Initialize default state. */
28f540f4
RM
84 __sigemptyset (&ss->blocked);
85 __sigemptyset (&ss->pending);
86 memset (&ss->sigaltstack, 0, sizeof (ss->sigaltstack));
10dc2a90 87 ss->preemptors = NULL;
28f540f4 88 ss->suspended = 0;
28f540f4
RM
89 ss->intr_port = MACH_PORT_NULL;
90 ss->context = NULL;
91
92 /* Initialize the sigaction vector from the default signal receiving
93 thread's state, and its from the system defaults. */
94 if (thread == _hurd_sigthread)
95 default_sigaction (ss->actions);
96 else
97 {
98 struct hurd_sigstate *s;
99 for (s = _hurd_sigstates; s != NULL; s = s->next)
100 if (s->thread == _hurd_sigthread)
101 break;
102 if (s)
103 {
104 __spin_lock (&s->lock);
105 memcpy (ss->actions, s->actions, sizeof (s->actions));
106 __spin_unlock (&s->lock);
107 }
108 else
109 default_sigaction (ss->actions);
110 }
111
112 ss->next = _hurd_sigstates;
113 _hurd_sigstates = ss;
114 }
115 __mutex_unlock (&_hurd_siglock);
116 return ss;
117}
118\f
119/* Signal delivery itself is on this page. */
120
121#include <hurd/fd.h>
9fd18b6c 122#include <hurd/crash.h>
28f540f4
RM
123#include <hurd/paths.h>
124#include <setjmp.h>
125#include <fcntl.h>
126#include <sys/wait.h>
127#include "thread_state.h"
128#include <hurd/msg_server.h>
129#include <hurd/msg_reply.h> /* For __msg_sig_post_reply. */
28f540f4 130#include <hurd/interrupt.h>
fb8e70d6
RM
131#include <assert.h>
132#include <unistd.h>
28f540f4
RM
133
134int _hurd_core_limit; /* XXX */
135
9fd18b6c 136/* Call the crash dump server to mummify us before we die.
28f540f4
RM
137 Returns nonzero if a core file was written. */
138static int
0e3426bb 139write_corefile (int signo, const struct hurd_signal_detail *detail)
28f540f4
RM
140{
141 error_t err;
142 mach_port_t coreserver;
143 file_t file, coredir;
144 const char *name;
145
146 /* XXX RLIMIT_CORE:
147 When we have a protocol to make the server return an error
148 for RLIMIT_FSIZE, then tell the corefile fs server the RLIMIT_CORE
149 value in place of the RLIMIT_FSIZE value. */
150
151 /* First get a port to the core dumping server. */
152 coreserver = MACH_PORT_NULL;
9fd18b6c 153 name = _hurdsig_getenv ("CRASHSERVER");
28f540f4
RM
154 if (name != NULL)
155 coreserver = __file_name_lookup (name, 0, 0);
156 if (coreserver == MACH_PORT_NULL)
9fd18b6c 157 coreserver = __file_name_lookup (_SERVERS_CRASH, 0, 0);
28f540f4
RM
158 if (coreserver == MACH_PORT_NULL)
159 return 0;
160
161 /* Get a port to the directory where the new core file will reside. */
78abf563 162 file = MACH_PORT_NULL;
28f540f4
RM
163 name = _hurdsig_getenv ("COREFILE");
164 if (name == NULL)
165 name = "core";
166 coredir = __file_name_split (name, (char **) &name);
78abf563
TBB
167 if (coredir != MACH_PORT_NULL)
168 /* Create the new file, but don't link it into the directory yet. */
169 __dir_mkfile (coredir, O_WRONLY|O_CREAT,
170 0600 & ~_hurd_umask, /* XXX ? */
0f110f41 171 &file);
28f540f4
RM
172
173 /* Call the core dumping server to write the core file. */
9fd18b6c
RM
174 err = __crash_dump_task (coreserver,
175 __mach_task_self (),
0e3426bb
RM
176 file,
177 signo, detail->code, detail->error,
178 detail->exc, detail->exc_code, detail->exc_subcode,
179 _hurd_ports[INIT_PORT_CTTYID].port,
180 MACH_MSG_TYPE_COPY_SEND);
28f540f4 181 __mach_port_deallocate (__mach_task_self (), coreserver);
78abf563
TBB
182
183 if (! err && file != MACH_PORT_NULL)
28f540f4
RM
184 /* The core dump into FILE succeeded, so now link it into the
185 directory. */
0e3426bb 186 err = __dir_link (file, coredir, name, 1);
28f540f4
RM
187 __mach_port_deallocate (__mach_task_self (), file);
188 __mach_port_deallocate (__mach_task_self (), coredir);
78abf563 189 return !err && file != MACH_PORT_NULL;
28f540f4
RM
190}
191
192
28f540f4
RM
193/* The lowest-numbered thread state flavor value is 1,
194 so we use bit 0 in machine_thread_all_state.set to
195 record whether we have done thread_abort. */
196#define THREAD_ABORTED 1
197
1a3a58fd 198/* SS->thread is suspended. Abort the thread and get its basic state. */
28f540f4
RM
199static void
200abort_thread (struct hurd_sigstate *ss, struct machine_thread_all_state *state,
1a3a58fd 201 void (*reply) (void))
28f540f4
RM
202{
203 if (!(state->set & THREAD_ABORTED))
204 {
b96bdcd7
RM
205 error_t err = __thread_abort (ss->thread);
206 assert_perror (err);
28f540f4
RM
207 /* Clear all thread state flavor set bits, because thread_abort may
208 have changed the state. */
209 state->set = THREAD_ABORTED;
210 }
211
1a3a58fd
RM
212 if (reply)
213 (*reply) ();
28f540f4
RM
214
215 machine_get_basic_state (ss->thread, state);
216}
217
218/* Find the location of the MiG reply port cell in use by the thread whose
54da5be3
RM
219 state is described by THREAD_STATE. If SIGTHREAD is nonzero, make sure
220 that this location can be set without faulting, or else return NULL. */
28f540f4
RM
221
222static mach_port_t *
54da5be3
RM
223interrupted_reply_port_location (struct machine_thread_all_state *thread_state,
224 int sigthread)
28f540f4
RM
225{
226 mach_port_t *portloc = (mach_port_t *) __hurd_threadvar_location_from_sp
227 (_HURD_THREADVAR_MIG_REPLY, (void *) thread_state->basic.SP);
228
fb8e70d6
RM
229 if (sigthread && _hurdsig_catch_memory_fault (portloc))
230 /* Faulted trying to read the stack. */
231 return NULL;
28f540f4
RM
232
233 /* Fault now if this pointer is bogus. */
234 *(volatile mach_port_t *) portloc = *portloc;
235
54da5be3
RM
236 if (sigthread)
237 _hurdsig_end_catch_fault ();
28f540f4
RM
238
239 return portloc;
240}
3fe9de0d 241\f
6df1b247 242#include <hurd/sigpreempt.h>
3fe9de0d 243#include "intr-msg.h"
28f540f4 244
d3dc731b
MB
245/* Timeout on interrupt_operation calls. */
246mach_msg_timeout_t _hurdsig_interrupt_timeout = 1000;
247
28f540f4
RM
248/* SS->thread is suspended.
249
250 Abort any interruptible RPC operation the thread is doing.
251
252 This uses only the constant member SS->thread and the unlocked, atomically
253 set member SS->intr_port, so no locking is needed.
254
255 If successfully sent an interrupt_operation and therefore the thread should
256 wait for its pending RPC to return (possibly EINTR) before taking the
257 incoming signal, returns the reply port to be received on. Otherwise
258 returns MACH_PORT_NULL.
259
75914335
RM
260 SIGNO is used to find the applicable SA_RESTART bit. If SIGNO is zero,
261 the RPC fails with EINTR instead of restarting (thread_cancel).
262
28f540f4
RM
263 *STATE_CHANGE is set nonzero if STATE->basic was modified and should
264 be applied back to the thread if it might ever run again, else zero. */
265
191abc51 266mach_port_t
75914335 267_hurdsig_abort_rpcs (struct hurd_sigstate *ss, int signo, int sigthread,
54da5be3 268 struct machine_thread_all_state *state, int *state_change,
1a3a58fd 269 void (*reply) (void))
28f540f4 270{
3fe9de0d 271 extern const void _hurd_intr_rpc_msg_in_trap;
54da5be3 272 mach_port_t rcv_port = MACH_PORT_NULL;
28f540f4
RM
273 mach_port_t intr_port;
274
275 *state_change = 0;
276
277 intr_port = ss->intr_port;
278 if (intr_port == MACH_PORT_NULL)
279 /* No interruption needs done. */
280 return MACH_PORT_NULL;
281
282 /* Abort the thread's kernel context, so any pending message send or
283 receive completes immediately or aborts. */
1a3a58fd 284 abort_thread (ss, state, reply);
28f540f4 285
54da5be3 286 if (state->basic.PC < (natural_t) &_hurd_intr_rpc_msg_in_trap)
28f540f4 287 {
54da5be3
RM
288 /* The thread is about to do the RPC, but hasn't yet entered
289 mach_msg. Mutate the thread's state so it knows not to try
290 the RPC. */
3fe9de0d 291 INTR_MSG_BACK_OUT (&state->basic);
54da5be3
RM
292 MACHINE_THREAD_STATE_SET_PC (&state->basic,
293 &_hurd_intr_rpc_msg_in_trap);
294 state->basic.SYSRETURN = MACH_SEND_INTERRUPTED;
295 *state_change = 1;
28f540f4 296 }
54da5be3
RM
297 else if (state->basic.PC == (natural_t) &_hurd_intr_rpc_msg_in_trap &&
298 /* The thread was blocked in the system call. After thread_abort,
299 the return value register indicates what state the RPC was in
300 when interrupted. */
301 state->basic.SYSRETURN == MACH_RCV_INTERRUPTED)
302 {
303 /* The RPC request message was sent and the thread was waiting for
304 the reply message; now the message receive has been aborted, so
305 the mach_msg call will return MACH_RCV_INTERRUPTED. We must tell
306 the server to interrupt the pending operation. The thread must
307 wait for the reply message before running the signal handler (to
308 guarantee that the operation has finished being interrupted), so
309 our nonzero return tells the trampoline code to finish the message
310 receive operation before running the handler. */
311
312 mach_port_t *reply = interrupted_reply_port_location (state,
313 sigthread);
d3dc731b 314 error_t err = __interrupt_operation (intr_port, _hurdsig_interrupt_timeout);
54da5be3
RM
315
316 if (err)
317 {
318 if (reply)
319 {
320 /* The interrupt didn't work.
321 Destroy the receive right the thread is blocked on. */
322 __mach_port_destroy (__mach_task_self (), *reply);
323 *reply = MACH_PORT_NULL;
324 }
28f540f4 325
54da5be3
RM
326 /* The system call return value register now contains
327 MACH_RCV_INTERRUPTED; when mach_msg resumes, it will retry the
328 call. Since we have just destroyed the receive right, the
329 retry will fail with MACH_RCV_INVALID_NAME. Instead, just
330 change the return value here to EINTR so mach_msg will not
331 retry and the EINTR error code will propagate up. */
332 state->basic.SYSRETURN = EINTR;
333 *state_change = 1;
334 }
335 else if (reply)
336 rcv_port = *reply;
337
338 /* All threads whose RPCs were interrupted by the interrupt_operation
339 call above will retry their RPCs unless we clear SS->intr_port.
340 So we clear it for the thread taking a signal when SA_RESTART is
341 clear, so that its call returns EINTR. */
342 if (! signo || !(ss->actions[signo].sa_flags & SA_RESTART))
343 ss->intr_port = MACH_PORT_NULL;
344 }
28f540f4 345
54da5be3 346 return rcv_port;
28f540f4
RM
347}
348
54da5be3 349
28f540f4
RM
350/* Abort the RPCs being run by all threads but this one;
351 all other threads should be suspended. If LIVE is nonzero, those
352 threads may run again, so they should be adjusted as necessary to be
353 happy when resumed. STATE is clobbered as a scratch area; its initial
354 contents are ignored, and its contents on return are not useful. */
355
356static void
357abort_all_rpcs (int signo, struct machine_thread_all_state *state, int live)
358{
359 /* We can just loop over the sigstates. Any thread doing something
360 interruptible must have one. We needn't bother locking because all
361 other threads are stopped. */
362
363 struct hurd_sigstate *ss;
364 size_t nthreads;
365 mach_port_t *reply_ports;
366
367 /* First loop over the sigstates to count them.
368 We need to know how big a vector we will need for REPLY_PORTS. */
369 nthreads = 0;
370 for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
371 ++nthreads;
372
373 reply_ports = alloca (nthreads * sizeof *reply_ports);
374
375 nthreads = 0;
fb8e70d6 376 for (ss = _hurd_sigstates; ss != NULL; ss = ss->next, ++nthreads)
28f540f4 377 if (ss->thread == _hurd_msgport_thread)
fb8e70d6 378 reply_ports[nthreads] = MACH_PORT_NULL;
28f540f4
RM
379 else
380 {
381 int state_changed;
382 state->set = 0; /* Reset scratch area. */
383
384 /* Abort any operation in progress with interrupt_operation.
385 Record the reply port the thread is waiting on.
386 We will wait for all the replies below. */
fb8e70d6
RM
387 reply_ports[nthreads] = _hurdsig_abort_rpcs (ss, signo, 1,
388 state, &state_changed,
389 NULL);
390 if (live)
391 {
392 if (reply_ports[nthreads] != MACH_PORT_NULL)
393 {
394 /* We will wait for the reply to this RPC below, so the
395 thread must issue a new RPC rather than waiting for the
396 reply to the one it sent. */
397 state->basic.SYSRETURN = EINTR;
398 state_changed = 1;
399 }
400 if (state_changed)
401 /* Aborting the RPC needed to change this thread's state,
402 and it might ever run again. So write back its state. */
403 __thread_set_state (ss->thread, MACHINE_THREAD_STATE_FLAVOR,
404 (natural_t *) &state->basic,
405 MACHINE_THREAD_STATE_COUNT);
406 }
28f540f4
RM
407 }
408
409 /* Wait for replies from all the successfully interrupted RPCs. */
410 while (nthreads-- > 0)
411 if (reply_ports[nthreads] != MACH_PORT_NULL)
412 {
413 error_t err;
414 mach_msg_header_t head;
54da5be3 415 err = __mach_msg (&head, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof head,
28f540f4 416 reply_ports[nthreads],
54da5be3
RM
417 _hurd_interrupted_rpc_timeout, MACH_PORT_NULL);
418 switch (err)
419 {
420 case MACH_RCV_TIMED_OUT:
421 case MACH_RCV_TOO_LARGE:
422 break;
423
424 default:
425 assert_perror (err);
426 }
28f540f4
RM
427 }
428}
429
43b0e40f 430struct hurd_signal_preemptor *_hurdsig_preemptors = 0;
6df1b247 431sigset_t _hurdsig_preempted_set;
28f540f4 432
6bac11d9
MB
433/* XXX temporary to deal with spelling fix */
434weak_alias (_hurdsig_preemptors, _hurdsig_preempters)
435
28f540f4
RM
436/* Mask of stop signals. */
437#define STOPSIGS (sigmask (SIGTTIN) | sigmask (SIGTTOU) | \
438 sigmask (SIGSTOP) | sigmask (SIGTSTP))
439
440/* Deliver a signal. SS is not locked. */
441void
442_hurd_internal_post_signal (struct hurd_sigstate *ss,
93a470c7 443 int signo, struct hurd_signal_detail *detail,
28f540f4
RM
444 mach_port_t reply_port,
445 mach_msg_type_name_t reply_port_type,
446 int untraced)
447{
b96bdcd7 448 error_t err;
28f540f4
RM
449 struct machine_thread_all_state thread_state;
450 enum { stop, ignore, core, term, handle } act;
10dc2a90 451 struct hurd_signal_preemptor *pe;
28f540f4 452 sighandler_t handler;
28f540f4
RM
453 sigset_t pending;
454 int ss_suspended;
455
456 /* Reply to this sig_post message. */
1a3a58fd
RM
457 __typeof (__msg_sig_post_reply) *reply_rpc
458 = (untraced ? __msg_sig_post_untraced_reply : __msg_sig_post_reply);
459 void reply (void)
28f540f4 460 {
1a3a58fd
RM
461 error_t err;
462 if (reply_port == MACH_PORT_NULL)
463 return;
464 err = (*reply_rpc) (reply_port, reply_port_type, 0);
465 reply_port = MACH_PORT_NULL;
466 if (err != MACH_SEND_INVALID_DEST) /* Ignore dead reply port. */
467 assert_perror (err);
28f540f4
RM
468 }
469
470 /* Mark the signal as pending. */
471 void mark_pending (void)
472 {
473 __sigaddset (&ss->pending, signo);
93a470c7 474 /* Save the details to be given to the handler when SIGNO is
28f540f4 475 unblocked. */
93a470c7 476 ss->pending_data[signo] = *detail;
28f540f4
RM
477 }
478
479 /* Suspend the process with SIGNO. */
480 void suspend (void)
481 {
482 /* Stop all other threads and mark ourselves stopped. */
483 __USEPORT (PROC,
484 ({
485 /* Hold the siglock while stopping other threads to be
486 sure it is not held by another thread afterwards. */
487 __mutex_lock (&_hurd_siglock);
488 __proc_dostop (port, _hurd_msgport_thread);
489 __mutex_unlock (&_hurd_siglock);
490 abort_all_rpcs (signo, &thread_state, 1);
6a7169a5 491 reply ();
0e3426bb 492 __proc_mark_stop (port, signo, detail->code);
28f540f4
RM
493 }));
494 _hurd_stopped = 1;
495 }
75914335
RM
496 /* Resume the process after a suspension. */
497 void resume (void)
498 {
499 /* Resume the process from being stopped. */
500 thread_t *threads;
501 mach_msg_type_number_t nthreads, i;
502 error_t err;
503
504 if (! _hurd_stopped)
505 return;
506
507 /* Tell the proc server we are continuing. */
508 __USEPORT (PROC, __proc_mark_cont (port));
509 /* Fetch ports to all our threads and resume them. */
510 err = __task_threads (__mach_task_self (), &threads, &nthreads);
511 assert_perror (err);
512 for (i = 0; i < nthreads; ++i)
513 {
514 if (threads[i] != _hurd_msgport_thread &&
515 (act != handle || threads[i] != ss->thread))
516 {
517 err = __thread_resume (threads[i]);
518 assert_perror (err);
519 }
520 err = __mach_port_deallocate (__mach_task_self (),
521 threads[i]);
522 assert_perror (err);
523 }
524 __vm_deallocate (__mach_task_self (),
525 (vm_address_t) threads,
526 nthreads * sizeof *threads);
527 _hurd_stopped = 0;
4d02a5b1
RM
528 if (act == handle)
529 /* The thread that will run the handler is already suspended. */
530 ss_suspended = 1;
75914335
RM
531 }
532
533 if (signo == 0)
534 {
535 if (untraced)
536 /* This is PTRACE_CONTINUE. */
537 resume ();
538
539 /* This call is just to check for pending signals. */
540 __spin_lock (&ss->lock);
541 goto check_pending_signals;
542 }
28f540f4
RM
543
544 post_signal:
545
546 thread_state.set = 0; /* We know nothing. */
547
6df1b247 548 __spin_lock (&ss->lock);
28f540f4 549
6df1b247
RM
550 /* Check for a preempted signal. Preempted signals can arrive during
551 critical sections. */
552
553 handler = SIG_ERR;
10dc2a90 554 for (pe = ss->preemptors; pe && handler == SIG_ERR; pe = pe->next)
93a470c7 555 if (HURD_PREEMPT_SIGNAL_P (pe, signo, detail->code))
10dc2a90 556 handler = (*pe->preemptor) (pe, ss, &signo, detail);
6df1b247
RM
557
558 if (handler == SIG_ERR && (__sigmask (signo) & _hurdsig_preempted_set))
559 {
560 __mutex_lock (&_hurd_siglock);
10dc2a90 561 for (pe = _hurdsig_preemptors; pe && handler == SIG_ERR; pe = pe->next)
0e3426bb 562 if (HURD_PREEMPT_SIGNAL_P (pe, signo, detail->code))
10dc2a90 563 handler = (*pe->preemptor) (pe, ss, &signo, detail);
6df1b247
RM
564 __mutex_unlock (&_hurd_siglock);
565 }
28f540f4
RM
566
567 ss_suspended = 0;
568
6df1b247
RM
569 if (handler == SIG_IGN)
570 /* Ignore the signal altogether. */
571 act = ignore;
572 if (handler != SIG_ERR)
28f540f4
RM
573 /* Run the preemption-provided handler. */
574 act = handle;
575 else
576 {
577 /* No preemption. Do normal handling. */
578
8f0c527e 579 if (!untraced && __sigismember (&_hurdsig_traced, signo))
28f540f4
RM
580 {
581 /* We are being traced. Stop to tell the debugger of the signal. */
582 if (_hurd_stopped)
583 /* Already stopped. Mark the signal as pending;
584 when resumed, we will notice it and stop again. */
585 mark_pending ();
586 else
587 suspend ();
588 __spin_unlock (&ss->lock);
589 reply ();
590 return;
591 }
592
75914335
RM
593 handler = ss->actions[signo].sa_handler;
594
28f540f4
RM
595 if (handler == SIG_DFL)
596 /* Figure out the default action for this signal. */
597 switch (signo)
598 {
599 case 0:
600 /* A sig_post msg with SIGNO==0 is sent to
601 tell us to check for pending signals. */
602 act = ignore;
603 break;
604
605 case SIGTTIN:
606 case SIGTTOU:
607 case SIGSTOP:
608 case SIGTSTP:
609 act = stop;
610 break;
611
612 case SIGCONT:
613 case SIGIO:
614 case SIGURG:
615 case SIGCHLD:
616 case SIGWINCH:
617 act = ignore;
618 break;
619
620 case SIGQUIT:
621 case SIGILL:
622 case SIGTRAP:
623 case SIGIOT:
624 case SIGEMT:
625 case SIGFPE:
626 case SIGBUS:
627 case SIGSEGV:
628 case SIGSYS:
629 act = core;
630 break;
631
632 case SIGINFO:
633 if (_hurd_pgrp == _hurd_pid)
634 {
635 /* We are the process group leader. Since there is no
636 user-specified handler for SIGINFO, we use a default one
637 which prints something interesting. We use the normal
638 handler mechanism instead of just doing it here to avoid
639 the signal thread faulting or blocking in this
640 potentially hairy operation. */
641 act = handle;
642 handler = _hurd_siginfo_handler;
643 }
644 else
645 act = ignore;
646 break;
647
648 default:
649 act = term;
650 break;
651 }
652 else if (handler == SIG_IGN)
653 act = ignore;
654 else
655 act = handle;
656
657 if (__sigmask (signo) & STOPSIGS)
658 /* Stop signals clear a pending SIGCONT even if they
659 are handled or ignored (but not if preempted). */
660 ss->pending &= ~sigmask (SIGCONT);
661 else
662 {
663 if (signo == SIGCONT)
664 /* Even if handled or ignored (but not preempted), SIGCONT clears
665 stop signals and resumes the process. */
666 ss->pending &= ~STOPSIGS;
667
668 if (_hurd_stopped && act != stop && (untraced || signo == SIGCONT))
75914335 669 resume ();
28f540f4
RM
670 }
671 }
672
673 if (_hurd_orphaned && act == stop &&
674 (__sigmask (signo) & (__sigmask (SIGTTIN) | __sigmask (SIGTTOU) |
675 __sigmask (SIGTSTP))))
676 {
677 /* If we would ordinarily stop for a job control signal, but we are
678 orphaned so noone would ever notice and continue us again, we just
679 quietly die, alone and in the dark. */
0e3426bb 680 detail->code = signo;
28f540f4
RM
681 signo = SIGKILL;
682 act = term;
683 }
684
75914335 685 /* Handle receipt of a blocked signal, or any signal while stopped. */
4d02a5b1
RM
686 if (act != ignore && /* Signals ignored now are forgotten now. */
687 __sigismember (&ss->blocked, signo) ||
28f540f4
RM
688 (signo != SIGKILL && _hurd_stopped))
689 {
690 mark_pending ();
691 act = ignore;
692 }
693
694 /* Perform the chosen action for the signal. */
695 switch (act)
696 {
697 case stop:
698 if (_hurd_stopped)
b96bdcd7
RM
699 {
700 /* We are already stopped, but receiving an untraced stop
701 signal. Instead of resuming and suspending again, just
702 notify the proc server of the new stop signal. */
0e3426bb
RM
703 error_t err = __USEPORT (PROC, __proc_mark_stop
704 (port, signo, detail->code));
b96bdcd7
RM
705 assert_perror (err);
706 }
28f540f4
RM
707 else
708 /* Suspend the process. */
709 suspend ();
710 break;
711
712 case ignore:
4d02a5b1
RM
713 /* Nobody cares about this signal. If there was a call to resume
714 above in SIGCONT processing and we've left a thread suspended,
715 now's the time to set it going. */
716 if (ss_suspended)
717 {
718 err = __thread_resume (ss->thread);
719 assert_perror (err);
720 ss_suspended = 0;
721 }
28f540f4
RM
722 break;
723
421f82e5
RM
724 sigbomb:
725 /* We got a fault setting up the stack frame for the handler.
726 Nothing to do but die; BSD gets SIGILL in this case. */
0e3426bb 727 detail->code = signo; /* XXX ? */
421f82e5
RM
728 signo = SIGILL;
729 act = core;
730 /* FALLTHROUGH */
731
28f540f4
RM
732 case term: /* Time to die. */
733 case core: /* And leave a rotting corpse. */
28f540f4 734 /* Have the proc server stop all other threads in our task. */
b96bdcd7
RM
735 err = __USEPORT (PROC, __proc_dostop (port, _hurd_msgport_thread));
736 assert_perror (err);
28f540f4
RM
737 /* No more user instructions will be executed.
738 The signal can now be considered delivered. */
739 reply ();
740 /* Abort all server operations now in progress. */
741 abort_all_rpcs (signo, &thread_state, 0);
742
743 {
744 int status = W_EXITCODE (0, signo);
745 /* Do a core dump if desired. Only set the wait status bit saying we
746 in fact dumped core if the operation was actually successful. */
0e3426bb 747 if (act == core && write_corefile (signo, detail))
28f540f4
RM
748 status |= WCOREFLAG;
749 /* Tell proc how we died and then stick the saber in the gut. */
750 _hurd_exit (status);
751 /* NOTREACHED */
752 }
753
754 case handle:
755 /* Call a handler for this signal. */
756 {
421f82e5 757 struct sigcontext *scp, ocontext;
28f540f4
RM
758 int wait_for_reply, state_changed;
759
760 /* Stop the thread and abort its pending RPC operations. */
761 if (! ss_suspended)
b96bdcd7
RM
762 {
763 err = __thread_suspend (ss->thread);
764 assert_perror (err);
765 }
28f540f4
RM
766
767 /* Abort the thread's kernel context, so any pending message send
768 or receive completes immediately or aborts. If an interruptible
769 RPC is in progress, abort_rpcs will do this. But we must always
770 do it before fetching the thread's state, because
771 thread_get_state is never kosher before thread_abort. */
1a3a58fd 772 abort_thread (ss, &thread_state, NULL);
28f540f4 773
421f82e5
RM
774 if (ss->context)
775 {
776 /* We have a previous sigcontext that sigreturn was about
777 to restore when another signal arrived. */
778
779 mach_port_t *loc;
28f540f4 780
fb8e70d6 781 if (_hurdsig_catch_memory_fault (ss->context))
421f82e5 782 {
421f82e5
RM
783 /* We faulted reading the thread's stack. Forget that
784 context and pretend it wasn't there. It almost
785 certainly crash if this handler returns, but that's it's
786 problem. */
787 ss->context = NULL;
788 }
789 else
790 {
791 /* Copy the context from the thread's stack before
792 we start diddling the stack to set up the handler. */
793 ocontext = *ss->context;
794 ss->context = &ocontext;
795 }
796 _hurdsig_end_catch_fault ();
75914335 797
421f82e5
RM
798 if (! machine_get_basic_state (ss->thread, &thread_state))
799 goto sigbomb;
54da5be3 800 loc = interrupted_reply_port_location (&thread_state, 1);
421f82e5
RM
801 if (loc && *loc != MACH_PORT_NULL)
802 /* This is the reply port for the context which called
803 sigreturn. Since we are abandoning that context entirely
804 and restoring SS->context instead, destroy this port. */
805 __mach_port_destroy (__mach_task_self (), *loc);
806
807 /* The thread was in sigreturn, not in any interruptible RPC. */
808 wait_for_reply = 0;
809
8f0c527e 810 assert (! __spin_lock_locked (&ss->critical_section_lock));
421f82e5
RM
811 }
812 else
28f540f4 813 {
e33b438a
RM
814 int crit = __spin_lock_locked (&ss->critical_section_lock);
815
54da5be3 816 wait_for_reply
e33b438a
RM
817 = (_hurdsig_abort_rpcs (ss,
818 /* In a critical section, any RPC
819 should be cancelled instead of
820 restarted, regardless of
3081378b 821 SA_RESTART, so the entire
e33b438a
RM
822 "atomic" operation can be aborted
823 as a unit. */
824 crit ? 0 : signo, 1,
54da5be3 825 &thread_state, &state_changed,
1a3a58fd 826 &reply)
54da5be3 827 != MACH_PORT_NULL);
421f82e5 828
e33b438a 829 if (crit)
421f82e5
RM
830 {
831 /* The thread is in a critical section. Mark the signal as
832 pending. When it finishes the critical section, it will
833 check for pending signals. */
834 mark_pending ();
e33b438a
RM
835 if (state_changed)
836 /* Some cases of interrupting an RPC must change the
837 thread state to back out the call. Normally this
838 change is rolled into the warping to the handler and
839 sigreturn, but we are not running the handler now
840 because the thread is in a critical section. Instead,
841 mutate the thread right away for the RPC interruption
842 and resume it; the RPC will return early so the
843 critical section can end soon. */
844 __thread_set_state (ss->thread, MACHINE_THREAD_STATE_FLAVOR,
845 (natural_t *) &thread_state.basic,
846 MACHINE_THREAD_STATE_COUNT);
847 /* */
848 ss->intr_port = MACH_PORT_NULL;
421f82e5
RM
849 __thread_resume (ss->thread);
850 break;
851 }
28f540f4
RM
852 }
853
854 /* Call the machine-dependent function to set the thread up
855 to run the signal handler, and preserve its old context. */
0e3426bb 856 scp = _hurd_setup_sighandler (ss, handler, signo, detail,
28f540f4
RM
857 wait_for_reply, &thread_state);
858 if (scp == NULL)
421f82e5 859 goto sigbomb;
28f540f4
RM
860
861 /* Set the machine-independent parts of the signal context. */
862
28f540f4
RM
863 {
864 /* Fetch the thread variable for the MiG reply port,
865 and set it to MACH_PORT_NULL. */
54da5be3
RM
866 mach_port_t *loc = interrupted_reply_port_location (&thread_state,
867 1);
28f540f4
RM
868 if (loc)
869 {
870 scp->sc_reply_port = *loc;
871 *loc = MACH_PORT_NULL;
872 }
873 else
874 scp->sc_reply_port = MACH_PORT_NULL;
421f82e5
RM
875
876 /* Save the intr_port in use by the interrupted code,
877 and clear the cell before running the trampoline. */
878 scp->sc_intr_port = ss->intr_port;
879 ss->intr_port = MACH_PORT_NULL;
880
881 if (ss->context)
882 {
883 /* After the handler runs we will restore to the state in
884 SS->context, not the state of the thread now. So restore
885 that context's reply port and intr port. */
886
887 scp->sc_reply_port = ss->context->sc_reply_port;
888 scp->sc_intr_port = ss->context->sc_intr_port;
889
890 ss->context = NULL;
891 }
28f540f4
RM
892 }
893
421f82e5 894 /* Backdoor extra argument to signal handler. */
0e3426bb 895 scp->sc_error = detail->error;
421f82e5 896
28f540f4
RM
897 /* Block SIGNO and requested signals while running the handler. */
898 scp->sc_mask = ss->blocked;
899 ss->blocked |= __sigmask (signo) | ss->actions[signo].sa_mask;
900
28f540f4
RM
901 /* Start the thread running the handler (or possibly waiting for an
902 RPC reply before running the handler). */
b96bdcd7
RM
903 err = __thread_set_state (ss->thread, MACHINE_THREAD_STATE_FLAVOR,
904 (natural_t *) &thread_state.basic,
905 MACHINE_THREAD_STATE_COUNT);
906 assert_perror (err);
907 err = __thread_resume (ss->thread);
908 assert_perror (err);
28f540f4
RM
909 thread_state.set = 0; /* Everything we know is now wrong. */
910 break;
911 }
912 }
913
914 /* The signal has either been ignored or is now being handled. We can
75914335
RM
915 consider it delivered and reply to the killer. */
916 reply ();
28f540f4
RM
917
918 /* We get here unless the signal was fatal. We still hold SS->lock.
919 Check for pending signals, and loop to post them. */
7cc645ed
RM
920 {
921 /* Return nonzero if SS has any signals pending we should worry about.
922 We don't worry about any pending signals if we are stopped, nor if
923 SS is in a critical section. We are guaranteed to get a sig_post
924 message before any of them become deliverable: either the SIGCONT
925 signal, or a sig_post with SIGNO==0 as an explicit poll when the
926 thread finishes its critical section. */
927 inline int signals_pending (void)
928 {
8f0c527e 929 if (_hurd_stopped || __spin_lock_locked (&ss->critical_section_lock))
7cc645ed
RM
930 return 0;
931 return pending = ss->pending & ~ss->blocked;
932 }
933
75914335
RM
934 check_pending_signals:
935 untraced = 0;
936
7cc645ed
RM
937 if (signals_pending ())
938 {
7cc645ed
RM
939 for (signo = 1; signo < NSIG; ++signo)
940 if (__sigismember (&pending, signo))
941 {
6172aaa3 942 deliver_pending:
7cc645ed 943 __sigdelset (&ss->pending, signo);
0e3426bb 944 *detail = ss->pending_data[signo];
7cc645ed
RM
945 __spin_unlock (&ss->lock);
946 goto post_signal;
947 }
948 }
949
950 /* No pending signals left undelivered for this thread.
951 If we were sent signal 0, we need to check for pending
952 signals for all threads. */
953 if (signo == 0)
954 {
955 __spin_unlock (&ss->lock);
956 __mutex_lock (&_hurd_siglock);
957 for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
28f540f4 958 {
7cc645ed 959 __spin_lock (&ss->lock);
4d02a5b1 960 for (signo = 1; signo < NSIG; ++signo)
1a6a8198
TBB
961 if (__sigismember (&ss->pending, signo)
962 && (!__sigismember (&ss->blocked, signo)
4bbb963e
RM
963 /* We "deliver" immediately pending blocked signals whose
964 action might be to ignore, so that if ignored they are
965 dropped right away. */
1a6a8198
TBB
966 || ss->actions[signo].sa_handler == SIG_IGN
967 || ss->actions[signo].sa_handler == SIG_DFL))
6a60a937
TBB
968 {
969 mutex_unlock (&_hurd_siglock);
970 goto deliver_pending;
971 }
28f540f4 972 __spin_unlock (&ss->lock);
28f540f4 973 }
7cc645ed
RM
974 __mutex_unlock (&_hurd_siglock);
975 }
976 else
977 {
978 /* No more signals pending; SS->lock is still locked.
979 Wake up any sigsuspend call that is blocking SS->thread. */
980 if (ss->suspended != MACH_PORT_NULL)
981 {
982 /* There is a sigsuspend waiting. Tell it to wake up. */
983 error_t err;
984 mach_msg_header_t msg;
985 err = __mach_port_insert_right (__mach_task_self (),
986 ss->suspended, ss->suspended,
987 MACH_MSG_TYPE_MAKE_SEND);
988 assert_perror (err);
989 msg.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_MOVE_SEND, 0);
990 msg.msgh_remote_port = ss->suspended;
991 msg.msgh_local_port = MACH_PORT_NULL;
992 /* These values do not matter. */
993 msg.msgh_id = 8675309; /* Jenny, Jenny. */
994 msg.msgh_seqno = 17; /* Random. */
995 ss->suspended = MACH_PORT_NULL;
996 err = __mach_msg (&msg, MACH_SEND_MSG, sizeof msg, 0,
997 MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE,
998 MACH_PORT_NULL);
999 assert_perror (err);
1000 }
1001 __spin_unlock (&ss->lock);
1002 }
1003 }
28f540f4
RM
1004
1005 /* All pending signals delivered to all threads.
1006 Now we can send the reply message even for signal 0. */
1007 reply ();
1008}
1009\f
1010/* Decide whether REFPORT enables the sender to send us a SIGNO signal.
1011 Returns zero if so, otherwise the error code to return to the sender. */
1012
1013static error_t
1014signal_allowed (int signo, mach_port_t refport)
1015{
1016 if (signo < 0 || signo >= NSIG)
1017 return EINVAL;
1018
1019 if (refport == __mach_task_self ())
1020 /* Can send any signal. */
1021 goto win;
1022
1023 /* Avoid needing to check for this below. */
1024 if (refport == MACH_PORT_NULL)
1025 return EPERM;
1026
1027 switch (signo)
1028 {
1029 case SIGINT:
1030 case SIGQUIT:
1031 case SIGTSTP:
1032 case SIGHUP:
1033 case SIGINFO:
1034 case SIGTTIN:
1035 case SIGTTOU:
ce7f605b 1036 case SIGWINCH:
28f540f4
RM
1037 /* Job control signals can be sent by the controlling terminal. */
1038 if (__USEPORT (CTTYID, port == refport))
1039 goto win;
1040 break;
1041
1042 case SIGCONT:
1043 {
1044 /* A continue signal can be sent by anyone in the session. */
1045 mach_port_t sessport;
1046 if (! __USEPORT (PROC, __proc_getsidport (port, &sessport)))
75914335 1047 {
28f540f4
RM
1048 __mach_port_deallocate (__mach_task_self (), sessport);
1049 if (refport == sessport)
1050 goto win;
1051 }
1052 }
1053 break;
1054
1055 case SIGIO:
1056 case SIGURG:
1057 {
1058 /* Any io object a file descriptor refers to might send us
1059 one of these signals using its async ID port for REFPORT.
1060
1061 This is pretty wide open; it is not unlikely that some random
1062 process can at least open for reading something we have open,
1063 get its async ID port, and send us a spurious SIGIO or SIGURG
1064 signal. But BSD is actually wider open than that!--you can set
1065 the owner of an io object to any process or process group
1066 whatsoever and send them gratuitous signals.
1067
1068 Someday we could implement some reasonable scheme for
1069 authorizing SIGIO and SIGURG signals properly. */
1070
1071 int d;
e0637da1 1072 int lucky = 0; /* True if we find a match for REFPORT. */
28f540f4 1073 __mutex_lock (&_hurd_dtable_lock);
e0637da1 1074 for (d = 0; !lucky && (unsigned) d < (unsigned) _hurd_dtablesize; ++d)
28f540f4
RM
1075 {
1076 struct hurd_userlink ulink;
1077 io_t port;
1078 mach_port_t asyncid;
1079 if (_hurd_dtable[d] == NULL)
1080 continue;
1081 port = _hurd_port_get (&_hurd_dtable[d]->port, &ulink);
1082 if (! __io_get_icky_async_id (port, &asyncid))
1083 {
1084 if (refport == asyncid)
1085 /* Break out of the loop on the next iteration. */
e0637da1 1086 lucky = 1;
28f540f4
RM
1087 __mach_port_deallocate (__mach_task_self (), asyncid);
1088 }
1089 _hurd_port_free (&_hurd_dtable[d]->port, &ulink, port);
1090 }
1091 /* If we found a lucky winner, we've set D to -1 in the loop. */
e0637da1 1092 if (lucky)
28f540f4
RM
1093 goto win;
1094 }
1095 }
1096
1097 /* If this signal is legit, we have done `goto win' by now.
1098 When we return the error, mig deallocates REFPORT. */
1099 return EPERM;
1100
1101 win:
1102 /* Deallocate the REFPORT send right; we are done with it. */
1103 __mach_port_deallocate (__mach_task_self (), refport);
1104
1105 return 0;
1106}
1107
1108/* Implement the sig_post RPC from <hurd/msg.defs>;
1109 sent when someone wants us to get a signal. */
1110kern_return_t
1111_S_msg_sig_post (mach_port_t me,
1112 mach_port_t reply_port, mach_msg_type_name_t reply_port_type,
8f0c527e 1113 int signo, natural_t sigcode,
28f540f4
RM
1114 mach_port_t refport)
1115{
1116 error_t err;
93a470c7 1117 struct hurd_signal_detail d;
28f540f4
RM
1118
1119 if (err = signal_allowed (signo, refport))
1120 return err;
1121
93a470c7
RM
1122 d.code = sigcode;
1123 d.exc = 0;
1124
28f540f4
RM
1125 /* Post the signal to the designated signal-receiving thread. This will
1126 reply when the signal can be considered delivered. */
1127 _hurd_internal_post_signal (_hurd_thread_sigstate (_hurd_sigthread),
93a470c7 1128 signo, &d, reply_port, reply_port_type,
28f540f4
RM
1129 0); /* Stop if traced. */
1130
1131 return MIG_NO_REPLY; /* Already replied. */
1132}
1133
1134/* Implement the sig_post_untraced RPC from <hurd/msg.defs>;
1135 sent when the debugger wants us to really get a signal
1136 even if we are traced. */
1137kern_return_t
1138_S_msg_sig_post_untraced (mach_port_t me,
1139 mach_port_t reply_port,
1140 mach_msg_type_name_t reply_port_type,
8f0c527e 1141 int signo, natural_t sigcode,
28f540f4
RM
1142 mach_port_t refport)
1143{
1144 error_t err;
93a470c7 1145 struct hurd_signal_detail d;
28f540f4
RM
1146
1147 if (err = signal_allowed (signo, refport))
1148 return err;
1149
93a470c7
RM
1150 d.code = sigcode;
1151 d.exc = 0;
1152
28f540f4
RM
1153 /* Post the signal to the designated signal-receiving thread. This will
1154 reply when the signal can be considered delivered. */
1155 _hurd_internal_post_signal (_hurd_thread_sigstate (_hurd_sigthread),
93a470c7 1156 signo, &d, reply_port, reply_port_type,
28f540f4
RM
1157 1); /* Untraced flag. */
1158
1159 return MIG_NO_REPLY; /* Already replied. */
1160}
1161\f
1162extern void __mig_init (void *);
1163
1164#include <mach/task_special_ports.h>
1165
1166/* Initialize the message port and _hurd_sigthread and start the signal
1167 thread. */
1168
1169void
1170_hurdsig_init (void)
1171{
1172 error_t err;
1173 vm_size_t stacksize;
1174
1175 __mutex_init (&_hurd_siglock);
1176
a5a81fec
RM
1177 err = __mach_port_allocate (__mach_task_self (),
1178 MACH_PORT_RIGHT_RECEIVE,
1179 &_hurd_msgport);
1180 assert_perror (err);
75914335 1181
28f540f4 1182 /* Make a send right to the signal port. */
a5a81fec
RM
1183 err = __mach_port_insert_right (__mach_task_self (),
1184 _hurd_msgport,
1185 _hurd_msgport,
1186 MACH_MSG_TYPE_MAKE_SEND);
1187 assert_perror (err);
28f540f4
RM
1188
1189 /* Set the default thread to receive task-global signals
1190 to this one, the main (first) user thread. */
1191 _hurd_sigthread = __mach_thread_self ();
1192
1193 /* Start the signal thread listening on the message port. */
1194
a5a81fec
RM
1195 err = __thread_create (__mach_task_self (), &_hurd_msgport_thread);
1196 assert_perror (err);
28f540f4 1197
1f205a47 1198 stacksize = __vm_page_size * 8; /* Small stack for signal thread. */
a5a81fec
RM
1199 err = __mach_setup_thread (__mach_task_self (), _hurd_msgport_thread,
1200 _hurd_msgport_receive,
1201 (vm_address_t *) &__hurd_sigthread_stack_base,
1202 &stacksize);
1203 assert_perror (err);
28f540f4
RM
1204
1205 __hurd_sigthread_stack_end = __hurd_sigthread_stack_base + stacksize;
1206 __hurd_sigthread_variables =
1207 malloc (__hurd_threadvar_max * sizeof (unsigned long int));
1208 if (__hurd_sigthread_variables == NULL)
1209 __libc_fatal ("hurd: Can't allocate thread variables for signal thread\n");
1210
1211 /* Reinitialize the MiG support routines so they will use a per-thread
1212 variable for the cached reply port. */
1213 __mig_init ((void *) __hurd_sigthread_stack_base);
1214
a5a81fec
RM
1215 err = __thread_resume (_hurd_msgport_thread);
1216 assert_perror (err);
75914335 1217
ae1025be
UD
1218 (void) _hurd_self_sigstate ();
1219
28f540f4
RM
1220 /* Receive exceptions on the signal port. */
1221 __task_set_special_port (__mach_task_self (),
1222 TASK_EXCEPTION_PORT, _hurd_msgport);
28f540f4
RM
1223}
1224\f /* XXXX */
1225/* Reauthenticate with the proc server. */
1226
1227static void
1228reauth_proc (mach_port_t new)
1229{
1230 mach_port_t ref, ignore;
1231
1232 ref = __mach_reply_port ();
1233 if (! HURD_PORT_USE (&_hurd_ports[INIT_PORT_PROC],
1234 __proc_reauthenticate (port, ref,
1235 MACH_MSG_TYPE_MAKE_SEND) ||
8f0c527e 1236 __auth_user_authenticate (new, ref,
28f540f4
RM
1237 MACH_MSG_TYPE_MAKE_SEND,
1238 &ignore))
1239 && ignore != MACH_PORT_NULL)
1240 __mach_port_deallocate (__mach_task_self (), ignore);
1241 __mach_port_destroy (__mach_task_self (), ref);
1242
762a2918
UD
1243 /* Set the owner of the process here too. */
1244 mutex_lock (&_hurd_id.lock);
1245 if (!_hurd_check_ids ())
1246 HURD_PORT_USE (&_hurd_ports[INIT_PORT_PROC],
1247 __proc_setowner (port,
1248 (_hurd_id.gen.nuids
1249 ? _hurd_id.gen.uids[0] : 0),
1250 !_hurd_id.gen.nuids));
1251 mutex_unlock (&_hurd_id.lock);
1252
28f540f4
RM
1253 (void) &reauth_proc; /* Silence compiler warning. */
1254}
1255text_set_element (_hurd_reauth_hook, reauth_proc);
1256\f
1257/* Like `getenv', but safe for the signal thread to run.
1258 If the environment is trashed, this will just return NULL. */
1259
1260const char *
1261_hurdsig_getenv (const char *variable)
1262{
fb8e70d6 1263 if (_hurdsig_catch_memory_fault (__environ))
28f540f4
RM
1264 /* We bombed in getenv. */
1265 return NULL;
1266 else
1267 {
fb8e70d6
RM
1268 const size_t len = strlen (variable);
1269 char *value = NULL;
1270 char *volatile *ep = __environ;
1271 while (*ep)
1272 {
1273 const char *p = *ep;
10dc2a90
UD
1274 _hurdsig_fault_preemptor.first = (long int) p;
1275 _hurdsig_fault_preemptor.last = VM_MAX_ADDRESS;
fb8e70d6
RM
1276 if (! strncmp (p, variable, len) && p[len] == '=')
1277 {
1278 char *value;
1279 size_t valuelen;
1280 p += len + 1;
1281 valuelen = strlen (p);
10dc2a90 1282 _hurdsig_fault_preemptor.last = (long int) (p + valuelen);
fb8e70d6
RM
1283 value = malloc (++valuelen);
1284 if (value)
1285 memcpy (value, p, valuelen);
1286 break;
1287 }
10dc2a90
UD
1288 _hurdsig_fault_preemptor.first = (long int) ++ep;
1289 _hurdsig_fault_preemptor.last = (long int) (ep + 1);
fb8e70d6 1290 }
28f540f4
RM
1291 _hurdsig_end_catch_fault ();
1292 return value;
1293 }
1294}