-
Notifications
You must be signed in to change notification settings - Fork 7
/
stress-chown.c
207 lines (186 loc) · 4.36 KB
/
stress-chown.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
/*
* Copyright (C) 2013-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"
/*
* do_fchown()
* set ownership different ways
*/
static int do_fchown(
const int fd,
const bool cap_chown,
const uid_t uid,
const gid_t gid)
{
int ret, tmp;
if (fchown(fd, uid, gid) < 0)
return -errno;
if (fchown(fd, -1, gid) < 0)
return -errno;
if (fchown(fd, uid, -1) < 0)
return -errno;
if (fchown(fd, -1, -1) < 0)
return -errno;
if (cap_chown)
return 0;
if (fchown(fd, 0, 0) == 0)
goto restore;
if (errno != EPERM)
goto restore;
if (fchown(fd, -1, 0) == 0)
goto restore;
if (errno != EPERM)
goto restore;
if (fchown(fd, 0, -1) == 0)
goto restore;
if (errno != EPERM)
goto restore;
return 0;
restore:
tmp = errno;
ret = fchown(fd, uid, gid);
(void)ret;
return -tmp;
}
/*
* do_chown()
* set ownership different ways
*/
static int do_chown(
const char *filename,
const bool cap_chown,
const uid_t uid,
const gid_t gid)
{
int ret, tmp;
if (chown(filename, uid, gid) < 0)
return -errno;
if (chown(filename, -1, gid) < 0)
return -errno;
if (chown(filename, uid, -1) < 0)
return -errno;
if (chown(filename, -1, -1) < 0)
return -errno;
if (cap_chown)
return 0;
if (chown(filename, 0, 0) == 0)
goto restore;
if (errno != EPERM)
goto restore;
if (chown(filename, -1, 0) == 0)
goto restore;
if (errno != EPERM)
goto restore;
if (chown(filename, 0, -1) == 0)
goto restore;
if (errno != EPERM)
goto restore;
return 0;
restore:
tmp = errno;
ret = chown(filename, uid, gid);
(void)ret;
return -tmp;
}
/*
* stress_chown
* stress chown
*/
int stress_chown(const args_t *args)
{
const pid_t ppid = getppid();
int fd = -1, rc = EXIT_FAILURE, retries = 0;
char filename[PATH_MAX], dirname[PATH_MAX];
const uid_t uid = getuid();
const gid_t gid = getgid();
bool cap_chown = false;
if (geteuid() == 0)
cap_chown = true;
/*
* Allow for multiple workers to chown the *same* file
*/
stress_temp_dir(dirname, sizeof(dirname), args->name, ppid, 0);
if (mkdir(dirname, 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;
(void)shim_usleep(100000);
if (++retries >= 100) {
pr_err("%s: chown: file %s took %d "
"retries to open and gave up "
"(instance %" PRIu32 ")\n",
args->name, filename, retries, args->instance);
goto tidy;
}
/* Timed out, then give up */
if (!g_keep_stressing_flag) {
rc = EXIT_SUCCESS;
goto tidy;
}
}
}
do {
int ret;
ret = do_fchown(fd, cap_chown, uid, gid);
if (ret < 0) {
pr_fail_err("fchown");
}
ret = do_chown(filename, cap_chown, uid, gid);
if (ret < 0) {
if (ret == -ENOENT || errno == -ENOTDIR) {
/*
* File was removed during test by
* another worker
*/
rc = EXIT_SUCCESS;
goto tidy;
}
pr_fail_err("chown");
}
inc_counter(args);
} while (keep_stressing());
rc = EXIT_SUCCESS;
tidy:
if (fd >= 0)
(void)close(fd);
(void)unlink(filename);
(void)rmdir(dirname);
return rc;
}