1 /* $NetBSD: t_sigqueue.c,v 1.6 2016/08/04 06:43:43 christos Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: t_sigqueue.c,v 1.6 2016/08/04 06:43:43 christos Exp $");
47 static void handler(int, siginfo_t *, void *);
49 #define VALUE (int)0xc001dad1
54 handler(int signo __unused, siginfo_t *info __unused, void *data __unused)
56 handler(int signo, siginfo_t *info, void *data)
59 value = info->si_value.sival_int;
63 ATF_TC(sigqueue_basic);
64 ATF_TC_HEAD(sigqueue_basic, tc)
66 atf_tc_set_md_var(tc, "descr", "Checks sigqueue(3) sigval delivery");
69 ATF_TC_BODY(sigqueue_basic, tc)
74 sa.sa_sigaction = handler;
75 sigemptyset(&sa.sa_mask);
76 sa.sa_flags = SA_SIGINFO;
78 if (sigaction(SIGUSR1, &sa, NULL) != 0)
79 atf_tc_fail("sigaction failed");
86 * Specification says sigqueue can only send signal to single process.
88 if (sigqueue(getpid(), SIGUSR1, sv) != 0)
90 if (sigqueue(0, SIGUSR1, sv) != 0)
92 atf_tc_fail("sigqueue failed");
95 ATF_REQUIRE_EQ(sv.sival_int, value);
99 ATF_TC_HEAD(sigqueue_err, tc)
101 atf_tc_set_md_var(tc, "descr", "Test errors from sigqueue(3)");
104 ATF_TC_BODY(sigqueue_err, tc)
106 static union sigval sv;
109 ATF_REQUIRE_ERRNO(EINVAL, sigqueue(getpid(), -1, sv) == -1);
112 static int signals[] = {
113 SIGINT, SIGRTMIN + 1, SIGINT, SIGRTMIN + 0, SIGRTMIN + 2,
114 SIGQUIT, SIGRTMIN + 1
117 #define CNT __arraycount(signals)
119 #define CNT (sizeof(signals) / sizeof(signals[0]))
122 static sig_atomic_t count = 0;
123 static int delivered[CNT];
126 myhandler(int signo, siginfo_t *info, void *context)
128 delivered[count++] = signo;
130 printf("Signal #%zu: signo: %d\n", (size_t)count, signo);
135 asc(const void *a, const void *b)
137 const int *ia = a, *ib = b;
142 * given a array of signals to be delivered in tosend of size len
143 * place in ordered the signals to be delivered in delivery order
144 * and return the number of signals that should be delivered
147 sigorder(int *ordered, const int *tosend, size_t len)
149 memcpy(ordered, tosend, len * sizeof(*tosend));
150 qsort(ordered, len, sizeof(*ordered), asc);
156 * Don't dedupe signal numbers (bug 212173)
158 * Per kib's comment..
161 * OTOH, FreeBSD behaviour is to treat all signals as realtime while
162 * there is no mem shortage and siginfo can be allocated. In
163 * particular, signals < SIGRTMIN are not collapsed when queued more
172 for (i = 0, j = 0; i < len - 1; i++) {
173 if (ordered[i] >= SIGRTMIN)
177 while (ordered[i] == ordered[j] && j < len)
181 ordered[i + 1] = ordered[j];
188 ATF_TC_HEAD(sigqueue_rt, tc)
190 atf_tc_set_md_var(tc, "descr", "Test queuing of real-time signals");
193 ATF_TC_BODY(sigqueue_rt, tc)
197 struct sigaction act;
199 struct sigaction oact[CNT];
202 ndelivered = sigorder(ordered, signals, CNT);
204 act.sa_flags = SA_SIGINFO;
205 act.sa_sigaction = myhandler;
206 sigemptyset(&act.sa_mask);
207 for (size_t i = 0; i < ndelivered; i++)
208 ATF_REQUIRE(sigaction(ordered[i], &act, &oact[i]) != -1);
215 for (size_t i = 0; i < CNT; i++)
217 if (sigaddset(&mask, signals[i]) == -1)
220 sigaddset(&mask, signals[i]);
223 ATF_REQUIRE(sigprocmask(SIG_BLOCK, &mask, &orig) != -1);
225 for (size_t i = 0; i < CNT; i++)
226 ATF_REQUIRE(sigqueue(pid, signals[i], val) != -1);
228 ATF_REQUIRE(sigprocmask(SIG_UNBLOCK, &mask, &orig) != -1);
231 ATF_CHECK_MSG((size_t)count == ndelivered,
232 "count %zu != ndelivered %zu", (size_t)count, ndelivered);
234 ATF_REQUIRE_MSG((size_t)count == ndelivered,
235 "count %zu != ndelivered %zu", (size_t)count, ndelivered);
237 for (size_t i = 0; i < ndelivered; i++)
238 ATF_REQUIRE_MSG(ordered[i] == delivered[i],
239 "%zu: ordered %d != delivered %d",
240 i, ordered[i], delivered[i]);
243 if (count > ndelivered)
244 for (size_t i = ndelivered; i < count; i++)
245 printf("Undelivered signal #%zu: %d\n", i, ordered[i]);
248 for (size_t i = 0; i < ndelivered; i++)
249 ATF_REQUIRE(sigaction(signals[i], &oact[i], NULL) != -1);
255 ATF_TP_ADD_TC(tp, sigqueue_basic);
256 ATF_TP_ADD_TC(tp, sigqueue_err);
257 ATF_TP_ADD_TC(tp, sigqueue_rt);
259 return atf_no_error();