summaryrefslogtreecommitdiffstats
path: root/contrib/netbsd-tests/lib/libc/sys/t_sigqueue.c
blob: e911d42e93a0f7577cf74d31b8727abe0592d330 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* $NetBSD: t_sigqueue.c,v 1.6 2016/08/04 06:43:43 christos Exp $ */

/*-
 * Copyright (c) 2011 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Christos Zoulas.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__RCSID("$NetBSD: t_sigqueue.c,v 1.6 2016/08/04 06:43:43 christos Exp $");

#include <atf-c.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <sched.h>
#include <unistd.h>

#ifdef __FreeBSD__
#include <err.h>
#include <stdio.h>
#endif

static void	handler(int, siginfo_t *, void *);

#define VALUE (int)0xc001dad1
static int value;

static void
#ifdef __FreeBSD__
handler(int signo __unused, siginfo_t *info __unused, void *data __unused)
#else
handler(int signo, siginfo_t *info, void *data)
#endif
{
	value = info->si_value.sival_int;
	kill(0, SIGINFO);
}

ATF_TC(sigqueue_basic);
ATF_TC_HEAD(sigqueue_basic, tc)
{
	atf_tc_set_md_var(tc, "descr", "Checks sigqueue(3) sigval delivery");
}

ATF_TC_BODY(sigqueue_basic, tc)
{
	struct sigaction sa;
	union sigval sv;

	sa.sa_sigaction = handler;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_SIGINFO;

	if (sigaction(SIGUSR1, &sa, NULL) != 0)
		atf_tc_fail("sigaction failed");

	sv.sival_int = VALUE;

#ifdef __FreeBSD__
	/*
	 * From kern_sig.c:
	 * Specification says sigqueue can only send signal to single process.
	 */
	if (sigqueue(getpid(), SIGUSR1, sv) != 0)
#else
	if (sigqueue(0, SIGUSR1, sv) != 0)
#endif
		atf_tc_fail("sigqueue failed");

	sched_yield();
	ATF_REQUIRE_EQ(sv.sival_int, value);
}

ATF_TC(sigqueue_err);
ATF_TC_HEAD(sigqueue_err, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test errors from sigqueue(3)");
}

ATF_TC_BODY(sigqueue_err, tc)
{
	static union sigval sv;

	errno = 0;
	ATF_REQUIRE_ERRNO(EINVAL, sigqueue(getpid(), -1, sv) == -1);
}

static int signals[] = {
	SIGINT, SIGRTMIN + 1, SIGINT, SIGRTMIN + 0, SIGRTMIN + 2,
	SIGQUIT, SIGRTMIN + 1
};
#ifdef __arraycount
#define CNT	__arraycount(signals)
#else
#define CNT	(sizeof(signals) / sizeof(signals[0]))
#endif

static sig_atomic_t count = 0;
static int delivered[CNT];

static void
myhandler(int signo, siginfo_t *info, void *context)
{
	delivered[count++] = signo;
#ifdef __FreeBSD__
	printf("Signal #%zu: signo: %d\n", (size_t)count, signo);
#endif
}

static int
asc(const void *a, const void *b)
{
	const int *ia = a, *ib = b;
	return *ib - *ia;
}

/*
 * given a array of signals to be delivered in tosend of size len
 * place in ordered the signals to be delivered in delivery order
 * and return the number of signals that should be delivered
 */
static size_t
sigorder(int *ordered, const int *tosend, size_t len)
{
	memcpy(ordered, tosend, len * sizeof(*tosend));
	qsort(ordered, len, sizeof(*ordered), asc);
	if (len == 1)
		return len;

#ifdef __FreeBSD__
	/*
	 * Don't dedupe signal numbers (bug 212173)
	 *
	 * Per kib's comment..
	 *
	 * "
	 * OTOH, FreeBSD behaviour is to treat all signals as realtime while
	 * there is no mem shortage and siginfo can be allocated.  In
	 * particular, signals < SIGRTMIN are not collapsed when queued more
	 * than once.
	 * "
	 */

	return len;
#else

	size_t i, j;
	for (i = 0, j = 0; i < len - 1; i++) {
		if (ordered[i] >= SIGRTMIN)
			continue;
		if (j == 0)
			j = i + 1;
		while (ordered[i] == ordered[j] && j < len)
			j++;
		if (j == len)
			break;
		ordered[i + 1] = ordered[j];
	}
	return i + 1;
#endif
}

ATF_TC(sigqueue_rt);
ATF_TC_HEAD(sigqueue_rt, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test queuing of real-time signals");
}

ATF_TC_BODY(sigqueue_rt, tc)
{
	pid_t pid;
	union sigval val;
	struct sigaction act;
	int ordered[CNT];
	struct sigaction oact[CNT];
	size_t ndelivered;

	ndelivered = sigorder(ordered, signals, CNT);

	act.sa_flags = SA_SIGINFO;
	act.sa_sigaction = myhandler;
	sigemptyset(&act.sa_mask);
	for (size_t i = 0; i < ndelivered; i++)
		ATF_REQUIRE(sigaction(ordered[i], &act, &oact[i]) != -1);

	val.sival_int = 0;
	pid = getpid();

	sigset_t mask, orig;
	sigemptyset(&mask);
	for (size_t i = 0; i < CNT; i++)
#ifdef __FreeBSD__
		if (sigaddset(&mask, signals[i]) == -1)
			warn("sigaddset");
#else
		sigaddset(&mask, signals[i]);
#endif

	ATF_REQUIRE(sigprocmask(SIG_BLOCK, &mask, &orig) != -1);
	
	for (size_t i = 0; i < CNT; i++)
		ATF_REQUIRE(sigqueue(pid, signals[i], val) != -1);
	
	ATF_REQUIRE(sigprocmask(SIG_UNBLOCK, &mask, &orig) != -1);
	sleep(1);
#ifdef __FreeBSD__
	ATF_CHECK_MSG((size_t)count == ndelivered,
	    "count %zu != ndelivered %zu", (size_t)count, ndelivered);
#else
	ATF_REQUIRE_MSG((size_t)count == ndelivered,
	    "count %zu != ndelivered %zu", (size_t)count, ndelivered);
#endif
	for (size_t i = 0; i < ndelivered; i++)
		ATF_REQUIRE_MSG(ordered[i] == delivered[i],
		    "%zu: ordered %d != delivered %d",
		    i, ordered[i], delivered[i]);

#ifdef __FreeBSD__
	if (count > ndelivered)
		for (size_t i = ndelivered; i < count; i++)
			printf("Undelivered signal #%zu: %d\n", i, ordered[i]);
#endif

	for (size_t i = 0; i < ndelivered; i++)
		ATF_REQUIRE(sigaction(signals[i], &oact[i], NULL) != -1);
}

ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, sigqueue_basic);
	ATF_TP_ADD_TC(tp, sigqueue_err);
	ATF_TP_ADD_TC(tp, sigqueue_rt);

	return atf_no_error();
}
OpenPOWER on IntegriCloud