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