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