1 /* $NetBSD: t_getitimer.c,v 1.2 2012/03/22 18:20:46 christos Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_getitimer.c,v 1.2 2012/03/22 18:20:46 christos Exp $");
44 static void sighandler(int);
50 if (signo
== SIGALRM
|| signo
== SIGVTALRM
)
54 ATF_TC(getitimer_empty
);
55 ATF_TC_HEAD(getitimer_empty
, tc
)
57 atf_tc_set_md_var(tc
, "descr", "getitimer(2) before setitimer(2)");
60 ATF_TC_BODY(getitimer_empty
, tc
)
65 * Verify that the passed structure remains
66 * empty after calling getitimer(2) but before
67 * actually arming the timer with setitimer(2).
69 (void)memset(&it
, 0, sizeof(struct itimerval
));
71 ATF_REQUIRE(getitimer(ITIMER_REAL
, &it
) == 0);
73 if (it
.it_value
.tv_sec
!= 0 || it
.it_value
.tv_usec
!= 0)
76 ATF_REQUIRE(getitimer(ITIMER_VIRTUAL
, &it
) == 0);
78 if (it
.it_value
.tv_sec
!= 0 || it
.it_value
.tv_usec
!= 0)
81 ATF_REQUIRE(getitimer(ITIMER_PROF
, &it
) == 0);
83 if (it
.it_value
.tv_sec
!= 0 || it
.it_value
.tv_usec
!= 0)
89 atf_tc_fail("getitimer(2) modfied the timer before it was armed");
92 ATF_TC(getitimer_err
);
93 ATF_TC_HEAD(getitimer_err
, tc
)
95 atf_tc_set_md_var(tc
, "descr", "Test errors from getitimer(2)");
98 ATF_TC_BODY(getitimer_err
, tc
)
103 ATF_REQUIRE_ERRNO(EINVAL
, getitimer(-1, &it
) == -1);
106 ATF_REQUIRE_ERRNO(EINVAL
, getitimer(INT_MAX
, &it
) == -1);
109 ATF_REQUIRE_ERRNO(EFAULT
, getitimer(ITIMER_REAL
, (void *)-1) == -1);
112 ATF_TC(setitimer_basic
);
113 ATF_TC_HEAD(setitimer_basic
, tc
)
115 atf_tc_set_md_var(tc
, "descr", "A basic test of setitimer(2)");
118 ATF_TC_BODY(setitimer_basic
, tc
)
122 it
.it_value
.tv_sec
= 0;
123 it
.it_value
.tv_usec
= 100;
125 it
.it_interval
.tv_sec
= 0;
126 it
.it_interval
.tv_usec
= 0;
130 ATF_REQUIRE(signal(SIGALRM
, sighandler
) != SIG_ERR
);
131 ATF_REQUIRE(setitimer(ITIMER_REAL
, &it
, NULL
) == 0);
134 * Although the interaction between
135 * setitimer(2) and sleep(3) can be
136 * unspecified, it is assumed that one
137 * second suspension will be enough for
143 atf_tc_fail("timer did not fire");
146 ATF_TC(setitimer_err
);
147 ATF_TC_HEAD(setitimer_err
, tc
)
149 atf_tc_set_md_var(tc
, "descr", "Test errors from setitimer(2)"
150 " (PR standards/44927)");
153 ATF_TC_BODY(setitimer_err
, tc
)
155 struct itimerval it
, ot
;
158 ATF_REQUIRE_ERRNO(EINVAL
, setitimer(-1, &it
, &ot
) == -1);
161 ATF_REQUIRE_ERRNO(EINVAL
, setitimer(INT_MAX
, &it
, &ot
) == -1);
164 ATF_REQUIRE_ERRNO(EFAULT
, setitimer(ITIMER_REAL
,(void*)-1, &ot
) == -1);
167 ATF_TC(setitimer_old
);
168 ATF_TC_HEAD(setitimer_old
, tc
)
170 atf_tc_set_md_var(tc
, "descr", "Test old values from setitimer(2)");
173 ATF_TC_BODY(setitimer_old
, tc
)
175 struct itimerval it
, ot
;
178 * Make two calls; the second one
179 * should store the old values.
181 it
.it_value
.tv_sec
= 4;
182 it
.it_value
.tv_usec
= 3;
184 it
.it_interval
.tv_sec
= 0;
185 it
.it_interval
.tv_usec
= 0;
187 ATF_REQUIRE(setitimer(ITIMER_REAL
, &it
, &ot
) == 0);
189 it
.it_value
.tv_sec
= 2;
190 it
.it_value
.tv_usec
= 1;
192 it
.it_interval
.tv_sec
= 0;
193 it
.it_interval
.tv_usec
= 0;
195 ATF_REQUIRE(setitimer(ITIMER_REAL
, &it
, &ot
) == 0);
197 if (ot
.it_value
.tv_sec
!= 4 || ot
.it_value
.tv_usec
!= 3)
198 atf_tc_fail("setitimer(2) did not store old values");
204 ATF_TP_ADD_TC(tp
, getitimer_empty
);
205 ATF_TP_ADD_TC(tp
, getitimer_err
);
206 ATF_TP_ADD_TC(tp
, setitimer_basic
);
207 ATF_TP_ADD_TC(tp
, setitimer_err
);
208 ATF_TP_ADD_TC(tp
, setitimer_old
);
210 return atf_no_error();