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