1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  *	07/2001 Ported by Wayne Boyer
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  * DESCRIPTION
22  *	1.	Use a component of the pathname, which is not a directory
23  *		in the "path" parameter to statfs(). Expect ENOTDIR
24  *	2.	Pass a filename which doesn't exist, and expect ENOENT.
25  *	3.	Pass a pathname which is more than MAXNAMLEN, and expect
26  *		ENAMETOOLONG.
27  *	4.	Pass a pointer to the pathname outside the address space of
28  *		the process, and expect EFAULT.
29  *	5.	Pass a pointer to the buf paramter outside the address space
30  *		of the process, and expect EFAULT.
31  *	6.	Pass a filename which has too many symbolic links, and expect
32  *		ELOOP.
33  */
34 
35 #include <sys/types.h>
36 #include <sys/statfs.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
39 #include <sys/vfs.h>
40 #include <sys/mman.h>
41 #include <errno.h>
42 #include "test.h"
43 #include "safe_macros.h"
44 
45 char *TCID = "statfs02";
46 
47 static int fd;
48 
49 #define TEST_FILE		"statfs_file"
50 #define TEST_FILE1		TEST_FILE"/statfs_file1"
51 #define TEST_NOEXIST		"statfs_noexist"
52 #define TEST_SYMLINK		"statfs_symlink"
53 
54 static char test_toolong[PATH_MAX+2];
55 static struct statfs buf;
56 
57 static struct test_case_t {
58 	char *path;
59 	struct statfs *buf;
60 	int exp_error;
61 } TC[] = {
62 	{TEST_FILE1, &buf, ENOTDIR},
63 	{TEST_NOEXIST, &buf, ENOENT},
64 	{test_toolong, &buf, ENAMETOOLONG},
65 #ifndef UCLINUX
66 	{(char *)-1, &buf, EFAULT},
67 	{TEST_FILE, (struct statfs *)-1, EFAULT},
68 #endif
69 	{TEST_SYMLINK, &buf, ELOOP},
70 };
71 
72 int TST_TOTAL = ARRAY_SIZE(TC);
73 static void setup(void);
74 static void cleanup(void);
75 static void statfs_verify(const struct test_case_t *);
76 
main(int ac,char ** av)77 int main(int ac, char **av)
78 {
79 	int lc;
80 	int i;
81 
82 	tst_parse_opts(ac, av, NULL, NULL);
83 
84 	setup();
85 
86 	for (lc = 0; TEST_LOOPING(lc); lc++) {
87 		tst_count = 0;
88 		for (i = 0; i < TST_TOTAL; i++)
89 			statfs_verify(&TC[i]);
90 	}
91 
92 	cleanup();
93 	tst_exit();
94 }
95 
setup(void)96 static void setup(void)
97 {
98 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
99 
100 	TEST_PAUSE;
101 
102 	tst_tmpdir();
103 
104 	fd = SAFE_CREAT(cleanup, TEST_FILE, 0444);
105 
106 	memset(test_toolong, 'a', PATH_MAX+1);
107 
108 #if !defined(UCLINUX)
109 	TC[3].path = SAFE_MMAP(cleanup, 0, 1, PROT_NONE,
110 			       MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
111 #endif
112 
113 	SAFE_SYMLINK(cleanup, TEST_SYMLINK, "statfs_symlink_2");
114 	SAFE_SYMLINK(cleanup, "statfs_symlink_2", TEST_SYMLINK);
115 }
116 
statfs_verify(const struct test_case_t * test)117 static void statfs_verify(const struct test_case_t *test)
118 {
119 	TEST(statfs(test->path, test->buf));
120 
121 	if (TEST_RETURN != -1) {
122 		tst_resm(TFAIL, "call succeeded unexpectedly");
123 		return;
124 	}
125 
126 	if (TEST_ERRNO == test->exp_error) {
127 		tst_resm(TPASS | TTERRNO, "expected failure");
128 	} else {
129 		tst_resm(TFAIL | TTERRNO, "unexpected error, expected %d",
130 			 TEST_ERRNO);
131 	}
132 }
133 
cleanup(void)134 static void cleanup(void)
135 {
136 	if (fd > 0)
137 		close(fd);
138 
139 	tst_rmdir();
140 }
141