xref: /freebsd-src/contrib/netbsd-tests/lib/libc/sys/t_sigqueue.c (revision 63d1fd5970ec814904aa0f4580b10a0d302d08b2)
1 /* $NetBSD: t_sigqueue.c,v 1.7 2017/01/13 20:44:10 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.7 2017/01/13 20:44:10 christos Exp $");
34 
35 #include <atf-c.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <sched.h>
42 #include <unistd.h>
43 
44 #ifdef __FreeBSD__
45 #include <err.h>
46 #include <stdio.h>
47 #endif
48 
49 static void	handler(int, siginfo_t *, void *);
50 
51 #define VALUE (int)0xc001dad1
52 static int value;
53 
54 static void
55 handler(int signo __unused, siginfo_t *info, void *data __unused)
56 {
57 	value = info->si_value.sival_int;
58 	kill(0, SIGINFO);
59 }
60 
61 ATF_TC(sigqueue_basic);
62 ATF_TC_HEAD(sigqueue_basic, tc)
63 {
64 	atf_tc_set_md_var(tc, "descr", "Checks sigqueue(3) sigval delivery");
65 }
66 
67 ATF_TC_BODY(sigqueue_basic, tc)
68 {
69 	struct sigaction sa;
70 	union sigval sv;
71 
72 	sa.sa_sigaction = handler;
73 	sigemptyset(&sa.sa_mask);
74 	sa.sa_flags = SA_SIGINFO;
75 
76 	if (sigaction(SIGUSR1, &sa, NULL) != 0)
77 		atf_tc_fail("sigaction failed");
78 
79 	sv.sival_int = VALUE;
80 
81 #ifdef __FreeBSD__
82 	/*
83 	 * From kern_sig.c:
84 	 * Specification says sigqueue can only send signal to single process.
85 	 */
86 	if (sigqueue(getpid(), SIGUSR1, sv) != 0)
87 #else
88 	if (sigqueue(0, SIGUSR1, sv) != 0)
89 #endif
90 		atf_tc_fail("sigqueue failed");
91 
92 	sched_yield();
93 	ATF_REQUIRE_EQ(sv.sival_int, value);
94 }
95 
96 ATF_TC(sigqueue_err);
97 ATF_TC_HEAD(sigqueue_err, tc)
98 {
99 	atf_tc_set_md_var(tc, "descr", "Test errors from sigqueue(3)");
100 }
101 
102 ATF_TC_BODY(sigqueue_err, tc)
103 {
104 	static union sigval sv;
105 
106 	errno = 0;
107 	ATF_REQUIRE_ERRNO(EINVAL, sigqueue(getpid(), -1, sv) == -1);
108 }
109 
110 static int signals[] = {
111 	SIGINT, SIGRTMIN + 1, SIGINT, SIGRTMIN + 0, SIGRTMIN + 2,
112 	SIGQUIT, SIGRTMIN + 1
113 };
114 #ifdef __arraycount
115 #define CNT	__arraycount(signals)
116 #else
117 #define CNT	(sizeof(signals) / sizeof(signals[0]))
118 #endif
119 
120 static sig_atomic_t count = 0;
121 static int delivered[CNT];
122 
123 static void
124 myhandler(int signo, siginfo_t *info, void *context __unused)
125 {
126 	delivered[count++] = signo;
127 	printf("Signal #%zu: signo: %d\n", (size_t)count, signo);
128 }
129 
130 static int
131 asc(const void *a, const void *b)
132 {
133 	const int *ia = a, *ib = b;
134 	return *ib - *ia;
135 }
136 
137 /*
138  * given a array of signals to be delivered in tosend of size len
139  * place in ordered the signals to be delivered in delivery order
140  * and return the number of signals that should be delivered
141  */
142 static size_t
143 sigorder(int *ordered, const int *tosend, size_t len)
144 {
145 	memcpy(ordered, tosend, len * sizeof(*tosend));
146 	qsort(ordered, len, sizeof(*ordered), asc);
147 	if (len == 1)
148 		return len;
149 
150 #ifdef __FreeBSD__
151 	/*
152 	 * Don't dedupe signal numbers (bug 212173)
153 	 *
154 	 * Per kib's comment..
155 	 *
156 	 * "
157 	 * OTOH, FreeBSD behaviour is to treat all signals as realtime while
158 	 * there is no mem shortage and siginfo can be allocated.  In
159 	 * particular, signals < SIGRTMIN are not collapsed when queued more
160 	 * than once.
161 	 * "
162 	 */
163 
164 	return len;
165 #else
166 
167 	size_t i, j;
168 	for (i = 0, j = 0; i < len - 1; i++) {
169 		if (ordered[i] >= SIGRTMIN)
170 			continue;
171 		if (j == 0)
172 			j = i + 1;
173 		while (ordered[i] == ordered[j] && j < len)
174 			j++;
175 		if (j == len)
176 			break;
177 		ordered[i + 1] = ordered[j];
178 	}
179 	return i + 1;
180 #endif
181 }
182 
183 ATF_TC(sigqueue_rt);
184 ATF_TC_HEAD(sigqueue_rt, tc)
185 {
186 	atf_tc_set_md_var(tc, "descr", "Test queuing of real-time signals");
187 }
188 
189 ATF_TC_BODY(sigqueue_rt, tc)
190 {
191 	pid_t pid;
192 	union sigval val;
193 	struct sigaction act;
194 	int ordered[CNT];
195 	struct sigaction oact[CNT];
196 	size_t ndelivered;
197 
198 	ndelivered = sigorder(ordered, signals, CNT);
199 
200 	act.sa_flags = SA_SIGINFO;
201 	act.sa_sigaction = myhandler;
202 	sigemptyset(&act.sa_mask);
203 	for (size_t i = 0; i < ndelivered; i++)
204 		ATF_REQUIRE(sigaction(ordered[i], &act, &oact[i]) != -1);
205 
206 	val.sival_int = 0;
207 	pid = getpid();
208 
209 	sigset_t mask, orig;
210 	sigemptyset(&mask);
211 	for (size_t i = 0; i < CNT; i++)
212 		if (sigaddset(&mask, signals[i]) == -1)
213 			warn("sigaddset");
214 
215 
216 	ATF_REQUIRE(sigprocmask(SIG_BLOCK, &mask, &orig) != -1);
217 
218 	for (size_t i = 0; i < CNT; i++)
219 		ATF_REQUIRE(sigqueue(pid, signals[i], val) != -1);
220 
221 	ATF_REQUIRE(sigprocmask(SIG_UNBLOCK, &mask, &orig) != -1);
222 	sleep(1);
223 	ATF_CHECK_MSG((size_t)count == ndelivered,
224 	    "count %zu != ndelivered %zu", (size_t)count, ndelivered);
225 
226 	for (size_t i = 0; i < ndelivered; i++)
227 		ATF_REQUIRE_MSG(ordered[i] == delivered[i],
228 		    "%zu: ordered %d != delivered %d",
229 		    i, ordered[i], delivered[i]);
230 
231 	if ((size_t)count > ndelivered)
232 		for (size_t i = ndelivered; i < (size_t)count; i++)
233 			printf("Undelivered signal #%zu: %d\n", i, ordered[i]);
234 
235 	for (size_t i = 0; i < ndelivered; i++)
236 		ATF_REQUIRE(sigaction(signals[i], &oact[i], NULL) != -1);
237 }
238 
239 ATF_TP_ADD_TCS(tp)
240 {
241 
242 	ATF_TP_ADD_TC(tp, sigqueue_basic);
243 	ATF_TP_ADD_TC(tp, sigqueue_err);
244 	ATF_TP_ADD_TC(tp, sigqueue_rt);
245 
246 	return atf_no_error();
247 }
248