0007-signal.c (3206B)
1 #include <assert.h> 2 #include <setjmp.h> 3 #include <signal.h> 4 #include <stdio.h> 5 6 sig_atomic_t abrt, fpe, iint, segv, term, def; 7 jmp_buf recover; 8 9 /* 10 output: 11 test 1 12 test 2 13 test 3 14 test 4 15 end: 16 */ 17 18 void 19 handler1(int sig) 20 { 21 switch (sig) { 22 case SIGABRT: 23 abrt = 1; 24 break; 25 case SIGFPE: 26 fpe = 1; 27 break; 28 case SIGINT: 29 iint = 1; 30 break; 31 case SIGSEGV: 32 segv = 1; 33 break; 34 case SIGTERM: 35 term = 1; 36 break; 37 default: 38 def = 1; 39 break; 40 } 41 } 42 43 void 44 handler2(int sig) 45 { 46 switch (sig) { 47 case SIGABRT: 48 abrt = 1; 49 break; 50 case SIGFPE: 51 fpe = 1; 52 break; 53 case SIGINT: 54 iint = 1; 55 break; 56 case SIGSEGV: 57 segv = 1; 58 break; 59 case SIGTERM: 60 term = 1; 61 break; 62 default: 63 def = 1; 64 break; 65 } 66 longjmp(recover, 1); 67 } 68 69 void 70 test1() 71 { 72 puts("test 1"); 73 assert(signal(SIGABRT, handler1) == SIG_DFL); 74 assert(signal(SIGFPE, handler1) == SIG_DFL); 75 assert(signal(SIGINT, handler1) == SIG_DFL); 76 assert(signal(SIGSEGV, handler1) == SIG_DFL); 77 assert(signal(SIGTERM, handler1) == SIG_DFL); 78 } 79 80 void 81 test2() 82 { 83 puts("test 2"); 84 term = abrt = fpe = iint = segv = 0; 85 assert(signal(SIGABRT, handler1) == handler1); 86 assert(signal(SIGFPE, handler1) == handler1); 87 assert(signal(SIGINT, handler1) == handler1); 88 assert(signal(SIGSEGV, handler1) == handler1); 89 assert(signal(SIGTERM, handler1) == handler1); 90 91 assert(raise(SIGABRT) != -1); 92 assert(raise(SIGFPE) != -1); 93 assert(raise(SIGINT) != -1); 94 assert(raise(SIGSEGV) != -1); 95 assert(raise(SIGTERM) != -1); 96 97 if (!abrt || !fpe || !iint || !segv || !term) 98 printf("a handled signal was missed: %d %d %d %d %d\n", 99 abrt, fpe, iint, segv, term); 100 if (def) 101 puts("a wrong signal was received"); 102 } 103 104 void 105 test3() 106 { 107 puts("test 3"); 108 def = abrt = term = fpe = iint = segv = 0; 109 assert(signal(SIGABRT, SIG_IGN) == handler1); 110 assert(signal(SIGFPE, SIG_IGN) == handler1); 111 assert(signal(SIGINT, SIG_IGN) == handler1); 112 assert(signal(SIGSEGV, SIG_IGN) == handler1); 113 assert(signal(SIGTERM, SIG_IGN) == handler1); 114 115 assert(raise(SIGABRT) != -1); 116 assert(raise(SIGFPE) != -1); 117 assert(raise(SIGINT) != -1); 118 assert(raise(SIGSEGV) != -1); 119 assert(raise(SIGTERM) != -1); 120 121 if (abrt || fpe || iint || segv || term) 122 printf("a handled signal was received: %d %d %d %d %d\n", 123 abrt, fpe, iint, segv, term); 124 if (def) 125 puts("a wrong signal was received"); 126 } 127 128 void 129 test4() 130 { 131 puts("test 4"); 132 def = abrt = term = fpe = iint = segv = 0; 133 assert(signal(SIGABRT, handler2) == SIG_IGN); 134 assert(signal(SIGFPE, handler2) == SIG_IGN); 135 assert(signal(SIGINT, handler2) == SIG_IGN); 136 assert(signal(SIGSEGV, handler2) == SIG_IGN); 137 assert(signal(SIGTERM, handler2) == SIG_IGN); 138 139 if (!setjmp(recover)) 140 assert(raise(SIGABRT) != -1); 141 if (!setjmp(recover)) 142 assert(raise(SIGFPE) != -1); 143 if (!setjmp(recover)) 144 assert(raise(SIGINT) != -1); 145 if (!setjmp(recover)) 146 assert(raise(SIGSEGV) != -1); 147 if (!setjmp(recover)) 148 assert(raise(SIGTERM) != -1); 149 150 if (!abrt || !fpe || !iint || !segv || !term) 151 printf("a handled signal was missed: %d %d %d %d %d\n", 152 abrt, fpe, iint, segv, term); 153 if (def) 154 puts("a wrong signal was received"); 155 } 156 157 int 158 main() 159 { 160 assert(SIG_ERR != SIG_IGN && SIG_ERR != SIG_DFL); 161 162 test1(); 163 test2(); 164 test3(); 165 test4(); 166 167 return 0; 168 }