Add /none/tests/use_after_close to .gitignore
[valgrind.git] / memcheck / tests / freebsd / sigwait.c
blob13dd2007e77bd21a5077482eb6d8cc00689848a8
1 #include <signal.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <assert.h>
6 // from stack overflow
7 // https://stackoverflow.com/questions/6326290/about-the-ambiguous-description-of-sigwait
8 // except the deliberate errors
10 static int sig_count;
12 void on_sigusr1(int sig)
14 ++sig_count;
17 int main(void)
19 // Set a signal handler for SIGUSR1
20 signal(SIGUSR1, &on_sigusr1);
22 // At program startup, SIGUSR1 is neither blocked nor pending, so raising it
23 // will call the signal handler
24 raise(SIGUSR1);
26 // Now let's block SIGUSR1
27 sigset_t* psigset = malloc(sizeof(sigset_t));
28 sigemptyset(psigset);
29 sigaddset(psigset, SIGUSR1);
30 sigprocmask(SIG_BLOCK, psigset, NULL);
32 // SIGUSR1 is now blocked, raising it will not call the signal handler
33 raise(SIGUSR1);
35 // SIGUSR1 is now blocked and pending -- this call to sigwait will return
36 // immediately
37 int sig;
38 int result = sigwait(psigset, &sig);
39 if(result == 0)
40 printf("sigwait got signal: %d\n", sig);
42 // SIGUSR1 is now no longer pending (but still blocked). Raise it again and
43 // unblock it
44 raise(SIGUSR1);
45 printf("About to unblock SIGUSR1\n");
46 sigprocmask(SIG_UNBLOCK, psigset, NULL);
47 printf("Unblocked SIGUSR1\n");
49 assert(sig_count == 2);
51 // now a couple of bad params
52 // reblock
53 sigprocmask(SIG_BLOCK, psigset, NULL);
54 raise(SIGUSR1);
56 int* psig = malloc(sizeof(int));
57 free(psig);
58 result = sigwait(psigset, psig);
60 free(psigset);
62 raise(SIGUSR1);
64 result = sigwait(psigset, &sig);
66 return 0;