xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_timer_create.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
1 /*	$NetBSD: t_timer_create.c,v 1.4 2012/03/18 07:00:52 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <atf-c.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <signal.h>
33 #include <string.h>
34 #include <time.h>
35 #include <unistd.h>
36 
37 static timer_t t;
38 static bool fail = true;
39 
40 static void
41 #ifdef __FreeBSD__
42 timer_signal_handler(int signo, siginfo_t *si, void *osi __unused)
43 #else
44 timer_signal_handler(int signo, siginfo_t *si, void *osi)
45 #endif
46 {
47 	timer_t *tp;
48 
49 	tp = si->si_value.sival_ptr;
50 
51 	if (*tp == t && signo == SIGALRM)
52 		fail = false;
53 
54 	(void)fprintf(stderr, "%s: %s\n", __func__, strsignal(signo));
55 }
56 
57 static void
58 timer_signal_create(clockid_t cid, bool expire)
59 {
60 	struct itimerspec tim;
61 	struct sigaction act;
62 	struct sigevent evt;
63 	sigset_t set;
64 
65 	t = 0;
66 	fail = true;
67 
68 	(void)memset(&evt, 0, sizeof(struct sigevent));
69 	(void)memset(&act, 0, sizeof(struct sigaction));
70 	(void)memset(&tim, 0, sizeof(struct itimerspec));
71 
72 	/*
73 	 * Set handler.
74 	 */
75 	act.sa_flags = SA_SIGINFO;
76 	act.sa_sigaction = timer_signal_handler;
77 
78 	ATF_REQUIRE(sigemptyset(&set) == 0);
79 	ATF_REQUIRE(sigemptyset(&act.sa_mask) == 0);
80 
81 	/*
82 	 * Block SIGALRM while configuring the timer.
83 	 */
84 	ATF_REQUIRE(sigaction(SIGALRM, &act, NULL) == 0);
85 	ATF_REQUIRE(sigaddset(&set, SIGALRM) == 0);
86 	ATF_REQUIRE(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
87 
88 	/*
89 	 * Create the timer (SIGEV_SIGNAL).
90 	 */
91 	evt.sigev_signo = SIGALRM;
92 	evt.sigev_value.sival_ptr = &t;
93 	evt.sigev_notify = SIGEV_SIGNAL;
94 
95 	ATF_REQUIRE(timer_create(cid, &evt, &t) == 0);
96 
97 	/*
98 	 * Start the timer. After this, unblock the signal.
99 	 */
100 	tim.it_value.tv_sec = expire ? 5 : 1;
101 	tim.it_value.tv_nsec = 0;
102 
103 	ATF_REQUIRE(timer_settime(t, 0, &tim, NULL) == 0);
104 
105 	(void)sigprocmask(SIG_UNBLOCK, &set, NULL);
106 	(void)sleep(2);
107 
108 	if (expire) {
109 		if (!fail)
110 			atf_tc_fail("timer fired too soon");
111 	} else {
112 		if (fail)
113 			atf_tc_fail("timer failed to fire");
114 	}
115 
116 	ATF_REQUIRE(timer_delete(t) == 0);
117 }
118 
119 ATF_TC(timer_create_err);
120 ATF_TC_HEAD(timer_create_err, tc)
121 {
122 	atf_tc_set_md_var(tc, "descr",
123 	    "Check errors from timer_create(2) (PR lib/42434");
124 }
125 
126 ATF_TC_BODY(timer_create_err, tc)
127 {
128 	struct sigevent ev;
129 
130 	(void)memset(&ev, 0, sizeof(struct sigevent));
131 
132 	errno = 0;
133 	ev.sigev_signo = -1;
134 	ev.sigev_notify = SIGEV_SIGNAL;
135 
136 	ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1);
137 
138 	errno = 0;
139 	ev.sigev_signo = SIGUSR1;
140 	ev.sigev_notify = SIGEV_THREAD + 100;
141 
142 	ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1);
143 }
144 
145 ATF_TC(timer_create_real);
146 ATF_TC_HEAD(timer_create_real, tc)
147 {
148 
149 	atf_tc_set_md_var(tc, "descr",
150 	    "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
151 	    "SIGEV_SIGNAL");
152 }
153 
154 ATF_TC_BODY(timer_create_real, tc)
155 {
156 	timer_signal_create(CLOCK_REALTIME, false);
157 }
158 
159 ATF_TC(timer_create_mono);
160 ATF_TC_HEAD(timer_create_mono, tc)
161 {
162 
163 	atf_tc_set_md_var(tc, "descr",
164 	    "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
165 	    "SIGEV_SIGNAL");
166 }
167 
168 ATF_TC_BODY(timer_create_mono, tc)
169 {
170 	timer_signal_create(CLOCK_MONOTONIC, false);
171 }
172 
173 ATF_TC(timer_create_real_expire);
174 ATF_TC_HEAD(timer_create_real_expire, tc)
175 {
176 
177 	atf_tc_set_md_var(tc, "descr",
178 	    "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
179 	    "SIGEV_SIGNAL, with expiration");
180 }
181 
182 ATF_TC_BODY(timer_create_real_expire, tc)
183 {
184 	timer_signal_create(CLOCK_REALTIME, true);
185 }
186 
187 ATF_TC(timer_create_mono_expire);
188 ATF_TC_HEAD(timer_create_mono_expire, tc)
189 {
190 
191 	atf_tc_set_md_var(tc, "descr",
192 	    "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
193 	    "SIGEV_SIGNAL, with expiration");
194 }
195 
196 ATF_TC_BODY(timer_create_mono_expire, tc)
197 {
198 	timer_signal_create(CLOCK_MONOTONIC, true);
199 }
200 
201 ATF_TP_ADD_TCS(tp)
202 {
203 
204 	ATF_TP_ADD_TC(tp, timer_create_err);
205 	ATF_TP_ADD_TC(tp, timer_create_real);
206 	ATF_TP_ADD_TC(tp, timer_create_mono);
207 	ATF_TP_ADD_TC(tp, timer_create_real_expire);
208 	ATF_TP_ADD_TC(tp, timer_create_mono_expire);
209 
210 	return atf_no_error();
211 }
212