forked from ColinIanKing/stress-ng
/
stress-chmod.c
219 lines (204 loc) · 4.8 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
/*
* Copyright (C) 2013-2016 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.
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include "stress-ng.h"
static mode_t modes[] = {
#ifdef S_ISUID
S_ISUID,
#endif
#ifdef S_ISGID
S_ISGID,
#endif
#ifdef S_ISVTX
S_ISVTX,
#endif
#ifdef S_IRUSR
S_IRUSR,
#endif
#ifdef S_IWUSR
S_IWUSR,
#endif
#ifdef S_IXUSR
S_IXUSR,
#endif
#ifdef S_IRGRP
S_IRGRP,
#endif
#ifdef S_IWGRP
S_IWGRP,
#endif
#ifdef S_IXGRP
S_IXGRP,
#endif
#ifdef S_IROTH
S_IROTH,
#endif
#ifdef S_IWOTH
S_IWOTH,
#endif
#ifdef S_IXOTH
S_IXOTH,
#endif
0
};
/*
* BSD systems can return EFTYPE which we can ignore
* as a "known" error on invalid chmod mode bits
*/
#ifdef 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
*/
int stress_chmod(
uint64_t *const counter,
const uint32_t instance,
const uint64_t max_ops,
const char *name)
{
const pid_t ppid = getppid();
int i, fd = -1, rc = EXIT_FAILURE, retries = 0;
mode_t all_mask = 0;
char filename[PATH_MAX], dirname[PATH_MAX];
/*
* Allow for multiple workers to chmod the *same* file
*/
stress_temp_dir(dirname, sizeof(dirname), name, ppid, 0);
if (mkdir(dirname, S_IRWXU) < 0) {
if (errno != EEXIST) {
pr_fail_err(name, "mkdir");
return EXIT_FAILURE;
}
}
(void)stress_temp_filename(filename, sizeof(filename),
name, ppid, 0, 0);
do {
errno = 0;
/*
* Try and open the file, it may be impossible momentarily
* because other chmod stressors have already created it and
* changed the permission bits. If so, wait a while and retry.
*/
if ((fd = creat(filename, S_IRUSR | S_IWUSR)) < 0) {
if (errno == EPERM || errno == EACCES) {
(void)usleep(100000);
continue;
}
pr_fail_err(name, "open");
goto tidy;
}
break;
} while (opt_do_run && ++retries < 100);
if (retries >= 100) {
pr_err(stderr, "%s: chmod: file %s took %d retries to create (instance %" PRIu32 ")\n",
name, filename, retries, 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(stderr, "%s: fchmod: errno=%d (%s)\n",
name, errno, strerror(errno));
}
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(stderr, "%s: chmod: errno=%d (%s)\n",
name, errno, strerror(errno));
}
}
(*counter)++;
} while (opt_do_run && (!max_ops || *counter < max_ops));
rc = EXIT_SUCCESS;
tidy:
(void)fchmod(fd, 0666);
if (fd >= 0)
(void)close(fd);
(void)unlink(filename);
(void)rmdir(dirname);
return rc;
}