]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/netbsd-tests/lib/libc/sys/t_sigqueue.c
MFhead @ r304232
[FreeBSD/FreeBSD.git] / contrib / netbsd-tests / lib / libc / sys / t_sigqueue.c
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
36 #include <atf-c.h>
37 #include <errno.h>
38 #include <signal.h>
39 #include <stdlib.h>
40 #include <sched.h>
41 #include <unistd.h>
42
43 static void     handler(int, siginfo_t *, void *);
44
45 #define VALUE (int)0xc001dad1
46 static int value;
47
48 static void
49 #ifdef __FreeBSD__
50 handler(int signo __unused, siginfo_t *info __unused, void *data __unused)
51 #else
52 handler(int signo, siginfo_t *info, void *data)
53 #endif
54 {
55         value = info->si_value.sival_int;
56         kill(0, SIGINFO);
57 }
58
59 ATF_TC(sigqueue_basic);
60 ATF_TC_HEAD(sigqueue_basic, tc)
61 {
62         atf_tc_set_md_var(tc, "descr", "Checks sigqueue(3) sigval delivery");
63 }
64
65 ATF_TC_BODY(sigqueue_basic, tc)
66 {
67         struct sigaction sa;
68         union sigval sv;
69
70         sa.sa_sigaction = handler;
71         sigemptyset(&sa.sa_mask);
72         sa.sa_flags = SA_SIGINFO;
73
74         if (sigaction(SIGUSR1, &sa, NULL) != 0)
75                 atf_tc_fail("sigaction failed");
76
77         sv.sival_int = VALUE;
78
79 #ifdef __FreeBSD__
80         /* 
81          * From kern_sig.c:
82          * Specification says sigqueue can only send signal to single process.
83          */
84         if (sigqueue(getpid(), SIGUSR1, sv) != 0)
85 #else
86         if (sigqueue(0, SIGUSR1, sv) != 0)
87 #endif
88                 atf_tc_fail("sigqueue failed");
89
90         sched_yield();
91         ATF_REQUIRE_EQ(sv.sival_int, value);
92 }
93
94 ATF_TC(sigqueue_err);
95 ATF_TC_HEAD(sigqueue_err, tc)
96 {
97         atf_tc_set_md_var(tc, "descr", "Test errors from sigqueue(3)");
98 }
99
100 ATF_TC_BODY(sigqueue_err, tc)
101 {
102         static union sigval sv;
103
104         errno = 0;
105         ATF_REQUIRE_ERRNO(EINVAL, sigqueue(getpid(), -1, sv) == -1);
106 }
107
108 static int signals[] = {
109         SIGINT, SIGRTMIN + 1, SIGINT, SIGRTMIN + 0, SIGRTMIN + 2,
110         SIGQUIT, SIGRTMIN + 1
111 };
112 #ifdef __arraycount
113 #define CNT     __arraycount(signals)
114 #else
115 #define CNT     (sizeof(signals) / sizeof(signals[0]))
116 #endif
117
118 static sig_atomic_t count = 0;
119 static int delivered[CNT];
120
121 static void
122 myhandler(int signo, siginfo_t *info, void *context)
123 {
124         delivered[count++] = signo;
125 }
126
127 static int
128 asc(const void *a, const void *b)
129 {
130         const int *ia = a, *ib = b;
131         return *ib - *ia;
132 }
133
134 /*
135  * given a array of signals to be delivered in tosend of size len
136  * place in ordered the signals to be delivered in delivery order
137  * and return the number of signals that should be delivered
138  */
139 static size_t
140 sigorder(int *ordered, const int *tosend, size_t len)
141 {
142         memcpy(ordered, tosend, len * sizeof(*tosend));
143         qsort(ordered, len, sizeof(*ordered), asc);
144         if (len == 1)
145                 return len;
146
147         size_t i, j;
148         for (i = 0, j = 0; i < len - 1; i++) {
149                 if (ordered[i] >= SIGRTMIN)
150                         continue;
151                 if (j == 0)
152                         j = i + 1;
153                 while (ordered[i] == ordered[j] && j < len)
154                         j++;
155                 if (j == len)
156                         break;
157                 ordered[i + 1] = ordered[j];
158         }
159         return i + 1;
160 }
161
162 ATF_TC(sigqueue_rt);
163 ATF_TC_HEAD(sigqueue_rt, tc)
164 {
165         atf_tc_set_md_var(tc, "descr", "Test queuing of real-time signals");
166 }
167
168 ATF_TC_BODY(sigqueue_rt, tc)
169 {
170         pid_t pid;
171         union sigval val;
172         struct sigaction act;
173         int ordered[CNT];
174         struct sigaction oact[CNT];
175         size_t ndelivered;
176
177         ndelivered = sigorder(ordered, signals, CNT);
178
179         act.sa_flags = SA_SIGINFO;
180         act.sa_sigaction = myhandler;
181         sigemptyset(&act.sa_mask);
182         for (size_t i = 0; i < ndelivered; i++)
183                 ATF_REQUIRE(sigaction(ordered[i], &act, &oact[i]) != -1);
184
185         val.sival_int = 0;
186         pid = getpid();
187
188         sigset_t mask, orig;
189         sigemptyset(&mask);
190         for (size_t i = 0; i < CNT; i++)
191                 sigaddset(&mask, signals[i]);
192
193         ATF_REQUIRE(sigprocmask(SIG_BLOCK, &mask, &orig) != -1);
194         
195         for (size_t i = 0; i < CNT; i++)
196                 ATF_REQUIRE(sigqueue(pid, signals[i], val) != -1);
197         
198         ATF_REQUIRE(sigprocmask(SIG_UNBLOCK, &mask, &orig) != -1);
199         sleep(1);
200         ATF_REQUIRE_MSG((size_t)count == ndelivered,
201             "count %zu != ndelivered %zu", (size_t)count, ndelivered);
202         for (size_t i = 0; i < ndelivered; i++)
203                 ATF_REQUIRE_MSG(ordered[i] == delivered[i],
204                     "%zu: ordered %d != delivered %d",
205                     i, ordered[i], delivered[i]);
206
207         for (size_t i = 0; i < ndelivered; i++)
208                 ATF_REQUIRE(sigaction(signals[i], &oact[i], NULL) != -1);
209 }
210
211 ATF_TP_ADD_TCS(tp)
212 {
213
214         ATF_TP_ADD_TC(tp, sigqueue_basic);
215         ATF_TP_ADD_TC(tp, sigqueue_err);
216         ATF_TP_ADD_TC(tp, sigqueue_rt);
217
218         return atf_no_error();
219 }