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 #include "safe_macros.h"
51 
52 void setup(void);
53 void cleanup(void);
54 
55 int fd[2];			/* fd's for the pipe() call in setup()  */
56 int pfd;			/* holds the value for fd[1]            */
57 int bfd = -1;			/* an invalid fd                        */
58 
59 struct test_case_t {
60 	int *fd;
61 	int error;
62 } TC[] = {
63 	/* EBADF - fd is invalid (-1) */
64 	{
65 	&bfd, EBADF},
66 	    /* EINVAL - fsync() on pipe should not succeed. */
67 	{
68 	&pfd, EINVAL}
69 };
70 
71 char *TCID = "fsync03";
72 int TST_TOTAL = 2;
73 
main(int ac,char ** av)74 int main(int ac, char **av)
75 {
76 	int lc;
77 	int i;
78 
79 	tst_parse_opts(ac, av, NULL, NULL);
80 
81 	setup();
82 
83 	for (lc = 0; TEST_LOOPING(lc); lc++) {
84 
85 		tst_count = 0;
86 
87 		/* loop through the test cases */
88 		for (i = 0; i < TST_TOTAL; i++) {
89 
90 			TEST(fsync(*(TC[i].fd)));
91 
92 			if (TEST_RETURN != -1) {
93 				tst_resm(TFAIL, "call succeeded unexpectedly");
94 				continue;
95 			}
96 
97 			if (TEST_ERRNO == TC[i].error) {
98 				tst_resm(TPASS, "expected failure - "
99 					 "errno = %d : %s", TEST_ERRNO,
100 					 strerror(TEST_ERRNO));
101 			} else {
102 				tst_resm(TFAIL, "unexpected error - %d : %s - "
103 					 "expected %d", TEST_ERRNO,
104 					 strerror(TEST_ERRNO), TC[i].error);
105 			}
106 		}
107 	}
108 	cleanup();
109 
110 	tst_exit();
111 }
112 
113 /*
114  * setup() - performs all ONE TIME setup for this test.
115  */
setup(void)116 void setup(void)
117 {
118 
119 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
120 
121 	TEST_PAUSE;
122 
123 	/* make a temporary directory and cd to it */
124 	tst_tmpdir();
125 
126 	SAFE_PIPE(cleanup, fd);
127 
128 	pfd = fd[1];
129 }
130 
131 /*
132  * cleanup() - performs all ONE TIME cleanup for this test at
133  *	       completion or premature exit.
134  */
cleanup(void)135 void cleanup(void)
136 {
137 
138 	/* delete the test directory created in setup() */
139 	tst_rmdir();
140 
141 }
142