• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015 Fujitsu Ltd.
3  * Author: Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program.
15  */
16 
17 /*
18  * DESCRIPTION
19  *  Basic test for O_PATH flag of open(2).
20  *  "Obtain a file descriptor that can be used to perform operations
21  *   that act purely at the file descriptor level, the file itself is
22  *   not opened, the operations read(2), write(2), fchmod(2), fchown(2)
23  *   and fgetxattr(2) fail with the error EBADF."
24  *
25  *  The operations include but are not limited to the syscalls above.
26  */
27 
28 #define _GNU_SOURCE
29 
30 #include "config.h"
31 
32 #include <errno.h>
33 #ifdef HAVE_SYS_XATTR_H
34 #include <sys/types.h>
35 #include <sys/xattr.h>
36 #endif
37 
38 #include "test.h"
39 #include "safe_macros.h"
40 #include "lapi/fcntl.h"
41 
42 #define TESTFILE	"testfile"
43 #define FILE_MODE	(S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID)
44 
45 static void setup(void);
46 static void verify_read(void);
47 static void verify_write(void);
48 static void verify_fchmod(void);
49 static void verify_fchown(void);
50 #ifdef HAVE_SYS_XATTR_H
51 static void verify_fgetxattr(void);
52 #endif
53 static void check_result(const char *call_name);
54 static void cleanup(void);
55 
56 static int fd;
57 
58 static void (*test_func[])(void) = {
59 	verify_read,
60 	verify_write,
61 	verify_fchmod,
62 	verify_fchown,
63 #ifdef HAVE_SYS_XATTR_H
64 	verify_fgetxattr
65 #endif
66 };
67 
68 char *TCID = "open13";
69 int TST_TOTAL = ARRAY_SIZE(test_func);
70 
main(int ac,char ** av)71 int main(int ac, char **av)
72 {
73 	int lc;
74 	int tc;
75 
76 	tst_parse_opts(ac, av, NULL, NULL);
77 
78 	setup();
79 
80 	for (lc = 0; TEST_LOOPING(lc); lc++) {
81 		tst_count = 0;
82 
83 		fd = SAFE_OPEN(cleanup, TESTFILE, O_RDWR | O_PATH);
84 
85 		for (tc = 0; tc < TST_TOTAL; tc++)
86 			(*test_func[tc])();
87 
88 		SAFE_CLOSE(cleanup, fd);
89 		fd = 0;
90 	}
91 
92 	cleanup();
93 	tst_exit();
94 }
95 
setup(void)96 static void setup(void)
97 {
98 	if ((tst_kvercmp(2, 6, 39)) < 0) {
99 		tst_brkm(TCONF, NULL, "This test can only run on kernels "
100 			"that are 2.6.39 or higher");
101 	}
102 
103 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
104 
105 	tst_tmpdir();
106 
107 	SAFE_TOUCH(cleanup, TESTFILE, FILE_MODE, NULL);
108 
109 	TEST_PAUSE;
110 }
111 
verify_read(void)112 static void verify_read(void)
113 {
114 	char buf[255];
115 
116 	TEST(read(fd, buf, sizeof(buf)));
117 	check_result("read(2)");
118 }
119 
verify_write(void)120 static void verify_write(void)
121 {
122 	TEST(write(fd, "w", 1));
123 	check_result("write(2)");
124 }
125 
verify_fchmod(void)126 static void verify_fchmod(void)
127 {
128 	TEST(fchmod(fd, 0666));
129 	check_result("fchmod(2)");
130 }
131 
verify_fchown(void)132 static void verify_fchown(void)
133 {
134 	TEST(fchown(fd, 1000, 1000));
135 	check_result("fchown(2)");
136 }
137 
138 #ifdef HAVE_SYS_XATTR_H
verify_fgetxattr(void)139 static void verify_fgetxattr(void)
140 {
141 	TEST(fgetxattr(fd, "tkey", NULL, 1));
142 	check_result("fgetxattr(2)");
143 }
144 #endif
145 
check_result(const char * call_name)146 static void check_result(const char *call_name)
147 {
148 	if (TEST_RETURN == 0) {
149 		tst_resm(TFAIL, "%s succeeded unexpectedly", call_name);
150 		return;
151 	}
152 
153 	if (TEST_ERRNO != EBADF) {
154 		tst_resm(TFAIL | TTERRNO, "%s failed unexpectedly, "
155 			"expected EBADF", call_name);
156 		return;
157 	}
158 
159 	tst_resm(TPASS, "%s failed with EBADF", call_name);
160 }
161 
cleanup(void)162 static void cleanup(void)
163 {
164 	if (fd > 0 && close(fd))
165 		tst_resm(TWARN | TERRNO, "failed to close file");
166 
167 	tst_rmdir();
168 }
169