1 /* test 37 - signals */ 2 3 #include <sys/types.h> 4 #include <sys/times.h> 5 #include <sys/wait.h> 6 #include <errno.h> 7 #include <signal.h> 8 #include <setjmp.h> 9 #include <stdlib.h> 10 #include <unistd.h> 11 #include <stdio.h> 12 #include <assert.h> 13 14 #define ITERATIONS 2 15 #define SIGS 14 16 int max_error = 4; 17 #include "common.h" 18 19 20 21 int iteration, cumsig, sig1, sig2; 22 23 int sigarray[SIGS] = {SIGHUP, SIGILL, SIGTRAP, SIGABRT, SIGIOT, 24 SIGFPE, SIGUSR1, SIGSEGV, SIGUSR2, SIGPIPE, SIGALRM, 25 SIGTERM}; 26 27 /* Prototypes produced automatically by mkptypes. */ 28 int main(int argc, char *argv []); 29 void test37a(void); 30 void func1(int sig); 31 void func2(int sig); 32 void test37b(void); 33 void catch1(int signo); 34 void catch2(int signo); 35 void test37c(void); 36 void catch3(int signo); 37 void test37d(void); 38 void catch4(int signo); 39 void test37e(void); 40 void catch5(int signo); 41 void test37f(void); 42 void sigint_handler(int signo); 43 void sigpipe_handler(int signo); 44 void test37g(void); 45 void sighup8(int signo); 46 void sigpip8(int signo); 47 void sigter8(int signo); 48 void test37h(void); 49 void sighup9(int signo); 50 void sigter9(int signo); 51 void test37i(void); 52 void sighup10(int signo); 53 void sigalrm_handler10(int signo); 54 void test37j(void); 55 void test37k(void); 56 void test37l(void); 57 void func_m1(void); 58 void func_m2(void); 59 void test37m(void); 60 void test37p(void); 61 void test37q(void); 62 void longjerr(void); 63 void catch14(int signo, int code, struct sigcontext * scp); 64 void test37n(void); 65 void catch15(int signo); 66 void test37o(void); 67 void clearsigstate(void); 68 void wait_for(int pid); 69 70 int main(argc, argv) 71 int argc; 72 char *argv[]; 73 { 74 int i, m = 0377777; 75 76 sync(); 77 78 start(37); 79 80 if (argc == 2) m = atoi(argv[1]); 81 82 for (i = 0; i < ITERATIONS; i++) { 83 iteration = i; 84 if (m & 0000001) test37a(); 85 if (m & 0000002) test37b(); 86 if (m & 0000004) test37c(); 87 if (m & 0000010) test37d(); 88 if (m & 0000020) test37e(); 89 if (m & 0000040) test37f(); 90 if (m & 0000100) test37g(); 91 if (m & 0000200) test37h(); 92 if (m & 0000400) test37i(); 93 if (m & 0001000) test37j(); 94 if (m & 0002000) test37k(); 95 if (m & 0004000) test37l(); 96 if (m & 0010000) test37m(); 97 if (m & 0020000) test37n(); 98 if (m & 0040000) test37o(); 99 if (m & 0100000) test37p(); 100 if (m & 0200000) test37q(); 101 } 102 103 quit(); 104 105 return(-1); /* Unreachable */ 106 } 107 108 void test37a() 109 { 110 /* Test signal set management. */ 111 112 sigset_t s; 113 114 subtest = 1; 115 clearsigstate(); 116 117 /* Create an empty set and see if any bits are on. */ 118 if (sigemptyset(&s) != 0) e(1); 119 if (sigismember(&s, SIGHUP) != 0) e(2); 120 if (sigismember(&s, SIGINT) != 0) e(3); 121 if (sigismember(&s, SIGQUIT) != 0) e(4); 122 if (sigismember(&s, SIGILL) != 0) e(5); 123 if (sigismember(&s, SIGTRAP) != 0) e(6); 124 if (sigismember(&s, SIGABRT) != 0) e(7); 125 if (sigismember(&s, SIGIOT) != 0) e(8); 126 if (sigismember(&s, SIGFPE) != 0) e(10); 127 if (sigismember(&s, SIGKILL) != 0) e(11); 128 if (sigismember(&s, SIGUSR1) != 0) e(12); 129 if (sigismember(&s, SIGSEGV) != 0) e(13); 130 if (sigismember(&s, SIGUSR2) != 0) e(14); 131 if (sigismember(&s, SIGPIPE) != 0) e(15); 132 if (sigismember(&s, SIGALRM) != 0) e(16); 133 if (sigismember(&s, SIGTERM) != 0) e(17); 134 if (sigismember(&s, SIGPWR) != 0) e(17); 135 136 /* Create a full set and see if any bits are off. */ 137 if (sigfillset(&s) != 0) e(19); 138 if (sigemptyset(&s) != 0) e(20); 139 if (sigfillset(&s) != 0) e(21); 140 if (sigismember(&s, SIGHUP) != 1) e(22); 141 if (sigismember(&s, SIGINT) != 1) e(23); 142 if (sigismember(&s, SIGQUIT) != 1) e(24); 143 if (sigismember(&s, SIGILL) != 1) e(25); 144 if (sigismember(&s, SIGTRAP) != 1) e(26); 145 if (sigismember(&s, SIGABRT) != 1) e(27); 146 if (sigismember(&s, SIGIOT) != 1) e(28); 147 if (sigismember(&s, SIGFPE) != 1) e(30); 148 if (sigismember(&s, SIGKILL) != 1) e(31); 149 if (sigismember(&s, SIGUSR1) != 1) e(32); 150 if (sigismember(&s, SIGSEGV) != 1) e(33); 151 if (sigismember(&s, SIGUSR2) != 1) e(34); 152 if (sigismember(&s, SIGPIPE) != 1) e(35); 153 if (sigismember(&s, SIGALRM) != 1) e(36); 154 if (sigismember(&s, SIGTERM) != 1) e(37); 155 if (sigismember(&s, SIGPWR) != 1) e(37); 156 157 /* Create an empty set, then turn on bits individually. */ 158 if (sigemptyset(&s) != 0) e(39); 159 if (sigaddset(&s, SIGHUP) != 0) e(40); 160 if (sigaddset(&s, SIGINT) != 0) e(41); 161 if (sigaddset(&s, SIGQUIT) != 0) e(42); 162 if (sigaddset(&s, SIGILL) != 0) e(43); 163 if (sigaddset(&s, SIGTRAP) != 0) e(44); 164 if (sigaddset(&s, SIGPWR) != 0) e(44); 165 166 /* See if the bits just turned on are indeed on. */ 167 if (sigismember(&s, SIGHUP) != 1) e(45); 168 if (sigismember(&s, SIGINT) != 1) e(46); 169 if (sigismember(&s, SIGQUIT) != 1) e(47); 170 if (sigismember(&s, SIGILL) != 1) e(48); 171 if (sigismember(&s, SIGTRAP) != 1) e(49); 172 if (sigismember(&s, SIGPWR) != 1) e(49); 173 174 /* The others should be turned off. */ 175 if (sigismember(&s, SIGABRT) != 0) e(50); 176 if (sigismember(&s, SIGIOT) != 0) e(51); 177 if (sigismember(&s, SIGFPE) != 0) e(53); 178 if (sigismember(&s, SIGKILL) != 0) e(54); 179 if (sigismember(&s, SIGUSR1) != 0) e(55); 180 if (sigismember(&s, SIGSEGV) != 0) e(56); 181 if (sigismember(&s, SIGUSR2) != 0) e(57); 182 if (sigismember(&s, SIGPIPE) != 0) e(58); 183 if (sigismember(&s, SIGALRM) != 0) e(59); 184 if (sigismember(&s, SIGTERM) != 0) e(60); 185 186 /* Now turn them off and see if all are off. */ 187 if (sigdelset(&s, SIGHUP) != 0) e(62); 188 if (sigdelset(&s, SIGINT) != 0) e(63); 189 if (sigdelset(&s, SIGQUIT) != 0) e(64); 190 if (sigdelset(&s, SIGILL) != 0) e(65); 191 if (sigdelset(&s, SIGTRAP) != 0) e(66); 192 if (sigdelset(&s, SIGPWR) != 0) e(66); 193 194 if (sigismember(&s, SIGHUP) != 0) e(67); 195 if (sigismember(&s, SIGINT) != 0) e(68); 196 if (sigismember(&s, SIGQUIT) != 0) e(69); 197 if (sigismember(&s, SIGILL) != 0) e(70); 198 if (sigismember(&s, SIGTRAP) != 0) e(71); 199 if (sigismember(&s, SIGABRT) != 0) e(72); 200 if (sigismember(&s, SIGIOT) != 0) e(73); 201 if (sigismember(&s, SIGFPE) != 0) e(75); 202 if (sigismember(&s, SIGKILL) != 0) e(76); 203 if (sigismember(&s, SIGUSR1) != 0) e(77); 204 if (sigismember(&s, SIGSEGV) != 0) e(78); 205 if (sigismember(&s, SIGUSR2) != 0) e(79); 206 if (sigismember(&s, SIGPIPE) != 0) e(80); 207 if (sigismember(&s, SIGALRM) != 0) e(81); 208 if (sigismember(&s, SIGTERM) != 0) e(82); 209 if (sigismember(&s, SIGPWR) != 0) e(82); 210 } 211 212 void func1(sig) 213 int sig; 214 { 215 sig1++; 216 } 217 218 void func2(sig) 219 int sig; 220 { 221 sig2++; 222 } 223 224 225 int sigmemcmp(sigset_t *s1, sigset_t *s2, int size) 226 { 227 int i; 228 int mismatch = 0; 229 assert(size == sizeof(sigset_t)); 230 for(i = 1; i < _NSIG; i++) { 231 if(sigismember(s1, i) && !sigismember(s2, i)) { 232 fprintf(stderr, "sig %d set in first but not in 2nd\n", i); 233 mismatch = 1; 234 } 235 if(!sigismember(s1, i) && sigismember(s2, i)) { 236 fprintf(stderr, "sig %d not set in first but is in 2nd\n", i); 237 mismatch = 1; 238 } 239 } 240 241 return mismatch; 242 } 243 244 void test37b() 245 { 246 /* Test sigprocmask and sigpending. */ 247 int i; 248 pid_t p; 249 sigset_t s, s1, s_empty, s_full, s_ill, s_ill_pip, s_nokill, s_nokill_stop; 250 struct sigaction sa, osa; 251 252 subtest = 2; 253 clearsigstate(); 254 255 /* Construct s_ill = {SIGILL} and s_ill_pip {SIGILL | SIGPIP}, etc. */ 256 if (sigemptyset(&s_empty) != 0) e(1); 257 if (sigemptyset(&s_ill) != 0) e(2); 258 if (sigemptyset(&s_ill_pip) != 0) e(3); 259 if (sigaddset(&s_ill, SIGILL) != 0) e(4); 260 if (sigaddset(&s_ill_pip, SIGILL) != 0) e(5); 261 if (sigaddset(&s_ill_pip, SIGPIPE) != 0) e(6); 262 if (sigfillset(&s_full) != 0) e(7); 263 s_nokill = s_full; 264 if (sigdelset(&s_nokill, SIGKILL) != 0) e(8); 265 s_nokill_stop = s_nokill; 266 if (sigdelset(&s_nokill_stop, SIGSTOP) != 0) e(8); 267 if (SIGSTOP >= _NSIG) e(666); 268 if (SIGSTOP < _NSIG && sigdelset(&s_nokill, SIGSTOP) != 0) e(888); 269 270 /* Now get most of the signals into default state. Don't change SIGINT 271 * or SIGQUIT, so this program can be killed. SIGKILL is also special. 272 */ 273 sa.sa_handler = SIG_DFL; 274 sa.sa_mask = s_empty; 275 sa.sa_flags = 0; 276 for (i = 0; i < SIGS; i++) sigaction(i, &sa, &osa); 277 278 /* The second argument may be zero. See if it wipes out the system. */ 279 for (i = 0; i < SIGS; i++) sigaction(i, (struct sigaction *) NULL, &osa); 280 281 /* Install a signal handler. */ 282 sa.sa_handler = func1; 283 sa.sa_mask = s_ill; 284 sa.sa_flags = SA_NODEFER | SA_NOCLDSTOP; 285 osa.sa_handler = SIG_IGN; 286 osa.sa_mask = s_empty; 287 osa.sa_flags = 0; 288 if (sigaction(SIGHUP, &sa, &osa) != 0) e(9); 289 if (osa.sa_handler != SIG_DFL) e(10); 290 if (sigmemcmp(&osa.sa_mask, &s_empty, sizeof(s_empty))) e(11); 291 if (osa.sa_flags != 0) e(12); 292 293 /* Replace action and see if old value is read back correctly. */ 294 sa.sa_handler = func2; 295 sa.sa_mask = s_ill_pip; 296 sa.sa_flags = SA_RESETHAND | SA_NODEFER; 297 osa.sa_handler = SIG_IGN; 298 osa.sa_mask = s_empty; 299 osa.sa_flags = 0; 300 if (sigaction(SIGHUP, &sa, &osa) != 0) e(13); 301 if (osa.sa_handler != func1) e(14); 302 if (sigmemcmp(&osa.sa_mask, &s_ill, sizeof(s_ill))) e(15); 303 if (osa.sa_flags != SA_NODEFER 304 && osa.sa_flags != (SA_NODEFER | SA_NOCLDSTOP)) e(16); 305 306 /* Replace action once more and check what is read back. */ 307 sa.sa_handler = SIG_DFL; 308 sa.sa_mask = s_empty; 309 osa.sa_handler = SIG_IGN; 310 osa.sa_mask = s_empty; 311 osa.sa_flags = 0; 312 if (sigaction(SIGHUP, &sa, &osa) != 0) e(17); 313 if (osa.sa_handler != func2) e(18); 314 if (sigmemcmp(&osa.sa_mask, &s_ill_pip, sizeof(s_ill_pip))) e(19); 315 if (osa.sa_flags != (SA_RESETHAND | SA_NODEFER)) e(20); 316 317 /* Test sigprocmask(SIG_SETMASK, ...). */ 318 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(18); /* block all */ 319 if (sigemptyset(&s1) != 0) e(19); 320 errno = 0; 321 if (sigprocmask(SIG_SETMASK, &s_empty, &s1) != 0) e(20); /* block none */ 322 if (sigmemcmp(&s1, &s_nokill_stop, sizeof(s1))) e(21); 323 if (sigprocmask(SIG_SETMASK, &s_ill, &s1) != 0) e(22); /* block SIGILL */ 324 errno = 0; 325 if (sigmemcmp(&s1, &s_empty, sizeof(s1))) e(23); 326 if (sigprocmask(SIG_SETMASK, &s_ill_pip, &s1) != 0) e(24); /* SIGILL+PIP */ 327 if (sigmemcmp(&s1, &s_ill, sizeof(s1))) e(25); 328 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(26); /* block all */ 329 if (sigmemcmp(&s1, &s_ill_pip, sizeof(s1))) e(27); 330 331 /* Test sigprocmask(SIG_UNBLOCK, ...) */ 332 if (sigprocmask(SIG_UNBLOCK, &s_ill, &s1) != 0) e(28); 333 if (sigmemcmp(&s1, &s_nokill_stop, sizeof(s1))) e(29); 334 if (sigprocmask(SIG_UNBLOCK, &s_ill_pip, &s1) != 0) e(30); 335 s = s_nokill_stop; 336 if (sigdelset(&s, SIGILL) != 0) e(31); 337 if (sigmemcmp(&s, &s1, sizeof(s))) e(32); 338 if (sigprocmask(SIG_UNBLOCK, &s_empty, &s1) != 0) e(33); 339 s = s_nokill_stop; 340 if (sigdelset(&s, SIGILL) != 0) e(34); 341 if (sigdelset(&s, SIGPIPE) != 0) e(35); 342 if (sigmemcmp(&s, &s1, sizeof(s))) e(36); 343 s1 = s_nokill_stop; 344 if (sigprocmask(SIG_SETMASK, &s_empty, &s1) != 0) e(37); 345 if (sigmemcmp(&s, &s1, sizeof(s))) e(38); 346 347 /* Test sigprocmask(SIG_BLOCK, ...) */ 348 if (sigprocmask(SIG_BLOCK, &s_ill, &s1) != 0) e(39); 349 if (sigmemcmp(&s1, &s_empty, sizeof(s1))) e(40); 350 if (sigprocmask(SIG_BLOCK, &s_ill_pip, &s1) != 0) e(41); 351 if (sigmemcmp(&s1, &s_ill, sizeof(s1))) e(42); 352 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(43); 353 if (sigmemcmp(&s1, &s_ill_pip, sizeof(s1))) e(44); 354 355 /* Check error condition. */ 356 errno = 0; 357 if (sigprocmask(20000, &s_full, &s1) != -1) e(45); 358 if (errno != EINVAL) e(46); 359 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(47); 360 if (sigmemcmp(&s1, &s_nokill_stop, sizeof(s1))) e(48); 361 362 /* If second arg is 0, nothing is set. */ 363 if (sigprocmask(SIG_SETMASK, (sigset_t *) NULL, &s1) != 0) e(49); 364 if (sigmemcmp(&s1, &s_nokill_stop, sizeof(s1))) e(50); 365 if (sigprocmask(SIG_SETMASK, &s_ill_pip, &s1) != 0) e(51); 366 if (sigmemcmp(&s1, &s_nokill_stop, sizeof(s1))) e(52); 367 if (sigprocmask(SIG_SETMASK, (sigset_t *) NULL, &s1) != 0) e(53); 368 if (sigmemcmp(&s1, &s_ill_pip, sizeof(s1))) e(54); 369 if (sigprocmask(SIG_BLOCK, (sigset_t *) NULL, &s1) != 0) e(55); 370 if (sigmemcmp(&s1, &s_ill_pip, sizeof(s1))) e(56); 371 if (sigprocmask(SIG_UNBLOCK, (sigset_t *) NULL, &s1) != 0) e(57); 372 if (sigmemcmp(&s1, &s_ill_pip, sizeof(s1))) e(58); 373 374 /* Trying to block SIGKILL is not allowed, but is not an error, either. */ 375 s = s_empty; 376 if (sigaddset(&s, SIGKILL) != 0) e(59); 377 if (sigprocmask(SIG_BLOCK, &s, &s1) != 0) e(60); 378 if (sigmemcmp(&s1, &s_ill_pip, sizeof(s1))) e(61); 379 if (sigprocmask(SIG_SETMASK, &s_full, &s1) != 0) e(62); 380 if (sigmemcmp(&s1, &s_ill_pip, sizeof(s1))) e(63); 381 382 /* Test sigpending. At this moment, all signals are blocked. */ 383 sa.sa_handler = func2; 384 sa.sa_mask = s_empty; 385 if (sigaction(SIGHUP, &sa, &osa) != 0) e(64); 386 p = getpid(); 387 kill(p, SIGHUP); /* send SIGHUP to self */ 388 if (sigpending(&s) != 0) e(65); 389 if (sigemptyset(&s1) != 0) e(66); 390 if (sigaddset(&s1, SIGHUP) != 0) e(67); 391 if (sigmemcmp(&s, &s1, sizeof(s))) e(68); 392 sa.sa_handler = SIG_IGN; 393 if (sigaction(SIGHUP, &sa, &osa) != 0) e(69); 394 if (sigpending(&s) != 0) e(70); 395 if (sigmemcmp(&s, &s_empty, sizeof(s))) e(71); 396 } 397 398 /*---------------------------------------------------------------------------*/ 399 int x; 400 sigset_t glo_vol_set; 401 402 void catch1(signo) 403 int signo; 404 { 405 x = 42; 406 } 407 408 void catch2(signo) 409 int signo; 410 { 411 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, (sigset_t *) &glo_vol_set) != 0) 412 e(1); 413 } 414 415 /* Verify that signal(2), which is now built on top of sigaction(2), still 416 * works. 417 */ 418 void test37c() 419 { 420 pid_t pid; 421 sigset_t sigset_var; 422 423 subtest = 3; 424 clearsigstate(); 425 x = 0; 426 427 /* Verify an installed signal handler persists across a fork(2). */ 428 if (signal(SIGTERM, catch1) == SIG_ERR) e(1); 429 switch (pid = fork()) { 430 case 0: /* child */ 431 errct = 0; 432 while (x == 0); 433 if (x != 42) e(2); 434 exit(errct == 0 ? 0 : 1); 435 case -1: e(3); break; 436 default: /* parent */ 437 sleep(1); 438 if (kill(pid, SIGTERM) != 0) e(4); 439 wait_for(pid); 440 break; 441 } 442 443 /* Verify that the return value is the previous handler. */ 444 signal(SIGINT, SIG_IGN); 445 if (signal(SIGINT, catch2) != SIG_IGN) e(5); 446 if (signal(SIGINT, catch1) != catch2) e(6); 447 if (signal(SIGINT, SIG_DFL) != catch1) e(7); 448 if (signal(SIGINT, catch1) != SIG_DFL) e(8); 449 if (signal(SIGINT, SIG_DFL) != catch1) e(9); 450 if (signal(SIGINT, SIG_DFL) != SIG_DFL) e(10); 451 if (signal(SIGINT, catch1) != SIG_DFL) e(11); 452 453 /* Verify that SIG_ERR is correctly generated. */ 454 if (signal(_NSIG, catch1) != SIG_ERR) e(12); 455 if (signal(0, catch1) != SIG_ERR) e(13); 456 if (signal(-1, SIG_DFL) != SIG_ERR) e(14); 457 458 /* Verify that caught signals are automatically reset to the default, 459 * and that further instances of the same signal are not blocked here 460 * or in the signal handler. 461 */ 462 if (signal(SIGTERM, catch1) == SIG_ERR) e(15); 463 switch ((pid = fork())) { 464 case 0: /* child */ 465 errct = 0; 466 while (x == 0); 467 if (x != 42) e(16); 468 if (sigismember((sigset_t *) &glo_vol_set, SIGTERM)) e(17); 469 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &sigset_var) != 0) e(18); 470 if (sigismember(&sigset_var, SIGTERM)) e(19); 471 472 #if 0 473 /* Use this if you have compiled signal() to have the broken SYSV behaviour. */ 474 if (signal(SIGTERM, catch1) != SIG_DFL) e(20); 475 #else 476 if (signal(SIGTERM, catch1) != catch1) e(20); 477 #endif 478 exit(errct == 0 ? 0 : 1); 479 default: /* parent */ 480 sleep(1); 481 if (kill(pid, SIGTERM) != 0) e(21); 482 wait_for(pid); 483 break; 484 case -1: e(22); break; 485 } 486 } 487 488 /*---------------------------------------------------------------------------*/ 489 /* Test that the signal handler can be invoked recursively with the 490 * state being properly saved and restored. 491 */ 492 493 static int y; 494 static int z; 495 496 void catch3(signo) 497 int signo; 498 { 499 if (z == 1) { /* catching a nested signal */ 500 y = 2; 501 return; 502 } 503 z = 1; 504 if (kill(getpid(), SIGHUP) != 0) e(1); 505 while (y != 2); 506 y = 1; 507 } 508 509 void test37d() 510 { 511 struct sigaction act; 512 513 subtest = 4; 514 clearsigstate(); 515 y = 0; 516 z = 0; 517 518 act.sa_handler = catch3; 519 sigemptyset(&act.sa_mask); 520 act.sa_flags = SA_NODEFER; /* Otherwise, nested occurence of 521 * SIGINT is blocked. */ 522 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) != 0) e(2); 523 if (kill(getpid(), SIGHUP) != 0) e(3); 524 if (y != 1) e(4); 525 } 526 527 /*---------------------------------------------------------------------------*/ 528 529 /* Test that the signal mask in effect for the duration of a signal handler 530 * is as specified in POSIX Section 3, lines 718 -724. Test that the 531 * previous signal mask is restored when the signal handler returns. 532 */ 533 534 void catch4(signo) 535 int signo; 536 { 537 sigset_t oset; 538 sigset_t set; 539 int i; 540 541 if (sigemptyset(&set) == -1) e(5001); 542 if (sigaddset(&set, SIGTERM) == -1) e(5002); 543 if (sigaddset(&set, SIGHUP) == -1) e(5003); 544 if (sigaddset(&set, SIGINT) == -1) e(5004); 545 if (sigaddset(&set, SIGPIPE) == -1) e(5005); 546 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &oset) != 0) e(5006); 547 if (sigmemcmp(&oset, &set, sizeof(set))) e(5007); 548 } 549 550 void test37e() 551 { 552 struct sigaction act, oact; 553 sigset_t set, oset; 554 555 subtest = 5; 556 clearsigstate(); 557 558 act.sa_handler = catch4; 559 sigemptyset(&act.sa_mask); 560 sigaddset(&act.sa_mask, SIGTERM); 561 sigaddset(&act.sa_mask, SIGHUP); 562 act.sa_flags = 0; 563 if (sigaction(SIGINT, &act, &oact) == -1) e(2); 564 565 if (sigemptyset(&set) == -1) e(3); 566 if (sigaddset(&set, SIGPIPE) == -1) e(4); 567 if (sigprocmask(SIG_SETMASK, &set, &oset) == -1) e(5); 568 if (kill(getpid(), SIGINT) == -1) e(6); 569 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &oset) == -1) e(7); 570 if (sigemptyset(&set) == -1) e(8); 571 if (sigaddset(&set, SIGPIPE) == -1) e(9); 572 if (sigmemcmp(&set, &oset, sizeof(set))) e(10); 573 } 574 575 /*---------------------------------------------------------------------------*/ 576 577 /* Test the basic functionality of sigsuspend(2). */ 578 579 void catch5(signo) 580 int signo; 581 { 582 x = 1; 583 } 584 585 void test37f() 586 { 587 sigset_t set; 588 int r; 589 struct sigaction act; 590 pid_t pid; 591 592 subtest = 6; 593 clearsigstate(); 594 595 switch (pid = fork()) { 596 case 0: /* child */ 597 errct = 0; 598 sleep(1); 599 if (kill(getppid(), SIGINT) == -1) e(1); 600 exit(errct == 0 ? 0 : 1); 601 case -1: e(2); break; 602 default: /* parent */ 603 if (sigemptyset(&act.sa_mask) == -1) e(3); 604 act.sa_flags = 0; 605 act.sa_handler = catch5; 606 if (sigaction(SIGINT, &act, (struct sigaction *) NULL) == -1) e(4); 607 608 if (sigemptyset(&set) == -1) e(5); 609 r = sigsuspend(&set); 610 611 if (r != -1 || errno != EINTR || x != 1) e(6); 612 wait_for(pid); 613 break; 614 } 615 } 616 617 /*----------------------------------------------------------------------*/ 618 619 /* Test that sigsuspend() does block the signals specified in its 620 * argument, and after sigsuspend returns, the previous signal 621 * mask is restored. 622 * 623 * The child sends two signals to the parent SIGINT and then SIGPIPE, 624 * separated by a long delay. The parent executes sigsuspend() with 625 * SIGINT blocked. It is expected that the parent's SIGPIPE handler 626 * will be invoked, then sigsuspend will return restoring the 627 * original signal mask, and then the SIGPIPE handler will be 628 * invoked. 629 */ 630 631 void sigint_handler(signo) 632 int signo; 633 { 634 x = 1; 635 z++; 636 } 637 638 void sigpipe_handler(signo) 639 int signo; 640 { 641 x = 2; 642 z++; 643 } 644 645 void test37g() 646 { 647 sigset_t set; 648 int r; 649 struct sigaction act; 650 pid_t pid; 651 652 subtest = 7; 653 clearsigstate(); 654 x = 0; 655 z = 0; 656 657 switch (pid = fork()) { 658 case 0: /* child */ 659 errct = 0; 660 sleep(1); 661 if (kill(getppid(), SIGINT) == -1) e(1); 662 sleep(1); 663 if (kill(getppid(), SIGPIPE) == -1) e(2); 664 exit(errct == 0 ? 0 : 1); 665 case -1: e(3); break; 666 default: /* parent */ 667 if (sigemptyset(&act.sa_mask) == -1) e(3); 668 act.sa_flags = 0; 669 act.sa_handler = sigint_handler; 670 if (sigaction(SIGINT, &act, (struct sigaction *) NULL) == -1) e(4); 671 672 act.sa_handler = sigpipe_handler; 673 if (sigaction(SIGPIPE, &act, (struct sigaction *) NULL) == -1) e(5); 674 675 if (sigemptyset(&set) == -1) e(6); 676 if (sigaddset(&set, SIGINT) == -1) e(7); 677 r = sigsuspend(&set); 678 if (r != -1) e(8); 679 if (errno != EINTR) e(9); 680 if (z != 2) e(10); 681 if (x != 1) e(11); 682 wait_for(pid); 683 break; 684 } 685 } 686 687 /*--------------------------------------------------------------------------*/ 688 689 /* Test that sigsuspend() does block the signals specified in its 690 * argument, and after sigsuspend returns, the previous signal 691 * mask is restored. 692 * 693 * The child sends three signals to the parent: SIGHUP, then SIGPIPE, 694 * and then SIGTERM, separated by a long delay. The parent executes 695 * sigsuspend() with SIGHUP and SIGPIPE blocked. It is expected that 696 * the parent's SIGTERM handler will be invoked first, then sigsuspend() 697 * will return restoring the original signal mask, and then the other 698 * two handlers will be invoked. 699 */ 700 701 void sighup8(signo) 702 int signo; 703 { 704 x = 1; 705 z++; 706 } 707 708 void sigpip8(signo) 709 int signo; 710 { 711 x = 1; 712 z++; 713 } 714 715 void sigter8(signo) 716 int signo; 717 { 718 x = 2; 719 z++; 720 } 721 722 void test37h() 723 { 724 sigset_t set; 725 int r; 726 struct sigaction act; 727 pid_t pid; 728 729 subtest = 8; 730 clearsigstate(); 731 x = 0; 732 z = 0; 733 734 switch (pid = fork()) { 735 case 0: /* child */ 736 errct = 0; 737 sleep(1); 738 if (kill(getppid(), SIGHUP) == -1) e(1); 739 sleep(1); 740 if (kill(getppid(), SIGPIPE) == -1) e(2); 741 sleep(1); 742 if (kill(getppid(), SIGTERM) == -1) e(3); 743 exit(errct == 0 ? 0 : 1); 744 case -1: e(5); break; 745 default: /* parent */ 746 if (sigemptyset(&act.sa_mask) == -1) e(6); 747 act.sa_flags = 0; 748 act.sa_handler = sighup8; 749 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(7); 750 751 act.sa_handler = sigpip8; 752 if (sigaction(SIGPIPE, &act, (struct sigaction *) NULL) == -1) e(8); 753 754 act.sa_handler = sigter8; 755 if (sigaction(SIGTERM, &act, (struct sigaction *) NULL) == -1) e(9); 756 757 if (sigemptyset(&set) == -1) e(10); 758 if (sigaddset(&set, SIGHUP) == -1) e(11); 759 if (sigaddset(&set, SIGPIPE) == -1) e(12); 760 r = sigsuspend(&set); 761 if (r != -1) e(13); 762 if (errno != EINTR) e(14); 763 if (z != 3) e(15); 764 if (x != 1) e(16); 765 wait_for(pid); 766 break; 767 } 768 } 769 770 /*--------------------------------------------------------------------------*/ 771 772 /* Block SIGHUP and SIGTERM with sigprocmask(), send ourself SIGHUP 773 * and SIGTERM, unblock these signals with sigprocmask, and verify 774 * that these signals are delivered. 775 */ 776 777 void sighup9(signo) 778 int signo; 779 { 780 y++; 781 } 782 783 void sigter9(signo) 784 int signo; 785 { 786 z++; 787 } 788 789 void test37i() 790 { 791 sigset_t set; 792 struct sigaction act; 793 794 subtest = 9; 795 clearsigstate(); 796 y = 0; 797 z = 0; 798 799 if (sigemptyset(&act.sa_mask) == -1) e(1); 800 act.sa_flags = 0; 801 802 act.sa_handler = sighup9; 803 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(2); 804 805 act.sa_handler = sigter9; 806 if (sigaction(SIGTERM, &act, (struct sigaction *) NULL) == -1) e(3); 807 808 if (sigemptyset(&set) == -1) e(4); 809 if (sigaddset(&set, SIGTERM) == -1) e(5); 810 if (sigaddset(&set, SIGHUP) == -1) e(6); 811 if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(7); 812 813 if (kill(getpid(), SIGHUP) == -1) e(8); 814 if (kill(getpid(), SIGTERM) == -1) e(9); 815 if (y != 0) e(10); 816 if (z != 0) e(11); 817 818 if (sigemptyset(&set) == -1) e(12); 819 if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(12); 820 if (y != 1) e(13); 821 if (z != 1) e(14); 822 } 823 824 /*---------------------------------------------------------------------------*/ 825 826 /* Block SIGINT and then send this signal to ourself. 827 * 828 * Install signal handlers for SIGALRM and SIGINT. 829 * 830 * Set an alarm for 6 seconds, then sleep for 7. 831 * 832 * The SIGALRM should interrupt the sleep, but the SIGINT 833 * should remain pending. 834 */ 835 836 void sighup10(signo) 837 int signo; 838 { 839 y++; 840 } 841 842 void sigalrm_handler10(signo) 843 int signo; 844 { 845 z++; 846 } 847 848 void test37j() 849 { 850 sigset_t set, set2; 851 struct sigaction act; 852 853 subtest = 10; 854 clearsigstate(); 855 y = 0; 856 z = 0; 857 858 if (sigemptyset(&act.sa_mask) == -1) e(1); 859 act.sa_flags = 0; 860 861 act.sa_handler = sighup10; 862 if (sigaction(SIGHUP, &act, (struct sigaction *) NULL) == -1) e(2); 863 864 act.sa_handler = sigalrm_handler10; 865 if (sigaction(SIGALRM, &act, (struct sigaction *) NULL) == -1) e(3); 866 867 if (sigemptyset(&set) == -1) e(4); 868 if (sigaddset(&set, SIGHUP) == -1) e(5); 869 if (sigprocmask(SIG_SETMASK, &set, (sigset_t *)NULL) == -1) e(6); 870 871 if (kill(getpid(), SIGHUP) == -1) e(7); 872 if (sigpending(&set) == -1) e(8); 873 if (sigemptyset(&set2) == -1) e(9); 874 if (sigaddset(&set2, SIGHUP) == -1) e(10); 875 if (sigmemcmp(&set2, &set, sizeof(set))) e(11); 876 alarm(6); 877 sleep(7); 878 if (sigpending(&set) == -1) e(12); 879 if (sigmemcmp(&set, &set2, sizeof(set))) e(13); 880 if (y != 0) e(14); 881 if (z != 1) e(15); 882 } 883 884 /*--------------------------------------------------------------------------*/ 885 886 void test37k() 887 { 888 subtest = 11; 889 } 890 void test37l() 891 { 892 subtest = 12; 893 } 894 895 /*---------------------------------------------------------------------------*/ 896 897 /* Basic test for setjmp/longjmp. This includes testing that the 898 * signal mask is properly restored. 899 */ 900 901 #define TEST_SETJMP(_name, _subtest, _type, _setjmp, _longjmp, _save) \ 902 void _name(void) \ 903 { \ 904 _type jb; \ 905 sigset_t ss, ss2, ss_orig; \ 906 \ 907 subtest = _subtest; \ 908 clearsigstate(); \ 909 \ 910 sigemptyset(&ss); sigemptyset(&ss2); \ 911 sigaddset(&ss, 2); sigaddset(&ss, 4); sigaddset(&ss, 5); \ 912 sigaddset(&ss2, 20); sigaddset(&ss2, 22); sigaddset(&ss2, 65); \ 913 memcpy(&ss_orig, &ss, sizeof(ss)); \ 914 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(1); \ 915 if (_setjmp) { \ 916 sigset_t ssexp; \ 917 if (sigprocmask(SIG_BLOCK, (sigset_t *)NULL, &ss) == -1) e(2); \ 918 ssexp = _save ? ss_orig : ss2; \ 919 sigdelset(&ssexp, SIGKILL); \ 920 if (sigmemcmp(&ss, &ssexp, sizeof(ss))) e(388); \ 921 return; \ 922 } \ 923 ss = ss2; \ 924 if (sigprocmask(SIG_SETMASK, &ss, (sigset_t *)NULL) == -1) e(4); \ 925 _longjmp; \ 926 } 927 928 TEST_SETJMP(test37m, 13, jmp_buf, setjmp(jb), longjmp(jb, 1), 1) 929 TEST_SETJMP(test37p, 16, sigjmp_buf, sigsetjmp(jb, 0), siglongjmp(jb, 1), 0) 930 TEST_SETJMP(test37q, 17, sigjmp_buf, sigsetjmp(jb, 1), siglongjmp(jb, 1), 1) 931 932 void longjerr() 933 { 934 e(5); 935 } 936 937 /*--------------------------------------------------------------------------*/ 938 939 /* Test for setjmp/longjmp. 940 * 941 * Catch a signal. While in signal handler do setjmp/longjmp. 942 */ 943 944 void catch14(signo, code, scp) 945 int signo; 946 int code; 947 struct sigcontext *scp; 948 { 949 jmp_buf jb; 950 951 if (setjmp(jb)) { 952 x++; 953 sigreturn(scp); 954 e(1); 955 } 956 y++; 957 longjmp(jb, 1); 958 e(2); 959 } 960 961 void test37n() 962 { 963 struct sigaction act; 964 typedef void(*sighandler_t) (int sig); 965 966 subtest = 14; 967 clearsigstate(); 968 x = 0; 969 y = 0; 970 971 act.sa_flags = 0; 972 sigemptyset(&act.sa_mask); 973 act.sa_handler = (sighandler_t) catch14; /* fudge */ 974 if (sigaction(SIGSEGV, &act, (struct sigaction *) NULL) == -1) e(3); 975 if (kill(getpid(), SIGSEGV) == -1) e(4); 976 977 if (x != 1) e(5); 978 if (y != 1) e(6); 979 } 980 981 /*---------------------------------------------------------------------------*/ 982 983 /* Test for setjmp/longjmp. 984 * 985 * Catch a signal. Longjmp out of signal handler. 986 */ 987 jmp_buf glo_jb; 988 989 void catch15(signo) 990 int signo; 991 { 992 z++; 993 longjmp(glo_jb, 7); 994 e(1); 995 996 } 997 998 void test37o() 999 { 1000 struct sigaction act; 1001 int k; 1002 1003 subtest = 15; 1004 clearsigstate(); 1005 z = 0; 1006 1007 act.sa_flags = 0; 1008 sigemptyset(&act.sa_mask); 1009 act.sa_handler = catch15; 1010 if (sigaction(SIGALRM, &act, (struct sigaction *) NULL) == -1) e(2); 1011 1012 if ((k = setjmp(glo_jb))) { 1013 if (z != 1) e(399); 1014 if (k != 7) e(4); 1015 return; 1016 } 1017 if (kill(getpid(), SIGALRM) == -1) e(5); 1018 } 1019 1020 void clearsigstate() 1021 { 1022 int i; 1023 sigset_t sigset_var; 1024 1025 /* Clear the signal state. */ 1026 for (i = 1; i < _NSIG; i++) signal(i, SIG_IGN); 1027 for (i = 1; i < _NSIG; i++) signal(i, SIG_DFL); 1028 sigfillset(&sigset_var); 1029 sigprocmask(SIG_UNBLOCK, &sigset_var, (sigset_t *)NULL); 1030 } 1031 1032 void wait_for(pid) 1033 pid_t pid; 1034 { 1035 /* Expect exactly one child, and that it exits with 0. */ 1036 1037 int r; 1038 int status; 1039 1040 errno = 0; 1041 while (1) { 1042 errno = 0; 1043 r = wait(&status); 1044 if (r == pid) { 1045 errno = 0; 1046 if (status != 0) e(90); 1047 return; 1048 } 1049 if (r < 0) { 1050 e(91); 1051 return; 1052 } 1053 e(92); 1054 } 1055 } 1056 1057