forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-daemon.c
134 lines (121 loc) · 2.9 KB
/
stress-daemon.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
/*
* 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"
/*
* daemons()
* fork off a child and let the parent die
*/
static void daemons(const args_t *args, const int fd)
{
int fds[3];
if (stress_sig_stop_stressing(args->name, SIGALRM) < 0)
goto err;
if (setsid() < 0)
goto err;
(void)close(0);
(void)close(1);
(void)close(2);
if ((fds[0] = open("/dev/null", O_RDWR)) < 0)
goto err;
if ((fds[1] = dup(0)) < 0)
goto err0;
if ((fds[2] = dup(0)) < 0)
goto err1;
while (g_keep_stressing_flag) {
pid_t pid;
pid = fork();
if (pid < 0) {
goto tidy;
} else if (pid == 0) {
/* Child */
char buf[1] = { 0xff };
ssize_t sz;
if (chdir("/") < 0)
goto err2;
(void)umask(0);
sz = write(fd, buf, sizeof(buf));
if (sz != sizeof(buf))
goto err2;
} else {
/* Parent, will be reaped by init */
break;
}
}
tidy:
(void)close(fds[2]);
(void)close(fds[1]);
(void)close(fds[0]);
return;
err2: (void)close(fds[2]);
err1: (void)close(fds[1]);
err0: (void)close(fds[0]);
err: (void)close(fd);
}
/*
* stress_daemon()
* stress by multiple daemonizing forks
*/
int stress_daemon(const args_t *args)
{
int fds[2];
pid_t pid;
if (stress_sig_stop_stressing(args->name, SIGALRM) < 0)
return EXIT_FAILURE;
if (pipe(fds) < 0) {
pr_fail_dbg("pipe");
return EXIT_FAILURE;
}
pid = fork();
if (pid < 0) {
pr_fail_dbg("fork");
(void)close(fds[0]);
(void)close(fds[1]);
return EXIT_FAILURE;
} else if (pid == 0) {
/* Children */
(void)close(fds[0]);
daemons(args, fds[1]);
(void)close(fds[1]);
} else {
/* Parent */
(void)close(fds[1]);
do {
ssize_t n;
char buf[1];
n = read(fds[0], buf, sizeof(buf));
if (n < 0) {
(void)close(fds[0]);
if (errno != EINTR) {
pr_dbg("read failed: "
"errno=%d (%s)\n",
errno, strerror(errno));
}
break;
}
inc_counter(args);
} while (keep_stressing());
}
return EXIT_SUCCESS;
}