forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-flock.c
94 lines (84 loc) · 2.47 KB
/
stress-flock.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
/*
* 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"
#if !defined(__sun__) && defined(LOCK_EX) && defined(LOCK_UN)
/*
* stress_flock
* stress file locking
*/
int stress_flock(const args_t *args)
{
int fd;
pid_t ppid = getppid();
char filename[PATH_MAX];
char dirname[PATH_MAX];
/*
* There will be a race to create the directory
* so EEXIST is expected on all but one instance
*/
(void)stress_temp_dir(dirname, sizeof(dirname), args->name, ppid, args->instance);
if (mkdir(dirname, S_IRWXU) < 0) {
if (errno != EEXIST) {
pr_fail_err("mkdir");
return exit_status(errno);
}
}
/*
* Lock file is based on parent pid and instance 0
* as we need to share this among all the other
* stress flock processes
*/
(void)stress_temp_filename(filename, sizeof(filename),
args->name, ppid, 0, 0);
retry:
if ((fd = open(filename, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
int rc = exit_status(errno);
if ((errno == ENOENT) && g_keep_stressing_flag) {
/* Race, sometimes we need to retry */
goto retry;
}
/* Not sure why this fails.. report and abort */
pr_fail_err("open");
(void)rmdir(dirname);
return rc;
}
do {
if (flock(fd, LOCK_EX) < 0)
continue;
(void)shim_sched_yield();
(void)flock(fd, LOCK_UN);
inc_counter(args);
} while (keep_stressing());
(void)close(fd);
(void)unlink(filename);
(void)rmdir(dirname);
return EXIT_SUCCESS;
}
#else
int stress_flock(const args_t *args)
{
return stress_not_implemented(args);
}
#endif