xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fdatasync/fdatasync02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or modify it
5*49cdfc7eSAndroid Build Coastguard Worker  * under the terms of version 2 of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation.
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful, but
9*49cdfc7eSAndroid Build Coastguard Worker  * WITHOUT ANY WARRANTY; without even the implied warranty of
10*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License along
13*49cdfc7eSAndroid Build Coastguard Worker  * with this program; if not, write the Free Software Foundation, Inc.,
14*49cdfc7eSAndroid Build Coastguard Worker  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker /**********************************************************
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  *    TEST IDENTIFIER   : fdatasync02
20*49cdfc7eSAndroid Build Coastguard Worker  *
21*49cdfc7eSAndroid Build Coastguard Worker  *    EXECUTED BY       : Any user
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  *    TEST TITLE        : Checking error conditions for fdatasync(2)
24*49cdfc7eSAndroid Build Coastguard Worker  *
25*49cdfc7eSAndroid Build Coastguard Worker  *    TEST CASE TOTAL   : 2
26*49cdfc7eSAndroid Build Coastguard Worker  *
27*49cdfc7eSAndroid Build Coastguard Worker  *    AUTHOR            : Madhu T L <[email protected]>
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  *    SIGNALS
30*49cdfc7eSAndroid Build Coastguard Worker  *      Uses SIGUSR1 to pause before test if option set.
31*49cdfc7eSAndroid Build Coastguard Worker  *      (See the parse_opts(3) man page).
32*49cdfc7eSAndroid Build Coastguard Worker  *
33*49cdfc7eSAndroid Build Coastguard Worker  *    DESCRIPTION
34*49cdfc7eSAndroid Build Coastguard Worker  *      Verify that,
35*49cdfc7eSAndroid Build Coastguard Worker  *      1. fdatasync(2) returns -1 and sets errno to EBADF for invalid
36*49cdfc7eSAndroid Build Coastguard Worker  *	   file descriptor.
37*49cdfc7eSAndroid Build Coastguard Worker  *      2. fdatasync(2) returns -1 and sets errno to EINVAL for file
38*49cdfc7eSAndroid Build Coastguard Worker  *         descriptor to a special file.
39*49cdfc7eSAndroid Build Coastguard Worker  *
40*49cdfc7eSAndroid Build Coastguard Worker  *      Setup:
41*49cdfc7eSAndroid Build Coastguard Worker  *        Setup signal handling.
42*49cdfc7eSAndroid Build Coastguard Worker  *        Set expected errnos for logging
43*49cdfc7eSAndroid Build Coastguard Worker  *        Pause for SIGUSR1 if option specified.
44*49cdfc7eSAndroid Build Coastguard Worker  *
45*49cdfc7eSAndroid Build Coastguard Worker  *      Test:
46*49cdfc7eSAndroid Build Coastguard Worker  *       Loop if the proper options are given.
47*49cdfc7eSAndroid Build Coastguard Worker  *	  Perform testcase specific setup (if needed)
48*49cdfc7eSAndroid Build Coastguard Worker  *        Execute system call
49*49cdfc7eSAndroid Build Coastguard Worker  *        Check return code and error number, if matching,
50*49cdfc7eSAndroid Build Coastguard Worker  *                   Issue PASS message
51*49cdfc7eSAndroid Build Coastguard Worker  *        Otherwise,
52*49cdfc7eSAndroid Build Coastguard Worker  *                   Issue FAIL message
53*49cdfc7eSAndroid Build Coastguard Worker  *	  Perform testcase specific cleanup (if needed)
54*49cdfc7eSAndroid Build Coastguard Worker  *
55*49cdfc7eSAndroid Build Coastguard Worker  *      Cleanup:
56*49cdfc7eSAndroid Build Coastguard Worker  *        Print errno log and/or timing stats if options given
57*49cdfc7eSAndroid Build Coastguard Worker  *
58*49cdfc7eSAndroid Build Coastguard Worker  * USAGE:  <for command-line>
59*49cdfc7eSAndroid Build Coastguard Worker  *  fdatasync02 [-c n] [-e] [-f] [-h] [-i n] [-I x] [-p] [-P x] [-t]
60*49cdfc7eSAndroid Build Coastguard Worker  *		where,  -c n : Run n copies concurrently.
61*49cdfc7eSAndroid Build Coastguard Worker  *			-e   : Turn on errno logging.
62*49cdfc7eSAndroid Build Coastguard Worker  *			-f   : Turn off functional testing
63*49cdfc7eSAndroid Build Coastguard Worker  *			-h   : Show help screen
64*49cdfc7eSAndroid Build Coastguard Worker  *			-i n : Execute test n times.
65*49cdfc7eSAndroid Build Coastguard Worker  *			-I x : Execute test for x seconds.
66*49cdfc7eSAndroid Build Coastguard Worker  *			-p   : Pause for SIGUSR1 before starting
67*49cdfc7eSAndroid Build Coastguard Worker  *			-P x : Pause for x seconds between iterations.
68*49cdfc7eSAndroid Build Coastguard Worker  *			-t   : Turn on syscall timing.
69*49cdfc7eSAndroid Build Coastguard Worker  *
70*49cdfc7eSAndroid Build Coastguard Worker  ****************************************************************/
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
73*49cdfc7eSAndroid Build Coastguard Worker #include <pwd.h>
74*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
75*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
76*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
77*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
78*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
79*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
80*49cdfc7eSAndroid Build Coastguard Worker 
81*49cdfc7eSAndroid Build Coastguard Worker #define EXP_RET_VAL	-1
82*49cdfc7eSAndroid Build Coastguard Worker #define SPL_FILE	"/dev/null"
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t {		/* test case structure */
85*49cdfc7eSAndroid Build Coastguard Worker 	int experrno;		/* expected errno */
86*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
87*49cdfc7eSAndroid Build Coastguard Worker 	int (*setup) (void);	/* Individual setup routine */
88*49cdfc7eSAndroid Build Coastguard Worker 	void (*cleanup) (void);	/* Individual cleanup routine */
89*49cdfc7eSAndroid Build Coastguard Worker };
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "fdatasync02";
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker static int testno;
94*49cdfc7eSAndroid Build Coastguard Worker static int fd;
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
97*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
98*49cdfc7eSAndroid Build Coastguard Worker static int setup1(void);
99*49cdfc7eSAndroid Build Coastguard Worker static int setup2(void);
100*49cdfc7eSAndroid Build Coastguard Worker static void cleanup2(void);
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker static struct test_case_t tdat[] = {
103*49cdfc7eSAndroid Build Coastguard Worker 	{EBADF, "invalid file descriptor", setup1, NULL},
104*49cdfc7eSAndroid Build Coastguard Worker 	{EINVAL, "file descriptor to a special file", setup2, cleanup2},
105*49cdfc7eSAndroid Build Coastguard Worker };
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
108*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char ** argv)109*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char **argv)
110*49cdfc7eSAndroid Build Coastguard Worker {
111*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(argc, argv, NULL, NULL);
114*49cdfc7eSAndroid Build Coastguard Worker 
115*49cdfc7eSAndroid Build Coastguard Worker 	setup();
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
118*49cdfc7eSAndroid Build Coastguard Worker 		/* reset tst_count in case we are looping */
119*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
120*49cdfc7eSAndroid Build Coastguard Worker 
121*49cdfc7eSAndroid Build Coastguard Worker 		for (testno = 0; testno < TST_TOTAL; ++testno) {
122*49cdfc7eSAndroid Build Coastguard Worker 			if ((tdat[testno].setup) && (tdat[testno].setup())) {
123*49cdfc7eSAndroid Build Coastguard Worker 				/* setup() failed, skip this test */
124*49cdfc7eSAndroid Build Coastguard Worker 				continue;
125*49cdfc7eSAndroid Build Coastguard Worker 			}
126*49cdfc7eSAndroid Build Coastguard Worker 
127*49cdfc7eSAndroid Build Coastguard Worker 			/* Test the system call */
128*49cdfc7eSAndroid Build Coastguard Worker 			TEST(fdatasync(fd));
129*49cdfc7eSAndroid Build Coastguard Worker 			if ((TEST_RETURN == EXP_RET_VAL) &&
130*49cdfc7eSAndroid Build Coastguard Worker 			    (TEST_ERRNO == tdat[testno].experrno)) {
131*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TPASS, "Expected failure for %s, "
132*49cdfc7eSAndroid Build Coastguard Worker 					 "errno: %d", tdat[testno].desc,
133*49cdfc7eSAndroid Build Coastguard Worker 					 TEST_ERRNO);
134*49cdfc7eSAndroid Build Coastguard Worker 			} else {
135*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "Unexpected results for %s ; "
136*49cdfc7eSAndroid Build Coastguard Worker 					 "returned %ld (expected %d), errno %d "
137*49cdfc7eSAndroid Build Coastguard Worker 					 "(expected %d)", tdat[testno].desc,
138*49cdfc7eSAndroid Build Coastguard Worker 					 TEST_RETURN, EXP_RET_VAL,
139*49cdfc7eSAndroid Build Coastguard Worker 					 TEST_ERRNO, tdat[testno].experrno);
140*49cdfc7eSAndroid Build Coastguard Worker 			}
141*49cdfc7eSAndroid Build Coastguard Worker 			if (tdat[testno].cleanup) {
142*49cdfc7eSAndroid Build Coastguard Worker 				tdat[testno].cleanup();
143*49cdfc7eSAndroid Build Coastguard Worker 			}
144*49cdfc7eSAndroid Build Coastguard Worker 		}
145*49cdfc7eSAndroid Build Coastguard Worker 	}
146*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
147*49cdfc7eSAndroid Build Coastguard Worker 
148*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
149*49cdfc7eSAndroid Build Coastguard Worker }
150*49cdfc7eSAndroid Build Coastguard Worker 
setup1(void)151*49cdfc7eSAndroid Build Coastguard Worker int setup1(void)
152*49cdfc7eSAndroid Build Coastguard Worker {
153*49cdfc7eSAndroid Build Coastguard Worker 	fd = -1;
154*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
155*49cdfc7eSAndroid Build Coastguard Worker }
156*49cdfc7eSAndroid Build Coastguard Worker 
setup2(void)157*49cdfc7eSAndroid Build Coastguard Worker int setup2(void)
158*49cdfc7eSAndroid Build Coastguard Worker {
159*49cdfc7eSAndroid Build Coastguard Worker 	/* Open special file */
160*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd = open(SPL_FILE, O_RDONLY)) == -1) {
161*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TBROK, "Failed to open %s", SPL_FILE);
162*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
163*49cdfc7eSAndroid Build Coastguard Worker 	}
164*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
165*49cdfc7eSAndroid Build Coastguard Worker }
166*49cdfc7eSAndroid Build Coastguard Worker 
cleanup2(void)167*49cdfc7eSAndroid Build Coastguard Worker void cleanup2(void)
168*49cdfc7eSAndroid Build Coastguard Worker {
169*49cdfc7eSAndroid Build Coastguard Worker 	/* close special file */
170*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(NULL, fd);
171*49cdfc7eSAndroid Build Coastguard Worker }
172*49cdfc7eSAndroid Build Coastguard Worker 
173*49cdfc7eSAndroid Build Coastguard Worker /*
174*49cdfc7eSAndroid Build Coastguard Worker  * setup()
175*49cdfc7eSAndroid Build Coastguard Worker  *	performs all ONE TIME setup for this test
176*49cdfc7eSAndroid Build Coastguard Worker  */
setup(void)177*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
178*49cdfc7eSAndroid Build Coastguard Worker {
179*49cdfc7eSAndroid Build Coastguard Worker 
180*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
181*49cdfc7eSAndroid Build Coastguard Worker 
182*49cdfc7eSAndroid Build Coastguard Worker 	/* Pause if that option was specified
183*49cdfc7eSAndroid Build Coastguard Worker 	 * TEST_PAUSE contains the code to fork the test with the -c option.
184*49cdfc7eSAndroid Build Coastguard Worker 	 */
185*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
186*49cdfc7eSAndroid Build Coastguard Worker 
187*49cdfc7eSAndroid Build Coastguard Worker }
188*49cdfc7eSAndroid Build Coastguard Worker 
189*49cdfc7eSAndroid Build Coastguard Worker /*
190*49cdfc7eSAndroid Build Coastguard Worker  * cleanup()
191*49cdfc7eSAndroid Build Coastguard Worker  *	performs all ONE TIME cleanup for this test at
192*49cdfc7eSAndroid Build Coastguard Worker  *	completion or premature exit
193*49cdfc7eSAndroid Build Coastguard Worker  */
cleanup(void)194*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
195*49cdfc7eSAndroid Build Coastguard Worker {
196*49cdfc7eSAndroid Build Coastguard Worker 
197*49cdfc7eSAndroid Build Coastguard Worker }
198