xref: /netbsd-src/tests/lib/libc/gen/t_raise.c (revision cb861154c176d3dcc8ff846f449e3c16a5f5edb5)
1 /*	$NetBSD: t_raise.c,v 1.3 2011/04/05 14:04: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.3 2011/04/05 14:04: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 
41 static bool	fail;
42 static void	handler(int);
43 static int	sig[] = { SIGALRM, SIGIO, SIGUSR1, SIGUSR2, SIGPWR };
44 
45 static void
46 handler(int signo)
47 {
48 	size_t i;
49 
50 	for (i = 0; i < __arraycount(sig); i++) {
51 
52 		if (sig[i] == signo) {
53 			fail = false;
54 			break;
55 		}
56 	}
57 }
58 
59 ATF_TC(raise_err);
60 ATF_TC_HEAD(raise_err, tc)
61 {
62 	atf_tc_set_md_var(tc, "descr", "Test raise(3) for invalid parameters");
63 }
64 
65 ATF_TC_BODY(raise_err, tc)
66 {
67 	int i = 0;
68 
69 	while (i < 10) {
70 
71 		ATF_REQUIRE(raise(10240 + i) == -1);
72 
73 		i++;
74 	}
75 }
76 
77 ATF_TC(raise_sig);
78 ATF_TC_HEAD(raise_sig, tc)
79 {
80 	atf_tc_set_md_var(tc, "descr", "A basic test of raise(3)");
81 }
82 
83 ATF_TC_BODY(raise_sig, tc)
84 {
85 	struct timespec tv, tr;
86 	struct sigaction sa;
87 	size_t i;
88 
89 	for (i = 0; i < __arraycount(sig); i++) {
90 
91 		(void)memset(&sa, 0, sizeof(struct sigaction));
92 
93 		fail = true;
94 
95 		tv.tv_sec = 0;
96 		tv.tv_nsec = 2;
97 
98 		sa.sa_flags = 0;
99 		sa.sa_handler = handler;
100 
101 		ATF_REQUIRE(sigemptyset(&sa.sa_mask) == 0);
102 		ATF_REQUIRE(sigaction(sig[i], &sa, 0) == 0);
103 
104 		ATF_REQUIRE(raise(sig[i]) == 0);
105 		ATF_REQUIRE(nanosleep(&tv, &tr) == 0);
106 
107 		if (fail != false)
108 			atf_tc_fail("raise(3) did not raise a signal");
109 	}
110 }
111 
112 ATF_TP_ADD_TCS(tp)
113 {
114 	ATF_TP_ADD_TC(tp, raise_err);
115 	ATF_TP_ADD_TC(tp, raise_sig);
116 
117 	return atf_no_error();
118 }
119