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
59char *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
69static void setup(void);
70static void cleanup(void);
71
72static 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
79int TST_TOTAL = sizeof(tc) / sizeof(tc[0]);
80
81int 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		exp_eno = ENODATA;
95
96		for (i = 0; i < TST_TOTAL; i++) {
97			TEST(getxattr(tc[0], XATTR_TEST_KEY, buf, BUFSIZ));
98
99			if (TEST_RETURN == -1 && TEST_ERRNO == exp_eno)
100				tst_resm(TPASS | TTERRNO, "expected behavior");
101			else
102				tst_resm(TFAIL | TTERRNO, "unexpected behavior"
103					 " - expected errno %d - Got", exp_eno);
104		}
105	}
106
107	cleanup();
108	tst_exit();
109}
110
111static void setup(void)
112{
113	int fd;
114	dev_t dev;
115
116	tst_require_root();
117
118	tst_tmpdir();
119
120	/* Test for xattr support */
121	fd = SAFE_CREAT(cleanup, "testfile", 0644);
122	close(fd);
123	if (setxattr("testfile", "user.test", "test", 4, XATTR_CREATE) == -1)
124		if (errno == ENOTSUP)
125			tst_brkm(TCONF, cleanup, "No xattr support in fs or "
126				 "mount without user_xattr option");
127	unlink("testfile");
128
129	/* Create test files */
130	if (mknod(FIFO, S_IFIFO | 0777, 0) == -1)
131		tst_brkm(TBROK | TERRNO, cleanup, "Create FIFO(%s) failed",
132			 FIFO);
133
134	dev = makedev(1, 3);
135	if (mknod(CHR, S_IFCHR | 0777, dev) == -1)
136		tst_brkm(TBROK | TERRNO, cleanup, "Create char special(%s)"
137			 " failed", CHR);
138
139	if (mknod(BLK, S_IFBLK | 0777, 0) == -1)
140		tst_brkm(TBROK | TERRNO, cleanup, "Create block special(%s)"
141			 " failed", BLK);
142
143	if (mknod(SOCK, S_IFSOCK | 0777, 0) == -1)
144		tst_brkm(TBROK | TERRNO, cleanup, "Create socket(%s) failed",
145			 SOCK);
146
147	TEST_PAUSE;
148}
149
150static void cleanup(void)
151{
152	tst_rmdir();
153}
154#else /* HAVE_SYS_XATTR_H */
155int main(int argc, char *argv[])
156{
157	tst_brkm(TCONF, NULL, "<sys/xattr.h> does not exist.");
158}
159#endif
160