diff options
| author | cvs2svn <admin@example.com> | 2025-08-02 06:16:35 +0000 |
|---|---|---|
| committer | cvs2svn <admin@example.com> | 2025-08-02 06:16:35 +0000 |
| commit | a397c95f8aea58533e72379d6e0de12683ecd0dc (patch) | |
| tree | 45ccaccd0ce2ea01650f0a9f5e9268a656e17e51 /src/regress/lib/libc/sys/t_getrusage.c | |
| parent | a79e90e7342954ae2287db505811ca3c1cd336d7 (diff) | |
| download | openbsd-tb_20250802.tar.gz openbsd-tb_20250802.tar.bz2 openbsd-tb_20250802.zip | |
This commit was manufactured by cvs2git to create tag 'tb_20250802'.tb_20250802
Diffstat (limited to 'src/regress/lib/libc/sys/t_getrusage.c')
| -rw-r--r-- | src/regress/lib/libc/sys/t_getrusage.c | 286 |
1 files changed, 0 insertions, 286 deletions
diff --git a/src/regress/lib/libc/sys/t_getrusage.c b/src/regress/lib/libc/sys/t_getrusage.c deleted file mode 100644 index 1a9e3d139c..0000000000 --- a/src/regress/lib/libc/sys/t_getrusage.c +++ /dev/null | |||
| @@ -1,286 +0,0 @@ | |||
| 1 | /* $OpenBSD: t_getrusage.c,v 1.3 2021/12/13 16:56:48 deraadt Exp $ */ | ||
| 2 | /* $NetBSD: t_getrusage.c,v 1.8 2018/05/09 08:45:03 mrg 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/resource.h> | ||
| 36 | #include <sys/time.h> | ||
| 37 | |||
| 38 | #include "atf-c.h" | ||
| 39 | #include <stdio.h> | ||
| 40 | #include <errno.h> | ||
| 41 | #include <limits.h> | ||
| 42 | #include <signal.h> | ||
| 43 | #include <stdint.h> | ||
| 44 | #include <stdlib.h> | ||
| 45 | #include <string.h> | ||
| 46 | #include <fcntl.h> | ||
| 47 | #include <sys/socket.h> | ||
| 48 | #include <netinet/in.h> | ||
| 49 | |||
| 50 | static void work(void); | ||
| 51 | static void sighandler(int); | ||
| 52 | |||
| 53 | static const size_t maxiter = 2000; | ||
| 54 | |||
| 55 | static void | ||
| 56 | sighandler(int signo __unused) | ||
| 57 | { | ||
| 58 | /* Nothing. */ | ||
| 59 | } | ||
| 60 | |||
| 61 | static void | ||
| 62 | work(void) | ||
| 63 | { | ||
| 64 | size_t n = UINT16_MAX * 10; | ||
| 65 | |||
| 66 | while (n > 0) { | ||
| 67 | #ifdef __or1k__ | ||
| 68 | asm volatile("l.nop"); /* Do something. */ | ||
| 69 | #elif defined(__ia64__) | ||
| 70 | asm volatile("nop 0"); /* Do something. */ | ||
| 71 | #else | ||
| 72 | asm volatile("nop"); /* Do something. */ | ||
| 73 | #endif | ||
| 74 | n--; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | ATF_TC(getrusage_err); | ||
| 79 | ATF_TC_HEAD(getrusage_err, tc) | ||
| 80 | { | ||
| 81 | atf_tc_set_md_var(tc, "descr", "Test error conditions"); | ||
| 82 | } | ||
| 83 | |||
| 84 | ATF_TC_BODY(getrusage_err, tc) | ||
| 85 | { | ||
| 86 | struct rusage ru; | ||
| 87 | |||
| 88 | errno = 0; | ||
| 89 | |||
| 90 | ATF_REQUIRE(getrusage(INT_MAX, &ru) != 0); | ||
| 91 | ATF_REQUIRE(errno == EINVAL); | ||
| 92 | |||
| 93 | errno = 0; | ||
| 94 | |||
| 95 | ATF_REQUIRE(getrusage(RUSAGE_SELF, (void *)0) != 0); | ||
| 96 | ATF_REQUIRE(errno == EFAULT); | ||
| 97 | } | ||
| 98 | |||
| 99 | ATF_TC(getrusage_sig); | ||
| 100 | ATF_TC_HEAD(getrusage_sig, tc) | ||
| 101 | { | ||
| 102 | atf_tc_set_md_var(tc, "descr", "Test signal count with getrusage(2)"); | ||
| 103 | } | ||
| 104 | |||
| 105 | ATF_TC_BODY(getrusage_sig, tc) | ||
| 106 | { | ||
| 107 | struct rusage ru; | ||
| 108 | const long n = 5; | ||
| 109 | int i; | ||
| 110 | |||
| 111 | /* | ||
| 112 | * Test that signals are recorded. | ||
| 113 | */ | ||
| 114 | ATF_REQUIRE(signal(SIGUSR1, sighandler) != SIG_ERR); | ||
| 115 | |||
| 116 | for (i = 0; i < n; i++) | ||
| 117 | ATF_REQUIRE(raise(SIGUSR1) == 0); | ||
| 118 | |||
| 119 | (void)memset(&ru, 0, sizeof(struct rusage)); | ||
| 120 | ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0); | ||
| 121 | |||
| 122 | if (n != ru.ru_nsignals) | ||
| 123 | atf_tc_fail("getrusage(2) did not record signals"); | ||
| 124 | } | ||
| 125 | |||
| 126 | ATF_TC(getrusage_maxrss); | ||
| 127 | ATF_TC_HEAD(getrusage_maxrss, tc) | ||
| 128 | { | ||
| 129 | atf_tc_set_md_var(tc, "descr", "Test maxrss growing with getrusage(2)"); | ||
| 130 | } | ||
| 131 | |||
| 132 | ATF_TC_BODY(getrusage_maxrss, tc) | ||
| 133 | { | ||
| 134 | struct rusage ru; | ||
| 135 | long maxrss; | ||
| 136 | int i, fd; | ||
| 137 | |||
| 138 | #define DUMP_FILE "dump" | ||
| 139 | |||
| 140 | fd = open(DUMP_FILE, O_WRONLY|O_CREAT|O_TRUNC, 0222); | ||
| 141 | ATF_REQUIRE(fd != -1); | ||
| 142 | |||
| 143 | (void)memset(&ru, 0, sizeof(struct rusage)); | ||
| 144 | ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0); | ||
| 145 | maxrss = ru.ru_maxrss; | ||
| 146 | |||
| 147 | #define CHUNK (1024 * 1024) | ||
| 148 | for (i = 0; i < 40; i++) { | ||
| 149 | void *p = malloc(CHUNK); | ||
| 150 | memset(p, 0, CHUNK); | ||
| 151 | write(fd, p, CHUNK); | ||
| 152 | } | ||
| 153 | close(fd); | ||
| 154 | unlink(DUMP_FILE); | ||
| 155 | |||
| 156 | ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0); | ||
| 157 | ATF_REQUIRE_MSG(maxrss < ru.ru_maxrss, | ||
| 158 | "maxrss: %ld, ru.ru_maxrss: %ld", maxrss, ru.ru_maxrss); | ||
| 159 | } | ||
| 160 | |||
| 161 | ATF_TC(getrusage_msgsnd); | ||
| 162 | ATF_TC_HEAD(getrusage_msgsnd, tc) | ||
| 163 | { | ||
| 164 | atf_tc_set_md_var(tc, "descr", "Test send growing with getrusage(2)"); | ||
| 165 | } | ||
| 166 | |||
| 167 | ATF_TC_BODY(getrusage_msgsnd, tc) | ||
| 168 | { | ||
| 169 | struct rusage ru; | ||
| 170 | long msgsnd; | ||
| 171 | int s, i; | ||
| 172 | struct sockaddr_in sin; | ||
| 173 | |||
| 174 | ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0); | ||
| 175 | msgsnd = ru.ru_msgsnd; | ||
| 176 | |||
| 177 | s = socket(AF_INET, SOCK_DGRAM, 0); | ||
| 178 | ATF_REQUIRE(s >= 0); | ||
| 179 | memset(&sin, 0, sizeof(sin)); | ||
| 180 | sin.sin_family = AF_INET; | ||
| 181 | sin.sin_len = sizeof(sin); | ||
| 182 | sin.sin_addr.s_addr = ntohl(INADDR_LOOPBACK); | ||
| 183 | sin.sin_port = htons(3333); | ||
| 184 | |||
| 185 | for (i = 0; i < 10; i++) | ||
| 186 | ATF_REQUIRE(sendto(s, &sin, sizeof(sin), 0, (void *)&sin, | ||
| 187 | (socklen_t)sizeof(sin)) != -1); | ||
| 188 | |||
| 189 | ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0); | ||
| 190 | ATF_REQUIRE(msgsnd + 10 == ru.ru_msgsnd); | ||
| 191 | close(s); | ||
| 192 | } | ||
| 193 | |||
| 194 | ATF_TC(getrusage_utime_back); | ||
| 195 | ATF_TC_HEAD(getrusage_utime_back, tc) | ||
| 196 | { | ||
| 197 | atf_tc_set_md_var(tc, "descr", "Test bogus values from getrusage(2)"); | ||
| 198 | } | ||
| 199 | |||
| 200 | ATF_TC_BODY(getrusage_utime_back, tc) | ||
| 201 | { | ||
| 202 | struct rusage ru1, ru2; | ||
| 203 | size_t i; | ||
| 204 | |||
| 205 | /* | ||
| 206 | * Test that two consecutive calls are sane. | ||
| 207 | */ | ||
| 208 | #ifndef __OpenBSD__ | ||
| 209 | atf_tc_expect_fail("PR kern/30115"); | ||
| 210 | #endif | ||
| 211 | |||
| 212 | for (i = 0; i < maxiter; i++) { | ||
| 213 | |||
| 214 | (void)memset(&ru1, 0, sizeof(struct rusage)); | ||
| 215 | (void)memset(&ru2, 0, sizeof(struct rusage)); | ||
| 216 | |||
| 217 | work(); | ||
| 218 | |||
| 219 | ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru1) == 0); | ||
| 220 | |||
| 221 | work(); | ||
| 222 | |||
| 223 | ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru2) == 0); | ||
| 224 | |||
| 225 | if (timercmp(&ru2.ru_utime, &ru1.ru_utime, <) != 0) | ||
| 226 | atf_tc_fail("user time went backwards"); | ||
| 227 | } | ||
| 228 | |||
| 229 | #ifndef __OpenBSD__ | ||
| 230 | atf_tc_fail("anticipated error did not occur"); | ||
| 231 | #endif | ||
| 232 | } | ||
| 233 | |||
| 234 | ATF_TC(getrusage_utime_zero); | ||
| 235 | ATF_TC_HEAD(getrusage_utime_zero, tc) | ||
| 236 | { | ||
| 237 | atf_tc_set_md_var(tc, "descr", "Test zero utime from getrusage(2)"); | ||
| 238 | } | ||
| 239 | |||
| 240 | ATF_TC_BODY(getrusage_utime_zero, tc) | ||
| 241 | { | ||
| 242 | struct rusage ru; | ||
| 243 | size_t i; | ||
| 244 | |||
| 245 | /* | ||
| 246 | * Test that getrusage(2) does not return | ||
| 247 | * zero user time for the calling process. | ||
| 248 | * | ||
| 249 | * See also (duplicate) PR port-amd64/41734. | ||
| 250 | */ | ||
| 251 | #ifndef __OpenBSD__ | ||
| 252 | atf_tc_expect_fail("PR kern/30115"); | ||
| 253 | #endif | ||
| 254 | |||
| 255 | for (i = 0; i < maxiter; i++) { | ||
| 256 | |||
| 257 | work(); | ||
| 258 | #ifdef __OpenBSD__ | ||
| 259 | } | ||
| 260 | #endif | ||
| 261 | |||
| 262 | (void)memset(&ru, 0, sizeof(struct rusage)); | ||
| 263 | |||
| 264 | ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0); | ||
| 265 | |||
| 266 | if (ru.ru_utime.tv_sec == 0 && ru.ru_utime.tv_usec == 0) | ||
| 267 | atf_tc_fail("zero user time from getrusage(2)"); | ||
| 268 | #ifndef __OpenBSD__ | ||
| 269 | } | ||
| 270 | |||
| 271 | atf_tc_fail("anticipated error did not occur"); | ||
| 272 | #endif | ||
| 273 | } | ||
| 274 | |||
| 275 | ATF_TP_ADD_TCS(tp) | ||
| 276 | { | ||
| 277 | |||
| 278 | ATF_TP_ADD_TC(tp, getrusage_err); | ||
| 279 | ATF_TP_ADD_TC(tp, getrusage_sig); | ||
| 280 | ATF_TP_ADD_TC(tp, getrusage_maxrss); | ||
| 281 | ATF_TP_ADD_TC(tp, getrusage_msgsnd); | ||
| 282 | ATF_TP_ADD_TC(tp, getrusage_utime_back); | ||
| 283 | ATF_TP_ADD_TC(tp, getrusage_utime_zero); | ||
| 284 | |||
| 285 | return atf_no_error(); | ||
| 286 | } | ||
