forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-chmod.c
226 lines (211 loc) · 5.01 KB
/
stress-chmod.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
/*
* Copyright (C) 2013-2019 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"
static const help_t help[] = {
{ NULL, "chmod N", "start N workers thrashing chmod file mode bits " },
{ NULL, "chmod-ops N", "stop chmod workers after N bogo operations" },
{ NULL, NULL, NULL }
};
static const mode_t modes[] = {
#if defined(S_ISUID)
S_ISUID,
#endif
#if defined(S_ISGID)
S_ISGID,
#endif
#if defined(S_ISVTX)
S_ISVTX,
#endif
#if defined(S_IRUSR)
S_IRUSR,
#endif
#if defined(S_IWUSR)
S_IWUSR,
#endif
#if defined(S_IXUSR)
S_IXUSR,
#endif
#if defined(S_IRGRP)
S_IRGRP,
#endif
#if defined(S_IWGRP)
S_IWGRP,
#endif
#if defined(S_IXGRP)
S_IXGRP,
#endif
#if defined(S_IROTH)
S_IROTH,
#endif
#if defined(S_IWOTH)
S_IWOTH,
#endif
#if defined( S_IXOTH)
S_IXOTH,
#endif
0
};
/*
* BSD systems can return EFTYPE which we can ignore
* as a "known" error on invalid chmod mode bits
*/
#if defined(EFTYPE)
#define CHECK(x) if ((x) && (errno != EFTYPE)) return -1
#else
#define CHECK(x) if (x) return -1
#endif
/*
* do_fchmod()
* fchmod the 4 different masks from a mode flag, namely:
* mode flag
* all mode flags or'd together
* inverse mode flag
* inverse all mode flags or'd together
*/
static int do_fchmod(
const int fd,
const int i,
const mode_t mask,
const mode_t all_mask)
{
CHECK(fchmod(fd, modes[i]) < 0);
CHECK(fchmod(fd, mask) < 0);
CHECK(fchmod(fd, modes[i] ^ all_mask) < 0);
CHECK(fchmod(fd, mask ^ all_mask) < 0);
return 0;
}
/*
* do_chmod()
* chmod the 4 different masks from a mode flag, namely:
* mode flag
* all mode flags or'd together
* inverse mode flag
* inverse all mode flags or'd together
*/
static int do_chmod(
const char *filename,
const int i,
const mode_t mask,
const mode_t all_mask)
{
CHECK(chmod(filename, modes[i]) < 0);
CHECK(chmod(filename, mask) < 0);
CHECK(chmod(filename, modes[i] ^ all_mask) < 0);
CHECK(chmod(filename, mask ^ all_mask) < 0);
return 0;
}
/*
* stress_chmod
* stress chmod
*/
static int stress_chmod(const args_t *args)
{
const pid_t ppid = getppid();
int i, fd = -1, rc = EXIT_FAILURE, retries = 0;
mode_t all_mask = 0;
char filename[PATH_MAX], pathname[PATH_MAX];
/*
* Allow for multiple workers to chmod the *same* file
*/
stress_temp_dir(pathname, sizeof(pathname), args->name, ppid, 0);
if (mkdir(pathname, S_IRUSR | S_IRWXU) < 0) {
if (errno != EEXIST) {
rc = exit_status(errno);
pr_fail_err("mkdir");
return rc;
}
}
(void)stress_temp_filename(filename, sizeof(filename),
args->name, ppid, 0, 0);
if (args->instance == 0) {
if ((fd = creat(filename, S_IRUSR | S_IWUSR)) < 0) {
rc = exit_status(errno);
pr_fail_err("creat");
goto tidy;
}
} else {
/* Other instances must try to open the file */
for (;;) {
if ((fd = open(filename, O_RDWR, S_IRUSR | S_IWUSR)) > - 1)
break;
#if defined(__NetBSD__)
/* For some reason usleep blocks */
(void)shim_sched_yield();
#else
(void)shim_usleep(100000);
#endif
/* Timed out, then give up */
if (!g_keep_stressing_flag) {
rc = EXIT_SUCCESS;
goto tidy;
}
/* Too many retries? */
if (++retries >= 10000) {
pr_err("%s: chmod: file %s took %d "
"retries to open and gave up "
"(instance %" PRIu32 ")\n",
args->name, filename, retries, args->instance);
goto tidy;
}
}
}
for (i = 0; modes[i]; i++)
all_mask |= modes[i];
do {
mode_t mask = 0;
for (i = 0; modes[i]; i++) {
mask |= modes[i];
if (do_fchmod(fd, i, mask, all_mask) < 0) {
pr_fail_err("fchmod");
}
if (do_chmod(filename, i, mask, all_mask) < 0) {
if (errno == ENOENT || errno == ENOTDIR) {
/*
* File was removed during test by
* another worker
*/
rc = EXIT_SUCCESS;
goto tidy;
}
pr_fail_err("chmod");
}
}
inc_counter(args);
} while (keep_stressing());
rc = EXIT_SUCCESS;
tidy:
if (fd >= 0) {
(void)fchmod(fd, 0666);
(void)close(fd);
}
(void)unlink(filename);
(void)rmdir(pathname);
return rc;
}
stressor_info_t stress_chmod_info = {
.stressor = stress_chmod,
.class = CLASS_FILESYSTEM | CLASS_OS,
.help = help
};