Home | History | Annotate | Download | only in pthread_atfork
      1 /*
      2 * Copyright (c) 2005, 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_atfork
     23 * -> check that EINTR is never returned
     24 
     25 */
     26 
     27 
     28 /******************************************************************************/
     29 /*********************** standard includes ************************************/
     30 /******************************************************************************/
     31 #include <pthread.h>
     32 #include <stdarg.h>
     33 #include <stdio.h>
     34 #include <stdlib.h>
     35 #include <string.h>
     36 #include <unistd.h>
     37 
     38 #include <sched.h>
     39 #include <semaphore.h>
     40 #include <errno.h>
     41 #include <signal.h>
     42 
     43 /******************************************************************************/
     44 /***********************   Test framework   ***********************************/
     45 /******************************************************************************/
     46 #include "../testfrmw/testfrmw.h"
     47 #include "../testfrmw/testfrmw.c"
     48 /* This header is responsible for defining the following macros:
     49  * UNRESOLVED(ret, descr);
     50  *    where descr is a description of the error and ret is an int
     51  *   (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 case   *********************************/
     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 void prepare(void)
    159 {
    160 	return;
    161 }
    162 
    163 void parent(void)
    164 {
    165 	return;
    166 }
    167 
    168 void child(void)
    169 {
    170 	return;
    171 }
    172 
    173 /* Test function -- calls pthread_setschedparam() and checks that EINTR is never returned. */
    174 void *test(void *arg)
    175 {
    176 	int ret = 0;
    177 
    178 	/* We don't block the signals SIGUSR1 and SIGUSR2 for this THREAD */
    179 	ret = pthread_sigmask(SIG_UNBLOCK, &usersigs, NULL);
    180 
    181 	if (ret != 0) {
    182 		UNRESOLVED(ret,
    183 			   "Unable to unblock SIGUSR1 and SIGUSR2 in worker thread");
    184 	}
    185 
    186 	while (do_it) {
    187 		count_ope++;
    188 		ret = pthread_atfork(prepare, parent, child);
    189 
    190 		if (ret == EINTR) {
    191 			FAILED("EINTR was returned");
    192 		}
    193 
    194 	}
    195 
    196 	return NULL;
    197 }
    198 
    199 /* Main function */
    200 int main(void)
    201 {
    202 	int ret;
    203 	pthread_t th_work, th_sig1, th_sig2, me;
    204 	thestruct arg1, arg2;
    205 
    206 	struct sigaction sa;
    207 
    208 	/* Initialize output routine */
    209 	output_init();
    210 
    211 	/* We need to register the signal handlers for the PROCESS */
    212 	sigemptyset(&sa.sa_mask);
    213 	sa.sa_flags = 0;
    214 	sa.sa_handler = sighdl1;
    215 
    216 	if ((ret = sigaction(SIGUSR1, &sa, NULL))) {
    217 		UNRESOLVED(ret, "Unable to register signal handler1");
    218 	}
    219 
    220 	sa.sa_handler = sighdl2;
    221 
    222 	if ((ret = sigaction(SIGUSR2, &sa, NULL))) {
    223 		UNRESOLVED(ret, "Unable to register signal handler2");
    224 	}
    225 
    226 	/* We prepare a signal set which includes SIGUSR1 and SIGUSR2 */
    227 	sigemptyset(&usersigs);
    228 
    229 	ret = sigaddset(&usersigs, SIGUSR1);
    230 
    231 	ret |= sigaddset(&usersigs, SIGUSR2);
    232 
    233 	if (ret != 0) {
    234 		UNRESOLVED(ret, "Unable to add SIGUSR1 or 2 to a signal set");
    235 	}
    236 
    237 	/* We now block the signals SIGUSR1 and SIGUSR2 for this THREAD */
    238 	ret = pthread_sigmask(SIG_BLOCK, &usersigs, NULL);
    239 
    240 	if (ret != 0) {
    241 		UNRESOLVED(ret,
    242 			   "Unable to block SIGUSR1 and SIGUSR2 in main thread");
    243 	}
    244 #ifdef WITH_SYNCHRO
    245 	if (sem_init(&semsig1, 0, 1)) {
    246 		UNRESOLVED(errno, "Semsig1  init");
    247 	}
    248 
    249 	if (sem_init(&semsig2, 0, 1)) {
    250 		UNRESOLVED(errno, "Semsig2  init");
    251 	}
    252 #endif
    253 
    254 	me = pthread_self();
    255 
    256 	if ((ret = pthread_create(&th_work, NULL, test, &me))) {
    257 		UNRESOLVED(ret, "Worker thread creation failed");
    258 	}
    259 
    260 	arg1.sig = SIGUSR1;
    261 	arg2.sig = SIGUSR2;
    262 #ifdef WITH_SYNCHRO
    263 	arg1.sem = &semsig1;
    264 	arg2.sem = &semsig2;
    265 #endif
    266 
    267 	if ((ret = pthread_create(&th_sig1, NULL, sendsig, (void *)&arg1))) {
    268 		UNRESOLVED(ret, "Signal 1 sender thread creation failed");
    269 	}
    270 
    271 	if ((ret = pthread_create(&th_sig2, NULL, sendsig, (void *)&arg2))) {
    272 		UNRESOLVED(ret, "Signal 2 sender thread creation failed");
    273 	}
    274 
    275 	/* Let's wait for a while now */
    276 	sleep(1);
    277 
    278 	/* Now stop the threads and join them */
    279 	do {
    280 		do_it = 0;
    281 	}
    282 	while (do_it);
    283 
    284 	if ((ret = pthread_join(th_sig1, NULL))) {
    285 		UNRESOLVED(ret, "Signal 1 sender thread join failed");
    286 	}
    287 
    288 	if ((ret = pthread_join(th_sig2, NULL))) {
    289 		UNRESOLVED(ret, "Signal 2 sender thread join failed");
    290 	}
    291 
    292 	if ((ret = pthread_join(th_work, NULL))) {
    293 		UNRESOLVED(ret, "Worker thread join failed");
    294 	}
    295 #if VERBOSE > 0
    296 	output("Test executed successfully.\n");
    297 
    298 	output("  %d operations.\n", count_ope);
    299 
    300 #ifdef WITH_SYNCHRO
    301 	output("  %d signals were sent meanwhile.\n", count_sig);
    302 
    303 #endif
    304 #endif
    305 	PASSED;
    306 }
    307