static kern_return_t set_int (int which, int value) { switch (which) { case INIT_UMASK: _hurd_umask = value; return 0; /* These are pretty odd things to do. But you asked for it. */ case INIT_SIGMASK: { struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread); __spin_lock (&ss->lock); ss->blocked = value; __spin_unlock (&ss->lock); return 0; } case INIT_SIGPENDING: { struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread); __spin_lock (&ss->lock); ss->pending = value; __spin_unlock (&ss->lock); return 0; } case INIT_SIGIGN: { struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread); int sig; const sigset_t ign = value; __spin_lock (&ss->lock); for (sig = 1; sig < NSIG; ++sig) { if (__sigismember (&ign, sig)) ss->actions[sig].sa_handler = SIG_IGN; else if (ss->actions[sig].sa_handler == SIG_IGN) ss->actions[sig].sa_handler = SIG_DFL; } __spin_unlock (&ss->lock); return 0; case INIT_TRACEMASK: _hurdsig_traced = value; return 0; } default: return EINVAL; } }
static kern_return_t get_int (int which, int *value) { switch (which) { case INIT_UMASK: *value = _hurd_umask; return 0; case INIT_SIGMASK: { struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread); __spin_lock (&ss->lock); *value = ss->blocked; __spin_unlock (&ss->lock); return 0; } case INIT_SIGPENDING: { struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread); __spin_lock (&ss->lock); *value = ss->pending; __spin_unlock (&ss->lock); return 0; } case INIT_SIGIGN: { struct hurd_sigstate *ss = _hurd_thread_sigstate (_hurd_sigthread); sigset_t ign; int sig; __spin_lock (&ss->lock); __sigemptyset (&ign); for (sig = 1; sig < NSIG; ++sig) if (ss->actions[sig].sa_handler == SIG_IGN) __sigaddset (&ign, sig); __spin_unlock (&ss->lock); *value = ign; return 0; } default: return EINVAL; } }
error_t hurd_thread_cancel (thread_t thread) { struct hurd_sigstate *ss = _hurd_thread_sigstate (thread); struct machine_thread_all_state state; int state_change; error_t err; if (! ss) return EINVAL; if (ss == _hurd_self_sigstate ()) { /* We are cancelling ourselves, so it is easy to succeed quickly. Since this function is not a cancellation point, we just leave the flag set pending the next cancellation point (hurd_check_cancel or RPC) and return success. */ ss->cancel = 1; return 0; } assert (! __spin_lock_locked (&ss->critical_section_lock)); __spin_lock (&ss->critical_section_lock); __spin_lock (&ss->lock); err = __thread_suspend (thread); __spin_unlock (&ss->lock); if (! err) { /* Set the flag telling the thread its operation is being cancelled. */ ss->cancel = 1; /* Interrupt any interruptible RPC now in progress. */ state.set = 0; _hurdsig_abort_rpcs (ss, 0, 0, &state, &state_change, NULL, 0, 0); if (state_change) err = __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR, (natural_t *) &state.basic, MACHINE_THREAD_STATE_COUNT); if (ss->cancel_hook) /* The code being cancelled has a special wakeup function. Calling this should make the thread wake up and check the cancellation flag. */ (*ss->cancel_hook) (); __thread_resume (thread); } _hurd_critical_section_unlock (ss); return err; }
error_t __pthread_sigstate (struct __pthread *thread, int how, const sigset_t *set, sigset_t *oset, int clear_pending) { error_t err = 0; struct hurd_sigstate *ss; ss = _hurd_thread_sigstate (thread->kernel_thread); assert (ss); __spin_lock (&ss->lock); if (oset != NULL) *oset = ss->blocked; if (set != NULL) { switch (how) { case SIG_BLOCK: ss->blocked |= *set; break; case SIG_SETMASK: ss->blocked = *set; break; case SIG_UNBLOCK: ss->blocked &= ~*set; break; default: err = EINVAL; break; } ss->blocked &= ~_SIG_CANT_MASK; } if (!err && clear_pending) __sigemptyset (&ss->pending); __spin_unlock (&ss->lock); return err; }
kern_return_t _S_catch_exception_raise (mach_port_t port, thread_t thread, task_t task, int exception, int code, int subcode) { int signo, sigcode, error; struct hurd_sigstate *ss; if (task != __mach_task_self ()) /* The sender wasn't the kernel. */ return EPERM; /* Call the machine-dependent function to translate the Mach exception codes into a signal number and subcode. */ _hurd_exception2signal (exception, code, subcode, &signo, &sigcode, &error); /* Find the sigstate structure for the faulting thread. */ __mutex_lock (&_hurd_siglock); for (ss = _hurd_sigstates; ss != NULL; ss = ss->next) if (ss->thread == thread) break; __mutex_unlock (&_hurd_siglock); if (ss == NULL) ss = _hurd_thread_sigstate (thread); /* Allocate a fresh one. */ if (__spin_lock_locked (&ss->lock.held)) /* Oops. The thread faulted with its sigstate lock held. Bad scene. What to do? */ ; /* XXX */ else __mutex_lock (&ss->lock); /* Post the signal. */ _hurd_internal_post_signal (ss, signo, sigcode, error, MACH_PORT_NULL, MACH_MSG_TYPE_PORT_SEND); return KERN_SUCCESS; }
kern_return_t _S_catch_exception_raise (mach_port_t port, thread_t thread, task_t task, #ifdef EXC_MASK_ALL /* New interface flavor. */ exception_type_t exception, exception_data_t code, mach_msg_type_number_t codeCnt #else /* Vanilla Mach 3.0 interface. */ integer_t exception, integer_t code, integer_t subcode #endif ) { struct hurd_sigstate *ss; int signo; struct hurd_signal_detail d; if (task != __mach_task_self ()) /* The sender wasn't the kernel. */ return EPERM; d.exc = exception; #ifdef EXC_MASK_ALL assert (codeCnt >= 2); d.exc_code = code[0]; d.exc_subcode = code[1]; #else d.exc_code = code; d.exc_subcode = subcode; #endif /* Call the machine-dependent function to translate the Mach exception codes into a signal number and subcode. */ _hurd_exception2signal (&d, &signo); /* Find the sigstate structure for the faulting thread. */ __mutex_lock (&_hurd_siglock); for (ss = _hurd_sigstates; ss != NULL; ss = ss->next) if (ss->thread == thread) break; __mutex_unlock (&_hurd_siglock); if (ss == NULL) ss = _hurd_thread_sigstate (thread); /* Allocate a fresh one. */ if (__spin_lock_locked (&ss->lock)) { /* Loser. The thread faulted with its sigstate lock held. Its sigstate data is now suspect. So we reset the parts of it which could cause trouble for the signal thread. Anything else clobbered therein will just hose this user thread, but it's faulting already. This is almost certainly a library bug: unless random memory clobberation caused the sigstate lock to gratuitously appear held, no code should do anything that can fault while holding the sigstate lock. */ __spin_unlock (&ss->critical_section_lock); ss->context = NULL; __spin_unlock (&ss->lock); } /* Post the signal. */ _hurd_internal_post_signal (ss, signo, &d, MACH_PORT_NULL, MACH_MSG_TYPE_PORT_SEND, 0); return KERN_SUCCESS; }