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#include "safe_macros.h"
80
81#define EXP_RET_VAL	-1
82#define SPL_FILE	"/dev/null"
83
84struct test_case_t {		/* test case structure */
85	int experrno;		/* expected errno */
86	char *desc;
87	int (*setup) (void);	/* Individual setup routine */
88	void (*cleanup) (void);	/* Individual cleanup routine */
89};
90
91char *TCID = "fdatasync02";
92
93static int testno;
94static int fd;
95
96static void setup(void);
97static void cleanup(void);
98static int setup1(void);
99static int setup2(void);
100static void cleanup2(void);
101
102static struct test_case_t tdat[] = {
103	{EBADF, "invalid file descriptor", setup1, NULL},
104	{EINVAL, "file descriptor to a special file", setup2, cleanup2},
105};
106
107int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
108
109int main(int argc, char **argv)
110{
111	int lc;
112
113	tst_parse_opts(argc, argv, NULL, NULL);
114
115	setup();
116
117	for (lc = 0; TEST_LOOPING(lc); lc++) {
118		/* reset tst_count in case we are looping */
119		tst_count = 0;
120
121		for (testno = 0; testno < TST_TOTAL; ++testno) {
122			if ((tdat[testno].setup) && (tdat[testno].setup())) {
123				/* setup() failed, skip this test */
124				continue;
125			}
126
127			/* Test the system call */
128			TEST(fdatasync(fd));
129			if ((TEST_RETURN == EXP_RET_VAL) &&
130			    (TEST_ERRNO == tdat[testno].experrno)) {
131				tst_resm(TPASS, "Expected failure for %s, "
132					 "errno: %d", tdat[testno].desc,
133					 TEST_ERRNO);
134			} else {
135				tst_resm(TFAIL, "Unexpected results for %s ; "
136					 "returned %ld (expected %d), errno %d "
137					 "(expected %d)", tdat[testno].desc,
138					 TEST_RETURN, EXP_RET_VAL,
139					 TEST_ERRNO, tdat[testno].experrno);
140			}
141			if (tdat[testno].cleanup) {
142				tdat[testno].cleanup();
143			}
144		}
145	}
146	cleanup();
147
148	tst_exit();
149}
150
151int setup1(void)
152{
153	fd = -1;
154	return 0;
155}
156
157int setup2(void)
158{
159	/* Open special file */
160	if ((fd = open(SPL_FILE, O_RDONLY)) == -1) {
161		tst_resm(TBROK, "Failed to open %s", SPL_FILE);
162		return 1;
163	}
164	return 0;
165}
166
167void cleanup2(void)
168{
169	/* close special file */
170	SAFE_CLOSE(NULL, fd);
171}
172
173/*
174 * setup()
175 *	performs all ONE TIME setup for this test
176 */
177void setup(void)
178{
179
180	tst_sig(NOFORK, DEF_HANDLER, cleanup);
181
182	/* Pause if that option was specified
183	 * TEST_PAUSE contains the code to fork the test with the -c option.
184	 */
185	TEST_PAUSE;
186
187}
188
189/*
190 * cleanup()
191 *	performs all ONE TIME cleanup for this test at
192 *	completion or premature exit
193 */
194void cleanup(void)
195{
196
197}
198