1*11be35a1SLionel Sambuc /* $NetBSD: t_timer_create.c,v 1.4 2012/03/18 07:00:52 jruoho Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc * Copyright (c) 2010 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc * All rights reserved.
6*11be35a1SLionel Sambuc *
7*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc * are met:
10*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc *
16*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*11be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*11be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*11be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*11be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*11be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*11be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*11be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*11be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*11be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*11be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
27*11be35a1SLionel Sambuc */
28*11be35a1SLionel Sambuc
29*11be35a1SLionel Sambuc #include <atf-c.h>
30*11be35a1SLionel Sambuc #include <errno.h>
31*11be35a1SLionel Sambuc #include <stdio.h>
32*11be35a1SLionel Sambuc #include <signal.h>
33*11be35a1SLionel Sambuc #include <string.h>
34*11be35a1SLionel Sambuc #include <time.h>
35*11be35a1SLionel Sambuc #include <unistd.h>
36*11be35a1SLionel Sambuc
37*11be35a1SLionel Sambuc static timer_t t;
38*11be35a1SLionel Sambuc static bool fail = true;
39*11be35a1SLionel Sambuc
40*11be35a1SLionel Sambuc static void
timer_signal_handler(int signo,siginfo_t * si,void * osi)41*11be35a1SLionel Sambuc timer_signal_handler(int signo, siginfo_t *si, void *osi)
42*11be35a1SLionel Sambuc {
43*11be35a1SLionel Sambuc timer_t *tp;
44*11be35a1SLionel Sambuc
45*11be35a1SLionel Sambuc tp = si->si_value.sival_ptr;
46*11be35a1SLionel Sambuc
47*11be35a1SLionel Sambuc if (*tp == t && signo == SIGALRM)
48*11be35a1SLionel Sambuc fail = false;
49*11be35a1SLionel Sambuc
50*11be35a1SLionel Sambuc (void)fprintf(stderr, "%s: %s\n", __func__, strsignal(signo));
51*11be35a1SLionel Sambuc }
52*11be35a1SLionel Sambuc
53*11be35a1SLionel Sambuc static void
timer_signal_create(clockid_t cid,bool expire)54*11be35a1SLionel Sambuc timer_signal_create(clockid_t cid, bool expire)
55*11be35a1SLionel Sambuc {
56*11be35a1SLionel Sambuc struct itimerspec tim;
57*11be35a1SLionel Sambuc struct sigaction act;
58*11be35a1SLionel Sambuc struct sigevent evt;
59*11be35a1SLionel Sambuc sigset_t set;
60*11be35a1SLionel Sambuc
61*11be35a1SLionel Sambuc t = 0;
62*11be35a1SLionel Sambuc fail = true;
63*11be35a1SLionel Sambuc
64*11be35a1SLionel Sambuc (void)memset(&evt, 0, sizeof(struct sigevent));
65*11be35a1SLionel Sambuc (void)memset(&act, 0, sizeof(struct sigaction));
66*11be35a1SLionel Sambuc (void)memset(&tim, 0, sizeof(struct itimerspec));
67*11be35a1SLionel Sambuc
68*11be35a1SLionel Sambuc /*
69*11be35a1SLionel Sambuc * Set handler.
70*11be35a1SLionel Sambuc */
71*11be35a1SLionel Sambuc act.sa_flags = SA_SIGINFO;
72*11be35a1SLionel Sambuc act.sa_sigaction = timer_signal_handler;
73*11be35a1SLionel Sambuc
74*11be35a1SLionel Sambuc ATF_REQUIRE(sigemptyset(&set) == 0);
75*11be35a1SLionel Sambuc ATF_REQUIRE(sigemptyset(&act.sa_mask) == 0);
76*11be35a1SLionel Sambuc
77*11be35a1SLionel Sambuc /*
78*11be35a1SLionel Sambuc * Block SIGALRM while configuring the timer.
79*11be35a1SLionel Sambuc */
80*11be35a1SLionel Sambuc ATF_REQUIRE(sigaction(SIGALRM, &act, NULL) == 0);
81*11be35a1SLionel Sambuc ATF_REQUIRE(sigaddset(&set, SIGALRM) == 0);
82*11be35a1SLionel Sambuc ATF_REQUIRE(sigprocmask(SIG_SETMASK, &set, NULL) == 0);
83*11be35a1SLionel Sambuc
84*11be35a1SLionel Sambuc /*
85*11be35a1SLionel Sambuc * Create the timer (SIGEV_SIGNAL).
86*11be35a1SLionel Sambuc */
87*11be35a1SLionel Sambuc evt.sigev_signo = SIGALRM;
88*11be35a1SLionel Sambuc evt.sigev_value.sival_ptr = &t;
89*11be35a1SLionel Sambuc evt.sigev_notify = SIGEV_SIGNAL;
90*11be35a1SLionel Sambuc
91*11be35a1SLionel Sambuc ATF_REQUIRE(timer_create(cid, &evt, &t) == 0);
92*11be35a1SLionel Sambuc
93*11be35a1SLionel Sambuc /*
94*11be35a1SLionel Sambuc * Start the timer. After this, unblock the signal.
95*11be35a1SLionel Sambuc */
96*11be35a1SLionel Sambuc tim.it_value.tv_sec = expire ? 5 : 1;
97*11be35a1SLionel Sambuc tim.it_value.tv_nsec = 0;
98*11be35a1SLionel Sambuc
99*11be35a1SLionel Sambuc ATF_REQUIRE(timer_settime(t, 0, &tim, NULL) == 0);
100*11be35a1SLionel Sambuc
101*11be35a1SLionel Sambuc (void)sigprocmask(SIG_UNBLOCK, &set, NULL);
102*11be35a1SLionel Sambuc (void)sleep(2);
103*11be35a1SLionel Sambuc
104*11be35a1SLionel Sambuc if (expire) {
105*11be35a1SLionel Sambuc if (!fail)
106*11be35a1SLionel Sambuc atf_tc_fail("timer fired too soon");
107*11be35a1SLionel Sambuc } else {
108*11be35a1SLionel Sambuc if (fail)
109*11be35a1SLionel Sambuc atf_tc_fail("timer failed to fire");
110*11be35a1SLionel Sambuc }
111*11be35a1SLionel Sambuc
112*11be35a1SLionel Sambuc ATF_REQUIRE(timer_delete(t) == 0);
113*11be35a1SLionel Sambuc }
114*11be35a1SLionel Sambuc
115*11be35a1SLionel Sambuc ATF_TC(timer_create_err);
ATF_TC_HEAD(timer_create_err,tc)116*11be35a1SLionel Sambuc ATF_TC_HEAD(timer_create_err, tc)
117*11be35a1SLionel Sambuc {
118*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
119*11be35a1SLionel Sambuc "Check errors from timer_create(2) (PR lib/42434");
120*11be35a1SLionel Sambuc }
121*11be35a1SLionel Sambuc
ATF_TC_BODY(timer_create_err,tc)122*11be35a1SLionel Sambuc ATF_TC_BODY(timer_create_err, tc)
123*11be35a1SLionel Sambuc {
124*11be35a1SLionel Sambuc struct sigevent ev;
125*11be35a1SLionel Sambuc
126*11be35a1SLionel Sambuc (void)memset(&ev, 0, sizeof(struct sigevent));
127*11be35a1SLionel Sambuc
128*11be35a1SLionel Sambuc errno = 0;
129*11be35a1SLionel Sambuc ev.sigev_signo = -1;
130*11be35a1SLionel Sambuc ev.sigev_notify = SIGEV_SIGNAL;
131*11be35a1SLionel Sambuc
132*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1);
133*11be35a1SLionel Sambuc
134*11be35a1SLionel Sambuc errno = 0;
135*11be35a1SLionel Sambuc ev.sigev_signo = SIGUSR1;
136*11be35a1SLionel Sambuc ev.sigev_notify = SIGEV_THREAD + 100;
137*11be35a1SLionel Sambuc
138*11be35a1SLionel Sambuc ATF_REQUIRE_ERRNO(EINVAL, timer_create(CLOCK_REALTIME, &ev, &t) == -1);
139*11be35a1SLionel Sambuc }
140*11be35a1SLionel Sambuc
141*11be35a1SLionel Sambuc ATF_TC(timer_create_real);
ATF_TC_HEAD(timer_create_real,tc)142*11be35a1SLionel Sambuc ATF_TC_HEAD(timer_create_real, tc)
143*11be35a1SLionel Sambuc {
144*11be35a1SLionel Sambuc
145*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
146*11be35a1SLionel Sambuc "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
147*11be35a1SLionel Sambuc "SIGEV_SIGNAL");
148*11be35a1SLionel Sambuc }
149*11be35a1SLionel Sambuc
ATF_TC_BODY(timer_create_real,tc)150*11be35a1SLionel Sambuc ATF_TC_BODY(timer_create_real, tc)
151*11be35a1SLionel Sambuc {
152*11be35a1SLionel Sambuc timer_signal_create(CLOCK_REALTIME, false);
153*11be35a1SLionel Sambuc }
154*11be35a1SLionel Sambuc
155*11be35a1SLionel Sambuc ATF_TC(timer_create_mono);
ATF_TC_HEAD(timer_create_mono,tc)156*11be35a1SLionel Sambuc ATF_TC_HEAD(timer_create_mono, tc)
157*11be35a1SLionel Sambuc {
158*11be35a1SLionel Sambuc
159*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
160*11be35a1SLionel Sambuc "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
161*11be35a1SLionel Sambuc "SIGEV_SIGNAL");
162*11be35a1SLionel Sambuc }
163*11be35a1SLionel Sambuc
ATF_TC_BODY(timer_create_mono,tc)164*11be35a1SLionel Sambuc ATF_TC_BODY(timer_create_mono, tc)
165*11be35a1SLionel Sambuc {
166*11be35a1SLionel Sambuc timer_signal_create(CLOCK_MONOTONIC, false);
167*11be35a1SLionel Sambuc }
168*11be35a1SLionel Sambuc
169*11be35a1SLionel Sambuc ATF_TC(timer_create_real_expire);
ATF_TC_HEAD(timer_create_real_expire,tc)170*11be35a1SLionel Sambuc ATF_TC_HEAD(timer_create_real_expire, tc)
171*11be35a1SLionel Sambuc {
172*11be35a1SLionel Sambuc
173*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
174*11be35a1SLionel Sambuc "Checks timer_create(2) with CLOCK_REALTIME and sigevent(3), "
175*11be35a1SLionel Sambuc "SIGEV_SIGNAL, with expiration");
176*11be35a1SLionel Sambuc }
177*11be35a1SLionel Sambuc
ATF_TC_BODY(timer_create_real_expire,tc)178*11be35a1SLionel Sambuc ATF_TC_BODY(timer_create_real_expire, tc)
179*11be35a1SLionel Sambuc {
180*11be35a1SLionel Sambuc timer_signal_create(CLOCK_REALTIME, true);
181*11be35a1SLionel Sambuc }
182*11be35a1SLionel Sambuc
183*11be35a1SLionel Sambuc ATF_TC(timer_create_mono_expire);
ATF_TC_HEAD(timer_create_mono_expire,tc)184*11be35a1SLionel Sambuc ATF_TC_HEAD(timer_create_mono_expire, tc)
185*11be35a1SLionel Sambuc {
186*11be35a1SLionel Sambuc
187*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr",
188*11be35a1SLionel Sambuc "Checks timer_create(2) with CLOCK_MONOTONIC and sigevent(3), "
189*11be35a1SLionel Sambuc "SIGEV_SIGNAL, with expiration");
190*11be35a1SLionel Sambuc }
191*11be35a1SLionel Sambuc
ATF_TC_BODY(timer_create_mono_expire,tc)192*11be35a1SLionel Sambuc ATF_TC_BODY(timer_create_mono_expire, tc)
193*11be35a1SLionel Sambuc {
194*11be35a1SLionel Sambuc timer_signal_create(CLOCK_MONOTONIC, true);
195*11be35a1SLionel Sambuc }
196*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)197*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
198*11be35a1SLionel Sambuc {
199*11be35a1SLionel Sambuc
200*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, timer_create_err);
201*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, timer_create_real);
202*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, timer_create_mono);
203*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, timer_create_real_expire);
204*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, timer_create_mono_expire);
205*11be35a1SLionel Sambuc
206*11be35a1SLionel Sambuc return atf_no_error();
207*11be35a1SLionel Sambuc }
208