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);
119 ATF_TC(timer_create_err);
120 ATF_TC_HEAD(timer_create_err, tc)
122 atf_tc_set_md_var(tc, "descr",
123 "Check errors from timer_create(2) (PR lib/42434");
126 ATF_TC_BODY(timer_create_err, tc)
130 (void)memset(&ev, 0, sizeof(struct sigevent));
134 ev.sigev_notify = SIGEV_SIGNAL;
136 ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1);
139 ev.sigev_signo = SIGUSR1;
140 ev.sigev_notify = SIGEV_THREAD + 100;
142 ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1);
145 ATF_TC(timer_create_real);
146 ATF_TC_HEAD(timer_create_real, tc)
149 atf_tc_set_md_var(tc, "descr",
150 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
154 ATF_TC_BODY(timer_create_real, tc)
156 timer_signal_create(CLOCK_REALTIME, false);
159 ATF_TC(timer_create_mono);
160 ATF_TC_HEAD(timer_create_mono, tc)
163 atf_tc_set_md_var(tc, "descr",
164 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
168 ATF_TC_BODY(timer_create_mono, tc)
170 timer_signal_create(CLOCK_MONOTONIC, false);
173 ATF_TC(timer_create_real_expire);
174 ATF_TC_HEAD(timer_create_real_expire, tc)
177 atf_tc_set_md_var(tc, "descr",
178 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
179 "SIGEV_SIGNAL, with expiration");
182 ATF_TC_BODY(timer_create_real_expire, tc)
184 timer_signal_create(CLOCK_REALTIME, true);
187 ATF_TC(timer_create_mono_expire);
188 ATF_TC_HEAD(timer_create_mono_expire, tc)
191 atf_tc_set_md_var(tc, "descr",
192 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
193 "SIGEV_SIGNAL, with expiration");
196 ATF_TC_BODY(timer_create_mono_expire, tc)
198 timer_signal_create(CLOCK_MONOTONIC, true);
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);
210 return atf_no_error();