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