t_getitimer.c revision 1.1 1 1.1 jruoho /* $NetBSD: t_getitimer.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
2 1.1 jruoho
3 1.1 jruoho /*-
4 1.1 jruoho * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.1 jruoho * All rights reserved.
6 1.1 jruoho *
7 1.1 jruoho * This code is derived from software contributed to The NetBSD Foundation
8 1.1 jruoho * by Jukka Ruohonen.
9 1.1 jruoho *
10 1.1 jruoho * Redistribution and use in source and binary forms, with or without
11 1.1 jruoho * modification, are permitted provided that the following conditions
12 1.1 jruoho * are met:
13 1.1 jruoho * 1. Redistributions of source code must retain the above copyright
14 1.1 jruoho * notice, this list of conditions and the following disclaimer.
15 1.1 jruoho * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jruoho * notice, this list of conditions and the following disclaimer in the
17 1.1 jruoho * documentation and/or other materials provided with the distribution.
18 1.1 jruoho *
19 1.1 jruoho * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 jruoho * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 jruoho * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 jruoho * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 jruoho * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 jruoho * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 jruoho * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 jruoho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 jruoho * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 jruoho * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 jruoho * POSSIBILITY OF SUCH DAMAGE.
30 1.1 jruoho */
31 1.1 jruoho #include <sys/cdefs.h>
32 1.1 jruoho __RCSID("$NetBSD: t_getitimer.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $");
33 1.1 jruoho
34 1.1 jruoho #include <sys/time.h>
35 1.1 jruoho
36 1.1 jruoho #include <atf-c.h>
37 1.1 jruoho #include <errno.h>
38 1.1 jruoho #include <limits.h>
39 1.1 jruoho #include <signal.h>
40 1.1 jruoho #include <string.h>
41 1.1 jruoho #include <unistd.h>
42 1.1 jruoho
43 1.1 jruoho static bool fail;
44 1.1 jruoho static void sighandler(int);
45 1.1 jruoho
46 1.1 jruoho static void
47 1.1 jruoho sighandler(int signo)
48 1.1 jruoho {
49 1.1 jruoho
50 1.1 jruoho if (signo == SIGALRM || signo == SIGVTALRM)
51 1.1 jruoho fail = false;
52 1.1 jruoho }
53 1.1 jruoho
54 1.1 jruoho ATF_TC(getitimer_empty);
55 1.1 jruoho ATF_TC_HEAD(getitimer_empty, tc)
56 1.1 jruoho {
57 1.1 jruoho atf_tc_set_md_var(tc, "descr", "getitimer(2) before setitimer(2)");
58 1.1 jruoho }
59 1.1 jruoho
60 1.1 jruoho ATF_TC_BODY(getitimer_empty, tc)
61 1.1 jruoho {
62 1.1 jruoho struct itimerval it;
63 1.1 jruoho
64 1.1 jruoho /*
65 1.1 jruoho * Verify that the passed structure remains
66 1.1 jruoho * empty after calling getitimer(2) but before
67 1.1 jruoho * actually arming the timer with setitimer(2).
68 1.1 jruoho */
69 1.1 jruoho (void)memset(&it, 0, sizeof(struct itimerval));
70 1.1 jruoho
71 1.1 jruoho ATF_REQUIRE(getitimer(ITIMER_REAL, &it) == 0);
72 1.1 jruoho
73 1.1 jruoho if (it.it_value.tv_sec != 0 || it.it_value.tv_usec != 0)
74 1.1 jruoho goto fail;
75 1.1 jruoho
76 1.1 jruoho ATF_REQUIRE(getitimer(ITIMER_VIRTUAL, &it) == 0);
77 1.1 jruoho
78 1.1 jruoho if (it.it_value.tv_sec != 0 || it.it_value.tv_usec != 0)
79 1.1 jruoho goto fail;
80 1.1 jruoho
81 1.1 jruoho ATF_REQUIRE(getitimer(ITIMER_PROF, &it) == 0);
82 1.1 jruoho
83 1.1 jruoho if (it.it_value.tv_sec != 0 || it.it_value.tv_usec != 0)
84 1.1 jruoho goto fail;
85 1.1 jruoho
86 1.1 jruoho return;
87 1.1 jruoho
88 1.1 jruoho fail:
89 1.1 jruoho atf_tc_fail("getitimer(2) modfied the timer before it was armed");
90 1.1 jruoho }
91 1.1 jruoho
92 1.1 jruoho ATF_TC(getitimer_err);
93 1.1 jruoho ATF_TC_HEAD(getitimer_err, tc)
94 1.1 jruoho {
95 1.1 jruoho atf_tc_set_md_var(tc, "descr", "Test errors from getitimer(2)");
96 1.1 jruoho }
97 1.1 jruoho
98 1.1 jruoho ATF_TC_BODY(getitimer_err, tc)
99 1.1 jruoho {
100 1.1 jruoho struct itimerval it;
101 1.1 jruoho
102 1.1 jruoho errno = 0;
103 1.1 jruoho ATF_REQUIRE_ERRNO(EINVAL, getitimer(-1, &it) == -1);
104 1.1 jruoho
105 1.1 jruoho errno = 0;
106 1.1 jruoho ATF_REQUIRE_ERRNO(EINVAL, getitimer(INT_MAX, &it) == -1);
107 1.1 jruoho
108 1.1 jruoho errno = 0;
109 1.1 jruoho ATF_REQUIRE_ERRNO(EFAULT, getitimer(ITIMER_REAL, (void *)-1) == -1);
110 1.1 jruoho }
111 1.1 jruoho
112 1.1 jruoho ATF_TC(setitimer_basic);
113 1.1 jruoho ATF_TC_HEAD(setitimer_basic, tc)
114 1.1 jruoho {
115 1.1 jruoho atf_tc_set_md_var(tc, "descr", "A basic test of setitimer(2)");
116 1.1 jruoho }
117 1.1 jruoho
118 1.1 jruoho ATF_TC_BODY(setitimer_basic, tc)
119 1.1 jruoho {
120 1.1 jruoho struct itimerval it;
121 1.1 jruoho
122 1.1 jruoho it.it_value.tv_sec = 0;
123 1.1 jruoho it.it_value.tv_usec = 100;
124 1.1 jruoho
125 1.1 jruoho it.it_interval.tv_sec = 0;
126 1.1 jruoho it.it_interval.tv_usec = 0;
127 1.1 jruoho
128 1.1 jruoho fail = true;
129 1.1 jruoho
130 1.1 jruoho ATF_REQUIRE(signal(SIGALRM, sighandler) != SIG_ERR);
131 1.1 jruoho ATF_REQUIRE(setitimer(ITIMER_REAL, &it, NULL) == 0);
132 1.1 jruoho
133 1.1 jruoho /*
134 1.1 jruoho * Although the interaction between
135 1.1 jruoho * setitimer(2) and sleep(3) can be
136 1.1 jruoho * unspecified, it is assumed that one
137 1.1 jruoho * second suspension will be enough for
138 1.1 jruoho * the timer to fire.
139 1.1 jruoho */
140 1.1 jruoho (void)sleep(1);
141 1.1 jruoho
142 1.1 jruoho if (fail != false)
143 1.1 jruoho atf_tc_fail("timer did not fire");
144 1.1 jruoho }
145 1.1 jruoho
146 1.1 jruoho ATF_TC(setitimer_err);
147 1.1 jruoho ATF_TC_HEAD(setitimer_err, tc)
148 1.1 jruoho {
149 1.1 jruoho atf_tc_set_md_var(tc, "descr", "Test errors from setitimer(2)");
150 1.1 jruoho }
151 1.1 jruoho
152 1.1 jruoho ATF_TC_BODY(setitimer_err, tc)
153 1.1 jruoho {
154 1.1 jruoho struct itimerval it, ot;
155 1.1 jruoho
156 1.1 jruoho errno = 0;
157 1.1 jruoho ATF_REQUIRE_ERRNO(EINVAL, setitimer(-1, &it, &ot) == -1);
158 1.1 jruoho
159 1.1 jruoho errno = 0;
160 1.1 jruoho ATF_REQUIRE_ERRNO(EINVAL, setitimer(INT_MAX, &it, &ot) == -1);
161 1.1 jruoho
162 1.1 jruoho /*
163 1.1 jruoho * This fails incorrectly with EPERM.
164 1.1 jruoho */
165 1.1 jruoho atf_tc_expect_fail("PR standards/44927");
166 1.1 jruoho
167 1.1 jruoho errno = 0;
168 1.1 jruoho ATF_REQUIRE_ERRNO(EFAULT, setitimer(ITIMER_REAL,(void*)-1, &ot) == -1);
169 1.1 jruoho }
170 1.1 jruoho
171 1.1 jruoho ATF_TC(setitimer_old);
172 1.1 jruoho ATF_TC_HEAD(setitimer_old, tc)
173 1.1 jruoho {
174 1.1 jruoho atf_tc_set_md_var(tc, "descr", "Test old values from setitimer(2)");
175 1.1 jruoho }
176 1.1 jruoho
177 1.1 jruoho ATF_TC_BODY(setitimer_old, tc)
178 1.1 jruoho {
179 1.1 jruoho struct itimerval it, ot;
180 1.1 jruoho
181 1.1 jruoho /*
182 1.1 jruoho * Make two calls; the second one
183 1.1 jruoho * should store the old values.
184 1.1 jruoho */
185 1.1 jruoho it.it_value.tv_sec = 4;
186 1.1 jruoho it.it_value.tv_usec = 3;
187 1.1 jruoho
188 1.1 jruoho it.it_interval.tv_sec = 0;
189 1.1 jruoho it.it_interval.tv_usec = 0;
190 1.1 jruoho
191 1.1 jruoho ATF_REQUIRE(setitimer(ITIMER_REAL, &it, &ot) == 0);
192 1.1 jruoho
193 1.1 jruoho it.it_value.tv_sec = 2;
194 1.1 jruoho it.it_value.tv_usec = 1;
195 1.1 jruoho
196 1.1 jruoho it.it_interval.tv_sec = 0;
197 1.1 jruoho it.it_interval.tv_usec = 0;
198 1.1 jruoho
199 1.1 jruoho ATF_REQUIRE(setitimer(ITIMER_REAL, &it, &ot) == 0);
200 1.1 jruoho
201 1.1 jruoho if (ot.it_value.tv_sec != 4 || ot.it_value.tv_usec != 3)
202 1.1 jruoho atf_tc_fail("setitimer(2) did not store old values");
203 1.1 jruoho }
204 1.1 jruoho
205 1.1 jruoho ATF_TP_ADD_TCS(tp)
206 1.1 jruoho {
207 1.1 jruoho
208 1.1 jruoho ATF_TP_ADD_TC(tp, getitimer_empty);
209 1.1 jruoho ATF_TP_ADD_TC(tp, getitimer_err);
210 1.1 jruoho ATF_TP_ADD_TC(tp, setitimer_basic);
211 1.1 jruoho ATF_TP_ADD_TC(tp, setitimer_err);
212 1.1 jruoho ATF_TP_ADD_TC(tp, setitimer_old);
213 1.1 jruoho
214 1.1 jruoho return atf_no_error();
215 1.1 jruoho }
216