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_setschedparam 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 <sched.h> 41 #include <semaphore.h> 42 #include <errno.h> 43 #include <signal.h> 44 45 /******************************************************************************/ 46 /*********************** Test framework ***********************************/ 47 /******************************************************************************/ 48 #include "../testfrmw/testfrmw.h" 49 #include "../testfrmw/testfrmw.c" 50 /* This header is responsible for defining the following macros: 51 * UNRESOLVED(ret, descr); 52 * where descr is a description of the error and ret is an int 53 * (error code for example) 54 * FAILED(descr); 55 * where descr is a short text saying why the test has failed. 56 * PASSED(); 57 * No parameter. 58 * 59 * Both three macros shall terminate the calling process. 60 * The testcase shall not terminate in any other maneer. 61 * 62 * The other file defines the functions 63 * void output_init() 64 * void output(char * string, ...) 65 * 66 * Those may be used to output information. 67 */ 68 69 /******************************************************************************/ 70 /***************************** Configuration **********************************/ 71 /******************************************************************************/ 72 #ifndef VERBOSE 73 #define VERBOSE 1 74 #endif 75 76 #define WITH_SYNCHRO 77 78 /******************************************************************************/ 79 /***************************** Test case *********************************/ 80 /******************************************************************************/ 81 82 char do_it = 1; 83 unsigned long count_ope = 0; 84 #ifdef WITH_SYNCHRO 85 sem_t semsig1; 86 sem_t semsig2; 87 unsigned long count_sig = 0; 88 #endif 89 90 sigset_t usersigs; 91 92 typedef struct { 93 int sig; 94 #ifdef WITH_SYNCHRO 95 sem_t *sem; 96 #endif 97 } thestruct; 98 99 /* the following function keeps on sending the signal to the process */ 100 void *sendsig(void *arg) 101 { 102 thestruct *thearg = (thestruct *) arg; 103 int ret; 104 pid_t process; 105 106 process = getpid(); 107 108 /* We block the signals SIGUSR1 and SIGUSR2 for this THREAD */ 109 ret = pthread_sigmask(SIG_BLOCK, &usersigs, NULL); 110 111 if (ret != 0) { 112 UNRESOLVED(ret, 113 "Unable to block SIGUSR1 and SIGUSR2 in signal thread"); 114 } 115 116 while (do_it) { 117 #ifdef WITH_SYNCHRO 118 119 if ((ret = sem_wait(thearg->sem))) { 120 UNRESOLVED(errno, "Sem_wait in sendsig"); 121 } 122 123 count_sig++; 124 #endif 125 126 ret = kill(process, thearg->sig); 127 128 if (ret != 0) { 129 UNRESOLVED(errno, "Kill in sendsig"); 130 } 131 132 } 133 134 return NULL; 135 } 136 137 /* Next are the signal handlers. */ 138 /* This one is registered for signal SIGUSR1 */ 139 void sighdl1(int sig) 140 { 141 #ifdef WITH_SYNCHRO 142 143 if (sem_post(&semsig1)) { 144 UNRESOLVED(errno, "Sem_post in signal handler 1"); 145 } 146 #endif 147 } 148 149 /* This one is registered for signal SIGUSR2 */ 150 void sighdl2(int sig) 151 { 152 #ifdef WITH_SYNCHRO 153 154 if (sem_post(&semsig2)) { 155 UNRESOLVED(errno, "Sem_post in signal handler 2"); 156 } 157 #endif 158 } 159 160 /* Test function -- calls pthread_setschedparam() and checks that EINTR is never returned. */ 161 void *test(void *arg) 162 { 163 int ret = 0; 164 int pol1, pol2; 165 166 struct sched_param sp1, sp2; 167 168 pol1 = SCHED_FIFO; 169 pol2 = SCHED_RR; 170 sp1.sched_priority = sched_get_priority_min(SCHED_FIFO); 171 sp2.sched_priority = sched_get_priority_min(SCHED_RR); 172 173 /* We don't block the signals SIGUSR1 and SIGUSR2 for this THREAD */ 174 ret = pthread_sigmask(SIG_UNBLOCK, &usersigs, NULL); 175 176 if (ret != 0) { 177 UNRESOLVED(ret, 178 "Unable to unblock SIGUSR1 and SIGUSR2 in worker thread"); 179 } 180 181 while (do_it) { 182 count_ope++; 183 ret = pthread_setschedparam(*(pthread_t *) arg, pol1, &sp1); 184 185 if (ret == EINTR) { 186 FAILED("EINTR was returned"); 187 } 188 189 ret = pthread_setschedparam(*(pthread_t *) arg, pol2, &sp2); 190 191 if (ret == EINTR) { 192 FAILED("EINTR was returned"); 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 prepare a signal set which includes SIGUSR1 and SIGUSR2 */ 212 sigemptyset(&usersigs); 213 214 ret = sigaddset(&usersigs, SIGUSR1); 215 216 ret |= sigaddset(&usersigs, SIGUSR2); 217 218 if (ret != 0) { 219 UNRESOLVED(ret, "Unable to add SIGUSR1 or 2 to a signal set"); 220 } 221 222 /* We now block the signals SIGUSR1 and SIGUSR2 for this THREAD */ 223 ret = pthread_sigmask(SIG_BLOCK, &usersigs, NULL); 224 225 if (ret != 0) { 226 UNRESOLVED(ret, 227 "Unable to block SIGUSR1 and SIGUSR2 in main thread"); 228 } 229 #ifdef WITH_SYNCHRO 230 if (sem_init(&semsig1, 0, 1)) { 231 UNRESOLVED(errno, "Semsig1 init"); 232 } 233 234 if (sem_init(&semsig2, 0, 1)) { 235 UNRESOLVED(errno, "Semsig2 init"); 236 } 237 #endif 238 239 /* We need to register the signal handlers for the PROCESS */ 240 sigemptyset(&sa.sa_mask); 241 sa.sa_flags = 0; 242 sa.sa_handler = sighdl1; 243 244 if ((ret = sigaction(SIGUSR1, &sa, NULL))) { 245 UNRESOLVED(ret, "Unable to register signal handler1"); 246 } 247 248 sa.sa_handler = sighdl2; 249 250 if ((ret = sigaction(SIGUSR2, &sa, NULL))) { 251 UNRESOLVED(ret, "Unable to register signal handler2"); 252 } 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