/*
 * sigwait.c.2   Tests the sigwait() service.  Create a single sigwaiter
 *               to wait for a SIGINT, SIGTERM, SIGHUP, or SIGQUIT 
 *               signals.
 */
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "utils.h"

#define THREADS ((int) 4)
extern int getpid( void );

void
waiter( void )
{
	sigset_t sigset;
	int caught = FAILURE;

	sigemptyset( &sigset );
	sigaddset( &sigset, SIGINT );
	sigaddset( &sigset, SIGTERM );
	sigaddset( &sigset, SIGHUP );
	sigaddset( &sigset, SIGQUIT );

	pthread_sigmask( SIG_SETMASK, &sigset, NULL );

	switch((caught = sigwait( sigset )))
	{
	  case SIGINT:
		print_str("Caught SIGINT");
		break;
	  case SIGTERM:
		print_str("Caught SIGTERM");
		break;
	  case SIGHUP:
		print_str("Caught SIGHUP");
		break;
	  case SIGQUIT:
		print_str("Caught SIGQUIT");
		break;
	  default:
                printf("Error: %d\n", caught );
		break;

	}

	pthread_exit( (void *) caught );
}

static pthread_t th;;

int 
main( int argc, char *argv[] )
{
	int exit_status, st;

	printf("pid %d: Blocked %d %d %d %d\n", 
		    getpid(), SIGINT, SIGQUIT, SIGHUP, SIGTERM );

	create_joinable( &th, (thread_proc_t) waiter, NULL );

	st = pthread_join( th, (void **) &exit_status );
	CHECK( st, "pthread_join()");

	return( EXIT_SUCCESS );
}
