Home | History | Annotate | Download | only in pthread_sigmask
      1 /*
      2 * Copyright (c) 2004, Bull S.A..  All rights reserved.
      3 * Created by: Sebastien Decugis
      4 
      5 * This program is free software; you can redistribute it and/or modify it
      6 * under the terms of version 2 of the GNU General Public License as
      7 * published by the Free Software Foundation.
      8 *
      9 * This program is distributed in the hope that it would be useful, but
     10 * WITHOUT ANY WARRANTY; without even the implied warranty of
     11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     12 *
     13 * You should have received a copy of the GNU General Public License along
     14 * with this program; if not, write the Free Software Foundation, Inc.,
     15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     16 
     17 * This sample test aims to check the following assertion:
     18 *
     19 * The function does not return EINTR
     20 
     21 * The steps are:
     22 * -> kill a thread which calls pthread_sigmask
     23 * -> check that EINTR is never returned
     24 
     25 */
     26 
     27 /* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
     28 #define _POSIX_C_SOURCE 200112L
     29 
     30 /********************************************************************************************/
     31 /****************************** standard includes *****************************************/
     32 /********************************************************************************************/
     33 #include <pthread.h>
     34 #include <stdarg.h>
     35 #include <stdio.h>
     36 #include <stdlib.h>
     37 #include <string.h>
     38 #include <unistd.h>
     39 
     40 #include <semaphore.h>
     41 #include <errno.h>
     42 #include <signal.h>
     43 
     44 /********************************************************************************************/
     45 /******************************   Test framework   *****************************************/
     46 /********************************************************************************************/
     47 #include "../testfrmw/testfrmw.h"
     48 #include "../testfrmw/testfrmw.c"
     49 /* This header is responsible for defining the following macros:
     50  * UNRESOLVED(ret, descr);
     51  *    where descr is a description of the error and ret is an int (error code for example)
     52  * FAILED(descr);
     53  *    where descr is a short text saying why the test has failed.
     54  * PASSED();
     55  *    No parameter.
     56  *
     57  * Both three macros shall terminate the calling process.
     58  * The testcase shall not terminate in any other maneer.
     59  *
     60  * The other file defines the functions
     61  * void output_init()
     62  * void output(char * string, ...)
     63  *
     64  * Those may be used to output information.
     65  */
     66 
     67 /********************************************************************************************/
     68 /********************************** Configuration ******************************************/
     69 /********************************************************************************************/
     70 #ifndef VERBOSE
     71 #define VERBOSE 1
     72 #endif
     73 
     74 #define WITH_SYNCHRO
     75 
     76 /********************************************************************************************/
     77 /***********************************    Test cases  *****************************************/
     78 /********************************************************************************************/
     79 
     80 char do_it = 1;
     81 unsigned long count_ope = 0;
     82 #ifdef WITH_SYNCHRO
     83 sem_t semsig1;
     84 sem_t semsig2;
     85 unsigned long count_sig = 0;
     86 #endif
     87 
     88 sigset_t usersigs;
     89 
     90 typedef struct {
     91 	int sig;
     92 #ifdef WITH_SYNCHRO
     93 	sem_t *sem;
     94 #endif
     95 } thestruct;
     96 
     97 /* the following function keeps on sending the signal to the process */
     98 void *sendsig(void *arg)
     99 {
    100 	thestruct *thearg = (thestruct *) arg;
    101 	int ret;
    102 	pid_t process;
    103 
    104 	process = getpid();
    105 
    106 	/* We block the signals SIGUSR1 and SIGUSR2 for this THREAD */
    107 	ret = pthread_sigmask(SIG_BLOCK, &usersigs, NULL);
    108 
    109 	if (ret != 0) {
    110 		UNRESOLVED(ret,
    111 			   "Unable to block SIGUSR1 and SIGUSR2 in signal thread");
    112 	}
    113 
    114 	while (do_it) {
    115 #ifdef WITH_SYNCHRO
    116 
    117 		if ((ret = sem_wait(thearg->sem))) {
    118 			UNRESOLVED(errno, "Sem_wait in sendsig");
    119 		}
    120 
    121 		count_sig++;
    122 #endif
    123 
    124 		ret = kill(process, thearg->sig);
    125 
    126 		if (ret != 0) {
    127 			UNRESOLVED(errno, "Kill in sendsig");
    128 		}
    129 
    130 	}
    131 
    132 	return NULL;
    133 }
    134 
    135 /* Next are the signal handlers. */
    136 /* This one is registered for signal SIGUSR1 */
    137 void sighdl1(int sig)
    138 {
    139 #ifdef WITH_SYNCHRO
    140 
    141 	if (sem_post(&semsig1)) {
    142 		UNRESOLVED(errno, "Sem_post in signal handler 1");
    143 	}
    144 #endif
    145 }
    146 
    147 /* This one is registered for signal SIGUSR2 */
    148 void sighdl2(int sig)
    149 {
    150 #ifdef WITH_SYNCHRO
    151 
    152 	if (sem_post(&semsig2)) {
    153 		UNRESOLVED(errno, "Sem_post in signal handler 2");
    154 	}
    155 #endif
    156 }
    157 
    158 int init_ctl;
    159 /* Init function */
    160 void initializer(void)
    161 {
    162 	init_ctl++;
    163 	return;
    164 }
    165 
    166 /* Test function -- calls pthread_sigmask() and checks that EINTR is never returned. */
    167 void *test(void *arg)
    168 {
    169 	int ret = 0;
    170 	sigset_t set;
    171 	int i, j = 0;
    172 	int signals[] = { SIGBUS, SIGKILL, SIGABRT, SIGCHLD, SIGHUP };
    173 #define NSIG (sizeof(signals)/sizeof(int))
    174 	int operation[] = { SIG_SETMASK, SIG_BLOCK, SIG_UNBLOCK };
    175 
    176 	ret = sigemptyset(&set);
    177 
    178 	if (ret != 0) {
    179 		UNRESOLVED(ret, "Failed to initialize signal set");
    180 	}
    181 
    182 	/* We don't block the signals SIGUSR1 and SIGUSR2 for this THREAD */
    183 	ret = pthread_sigmask(SIG_UNBLOCK, &usersigs, NULL);
    184 
    185 	if (ret != 0) {
    186 		UNRESOLVED(ret,
    187 			   "Unable to unblock SIGUSR1 and SIGUSR2 in worker thread");
    188 	}
    189 
    190 	while (do_it) {
    191 		count_ope++;
    192 
    193 		for (i = 0; i < 3; i++) {
    194 			j++;
    195 			j %= 2 * NSIG;
    196 
    197 			if (j >= NSIG)
    198 				ret = sigdelset(&set, signals[j - NSIG]);
    199 			else
    200 				ret = sigaddset(&set, signals[j]);
    201 
    202 			if (ret != 0) {
    203 				UNRESOLVED(errno,
    204 					   "Failed to initialize signal set");
    205 			}
    206 
    207 			ret = pthread_sigmask(operation[i], &set, NULL);
    208 
    209 			if (ret == EINTR) {
    210 				FAILED("pthread_sigmask returned EINTR");
    211 			}
    212 
    213 			if (ret != 0) {
    214 				UNRESOLVED(ret,
    215 					   "Failed to initialize signal set");
    216 			}
    217 		}
    218 	}
    219 
    220 	return NULL;
    221 }
    222 
    223 /* Main function */
    224 int main(void)
    225 {
    226 	int ret;
    227 	pthread_t th_work, th_sig1, th_sig2;
    228 	thestruct arg1, arg2;
    229 
    230 	struct sigaction sa;
    231 
    232 	/* Initialize output routine */
    233 	output_init();
    234 
    235 	/* We need to register the signal handlers for the PROCESS */
    236 	sigemptyset(&sa.sa_mask);
    237 	sa.sa_flags = 0;
    238 	sa.sa_handler = sighdl1;
    239 
    240 	if ((ret = sigaction(SIGUSR1, &sa, NULL))) {
    241 		UNRESOLVED(ret, "Unable to register signal handler1");
    242 	}
    243 
    244 	sa.sa_handler = sighdl2;
    245 
    246 	if ((ret = sigaction(SIGUSR2, &sa, NULL))) {
    247 		UNRESOLVED(ret, "Unable to register signal handler2");
    248 	}
    249 
    250 	/* We prepare a signal set which includes SIGUSR1 and SIGUSR2 */
    251 	sigemptyset(&usersigs);
    252 
    253 	ret = sigaddset(&usersigs, SIGUSR1);
    254 
    255 	ret |= sigaddset(&usersigs, SIGUSR2);
    256 
    257 	if (ret != 0) {
    258 		UNRESOLVED(ret, "Unable to add SIGUSR1 or 2 to a signal set");
    259 	}
    260 
    261 	/* We now block the signals SIGUSR1 and SIGUSR2 for this THREAD */
    262 	ret = pthread_sigmask(SIG_BLOCK, &usersigs, NULL);
    263 
    264 	if (ret != 0) {
    265 		UNRESOLVED(ret,
    266 			   "Unable to block SIGUSR1 and SIGUSR2 in main thread");
    267 	}
    268 #ifdef WITH_SYNCHRO
    269 	if (sem_init(&semsig1, 0, 1)) {
    270 		UNRESOLVED(errno, "Semsig1  init");
    271 	}
    272 
    273 	if (sem_init(&semsig2, 0, 1)) {
    274 		UNRESOLVED(errno, "Semsig2  init");
    275 	}
    276 #endif
    277 
    278 	if ((ret = pthread_create(&th_work, NULL, test, NULL))) {
    279 		UNRESOLVED(ret, "Worker thread creation failed");
    280 	}
    281 
    282 	arg1.sig = SIGUSR1;
    283 	arg2.sig = SIGUSR2;
    284 #ifdef WITH_SYNCHRO
    285 	arg1.sem = &semsig1;
    286 	arg2.sem = &semsig2;
    287 #endif
    288 
    289 	if ((ret = pthread_create(&th_sig1, NULL, sendsig, (void *)&arg1))) {
    290 		UNRESOLVED(ret, "Signal 1 sender thread creation failed");
    291 	}
    292 
    293 	if ((ret = pthread_create(&th_sig2, NULL, sendsig, (void *)&arg2))) {
    294 		UNRESOLVED(ret, "Signal 2 sender thread creation failed");
    295 	}
    296 
    297 	/* Let's wait for a while now */
    298 	sleep(1);
    299 
    300 	/* Now stop the threads and join them */
    301 	do {
    302 		do_it = 0;
    303 	}
    304 	while (do_it);
    305 
    306 	if ((ret = pthread_join(th_sig1, NULL))) {
    307 		UNRESOLVED(ret, "Signal 1 sender thread join failed");
    308 	}
    309 
    310 	if ((ret = pthread_join(th_sig2, NULL))) {
    311 		UNRESOLVED(ret, "Signal 2 sender thread join failed");
    312 	}
    313 
    314 	if ((ret = pthread_join(th_work, NULL))) {
    315 		UNRESOLVED(ret, "Worker thread join failed");
    316 	}
    317 #if VERBOSE > 0
    318 	output("Test executed successfully.\n");
    319 
    320 	output("  %d operations.\n", count_ope);
    321 
    322 #ifdef WITH_SYNCHRO
    323 	output("  %d signals were sent meanwhile.\n", count_sig);
    324 
    325 #endif
    326 #endif
    327 	PASSED;
    328 }
    329