1 /* $NetBSD: t_timer_create.c,v 1.5 2017/01/16 16:32:13 christos Exp $ */
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
38 static bool fail = true;
41 timer_signal_handler(int signo, siginfo_t *si, void *osi __unused)
45 tp = si->si_value.sival_ptr;
47 if (*tp == t && signo == SIGALRM)
50 (void)fprintf(stderr, "%s: %s\n", __func__, strsignal(signo));
54 timer_signal_create(clockid_t cid, bool expire)
56 struct itimerspec tim;
64 (void)memset(&evt, 0, sizeof(struct sigevent));
65 (void)memset(&act, 0, sizeof(struct sigaction));
66 (void)memset(&tim, 0, sizeof(struct itimerspec));
71 act.sa_flags = SA_SIGINFO;
72 act.sa_sigaction = timer_signal_handler;
74 ATF_REQUIRE(sigemptyset(&set) == 0);
75 ATF_REQUIRE(sigemptyset(&act.sa_mask) == 0);
78 * Block SIGALRM while configuring the timer.
80 ATF_REQUIRE(sigaction(SIGALRM, &act, NULL) == 0);
81 ATF_REQUIRE(sigaddset(&set, SIGALRM) == 0);
82 ATF_REQUIRE(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
85 * Create the timer (SIGEV_SIGNAL).
87 evt.sigev_signo = SIGALRM;
88 evt.sigev_value.sival_ptr = &t;
89 evt.sigev_notify = SIGEV_SIGNAL;
91 ATF_REQUIRE(timer_create(cid, &evt, &t) == 0);
94 * Start the timer. After this, unblock the signal.
96 tim.it_value.tv_sec = expire ? 5 : 1;
97 tim.it_value.tv_nsec = 0;
99 ATF_REQUIRE(timer_settime(t, 0, &tim, NULL) == 0);
101 (void)sigprocmask(SIG_UNBLOCK, &set, NULL);
106 atf_tc_fail("timer fired too soon");
109 atf_tc_fail("timer failed to fire");
112 ATF_REQUIRE(timer_delete(t) == 0);
117 timer_callback(union sigval value)
121 tp = value.sival_ptr;
128 timer_thread_create(clockid_t cid, bool expire)
130 struct itimerspec tim;
136 (void)memset(&evt, 0, sizeof(struct sigevent));
137 (void)memset(&tim, 0, sizeof(struct itimerspec));
140 * Create the timer (SIGEV_THREAD).
142 evt.sigev_notify_function = timer_callback;
143 evt.sigev_value.sival_ptr = &t;
144 evt.sigev_notify = SIGEV_THREAD;
146 ATF_REQUIRE(timer_create(cid, &evt, &t) == 0);
151 tim.it_value.tv_sec = expire ? 5 : 1;
152 tim.it_value.tv_nsec = 0;
154 ATF_REQUIRE(timer_settime(t, 0, &tim, NULL) == 0);
160 atf_tc_fail("timer fired too soon");
163 atf_tc_fail("timer failed to fire");
166 ATF_REQUIRE(timer_delete(t) == 0);
170 ATF_TC(timer_create_err);
171 ATF_TC_HEAD(timer_create_err, tc)
173 atf_tc_set_md_var(tc, "descr",
174 "Check errors from timer_create(2) (PR lib/42434");
177 ATF_TC_BODY(timer_create_err, tc)
181 (void)memset(&ev, 0, sizeof(struct sigevent));
185 ev.sigev_notify = SIGEV_SIGNAL;
187 ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1);
190 ev.sigev_signo = SIGUSR1;
191 ev.sigev_notify = SIGEV_THREAD + 100;
193 ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1);
196 ATF_TC(timer_create_real);
197 ATF_TC_HEAD(timer_create_real, tc)
200 atf_tc_set_md_var(tc, "descr",
201 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
205 ATF_TC_BODY(timer_create_real, tc)
207 timer_signal_create(CLOCK_REALTIME, false);
210 ATF_TC(timer_create_mono);
211 ATF_TC_HEAD(timer_create_mono, tc)
214 atf_tc_set_md_var(tc, "descr",
215 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
219 ATF_TC_BODY(timer_create_mono, tc)
221 timer_signal_create(CLOCK_MONOTONIC, false);
224 ATF_TC(timer_create_real_expire);
225 ATF_TC_HEAD(timer_create_real_expire, tc)
228 atf_tc_set_md_var(tc, "descr",
229 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
230 "SIGEV_SIGNAL, with expiration");
233 ATF_TC_BODY(timer_create_real_expire, tc)
235 timer_signal_create(CLOCK_REALTIME, true);
238 ATF_TC(timer_create_mono_expire);
239 ATF_TC_HEAD(timer_create_mono_expire, tc)
242 atf_tc_set_md_var(tc, "descr",
243 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
244 "SIGEV_SIGNAL, with expiration");
247 ATF_TC_BODY(timer_create_mono_expire, tc)
249 timer_signal_create(CLOCK_MONOTONIC, true);
252 ATF_TC(timer_thread_create_real);
253 ATF_TC_HEAD(timer_thread_create_real, tc)
256 atf_tc_set_md_var(tc, "descr",
257 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
262 ATF_TC_BODY(timer_thread_create_real, tc)
264 timer_thread_create(CLOCK_REALTIME, false);
267 ATF_TC(timer_thread_create_mono);
268 ATF_TC_HEAD(timer_thread_create_mono, tc)
271 atf_tc_set_md_var(tc, "descr",
272 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
276 ATF_TC_BODY(timer_thread_create_mono, tc)
278 timer_thread_create(CLOCK_MONOTONIC, false);
281 ATF_TC(timer_thread_create_real_expire);
282 ATF_TC_HEAD(timer_thread_create_real_expire, tc)
285 atf_tc_set_md_var(tc, "descr",
286 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
287 "SIGEV_THREAD, with expiration");
290 ATF_TC_BODY(timer_thread_create_real_expire, tc)
292 timer_thread_create(CLOCK_REALTIME, true);
295 ATF_TC(timer_thread_create_mono_expire);
296 ATF_TC_HEAD(timer_thread_create_mono_expire, tc)
299 atf_tc_set_md_var(tc, "descr",
300 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
301 "SIGEV_THREAD, with expiration");
304 ATF_TC_BODY(timer_thread_create_mono_expire, tc)
306 timer_thread_create(CLOCK_MONOTONIC, true);
313 ATF_TP_ADD_TC(tp, timer_create_err);
314 ATF_TP_ADD_TC(tp, timer_create_real);
315 ATF_TP_ADD_TC(tp, timer_create_mono);
316 ATF_TP_ADD_TC(tp, timer_create_real_expire);
317 ATF_TP_ADD_TC(tp, timer_create_mono_expire);
319 ATF_TP_ADD_TC(tp, timer_thread_create_real);
320 ATF_TP_ADD_TC(tp, timer_thread_create_mono);
321 ATF_TP_ADD_TC(tp, timer_thread_create_real_expire);
322 ATF_TP_ADD_TC(tp, timer_thread_create_mono_expire);
325 return atf_no_error();