1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  *
4  * This program is free software;  you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12  * the GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program;  if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * DESCRIPTION
21  *	Testcase to check fstatfs() sets errno correctly.
22  */
23 
24 #include <sys/vfs.h>
25 #include <sys/types.h>
26 #include <sys/statfs.h>
27 #include <errno.h>
28 #include "test.h"
29 #include "safe_macros.h"
30 
31 static void setup(void);
32 static void cleanup(void);
33 
34 char *TCID = "fstatfs02";
35 
36 static struct statfs buf;
37 
38 static struct test_case_t {
39 	int fd;
40 	struct statfs *sbuf;
41 	int error;
42 } TC[] = {
43 	/* EBADF - fd is invalid */
44 	{
45 	-1, &buf, EBADF},
46 #ifndef UCLINUX
47 	    /* Skip since uClinux does not implement memory protection */
48 	    /* EFAULT - address for buf is invalid */
49 	{
50 	-1, (void *)-1, EFAULT}
51 #endif
52 };
53 
54 int TST_TOTAL = ARRAY_SIZE(TC);
55 
56 int main(int ac, char **av)
57 {
58 	int lc;
59 	int i;
60 
61 	tst_parse_opts(ac, av, NULL, NULL);
62 
63 	setup();
64 
65 	for (lc = 0; TEST_LOOPING(lc); lc++) {
66 
67 		tst_count = 0;
68 
69 		for (i = 0; i < TST_TOTAL; i++) {
70 
71 			TEST(fstatfs(TC[i].fd, TC[i].sbuf));
72 
73 			if (TEST_RETURN != -1) {
74 				tst_resm(TFAIL, "call succeeded unexpectedly");
75 				continue;
76 			}
77 
78 			if (TEST_ERRNO == TC[i].error) {
79 				tst_resm(TPASS, "expected failure - "
80 					 "errno = %d : %s", TEST_ERRNO,
81 					 strerror(TEST_ERRNO));
82 			} else {
83 				tst_resm(TFAIL, "unexpected error - %d : %s - "
84 					 "expected %d", TEST_ERRNO,
85 					 strerror(TEST_ERRNO), TC[i].error);
86 			}
87 		}
88 	}
89 	cleanup();
90 
91 	tst_exit();
92 }
93 
94 static void setup(void)
95 {
96 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
97 
98 	TEST_PAUSE;
99 
100 	tst_tmpdir();
101 #ifndef UCLINUX
102 	TC[1].fd = SAFE_OPEN(cleanup, "tempfile", O_RDWR | O_CREAT, 0700);
103 #endif
104 }
105 
106 static void cleanup(void)
107 {
108 #ifndef UCLINUX
109 	if (TC[1].fd > 0 && close(TC[1].fd))
110 		tst_resm(TWARN | TERRNO, "Failed to close fd");
111 #endif
112 
113 	tst_rmdir();
114 }
115