blob: 65430ab20f12df57d083e0eedbd206b0f193c7a9 [file] [log] [blame]
Eryu Guanfb19ffd2012-01-06 20:46:12 +08001/*
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
Caspar Zhanga97441d2012-01-10 11:01:10 +080041#include "config.h"
Eryu Guanfb19ffd2012-01-06 20:46:12 +080042#include <sys/types.h>
43#include <sys/stat.h>
44#include <sys/wait.h>
Eryu Guanfb19ffd2012-01-06 20:46:12 +080045#include <errno.h>
46#include <fcntl.h>
47#include <unistd.h>
48#include <signal.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
Caspar Zhanga97441d2012-01-10 11:01:10 +080052#ifdef HAVE_ATTR_XATTR_H
53#include <attr/xattr.h>
54#endif
Eryu Guanfb19ffd2012-01-06 20:46:12 +080055#include "test.h"
Eryu Guanfb19ffd2012-01-06 20:46:12 +080056
Caspar Zhanga97441d2012-01-10 11:01:10 +080057char *TCID = "getxattr02";
58
59#ifdef HAVE_ATTR_XATTR_H
Eryu Guanfb19ffd2012-01-06 20:46:12 +080060#define XATTR_TEST_KEY "user.testkey"
61
62#define FIFO "getxattr02fifo"
63#define CHR "getxattr02chr"
64#define BLK "getxattr02blk"
65#define SOCK "getxattr02sock"
66
67static void setup(void);
68static void cleanup(void);
69
Eryu Guanfb19ffd2012-01-06 20:46:12 +080070static char *tc[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +080071 FIFO, /* case 00, get attr from fifo */
72 CHR, /* case 01, get attr from char special */
73 BLK, /* case 02, get attr from block special */
74 SOCK, /* case 03, get attr from UNIX domain socket */
Eryu Guanfb19ffd2012-01-06 20:46:12 +080075};
76
77int TST_TOTAL = sizeof(tc) / sizeof(tc[0]);
78
79int main(int argc, char *argv[])
80{
81 int lc;
82 int i;
Peng Haitao3d2ae5e2012-01-10 11:34:14 +080083 int exp_eno;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020084 const char *msg;
Eryu Guanfb19ffd2012-01-06 20:46:12 +080085 char buf[BUFSIZ];
86
87 msg = parse_opts(argc, argv, NULL, NULL);
88 if (msg != NULL)
89 tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg);
90
91 setup();
92
93 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080094 tst_count = 0;
Eryu Guanfb19ffd2012-01-06 20:46:12 +080095
Peng Haitao3d2ae5e2012-01-10 11:34:14 +080096 /*
97 * Before kernel 3.0.0, getxattr(2) will set errno with 'EPERM'
98 * when the file is not a regular file and directory, refer to
99 * commitid 55b23bd
100 */
101 if (tst_kvercmp(3, 0, 0) >= 0)
102 exp_eno = ENODATA;
103 else
104 exp_eno = EPERM;
105
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 for (i = 0; i < TST_TOTAL; i++) {
Eryu Guanfb19ffd2012-01-06 20:46:12 +0800107 TEST(getxattr(tc[0], XATTR_TEST_KEY, buf, BUFSIZ));
108
Peng Haitao3d2ae5e2012-01-10 11:34:14 +0800109 if (TEST_RETURN == -1 && TEST_ERRNO == exp_eno)
Eryu Guanfb19ffd2012-01-06 20:46:12 +0800110 tst_resm(TPASS | TTERRNO, "expected behavior");
Peng Haitao3d2ae5e2012-01-10 11:34:14 +0800111 else
Eryu Guanfb19ffd2012-01-06 20:46:12 +0800112 tst_resm(TFAIL | TTERRNO, "unexpected behavior"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113 " - expected errno %d - Got", exp_eno);
Eryu Guanfb19ffd2012-01-06 20:46:12 +0800114 }
115 }
116
117 cleanup();
118 tst_exit();
119}
120
121static void setup(void)
122{
123 int fd;
124
125 tst_require_root(NULL);
126
127 tst_tmpdir();
128
129 /* Test for xattr support */
130 fd = creat("testfile", 0644);
131 if (fd == -1)
132 tst_brkm(TBROK | TERRNO, cleanup, "Create testfile failed");
133 close(fd);
134 if (setxattr("testfile", "user.test", "test", 4, XATTR_CREATE) == -1)
135 if (errno == ENOTSUP)
136 tst_brkm(TCONF, cleanup, "No xattr support in fs or "
Wanlong Gao354ebb42012-12-07 10:10:04 +0800137 "mount without user_xattr option");
Eryu Guanfb19ffd2012-01-06 20:46:12 +0800138 unlink("testfile");
139
140 /* Create test files */
141 if (mknod(FIFO, S_IFIFO | 0777, 0) == -1)
142 tst_brkm(TBROK | TERRNO, cleanup, "Create FIFO(%s) failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800143 FIFO);
Eryu Guanfb19ffd2012-01-06 20:46:12 +0800144
145 if (mknod(CHR, S_IFCHR | 0777, 0) == -1)
146 tst_brkm(TBROK | TERRNO, cleanup, "Create char special(%s)"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800147 " failed", CHR);
Eryu Guanfb19ffd2012-01-06 20:46:12 +0800148
149 if (mknod(BLK, S_IFBLK | 0777, 0) == -1)
150 tst_brkm(TBROK | TERRNO, cleanup, "Create block special(%s)"
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 " failed", BLK);
Eryu Guanfb19ffd2012-01-06 20:46:12 +0800152
153 if (mknod(SOCK, S_IFSOCK | 0777, 0) == -1)
154 tst_brkm(TBROK | TERRNO, cleanup, "Create socket(%s) failed",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800155 SOCK);
Eryu Guanfb19ffd2012-01-06 20:46:12 +0800156
157 TEST_PAUSE;
158}
159
160static void cleanup(void)
161{
Eryu Guanfb19ffd2012-01-06 20:46:12 +0800162 tst_rmdir();
163}
Caspar Zhanga97441d2012-01-10 11:01:10 +0800164#else /* HAVE_ATTR_XATTR_H */
165int main(int argc, char *argv[])
166{
167 tst_brkm(TCONF, NULL, "<attr/xattr.h> does not exist.");
168}
169#endif