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