1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/xattr.h>
#define assert_perror(x) \
({ \
int res = (x); \
if (res < 0) { \
perror(#x); \
return 1; \
} \
res; \
})
#define soft_assert(x, m) \
({ \
if (!(x)) { \
fprintf(stderr, "cond \"%s\" failed: %s\n", #x, m); \
return 1; \
} \
})
void remove_tmp(const char (*fname)[]) {
unlink(&(*fname)[0]);
}
#define _cleanup_file_ __attribute__((cleanup(remove_tmp)))
int main(void) {
int ret;
char buf[32] = { 0 };
_cleanup_file_ char filename[] = "xattr_test.XXXXXX";
_cleanup_file_ char filename2[] = "xattr_test.XXXXXX.2";
int tmpfile = assert_perror(mkstemp(filename));
memcpy(filename2, filename, sizeof(filename) - 1);
assert_perror(symlink(filename, filename2));
assert_perror(setxattr(filename, "user.T1", "ABC", 3, XATTR_CREATE));
assert_perror(fsetxattr(tmpfile, "user.T2", "DEF", 3, XATTR_CREATE));
// for testing remove
assert_perror(fsetxattr(tmpfile, "user.T3", "DEF", 3, XATTR_CREATE));
if ((ret = getxattr(filename, "user.T1", buf, 3)) != 3) {
if (ret < 0) {
perror("getxattr");
return 1;
}
soft_assert(memcmp(buf, "ABC", 3) == 0, "xattr read wrong");
}
ret = lgetxattr(filename2, "user.T1", buf, 3);
soft_assert(ret < 0 && errno == ENODATA, "lgetxattr deref'd");
if ((ret = fgetxattr(tmpfile, "user.T3", buf, 3)) != 3) {
if (ret < 0) {
perror("fgetxattr");
return 1;
}
soft_assert(memcmp(buf, "DEF", 3) == 0, "xattr read wrong");
}
assert_perror(removexattr(filename, "user.T2"));
assert_perror(fremovexattr(tmpfile, "user.T3"));
ret = assert_perror(listxattr(filename, buf, sizeof(buf) - 1));
soft_assert(memmem(buf, ret, "user.T1", 7), "user.T1 not found");
soft_assert(!memmem(buf, ret, "user.T2", 7), "user.T2 found");
soft_assert(!memmem(buf, ret, "user.T3", 7), "user.T3 found");
ret = assert_perror(flistxattr(tmpfile, buf, sizeof(buf) - 1));
soft_assert(memmem(buf, ret, "user.T1", 7), "user.T1 not found");
soft_assert(!memmem(buf, ret, "user.T2", 7), "user.T2 found");
soft_assert(!memmem(buf, ret, "user.T3", 7), "user.T3 found");
}
|