xref: /netbsd-src/tests/lib/libc/sys/t_getitimer.c (revision 3a6e4efaf3e51592f637315b9d8c1d97e3aa92b6)
1 /* $NetBSD: t_getitimer.c,v 1.5 2024/12/19 20:07:16 riastradh Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_getitimer.c,v 1.5 2024/12/19 20:07:16 riastradh Exp $");
33 
34 #include <sys/time.h>
35 
36 #include <atf-c.h>
37 #include <errno.h>
38 #include <limits.h>
39 #include <signal.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include "h_macros.h"
44 
45 static sig_atomic_t	fired;
46 static void		sighandler(int);
47 
48 static void
49 sighandler(int signo)
50 {
51 
52 	if (signo == SIGALRM || signo == SIGVTALRM)
53 		fired = 1;
54 }
55 
56 ATF_TC(getitimer_empty);
57 ATF_TC_HEAD(getitimer_empty, tc)
58 {
59 	atf_tc_set_md_var(tc, "descr", "getitimer(2) before setitimer(2)");
60 }
61 
62 ATF_TC_BODY(getitimer_empty, tc)
63 {
64 	struct itimerval it;
65 
66 	/*
67 	 * Verify that the passed structure remains
68 	 * empty after calling getitimer(2) but before
69 	 * actually arming the timer with setitimer(2).
70 	 */
71 	(void)memset(&it, 0, sizeof(struct itimerval));
72 
73 	ATF_REQUIRE(getitimer(ITIMER_REAL, &it) == 0);
74 
75 	if (it.it_value.tv_sec != 0 || it.it_value.tv_usec != 0)
76 		goto fail;
77 
78 	ATF_REQUIRE(getitimer(ITIMER_VIRTUAL, &it) == 0);
79 
80 	if (it.it_value.tv_sec != 0 || it.it_value.tv_usec != 0)
81 		goto fail;
82 
83 	ATF_REQUIRE(getitimer(ITIMER_PROF, &it) == 0);
84 
85 	if (it.it_value.tv_sec != 0 || it.it_value.tv_usec != 0)
86 		goto fail;
87 
88 	return;
89 
90 fail:
91 	atf_tc_fail("getitimer(2) modified the timer before it was armed");
92 }
93 
94 ATF_TC(getitimer_err);
95 ATF_TC_HEAD(getitimer_err, tc)
96 {
97 	atf_tc_set_md_var(tc, "descr", "Test errors from getitimer(2)");
98 }
99 
100 ATF_TC_BODY(getitimer_err, tc)
101 {
102 	struct itimerval it;
103 
104 	errno = 0;
105 	ATF_REQUIRE_ERRNO(EINVAL, getitimer(-1, &it) == -1);
106 
107 	errno = 0;
108 	ATF_REQUIRE_ERRNO(EINVAL, getitimer(INT_MAX, &it) == -1);
109 
110 	errno = 0;
111 	ATF_REQUIRE_ERRNO(EFAULT, getitimer(ITIMER_REAL, (void *)-1) == -1);
112 }
113 
114 ATF_TC(setitimer_basic);
115 ATF_TC_HEAD(setitimer_basic, tc)
116 {
117 	atf_tc_set_md_var(tc, "descr", "A basic test of setitimer(2)");
118 }
119 
120 ATF_TC_BODY(setitimer_basic, tc)
121 {
122 	struct itimerval it;
123 
124 	it.it_value.tv_sec = 0;
125 	it.it_value.tv_usec = 100;
126 
127 	it.it_interval.tv_sec = 0;
128 	it.it_interval.tv_usec = 0;
129 
130 	fired = 0;
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 (!fired)
144 		atf_tc_fail("timer did not fire");
145 }
146 
147 ATF_TC(setitimer_err);
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 
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);
169 ATF_TC_HEAD(setitimer_old, tc)
170 {
171 	atf_tc_set_md_var(tc, "descr", "Test old values from setitimer(2)");
172 }
173 
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 
205 ATF_TC(setitimer_invalidtime);
206 ATF_TC_HEAD(setitimer_invalidtime, tc)
207 {
208 	atf_tc_set_md_var(tc, "descr", "Test invalid values in setitimer(2)");
209 }
210 
211 ATF_TC_BODY(setitimer_invalidtime, tc)
212 {
213 	const struct itimerval it[] = {
214 		[0] = { .it_value = {-1, 0} },
215 		[1] = { .it_value = {0, -1} },
216 		[2] = { .it_value = {0, 1000001} },
217 		[3] = { .it_value = {1, 0}, .it_interval = {-1, 0} },
218 		[4] = { .it_value = {1, 0}, .it_interval = {0, -1} },
219 		[5] = { .it_value = {1, 0}, .it_interval = {0, 1000001} },
220 	};
221 	sigset_t sigs, mask;
222 	unsigned i;
223 
224 	RL(sigemptyset(&sigs));
225 	RL(sigaddset(&sigs, SIGALRM));
226 	RL(sigprocmask(SIG_BLOCK, &sigs, &mask));
227 
228 	for (i = 0; i < __arraycount(it); i++) {
229 		fprintf(stderr, "case %u\n", i);
230 		ATF_CHECK_ERRNO(EINVAL,
231 		    setitimer(ITIMER_REAL, &it[i], NULL) == -1);
232 	}
233 
234 	/* Wait up to 2sec to make sure no timer got set anyway. */
235 	ATF_CHECK_ERRNO(EAGAIN,
236 	    sigtimedwait(&sigs, NULL, &(const struct timespec){2, 0}) == -1);
237 	RL(sigprocmask(SIG_SETMASK, &mask, NULL));
238 }
239 
240 ATF_TP_ADD_TCS(tp)
241 {
242 
243 	ATF_TP_ADD_TC(tp, getitimer_empty);
244 	ATF_TP_ADD_TC(tp, getitimer_err);
245 	ATF_TP_ADD_TC(tp, setitimer_basic);
246 	ATF_TP_ADD_TC(tp, setitimer_err);
247 	ATF_TP_ADD_TC(tp, setitimer_old);
248 	ATF_TP_ADD_TC(tp, setitimer_invalidtime);
249 
250 	return atf_no_error();
251 }
252