Deleted Added
full compact
t_raise.c (272458) t_raise.c (272910)
1/* $NetBSD: t_raise.c,v 1.5 2011/05/10 12:43:42 jruoho 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 Jukka Ruohonen.
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#include <sys/cdefs.h>
32__RCSID("$NetBSD: t_raise.c,v 1.5 2011/05/10 12:43:42 jruoho Exp $");
33
34#include <atf-c.h>
35
36#include <signal.h>
37#include <string.h>
38#include <time.h>
39#include <unistd.h>
40
41static bool fail;
42static int count;
43static void handler_err(int);
44static void handler_ret(int);
45static void handler_stress(int);
1/* $NetBSD: t_raise.c,v 1.5 2011/05/10 12:43:42 jruoho 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 Jukka Ruohonen.
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#include <sys/cdefs.h>
32__RCSID("$NetBSD: t_raise.c,v 1.5 2011/05/10 12:43:42 jruoho Exp $");
33
34#include <atf-c.h>
35
36#include <signal.h>
37#include <string.h>
38#include <time.h>
39#include <unistd.h>
40
41static bool fail;
42static int count;
43static void handler_err(int);
44static void handler_ret(int);
45static void handler_stress(int);
46#if defined(__FreeBSD__)
47static int sig[] = { SIGALRM, SIGIO, SIGUSR1, SIGUSR2 };
48#else
46static int sig[] = { SIGALRM, SIGIO, SIGUSR1, SIGUSR2, SIGPWR };
49static int sig[] = { SIGALRM, SIGIO, SIGUSR1, SIGUSR2, SIGPWR };
50#endif
47
48static void
49handler_stress(int signo)
50{
51 count++;
52}
53
54static void
55handler_err(int signo)
56{
57 size_t i;
58
59 for (i = 0; i < __arraycount(sig); i++) {
60
61 if (sig[i] == signo) {
62 fail = false;
63 break;
64 }
65 }
66}
67
68static void
69handler_ret(int signo)
70{
71
72 (void)sleep(1);
73
74 fail = false;
75}
76
77ATF_TC(raise_err);
78ATF_TC_HEAD(raise_err, tc)
79{
80 atf_tc_set_md_var(tc, "descr", "Test raise(3) for invalid parameters");
81}
82
83ATF_TC_BODY(raise_err, tc)
84{
85 int i = 0;
86
87 while (i < 10) {
88
89 ATF_REQUIRE(raise(10240 + i) == -1);
90
91 i++;
92 }
93}
94
95ATF_TC(raise_ret);
96ATF_TC_HEAD(raise_ret, tc)
97{
98 atf_tc_set_md_var(tc, "descr", "Test return order of raise(3)");
99}
100
101ATF_TC_BODY(raise_ret, tc)
102{
103 struct sigaction sa;
104
105 fail = true;
106
107 sa.sa_flags = 0;
108 sa.sa_handler = handler_ret;
109
110 /*
111 * Verify that raise(3) does not return
112 * before the signal handler returns.
113 */
114 ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);
115 ATF_REQUIRE(sigaction(SIGUSR1, &sa, 0) == 0);
116 ATF_REQUIRE(raise(SIGUSR1) == 0);
117
118 if (fail != false)
119 atf_tc_fail("raise(3) returned before signal handler");
120}
121
122ATF_TC(raise_sig);
123ATF_TC_HEAD(raise_sig, tc)
124{
125 atf_tc_set_md_var(tc, "descr", "A basic test of raise(3)");
126}
127
128ATF_TC_BODY(raise_sig, tc)
129{
130 struct timespec tv, tr;
131 struct sigaction sa;
132 size_t i;
133
134 for (i = 0; i < __arraycount(sig); i++) {
135
136 (void)memset(&sa, 0, sizeof(struct sigaction));
137
138 fail = true;
139
140 tv.tv_sec = 0;
141 tv.tv_nsec = 2;
142
143 sa.sa_flags = 0;
144 sa.sa_handler = handler_err;
145
146 ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);
147 ATF_REQUIRE(sigaction(sig[i], &sa, 0) == 0);
148
149 ATF_REQUIRE(raise(sig[i]) == 0);
150 ATF_REQUIRE(nanosleep(&tv, &tr) == 0);
151
152 if (fail != false)
153 atf_tc_fail("raise(3) did not raise a signal");
154 }
155}
156
157ATF_TC(raise_stress);
158ATF_TC_HEAD(raise_stress, tc)
159{
160 atf_tc_set_md_var(tc, "descr", "A basic stress test with raise(3)");
161}
162
163ATF_TC_BODY(raise_stress, tc)
164{
165 static const int maxiter = 1000 * 10;
166 struct sigaction sa;
167 int i;
168
169 sa.sa_flags = 0;
170 sa.sa_handler = handler_stress;
171
172 ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);
173 ATF_REQUIRE(sigaction(SIGUSR1, &sa, 0) == 0);
174
175 for (count = i = 0; i < maxiter; i++)
176 (void)raise(SIGUSR1);
177
178 if (count != maxiter)
179 atf_tc_fail("not all signals were catched");
180}
181
182ATF_TP_ADD_TCS(tp)
183{
184 ATF_TP_ADD_TC(tp, raise_err);
185 ATF_TP_ADD_TC(tp, raise_ret);
186 ATF_TP_ADD_TC(tp, raise_sig);
187 ATF_TP_ADD_TC(tp, raise_stress);
188
189 return atf_no_error();
190}
51
52static void
53handler_stress(int signo)
54{
55 count++;
56}
57
58static void
59handler_err(int signo)
60{
61 size_t i;
62
63 for (i = 0; i < __arraycount(sig); i++) {
64
65 if (sig[i] == signo) {
66 fail = false;
67 break;
68 }
69 }
70}
71
72static void
73handler_ret(int signo)
74{
75
76 (void)sleep(1);
77
78 fail = false;
79}
80
81ATF_TC(raise_err);
82ATF_TC_HEAD(raise_err, tc)
83{
84 atf_tc_set_md_var(tc, "descr", "Test raise(3) for invalid parameters");
85}
86
87ATF_TC_BODY(raise_err, tc)
88{
89 int i = 0;
90
91 while (i < 10) {
92
93 ATF_REQUIRE(raise(10240 + i) == -1);
94
95 i++;
96 }
97}
98
99ATF_TC(raise_ret);
100ATF_TC_HEAD(raise_ret, tc)
101{
102 atf_tc_set_md_var(tc, "descr", "Test return order of raise(3)");
103}
104
105ATF_TC_BODY(raise_ret, tc)
106{
107 struct sigaction sa;
108
109 fail = true;
110
111 sa.sa_flags = 0;
112 sa.sa_handler = handler_ret;
113
114 /*
115 * Verify that raise(3) does not return
116 * before the signal handler returns.
117 */
118 ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);
119 ATF_REQUIRE(sigaction(SIGUSR1, &sa, 0) == 0);
120 ATF_REQUIRE(raise(SIGUSR1) == 0);
121
122 if (fail != false)
123 atf_tc_fail("raise(3) returned before signal handler");
124}
125
126ATF_TC(raise_sig);
127ATF_TC_HEAD(raise_sig, tc)
128{
129 atf_tc_set_md_var(tc, "descr", "A basic test of raise(3)");
130}
131
132ATF_TC_BODY(raise_sig, tc)
133{
134 struct timespec tv, tr;
135 struct sigaction sa;
136 size_t i;
137
138 for (i = 0; i < __arraycount(sig); i++) {
139
140 (void)memset(&sa, 0, sizeof(struct sigaction));
141
142 fail = true;
143
144 tv.tv_sec = 0;
145 tv.tv_nsec = 2;
146
147 sa.sa_flags = 0;
148 sa.sa_handler = handler_err;
149
150 ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);
151 ATF_REQUIRE(sigaction(sig[i], &sa, 0) == 0);
152
153 ATF_REQUIRE(raise(sig[i]) == 0);
154 ATF_REQUIRE(nanosleep(&tv, &tr) == 0);
155
156 if (fail != false)
157 atf_tc_fail("raise(3) did not raise a signal");
158 }
159}
160
161ATF_TC(raise_stress);
162ATF_TC_HEAD(raise_stress, tc)
163{
164 atf_tc_set_md_var(tc, "descr", "A basic stress test with raise(3)");
165}
166
167ATF_TC_BODY(raise_stress, tc)
168{
169 static const int maxiter = 1000 * 10;
170 struct sigaction sa;
171 int i;
172
173 sa.sa_flags = 0;
174 sa.sa_handler = handler_stress;
175
176 ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);
177 ATF_REQUIRE(sigaction(SIGUSR1, &sa, 0) == 0);
178
179 for (count = i = 0; i < maxiter; i++)
180 (void)raise(SIGUSR1);
181
182 if (count != maxiter)
183 atf_tc_fail("not all signals were catched");
184}
185
186ATF_TP_ADD_TCS(tp)
187{
188 ATF_TP_ADD_TC(tp, raise_err);
189 ATF_TP_ADD_TC(tp, raise_ret);
190 ATF_TP_ADD_TC(tp, raise_sig);
191 ATF_TP_ADD_TC(tp, raise_stress);
192
193 return atf_no_error();
194}