forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 1
/
core-try-open.c
189 lines (168 loc) · 4.53 KB
/
core-try-open.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
/*
* Copyright (C) 2013-2021 Canonical, Ltd.
* Copyright (C) 2022 Colin Ian King.
*
* 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.
*
*/
#include "stress-ng.h"
static void stress_try_kill(
const stress_args_t *args,
const pid_t pid,
const char *path)
{
int i;
for (i = 0; i < 10; i++) {
int status;
VOID_RET(int, kill(pid, SIGKILL));
VOID_RET(int, waitpid(pid, &status, WNOHANG));
if ((kill(pid, 0) < 0) && (errno == ESRCH))
return;
(void)shim_usleep(100000);
}
pr_dbg("%s: can't kill pid %" PRIdMAX " opening %s\n",
args->name, (intmax_t)pid, path);
}
/*
* Try to open a file, return 0 if can open it, non-zero
* if it cannot be opened within timeout nanoseconds.
*/
int stress_try_open(
const stress_args_t *args,
const char *path,
const int flags,
const unsigned long timeout_ns)
{
pid_t pid;
int ret, status = 0;
struct stat statbuf;
const int retries = 20;
unsigned long sleep_ns = timeout_ns / retries;
int i;
(void)args;
/* Don't try to open if file can't be stat'd */
if (stat(path, &statbuf) < 0)
return -1;
pid = fork();
if (pid < 0)
return STRESS_TRY_OPEN_FORK_FAIL;
if (pid == 0) {
int fd;
(void)alarm(1);
fd = open(path, flags);
if (fd < 0) {
/* blocked or out of memory, don't give up */
if ((errno == EBUSY) ||
(errno == ENOMEM))
_exit(STRESS_TRY_AGAIN);
_exit(STRESS_TRY_OPEN_FAIL);
}
_exit(STRESS_TRY_OPEN_OK);
}
for (i = 0; i < retries; i++) {
/*
* Child may block on open forever if the driver
* is broken, so use WNOHANG wait to poll rather
* than wait forever on a locked up process
*/
ret = waitpid(pid, &status, WNOHANG);
if (ret < 0) {
/*
* EINTR or something else, treat as failed anyhow
* and forcibly kill child and re-wait. The child
* may be zombified by will get reaped by init
*/
stress_try_kill(args, pid, path);
return STRESS_TRY_OPEN_WAIT_FAIL;
}
/* Has pid gone? */
if ((kill(pid, 0) < 0) && (errno == ESRCH))
goto done;
/* Sleep and retry */
(void)shim_nanosleep_uint64(sleep_ns);
}
/* Give up, force kill */
stress_try_kill(args, pid, path);
done:
/* Seems like we can open the device successfully */
if (WIFEXITED(status))
return WEXITSTATUS(status);
return STRESS_TRY_OPEN_EXIT_FAIL;
}
#if defined(HAVE_LIB_RT) && \
defined(HAVE_TIMER_CREATE) && \
defined(HAVE_TIMER_DELETE) && \
defined(HAVE_TIMER_GETOVERRUN) && \
defined(HAVE_TIMER_SETTIME)
static void MLOCKED_TEXT stress_timer_handler(int sig)
{
(void)sig;
}
/*
* Try to open a file, return 0 if can open it, non-zero
* if it cannot be opened within timeout nanoseconds.
*/
int stress_open_timeout(
const char *name,
const char *path,
const int flags,
const unsigned long timeout_ns)
{
int ret, t_ret, tmp;
struct sigevent sev;
timer_t timerid;
struct itimerspec timer;
/*
* If a handler can't be installed then
* we can't test, so just return 0 and try
* it anyhow.
*/
ret = stress_sighandler(name, SIGRTMIN, stress_timer_handler, NULL);
if (ret < 0)
return open(path, flags);
/*
* Enable a timer to interrupt log open waits
*/
(void)memset(&sev, 0, sizeof(sev));
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGRTMIN;
sev.sigev_value.sival_ptr = &timerid;
t_ret = timer_create(CLOCK_REALTIME, &sev, &timerid);
if (!t_ret) {
timer.it_value.tv_sec = timeout_ns / STRESS_NANOSECOND;
timer.it_value.tv_nsec = timeout_ns % STRESS_NANOSECOND;
timer.it_interval.tv_sec = timer.it_value.tv_sec;
timer.it_interval.tv_nsec = timer.it_value.tv_nsec;
t_ret = timer_settime(timerid, 0, &timer, NULL);
}
ret = open(path, flags);
tmp = errno;
if (!t_ret)
(void)timer_delete(timerid);
errno = tmp;
return ret;
}
#else
int stress_open_timeout(
const char *name,
const char *path,
const int flags,
const unsigned long timeout_ns)
{
(void)name;
(void)timeout_ns;
return open(path, flags);
}
#endif