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_kill 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_kill() and checks that EINTR is never returned. */ 167 void *test(void *arg) 168 { 169 int ret = 0; 170 171 /* We don't block the signals SIGUSR1 and SIGUSR2 for this THREAD */ 172 ret = pthread_sigmask(SIG_UNBLOCK, &usersigs, NULL); 173 174 if (ret != 0) { 175 UNRESOLVED(ret, 176 "Unable to unblock SIGUSR1 and SIGUSR2 in worker thread"); 177 } 178 179 while (do_it) { 180 count_ope++; 181 182 ret = pthread_kill(pthread_self(), 0); 183 184 if (ret == EINTR) { 185 FAILED("pthread_kill returned EINTR"); 186 } 187 188 if (ret != 0) { 189 UNRESOLVED(ret, 190 "pthread_kill returned an unexpected error"); 191 } 192 193 ret = pthread_kill(pthread_self(), SIGUSR1); 194 195 if (ret == EINTR) { 196 FAILED("pthread_kill returned EINTR"); 197 } 198 199 if (ret != 0) { 200 UNRESOLVED(ret, 201 "pthread_kill returned an unexpected error"); 202 } 203 } 204 205 return NULL; 206 } 207 208 /* Main function */ 209 int main(void) 210 { 211 int ret; 212 pthread_t th_work, th_sig1, th_sig2; 213 thestruct arg1, arg2; 214 215 struct sigaction sa; 216 217 /* Initialize output routine */ 218 output_init(); 219 220 /* We need to register the signal handlers for the PROCESS */ 221 sigemptyset(&sa.sa_mask); 222 sa.sa_flags = 0; 223 sa.sa_handler = sighdl1; 224 225 if ((ret = sigaction(SIGUSR1, &sa, NULL))) { 226 UNRESOLVED(ret, "Unable to register signal handler1"); 227 } 228 229 sa.sa_handler = sighdl2; 230 231 if ((ret = sigaction(SIGUSR2, &sa, NULL))) { 232 UNRESOLVED(ret, "Unable to register signal handler2"); 233 } 234 235 /* We prepare a signal set which includes SIGUSR1 and SIGUSR2 */ 236 sigemptyset(&usersigs); 237 238 ret = sigaddset(&usersigs, SIGUSR1); 239 240 ret |= sigaddset(&usersigs, SIGUSR2); 241 242 if (ret != 0) { 243 UNRESOLVED(ret, "Unable to add SIGUSR1 or 2 to a signal set"); 244 } 245 246 /* We now block the signals SIGUSR1 and SIGUSR2 for this THREAD */ 247 ret = pthread_sigmask(SIG_BLOCK, &usersigs, NULL); 248 249 if (ret != 0) { 250 UNRESOLVED(ret, 251 "Unable to block SIGUSR1 and SIGUSR2 in main thread"); 252 } 253 #ifdef WITH_SYNCHRO 254 if (sem_init(&semsig1, 0, 1)) { 255 UNRESOLVED(errno, "Semsig1 init"); 256 } 257 258 if (sem_init(&semsig2, 0, 1)) { 259 UNRESOLVED(errno, "Semsig2 init"); 260 } 261 #endif 262 263 if ((ret = pthread_create(&th_work, NULL, test, NULL))) { 264 UNRESOLVED(ret, "Worker thread creation failed"); 265 } 266 267 arg1.sig = SIGUSR1; 268 arg2.sig = SIGUSR2; 269 #ifdef WITH_SYNCHRO 270 arg1.sem = &semsig1; 271 arg2.sem = &semsig2; 272 #endif 273 274 if ((ret = pthread_create(&th_sig1, NULL, sendsig, (void *)&arg1))) { 275 UNRESOLVED(ret, "Signal 1 sender thread creation failed"); 276 } 277 278 if ((ret = pthread_create(&th_sig2, NULL, sendsig, (void *)&arg2))) { 279 UNRESOLVED(ret, "Signal 2 sender thread creation failed"); 280 } 281 282 /* Let's wait for a while now */ 283 sleep(1); 284 285 /* Now stop the threads and join them */ 286 do { 287 do_it = 0; 288 } 289 while (do_it); 290 291 if ((ret = pthread_join(th_sig1, NULL))) { 292 UNRESOLVED(ret, "Signal 1 sender thread join failed"); 293 } 294 295 if ((ret = pthread_join(th_sig2, NULL))) { 296 UNRESOLVED(ret, "Signal 2 sender thread join failed"); 297 } 298 299 if ((ret = pthread_join(th_work, NULL))) { 300 UNRESOLVED(ret, "Worker thread join failed"); 301 } 302 #if VERBOSE > 0 303 output("Test executed successfully.\n"); 304 305 output(" %d operations.\n", count_ope); 306 307 #ifdef WITH_SYNCHRO 308 output(" %d signals were sent meanwhile.\n", count_sig); 309 310 #endif 311 #endif 312 PASSED; 313 } 314