1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * NAME
22  *	fsync03.c
23  *
24  * DESCRIPTION
25  *	Testcase to check that fsync(2) sets errno correctly.
26  *
27  * ALGORITHM
28  *	1. Call fsync() with an invalid fd, and test for EBADF.
29  *	2. Call fsync() on a pipe(fd), and expect EINVAL.
30  *
31  * USAGE:  <for command-line>
32  *  fsync03 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
33  *     where,  -c n : Run n copies concurrently.
34  *             -e   : Turn on errno logging.
35  *             -i n : Execute test n times.
36  *             -I x : Execute test for x seconds.
37  *             -P x : Pause for x seconds between iterations.
38  *             -t   : Turn on syscall timing.
39  *
40  * HISTORY
41  *	07/2001 Ported by Wayne Boyer
42  *
43  * RESTRICTIONS
44  *	NONE
45  */
46 
47 #include <unistd.h>
48 #include <errno.h>
49 #include "test.h"
50 
51 void setup(void);
52 void cleanup(void);
53 
54 int fd[2];			/* fd's for the pipe() call in setup()  */
55 int pfd;			/* holds the value for fd[1]            */
56 int bfd = -1;			/* an invalid fd                        */
57 
58 struct test_case_t {
59 	int *fd;
60 	int error;
61 } TC[] = {
62 	/* EBADF - fd is invalid (-1) */
63 	{
64 	&bfd, EBADF},
65 	    /* EINVAL - fsync() on pipe should not succeed. */
66 	{
67 	&pfd, EINVAL}
68 };
69 
70 char *TCID = "fsync03";
71 int TST_TOTAL = 2;
72 
main(int ac,char ** av)73 int main(int ac, char **av)
74 {
75 	int lc;
76 	int i;
77 
78 	tst_parse_opts(ac, av, NULL, NULL);
79 
80 	setup();
81 
82 	for (lc = 0; TEST_LOOPING(lc); lc++) {
83 
84 		tst_count = 0;
85 
86 		/* loop through the test cases */
87 		for (i = 0; i < TST_TOTAL; i++) {
88 
89 			TEST(fsync(*(TC[i].fd)));
90 
91 			if (TEST_RETURN != -1) {
92 				tst_resm(TFAIL, "call succeeded unexpectedly");
93 				continue;
94 			}
95 
96 			if (TEST_ERRNO == TC[i].error) {
97 				tst_resm(TPASS, "expected failure - "
98 					 "errno = %d : %s", TEST_ERRNO,
99 					 strerror(TEST_ERRNO));
100 			} else {
101 				tst_resm(TFAIL, "unexpected error - %d : %s - "
102 					 "expected %d", TEST_ERRNO,
103 					 strerror(TEST_ERRNO), TC[i].error);
104 			}
105 		}
106 	}
107 	cleanup();
108 
109 	tst_exit();
110 }
111 
112 /*
113  * setup() - performs all ONE TIME setup for this test.
114  */
setup(void)115 void setup(void)
116 {
117 
118 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
119 
120 	TEST_PAUSE;
121 
122 	/* make a temporary directory and cd to it */
123 	tst_tmpdir();
124 
125 	if (pipe(fd) < 0) {
126 		tst_brkm(TBROK, cleanup, "pipe call failed");
127 	}
128 
129 	pfd = fd[1];
130 }
131 
132 /*
133  * cleanup() - performs all ONE TIME cleanup for this test at
134  *	       completion or premature exit.
135  */
cleanup(void)136 void cleanup(void)
137 {
138 
139 	/* delete the test directory created in setup() */
140 	tst_rmdir();
141 
142 }
143