1 /*
2  * Copyright (C) 2012 Red Hat, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it
13  * is free of the rightful claim of any third person regarding
14  * infringement or the like.  Any license provided herein, whether
15  * implied or otherwise, applies only to this software file.  Patent
16  * licenses, if any, provided herein do not apply to combinations of
17  * this program with other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301, USA.
23  */
24 
25 /*
26  * An empty buffer of size zero can be passed into getxattr(2) to return
27  * the current size of the named extended attribute.
28  */
29 
30 #include "config.h"
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <sys/wait.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #ifdef HAVE_ATTR_XATTR_H
42 #include <attr/xattr.h>
43 #endif
44 #include "test.h"
45 
46 char *TCID = "getxattr03";
47 
48 #ifdef HAVE_ATTR_XATTR_H
49 #define XATTR_TEST_KEY "user.testkey"
50 #define XATTR_TEST_VALUE "test value"
51 #define XATTR_TEST_VALUE_SIZE (sizeof(XATTR_TEST_VALUE) - 1)
52 #define TESTFILE "getxattr03testfile"
53 
54 static void setup(void);
55 static void cleanup(void);
56 
57 int TST_TOTAL = 1;
58 
main(int argc,char * argv[])59 int main(int argc, char *argv[])
60 {
61 	int lc;
62 
63 	tst_parse_opts(argc, argv, NULL, NULL);
64 
65 	setup();
66 
67 	for (lc = 0; TEST_LOOPING(lc); lc++) {
68 		tst_count = 0;
69 
70 		TEST(getxattr(TESTFILE, XATTR_TEST_KEY, NULL, 0));
71 
72 		if (TEST_RETURN == XATTR_TEST_VALUE_SIZE)
73 			tst_resm(TPASS, "getxattr(2) returned correct value");
74 		else
75 			tst_resm(TFAIL | TTERRNO, "getxattr(2) failed");
76 	}
77 
78 	cleanup();
79 	tst_exit();
80 }
81 
setup(void)82 static void setup(void)
83 {
84 	int fd;
85 
86 	tst_require_root();
87 
88 	tst_tmpdir();
89 
90 	/* Test for xattr support and set attr value */
91 	fd = creat(TESTFILE, 0644);
92 	if (fd == -1)
93 		tst_brkm(TBROK | TERRNO, cleanup, "Create %s failed", TESTFILE);
94 	close(fd);
95 
96 	if (setxattr(TESTFILE, XATTR_TEST_KEY, XATTR_TEST_VALUE,
97 		     XATTR_TEST_VALUE_SIZE, XATTR_CREATE) == -1) {
98 		if (errno == ENOTSUP)
99 			tst_brkm(TCONF, cleanup, "No xattr support in fs or "
100 				 "fs mounted without user_xattr option");
101 		else
102 			tst_brkm(TBROK | TERRNO, cleanup, "setxattr %s failed",
103 				 TESTFILE);
104 	}
105 
106 	TEST_PAUSE;
107 }
108 
cleanup(void)109 static void cleanup(void)
110 {
111 	tst_rmdir();
112 }
113 #else /* HAVE_ATTR_XATTR_H */
main(int argc,char * argv[])114 int main(int argc, char *argv[])
115 {
116 	tst_brkm(TCONF, NULL, "<attr/xattr.h> does not exist.");
117 }
118 #endif
119