1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci *
3f08c3bdfSopenharmony_ci *   Copyright (c) International Business Machines  Corp., 2002
4f08c3bdfSopenharmony_ci *   Copyright (c) Cyril Hrubis chrubis@suse.cz 2009
5f08c3bdfSopenharmony_ci *
6f08c3bdfSopenharmony_ci *   This program is free software;  you can redistribute it and/or modify
7f08c3bdfSopenharmony_ci *   it under the terms of the GNU General Public License as published by
8f08c3bdfSopenharmony_ci *   the Free Software Foundation; either version 2 of the License, or
9f08c3bdfSopenharmony_ci *   (at your option) any later version.
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci *   This program is distributed in the hope that it will be useful,
12f08c3bdfSopenharmony_ci *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
13f08c3bdfSopenharmony_ci *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14f08c3bdfSopenharmony_ci *   the GNU General Public License for more details.
15f08c3bdfSopenharmony_ci *
16f08c3bdfSopenharmony_ci *   You should have received a copy of the GNU General Public License
17f08c3bdfSopenharmony_ci *   along with this program;  if not, write to the Free Software
18f08c3bdfSopenharmony_ci *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19f08c3bdfSopenharmony_ci */
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci/*
22f08c3bdfSopenharmony_ci * NAME
23f08c3bdfSopenharmony_ci *	ftest02.c -- test inode things (ported from SPIE section2, filesuite, by Airong Zhang)
24f08c3bdfSopenharmony_ci *
25f08c3bdfSopenharmony_ci * CALLS
26f08c3bdfSopenharmony_ci *	open, close,  read, write, lseek,
27f08c3bdfSopenharmony_ci *	unlink, chdir
28f08c3bdfSopenharmony_ci *
29f08c3bdfSopenharmony_ci *
30f08c3bdfSopenharmony_ci * ALGORITHM
31f08c3bdfSopenharmony_ci *
32f08c3bdfSopenharmony_ci *
33f08c3bdfSopenharmony_ci *	ftest02 [-f tmpdirname] nchild iterations [partition]
34f08c3bdfSopenharmony_ci *
35f08c3bdfSopenharmony_ci *	This forks some child processes, they do some random operations
36f08c3bdfSopenharmony_ci *	which use lots of directory operations.
37f08c3bdfSopenharmony_ci *
38f08c3bdfSopenharmony_ci * RESTRICTIONS
39f08c3bdfSopenharmony_ci *	Runs a long time with default args - can take others on input
40f08c3bdfSopenharmony_ci *	line.  Use with "term mode".
41f08c3bdfSopenharmony_ci *	If run on vax the ftruncate will not be random - will always go to
42f08c3bdfSopenharmony_ci *	start of file.  NOTE: produces a very high load average!!
43f08c3bdfSopenharmony_ci *
44f08c3bdfSopenharmony_ci */
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci#include <stdio.h>
47f08c3bdfSopenharmony_ci#include <sys/types.h>
48f08c3bdfSopenharmony_ci#include <sys/param.h>
49f08c3bdfSopenharmony_ci#include <sys/wait.h>
50f08c3bdfSopenharmony_ci#include <fcntl.h>
51f08c3bdfSopenharmony_ci#include <sys/stat.h>
52f08c3bdfSopenharmony_ci#include <errno.h>
53f08c3bdfSopenharmony_ci#include <sys/mount.h>
54f08c3bdfSopenharmony_ci#include <signal.h>
55f08c3bdfSopenharmony_ci#include <unistd.h>
56f08c3bdfSopenharmony_ci#include "test.h"
57f08c3bdfSopenharmony_ci#include "libftest.h"
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci#define MAXCHILD	25
60f08c3bdfSopenharmony_ci#define K_1		1024
61f08c3bdfSopenharmony_ci#define K_2		2048
62f08c3bdfSopenharmony_ci#define K_4		4096
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_cichar *TCID = "ftest02";
65f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci#define PASSED 1
68f08c3bdfSopenharmony_ci#define FAILED 0
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_cistatic void crfile(int, int);
71f08c3bdfSopenharmony_cistatic void unlfile(int, int);
72f08c3bdfSopenharmony_cistatic void fussdir(int, int);
73f08c3bdfSopenharmony_cistatic void dotest(int, int);
74f08c3bdfSopenharmony_cistatic void dowarn(int, char *, char *);
75f08c3bdfSopenharmony_cistatic void term(int sig);
76f08c3bdfSopenharmony_cistatic void cleanup(void);
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci#define	M	(1024*1024)
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_cistatic int iterations;
81f08c3bdfSopenharmony_cistatic int nchild;
82f08c3bdfSopenharmony_cistatic int parent_pid;
83f08c3bdfSopenharmony_cistatic int pidlist[MAXCHILD];
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_cistatic char homedir[MAXPATHLEN];
86f08c3bdfSopenharmony_cistatic char dirname[MAXPATHLEN];
87f08c3bdfSopenharmony_cistatic char tmpname[MAXPATHLEN];
88f08c3bdfSopenharmony_cistatic int dirlen;
89f08c3bdfSopenharmony_cistatic int mnt = 0;
90f08c3bdfSopenharmony_cistatic char startdir[MAXPATHLEN], mntpoint[MAXPATHLEN];
91f08c3bdfSopenharmony_cistatic char *partition;
92f08c3bdfSopenharmony_cistatic char *cwd;
93f08c3bdfSopenharmony_cistatic char *fstyp;
94f08c3bdfSopenharmony_cistatic int local_flag;
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ciint main(void)
97f08c3bdfSopenharmony_ci{
98f08c3bdfSopenharmony_ci	int k, j, pid, child, status, count;
99f08c3bdfSopenharmony_ci	char name[128];
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci	/*
102f08c3bdfSopenharmony_ci	 * Default values for run conditions.
103f08c3bdfSopenharmony_ci	 */
104f08c3bdfSopenharmony_ci	iterations = 50;
105f08c3bdfSopenharmony_ci	nchild = 5;
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_ci	if (signal(SIGTERM, term) == SIG_ERR) {
108f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "first signal failed");
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_ci	}
111f08c3bdfSopenharmony_ci
112f08c3bdfSopenharmony_ci	/*
113f08c3bdfSopenharmony_ci	 * Make a directory to do this in; ignore error if already exists.
114f08c3bdfSopenharmony_ci	 */
115f08c3bdfSopenharmony_ci	local_flag = PASSED;
116f08c3bdfSopenharmony_ci	parent_pid = getpid();
117f08c3bdfSopenharmony_ci	tst_tmpdir();
118f08c3bdfSopenharmony_ci
119f08c3bdfSopenharmony_ci	if (!startdir[0]) {
120f08c3bdfSopenharmony_ci		if (getcwd(startdir, MAXPATHLEN) == NULL) {
121f08c3bdfSopenharmony_ci			tst_resm(TBROK, "getcwd failed");
122f08c3bdfSopenharmony_ci
123f08c3bdfSopenharmony_ci		}
124f08c3bdfSopenharmony_ci	}
125f08c3bdfSopenharmony_ci	cwd = startdir;
126f08c3bdfSopenharmony_ci	strcat(dirname, cwd);
127f08c3bdfSopenharmony_ci	sprintf(tmpname, "/ftest02.%d", getpid());
128f08c3bdfSopenharmony_ci	strcat(dirname, tmpname);
129f08c3bdfSopenharmony_ci	strcat(homedir, cwd);
130f08c3bdfSopenharmony_ci	sprintf(tmpname, "/ftest02h.%d", getpid());
131f08c3bdfSopenharmony_ci	strcat(homedir, tmpname);
132f08c3bdfSopenharmony_ci
133f08c3bdfSopenharmony_ci	mkdir(dirname, 0755);
134f08c3bdfSopenharmony_ci	mkdir(homedir, 0755);
135f08c3bdfSopenharmony_ci	if (chdir(dirname) < 0) {
136f08c3bdfSopenharmony_ci		tst_resm(TBROK, "\tCan't chdir(%s), error %d.", dirname, errno);
137f08c3bdfSopenharmony_ci		cleanup();
138f08c3bdfSopenharmony_ci
139f08c3bdfSopenharmony_ci	}
140f08c3bdfSopenharmony_ci	dirlen = strlen(dirname);
141f08c3bdfSopenharmony_ci	if (chdir(homedir) < 0) {
142f08c3bdfSopenharmony_ci		tst_resm(TBROK, "\tCan't chdir(%s), error %d.", homedir, errno);
143f08c3bdfSopenharmony_ci		cleanup();
144f08c3bdfSopenharmony_ci
145f08c3bdfSopenharmony_ci	}
146f08c3bdfSopenharmony_ci
147f08c3bdfSopenharmony_ci	for (k = 0; k < nchild; k++) {
148f08c3bdfSopenharmony_ci		if ((child = fork()) == 0) {
149f08c3bdfSopenharmony_ci			dotest(k, iterations);
150f08c3bdfSopenharmony_ci			exit(0);
151f08c3bdfSopenharmony_ci		}
152f08c3bdfSopenharmony_ci		if (child < 0) {
153f08c3bdfSopenharmony_ci			tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
154f08c3bdfSopenharmony_ci		}
155f08c3bdfSopenharmony_ci		pidlist[k] = child;
156f08c3bdfSopenharmony_ci	}
157f08c3bdfSopenharmony_ci
158f08c3bdfSopenharmony_ci	/*
159f08c3bdfSopenharmony_ci	 * Wait for children to finish.
160f08c3bdfSopenharmony_ci	 */
161f08c3bdfSopenharmony_ci	count = 0;
162f08c3bdfSopenharmony_ci	while ((child = wait(&status)) > 0) {
163f08c3bdfSopenharmony_ci		//tst_resm(TINFO,"Test{%d} exited status = 0x%x", child, status);
164f08c3bdfSopenharmony_ci		//tst_resm(TINFO,"status is %d",status);
165f08c3bdfSopenharmony_ci		if (status) {
166f08c3bdfSopenharmony_ci			tst_resm(TFAIL, "Test{%d} failed, expected 0 exit.",
167f08c3bdfSopenharmony_ci				 child);
168f08c3bdfSopenharmony_ci			local_flag = FAILED;
169f08c3bdfSopenharmony_ci		}
170f08c3bdfSopenharmony_ci		++count;
171f08c3bdfSopenharmony_ci	}
172f08c3bdfSopenharmony_ci
173f08c3bdfSopenharmony_ci	/*
174f08c3bdfSopenharmony_ci	 * Should have collected all children.
175f08c3bdfSopenharmony_ci	 */
176f08c3bdfSopenharmony_ci	if (count != nchild) {
177f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "Wrong # children waited on, count = %d",
178f08c3bdfSopenharmony_ci			 count);
179f08c3bdfSopenharmony_ci		local_flag = FAILED;
180f08c3bdfSopenharmony_ci	}
181f08c3bdfSopenharmony_ci
182f08c3bdfSopenharmony_ci	if (local_flag == FAILED)
183f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "Test failed in fork-wait part.");
184f08c3bdfSopenharmony_ci	else
185f08c3bdfSopenharmony_ci		tst_resm(TPASS, "Test passed in fork-wait part.");
186f08c3bdfSopenharmony_ci
187f08c3bdfSopenharmony_ci	if (iterations > 26)
188f08c3bdfSopenharmony_ci		iterations = 26;
189f08c3bdfSopenharmony_ci
190f08c3bdfSopenharmony_ci	for (k = 0; k < nchild; k++)
191f08c3bdfSopenharmony_ci		for (j = 0; j < iterations + 1; j++) {
192f08c3bdfSopenharmony_ci			ft_mkname(name, dirname, k, j);
193f08c3bdfSopenharmony_ci			rmdir(name);
194f08c3bdfSopenharmony_ci			unlink(name);
195f08c3bdfSopenharmony_ci		}
196f08c3bdfSopenharmony_ci
197f08c3bdfSopenharmony_ci	chdir(startdir);
198f08c3bdfSopenharmony_ci
199f08c3bdfSopenharmony_ci	pid = fork();
200f08c3bdfSopenharmony_ci
201f08c3bdfSopenharmony_ci	if (pid < 0) {
202f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
203f08c3bdfSopenharmony_ci	}
204f08c3bdfSopenharmony_ci
205f08c3bdfSopenharmony_ci	if (pid == 0) {
206f08c3bdfSopenharmony_ci		execl("/bin/rm", "rm", "-rf", homedir, NULL);
207f08c3bdfSopenharmony_ci		exit(1);
208f08c3bdfSopenharmony_ci	} else
209f08c3bdfSopenharmony_ci		wait(&status);
210f08c3bdfSopenharmony_ci
211f08c3bdfSopenharmony_ci	if (status)
212f08c3bdfSopenharmony_ci		tst_resm(TINFO,
213f08c3bdfSopenharmony_ci			 "CAUTION - ftest02, '%s' may not have been removed.",
214f08c3bdfSopenharmony_ci			 homedir);
215f08c3bdfSopenharmony_ci
216f08c3bdfSopenharmony_ci	pid = fork();
217f08c3bdfSopenharmony_ci
218f08c3bdfSopenharmony_ci	if (pid < 0) {
219f08c3bdfSopenharmony_ci		tst_brkm(TBROK | TERRNO, cleanup, "fork failed");
220f08c3bdfSopenharmony_ci	}
221f08c3bdfSopenharmony_ci
222f08c3bdfSopenharmony_ci	if (pid == 0) {
223f08c3bdfSopenharmony_ci		execl("/bin/rm", "rm", "-rf", dirname, NULL);
224f08c3bdfSopenharmony_ci		exit(1);
225f08c3bdfSopenharmony_ci	} else
226f08c3bdfSopenharmony_ci		wait(&status);
227f08c3bdfSopenharmony_ci
228f08c3bdfSopenharmony_ci	if (status) {
229f08c3bdfSopenharmony_ci		tst_resm(TINFO,
230f08c3bdfSopenharmony_ci			 "CAUTION - ftest02, '%s' may not have been removed.",
231f08c3bdfSopenharmony_ci			 dirname);
232f08c3bdfSopenharmony_ci	}
233f08c3bdfSopenharmony_ci
234f08c3bdfSopenharmony_ci	sync();
235f08c3bdfSopenharmony_ci
236f08c3bdfSopenharmony_ci	cleanup();
237f08c3bdfSopenharmony_ci
238f08c3bdfSopenharmony_ci	tst_exit();
239f08c3bdfSopenharmony_ci}
240f08c3bdfSopenharmony_ci
241f08c3bdfSopenharmony_ci#define	warn(val,m1,m2)	if ((val) < 0) dowarn(me,m1,m2)
242f08c3bdfSopenharmony_ci
243f08c3bdfSopenharmony_ci/*
244f08c3bdfSopenharmony_ci * crfile()
245f08c3bdfSopenharmony_ci *	Create a file and write something into it.
246f08c3bdfSopenharmony_ci */
247f08c3bdfSopenharmony_ci
248f08c3bdfSopenharmony_cichar crmsg[] = "Gee, let's write something in the file!\n";
249f08c3bdfSopenharmony_ci
250f08c3bdfSopenharmony_cistatic void crfile(int me, int count)
251f08c3bdfSopenharmony_ci{
252f08c3bdfSopenharmony_ci	int fd, val;
253f08c3bdfSopenharmony_ci	char fname[MAXPATHLEN], buf[MAXPATHLEN];
254f08c3bdfSopenharmony_ci
255f08c3bdfSopenharmony_ci	ft_mkname(fname, dirname, me, count);
256f08c3bdfSopenharmony_ci
257f08c3bdfSopenharmony_ci	fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
258f08c3bdfSopenharmony_ci	if (fd < 0 && errno == EISDIR) {
259f08c3bdfSopenharmony_ci		val = rmdir(fname);
260f08c3bdfSopenharmony_ci		warn(val, "rmdir", fname);
261f08c3bdfSopenharmony_ci		fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
262f08c3bdfSopenharmony_ci	}
263f08c3bdfSopenharmony_ci	warn(fd, "creating", fname);
264f08c3bdfSopenharmony_ci
265f08c3bdfSopenharmony_ci	val = lseek(fd, (rand() % M), 0);
266f08c3bdfSopenharmony_ci	warn(val, "lseek", 0);
267f08c3bdfSopenharmony_ci
268f08c3bdfSopenharmony_ci	val = write(fd, crmsg, sizeof(crmsg) - 1);
269f08c3bdfSopenharmony_ci	warn(val, "write", 0);
270f08c3bdfSopenharmony_ci
271f08c3bdfSopenharmony_ci	val = lseek(fd, -((off_t) sizeof(crmsg) - 1), 1);
272f08c3bdfSopenharmony_ci	warn(val, "lseek", 0);
273f08c3bdfSopenharmony_ci
274f08c3bdfSopenharmony_ci	val = read(fd, buf, sizeof(crmsg) - 1);
275f08c3bdfSopenharmony_ci	warn(val, "read", 0);
276f08c3bdfSopenharmony_ci
277f08c3bdfSopenharmony_ci	if (strncmp(crmsg, buf, sizeof(crmsg) - 1))
278f08c3bdfSopenharmony_ci		dowarn(me, "compare", 0);
279f08c3bdfSopenharmony_ci
280f08c3bdfSopenharmony_ci	val = close(fd);
281f08c3bdfSopenharmony_ci	warn(val, "close", 0);
282f08c3bdfSopenharmony_ci}
283f08c3bdfSopenharmony_ci
284f08c3bdfSopenharmony_ci/*
285f08c3bdfSopenharmony_ci * unlfile()
286f08c3bdfSopenharmony_ci *	Unlink some of the files.
287f08c3bdfSopenharmony_ci */
288f08c3bdfSopenharmony_cistatic void unlfile(int me, int count)
289f08c3bdfSopenharmony_ci{
290f08c3bdfSopenharmony_ci	int i;
291f08c3bdfSopenharmony_ci	int val;
292f08c3bdfSopenharmony_ci	char fname[MAXPATHLEN];
293f08c3bdfSopenharmony_ci
294f08c3bdfSopenharmony_ci	i = count - 10;
295f08c3bdfSopenharmony_ci
296f08c3bdfSopenharmony_ci	if (i < 0)
297f08c3bdfSopenharmony_ci		i = 0;
298f08c3bdfSopenharmony_ci
299f08c3bdfSopenharmony_ci	for (; i < count; i++) {
300f08c3bdfSopenharmony_ci		ft_mkname(fname, dirname, me, i);
301f08c3bdfSopenharmony_ci		val = rmdir(fname);
302f08c3bdfSopenharmony_ci		if (val < 0)
303f08c3bdfSopenharmony_ci			val = unlink(fname);
304f08c3bdfSopenharmony_ci		if (val == 0 || errno == ENOENT)
305f08c3bdfSopenharmony_ci			continue;
306f08c3bdfSopenharmony_ci		dowarn(me, "unlink", fname);
307f08c3bdfSopenharmony_ci	}
308f08c3bdfSopenharmony_ci}
309f08c3bdfSopenharmony_ci
310f08c3bdfSopenharmony_ci/*
311f08c3bdfSopenharmony_ci * fussdir()
312f08c3bdfSopenharmony_ci *	Make a directory, put stuff in it, remove it, and remove directory.
313f08c3bdfSopenharmony_ci *
314f08c3bdfSopenharmony_ci * Randomly leave the directory there.
315f08c3bdfSopenharmony_ci */
316f08c3bdfSopenharmony_cistatic void fussdir(int me, int count)
317f08c3bdfSopenharmony_ci{
318f08c3bdfSopenharmony_ci	int val;
319f08c3bdfSopenharmony_ci	char dir[MAXPATHLEN], fname[MAXPATHLEN], savedir[MAXPATHLEN];
320f08c3bdfSopenharmony_ci
321f08c3bdfSopenharmony_ci	ft_mkname(dir, dirname, me, count);
322f08c3bdfSopenharmony_ci	rmdir(dir);
323f08c3bdfSopenharmony_ci	unlink(dir);
324f08c3bdfSopenharmony_ci
325f08c3bdfSopenharmony_ci	val = mkdir(dir, 0755);
326f08c3bdfSopenharmony_ci	warn(val, "mkdir", dir);
327f08c3bdfSopenharmony_ci
328f08c3bdfSopenharmony_ci	/*
329f08c3bdfSopenharmony_ci	 * Arrange to create files in the directory.
330f08c3bdfSopenharmony_ci	 */
331f08c3bdfSopenharmony_ci	strcpy(savedir, dirname);
332f08c3bdfSopenharmony_ci	strcpy(dirname, "");
333f08c3bdfSopenharmony_ci
334f08c3bdfSopenharmony_ci	val = chdir(dir);
335f08c3bdfSopenharmony_ci	warn(val, "chdir", dir);
336f08c3bdfSopenharmony_ci
337f08c3bdfSopenharmony_ci	crfile(me, count);
338f08c3bdfSopenharmony_ci	crfile(me, count + 1);
339f08c3bdfSopenharmony_ci
340f08c3bdfSopenharmony_ci	val = chdir("..");
341f08c3bdfSopenharmony_ci	warn(val, "chdir", "..");
342f08c3bdfSopenharmony_ci
343f08c3bdfSopenharmony_ci	val = rmdir(dir);
344f08c3bdfSopenharmony_ci
345f08c3bdfSopenharmony_ci	if (val >= 0) {
346f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL,
347f08c3bdfSopenharmony_ci			 "Test[%d]: rmdir of non-empty %s succeeds!", me,
348f08c3bdfSopenharmony_ci			 dir);
349f08c3bdfSopenharmony_ci	}
350f08c3bdfSopenharmony_ci
351f08c3bdfSopenharmony_ci	val = chdir(dir);
352f08c3bdfSopenharmony_ci	warn(val, "chdir", dir);
353f08c3bdfSopenharmony_ci
354f08c3bdfSopenharmony_ci	ft_mkname(fname, dirname, me, count);
355f08c3bdfSopenharmony_ci	val = unlink(fname);
356f08c3bdfSopenharmony_ci	warn(val, "unlink", fname);
357f08c3bdfSopenharmony_ci
358f08c3bdfSopenharmony_ci	ft_mkname(fname, dirname, me, count + 1);
359f08c3bdfSopenharmony_ci	val = unlink(fname);
360f08c3bdfSopenharmony_ci	warn(val, "unlink", fname);
361f08c3bdfSopenharmony_ci
362f08c3bdfSopenharmony_ci	val = chdir(homedir);
363f08c3bdfSopenharmony_ci	warn(val, "chdir", homedir);
364f08c3bdfSopenharmony_ci
365f08c3bdfSopenharmony_ci	if (rand() & 0x01) {
366f08c3bdfSopenharmony_ci		val = rmdir(dir);
367f08c3bdfSopenharmony_ci		warn(val, "rmdir", dir);
368f08c3bdfSopenharmony_ci	}
369f08c3bdfSopenharmony_ci
370f08c3bdfSopenharmony_ci	strcpy(dirname, savedir);
371f08c3bdfSopenharmony_ci}
372f08c3bdfSopenharmony_ci
373f08c3bdfSopenharmony_ci/*
374f08c3bdfSopenharmony_ci * dotest()
375f08c3bdfSopenharmony_ci *	Children execute this.
376f08c3bdfSopenharmony_ci *
377f08c3bdfSopenharmony_ci * Randomly do an inode thing; loop for # iterations.
378f08c3bdfSopenharmony_ci */
379f08c3bdfSopenharmony_ci#define	THING(p)	{p, "p"}
380f08c3bdfSopenharmony_ci
381f08c3bdfSopenharmony_cistruct ino_thing {
382f08c3bdfSopenharmony_ci	void (*it_proc) ();
383f08c3bdfSopenharmony_ci	char *it_name;
384f08c3bdfSopenharmony_ci} ino_thing[] = {
385f08c3bdfSopenharmony_ciTHING(crfile), THING(unlfile), THING(fussdir), THING(sync),};
386f08c3bdfSopenharmony_ci
387f08c3bdfSopenharmony_ci#define	NTHING	ARRAY_SIZE(ino_thing)
388f08c3bdfSopenharmony_ci
389f08c3bdfSopenharmony_ciint thing_cnt[NTHING];
390f08c3bdfSopenharmony_ciint thing_last[NTHING];
391f08c3bdfSopenharmony_ci
392f08c3bdfSopenharmony_cistatic void dotest(int me, int count)
393f08c3bdfSopenharmony_ci{
394f08c3bdfSopenharmony_ci	int i, thing;
395f08c3bdfSopenharmony_ci
396f08c3bdfSopenharmony_ci	//tst_resm(TINFO,"Test %d pid %d starting.", me, getpid());
397f08c3bdfSopenharmony_ci
398f08c3bdfSopenharmony_ci	srand(getpid());
399f08c3bdfSopenharmony_ci
400f08c3bdfSopenharmony_ci	for (i = 0; i < count; i++) {
401f08c3bdfSopenharmony_ci		thing = (rand() >> 3) % NTHING;
402f08c3bdfSopenharmony_ci		(*ino_thing[thing].it_proc) (me, i, ino_thing[thing].it_name);
403f08c3bdfSopenharmony_ci		++thing_cnt[thing];
404f08c3bdfSopenharmony_ci	}
405f08c3bdfSopenharmony_ci
406f08c3bdfSopenharmony_ci	//tst_resm(TINFO,"Test %d pid %d exiting.", me, getpid());
407f08c3bdfSopenharmony_ci}
408f08c3bdfSopenharmony_ci
409f08c3bdfSopenharmony_cistatic void dowarn(int me, char *m1, char *m2)
410f08c3bdfSopenharmony_ci{
411f08c3bdfSopenharmony_ci	int err = errno;
412f08c3bdfSopenharmony_ci
413f08c3bdfSopenharmony_ci	tst_brkm(TBROK, NULL, "Test[%d]: error %d on %s %s",
414f08c3bdfSopenharmony_ci		 me, err, m1, (m2 ? m2 : ""));
415f08c3bdfSopenharmony_ci}
416f08c3bdfSopenharmony_ci
417f08c3bdfSopenharmony_ci/*
418f08c3bdfSopenharmony_ci * SIGTERM signal handler.
419f08c3bdfSopenharmony_ci */
420f08c3bdfSopenharmony_cistatic void term(int sig LTP_ATTRIBUTE_UNUSED)
421f08c3bdfSopenharmony_ci{
422f08c3bdfSopenharmony_ci	int i;
423f08c3bdfSopenharmony_ci
424f08c3bdfSopenharmony_ci	if (parent_pid == getpid()) {
425f08c3bdfSopenharmony_ci		for (i = 0; i < nchild; i++)
426f08c3bdfSopenharmony_ci			if (pidlist[i])
427f08c3bdfSopenharmony_ci				kill(pidlist[i], SIGTERM);
428f08c3bdfSopenharmony_ci		return;
429f08c3bdfSopenharmony_ci	}
430f08c3bdfSopenharmony_ci
431f08c3bdfSopenharmony_ci	tst_brkm(TBROK, NULL, "Child process exiting.");
432f08c3bdfSopenharmony_ci}
433f08c3bdfSopenharmony_ci
434f08c3bdfSopenharmony_cistatic void cleanup(void)
435f08c3bdfSopenharmony_ci{
436f08c3bdfSopenharmony_ci	char mount_buffer[1024];
437f08c3bdfSopenharmony_ci
438f08c3bdfSopenharmony_ci	if (mnt == 1) {
439f08c3bdfSopenharmony_ci
440f08c3bdfSopenharmony_ci		if (chdir(startdir) < 0)
441f08c3bdfSopenharmony_ci			tst_resm(TBROK, "Could not change to %s ", startdir);
442f08c3bdfSopenharmony_ci
443f08c3bdfSopenharmony_ci		if (!strcmp(fstyp, "cfs")) {
444f08c3bdfSopenharmony_ci
445f08c3bdfSopenharmony_ci			sprintf(mount_buffer, "/bin/umount %s", partition);
446f08c3bdfSopenharmony_ci
447f08c3bdfSopenharmony_ci			if (system(mount_buffer) != 0) {
448f08c3bdfSopenharmony_ci
449f08c3bdfSopenharmony_ci				tst_resm(TBROK, "Unable to unmount %s from %s ",
450f08c3bdfSopenharmony_ci					 partition, mntpoint);
451f08c3bdfSopenharmony_ci
452f08c3bdfSopenharmony_ci				if (umount(partition))
453f08c3bdfSopenharmony_ci					tst_resm(TBROK,
454f08c3bdfSopenharmony_ci						 "Unable to unmount %s from %s ",
455f08c3bdfSopenharmony_ci						 partition, mntpoint);
456f08c3bdfSopenharmony_ci				else
457f08c3bdfSopenharmony_ci					tst_resm(TINFO,
458f08c3bdfSopenharmony_ci						 "Forced umount for %s, /etc/mtab now dirty",
459f08c3bdfSopenharmony_ci						 partition);
460f08c3bdfSopenharmony_ci			}
461f08c3bdfSopenharmony_ci
462f08c3bdfSopenharmony_ci		} else if (umount(partition))
463f08c3bdfSopenharmony_ci			tst_resm(TBROK, "Unable to unmount %s from %s ",
464f08c3bdfSopenharmony_ci				 partition, mntpoint);
465f08c3bdfSopenharmony_ci
466f08c3bdfSopenharmony_ci		if (rmdir(mntpoint) != 0)
467f08c3bdfSopenharmony_ci			tst_resm(TBROK, "Unable to rmdir %s ", mntpoint);
468f08c3bdfSopenharmony_ci
469f08c3bdfSopenharmony_ci	}
470f08c3bdfSopenharmony_ci
471f08c3bdfSopenharmony_ci	tst_rmdir();
472f08c3bdfSopenharmony_ci}
473