1*26e01050Schristos /* $NetBSD: t_sigqueue.c,v 1.7 2017/01/13 20:44:10 christos Exp $ */
237cdb633Schristos
337cdb633Schristos /*-
437cdb633Schristos * Copyright (c) 2011 The NetBSD Foundation, Inc.
537cdb633Schristos * All rights reserved.
637cdb633Schristos *
737cdb633Schristos * This code is derived from software contributed to The NetBSD Foundation
837cdb633Schristos * by Christos Zoulas.
937cdb633Schristos *
1037cdb633Schristos * Redistribution and use in source and binary forms, with or without
1137cdb633Schristos * modification, are permitted provided that the following conditions
1237cdb633Schristos * are met:
1337cdb633Schristos * 1. Redistributions of source code must retain the above copyright
1437cdb633Schristos * notice, this list of conditions and the following disclaimer.
1537cdb633Schristos * 2. Redistributions in binary form must reproduce the above copyright
1637cdb633Schristos * notice, this list of conditions and the following disclaimer in the
1737cdb633Schristos * documentation and/or other materials provided with the distribution.
1837cdb633Schristos *
1937cdb633Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2037cdb633Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2137cdb633Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2237cdb633Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2337cdb633Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2437cdb633Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2537cdb633Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2637cdb633Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2737cdb633Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2837cdb633Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2937cdb633Schristos * POSSIBILITY OF SUCH DAMAGE.
3037cdb633Schristos */
3137cdb633Schristos
3237cdb633Schristos #include <sys/cdefs.h>
33*26e01050Schristos __RCSID("$NetBSD: t_sigqueue.c,v 1.7 2017/01/13 20:44:10 christos Exp $");
3437cdb633Schristos
3537cdb633Schristos #include <atf-c.h>
36*26e01050Schristos #include <err.h>
37b238e6b2Sjruoho #include <errno.h>
38b238e6b2Sjruoho #include <signal.h>
39*26e01050Schristos #include <stdio.h>
40b238e6b2Sjruoho #include <stdlib.h>
41b238e6b2Sjruoho #include <sched.h>
42b238e6b2Sjruoho #include <unistd.h>
4337cdb633Schristos
44b238e6b2Sjruoho static void handler(int, siginfo_t *, void *);
4537cdb633Schristos
464e376e05Schristos #define VALUE (int)0xc001dad1
4737cdb633Schristos static int value;
4837cdb633Schristos
4937cdb633Schristos static void
handler(int signo __unused,siginfo_t * info,void * data __unused)50*26e01050Schristos handler(int signo __unused, siginfo_t *info, void *data __unused)
5137cdb633Schristos {
5237cdb633Schristos value = info->si_value.sival_int;
5337cdb633Schristos kill(0, SIGINFO);
5437cdb633Schristos }
5537cdb633Schristos
56b238e6b2Sjruoho ATF_TC(sigqueue_basic);
ATF_TC_HEAD(sigqueue_basic,tc)57b238e6b2Sjruoho ATF_TC_HEAD(sigqueue_basic, tc)
58b238e6b2Sjruoho {
59b238e6b2Sjruoho atf_tc_set_md_var(tc, "descr", "Checks sigqueue(3) sigval delivery");
60b238e6b2Sjruoho }
61b238e6b2Sjruoho
ATF_TC_BODY(sigqueue_basic,tc)62b238e6b2Sjruoho ATF_TC_BODY(sigqueue_basic, tc)
6337cdb633Schristos {
6437cdb633Schristos struct sigaction sa;
6537cdb633Schristos union sigval sv;
6637cdb633Schristos
6737cdb633Schristos sa.sa_sigaction = handler;
6837cdb633Schristos sigemptyset(&sa.sa_mask);
6937cdb633Schristos sa.sa_flags = SA_SIGINFO;
7037cdb633Schristos
71b238e6b2Sjruoho if (sigaction(SIGUSR1, &sa, NULL) != 0)
72b238e6b2Sjruoho atf_tc_fail("sigaction failed");
7337cdb633Schristos
7437cdb633Schristos sv.sival_int = VALUE;
75b238e6b2Sjruoho
76b238e6b2Sjruoho if (sigqueue(0, SIGUSR1, sv) != 0)
77b238e6b2Sjruoho atf_tc_fail("sigqueue failed");
7837cdb633Schristos
7937cdb633Schristos sched_yield();
8037cdb633Schristos ATF_REQUIRE_EQ(sv.sival_int, value);
8137cdb633Schristos }
8237cdb633Schristos
83b238e6b2Sjruoho ATF_TC(sigqueue_err);
ATF_TC_HEAD(sigqueue_err,tc)84b238e6b2Sjruoho ATF_TC_HEAD(sigqueue_err, tc)
85b238e6b2Sjruoho {
86b238e6b2Sjruoho atf_tc_set_md_var(tc, "descr", "Test errors from sigqueue(3)");
87b238e6b2Sjruoho }
88b238e6b2Sjruoho
ATF_TC_BODY(sigqueue_err,tc)89b238e6b2Sjruoho ATF_TC_BODY(sigqueue_err, tc)
90b238e6b2Sjruoho {
91810e0f52Sjoerg static union sigval sv;
92b238e6b2Sjruoho
93b238e6b2Sjruoho errno = 0;
94b238e6b2Sjruoho ATF_REQUIRE_ERRNO(EINVAL, sigqueue(getpid(), -1, sv) == -1);
95b238e6b2Sjruoho }
96b238e6b2Sjruoho
97c10c4abeSchristos static int signals[] = {
98c10c4abeSchristos SIGINT, SIGRTMIN + 1, SIGINT, SIGRTMIN + 0, SIGRTMIN + 2,
99c10c4abeSchristos SIGQUIT, SIGRTMIN + 1
100c10c4abeSchristos };
101c10c4abeSchristos #ifdef __arraycount
102c10c4abeSchristos #define CNT __arraycount(signals)
103c10c4abeSchristos #else
104c10c4abeSchristos #define CNT (sizeof(signals) / sizeof(signals[0]))
105c10c4abeSchristos #endif
106c10c4abeSchristos
107c10c4abeSchristos static sig_atomic_t count = 0;
108c10c4abeSchristos static int delivered[CNT];
109c10c4abeSchristos
110c10c4abeSchristos static void
myhandler(int signo,siginfo_t * info,void * context __unused)111*26e01050Schristos myhandler(int signo, siginfo_t *info, void *context __unused)
112c10c4abeSchristos {
113c10c4abeSchristos delivered[count++] = signo;
114*26e01050Schristos printf("Signal #%zu: signo: %d\n", (size_t)count, signo);
115c10c4abeSchristos }
116c10c4abeSchristos
117c10c4abeSchristos static int
asc(const void * a,const void * b)118c10c4abeSchristos asc(const void *a, const void *b)
119c10c4abeSchristos {
120c10c4abeSchristos const int *ia = a, *ib = b;
121c10c4abeSchristos return *ib - *ia;
122c10c4abeSchristos }
123c10c4abeSchristos
124c10c4abeSchristos /*
125c10c4abeSchristos * given a array of signals to be delivered in tosend of size len
126c10c4abeSchristos * place in ordered the signals to be delivered in delivery order
127c10c4abeSchristos * and return the number of signals that should be delivered
128c10c4abeSchristos */
129c10c4abeSchristos static size_t
sigorder(int * ordered,const int * tosend,size_t len)130c10c4abeSchristos sigorder(int *ordered, const int *tosend, size_t len)
131c10c4abeSchristos {
132c10c4abeSchristos memcpy(ordered, tosend, len * sizeof(*tosend));
133c10c4abeSchristos qsort(ordered, len, sizeof(*ordered), asc);
134c10c4abeSchristos if (len == 1)
135c10c4abeSchristos return len;
136c10c4abeSchristos
137c10c4abeSchristos size_t i, j;
138c10c4abeSchristos for (i = 0, j = 0; i < len - 1; i++) {
139c10c4abeSchristos if (ordered[i] >= SIGRTMIN)
140c10c4abeSchristos continue;
141c10c4abeSchristos if (j == 0)
142c10c4abeSchristos j = i + 1;
143c10c4abeSchristos while (ordered[i] == ordered[j] && j < len)
144c10c4abeSchristos j++;
145c10c4abeSchristos if (j == len)
146c10c4abeSchristos break;
147c10c4abeSchristos ordered[i + 1] = ordered[j];
148c10c4abeSchristos }
149c10c4abeSchristos return i + 1;
150c10c4abeSchristos }
151c10c4abeSchristos
152c10c4abeSchristos ATF_TC(sigqueue_rt);
ATF_TC_HEAD(sigqueue_rt,tc)153c10c4abeSchristos ATF_TC_HEAD(sigqueue_rt, tc)
154c10c4abeSchristos {
155c10c4abeSchristos atf_tc_set_md_var(tc, "descr", "Test queuing of real-time signals");
156c10c4abeSchristos }
157c10c4abeSchristos
ATF_TC_BODY(sigqueue_rt,tc)158c10c4abeSchristos ATF_TC_BODY(sigqueue_rt, tc)
159c10c4abeSchristos {
160c10c4abeSchristos pid_t pid;
161c10c4abeSchristos union sigval val;
162c10c4abeSchristos struct sigaction act;
163c10c4abeSchristos int ordered[CNT];
164c10c4abeSchristos struct sigaction oact[CNT];
165c10c4abeSchristos size_t ndelivered;
166c10c4abeSchristos
167c10c4abeSchristos ndelivered = sigorder(ordered, signals, CNT);
168c10c4abeSchristos
169c10c4abeSchristos act.sa_flags = SA_SIGINFO;
170c10c4abeSchristos act.sa_sigaction = myhandler;
171c10c4abeSchristos sigemptyset(&act.sa_mask);
172c10c4abeSchristos for (size_t i = 0; i < ndelivered; i++)
173c10c4abeSchristos ATF_REQUIRE(sigaction(ordered[i], &act, &oact[i]) != -1);
174c10c4abeSchristos
175c10c4abeSchristos val.sival_int = 0;
176c10c4abeSchristos pid = getpid();
177c10c4abeSchristos
178c10c4abeSchristos sigset_t mask, orig;
179c10c4abeSchristos sigemptyset(&mask);
180c10c4abeSchristos for (size_t i = 0; i < CNT; i++)
181*26e01050Schristos if (sigaddset(&mask, signals[i]) == -1)
182*26e01050Schristos warn("sigaddset");
183c10c4abeSchristos
184c10c4abeSchristos ATF_REQUIRE(sigprocmask(SIG_BLOCK, &mask, &orig) != -1);
185c10c4abeSchristos
186c10c4abeSchristos for (size_t i = 0; i < CNT; i++)
187c10c4abeSchristos ATF_REQUIRE(sigqueue(pid, signals[i], val) != -1);
188c10c4abeSchristos
189c10c4abeSchristos ATF_REQUIRE(sigprocmask(SIG_UNBLOCK, &mask, &orig) != -1);
190c10c4abeSchristos sleep(1);
191*26e01050Schristos ATF_CHECK_MSG((size_t)count == ndelivered,
192c10c4abeSchristos "count %zu != ndelivered %zu", (size_t)count, ndelivered);
193c10c4abeSchristos for (size_t i = 0; i < ndelivered; i++)
194c10c4abeSchristos ATF_REQUIRE_MSG(ordered[i] == delivered[i],
195c10c4abeSchristos "%zu: ordered %d != delivered %d",
196c10c4abeSchristos i, ordered[i], delivered[i]);
197c10c4abeSchristos
198*26e01050Schristos if ((size_t)count > ndelivered)
199*26e01050Schristos for (size_t i = ndelivered; i < (size_t)count; i++)
200*26e01050Schristos printf("Undelivered signal #%zu: %d\n", i, ordered[i]);
201*26e01050Schristos
202c10c4abeSchristos for (size_t i = 0; i < ndelivered; i++)
203c10c4abeSchristos ATF_REQUIRE(sigaction(signals[i], &oact[i], NULL) != -1);
204c10c4abeSchristos }
205c10c4abeSchristos
ATF_TP_ADD_TCS(tp)20637cdb633Schristos ATF_TP_ADD_TCS(tp)
20737cdb633Schristos {
208b238e6b2Sjruoho
209b238e6b2Sjruoho ATF_TP_ADD_TC(tp, sigqueue_basic);
210b238e6b2Sjruoho ATF_TP_ADD_TC(tp, sigqueue_err);
211c10c4abeSchristos ATF_TP_ADD_TC(tp, sigqueue_rt);
21237cdb633Schristos
21337cdb633Schristos return atf_no_error();
21437cdb633Schristos }
215