summaryrefslogtreecommitdiff
path: root/regress/lib/libc/sys/t_getitimer.c
blob: b18bc75cd08454009da66b74f9b2a0e932cfd7df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*	$OpenBSD: t_getitimer.c,v 1.4 2021/12/13 16:56:48 deraadt Exp $	*/
/* $NetBSD: t_getitimer.c,v 1.3 2019/07/13 12:44:02 gson Exp $ */

/*-
 * Copyright (c) 2011 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jukka Ruohonen.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "macros.h"

#include <sys/time.h>

#include "atf-c.h"
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

static bool	fail;
static void	sighandler(int);

static void
sighandler(int signo)
{

	if (signo == SIGALRM || signo == SIGVTALRM)
		fail = false;
}

ATF_TC(getitimer_empty);
ATF_TC_HEAD(getitimer_empty, tc)
{
	atf_tc_set_md_var(tc, "descr", "getitimer(2) before setitimer(2)");
}

ATF_TC_BODY(getitimer_empty, tc)
{
	struct itimerval it;

	/*
	 * Verify that the passed structure remains
	 * empty after calling getitimer(2) but before
	 * actually arming the timer with setitimer(2).
	 */
	(void)memset(&it, 0, sizeof(struct itimerval));

	ATF_REQUIRE(getitimer(ITIMER_REAL, &it) == 0);

	if (it.it_value.tv_sec != 0 || it.it_value.tv_usec != 0)
		goto fail;

	ATF_REQUIRE(getitimer(ITIMER_VIRTUAL, &it) == 0);

	if (it.it_value.tv_sec != 0 || it.it_value.tv_usec != 0)
		goto fail;

	ATF_REQUIRE(getitimer(ITIMER_PROF, &it) == 0);

	if (it.it_value.tv_sec != 0 || it.it_value.tv_usec != 0)
		goto fail;

	return;

fail:
	atf_tc_fail("getitimer(2) modfied the timer before it was armed");
}

ATF_TC(getitimer_err);
ATF_TC_HEAD(getitimer_err, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test errors from getitimer(2)");
}

ATF_TC_BODY(getitimer_err, tc)
{
	struct itimerval it;

	errno = 0;
	ATF_REQUIRE_ERRNO(EINVAL, getitimer(-1, &it) == -1);

	errno = 0;
	ATF_REQUIRE_ERRNO(EINVAL, getitimer(INT_MAX, &it) == -1);

	errno = 0;
	ATF_REQUIRE_ERRNO(EFAULT, getitimer(ITIMER_REAL, (void *)-1) == -1);
}

ATF_TC(setitimer_basic);
ATF_TC_HEAD(setitimer_basic, tc)
{
	atf_tc_set_md_var(tc, "descr", "A basic test of setitimer(2)");
}

ATF_TC_BODY(setitimer_basic, tc)
{
	struct itimerval it;

	it.it_value.tv_sec = 0;
	it.it_value.tv_usec = 100;

	it.it_interval.tv_sec = 0;
	it.it_interval.tv_usec = 0;

	fail = true;

	ATF_REQUIRE(signal(SIGALRM, sighandler) != SIG_ERR);
	ATF_REQUIRE(setitimer(ITIMER_REAL, &it, NULL) == 0);

	/*
	 * Although the interaction between
	 * setitimer(2) and sleep(3) can be
	 * unspecified, it is assumed that one
	 * second suspension will be enough for
	 * the timer to fire.
	 */
	(void)sleep(1);

	if (fail != false)
		atf_tc_fail("timer did not fire");
}

ATF_TC(setitimer_err);
ATF_TC_HEAD(setitimer_err, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test errors from setitimer(2)"
	    " (PR standards/44927)");
}

ATF_TC_BODY(setitimer_err, tc)
{
	struct itimerval it, ot;

	errno = 0;
	ATF_REQUIRE_ERRNO(EINVAL, setitimer(-1, &it, &ot) == -1);

	errno = 0;
	ATF_REQUIRE_ERRNO(EINVAL, setitimer(INT_MAX, &it, &ot) == -1);

	errno = 0;
	ATF_REQUIRE_ERRNO(EFAULT, setitimer(ITIMER_REAL,(void*)-1, &ot) == -1);
}

ATF_TC(setitimer_old);
ATF_TC_HEAD(setitimer_old, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test old values from setitimer(2)");
}

ATF_TC_BODY(setitimer_old, tc)
{
	struct itimerval it, ot;

	/*
	 * Make two calls; the second one should store the old
	 * timer value which should be the same as that set in
	 * the first call, or slightly less due to time passing
	 * between the two calls.
	 */
	it.it_value.tv_sec = 4;
	it.it_value.tv_usec = 999999;

	it.it_interval.tv_sec = 0;
	it.it_interval.tv_usec = 0;

	ATF_REQUIRE(setitimer(ITIMER_REAL, &it, &ot) == 0);

	it.it_value.tv_sec = 2;
	it.it_value.tv_usec = 1;

	it.it_interval.tv_sec = 0;
	it.it_interval.tv_usec = 0;

	ATF_REQUIRE(setitimer(ITIMER_REAL, &it, &ot) == 0);

	/* Check seconds only as microseconds may have decremented */
	if (ot.it_value.tv_sec != 4)
		atf_tc_fail("setitimer(2) did not store old values");
}

ATF_TP_ADD_TCS(tp)
{

	ATF_TP_ADD_TC(tp, getitimer_empty);
	ATF_TP_ADD_TC(tp, getitimer_err);
	ATF_TP_ADD_TC(tp, setitimer_basic);
	ATF_TP_ADD_TC(tp, setitimer_err);
	ATF_TP_ADD_TC(tp, setitimer_old);

	return atf_no_error();
}