1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-only
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (C) 2011 Red Hat, Inc.
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Linux Test Project, 2012-2022
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2023 Marius Kittler <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * In the user.* namespace, only regular files and directories can
12*49cdfc7eSAndroid Build Coastguard Worker * have extended attributes. Otherwise getxattr(2) will return -1
13*49cdfc7eSAndroid Build Coastguard Worker * and set errno to ENODATA.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * There are 4 test cases:
16*49cdfc7eSAndroid Build Coastguard Worker *
17*49cdfc7eSAndroid Build Coastguard Worker * - Get attribute from a FIFO, setxattr(2) should return -1 and
18*49cdfc7eSAndroid Build Coastguard Worker * set errno to ENODATA
19*49cdfc7eSAndroid Build Coastguard Worker * - Get attribute from a char special file, setxattr(2) should
20*49cdfc7eSAndroid Build Coastguard Worker * return -1 and set errno to ENODATA
21*49cdfc7eSAndroid Build Coastguard Worker * - Get attribute from a block special file, setxattr(2) should
22*49cdfc7eSAndroid Build Coastguard Worker * return -1 and set errno to ENODATA
23*49cdfc7eSAndroid Build Coastguard Worker * - Get attribute from a UNIX domain socket, setxattr(2) should
24*49cdfc7eSAndroid Build Coastguard Worker * return -1 and set errno to ENODATA
25*49cdfc7eSAndroid Build Coastguard Worker */
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <sys/sysmacros.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <sys/xattr.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
32*49cdfc7eSAndroid Build Coastguard Worker
33*49cdfc7eSAndroid Build Coastguard Worker #include "tst_res_flags.h"
34*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
35*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test_macros.h"
36*49cdfc7eSAndroid Build Coastguard Worker
37*49cdfc7eSAndroid Build Coastguard Worker #define MNTPOINT "mntpoint"
38*49cdfc7eSAndroid Build Coastguard Worker #define FNAME MNTPOINT"/getxattr02"
39*49cdfc7eSAndroid Build Coastguard Worker #define XATTR_TEST_KEY "user.testkey"
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker #define FIFO "getxattr02fifo"
42*49cdfc7eSAndroid Build Coastguard Worker #define CHR "getxattr02chr"
43*49cdfc7eSAndroid Build Coastguard Worker #define BLK "getxattr02blk"
44*49cdfc7eSAndroid Build Coastguard Worker #define SOCK "getxattr02sock"
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker static struct test_case {
47*49cdfc7eSAndroid Build Coastguard Worker const char *desc;
48*49cdfc7eSAndroid Build Coastguard Worker char *fname;
49*49cdfc7eSAndroid Build Coastguard Worker int mode;
50*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
51*49cdfc7eSAndroid Build Coastguard Worker {
52*49cdfc7eSAndroid Build Coastguard Worker .desc = "get attr from fifo",
53*49cdfc7eSAndroid Build Coastguard Worker .fname = FNAME FIFO,
54*49cdfc7eSAndroid Build Coastguard Worker .mode = S_IFIFO,
55*49cdfc7eSAndroid Build Coastguard Worker },
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker .desc = "get attr from char special",
58*49cdfc7eSAndroid Build Coastguard Worker .fname = FNAME CHR,
59*49cdfc7eSAndroid Build Coastguard Worker .mode = S_IFCHR,
60*49cdfc7eSAndroid Build Coastguard Worker },
61*49cdfc7eSAndroid Build Coastguard Worker {
62*49cdfc7eSAndroid Build Coastguard Worker .desc = "get attr from block special",
63*49cdfc7eSAndroid Build Coastguard Worker .fname = FNAME BLK,
64*49cdfc7eSAndroid Build Coastguard Worker .mode = S_IFBLK,
65*49cdfc7eSAndroid Build Coastguard Worker },
66*49cdfc7eSAndroid Build Coastguard Worker {
67*49cdfc7eSAndroid Build Coastguard Worker .desc = "get attr from UNIX domain socket",
68*49cdfc7eSAndroid Build Coastguard Worker .fname = FNAME SOCK,
69*49cdfc7eSAndroid Build Coastguard Worker .mode = S_IFSOCK,
70*49cdfc7eSAndroid Build Coastguard Worker },
71*49cdfc7eSAndroid Build Coastguard Worker };
72*49cdfc7eSAndroid Build Coastguard Worker
run(unsigned int i)73*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int i)
74*49cdfc7eSAndroid Build Coastguard Worker {
75*49cdfc7eSAndroid Build Coastguard Worker char buf[BUFSIZ];
76*49cdfc7eSAndroid Build Coastguard Worker struct test_case *tc = &tcases[i];
77*49cdfc7eSAndroid Build Coastguard Worker dev_t dev = tc->mode == S_IFCHR ? makedev(1, 3) : 0u;
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker if (mknod(tc->fname, tc->mode | 0777, dev) < 0)
80*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TERRNO, "create %s (mode %i) failed (%s)",
81*49cdfc7eSAndroid Build Coastguard Worker tc->fname, tc->mode, tc->desc);
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker TEST(getxattr(tc->fname, XATTR_TEST_KEY, buf, BUFSIZ));
84*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1 && TST_ERR == ENODATA)
85*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "%s: expected return value",
86*49cdfc7eSAndroid Build Coastguard Worker tc->desc);
87*49cdfc7eSAndroid Build Coastguard Worker else
88*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO,
89*49cdfc7eSAndroid Build Coastguard Worker "%s: unexpected return value - expected errno %d - got",
90*49cdfc7eSAndroid Build Coastguard Worker tc->desc, ENODATA);
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker unlink(tc->fname);
93*49cdfc7eSAndroid Build Coastguard Worker }
94*49cdfc7eSAndroid Build Coastguard Worker
setup(void)95*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
96*49cdfc7eSAndroid Build Coastguard Worker {
97*49cdfc7eSAndroid Build Coastguard Worker /* assert xattr support in the current filesystem */
98*49cdfc7eSAndroid Build Coastguard Worker SAFE_TOUCH(FNAME, 0644, NULL);
99*49cdfc7eSAndroid Build Coastguard Worker TEST(setxattr(FNAME, "user.test", "test", 4, XATTR_CREATE));
100*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == ENOTSUP)
101*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF,
102*49cdfc7eSAndroid Build Coastguard Worker "No xattr support in fs or mount without user_xattr option");
103*49cdfc7eSAndroid Build Coastguard Worker else if (TST_RET != 0)
104*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TTERRNO, "setxattr failed");
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
108*49cdfc7eSAndroid Build Coastguard Worker .all_filesystems = 1,
109*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
110*49cdfc7eSAndroid Build Coastguard Worker .mntpoint = MNTPOINT,
111*49cdfc7eSAndroid Build Coastguard Worker .mount_device = 1,
112*49cdfc7eSAndroid Build Coastguard Worker .skip_filesystems = (const char *const []) {
113*49cdfc7eSAndroid Build Coastguard Worker "ramfs",
114*49cdfc7eSAndroid Build Coastguard Worker "nfs",
115*49cdfc7eSAndroid Build Coastguard Worker NULL
116*49cdfc7eSAndroid Build Coastguard Worker },
117*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
118*49cdfc7eSAndroid Build Coastguard Worker .test = run,
119*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases)
120*49cdfc7eSAndroid Build Coastguard Worker };
121