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
115ATF_TC(timer_create_err);
116ATF_TC_HEAD(timer_create_err, tc)
117{
118	atf_tc_set_md_var(tc, "descr",
119	    "Check errors from timer_create(2) (PR lib/42434");
120}
121
122ATF_TC_BODY(timer_create_err, tc)
123{
124	struct sigevent ev;
125
126	(void)memset(&ev, 0, sizeof(struct sigevent));
127
128	errno = 0;
129	ev.sigev_signo = -1;
130	ev.sigev_notify = SIGEV_SIGNAL;
131
132	ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1);
133
134	errno = 0;
135	ev.sigev_signo = SIGUSR1;
136	ev.sigev_notify = SIGEV_THREAD + 100;
137
138	ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1);
139}
140
141ATF_TC(timer_create_real);
142ATF_TC_HEAD(timer_create_real, tc)
143{
144
145	atf_tc_set_md_var(tc, "descr",
146	    "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
147	    "SIGEV_SIGNAL");
148}
149
150ATF_TC_BODY(timer_create_real, tc)
151{
152	timer_signal_create(CLOCK_REALTIME, false);
153}
154
155ATF_TC(timer_create_mono);
156ATF_TC_HEAD(timer_create_mono, tc)
157{
158
159	atf_tc_set_md_var(tc, "descr",
160	    "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
161	    "SIGEV_SIGNAL");
162}
163
164ATF_TC_BODY(timer_create_mono, tc)
165{
166	timer_signal_create(CLOCK_MONOTONIC, false);
167}
168
169ATF_TC(timer_create_real_expire);
170ATF_TC_HEAD(timer_create_real_expire, tc)
171{
172
173	atf_tc_set_md_var(tc, "descr",
174	    "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
175	    "SIGEV_SIGNAL, with expiration");
176}
177
178ATF_TC_BODY(timer_create_real_expire, tc)
179{
180	timer_signal_create(CLOCK_REALTIME, true);
181}
182
183ATF_TC(timer_create_mono_expire);
184ATF_TC_HEAD(timer_create_mono_expire, tc)
185{
186
187	atf_tc_set_md_var(tc, "descr",
188	    "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
189	    "SIGEV_SIGNAL, with expiration");
190}
191
192ATF_TC_BODY(timer_create_mono_expire, tc)
193{
194	timer_signal_create(CLOCK_MONOTONIC, true);
195}
196
197ATF_TP_ADD_TCS(tp)
198{
199
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);
205
206	return atf_no_error();
207}
208