xref: /freebsd/tools/regression/sigqueue/sigqtest1/sigqtest1.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 #include <err.h>
2 #include <errno.h>
3 #include <signal.h>
4 #include <stdio.h>
5 #include <unistd.h>
6 
7 int received;
8 
9 void
10 handler(int sig, siginfo_t *si, void *ctx)
11 {
12 	if (si->si_code != SI_QUEUE)
13 		errx(1, "si_code != SI_QUEUE");
14 	if (si->si_value.sival_int != received)
15 		errx(1, "signal is out of order");
16 	received++;
17 }
18 
19 int
20 main()
21 {
22 	struct sigaction sa;
23 	union sigval val;
24 	int ret;
25 	int i;
26 	sigset_t set;
27 
28 	sa.sa_flags = SA_SIGINFO;
29 	sigemptyset(&sa.sa_mask);
30 	sa.sa_sigaction = handler;
31 	sigaction(SIGRTMIN, &sa, NULL);
32 	sigemptyset(&set);
33 	sigaddset(&set, SIGRTMIN);
34 	sigprocmask(SIG_BLOCK, &set, NULL);
35 	i = 0;
36 	for (;;) {
37 		val.sival_int = i;
38 		ret = sigqueue(getpid(), SIGRTMIN, val);
39 		if (ret == -1) {
40 			if (errno != EAGAIN) {
41 				errx(1, "errno != EAGAIN");
42 			}
43 			break;
44 		}
45 		i++;
46 	}
47 	sigprocmask(SIG_UNBLOCK, &set, NULL);
48 	if (received != i)
49 		errx(1, "error, signal lost");
50 	printf("OK\n");
51 }
52