1 /* $NetBSD: t_timer_create.c,v 1.4 2012/03/18 07:00:52 jruoho 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;
42 timer_signal_handler(int signo, siginfo_t *si, void *osi __unused)
44 timer_signal_handler(int signo, siginfo_t *si, void *osi)
49 tp = si->si_value.sival_ptr;
51 if (*tp == t && signo == SIGALRM)
54 (void)fprintf(stderr, "%s: %s\n", __func__, strsignal(signo));
58 timer_signal_create(clockid_t cid, bool expire)
60 struct itimerspec tim;
68 (void)memset(&evt, 0, sizeof(struct sigevent));
69 (void)memset(&act, 0, sizeof(struct sigaction));
70 (void)memset(&tim, 0, sizeof(struct itimerspec));
75 act.sa_flags = SA_SIGINFO;
76 act.sa_sigaction = timer_signal_handler;
78 ATF_REQUIRE(sigemptyset(&set) == 0);
79 ATF_REQUIRE(sigemptyset(&act.sa_mask) == 0);
82 * Block SIGALRM while configuring the timer.
84 ATF_REQUIRE(sigaction(SIGALRM, &act, NULL) == 0);
85 ATF_REQUIRE(sigaddset(&set, SIGALRM) == 0);
86 ATF_REQUIRE(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
89 * Create the timer (SIGEV_SIGNAL).
91 evt.sigev_signo = SIGALRM;
92 evt.sigev_value.sival_ptr = &t;
93 evt.sigev_notify = SIGEV_SIGNAL;
95 ATF_REQUIRE(timer_create(cid, &evt, &t) == 0);
98 * Start the timer. After this, unblock the signal.
100 tim.it_value.tv_sec = expire ? 5 : 1;
101 tim.it_value.tv_nsec = 0;
103 ATF_REQUIRE(timer_settime(t, 0, &tim, NULL) == 0);
105 (void)sigprocmask(SIG_UNBLOCK, &set, NULL);
110 atf_tc_fail("timer fired too soon");
113 atf_tc_fail("timer failed to fire");
116 ATF_REQUIRE(timer_delete(t) == 0);
121 timer_callback(union sigval value)
125 tp = value.sival_ptr;
132 timer_thread_create(clockid_t cid, bool expire)
134 struct itimerspec tim;
140 (void)memset(&evt, 0, sizeof(struct sigevent));
141 (void)memset(&tim, 0, sizeof(struct itimerspec));
144 * Create the timer (SIGEV_THREAD).
146 evt.sigev_notify_function = timer_callback;
147 evt.sigev_value.sival_ptr = &t;
148 evt.sigev_notify = SIGEV_THREAD;
150 ATF_REQUIRE(timer_create(cid, &evt, &t) == 0);
155 tim.it_value.tv_sec = expire ? 5 : 1;
156 tim.it_value.tv_nsec = 0;
158 ATF_REQUIRE(timer_settime(t, 0, &tim, NULL) == 0);
164 atf_tc_fail("timer fired too soon");
167 atf_tc_fail("timer failed to fire");
170 ATF_REQUIRE(timer_delete(t) == 0);
174 ATF_TC(timer_create_err);
175 ATF_TC_HEAD(timer_create_err, tc)
177 atf_tc_set_md_var(tc, "descr",
178 "Check errors from timer_create(2) (PR lib/42434");
181 ATF_TC_BODY(timer_create_err, tc)
185 (void)memset(&ev, 0, sizeof(struct sigevent));
189 ev.sigev_notify = SIGEV_SIGNAL;
191 ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1);
194 ev.sigev_signo = SIGUSR1;
195 ev.sigev_notify = SIGEV_THREAD + 100;
197 ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1);
200 ATF_TC(timer_create_real);
201 ATF_TC_HEAD(timer_create_real, tc)
204 atf_tc_set_md_var(tc, "descr",
205 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
209 ATF_TC_BODY(timer_create_real, tc)
211 timer_signal_create(CLOCK_REALTIME, false);
214 ATF_TC(timer_create_mono);
215 ATF_TC_HEAD(timer_create_mono, tc)
218 atf_tc_set_md_var(tc, "descr",
219 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
223 ATF_TC_BODY(timer_create_mono, tc)
225 timer_signal_create(CLOCK_MONOTONIC, false);
228 ATF_TC(timer_create_real_expire);
229 ATF_TC_HEAD(timer_create_real_expire, tc)
232 atf_tc_set_md_var(tc, "descr",
233 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
234 "SIGEV_SIGNAL, with expiration");
237 ATF_TC_BODY(timer_create_real_expire, tc)
239 timer_signal_create(CLOCK_REALTIME, true);
242 ATF_TC(timer_create_mono_expire);
243 ATF_TC_HEAD(timer_create_mono_expire, tc)
246 atf_tc_set_md_var(tc, "descr",
247 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
248 "SIGEV_SIGNAL, with expiration");
251 ATF_TC_BODY(timer_create_mono_expire, tc)
253 timer_signal_create(CLOCK_MONOTONIC, true);
256 ATF_TC(timer_thread_create_real);
257 ATF_TC_HEAD(timer_thread_create_real, tc)
260 atf_tc_set_md_var(tc, "descr",
261 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
266 ATF_TC_BODY(timer_thread_create_real, tc)
268 timer_thread_create(CLOCK_REALTIME, false);
271 ATF_TC(timer_thread_create_mono);
272 ATF_TC_HEAD(timer_thread_create_mono, tc)
275 atf_tc_set_md_var(tc, "descr",
276 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
280 ATF_TC_BODY(timer_thread_create_mono, tc)
282 timer_thread_create(CLOCK_MONOTONIC, false);
285 ATF_TC(timer_thread_create_real_expire);
286 ATF_TC_HEAD(timer_thread_create_real_expire, tc)
289 atf_tc_set_md_var(tc, "descr",
290 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
291 "SIGEV_THREAD, with expiration");
294 ATF_TC_BODY(timer_thread_create_real_expire, tc)
296 timer_thread_create(CLOCK_REALTIME, true);
299 ATF_TC(timer_thread_create_mono_expire);
300 ATF_TC_HEAD(timer_thread_create_mono_expire, tc)
303 atf_tc_set_md_var(tc, "descr",
304 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
305 "SIGEV_THREAD, with expiration");
308 ATF_TC_BODY(timer_thread_create_mono_expire, tc)
310 timer_thread_create(CLOCK_MONOTONIC, true);
317 ATF_TP_ADD_TC(tp, timer_create_err);
318 ATF_TP_ADD_TC(tp, timer_create_real);
319 ATF_TP_ADD_TC(tp, timer_create_mono);
320 ATF_TP_ADD_TC(tp, timer_create_real_expire);
321 ATF_TP_ADD_TC(tp, timer_create_mono_expire);
323 ATF_TP_ADD_TC(tp, timer_thread_create_real);
324 ATF_TP_ADD_TC(tp, timer_thread_create_mono);
325 ATF_TP_ADD_TC(tp, timer_thread_create_real_expire);
326 ATF_TP_ADD_TC(tp, timer_thread_create_mono_expire);
329 return atf_no_error();