t_timer_create.c revision 314817
1/*	$NetBSD: t_timer_create.c,v 1.5 2017/01/16 16:32:13 christos 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
37static timer_t t;
38static bool fail = true;
39
40static void
41timer_signal_handler(int signo, siginfo_t *si, void *osi __unused)
42{
43	timer_t *tp;
44
45	tp = si->si_value.sival_ptr;
46
47	if (*tp == t && signo == SIGALRM)
48		fail = false;
49
50	(void)fprintf(stderr, "%s: %s\n", __func__, strsignal(signo));
51}
52
53static void
54timer_signal_create(clockid_t cid, bool expire)
55{
56	struct itimerspec tim;
57	struct sigaction act;
58	struct sigevent evt;
59	sigset_t set;
60
61	t = 0;
62	fail = true;
63
64	(void)memset(&evt, 0, sizeof(struct sigevent));
65	(void)memset(&act, 0, sizeof(struct sigaction));
66	(void)memset(&tim, 0, sizeof(struct itimerspec));
67
68	/*
69	 * Set handler.
70	 */
71	act.sa_flags = SA_SIGINFO;
72	act.sa_sigaction = timer_signal_handler;
73
74	ATF_REQUIRE(sigemptyset(&set) == 0);
75	ATF_REQUIRE(sigemptyset(&act.sa_mask) == 0);
76
77	/*
78	 * Block SIGALRM while configuring the timer.
79	 */
80	ATF_REQUIRE(sigaction(SIGALRM, &act, NULL) == 0);
81	ATF_REQUIRE(sigaddset(&set, SIGALRM) == 0);
82	ATF_REQUIRE(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
83
84	/*
85	 * Create the timer (SIGEV_SIGNAL).
86	 */
87	evt.sigev_signo = SIGALRM;
88	evt.sigev_value.sival_ptr = &t;
89	evt.sigev_notify = SIGEV_SIGNAL;
90
91	ATF_REQUIRE(timer_create(cid, &evt, &t) == 0);
92
93	/*
94	 * Start the timer. After this, unblock the signal.
95	 */
96	tim.it_value.tv_sec = expire ? 5 : 1;
97	tim.it_value.tv_nsec = 0;
98
99	ATF_REQUIRE(timer_settime(t, 0, &tim, NULL) == 0);
100
101	(void)sigprocmask(SIG_UNBLOCK, &set, NULL);
102	(void)sleep(2);
103
104	if (expire) {
105		if (!fail)
106			atf_tc_fail("timer fired too soon");
107	} else {
108		if (fail)
109			atf_tc_fail("timer failed to fire");
110	}
111
112	ATF_REQUIRE(timer_delete(t) == 0);
113}
114
115#ifdef __FreeBSD__
116static void
117timer_callback(union sigval value)
118{
119	timer_t *tp;
120
121	tp = value.sival_ptr;
122
123	if (*tp == t)
124		fail = false;
125}
126
127static void
128timer_thread_create(clockid_t cid, bool expire)
129{
130	struct itimerspec tim;
131	struct sigevent evt;
132
133	t = 0;
134	fail = true;
135
136	(void)memset(&evt, 0, sizeof(struct sigevent));
137	(void)memset(&tim, 0, sizeof(struct itimerspec));
138
139	/*
140	 * Create the timer (SIGEV_THREAD).
141	 */
142	evt.sigev_notify_function = timer_callback;
143	evt.sigev_value.sival_ptr = &t;
144	evt.sigev_notify = SIGEV_THREAD;
145
146	ATF_REQUIRE(timer_create(cid, &evt, &t) == 0);
147
148	/*
149	 * Start the timer.
150	 */
151	tim.it_value.tv_sec = expire ? 5 : 1;
152	tim.it_value.tv_nsec = 0;
153
154	ATF_REQUIRE(timer_settime(t, 0, &tim, NULL) == 0);
155
156	(void)sleep(2);
157
158	if (expire) {
159		if (!fail)
160			atf_tc_fail("timer fired too soon");
161	} else {
162		if (fail)
163			atf_tc_fail("timer failed to fire");
164	}
165
166	ATF_REQUIRE(timer_delete(t) == 0);
167}
168#endif
169
170ATF_TC(timer_create_err);
171ATF_TC_HEAD(timer_create_err, tc)
172{
173	atf_tc_set_md_var(tc, "descr",
174	    "Check errors from timer_create(2) (PR lib/42434");
175}
176
177ATF_TC_BODY(timer_create_err, tc)
178{
179	struct sigevent ev;
180
181	(void)memset(&ev, 0, sizeof(struct sigevent));
182
183	errno = 0;
184	ev.sigev_signo = -1;
185	ev.sigev_notify = SIGEV_SIGNAL;
186
187	ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1);
188
189	errno = 0;
190	ev.sigev_signo = SIGUSR1;
191	ev.sigev_notify = SIGEV_THREAD + 100;
192
193	ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1);
194}
195
196ATF_TC(timer_create_real);
197ATF_TC_HEAD(timer_create_real, tc)
198{
199
200	atf_tc_set_md_var(tc, "descr",
201	    "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
202	    "SIGEV_SIGNAL");
203}
204
205ATF_TC_BODY(timer_create_real, tc)
206{
207	timer_signal_create(CLOCK_REALTIME, false);
208}
209
210ATF_TC(timer_create_mono);
211ATF_TC_HEAD(timer_create_mono, tc)
212{
213
214	atf_tc_set_md_var(tc, "descr",
215	    "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
216	    "SIGEV_SIGNAL");
217}
218
219ATF_TC_BODY(timer_create_mono, tc)
220{
221	timer_signal_create(CLOCK_MONOTONIC, false);
222}
223
224ATF_TC(timer_create_real_expire);
225ATF_TC_HEAD(timer_create_real_expire, tc)
226{
227
228	atf_tc_set_md_var(tc, "descr",
229	    "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
230	    "SIGEV_SIGNAL, with expiration");
231}
232
233ATF_TC_BODY(timer_create_real_expire, tc)
234{
235	timer_signal_create(CLOCK_REALTIME, true);
236}
237
238ATF_TC(timer_create_mono_expire);
239ATF_TC_HEAD(timer_create_mono_expire, tc)
240{
241
242	atf_tc_set_md_var(tc, "descr",
243	    "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
244	    "SIGEV_SIGNAL, with expiration");
245}
246
247ATF_TC_BODY(timer_create_mono_expire, tc)
248{
249	timer_signal_create(CLOCK_MONOTONIC, true);
250}
251
252ATF_TC(timer_thread_create_real);
253ATF_TC_HEAD(timer_thread_create_real, tc)
254{
255
256	atf_tc_set_md_var(tc, "descr",
257	    "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
258	    "SIGEV_THREAD");
259}
260
261#ifdef __FreeBSD__
262ATF_TC_BODY(timer_thread_create_real, tc)
263{
264	timer_thread_create(CLOCK_REALTIME, false);
265}
266
267ATF_TC(timer_thread_create_mono);
268ATF_TC_HEAD(timer_thread_create_mono, tc)
269{
270
271	atf_tc_set_md_var(tc, "descr",
272	    "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
273	    "SIGEV_THREAD");
274}
275
276ATF_TC_BODY(timer_thread_create_mono, tc)
277{
278	timer_thread_create(CLOCK_MONOTONIC, false);
279}
280
281ATF_TC(timer_thread_create_real_expire);
282ATF_TC_HEAD(timer_thread_create_real_expire, tc)
283{
284
285	atf_tc_set_md_var(tc, "descr",
286	    "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
287	    "SIGEV_THREAD, with expiration");
288}
289
290ATF_TC_BODY(timer_thread_create_real_expire, tc)
291{
292	timer_thread_create(CLOCK_REALTIME, true);
293}
294
295ATF_TC(timer_thread_create_mono_expire);
296ATF_TC_HEAD(timer_thread_create_mono_expire, tc)
297{
298
299	atf_tc_set_md_var(tc, "descr",
300	    "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
301	    "SIGEV_THREAD, with expiration");
302}
303
304ATF_TC_BODY(timer_thread_create_mono_expire, tc)
305{
306	timer_thread_create(CLOCK_MONOTONIC, true);
307}
308#endif
309
310ATF_TP_ADD_TCS(tp)
311{
312
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);
318#ifdef __FreeBSD__
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);
323#endif
324
325	return atf_no_error();
326}
327