forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-dup.c
100 lines (90 loc) · 2.4 KB
/
stress-dup.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
/*
* 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_dup()
* stress system by rapid dup/close calls
*/
int stress_dup(const args_t *args)
{
int fds[STRESS_FD_MAX];
size_t max_fd = stress_get_file_limit();
size_t i;
#if defined(__linux__)
bool do_dup3 = true;
#endif
if (max_fd > SIZEOF_ARRAY(fds))
max_fd = SIZEOF_ARRAY(fds);
fds[0] = open("/dev/zero", O_RDONLY);
if (fds[0] < 0) {
pr_fail_dbg("open on /dev/zero");
return EXIT_FAILURE;
}
do {
size_t n;
for (n = 1; n < max_fd; n++) {
int tmp;
fds[n] = dup(fds[0]);
if (fds[n] < 0)
break;
#if defined(__linux__)
if (do_dup3 && (mwc32() & 1)) {
int fd;
fd = dup3(fds[0], fds[n], O_CLOEXEC);
/* No dup3 support? then fallback to dup2 */
if ((fd < 0) && (errno == ENOSYS)) {
fd = dup2(fds[0], fds[n]);
do_dup3 = false;
}
fds[n] = fd;
} else {
fds[n] = dup2(fds[0], fds[n]);
}
#else
fds[n] = dup2(fds[0], fds[n]);
#endif
if (fds[n] < 0)
break;
/* dup2 on the same fd should be a no-op */
tmp = dup2(fds[n], fds[n]);
if (tmp != fds[n]) {
pr_fail_err("dup2 with same fds");
break;
}
if (!g_keep_stressing_flag)
break;
inc_counter(args);
}
for (i = 1; i < n; i++) {
if (fds[i] < 0)
break;
if (!g_keep_stressing_flag)
break;
(void)close(fds[i]);
}
} while (keep_stressing());
(void)close(fds[0]);
return EXIT_SUCCESS;
}