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;
41 timer_signal_handler(int signo
, siginfo_t
*si
, void *osi
)
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);
115 ATF_TC(timer_create_err
);
116 ATF_TC_HEAD(timer_create_err
, tc
)
118 atf_tc_set_md_var(tc
, "descr",
119 "Check errors from timer_create(2) (PR lib/42434");
122 ATF_TC_BODY(timer_create_err
, tc
)
126 (void)memset(&ev
, 0, sizeof(struct sigevent
));
130 ev
.sigev_notify
= SIGEV_SIGNAL
;
132 ATF_REQUIRE_ERRNO(EINVAL
, timer_create(CLOCK_REALTIME
, &ev
, &t
) == -1);
135 ev
.sigev_signo
= SIGUSR1
;
136 ev
.sigev_notify
= SIGEV_THREAD
+ 100;
138 ATF_REQUIRE_ERRNO(EINVAL
, timer_create(CLOCK_REALTIME
, &ev
, &t
) == -1);
141 ATF_TC(timer_create_real
);
142 ATF_TC_HEAD(timer_create_real
, tc
)
145 atf_tc_set_md_var(tc
, "descr",
146 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
150 ATF_TC_BODY(timer_create_real
, tc
)
152 timer_signal_create(CLOCK_REALTIME
, false);
155 ATF_TC(timer_create_mono
);
156 ATF_TC_HEAD(timer_create_mono
, tc
)
159 atf_tc_set_md_var(tc
, "descr",
160 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
164 ATF_TC_BODY(timer_create_mono
, tc
)
166 timer_signal_create(CLOCK_MONOTONIC
, false);
169 ATF_TC(timer_create_real_expire
);
170 ATF_TC_HEAD(timer_create_real_expire
, tc
)
173 atf_tc_set_md_var(tc
, "descr",
174 "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
175 "SIGEV_SIGNAL, with expiration");
178 ATF_TC_BODY(timer_create_real_expire
, tc
)
180 timer_signal_create(CLOCK_REALTIME
, true);
183 ATF_TC(timer_create_mono_expire
);
184 ATF_TC_HEAD(timer_create_mono_expire
, tc
)
187 atf_tc_set_md_var(tc
, "descr",
188 "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
189 "SIGEV_SIGNAL, with expiration");
192 ATF_TC_BODY(timer_create_mono_expire
, tc
)
194 timer_signal_create(CLOCK_MONOTONIC
, true);
200 ATF_TP_ADD_TC(tp
, timer_create_err
);
201 ATF_TP_ADD_TC(tp
, timer_create_real
);
202 ATF_TP_ADD_TC(tp
, timer_create_mono
);
203 ATF_TP_ADD_TC(tp
, timer_create_real_expire
);
204 ATF_TP_ADD_TC(tp
, timer_create_mono_expire
);
206 return atf_no_error();