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_equal 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 volatile int 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 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 if ((ret = sem_wait(thearg->sem))) { 116 UNRESOLVED(errno, "Sem_wait in sendsig"); 117 } 118 count_sig++; 119 #endif 120 121 ret = kill(process, thearg->sig); 122 if (ret != 0) { 123 UNRESOLVED(errno, "Kill in sendsig"); 124 } 125 126 } 127 return NULL; 128 } 129 130 /* Next are the signal handlers. */ 131 /* This one is registered for signal SIGUSR1 */ 132 void sighdl1(int sig) 133 { 134 #ifdef WITH_SYNCHRO 135 if (sem_post(&semsig1)) { 136 UNRESOLVED(errno, "Sem_post in signal handler 1"); 137 } 138 #endif 139 } 140 141 /* This one is registered for signal SIGUSR2 */ 142 void sighdl2(int sig) 143 { 144 #ifdef WITH_SYNCHRO 145 if (sem_post(&semsig2)) { 146 UNRESOLVED(errno, "Sem_post in signal handler 2"); 147 } 148 #endif 149 } 150 151 /* Test function -- calls pthread_equal() and checks that EINTR is never returned. */ 152 void *test(void *arg) 153 { 154 int ret = 0; 155 156 pthread_t me = pthread_self(); 157 158 /* We don't block the signals SIGUSR1 and SIGUSR2 for this THREAD */ 159 ret = pthread_sigmask(SIG_UNBLOCK, &usersigs, NULL); 160 if (ret != 0) { 161 UNRESOLVED(ret, 162 "Unable to unblock SIGUSR1 and SIGUSR2 in worker thread"); 163 } 164 165 while (do_it) { 166 count_ope++; 167 168 ret = pthread_equal(me, *(pthread_t *) arg); 169 if (ret != 0) { 170 UNRESOLVED(ret, 171 "pthread_equal failed to return the correct value"); 172 } 173 174 ret = pthread_equal(me, me); 175 if (ret == 0) { 176 UNRESOLVED(ret, "pthread_equal returned a bad result"); 177 } 178 if (ret == EINTR) { 179 FAILED("pthread_equal returned EINTR status"); 180 } 181 } 182 return NULL; 183 } 184 185 /* Main function */ 186 int main(void) 187 { 188 int ret; 189 pthread_t th_work, th_sig1, th_sig2, me; 190 thestruct arg1, arg2; 191 struct sigaction sa; 192 193 /* Initialize output routine */ 194 output_init(); 195 196 /* We need to register the signal handlers for the PROCESS */ 197 sigemptyset(&sa.sa_mask); 198 sa.sa_flags = 0; 199 sa.sa_handler = sighdl1; 200 if ((ret = sigaction(SIGUSR1, &sa, NULL))) { 201 UNRESOLVED(ret, "Unable to register signal handler1"); 202 } 203 sa.sa_handler = sighdl2; 204 if ((ret = sigaction(SIGUSR2, &sa, NULL))) { 205 UNRESOLVED(ret, "Unable to register signal handler2"); 206 } 207 208 /* We prepare a signal set which includes SIGUSR1 and SIGUSR2 */ 209 sigemptyset(&usersigs); 210 ret = sigaddset(&usersigs, SIGUSR1); 211 ret |= sigaddset(&usersigs, SIGUSR2); 212 if (ret != 0) { 213 UNRESOLVED(ret, "Unable to add SIGUSR1 or 2 to a signal set"); 214 } 215 216 /* We now block the signals SIGUSR1 and SIGUSR2 for this THREAD */ 217 ret = pthread_sigmask(SIG_BLOCK, &usersigs, NULL); 218 if (ret != 0) { 219 UNRESOLVED(ret, 220 "Unable to block SIGUSR1 and SIGUSR2 in main thread"); 221 } 222 #ifdef WITH_SYNCHRO 223 if (sem_init(&semsig1, 0, 1)) { 224 UNRESOLVED(errno, "Semsig1 init"); 225 } 226 if (sem_init(&semsig2, 0, 1)) { 227 UNRESOLVED(errno, "Semsig2 init"); 228 } 229 #endif 230 231 me = pthread_self(); 232 233 if ((ret = pthread_create(&th_work, NULL, test, (void *)&me))) { 234 UNRESOLVED(ret, "Worker thread creation failed"); 235 } 236 237 arg1.sig = SIGUSR1; 238 arg2.sig = SIGUSR2; 239 #ifdef WITH_SYNCHRO 240 arg1.sem = &semsig1; 241 arg2.sem = &semsig2; 242 #endif 243 244 if ((ret = pthread_create(&th_sig1, NULL, sendsig, (void *)&arg1))) { 245 UNRESOLVED(ret, "Signal 1 sender thread creation failed"); 246 } 247 if ((ret = pthread_create(&th_sig2, NULL, sendsig, (void *)&arg2))) { 248 UNRESOLVED(ret, "Signal 2 sender thread creation failed"); 249 } 250 251 /* Let's wait for a while now */ 252 sleep(1); 253 254 /* Now stop the threads and join them */ 255 do { 256 do_it = 0; 257 } 258 while (do_it); 259 260 if ((ret = pthread_join(th_sig1, NULL))) { 261 UNRESOLVED(ret, "Signal 1 sender thread join failed"); 262 } 263 264 if ((ret = pthread_join(th_sig2, NULL))) { 265 UNRESOLVED(ret, "Signal 2 sender thread join failed"); 266 } 267 268 if ((ret = pthread_join(th_work, NULL))) { 269 UNRESOLVED(ret, "Worker thread join failed"); 270 } 271 #if VERBOSE > 0 272 output("Test executed successfully.\n"); 273 output(" %d thread comparison.\n", count_ope); 274 #ifdef WITH_SYNCHRO 275 output(" %d signals were sent meanwhile.\n", count_sig); 276 #endif 277 #endif 278 PASSED; 279 } 280