1 /*
2  * Copyright (C) 2013-2021 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * This code is a complete clean re-write of the stress tool by
19  * Colin Ian King <colin.king@canonical.com> and attempts to be
20  * backwardly compatible with the stress tool by Amos Waterland
21  * <apw@rossby.metr.ou.edu> but has more stress tests and more
22  * functionality.
23  *
24  */
25 #include "stress-ng.h"
26 
27 static const stress_help_t help[] = {
28 	{ NULL,	"tee N",	"start N workers exercising the tee system call" },
29 	{ NULL,	"tee-ops N",	"stop after N tee bogo operations" },
30 	{ NULL,	NULL,		NULL }
31 };
32 
33 #if defined(HAVE_TEE) &&	\
34     defined(SPLICE_F_NONBLOCK)
35 
36 #define TEE_IO_SIZE	(65536)
37 
38 /*
39  *  stress_tee_spawn()
40  *	spawn off tee I/O processes
41  */
stress_tee_spawn(const stress_args_t * args,void (* func)(int fds[2]),int fds[2])42 static pid_t stress_tee_spawn(
43 	const stress_args_t *args,
44 	void (*func)(int fds[2]),
45 	int fds[2])
46 {
47 	pid_t pid;
48 
49 	if (pipe(fds) < 0) {
50 		pr_err("%s: pipe failed: %d (%s)\n",
51 			args->name, errno, strerror(errno));
52 		return -1;
53 	}
54 
55 again:
56 	pid = fork();
57 	if (pid < 0) {
58 		if (stress_redo_fork(errno))
59 			goto again;
60 		(void)close(fds[0]);
61 		(void)close(fds[1]);
62 		if (!keep_stressing(args))
63 			return -1;
64 		pr_err("%s: fork failed: %d (%s)\n",
65 			args->name, errno, strerror(errno));
66 		return -1;
67 	}
68 	if (pid == 0) {
69 		(void)setpgid(0, g_pgrp);
70 		stress_parent_died_alarm();
71 		(void)sched_settings_apply(true);
72 
73 		func(fds);
74 		_exit(EXIT_SUCCESS);
75 	}
76 	(void)setpgid(pid, g_pgrp);
77 	return pid;
78 }
79 
80 /*
81  *  stress_tee_pipe_write()
82  *	write data down a pipe
83  */
stress_tee_pipe_write(int fds[2])84 static void stress_tee_pipe_write(int fds[2])
85 {
86 	static char buffer[TEE_IO_SIZE];
87 
88 	(void)close(fds[0]);
89 
90 	(void)memset(buffer, 0, sizeof(buffer));
91 	while (keep_stressing_flag()) {
92 		ssize_t ret;
93 
94 		ret = write(fds[1], buffer, sizeof(buffer));
95 		if (ret < 0) {
96 			if (errno != EAGAIN)
97 				break;
98 		}
99 	}
100 	(void)close(fds[1]);
101 }
102 
103 /*
104  *  stress_tee_pipe_read()
105  *	read data from a pipe
106  */
stress_tee_pipe_read(int fds[2])107 static void stress_tee_pipe_read(int fds[2])
108 {
109 	static char buffer[TEE_IO_SIZE];
110 
111 	(void)close(fds[1]);
112 
113 	while (keep_stressing_flag()) {
114 		ssize_t ret;
115 
116 		ret = read(fds[0], buffer, sizeof(buffer));
117 		if (ret < 0)
118 			if (errno != EAGAIN)
119 				break;
120 	}
121 	(void)close(fds[1]);
122 }
123 
124 /*
125  *  exercise_tee()
126  *	exercise the tee syscall in most possible ways
127  */
exercise_tee(const stress_args_t * args,const int release,const int fd_in,const int fd_out)128 static int exercise_tee(
129 	const stress_args_t *args,
130 	const int release,
131 	const int fd_in,
132 	const int fd_out)
133 {
134 	ssize_t ret;
135 
136 	if ((release != -1) &&
137             (release >= stress_kernel_release(4, 10, 0))) {
138 		/*
139 		 *  Linux commit 3d6ea290f337
140 		 *  ("splice/tee/vmsplice: validate flags")
141 		 *  added a check for flags against ~SPLICE_F_ALL
142 		 *  in Linux 4.10.  For now disable this test
143 		 *  as it is throwing errors for pre-4.10 kernels
144 		 */
145 		ret = tee(fd_in, fd_out, INT_MAX, ~0U);
146 		if (ret >= 0) {
147 			pr_fail("%s: tee with illegal flags "
148 				"unexpectedly succeeded\n",
149 				args->name);
150 			return -1;
151 		}
152 	}
153 
154 	/* Exercise on same pipe */
155 	ret = tee(fd_in, fd_in, INT_MAX, 0);
156 	if (ret >= 0) {
157 		pr_fail("%s: tee on same fd_out and fd_in "
158 			"unexpectedly succeeded\n",
159 			args->name);
160 		return -1;
161 	}
162 
163 	/*
164 	 * Exercise tee on with 0 len argument creating absolutely
165 	 * no difference other than increase in kernel test coverage
166 	 */
167 	ret = tee(fd_in, fd_out, 0, 0);
168 	if (ret < 0) {
169 		pr_fail("%s: tee with 0 len argument "
170 			"unexpectedly failed\n",
171 			args->name);
172 		return -1;
173 	}
174 
175 	return 0;
176 }
177 
178 /*
179  *  stress_tee()
180  *	stress the Linux tee syscall
181  */
stress_tee(const stress_args_t * args)182 static int stress_tee(const stress_args_t *args)
183 {
184 	ssize_t len, slen;
185 	int fd, pipe_in[2], pipe_out[2];
186 	pid_t pids[2];
187 	int ret = EXIT_FAILURE, status;
188 	const int release = stress_get_kernel_release();
189 
190 	fd = open("/dev/null", O_WRONLY);
191 	if (fd < 0) {
192 		pr_err("%s: open /dev/null failed: errno=%d (%s)\n",
193 			args->name, errno, strerror(errno));
194 		return EXIT_FAILURE;
195 	}
196 
197 	stress_set_proc_state(args->name, STRESS_STATE_RUN);
198 
199 	pids[0] = stress_tee_spawn(args, stress_tee_pipe_write, pipe_in);
200 	if (pids[0] < 0) {
201 		(void)close(fd);
202 		return EXIT_FAILURE;
203 	}
204 	(void)close(pipe_in[1]);
205 
206 	pids[1] = stress_tee_spawn(args, stress_tee_pipe_read, pipe_out);
207 	if (pids[0] < 0)
208 		goto tidy_child1;
209 	(void)close(pipe_out[0]);
210 
211 
212 	do {
213 		len = tee(pipe_in[0], pipe_out[1],
214 			INT_MAX, 0 & SPLICE_F_NONBLOCK);
215 
216 		if (len < 0) {
217 			if (errno == EAGAIN)
218 				continue;
219 			if (errno == EINTR)
220 				break;
221 			if (errno == ENOMEM) {
222 				pr_inf_skip("%s: skipping stressor, out of memory\n",
223 					args->name);
224 				ret = EXIT_NO_RESOURCE;
225 				goto tidy_child2;
226 			}
227 			pr_err("%s: tee failed: errno=%d (%s)\n",
228 				args->name, errno, strerror(errno));
229 			goto tidy_child2;
230 		} else {
231 			if (len == 0)
232 				break;
233 		}
234 
235 		while (len > 0) {
236 			slen = splice(pipe_in[0], NULL, fd, NULL,
237 				(size_t)len, SPLICE_F_MOVE);
238 			if (errno == EINTR)
239 				break;
240 			if (slen < 0) {
241 				pr_err("%s: splice failed: errno=%d (%s)\n",
242 					args->name, errno, strerror(errno));
243 				goto tidy_child2;
244 			}
245 			len -= slen;
246 		}
247 
248 		if (exercise_tee(args, release, pipe_in[0], pipe_out[1]) < 0)
249 			goto tidy_child2;
250 
251 		inc_counter(args);
252 	} while (keep_stressing(args));
253 
254 	ret = EXIT_SUCCESS;
255 
256 tidy_child2:
257 	stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
258 	(void)close(pipe_out[1]);
259 	(void)kill(pids[1], SIGKILL);
260 	(void)shim_waitpid(pids[1], &status, 0);
261 
262 tidy_child1:
263 	stress_set_proc_state(args->name, STRESS_STATE_DEINIT);
264 	(void)close(pipe_in[0]);
265 	(void)kill(pids[0], SIGKILL);
266 	(void)shim_waitpid(pids[0], &status, 0);
267 
268 	(void)close(fd);
269 
270 	return ret;
271 }
272 
273 stressor_info_t stress_tee_info = {
274 	.stressor = stress_tee,
275 	.class = CLASS_PIPE_IO | CLASS_OS | CLASS_SCHEDULER,
276 	.help = help
277 };
278 #else
279 stressor_info_t stress_tee_info = {
280 	.stressor = stress_not_implemented,
281 	.class = CLASS_PIPE_IO | CLASS_OS | CLASS_SCHEDULER,
282 	.help = help
283 };
284 #endif
285