xref: /netbsd-src/tests/lib/libc/sys/t_getrusage.c (revision c9496f6b604074a9451a67df576a5b423068e71e)
1 /* $NetBSD: t_getrusage.c,v 1.5 2017/01/13 20:31:06 christos 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_getrusage.c,v 1.5 2017/01/13 20:31:06 christos Exp $");
33 
34 #include <sys/resource.h>
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 <stdint.h>
42 #include <string.h>
43 
44 static void		work(void);
45 static void		sighandler(int);
46 
47 static const size_t	maxiter = 2000;
48 
49 static void
50 sighandler(int signo __unused)
51 {
52 	/* Nothing. */
53 }
54 
55 static void
56 work(void)
57 {
58 	size_t n = UINT16_MAX * 10;
59 
60 	while (n > 0) {
61 #ifdef __or1k__
62 		 asm volatile("l.nop");	/* Do something. */
63 #elif defined(__ia64__)
64 		 asm volatile("nop 0"); /* Do something. */
65 #else
66 		 asm volatile("nop");	/* Do something. */
67 #endif
68 		 n--;
69 	}
70 }
71 
72 ATF_TC(getrusage_err);
73 ATF_TC_HEAD(getrusage_err, tc)
74 {
75 	atf_tc_set_md_var(tc, "descr", "Test error conditions");
76 }
77 
78 ATF_TC_BODY(getrusage_err, tc)
79 {
80 	struct rusage ru;
81 
82 	errno = 0;
83 
84 	ATF_REQUIRE(getrusage(INT_MAX, &ru) != 0);
85 	ATF_REQUIRE(errno == EINVAL);
86 
87 	errno = 0;
88 
89 	ATF_REQUIRE(getrusage(RUSAGE_SELF, (void *)0) != 0);
90 	ATF_REQUIRE(errno == EFAULT);
91 }
92 
93 ATF_TC(getrusage_sig);
94 ATF_TC_HEAD(getrusage_sig, tc)
95 {
96 	atf_tc_set_md_var(tc, "descr", "Test signal count with getrusage(2)");
97 }
98 
99 ATF_TC_BODY(getrusage_sig, tc)
100 {
101 	struct rusage ru;
102 	const long n = 5;
103 	int i;
104 
105 	/*
106 	 * Test that signals are recorded.
107 	 */
108 	ATF_REQUIRE(signal(SIGUSR1, sighandler) != SIG_ERR);
109 
110 	for (i = 0; i < n; i++)
111 		ATF_REQUIRE(raise(SIGUSR1) == 0);
112 
113 	(void)memset(&ru, 0, sizeof(struct rusage));
114 	ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0);
115 
116 	if (n != ru.ru_nsignals)
117 		atf_tc_fail("getrusage(2) did not record signals");
118 }
119 
120 ATF_TC(getrusage_utime_back);
121 ATF_TC_HEAD(getrusage_utime_back, tc)
122 {
123 	atf_tc_set_md_var(tc, "descr", "Test bogus values from getrusage(2)");
124 }
125 
126 ATF_TC_BODY(getrusage_utime_back, tc)
127 {
128 	struct rusage ru1, ru2;
129 	size_t i;
130 
131 	/*
132 	 * Test that two consecutive calls are sane.
133 	 */
134 	atf_tc_expect_fail("PR kern/30115");
135 
136 	for (i = 0; i < maxiter; i++) {
137 
138 		(void)memset(&ru1, 0, sizeof(struct rusage));
139 		(void)memset(&ru2, 0, sizeof(struct rusage));
140 
141 		work();
142 
143 		ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru1) == 0);
144 
145 		work();
146 
147 		ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru2) == 0);
148 
149 		if (timercmp(&ru2.ru_utime, &ru1.ru_utime, <) != 0)
150 			atf_tc_fail("user time went backwards");
151 	}
152 
153 	atf_tc_fail("anticipated error did not occur");
154 }
155 
156 ATF_TC(getrusage_utime_zero);
157 ATF_TC_HEAD(getrusage_utime_zero, tc)
158 {
159 	atf_tc_set_md_var(tc, "descr", "Test zero utime from getrusage(2)");
160 }
161 
162 ATF_TC_BODY(getrusage_utime_zero, tc)
163 {
164 	struct rusage ru;
165 	size_t i;
166 
167 	/*
168 	 * Test that getrusage(2) does not return
169 	 * zero user time for the calling process.
170 	 *
171 	 * See also (duplicate) PR port-amd64/41734.
172 	 */
173 	atf_tc_expect_fail("PR kern/30115");
174 
175 	for (i = 0; i < maxiter; i++) {
176 
177 		work();
178 
179 		(void)memset(&ru, 0, sizeof(struct rusage));
180 
181 		ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0);
182 
183 		if (ru.ru_utime.tv_sec == 0 && ru.ru_utime.tv_usec == 0)
184 			atf_tc_fail("zero user time from getrusage(2)");
185 	}
186 
187 	atf_tc_fail("anticipated error did not occur");
188 }
189 
190 ATF_TP_ADD_TCS(tp)
191 {
192 
193 	ATF_TP_ADD_TC(tp, getrusage_err);
194 	ATF_TP_ADD_TC(tp, getrusage_sig);
195 	ATF_TP_ADD_TC(tp, getrusage_utime_back);
196 	ATF_TP_ADD_TC(tp, getrusage_utime_zero);
197 
198 	return atf_no_error();
199 }
200