forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-get.c
310 lines (273 loc) · 6.88 KB
/
stress-get.c
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Copyright (C) 2013-2017 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <[email protected]> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <[email protected]> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
#define _DEFAULT_SOURCE 1
#define _BSD_SOURCE 1
#if defined(__linux__)
#include <sys/utsname.h>
#include <sys/timex.h>
#endif
#define check_do_run() \
if (!g_keep_stressing_flag) \
break; \
#define GIDS_MAX (1024)
static const int rusages[] = {
#if defined(RUSAGE_SELF)
RUSAGE_SELF,
#endif
#if defined(RUSAGE_CHILDREN)
RUSAGE_CHILDREN,
#endif
#if defined(RUSAGE_THREAD)
RUSAGE_THREAD
#endif
};
static const int rlimits[] = {
#if defined(RLIMIT_AS)
RLIMIT_AS,
#endif
#if defined(RLIMIT_CORE)
RLIMIT_CORE,
#endif
#if defined(RLIMIT_CPU)
RLIMIT_CPU,
#endif
#if defined(RLIMIT_DATA)
RLIMIT_DATA,
#endif
#if defined(RLIMIT_FSIZE)
RLIMIT_FSIZE,
#endif
#if defined(RLIMIT_MEMLOCK)
RLIMIT_MEMLOCK,
#endif
#if defined(RLIMIT_MSGQUEUE)
RLIMIT_MSGQUEUE,
#endif
#if defined(RLIMIT_NICE)
RLIMIT_NICE,
#endif
#if defined(RLIMIT_NOFILE)
RLIMIT_NOFILE,
#endif
#if defined(RLIMIT_RSS)
RLIMIT_RSS,
#endif
#if defined(RLIMIT_RTPRIO)
RLIMIT_RTPRIO,
#endif
#if defined(RLIMIT_RTTIME)
RLIMIT_RTTIME,
#endif
#if defined(RLIMIT_SIGPENDING)
RLIMIT_SIGPENDING,
#endif
#if defined(RLIMIT_STACK)
RLIMIT_STACK
#endif
};
static const int priorities[] = {
#if defined(PRIO_PROCESS)
PRIO_PROCESS,
#endif
#if defined(PRIO_PGRP)
PRIO_PGRP,
#endif
#if defined(PRIO_USER)
PRIO_USER
#endif
};
/*
* stress on get*() calls
* stress system by rapid get*() system calls
*/
int stress_get(const args_t *args)
{
const bool verify = (g_opt_flags & OPT_FLAGS_VERIFY);
const bool is_root = (geteuid() == 0);
do {
char path[PATH_MAX];
char *ptr;
gid_t gids[GIDS_MAX];
unsigned cpu, node;
#if defined(__linux__)
gid_t rgid, egid, sgid;
uid_t ruid, euid, suid;
struct utsname utsbuf;
struct timex timexbuf;
#endif
#if !defined(__minix__)
const pid_t mypid = getpid();
#endif
int ret, n, fs_index;
size_t i;
struct timeval delta, tv;
time_t t;
pid_t pid;
gid_t gid;
uid_t uid;
check_do_run();
pid = getppid();
(void)pid;
check_do_run();
ptr = getcwd(path, sizeof path);
if (verify && !ptr)
pr_fail_err("getcwd");
check_do_run();
gid = getgid();
(void)gid;
check_do_run();
gid = getegid();
(void)gid;
check_do_run();
uid = getuid();
(void)uid;
check_do_run();
uid = geteuid();
(void)uid;
check_do_run();
ret = getgroups(GIDS_MAX, gids);
if (verify && (ret < 0))
pr_fail_err("getgroups");
check_do_run();
#if !defined(__minix__)
pid = getpgrp();
(void)pid;
check_do_run();
#endif
#if !defined(__minix__)
pid = getpgid(mypid);
(void)pid;
check_do_run();
#endif
for (i = 0; i < SIZEOF_ARRAY(priorities); i++) {
errno = 0;
ret = getpriority(priorities[i], 0);
if (verify && errno && (ret < 0))
pr_fail_err("getpriority");
check_do_run();
}
#if defined(__linux__)
ret = getresgid(&rgid, &egid, &sgid);
if (verify && (ret < 0))
pr_fail_err("getresgid");
check_do_run();
ret = getresuid(&ruid, &euid, &suid);
if (verify && (ret < 0))
pr_fail_err("getresuid");
check_do_run();
#endif
for (i = 0; i < SIZEOF_ARRAY(rlimits); i++) {
struct rlimit rlim;
ret = getrlimit(rlimits[i], &rlim);
if (verify && (ret < 0))
pr_fail("%s: getrlimit(%zu, ..) failed, errno=%d (%s)\n",
args->name, i, errno, strerror(errno));
check_do_run();
}
#if defined(__linux__) && NEED_GLIBC(2,13,0) && defined(EOVERFLOW)
for (i = 0; i < SIZEOF_ARRAY(rlimits); i++) {
struct rlimit rlim[2];
ret = prlimit(mypid, rlimits[i], NULL, &rlim[0]);
if (verify && (ret < 0) && (errno != EOVERFLOW))
pr_fail("%s: prlimit(%d, %zu, ..) failed, errno=%d (%s)\n",
args->name, mypid, i, errno, strerror(errno));
if (!ret) {
ret = prlimit(mypid, rlimits[i], &rlim[0], NULL);
if (verify && (ret < 0) && (errno != EOVERFLOW))
pr_fail("%s: prlimit(%d, %zu, ..) failed, errno=%d (%s)\n",
args->name, mypid, i, errno, strerror(errno));
ret = prlimit(mypid, rlimits[i], &rlim[0], &rlim[1]);
if (verify && (ret < 0) && (errno != EOVERFLOW))
pr_fail("%s: prlimit(%d, %zu, ..) failed, errno=%d (%s)\n",
args->name, mypid, i, errno, strerror(errno));
}
check_do_run();
}
#endif
for (i = 0; i < SIZEOF_ARRAY(rusages); i++) {
struct rusage usage;
ret = getrusage(rusages[i], &usage);
if (verify && (ret < 0))
pr_fail("%s: getrusage(%zu, ..) failed, errno=%d (%s)\n",
args->name, i, errno, strerror(errno));
check_do_run();
}
#if _XOPEN_SOURCE >= 500 || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
ret = getsid(mypid);
if (verify && (ret < 0))
pr_fail_err("getsid");
check_do_run();
#endif
(void)shim_gettid();
check_do_run();
(void)shim_getcpu(&cpu, &node, NULL);
(void)shim_getcpu(NULL, &node, NULL);
(void)shim_getcpu(&cpu, NULL, NULL);
(void)shim_getcpu(NULL, NULL, NULL);
check_do_run();
t = time(NULL);
if (verify && (t == (time_t)-1))
pr_fail_err("time");
ret = gettimeofday(&tv, NULL);
if (verify && (ret < 0))
pr_fail_err("gettimeval");
#if defined(__linux__)
ret = uname(&utsbuf);
if (verify && (ret < 0))
pr_fail_err("uname");
#endif
#if defined(__linux__)
timexbuf.modes = 0;
ret = adjtimex(&timexbuf);
if (is_root && verify && (ret < 0))
pr_fail_err("adjtimex");
#endif
(void)memset(&delta, 0, sizeof(delta));
ret = adjtime(&delta, &tv);
if (is_root && verify && (ret < 0))
pr_fail_err("adjtime");
/* Get number of file system types */
n = shim_sysfs(3);
for (fs_index = 0; fs_index < n; fs_index++) {
char buf[4096];
ret = shim_sysfs(2, fs_index, buf);
if (!ret) {
ret = shim_sysfs(1, buf);
if (verify && (ret != fs_index)) {
pr_fail("%s: sysfs(1, %s) failed, errno=%d (%s)\n",
args->name, buf, errno, strerror(errno));
}
} else {
if (verify) {
pr_fail("%s: sysfs(2, %d, buf) failed, errno=%d (%s)\n",
args->name, fs_index, errno, strerror(errno));
}
}
}
inc_counter(args);
} while (keep_stressing());
return EXIT_SUCCESS;
}