1 /*
2 * Copyright (C) 2011 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 * In the user.* namespace, only regular files and directories can
27 * have extended attributes. Otherwise getxattr(2) will return -1
28 * and set errno to ENODATA.
29 *
30 * There are 4 test cases:
31 * 1. Get attribute from a FIFO, setxattr(2) should return -1 and
32 * set errno to ENODATA
33 * 2. Get attribute from a char special file, setxattr(2) should
34 * return -1 and set errno to ENODATA
35 * 3. Get attribute from a block special file, setxattr(2) should
36 * return -1 and set errno to ENODATA
37 * 4. Get attribute from a UNIX domain socket, setxattr(2) should
38 * return -1 and set errno to ENODATA
39 */
40
41 #include "config.h"
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/sysmacros.h>
45 #include <sys/wait.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <unistd.h>
49 #include <signal.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #ifdef HAVE_SYS_XATTR_H
54 # include <sys/xattr.h>
55 #endif
56 #include "test.h"
57 #include "safe_macros.h"
58
59 char *TCID = "getxattr02";
60
61 #ifdef HAVE_SYS_XATTR_H
62 #define XATTR_TEST_KEY "user.testkey"
63
64 #define FIFO "getxattr02fifo"
65 #define CHR "getxattr02chr"
66 #define BLK "getxattr02blk"
67 #define SOCK "getxattr02sock"
68
69 static void setup(void);
70 static void cleanup(void);
71
72 static char *tc[] = {
73 FIFO, /* case 00, get attr from fifo */
74 CHR, /* case 01, get attr from char special */
75 BLK, /* case 02, get attr from block special */
76 SOCK, /* case 03, get attr from UNIX domain socket */
77 };
78
79 int TST_TOTAL = sizeof(tc) / sizeof(tc[0]);
80
main(int argc,char * argv[])81 int main(int argc, char *argv[])
82 {
83 int lc;
84 int i;
85 int exp_eno;
86 char buf[BUFSIZ];
87
88 tst_parse_opts(argc, argv, NULL, NULL);
89
90 setup();
91
92 for (lc = 0; TEST_LOOPING(lc); lc++) {
93 tst_count = 0;
94
95 /*
96 * Before kernel 3.0.0, getxattr(2) will set errno with 'EPERM'
97 * when the file is not a regular file and directory, refer to
98 * commitid 55b23bd
99 */
100 if (tst_kvercmp(3, 0, 0) >= 0)
101 exp_eno = ENODATA;
102 else
103 exp_eno = EPERM;
104
105 for (i = 0; i < TST_TOTAL; i++) {
106 TEST(getxattr(tc[0], XATTR_TEST_KEY, buf, BUFSIZ));
107
108 if (TEST_RETURN == -1 && TEST_ERRNO == exp_eno)
109 tst_resm(TPASS | TTERRNO, "expected behavior");
110 else
111 tst_resm(TFAIL | TTERRNO, "unexpected behavior"
112 " - expected errno %d - Got", exp_eno);
113 }
114 }
115
116 cleanup();
117 tst_exit();
118 }
119
setup(void)120 static void setup(void)
121 {
122 int fd;
123 dev_t dev;
124
125 tst_require_root();
126
127 tst_tmpdir();
128
129 /* Test for xattr support */
130 fd = SAFE_CREAT(cleanup, "testfile", 0644);
131 close(fd);
132 if (setxattr("testfile", "user.test", "test", 4, XATTR_CREATE) == -1)
133 if (errno == ENOTSUP)
134 tst_brkm(TCONF, cleanup, "No xattr support in fs or "
135 "mount without user_xattr option");
136 unlink("testfile");
137
138 /* Create test files */
139 if (mknod(FIFO, S_IFIFO | 0777, 0) == -1)
140 tst_brkm(TBROK | TERRNO, cleanup, "Create FIFO(%s) failed",
141 FIFO);
142
143 dev = makedev(1, 3);
144 if (mknod(CHR, S_IFCHR | 0777, dev) == -1)
145 tst_brkm(TBROK | TERRNO, cleanup, "Create char special(%s)"
146 " failed", CHR);
147
148 if (mknod(BLK, S_IFBLK | 0777, 0) == -1)
149 tst_brkm(TBROK | TERRNO, cleanup, "Create block special(%s)"
150 " failed", BLK);
151
152 if (mknod(SOCK, S_IFSOCK | 0777, 0) == -1)
153 tst_brkm(TBROK | TERRNO, cleanup, "Create socket(%s) failed",
154 SOCK);
155
156 TEST_PAUSE;
157 }
158
cleanup(void)159 static void cleanup(void)
160 {
161 tst_rmdir();
162 }
163 #else /* HAVE_SYS_XATTR_H */
main(int argc,char * argv[])164 int main(int argc, char *argv[])
165 {
166 tst_brkm(TCONF, NULL, "<sys/xattr.h> does not exist.");
167 }
168 #endif
169