1*f26cf4cbSkamil /* $NetBSD: t_raise.c,v 1.6 2016/11/03 22:08:31 kamil Exp $ */
2629e716aSjruoho
3629e716aSjruoho /*-
4629e716aSjruoho * Copyright (c) 2011 The NetBSD Foundation, Inc.
5629e716aSjruoho * All rights reserved.
6629e716aSjruoho *
7629e716aSjruoho * This code is derived from software contributed to The NetBSD Foundation
8629e716aSjruoho * by Jukka Ruohonen.
9629e716aSjruoho *
10629e716aSjruoho * Redistribution and use in source and binary forms, with or without
11629e716aSjruoho * modification, are permitted provided that the following conditions
12629e716aSjruoho * are met:
13629e716aSjruoho * 1. Redistributions of source code must retain the above copyright
14629e716aSjruoho * notice, this list of conditions and the following disclaimer.
15629e716aSjruoho * 2. Redistributions in binary form must reproduce the above copyright
16629e716aSjruoho * notice, this list of conditions and the following disclaimer in the
17629e716aSjruoho * documentation and/or other materials provided with the distribution.
18629e716aSjruoho *
19629e716aSjruoho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20629e716aSjruoho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21629e716aSjruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22629e716aSjruoho * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23629e716aSjruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24629e716aSjruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25629e716aSjruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26629e716aSjruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27629e716aSjruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28629e716aSjruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29629e716aSjruoho * POSSIBILITY OF SUCH DAMAGE.
30629e716aSjruoho */
31a873c5cdSjruoho #include <sys/cdefs.h>
32*f26cf4cbSkamil __RCSID("$NetBSD: t_raise.c,v 1.6 2016/11/03 22:08:31 kamil Exp $");
33629e716aSjruoho
34629e716aSjruoho #include <atf-c.h>
35629e716aSjruoho
36629e716aSjruoho #include <signal.h>
37629e716aSjruoho #include <string.h>
38629e716aSjruoho #include <time.h>
39629e716aSjruoho #include <unistd.h>
40629e716aSjruoho
41629e716aSjruoho static bool fail;
4244744829Sjruoho static int count;
43105ad5cdSjruoho static void handler_err(int);
44105ad5cdSjruoho static void handler_ret(int);
4544744829Sjruoho static void handler_stress(int);
46629e716aSjruoho static int sig[] = { SIGALRM, SIGIO, SIGUSR1, SIGUSR2, SIGPWR };
47629e716aSjruoho
48629e716aSjruoho static void
handler_stress(int signo)4944744829Sjruoho handler_stress(int signo)
5044744829Sjruoho {
5144744829Sjruoho count++;
5244744829Sjruoho }
5344744829Sjruoho
5444744829Sjruoho static void
handler_err(int signo)55105ad5cdSjruoho handler_err(int signo)
56629e716aSjruoho {
57629e716aSjruoho size_t i;
58629e716aSjruoho
59629e716aSjruoho for (i = 0; i < __arraycount(sig); i++) {
60629e716aSjruoho
61629e716aSjruoho if (sig[i] == signo) {
62629e716aSjruoho fail = false;
63629e716aSjruoho break;
64629e716aSjruoho }
65629e716aSjruoho }
66629e716aSjruoho }
67629e716aSjruoho
68105ad5cdSjruoho static void
handler_ret(int signo)69105ad5cdSjruoho handler_ret(int signo)
70105ad5cdSjruoho {
71105ad5cdSjruoho
72105ad5cdSjruoho (void)sleep(1);
73105ad5cdSjruoho
74105ad5cdSjruoho fail = false;
75105ad5cdSjruoho }
76105ad5cdSjruoho
7701072bafSjruoho ATF_TC(raise_err);
ATF_TC_HEAD(raise_err,tc)7801072bafSjruoho ATF_TC_HEAD(raise_err, tc)
79629e716aSjruoho {
80629e716aSjruoho atf_tc_set_md_var(tc, "descr", "Test raise(3) for invalid parameters");
81629e716aSjruoho }
82629e716aSjruoho
ATF_TC_BODY(raise_err,tc)8301072bafSjruoho ATF_TC_BODY(raise_err, tc)
84629e716aSjruoho {
85629e716aSjruoho int i = 0;
86629e716aSjruoho
87629e716aSjruoho while (i < 10) {
88629e716aSjruoho
89629e716aSjruoho ATF_REQUIRE(raise(10240 + i) == -1);
90629e716aSjruoho
91629e716aSjruoho i++;
92629e716aSjruoho }
93629e716aSjruoho }
94629e716aSjruoho
95105ad5cdSjruoho ATF_TC(raise_ret);
ATF_TC_HEAD(raise_ret,tc)96105ad5cdSjruoho ATF_TC_HEAD(raise_ret, tc)
97105ad5cdSjruoho {
98105ad5cdSjruoho atf_tc_set_md_var(tc, "descr", "Test return order of raise(3)");
99105ad5cdSjruoho }
100105ad5cdSjruoho
ATF_TC_BODY(raise_ret,tc)101105ad5cdSjruoho ATF_TC_BODY(raise_ret, tc)
102105ad5cdSjruoho {
103105ad5cdSjruoho struct sigaction sa;
104105ad5cdSjruoho
105105ad5cdSjruoho fail = true;
106105ad5cdSjruoho
107105ad5cdSjruoho sa.sa_flags = 0;
108105ad5cdSjruoho sa.sa_handler = handler_ret;
109105ad5cdSjruoho
110105ad5cdSjruoho /*
111105ad5cdSjruoho * Verify that raise(3) does not return
112105ad5cdSjruoho * before the signal handler returns.
113105ad5cdSjruoho */
114105ad5cdSjruoho ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);
115105ad5cdSjruoho ATF_REQUIRE(sigaction(SIGUSR1, &sa, 0) == 0);
116105ad5cdSjruoho ATF_REQUIRE(raise(SIGUSR1) == 0);
117105ad5cdSjruoho
118105ad5cdSjruoho if (fail != false)
119105ad5cdSjruoho atf_tc_fail("raise(3) returned before signal handler");
120105ad5cdSjruoho }
121105ad5cdSjruoho
12201072bafSjruoho ATF_TC(raise_sig);
ATF_TC_HEAD(raise_sig,tc)12301072bafSjruoho ATF_TC_HEAD(raise_sig, tc)
124629e716aSjruoho {
125629e716aSjruoho atf_tc_set_md_var(tc, "descr", "A basic test of raise(3)");
126629e716aSjruoho }
127629e716aSjruoho
ATF_TC_BODY(raise_sig,tc)12801072bafSjruoho ATF_TC_BODY(raise_sig, tc)
129629e716aSjruoho {
130629e716aSjruoho struct timespec tv, tr;
131629e716aSjruoho struct sigaction sa;
132629e716aSjruoho size_t i;
133629e716aSjruoho
134629e716aSjruoho for (i = 0; i < __arraycount(sig); i++) {
135629e716aSjruoho
136629e716aSjruoho (void)memset(&sa, 0, sizeof(struct sigaction));
137629e716aSjruoho
138629e716aSjruoho fail = true;
139629e716aSjruoho
140629e716aSjruoho tv.tv_sec = 0;
141629e716aSjruoho tv.tv_nsec = 2;
142629e716aSjruoho
143629e716aSjruoho sa.sa_flags = 0;
144105ad5cdSjruoho sa.sa_handler = handler_err;
145629e716aSjruoho
146629e716aSjruoho ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);
147629e716aSjruoho ATF_REQUIRE(sigaction(sig[i], &sa, 0) == 0);
148629e716aSjruoho
149629e716aSjruoho ATF_REQUIRE(raise(sig[i]) == 0);
150629e716aSjruoho ATF_REQUIRE(nanosleep(&tv, &tr) == 0);
151629e716aSjruoho
152629e716aSjruoho if (fail != false)
153629e716aSjruoho atf_tc_fail("raise(3) did not raise a signal");
154629e716aSjruoho }
155629e716aSjruoho }
156629e716aSjruoho
15744744829Sjruoho ATF_TC(raise_stress);
ATF_TC_HEAD(raise_stress,tc)15844744829Sjruoho ATF_TC_HEAD(raise_stress, tc)
15944744829Sjruoho {
16044744829Sjruoho atf_tc_set_md_var(tc, "descr", "A basic stress test with raise(3)");
16144744829Sjruoho }
16244744829Sjruoho
ATF_TC_BODY(raise_stress,tc)16344744829Sjruoho ATF_TC_BODY(raise_stress, tc)
16444744829Sjruoho {
16544744829Sjruoho static const int maxiter = 1000 * 10;
16644744829Sjruoho struct sigaction sa;
16744744829Sjruoho int i;
16844744829Sjruoho
16944744829Sjruoho sa.sa_flags = 0;
17044744829Sjruoho sa.sa_handler = handler_stress;
17144744829Sjruoho
17244744829Sjruoho ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);
17344744829Sjruoho ATF_REQUIRE(sigaction(SIGUSR1, &sa, 0) == 0);
17444744829Sjruoho
17544744829Sjruoho for (count = i = 0; i < maxiter; i++)
17644744829Sjruoho (void)raise(SIGUSR1);
17744744829Sjruoho
17844744829Sjruoho if (count != maxiter)
179*f26cf4cbSkamil atf_tc_fail("not all signals were caught");
18044744829Sjruoho }
18144744829Sjruoho
ATF_TP_ADD_TCS(tp)182629e716aSjruoho ATF_TP_ADD_TCS(tp)
183629e716aSjruoho {
18401072bafSjruoho ATF_TP_ADD_TC(tp, raise_err);
185105ad5cdSjruoho ATF_TP_ADD_TC(tp, raise_ret);
18601072bafSjruoho ATF_TP_ADD_TC(tp, raise_sig);
18744744829Sjruoho ATF_TP_ADD_TC(tp, raise_stress);
188629e716aSjruoho
189629e716aSjruoho return atf_no_error();
190629e716aSjruoho }
191