forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-chdir.c
133 lines (120 loc) · 3.2 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
/*
* 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"
/*
* stress_set_chdir_dirs()
* set number of chdir directories from given option string
*/
void stress_set_chdir_dirs(const char *opt)
{
uint64_t chdir_dirs;
chdir_dirs = get_uint64_byte(opt);
check_range("chdir-dirs", chdir_dirs,
MIN_CHDIR_DIRS, MAX_CHDIR_DIRS);
set_setting("chdir-dirs", TYPE_ID_UINT64, &chdir_dirs);
}
/*
* stress_chdir
* stress chdir calls
*/
int stress_chdir(const args_t *args)
{
uint64_t i, chdir_dirs = DEFAULT_CHDIR_DIRS;
char path[PATH_MAX], cwd[PATH_MAX];
int rc, ret = EXIT_FAILURE;
char **paths;
(void)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;
}
if (getcwd(cwd, sizeof(cwd)) == NULL) {
pr_fail_err("getcwd");
goto err;
}
rc = stress_temp_dir_mk_args(args);
if (rc < 0) {
ret = exit_status(-rc);
goto err;
}
/* Populate */
for (i = 0; i < chdir_dirs; i++) {
uint64_t gray_code = (i >> 1) ^ i;
(void)stress_temp_filename_args(args,
path, sizeof(path), gray_code);
paths[i] = strdup(path);
if (paths[i] == NULL)
goto abort;
rc = mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR);
if (rc < 0) {
ret = exit_status(errno);
pr_fail_err("mkdir");
goto abort;
}
if (!g_keep_stressing_flag)
goto done;
}
do {
for (i = 0; i < chdir_dirs; i++) {
if (!keep_stressing())
goto done;
if (chdir(paths[i]) < 0) {
if (errno != ENOMEM) {
pr_fail_err("chdir");
goto abort;
}
}
redo:
if (!keep_stressing())
goto done;
/* We need chdir to cwd to always succeed */
if (chdir(cwd) < 0) {
/* Maybe low memory, force retry */
if (errno == ENOMEM)
goto redo;
pr_fail_err("chdir");
goto abort;
}
}
inc_counter(args);
} while (keep_stressing());
done:
ret = EXIT_SUCCESS;
abort:
if (chdir(cwd) < 0)
pr_fail_err("chdir");
/* force unlink of all files */
pr_tidy("%s: removing %" PRIu64 " directories\n",
args->name, chdir_dirs);
for (i = 0; (i < chdir_dirs) && paths[i] ; i++) {
(void)rmdir(paths[i]);
free(paths[i]);
}
(void)stress_temp_dir_rm_args(args);
err:
free(paths);
return ret;
}