Home | History | Annotate | Download | only in pthread_sigmask
      1 /*
      2  * Copyright (c) 2003, Intel Corporation. All rights reserved.
      3  * Created by:  salwan.searty REMOVE-THIS AT intel DOT com
      4  * This file is licensed under the GPL license.  For the full content
      5  * of this license, see the COPYING file at the top level of this
      6  * source tree.
      7 
      8  Steps:
      9  1. Set the signal mask to only having SIGABRT.
     10  2. Call pthread_sigmask again, this time with a randomly generated
     11  value of  how that is checked to make sure it does not equal any of the three defined
     12  values of how which are SIG_SETMASK, SIG_BLOCK, or SIG_UNBLOCK. This should
     13  cause pthread_sigmask() to return -1. For the second parameter in the
     14  pthread_sigmask() function, use a set which contains SIGABRT and SIGALRM.
     15  3. Now verify using the is_changed() function that the only signal that is still
     16  in the signal mask is SIGABRT. Neither SIGALRM nor any other signal should be
     17  in the signal mask of the process.
     18 
     19 */
     20 
     21 #include <pthread.h>
     22 #include <errno.h>
     23 #include <signal.h>
     24 #include <stdio.h>
     25 #include <stdlib.h>
     26 #include "posixtest.h"
     27 
     28 #define NUMSIGNALS (sizeof(siglist) / sizeof(siglist[0]))
     29 
     30 int is_changed(sigset_t set, int sig)
     31 {
     32 
     33 	int i;
     34 	int siglist[] = { SIGABRT, SIGALRM, SIGBUS, SIGCHLD,
     35 		SIGCONT, SIGFPE, SIGHUP, SIGILL, SIGINT,
     36 		SIGPIPE, SIGQUIT, SIGSEGV,
     37 		SIGTERM, SIGTSTP, SIGTTIN, SIGTTOU,
     38 		SIGUSR1, SIGUSR2,
     39 #ifdef SIGPOLL
     40 		SIGPOLL,
     41 #endif
     42 #ifdef SIGPROF
     43 		SIGPROF,
     44 #endif
     45 		SIGSYS,
     46 		SIGTRAP, SIGURG, SIGVTALRM, SIGXCPU, SIGXFSZ
     47 	};
     48 
     49 	if (sigismember(&set, sig) != 1) {
     50 		return 1;
     51 	}
     52 	for (i = 0; i < NUMSIGNALS; i++) {
     53 		if ((siglist[i] != sig)) {
     54 			if (sigismember(&set, siglist[i]) != 0) {
     55 				return 1;
     56 			}
     57 		}
     58 	}
     59 	return 0;
     60 }
     61 
     62 int get_rand()
     63 {
     64 
     65 	int r;
     66 	r = rand();
     67 	if ((r == SIG_BLOCK) || (r == SIG_SETMASK) || (r == SIG_UNBLOCK)) {
     68 		r = get_rand();
     69 	}
     70 	return r;
     71 }
     72 
     73 void *a_thread_func()
     74 {
     75 
     76 	int r = get_rand();
     77 	sigset_t actl, oactl;
     78 /*
     79 	printf("SIG_SETMASK=%d\n", SIG_SETMASK);
     80 	printf("SIG_BLOCK=%d\n", SIG_BLOCK);
     81 	printf("SIG_UNBLOCK=%d\n", SIG_UNBLOCK);
     82 	printf("r=%d\n", r);
     83 */
     84 	sigemptyset(&actl);
     85 	sigemptyset(&oactl);
     86 	sigaddset(&actl, SIGABRT);
     87 
     88 	pthread_sigmask(SIG_SETMASK, &actl, NULL);
     89 
     90 	sigaddset(&actl, SIGALRM);
     91 	if (pthread_sigmask(r, &actl, NULL) != EINVAL) {
     92 		perror
     93 		    ("pthread_sigmask() did not fail even though invalid how parameter was passed to it.\n");
     94 		pthread_exit((void *)1);
     95 	}
     96 
     97 	pthread_sigmask(SIG_SETMASK, NULL, &oactl);
     98 
     99 	if (is_changed(oactl, SIGABRT)) {
    100 		printf("FAIL: signal mask was changed. \n");
    101 		pthread_exit((void *)-1);
    102 	}
    103 
    104 /*
    105 	printf("sigismember(SIGABRT)=%d\n", sigismember(&oactl, SIGABRT));
    106 	printf("sigismember(SIGALRM)=%d\n", sigismember(&oactl, SIGALRM));
    107 */
    108 
    109 	printf("PASS: signal mask was not changed.\n");
    110 	pthread_exit(NULL);
    111 
    112 	/* To please some compilers */
    113 	return NULL;
    114 }
    115 
    116 int main(void)
    117 {
    118 
    119 	int *thread_return_value;
    120 
    121 	pthread_t new_thread;
    122 
    123 	if (pthread_create(&new_thread, NULL, a_thread_func, NULL) != 0) {
    124 		perror("Error creating new thread\n");
    125 		return PTS_UNRESOLVED;
    126 	}
    127 
    128 	if (pthread_join(new_thread, (void *)&thread_return_value) != 0) {
    129 		perror("Error in pthread_join()\n");
    130 		return PTS_UNRESOLVED;
    131 	}
    132 
    133 	if ((long)thread_return_value != 0) {
    134 		if ((long)thread_return_value == 1) {
    135 			printf("Test UNRESOLVED\n");
    136 			return PTS_UNRESOLVED;
    137 		} else if ((long)thread_return_value == -1) {
    138 			printf("Test FAILED\n");
    139 			return PTS_FAIL;
    140 		} else {
    141 			printf("Test UNRESOLVED\n");
    142 			return PTS_UNRESOLVED;
    143 		}
    144 	}
    145 	return PTS_PASS;
    146 }
    147