forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-fstat.c
300 lines (263 loc) · 6.89 KB
/
stress-fstat.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
/*
* 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 MAX_FSTAT_THREADS (4)
#define FSTAT_LOOPS (16)
static volatile bool keep_running;
static sigset_t set;
/* paths we should never stat */
static const char *blacklist[] = {
"/dev/watchdog"
};
#define IGNORE_STAT 0x0001
#define IGNORE_LSTAT 0x0002
#define IGNORE_STATX 0x0004
#define IGNORE_FSTAT 0x0008
#define IGNORE_ALL 0x000f
/* stat path information */
typedef struct stat_info {
char *path; /* path to stat */
uint16_t ignore; /* true to ignore this path */
bool access; /* false if we can't access path */
struct stat_info *next; /* next stat_info in list */
} stat_info_t;
/* Thread context information */
typedef struct ctxt {
const args_t *args; /* Stressor args */
stat_info_t *si; /* path stat information */
const uid_t euid; /* euid of process */
} ctxt_t;
void stress_set_fstat_dir(const char *opt)
{
set_setting("fstat-dir", TYPE_ID_STR, (void *)opt);
}
/*
* handle_fstat_sigalrm()
* catch SIGALRM
*/
static void MLOCKED handle_fstat_sigalrm(int dummy)
{
(void)dummy;
keep_running = false;
g_keep_stressing_flag = false;
}
/*
* do_not_stat()
* Check if file should not be stat'd
*/
static bool do_not_stat(const char *filename)
{
size_t i;
for (i = 0; i < SIZEOF_ARRAY(blacklist); i++) {
if (!strncmp(filename, blacklist[i], strlen(blacklist[i])))
return true;
}
return false;
}
static void stress_fstat_helper(const ctxt_t *ctxt)
{
struct stat buf;
#if defined(AT_EMPTY_PATH) && defined(AT_SYMLINK_NOFOLLOW)
struct shim_statx bufx;
#endif
stat_info_t *si = ctxt->si;
if ((stat(si->path, &buf) < 0) && (errno != ENOMEM)) {
si->ignore |= IGNORE_STAT;
}
if ((lstat(si->path, &buf) < 0) && (errno != ENOMEM)) {
si->ignore |= IGNORE_LSTAT;
}
#if defined(AT_EMPTY_PATH) && defined(AT_SYMLINK_NOFOLLOW)
/* Heavy weight statx */
if ((shim_statx(AT_EMPTY_PATH, si->path, AT_SYMLINK_NOFOLLOW,
SHIM_STATX_ALL, &bufx) < 0) && (errno != ENOMEM)) {
si->ignore |= IGNORE_STATX;
}
#endif
/*
* Opening /dev files such as /dev/urandom
* may block when running as root, so
* avoid this.
*/
if ((si->access) && (ctxt->euid)) {
int fd;
fd = open(si->path, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
si->access = false;
return;
}
if ((fstat(fd, &buf) < 0) && (errno != ENOMEM))
si->ignore |= IGNORE_FSTAT;
(void)close(fd);
}
}
#if defined(HAVE_LIB_PTHREAD)
/*
* stress_fstat_thread
* keep exercising a file until
* controlling thread triggers an exit
*/
static void *stress_fstat_thread(void *ctxt_ptr)
{
static void *nowt = NULL;
uint8_t stack[SIGSTKSZ + STACK_ALIGNMENT];
const ctxt_t *ctxt = (const ctxt_t *)ctxt_ptr;
/*
* Block all signals, let controlling thread
* handle these
*/
(void)sigprocmask(SIG_BLOCK, &set, NULL);
/*
* According to POSIX.1 a thread should have
* a distinct alternative signal stack.
* However, we block signals in this thread
* so this is probably just totally unncessary.
*/
(void)memset(stack, 0, sizeof(stack));
if (stress_sigaltstack(stack, SIGSTKSZ) < 0)
return &nowt;
while (keep_running && g_keep_stressing_flag) {
size_t i;
for (i = 0; i < FSTAT_LOOPS; i++) {
if (!g_keep_stressing_flag)
break;
stress_fstat_helper(ctxt);
}
}
return &nowt;
}
#endif
/*
* stress_fstat_threads()
* create a bunch of threads to thrash a file
*/
static void stress_fstat_threads(const args_t *args, stat_info_t *si, const uid_t euid)
{
size_t i;
#if defined(HAVE_LIB_PTHREAD)
pthread_t pthreads[MAX_FSTAT_THREADS];
int ret[MAX_FSTAT_THREADS];
#endif
ctxt_t ctxt = {
.args = args,
.si = si,
.euid = euid
};
keep_running = true;
#if defined(HAVE_LIB_PTHREAD)
(void)memset(ret, 0, sizeof(ret));
(void)memset(pthreads, 0, sizeof(pthreads));
for (i = 0; i < MAX_FSTAT_THREADS; i++) {
ret[i] = pthread_create(&pthreads[i], NULL,
stress_fstat_thread, &ctxt);
}
#endif
for (i = 0; i < FSTAT_LOOPS; i++) {
if (!g_keep_stressing_flag)
break;
stress_fstat_helper(&ctxt);
}
keep_running = false;
#if defined(HAVE_LIB_PTHREAD)
for (i = 0; i < MAX_FSTAT_THREADS; i++) {
if (ret[i] == 0)
pthread_join(pthreads[i], NULL);
}
#endif
}
/*
* stress_fstat()
* stress system with fstat
*/
int stress_fstat(const args_t *args)
{
stat_info_t *si;
static stat_info_t *stat_info;
struct dirent *d;
NOCLOBBER int ret = EXIT_FAILURE;
bool stat_some;
const uid_t euid = geteuid();
DIR *dp;
char *fstat_dir = "/dev";
(void)get_setting("fstat-dir", &fstat_dir);
if (stress_sighandler(args->name, SIGALRM, handle_fstat_sigalrm, NULL) < 0)
return EXIT_FAILURE;
if ((dp = opendir(fstat_dir)) == NULL) {
pr_err("%s: opendir on %s failed: errno=%d: (%s)\n",
args->name, fstat_dir, errno, strerror(errno));
return EXIT_FAILURE;
}
/* Cache all the directory entries */
while ((d = readdir(dp)) != NULL) {
char path[PATH_MAX];
if (!g_keep_stressing_flag) {
ret = EXIT_SUCCESS;
(void)closedir(dp);
goto free_cache;
}
(void)snprintf(path, sizeof(path), "%s/%s", fstat_dir, d->d_name);
if (do_not_stat(path))
continue;
if ((si = calloc(1, sizeof(*si))) == NULL) {
pr_err("%s: out of memory\n", args->name);
(void)closedir(dp);
goto free_cache;
}
if ((si->path = strdup(path)) == NULL) {
pr_err("%s: out of memory\n", args->name);
free(si);
(void)closedir(dp);
goto free_cache;
}
si->ignore = 0;
si->access = true;
si->next = stat_info;
stat_info = si;
}
(void)closedir(dp);
(void)sigfillset(&set);
do {
stat_some = false;
for (si = stat_info; g_keep_stressing_flag && si; si = si->next) {
if (!keep_stressing())
break;
if (si->ignore == IGNORE_ALL)
continue;
stress_fstat_threads(args, si, euid);
stat_some = true;
inc_counter(args);
}
} while (stat_some && keep_stressing());
ret = EXIT_SUCCESS;
free_cache:
/* Free cache */
for (si = stat_info; si; ) {
stat_info_t *next = si->next;
free(si->path);
free(si);
si = next;
}
return ret;
}