#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <pthread.h>
#include <pthread_np.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
static int sigcounts[NSIG + 1];
static int sigfifo[NSIG + 1];
static int fifo_depth = 0;
static sigset_t suspender_mask;
static pthread_t suspender_tid;
static void *
sigsuspender (void *arg)
{
int status, i;
sigset_t run_mask;
sigfillset (&run_mask);
sigprocmask (SIG_SETMASK, &run_mask, NULL);
sigfillset (&suspender_mask);
sigdelset (&suspender_mask, SIGINT);
sigdelset (&suspender_mask, SIGHUP);
sigdelset (&suspender_mask, SIGQUIT);
sigdelset (&suspender_mask, SIGURG);
sigdelset (&suspender_mask, SIGIO);
sigdelset (&suspender_mask, SIGUSR2);
while (sigcounts[SIGINT] == 0) {
status = sigsuspend (&suspender_mask);
if ((status == 0) || (errno != EINTR)) {
fprintf (stderr, "Unable to suspend for signals, "
"errno %d, return value %d\n",
errno, status);
exit (1);
}
for (i = 0; i < fifo_depth; i++)
fprintf (stderr, "Sigsuspend woke up by signal %d\n",
sigfifo[i]);
fifo_depth = 0;
}
pthread_exit (arg);
return (NULL);
}
static void
sighandler (int signo)
{
sigset_t set, suspend_set;
pthread_t self;
if ((signo >= 0) && (signo <= NSIG))
sigcounts[signo]++;
self = pthread_self ();
if (self == suspender_tid) {
sigfifo[fifo_depth] = signo;
fifo_depth++;
fprintf (stderr,
" -> Suspender thread signal handler caught signal %d\n",
signo);
sigprocmask (SIG_SETMASK, NULL, &set);
suspend_set = suspender_mask;
sigaddset(&suspend_set, signo);
if (memcmp(&set, &suspend_set, sizeof(set)))
fprintf (stderr,
" >>> FAIL: sigsuspender signal handler running "
"with incorrect mask.\n");
}
else
fprintf (stderr,
" -> Main thread signal handler caught signal %d\n",
signo);
}
static void
send_thread_signal (pthread_t tid, int signo)
{
if (pthread_kill (tid, signo) != 0) {
fprintf (stderr, "Unable to send thread signal, errno %d.\n",
errno);
exit (1);
}
}
static void
send_process_signal (int signo)
{
if (kill (getpid (), signo) != 0) {
fprintf (stderr, "Unable to send process signal, errno %d.\n",
errno);
exit (1);
}
}
int main (int argc, char *argv[])
{
pthread_attr_t pattr;
void * exit_status;
struct sigaction act;
sigset_t oldset;
sigset_t newset;
memset ((void *) sigcounts, 0, NSIG * sizeof (int));
sigemptyset (&act.sa_mask);
sigaddset (&act.sa_mask, SIGIO);
act.sa_handler = SIG_IGN;
act.sa_flags = 0;
sigaction (SIGIO, &act, NULL);
sigemptyset (&act.sa_mask);
sigaddset (&act.sa_mask, SIGURG);
act.sa_handler = sighandler;
act.sa_flags = SA_RESTART;
sigaction (SIGURG, &act, NULL);
sigemptyset (&act.sa_mask);
sigaddset (&act.sa_mask, SIGXCPU);
sigaction (SIGXCPU, &act, NULL);
sigprocmask (SIG_SETMASK, NULL, &oldset);
newset = oldset;
sigaddset (&newset, SIGUSR1);
sigaddset (&newset, SIGUSR2);
sigprocmask (SIG_SETMASK, &newset, NULL);
sigemptyset (&act.sa_mask);
sigaddset (&act.sa_mask, SIGUSR1);
sigaction (SIGUSR1, &act, NULL);
sigemptyset (&act.sa_mask);
sigaddset (&act.sa_mask, SIGUSR2);
sigaction (SIGUSR2, &act, NULL);
if ((pthread_attr_init (&pattr) != 0) ||
(pthread_attr_setdetachstate (&pattr,
PTHREAD_CREATE_JOINABLE) != 0)) {
fprintf (stderr, "Unable to initialize thread attributes.\n");
exit (1);
}
if (pthread_create (&suspender_tid, &pattr, sigsuspender, NULL) != 0) {
fprintf (stderr, "Unable to create thread, errno %d.\n", errno);
exit (1);
}
pthread_set_name_np (suspender_tid, "sigsuspender");
send_thread_signal (suspender_tid, SIGIO);
sleep (1);
send_process_signal (SIGIO);
sleep (1);
if (sigcounts[SIGIO] != 0)
fprintf (stderr, "FAIL: sigsuspend wakes up for ignored signal "
"SIGIO.\n");
send_thread_signal (suspender_tid, SIGURG);
sleep (1);
send_process_signal (SIGURG);
sleep (1);
if (sigcounts[SIGURG] != 2)
fprintf (stderr,
"FAIL: sigsuspend doesn't wake up for SIGURG.\n");
send_thread_signal (suspender_tid, SIGUSR2);
sleep (1);
send_process_signal (SIGUSR2);
sleep (1);
if (sigcounts[SIGUSR2] != 2)
fprintf (stderr,
"FAIL: sigsuspend doesn't wake up for SIGUSR2.\n");
send_thread_signal (suspender_tid, SIGUSR1);
sleep (1);
send_process_signal (SIGUSR1);
sleep (1);
if (sigcounts[SIGUSR1] != 0)
fprintf (stderr, "FAIL: signal hander called for SIGUSR1.\n");
send_process_signal (SIGPIPE);
fprintf (stderr, "FAIL: SIGPIPE did not terminate process.\n");
pthread_join (suspender_tid, &exit_status);
return (0);
}