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_once 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 <errno.h> 34 #include <pthread.h> 35 #include <semaphore.h> 36 #include <signal.h> 37 #include <stdarg.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <unistd.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 (error code for example) 51 * FAILED(descr); 52 * where descr is a short text saying why the test has failed. 53 * PASSED(); 54 * No parameter. 55 * 56 * Both three macros shall terminate the calling process. 57 * The testcase shall not terminate in any other maneer. 58 * 59 * The other file defines the functions 60 * void output_init() 61 * void output(char * string, ...) 62 * 63 * Those may be used to output information. 64 */ 65 66 /********************************************************************************************/ 67 /********************************** Configuration ******************************************/ 68 /********************************************************************************************/ 69 #ifndef VERBOSE 70 #define VERBOSE 1 71 #endif 72 73 #define WITH_SYNCHRO 74 75 /********************************************************************************************/ 76 /*********************************** Test cases *****************************************/ 77 /********************************************************************************************/ 78 79 char do_it = 1; 80 unsigned long count_ope = 0; 81 #ifdef WITH_SYNCHRO 82 sem_t semsig1; 83 sem_t semsig2; 84 unsigned long count_sig = 0; 85 #endif 86 87 sigset_t usersigs; 88 89 typedef struct { 90 int sig; 91 #ifdef WITH_SYNCHRO 92 sem_t *sem; 93 #endif 94 } thestruct; 95 96 /* the following function keeps on sending the signal to the process */ 97 void *sendsig(void *arg) 98 { 99 thestruct *thearg = (thestruct *) arg; 100 int ret; 101 pid_t process; 102 103 process = getpid(); 104 105 /* We block the signals SIGUSR1 and SIGUSR2 for this THREAD */ 106 ret = pthread_sigmask(SIG_BLOCK, &usersigs, NULL); 107 108 if (ret != 0) { 109 UNRESOLVED(ret, 110 "Unable to block SIGUSR1 and SIGUSR2 in signal thread"); 111 } 112 113 while (do_it) { 114 #ifdef WITH_SYNCHRO 115 116 if ((ret = sem_wait(thearg->sem))) { 117 UNRESOLVED(errno, "Sem_wait in sendsig"); 118 } 119 120 count_sig++; 121 #endif 122 123 ret = kill(process, thearg->sig); 124 125 if (ret != 0) { 126 UNRESOLVED(errno, "Kill in sendsig"); 127 } 128 129 } 130 131 return NULL; 132 } 133 134 /* Next are the signal handlers. */ 135 /* This one is registered for signal SIGUSR1 */ 136 void sighdl1(int sig) 137 { 138 #ifdef WITH_SYNCHRO 139 140 if (sem_post(&semsig1)) { 141 UNRESOLVED(errno, "Sem_post in signal handler 1"); 142 } 143 #endif 144 } 145 146 /* This one is registered for signal SIGUSR2 */ 147 void sighdl2(int sig) 148 { 149 #ifdef WITH_SYNCHRO 150 151 if (sem_post(&semsig2)) { 152 UNRESOLVED(errno, "Sem_post in signal handler 2"); 153 } 154 #endif 155 } 156 157 int init_ctl; 158 /* Init function */ 159 void initializer(void) 160 { 161 init_ctl++; 162 return; 163 } 164 165 /* Test function -- calls pthread_equal() and checks that EINTR is never returned. */ 166 void *test(void *arg) 167 { 168 int ret = 0; 169 170 /* We don't block the signals SIGUSR1 and SIGUSR2 for this THREAD */ 171 ret = pthread_sigmask(SIG_UNBLOCK, &usersigs, NULL); 172 173 if (ret != 0) { 174 UNRESOLVED(ret, 175 "Unable to unblock SIGUSR1 and SIGUSR2 in worker thread"); 176 } 177 178 while (do_it) { 179 180 pthread_once_t once_ctl = PTHREAD_ONCE_INIT; 181 182 count_ope++; 183 184 init_ctl = 0; 185 186 ret = pthread_once(&once_ctl, initializer); 187 188 if (ret == EINTR) { 189 FAILED("pthread_once returned EINTR"); 190 } 191 192 if (ret != 0) { 193 UNRESOLVED(ret, "pthread_once failed"); 194 } 195 196 ret = pthread_once(&once_ctl, initializer); 197 198 if (ret == EINTR) { 199 FAILED("pthread_once returned EINTR"); 200 } 201 202 if (ret != 0) { 203 UNRESOLVED(ret, "pthread_once failed"); 204 } 205 206 if (init_ctl != 1) { 207 output("init_ctl: %d\n", init_ctl); 208 FAILED("The initializer did not execute as expected"); 209 } 210 211 } 212 213 return NULL; 214 } 215 216 /* Main function */ 217 int main(void) 218 { 219 int ret; 220 pthread_t th_work, th_sig1, th_sig2; 221 thestruct arg1, arg2; 222 223 struct sigaction sa; 224 225 /* Initialize output routine */ 226 output_init(); 227 228 /* We need to register the signal handlers for the PROCESS */ 229 sigemptyset(&sa.sa_mask); 230 sa.sa_flags = 0; 231 sa.sa_handler = sighdl1; 232 233 if ((ret = sigaction(SIGUSR1, &sa, NULL)) == -1) { 234 UNRESOLVED(ret, "Unable to register signal handler1"); 235 } 236 237 sa.sa_handler = sighdl2; 238 239 if ((ret = sigaction(SIGUSR2, &sa, NULL)) == -1) { 240 UNRESOLVED(ret, "Unable to register signal handler2"); 241 } 242 243 /* We prepare a signal set which includes SIGUSR1 and SIGUSR2 */ 244 sigemptyset(&usersigs); 245 246 ret = sigaddset(&usersigs, SIGUSR1); 247 248 ret |= sigaddset(&usersigs, SIGUSR2); 249 250 if (ret != 0) { 251 UNRESOLVED(ret, "Unable to add SIGUSR1 or 2 to a signal set"); 252 } 253 254 /* We now block the signals SIGUSR1 and SIGUSR2 for this THREAD */ 255 ret = pthread_sigmask(SIG_BLOCK, &usersigs, NULL); 256 257 if (ret != 0) { 258 UNRESOLVED(ret, 259 "Unable to block SIGUSR1 and SIGUSR2 in main thread"); 260 } 261 #ifdef WITH_SYNCHRO 262 if (sem_init(&semsig1, 0, 1)) { 263 UNRESOLVED(errno, "Semsig1 init"); 264 } 265 266 if (sem_init(&semsig2, 0, 1)) { 267 UNRESOLVED(errno, "Semsig2 init"); 268 } 269 #endif 270 271 if ((ret = pthread_create(&th_work, NULL, test, NULL))) { 272 UNRESOLVED(ret, "Worker thread creation failed"); 273 } 274 275 arg1.sig = SIGUSR1; 276 arg2.sig = SIGUSR2; 277 #ifdef WITH_SYNCHRO 278 arg1.sem = &semsig1; 279 arg2.sem = &semsig2; 280 #endif 281 282 if ((ret = pthread_create(&th_sig1, NULL, sendsig, (void *)&arg1))) { 283 UNRESOLVED(ret, "Signal 1 sender thread creation failed"); 284 } 285 286 if ((ret = pthread_create(&th_sig2, NULL, sendsig, (void *)&arg2))) { 287 UNRESOLVED(ret, "Signal 2 sender thread creation failed"); 288 } 289 290 /* Let's wait for a while now */ 291 sleep(1); 292 293 /* Now stop the threads and join them */ 294 do { 295 do_it = 0; 296 } 297 while (do_it); 298 299 if ((ret = pthread_join(th_sig1, NULL))) { 300 UNRESOLVED(ret, "Signal 1 sender thread join failed"); 301 } 302 303 if ((ret = pthread_join(th_sig2, NULL))) { 304 UNRESOLVED(ret, "Signal 2 sender thread join failed"); 305 } 306 307 if ((ret = pthread_join(th_work, NULL))) { 308 UNRESOLVED(ret, "Worker thread join failed"); 309 } 310 #if VERBOSE > 0 311 output("Test executed successfully.\n"); 312 313 output(" %d initializations.\n", count_ope); 314 315 #ifdef WITH_SYNCHRO 316 output(" %d signals were sent meanwhile.\n", count_sig); 317 318 #endif 319 #endif 320 PASSED; 321 } 322