xref: /openbsd/regress/lib/libc/sys/t_getitimer.c (revision 09467b48)
1 /*	$OpenBSD: t_getitimer.c,v 1.2 2019/11/22 15:59:53 bluhm 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/cdefs.h>
36 __RCSID("$NetBSD: t_getitimer.c,v 1.2 2012/03/22 18:20:46 christos Exp $");
37 
38 #include <sys/time.h>
39 
40 #include "atf-c.h"
41 #include <errno.h>
42 #include <limits.h>
43 #include <signal.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 static bool	fail;
48 static void	sighandler(int);
49 
50 static void
51 sighandler(int signo)
52 {
53 
54 	if (signo == SIGALRM || signo == SIGVTALRM)
55 		fail = false;
56 }
57 
58 ATF_TC(getitimer_empty);
59 ATF_TC_HEAD(getitimer_empty, tc)
60 {
61 	atf_tc_set_md_var(tc, "descr", "getitimer(2) before setitimer(2)");
62 }
63 
64 ATF_TC_BODY(getitimer_empty, tc)
65 {
66 	struct itimerval it;
67 
68 	/*
69 	 * Verify that the passed structure remains
70 	 * empty after calling getitimer(2) but before
71 	 * actually arming the timer with setitimer(2).
72 	 */
73 	(void)memset(&it, 0, sizeof(struct itimerval));
74 
75 	ATF_REQUIRE(getitimer(ITIMER_REAL, &it) == 0);
76 
77 	if (it.it_value.tv_sec != 0 || it.it_value.tv_usec != 0)
78 		goto fail;
79 
80 	ATF_REQUIRE(getitimer(ITIMER_VIRTUAL, &it) == 0);
81 
82 	if (it.it_value.tv_sec != 0 || it.it_value.tv_usec != 0)
83 		goto fail;
84 
85 	ATF_REQUIRE(getitimer(ITIMER_PROF, &it) == 0);
86 
87 	if (it.it_value.tv_sec != 0 || it.it_value.tv_usec != 0)
88 		goto fail;
89 
90 	return;
91 
92 fail:
93 	atf_tc_fail("getitimer(2) modfied the timer before it was armed");
94 }
95 
96 ATF_TC(getitimer_err);
97 ATF_TC_HEAD(getitimer_err, tc)
98 {
99 	atf_tc_set_md_var(tc, "descr", "Test errors from getitimer(2)");
100 }
101 
102 ATF_TC_BODY(getitimer_err, tc)
103 {
104 	struct itimerval it;
105 
106 	errno = 0;
107 	ATF_REQUIRE_ERRNO(EINVAL, getitimer(-1, &it) == -1);
108 
109 	errno = 0;
110 	ATF_REQUIRE_ERRNO(EINVAL, getitimer(INT_MAX, &it) == -1);
111 
112 	errno = 0;
113 	ATF_REQUIRE_ERRNO(EFAULT, getitimer(ITIMER_REAL, (void *)-1) == -1);
114 }
115 
116 ATF_TC(setitimer_basic);
117 ATF_TC_HEAD(setitimer_basic, tc)
118 {
119 	atf_tc_set_md_var(tc, "descr", "A basic test of setitimer(2)");
120 }
121 
122 ATF_TC_BODY(setitimer_basic, tc)
123 {
124 	struct itimerval it;
125 
126 	it.it_value.tv_sec = 0;
127 	it.it_value.tv_usec = 100;
128 
129 	it.it_interval.tv_sec = 0;
130 	it.it_interval.tv_usec = 0;
131 
132 	fail = true;
133 
134 	ATF_REQUIRE(signal(SIGALRM, sighandler) != SIG_ERR);
135 	ATF_REQUIRE(setitimer(ITIMER_REAL, &it, NULL) == 0);
136 
137 	/*
138 	 * Although the interaction between
139 	 * setitimer(2) and sleep(3) can be
140 	 * unspecified, it is assumed that one
141 	 * second suspension will be enough for
142 	 * the timer to fire.
143 	 */
144 	(void)sleep(1);
145 
146 	if (fail != false)
147 		atf_tc_fail("timer did not fire");
148 }
149 
150 ATF_TC(setitimer_err);
151 ATF_TC_HEAD(setitimer_err, tc)
152 {
153 	atf_tc_set_md_var(tc, "descr", "Test errors from setitimer(2)"
154 	    " (PR standards/44927)");
155 }
156 
157 ATF_TC_BODY(setitimer_err, tc)
158 {
159 	struct itimerval it, ot;
160 
161 	errno = 0;
162 	ATF_REQUIRE_ERRNO(EINVAL, setitimer(-1, &it, &ot) == -1);
163 
164 	errno = 0;
165 	ATF_REQUIRE_ERRNO(EINVAL, setitimer(INT_MAX, &it, &ot) == -1);
166 
167 	errno = 0;
168 	ATF_REQUIRE_ERRNO(EFAULT, setitimer(ITIMER_REAL,(void*)-1, &ot) == -1);
169 }
170 
171 ATF_TC(setitimer_old);
172 ATF_TC_HEAD(setitimer_old, tc)
173 {
174 	atf_tc_set_md_var(tc, "descr", "Test old values from setitimer(2)");
175 }
176 
177 ATF_TC_BODY(setitimer_old, tc)
178 {
179 	struct itimerval it, ot;
180 
181 	/*
182 	 * Make two calls; the second one should store the old
183 	 * timer value which should be the same as that set in
184 	 * the first call, or slightly less due to time passing
185 	 * between the two calls.
186 	 */
187 	it.it_value.tv_sec = 4;
188 	it.it_value.tv_usec = 999999;
189 
190 	it.it_interval.tv_sec = 0;
191 	it.it_interval.tv_usec = 0;
192 
193 	ATF_REQUIRE(setitimer(ITIMER_REAL, &it, &ot) == 0);
194 
195 	it.it_value.tv_sec = 2;
196 	it.it_value.tv_usec = 1;
197 
198 	it.it_interval.tv_sec = 0;
199 	it.it_interval.tv_usec = 0;
200 
201 	ATF_REQUIRE(setitimer(ITIMER_REAL, &it, &ot) == 0);
202 
203 	/* Check seconds only as microseconds may have decremented */
204 	if (ot.it_value.tv_sec != 4)
205 		atf_tc_fail("setitimer(2) did not store old values");
206 }
207 
208 ATF_TP_ADD_TCS(tp)
209 {
210 
211 	ATF_TP_ADD_TC(tp, getitimer_empty);
212 	ATF_TP_ADD_TC(tp, getitimer_err);
213 	ATF_TP_ADD_TC(tp, setitimer_basic);
214 	ATF_TP_ADD_TC(tp, setitimer_err);
215 	ATF_TP_ADD_TC(tp, setitimer_old);
216 
217 	return atf_no_error();
218 }
219