forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstress-chdir.c
279 lines (246 loc) · 7.01 KB
/
stress-chdir.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
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022 Colin Ian King.
*
* 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.
*
*/
#include "stress-ng.h"
#include "core-capabilities.h"
#define MIN_CHDIR_DIRS (64)
#define MAX_CHDIR_DIRS (65536)
#define DEFAULT_CHDIR_DIRS (8192)
static const stress_help_t help[] = {
{ NULL, "chdir N", "start N workers thrashing chdir on many paths" },
{ NULL, "chdir-ops N", "stop chdir workers after N bogo chdir operations" },
{ NULL, "chdir-dirs N", "select number of directories to exercise chdir on" },
{ NULL, NULL, NULL }
};
/*
* stress_set_chdir_dirs()
* set number of chdir directories from given option string
*/
static int stress_set_chdir_dirs(const char *opt)
{
uint64_t chdir_dirs;
chdir_dirs = stress_get_uint32(opt);
stress_check_range("chdir-dirs", chdir_dirs,
MIN_CHDIR_DIRS, MAX_CHDIR_DIRS);
return stress_set_setting("chdir-dirs", TYPE_ID_UINT32, &chdir_dirs);
}
/*
* stress_chdir
* stress chdir calls
*/
static int stress_chdir(const stress_args_t *args)
{
uint32_t i, chdir_dirs = DEFAULT_CHDIR_DIRS;
char path[PATH_MAX], cwd[PATH_MAX], badpath[PATH_MAX], longpath[PATH_MAX + 16];
int rc, ret = EXIT_FAILURE, *fds;
char **paths;
bool *mkdir_ok;
struct stat statbuf;
const bool is_root = stress_check_capability(SHIM_CAP_IS_ROOT);
bool got_statbuf = false;
(void)stress_get_setting("chdir-dirs", &chdir_dirs);
paths = calloc(chdir_dirs, sizeof(*paths));
if (!paths) {
pr_err("%s: out of memory allocating paths\n", args->name);
return EXIT_NO_RESOURCE;
}
fds = calloc(chdir_dirs, sizeof(*fds));
if (!fds) {
pr_err("%s: out of memory allocating file descriptors\n", args->name);
free(paths);
return EXIT_NO_RESOURCE;
}
mkdir_ok = calloc(chdir_dirs, sizeof(*mkdir_ok));
if (!mkdir_ok) {
pr_err("%s: out of memory allocating file descriptors\n", args->name);
free(fds);
free(paths);
return EXIT_NO_RESOURCE;
}
if (getcwd(cwd, sizeof(cwd)) == NULL) {
pr_fail("%s: getcwd failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
goto err;
}
rc = stress_temp_dir_mk_args(args);
if (rc < 0) {
ret = exit_status(-rc);
goto err;
}
for (i = 0; i < chdir_dirs; i++) {
fds[i] = -1;
paths[i] = NULL;
}
stress_strnrnd(longpath, sizeof(longpath));
longpath[0] = '/';
(void)stress_temp_filename_args(args, badpath, sizeof(badpath), ~0ULL);
(void)memset(&statbuf, 0, sizeof(statbuf));
*path = '\0'; /* Keep static analysis tools happy */
/* Populate */
for (i = 0; keep_stressing(args) && (i < chdir_dirs); i++) {
uint64_t rnd = (uint64_t)stress_mwc32() << 32;
uint32_t gray_code = (i >> 1) ^ i;
int flags = O_RDONLY;
#if defined(O_DIRECTORY)
flags |= O_DIRECTORY;
#else
UNEXPECTED
#endif
(void)stress_temp_filename_args(args,
path, sizeof(path), rnd | gray_code);
paths[i] = strdup(path);
if (paths[i] == NULL)
goto abort;
rc = mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR);
if (rc < 0) {
if ((errno == ENOMEM) ||
(errno == ENOSPC) ||
(errno == EMLINK)) {
mkdir_ok[i] = false;
fds[i] = -1;
continue;
}
ret = exit_status(errno);
if (ret == EXIT_FAILURE)
pr_fail("%s: mkdir %s failed, errno=%d (%s)\n",
args->name, path, errno, strerror(errno));
goto abort;
}
mkdir_ok[i] = true;
fds[i] = open(paths[i], flags);
if (!got_statbuf) {
if (stat(path, &statbuf) == 0)
got_statbuf = true;
}
}
if (!got_statbuf && *path) {
pr_fail("%s: fstat on %s failed, errno=%d (%s)\n",
args->name, path, errno, strerror(errno));
goto abort;
}
stress_set_proc_state(args->name, STRESS_STATE_RUN);
do {
for (i = 0; keep_stressing(args) && (i < chdir_dirs); i++) {
uint32_t j = stress_mwc32() % chdir_dirs;
const int fd = fds[j] >= 0 ? fds[j] : fds[0];
if (mkdir_ok[i] && (chdir(paths[i]) < 0)) {
if (errno != ENOMEM) {
pr_fail("%s: chdir %s failed, errno=%d (%s)\n",
args->name, paths[i], errno, strerror(errno));
goto abort;
}
}
if ((fd >= 0) && (fchdir(fd) < 0)) {
if (errno != ENOMEM) {
pr_fail("%s: fchdir failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
goto abort;
}
}
/*
* chdir to / should always work, surely?
*/
if (chdir("/") < 0) {
if ((errno != ENOMEM) && (errno != EACCES)) {
pr_fail("%s: chdir / failed, errno=%d (%s)\n",
args->name, errno, strerror(errno));
goto abort;
}
}
/*
* chdir to path that won't allow to access,
* this is designed to exercise a failure. Don't
* exercise this if root as this won't fail for
* root.
*/
if (!is_root && (fchmod(fd, 0000) == 0)) {
VOID_RET(int, fchdir(fd));
VOID_RET(int, fchmod(fd, statbuf.st_mode & 0777));
}
while (keep_stressing(args)) {
/* We need chdir to cwd to always succeed */
if (chdir(cwd) == 0)
break;
/* Maybe low memory, force retry */
if (errno != ENOMEM) {
pr_fail("%s: chdir %s failed, errno=%d (%s)\n",
args->name, cwd, errno, strerror(errno));
goto tidy;
}
}
}
/*
* chdir to a non-existent path
*/
VOID_RET(int, chdir(badpath));
/*
* chdir to an invalid non-directory
*/
VOID_RET(int, chdir("/dev/null"));
/*
* fchdir to an invalid file descriptor
*/
VOID_RET(int, fchdir(-1));
/*
* chdir to a bad directory
*/
VOID_RET(int, chdir(""));
/*
* chdir to an overly long directory name
*/
VOID_RET(int, chdir(longpath));
inc_counter(args);
} while (keep_stressing(args));
ret = EXIT_SUCCESS;
abort:
if (chdir(cwd) < 0)
pr_fail("%s: chdir %s failed, errno=%d (%s)\n",
args->name, cwd, errno, strerror(errno));
tidy:
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
/* force unlink of all files */
pr_tidy("%s: removing %" PRIu32 " directories\n",
args->name, chdir_dirs);
for (i = 0; (i < chdir_dirs) && paths[i] ; i++) {
if (fds[i] >= 0)
(void)close(fds[i]);
if (paths[i]) {
(void)shim_rmdir(paths[i]);
free(paths[i]);
}
}
(void)stress_temp_dir_rm_args(args);
err:
stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
free(mkdir_ok);
free(fds);
free(paths);
return ret;
}
static const stress_opt_set_func_t opt_set_funcs[] = {
{ OPT_chdir_dirs, stress_set_chdir_dirs },
{ 0, NULL }
};
stressor_info_t stress_chdir_info = {
.stressor = stress_chdir,
.class = CLASS_FILESYSTEM | CLASS_OS,
.opt_set_funcs = opt_set_funcs,
.verify = VERIFY_ALWAYS,
.help = help
};