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_SYS_XATTR_H
42# include <sys/xattr.h>
43#endif
44#include "test.h"
45#include "safe_macros.h"
46
47char *TCID = "getxattr03";
48
49#ifdef HAVE_SYS_XATTR_H
50#define XATTR_TEST_KEY "user.testkey"
51#define XATTR_TEST_VALUE "test value"
52#define XATTR_TEST_VALUE_SIZE (sizeof(XATTR_TEST_VALUE) - 1)
53#define TESTFILE "getxattr03testfile"
54
55static void setup(void);
56static void cleanup(void);
57
58int TST_TOTAL = 1;
59
60int main(int argc, char *argv[])
61{
62	int lc;
63
64	tst_parse_opts(argc, argv, NULL, NULL);
65
66	setup();
67
68	for (lc = 0; TEST_LOOPING(lc); lc++) {
69		tst_count = 0;
70
71		TEST(getxattr(TESTFILE, XATTR_TEST_KEY, NULL, 0));
72
73		if (TEST_RETURN == XATTR_TEST_VALUE_SIZE)
74			tst_resm(TPASS, "getxattr(2) returned correct value");
75		else
76			tst_resm(TFAIL | TTERRNO, "getxattr(2) failed");
77	}
78
79	cleanup();
80	tst_exit();
81}
82
83static void setup(void)
84{
85	int fd;
86
87	tst_require_root();
88
89	tst_tmpdir();
90
91	/* Test for xattr support and set attr value */
92	fd = SAFE_CREAT(cleanup, TESTFILE, 0644);
93	close(fd);
94
95	if (setxattr(TESTFILE, XATTR_TEST_KEY, XATTR_TEST_VALUE,
96		     XATTR_TEST_VALUE_SIZE, XATTR_CREATE) == -1) {
97		if (errno == ENOTSUP)
98			tst_brkm(TCONF, cleanup, "No xattr support in fs or "
99				 "fs mounted without user_xattr option");
100		else
101			tst_brkm(TBROK | TERRNO, cleanup, "setxattr %s failed",
102				 TESTFILE);
103	}
104
105	TEST_PAUSE;
106}
107
108static void cleanup(void)
109{
110	tst_rmdir();
111}
112#else /* HAVE_SYS_XATTR_H */
113int main(int argc, char *argv[])
114{
115	tst_brkm(TCONF, NULL, "<sys/xattr.h> does not exist.");
116}
117#endif
118