-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstress-dnotify.c
403 lines (347 loc) · 9.45 KB
/
stress-dnotify.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
* Copyright (C) 2012-2018 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 <colin.king@canonical.com> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <apw@rossby.metr.ou.edu> but has more stress tests and more
* functionality.
*/
#include "stress-ng.h"
#if defined(__linux__) && defined(F_NOTIFY)
#include <sys/select.h>
#define DIR_FLAGS (S_IRWXU | S_IRWXG)
#define FILE_FLAGS (S_IRUSR | S_IWUSR)
#define BUF_SIZE (4096)
static volatile int dnotify_fd;
static void dnotify_handler(int sig, siginfo_t *si, void *data)
{
(void)sig;
(void)data;
dnotify_fd = si->si_fd;
}
typedef int (*dnotify_helper)(const args_t *args, const char *path, const void *private);
typedef void (*dnotify_func)(const args_t *args, const char *path);
typedef struct {
const dnotify_func func;
const char* description;
} dnotify_stress_t;
/*
* dnotify_exercise()
* run a given test helper function 'func' and see if this triggers the
* required dnotify event flags 'flags'.
*/
static void dnotify_exercise(
const args_t *args, /* Stressor args */
const char *filename, /* Filename in test */
const char *watchname, /* File or directory to watch using dnotify */
const dnotify_helper func, /* Helper func */
const int flags, /* DN_* flags to watch for */
void *private) /* Helper func private data */
{
int fd, i = 0;
if ((fd = open(watchname, O_RDONLY)) < 0) {
pr_fail_err("open");
return;
}
if (fcntl(fd, F_SETSIG, SIGRTMIN + 1) < 0) {
pr_fail_err("fcntl F_SETSIG");
goto cleanup;
}
if (fcntl(fd, F_NOTIFY, flags) < 0) {
pr_fail_err("fcntl F_NOTIFY");
goto cleanup;
}
dnotify_fd = -1;
if (func(args, filename, private) < 0)
goto cleanup;
/* Wait for up to 1 second for event */
while ((i < 1000) && (dnotify_fd == -1)) {
i++;
(void)shim_usleep(1000);
}
if (dnotify_fd != fd) {
pr_fail("%s: did not get expected dnotify "
"file descriptor\n", args->name);
}
cleanup:
(void)close(fd);
}
/*
* rm_file()
* remove a file
*/
static int rm_file(const args_t *args, const char *path)
{
if ((unlink(path) < 0) && errno != ENOENT) {
pr_err("%s: cannot remove file %s: errno=%d (%s)\n",
args->name, path, errno, strerror(errno));
return -1;
}
return 0;
}
/*
* mk_filename()
* simple helper to create a filename
*/
static inline void mk_filename(
char *filename,
const size_t len,
const char *path,
const char *name)
{
(void)snprintf(filename, len, "%s/%s", path, name);
}
/*
* mk_file()
* create file of length len bytes
*/
static int mk_file(const args_t *args, const char *filename, const size_t len)
{
int fd;
size_t sz = len;
char buffer[BUF_SIZE];
if ((fd = open(filename, O_CREAT | O_RDWR, FILE_FLAGS)) < 0) {
pr_err("%s: cannot create file %s: errno=%d (%s)\n",
args->name, filename, errno, strerror(errno));
return -1;
}
memset(buffer, 'x', BUF_SIZE);
while (sz > 0) {
size_t n = (sz > BUF_SIZE) ? BUF_SIZE : sz;
int ret;
if ((ret = write(fd, buffer, n)) < 0) {
pr_err("%s: error writing to file %s: errno=%d (%s)\n",
args->name, filename, errno, strerror(errno));
(void)close(fd);
return -1;
}
sz -= ret;
}
if (close(fd) < 0) {
pr_err("%s: cannot close file %s: errno=%d (%s)\n",
args->name, filename, errno, strerror(errno));
return -1;
}
return 0;
}
static int dnotify_attrib_helper(
const args_t *args,
const char *path,
const void *dummy)
{
(void)dummy;
if (chmod(path, S_IRUSR | S_IWUSR) < 0) {
pr_err("%s: cannot chmod file %s: errno=%d (%s)\n",
args->name, path, errno, strerror(errno));
return -1;
}
return 0;
}
static void dnotify_attrib_file(const args_t *args, const char *path)
{
char filepath[PATH_MAX];
mk_filename(filepath, PATH_MAX, path, "dnotify_file");
if (mk_file(args, filepath, 4096) < 0)
return;
dnotify_exercise(args, filepath, path,
dnotify_attrib_helper, DN_ATTRIB, NULL);
(void)rm_file(args, filepath);
}
static int dnotify_access_helper(
const args_t *args,
const char *path,
const void *dummy)
{
int fd;
char buffer[1];
int rc = 0;
(void)dummy;
if ((fd = open(path, O_RDONLY)) < 0) {
pr_err("%s: cannot open file %s: errno=%d (%s)\n",
args->name, path, errno, strerror(errno));
return -1;
}
/* Just want to force an access */
do_access:
if (g_keep_stressing_flag && (read(fd, buffer, 1) < 0)) {
if ((errno == EAGAIN) || (errno == EINTR))
goto do_access;
pr_err("%s: cannot read file %s: errno=%d (%s)\n",
args->name, path, errno, strerror(errno));
rc = -1;
}
(void)close(fd);
return rc;
}
static void dnotify_access_file(const args_t *args, const char *path)
{
char filepath[PATH_MAX];
mk_filename(filepath, PATH_MAX, path, "dnotify_file");
if (mk_file(args, filepath, 4096) < 0)
return;
dnotify_exercise(args, filepath, path,
dnotify_access_helper, DN_ACCESS, NULL);
(void)rm_file(args, filepath);
}
static int dnotify_modify_helper(
const args_t *args,
const char *path,
const void *dummy)
{
int fd, rc = 0;
char buffer[1] = { 0 };
(void)dummy;
if (mk_file(args, path, 4096) < 0)
return -1;
if ((fd = open(path, O_RDWR)) < 0) {
pr_err("%s: cannot open file %s: errno=%d (%s)\n",
args->name, path, errno, strerror(errno));
rc = -1;
goto remove;
}
do_modify:
if (g_keep_stressing_flag && (write(fd, buffer, 1) < 0)) {
if ((errno == EAGAIN) || (errno == EINTR))
goto do_modify;
pr_err("%s: cannot write to file %s: errno=%d (%s)\n",
args->name, path, errno, strerror(errno));
rc = -1;
}
(void)close(fd);
remove:
(void)rm_file(args, path);
return rc;
}
static void dnotify_modify_file(const args_t *args, const char *path)
{
char filepath[PATH_MAX];
mk_filename(filepath, PATH_MAX, path, "dnotify_file");
dnotify_exercise(args, filepath, path,
dnotify_modify_helper, DN_MODIFY, NULL);
}
static int dnotify_creat_helper(
const args_t *args,
const char *path,
const void *dummy)
{
(void)dummy;
int fd;
if ((fd = creat(path, FILE_FLAGS)) < 0) {
pr_err("%s: cannot create file %s: errno=%d (%s)\n",
args->name, path, errno, strerror(errno));
return -1;
}
(void)close(fd);
return 0;
}
static void dnotify_creat_file(const args_t *args, const char *path)
{
char filepath[PATH_MAX];
mk_filename(filepath, PATH_MAX, path, "dnotify_file");
dnotify_exercise(args, filepath, path,
dnotify_creat_helper, DN_CREATE, NULL);
(void)rm_file(args, filepath);
}
static int dnotify_delete_helper(
const args_t *args,
const char *path,
const void *dummy)
{
(void)dummy;
return rm_file(args, path);
}
static void dnotify_delete_file(const args_t *args, const char *path)
{
char filepath[PATH_MAX];
mk_filename(filepath, PATH_MAX, path, "dnotify_file");
if (mk_file(args, filepath, 4096) < 0)
return;
dnotify_exercise(args, filepath, path,
dnotify_delete_helper, DN_DELETE, NULL);
/* We remove (again) it just in case the test failed */
(void)rm_file(args, filepath);
}
static int dnotify_rename_helper(
const args_t *args,
const char *oldpath,
const void *private)
{
const char *newpath = (const char *)private;
if (rename(oldpath, newpath) < 0) {
pr_err("%s: cannot rename %s to %s: errno=%d (%s)\n",
args->name, oldpath, newpath, errno, strerror(errno));
return -1;
}
return 0;
}
static void dnotify_rename_file(const args_t *args, const char *path)
{
char oldfile[PATH_MAX], newfile[PATH_MAX];
mk_filename(oldfile, PATH_MAX, path, "dnotify_file");
mk_filename(newfile, PATH_MAX, path, "dnotify_file_renamed");
if (mk_file(args, oldfile, 4096) < 0)
return;
dnotify_exercise(args, oldfile, path,
dnotify_rename_helper, DN_RENAME, newfile);
(void)rm_file(args, oldfile); /* In case rename failed */
(void)rm_file(args, newfile);
}
static const dnotify_stress_t dnotify_stressors[] = {
{ dnotify_access_file, "DN_ACCESS" },
{ dnotify_modify_file, "DN_MODIFY" },
{ dnotify_creat_file, "DN_CREATE" },
{ dnotify_delete_file, "DN_DELETE" },
{ dnotify_rename_file, "DN_RENAME" },
{ dnotify_attrib_file, "DN_ATTRIB" },
{ NULL, NULL }
};
/*
* stress_dnotify()
* stress dnotify
*/
int stress_dnotify(const args_t *args)
{
char dirname[PATH_MAX];
int ret, i;
struct sigaction act;
act.sa_sigaction = dnotify_handler;
(void)sigemptyset(&act.sa_mask);
act.sa_flags = SA_SIGINFO;
if (sigaction(SIGRTMIN + 1, &act, NULL) < 0) {
pr_err("%s: sigaction failed: errno=%d (%s)\n",
args->name, errno, strerror(errno));
return EXIT_NO_RESOURCE;
}
stress_temp_dir_args(args, dirname, sizeof(dirname));
ret = stress_temp_dir_mk_args(args);
if (ret < 0)
return exit_status(-ret);
do {
for (i = 0; g_keep_stressing_flag && dnotify_stressors[i].func; i++)
dnotify_stressors[i].func(args, dirname);
inc_counter(args);
} while (keep_stressing());
(void)stress_temp_dir_rm_args(args);
return EXIT_SUCCESS;
}
#else
int stress_dnotify(const args_t *args)
{
return stress_not_implemented(args);
}
#endif