t_sigqueue.c revision 313498
1/* $NetBSD: t_sigqueue.c,v 1.6 2016/08/04 06:43:43 christos Exp $ */
2
3/*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
18 *
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.
30 */
31
32#include <sys/cdefs.h>
33__RCSID("$NetBSD: t_sigqueue.c,v 1.6 2016/08/04 06:43:43 christos Exp $");
34
35#include <atf-c.h>
36#include <errno.h>
37#include <signal.h>
38#include <stdlib.h>
39#include <sched.h>
40#include <unistd.h>
41
42#ifdef __FreeBSD__
43#include <err.h>
44#include <stdio.h>
45#endif
46
47static void	handler(int, siginfo_t *, void *);
48
49#define VALUE (int)0xc001dad1
50static int value;
51
52static void
53#ifdef __FreeBSD__
54handler(int signo __unused, siginfo_t *info __unused, void *data __unused)
55#else
56handler(int signo, siginfo_t *info, void *data)
57#endif
58{
59	value = info->si_value.sival_int;
60	kill(0, SIGINFO);
61}
62
63ATF_TC(sigqueue_basic);
64ATF_TC_HEAD(sigqueue_basic, tc)
65{
66	atf_tc_set_md_var(tc, "descr", "Checks sigqueue(3) sigval delivery");
67}
68
69ATF_TC_BODY(sigqueue_basic, tc)
70{
71	struct sigaction sa;
72	union sigval sv;
73
74	sa.sa_sigaction = handler;
75	sigemptyset(&sa.sa_mask);
76	sa.sa_flags = SA_SIGINFO;
77
78	if (sigaction(SIGUSR1, &sa, NULL) != 0)
79		atf_tc_fail("sigaction failed");
80
81	sv.sival_int = VALUE;
82
83#ifdef __FreeBSD__
84	/*
85	 * From kern_sig.c:
86	 * Specification says sigqueue can only send signal to single process.
87	 */
88	if (sigqueue(getpid(), SIGUSR1, sv) != 0)
89#else
90	if (sigqueue(0, SIGUSR1, sv) != 0)
91#endif
92		atf_tc_fail("sigqueue failed");
93
94	sched_yield();
95	ATF_REQUIRE_EQ(sv.sival_int, value);
96}
97
98ATF_TC(sigqueue_err);
99ATF_TC_HEAD(sigqueue_err, tc)
100{
101	atf_tc_set_md_var(tc, "descr", "Test errors from sigqueue(3)");
102}
103
104ATF_TC_BODY(sigqueue_err, tc)
105{
106	static union sigval sv;
107
108	errno = 0;
109	ATF_REQUIRE_ERRNO(EINVAL, sigqueue(getpid(), -1, sv) == -1);
110}
111
112static int signals[] = {
113	SIGINT, SIGRTMIN + 1, SIGINT, SIGRTMIN + 0, SIGRTMIN + 2,
114	SIGQUIT, SIGRTMIN + 1
115};
116#ifdef __arraycount
117#define CNT	__arraycount(signals)
118#else
119#define CNT	(sizeof(signals) / sizeof(signals[0]))
120#endif
121
122static sig_atomic_t count = 0;
123static int delivered[CNT];
124
125static void
126myhandler(int signo, siginfo_t *info, void *context)
127{
128	delivered[count++] = signo;
129#ifdef __FreeBSD__
130	printf("Signal #%zu: signo: %d\n", (size_t)count, signo);
131#endif
132}
133
134static int
135asc(const void *a, const void *b)
136{
137	const int *ia = a, *ib = b;
138	return *ib - *ia;
139}
140
141/*
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
145 */
146static size_t
147sigorder(int *ordered, const int *tosend, size_t len)
148{
149	memcpy(ordered, tosend, len * sizeof(*tosend));
150	qsort(ordered, len, sizeof(*ordered), asc);
151	if (len == 1)
152		return len;
153
154#ifdef __FreeBSD__
155	/*
156	 * Don't dedupe signal numbers (bug 212173)
157	 *
158	 * Per kib's comment..
159	 *
160	 * "
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
164	 * than once.
165	 * "
166	 */
167
168	return len;
169#else
170
171	size_t i, j;
172	for (i = 0, j = 0; i < len - 1; i++) {
173		if (ordered[i] >= SIGRTMIN)
174			continue;
175		if (j == 0)
176			j = i + 1;
177		while (ordered[i] == ordered[j] && j < len)
178			j++;
179		if (j == len)
180			break;
181		ordered[i + 1] = ordered[j];
182	}
183	return i + 1;
184#endif
185}
186
187ATF_TC(sigqueue_rt);
188ATF_TC_HEAD(sigqueue_rt, tc)
189{
190	atf_tc_set_md_var(tc, "descr", "Test queuing of real-time signals");
191}
192
193ATF_TC_BODY(sigqueue_rt, tc)
194{
195	pid_t pid;
196	union sigval val;
197	struct sigaction act;
198	int ordered[CNT];
199	struct sigaction oact[CNT];
200	size_t ndelivered;
201
202	ndelivered = sigorder(ordered, signals, CNT);
203
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);
209
210	val.sival_int = 0;
211	pid = getpid();
212
213	sigset_t mask, orig;
214	sigemptyset(&mask);
215	for (size_t i = 0; i < CNT; i++)
216#ifdef __FreeBSD__
217		if (sigaddset(&mask, signals[i]) == -1)
218			warn("sigaddset");
219#else
220		sigaddset(&mask, signals[i]);
221#endif
222
223	ATF_REQUIRE(sigprocmask(SIG_BLOCK, &mask, &orig) != -1);
224
225	for (size_t i = 0; i < CNT; i++)
226		ATF_REQUIRE(sigqueue(pid, signals[i], val) != -1);
227
228	ATF_REQUIRE(sigprocmask(SIG_UNBLOCK, &mask, &orig) != -1);
229	sleep(1);
230#ifdef __FreeBSD__
231	ATF_CHECK_MSG((size_t)count == ndelivered,
232	    "count %zu != ndelivered %zu", (size_t)count, ndelivered);
233#else
234	ATF_REQUIRE_MSG((size_t)count == ndelivered,
235	    "count %zu != ndelivered %zu", (size_t)count, ndelivered);
236#endif
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]);
241
242#ifdef __FreeBSD__
243	if (count > ndelivered)
244		for (size_t i = ndelivered; i < count; i++)
245			printf("Undelivered signal #%zu: %d\n", i, ordered[i]);
246#endif
247
248	for (size_t i = 0; i < ndelivered; i++)
249		ATF_REQUIRE(sigaction(signals[i], &oact[i], NULL) != -1);
250}
251
252ATF_TP_ADD_TCS(tp)
253{
254
255	ATF_TP_ADD_TC(tp, sigqueue_basic);
256	ATF_TP_ADD_TC(tp, sigqueue_err);
257	ATF_TP_ADD_TC(tp, sigqueue_rt);
258
259	return atf_no_error();
260}
261