etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libc / gen / t_raise.c
blobadc032fdd98a95020c000cab11d8c093854da31b
1 /* $NetBSD: t_raise.c,v 1.5 2011/05/10 12:43:42 jruoho Exp $ */
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jukka Ruohonen.
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_raise.c,v 1.5 2011/05/10 12:43:42 jruoho Exp $");
34 #include <atf-c.h>
36 #include <signal.h>
37 #include <string.h>
38 #include <time.h>
39 #include <unistd.h>
41 static bool fail;
42 static int count;
43 static void handler_err(int);
44 static void handler_ret(int);
45 static void handler_stress(int);
46 static int sig[] = { SIGALRM, SIGIO, SIGUSR1, SIGUSR2, SIGPWR };
48 static void
49 handler_stress(int signo)
51 count++;
54 static void
55 handler_err(int signo)
57 size_t i;
59 for (i = 0; i < __arraycount(sig); i++) {
61 if (sig[i] == signo) {
62 fail = false;
63 break;
68 static void
69 handler_ret(int signo)
72 (void)sleep(1);
74 fail = false;
77 ATF_TC(raise_err);
78 ATF_TC_HEAD(raise_err, tc)
80 atf_tc_set_md_var(tc, "descr", "Test raise(3) for invalid parameters");
83 ATF_TC_BODY(raise_err, tc)
85 int i = 0;
87 while (i < 10) {
89 ATF_REQUIRE(raise(10240 + i) == -1);
91 i++;
95 ATF_TC(raise_ret);
96 ATF_TC_HEAD(raise_ret, tc)
98 atf_tc_set_md_var(tc, "descr", "Test return order of raise(3)");
101 ATF_TC_BODY(raise_ret, tc)
103 struct sigaction sa;
105 fail = true;
107 sa.sa_flags = 0;
108 sa.sa_handler = handler_ret;
111 * Verify that raise(3) does not return
112 * before the signal handler returns.
114 ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);
115 ATF_REQUIRE(sigaction(SIGUSR1, &sa, 0) == 0);
116 ATF_REQUIRE(raise(SIGUSR1) == 0);
118 if (fail != false)
119 atf_tc_fail("raise(3) returned before signal handler");
122 ATF_TC(raise_sig);
123 ATF_TC_HEAD(raise_sig, tc)
125 atf_tc_set_md_var(tc, "descr", "A basic test of raise(3)");
128 ATF_TC_BODY(raise_sig, tc)
130 struct timespec tv, tr;
131 struct sigaction sa;
132 size_t i;
134 for (i = 0; i < __arraycount(sig); i++) {
136 (void)memset(&sa, 0, sizeof(struct sigaction));
138 fail = true;
140 tv.tv_sec = 0;
141 tv.tv_nsec = 2;
143 sa.sa_flags = 0;
144 sa.sa_handler = handler_err;
146 ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);
147 ATF_REQUIRE(sigaction(sig[i], &sa, 0) == 0);
149 ATF_REQUIRE(raise(sig[i]) == 0);
150 ATF_REQUIRE(nanosleep(&tv, &tr) == 0);
152 if (fail != false)
153 atf_tc_fail("raise(3) did not raise a signal");
157 ATF_TC(raise_stress);
158 ATF_TC_HEAD(raise_stress, tc)
160 atf_tc_set_md_var(tc, "descr", "A basic stress test with raise(3)");
163 ATF_TC_BODY(raise_stress, tc)
165 static const int maxiter = 1000 * 10;
166 struct sigaction sa;
167 int i;
169 sa.sa_flags = 0;
170 sa.sa_handler = handler_stress;
172 ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);
173 ATF_REQUIRE(sigaction(SIGUSR1, &sa, 0) == 0);
175 for (count = i = 0; i < maxiter; i++)
176 (void)raise(SIGUSR1);
178 if (count != maxiter)
179 atf_tc_fail("not all signals were catched");
182 ATF_TP_ADD_TCS(tp)
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);
189 return atf_no_error();