sewardj | cbdddcf | 2005-03-10 23:23:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | Test pending signals |
| 3 | |
| 4 | 1. Signals should remain pending while blocked, and not delivered early |
| 5 | |
| 6 | 2. When unblocking the signal, the signal should have been delivered |
| 7 | by the time sigprocmask syscall is complete. |
| 8 | */ |
| 9 | #include <signal.h> |
| 10 | #include <stdio.h> |
| 11 | #include <unistd.h> |
| 12 | #include <stdlib.h> |
| 13 | |
| 14 | static volatile int gotsig = 0; |
| 15 | static volatile int early = 1; |
| 16 | |
| 17 | static void handler(int sig) |
| 18 | { |
| 19 | printf("4: got signal %d\n", sig); |
| 20 | |
| 21 | if (sig != SIGUSR1) { |
| 22 | fprintf(stderr, "FAILED: got signal %d instead\n", sig); |
| 23 | exit(1); |
| 24 | } |
| 25 | |
| 26 | if (early) { |
| 27 | fprintf(stderr, "FAILED: signal delivered early (in handler)\n"); |
| 28 | exit(1); |
| 29 | } |
| 30 | |
| 31 | gotsig = 1; |
| 32 | } |
| 33 | |
| 34 | int main() |
| 35 | { |
| 36 | sigset_t all; |
| 37 | sigset_t sigusr1; |
| 38 | siginfo_t info; |
| 39 | |
| 40 | sigfillset(&all); |
| 41 | sigemptyset(&sigusr1); |
| 42 | sigaddset(&sigusr1, SIGUSR1); |
| 43 | |
| 44 | sigprocmask(SIG_BLOCK, &all, NULL); |
| 45 | |
| 46 | signal(SIGUSR1, handler); |
| 47 | signal(SIGUSR2, handler); |
| 48 | |
| 49 | printf("1: sending signal\n"); |
| 50 | kill(getpid(), SIGUSR1); |
| 51 | kill(getpid(), SIGUSR2); |
| 52 | |
| 53 | printf("2: sleeping\n"); |
| 54 | sleep(1); |
| 55 | |
| 56 | if (gotsig) { |
| 57 | fprintf(stderr, "FAILED: signal delivered too early\n"); |
| 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | printf("3: unblocking\n"); |
| 62 | early = 0; |
| 63 | sigprocmask(SIG_UNBLOCK, &sigusr1, NULL); |
| 64 | |
| 65 | printf("5: unblocked...\n"); |
| 66 | if (!gotsig) { |
| 67 | fprintf(stderr, "FAILED: signal not delivered\n"); |
| 68 | return 1; |
| 69 | } |
| 70 | |
| 71 | printf("6: checking SIGUSR2 still pending...\n"); |
| 72 | if (sigwaitinfo(&all, &info) == -1) { |
| 73 | perror("FAILED: sigwaitinfo failed"); |
| 74 | return 1; |
| 75 | } |
| 76 | if (info.si_signo != SIGUSR2) { |
| 77 | fprintf(stderr, "FAILED: SIGUSR2 not still pending; got signal %d\n", |
| 78 | info.si_signo); |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | printf("OK\n"); |
| 83 | return 0; |
| 84 | } |