static int do_sigwait (const sigset_t *set, int *sig) { sigset_t tmp_mask; struct sigaction saved[NSIG]; struct sigaction action; int save_errno; int this; /* Prepare set. */ __sigfillset (&tmp_mask); /* Unblock all signals in the SET and register our nice handler. */ action.sa_handler = ignore_signal; action.sa_flags = 0; __sigfillset (&action.sa_mask); /* Block all signals for handler. */ /* Make sure we recognize error conditions by setting WAS_SIG to a value which does not describe a legal signal number. */ was_sig = -1; for (this = 1; this < NSIG; ++this) if (__sigismember (set, this)) { /* Unblock this signal. */ __sigdelset (&tmp_mask, this); /* Register temporary action handler. */ if (__sigaction (this, &action, &saved[this]) != 0) goto restore_handler; } /* Now we can wait for signals. */ __sigsuspend (&tmp_mask); restore_handler: save_errno = errno; while (--this >= 1) if (__sigismember (set, this)) /* We ignore errors here since we must restore all handlers. */ __sigaction (this, &saved[this], NULL); __set_errno (save_errno); /* Store the result and return. */ *sig = was_sig; return was_sig == -1 ? -1 : 0; }
/* Set the mask of blocked signals to MASK, wait for a signal to arrive, and then restore the mask. */ static int do_sigpause (int sig_or_mask, int is_sig) { sigset_t set; if (is_sig != 0) { /* The modern X/Open implementation is requested. */ if (__sigprocmask (0, NULL, &set) < 0 || sigdelset (&set, sig_or_mask) < 0) return -1; } else if (sigset_set_old_mask (&set, sig_or_mask) < 0) return -1; /* Note the sigpause() is a cancellation point. But since we call sigsuspend() which itself is a cancellation point we do not have to do anything here. */ return __sigsuspend (&set); }