xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_sigqueue.c (revision 9c73efe89c234855b7c7d705171488a62291181a)
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 
47 static void	handler(int, siginfo_t *, void *);
48 
49 #define VALUE (int)0xc001dad1
50 static int value;
51 
52 static void
53 #ifdef __FreeBSD__
54 handler(int signo __unused, siginfo_t *info __unused, void *data __unused)
55 #else
56 handler(int signo, siginfo_t *info, void *data)
57 #endif
58 {
59 	value = info->si_value.sival_int;
60 	kill(0, SIGINFO);
61 }
62 
63 ATF_TC(sigqueue_basic);
64 ATF_TC_HEAD(sigqueue_basic, tc)
65 {
66 	atf_tc_set_md_var(tc, "descr", "Checks sigqueue(3) sigval delivery");
67 }
68 
69 ATF_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 
98 ATF_TC(sigqueue_err);
99 ATF_TC_HEAD(sigqueue_err, tc)
100 {
101 	atf_tc_set_md_var(tc, "descr", "Test errors from sigqueue(3)");
102 }
103 
104 ATF_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 
112 static 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 
122 static sig_atomic_t count = 0;
123 static int delivered[CNT];
124 
125 static void
126 myhandler(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 
134 static int
135 asc(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  */
146 static size_t
147 sigorder(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 	size_t i, j;
155 	for (i = 0, j = 0; i < len - 1; i++) {
156 		if (ordered[i] >= SIGRTMIN)
157 			continue;
158 		if (j == 0)
159 			j = i + 1;
160 		while (ordered[i] == ordered[j] && j < len)
161 			j++;
162 		if (j == len)
163 			break;
164 		ordered[i + 1] = ordered[j];
165 	}
166 	return i + 1;
167 }
168 
169 ATF_TC(sigqueue_rt);
170 ATF_TC_HEAD(sigqueue_rt, tc)
171 {
172 	atf_tc_set_md_var(tc, "descr", "Test queuing of real-time signals");
173 }
174 
175 ATF_TC_BODY(sigqueue_rt, tc)
176 {
177 	pid_t pid;
178 	union sigval val;
179 	struct sigaction act;
180 	int ordered[CNT];
181 	struct sigaction oact[CNT];
182 	size_t ndelivered;
183 
184 	ndelivered = sigorder(ordered, signals, CNT);
185 
186 	act.sa_flags = SA_SIGINFO;
187 	act.sa_sigaction = myhandler;
188 	sigemptyset(&act.sa_mask);
189 	for (size_t i = 0; i < ndelivered; i++)
190 		ATF_REQUIRE(sigaction(ordered[i], &act, &oact[i]) != -1);
191 
192 	val.sival_int = 0;
193 	pid = getpid();
194 
195 	sigset_t mask, orig;
196 	sigemptyset(&mask);
197 	for (size_t i = 0; i < CNT; i++)
198 #ifdef __FreeBSD__
199 		if (sigaddset(&mask, signals[i]) == -1)
200 			warn("sigaddset");
201 #else
202 		sigaddset(&mask, signals[i]);
203 #endif
204 
205 	ATF_REQUIRE(sigprocmask(SIG_BLOCK, &mask, &orig) != -1);
206 
207 	for (size_t i = 0; i < CNT; i++)
208 		ATF_REQUIRE(sigqueue(pid, signals[i], val) != -1);
209 
210 	ATF_REQUIRE(sigprocmask(SIG_UNBLOCK, &mask, &orig) != -1);
211 	sleep(1);
212 #ifdef __FreeBSD__
213 	ATF_CHECK_MSG((size_t)count == ndelivered,
214 	    "count %zu != ndelivered %zu", (size_t)count, ndelivered);
215 #else
216 	ATF_REQUIRE_MSG((size_t)count == ndelivered,
217 	    "count %zu != ndelivered %zu", (size_t)count, ndelivered);
218 #endif
219 	for (size_t i = 0; i < ndelivered; i++)
220 		ATF_REQUIRE_MSG(ordered[i] == delivered[i],
221 		    "%zu: ordered %d != delivered %d",
222 		    i, ordered[i], delivered[i]);
223 
224 #ifdef __FreeBSD__
225 	if (count > ndelivered)
226 		for (size_t i = ndelivered; i < count; i++)
227 			printf("Undelivered signal #%zu: %d\n", i, ordered[i]);
228 #endif
229 
230 	for (size_t i = 0; i < ndelivered; i++)
231 		ATF_REQUIRE(sigaction(signals[i], &oact[i], NULL) != -1);
232 }
233 
234 ATF_TP_ADD_TCS(tp)
235 {
236 
237 	ATF_TP_ADD_TC(tp, sigqueue_basic);
238 	ATF_TP_ADD_TC(tp, sigqueue_err);
239 	ATF_TP_ADD_TC(tp, sigqueue_rt);
240 
241 	return atf_no_error();
242 }
243