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