2 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifdef HAVE_SYS_XATTR_H
33 # include <sys/xattr.h>
35 # ifndef XATTR_SIZE_MAX
36 # define XATTR_SIZE_MAX 65536
39 #define DEFAULT_STRLEN 32
44 static const char name[] = "strace.test";
45 static const char c_value[] = "foo\0bar";
46 static const char q_value[] = "foo\\0bar";
48 const char *const z_value = tail_memdup(c_value, sizeof(c_value));
49 char *const efault = tail_alloc(1) + 1;
50 const char *const value = tail_memdup(c_value, sizeof(c_value) - 1);
51 char *const big = tail_alloc(XATTR_SIZE_MAX + 1);
55 rc = fsetxattr(-1, 0, 0, 0, XATTR_CREATE);
56 printf("fsetxattr(-1, NULL, NULL, 0, XATTR_CREATE) = %s\n",
59 rc = fsetxattr(-1, 0, z_value, 0, XATTR_CREATE);
60 printf("fsetxattr(-1, NULL, \"\", 0, XATTR_CREATE) = %s\n",
63 rc = fsetxattr(-1, name, big, XATTR_SIZE_MAX + 1, XATTR_CREATE);
64 printf("fsetxattr(-1, \"%s\", %p, %u, XATTR_CREATE) = %s\n",
65 name, big, XATTR_SIZE_MAX + 1, sprintrc(rc));
67 rc = fsetxattr(-1, name, value, sizeof(c_value), XATTR_CREATE);
68 printf("fsetxattr(-1, \"%s\", %p, %u, XATTR_CREATE) = %s\n",
69 name, value, (unsigned) sizeof(c_value), sprintrc(rc));
71 rc = fsetxattr(-1, name, z_value, sizeof(c_value), XATTR_REPLACE);
72 printf("fsetxattr(-1, \"%s\", \"%s\", %u, XATTR_REPLACE) = %s\n",
73 name, q_value, (unsigned) sizeof(c_value), sprintrc(rc));
75 rc = fsetxattr(-1, name, value, sizeof(c_value) - 1, XATTR_CREATE|XATTR_REPLACE);
76 printf("fsetxattr(-1, \"%s\", \"%s\", %u, XATTR_CREATE|XATTR_REPLACE)"
78 name, q_value, (unsigned) sizeof(c_value) - 1, sprintrc(rc));
80 rc = setxattr(".", name, z_value, sizeof(c_value), XATTR_CREATE);
81 printf("setxattr(\".\", \"%s\", \"%s\", %u, XATTR_CREATE) = %s\n",
82 name, q_value, (unsigned) sizeof(c_value), sprintrc(rc));
84 rc = lsetxattr(".", name, value, sizeof(c_value) - 1, XATTR_CREATE);
85 printf("lsetxattr(\".\", \"%s\", \"%s\", %u, XATTR_CREATE) = %s\n",
86 name, q_value, (unsigned) sizeof(c_value) - 1, sprintrc(rc));
88 rc = fgetxattr(-1, name, efault, 4);
89 printf("fgetxattr(-1, \"%s\", %p, 4) = %s\n",
90 name, efault, sprintrc(rc));
92 rc = getxattr(".", name, big, XATTR_SIZE_MAX + 1);
93 printf("getxattr(\".\", \"%s\", %p, %u) = %s\n",
94 name, big, XATTR_SIZE_MAX + 1, sprintrc(rc));
96 rc = lgetxattr(".", name, big + 1, XATTR_SIZE_MAX);
97 printf("lgetxattr(\".\", \"%s\", %p, %u) = %s\n",
98 name, big + 1, XATTR_SIZE_MAX, sprintrc(rc));
100 rc = flistxattr(-1, efault, 4);
101 printf("flistxattr(-1, %p, 4) = %s\n", efault, sprintrc(rc));
103 rc = llistxattr("", efault + 1, 4);
104 printf("llistxattr(\"\", %p, 4) = %s\n", efault + 1, sprintrc(rc));
106 rc = listxattr(".", big, 0);
107 printf("listxattr(\".\", %p, 0) = %s\n", big, sprintrc(rc));
109 rc = listxattr(".", big, XATTR_SIZE_MAX + 1);
110 errstr = sprintrc(rc);
111 printf("listxattr(\".\", ");
115 const int ellipsis = rc > DEFAULT_STRLEN;
118 print_quoted_memory(big, ellipsis ? DEFAULT_STRLEN : rc);
121 fputs("...", stdout);
123 printf(", %u) = %s\n", XATTR_SIZE_MAX + 1, errstr);
125 rc = fremovexattr(-1, name);
126 printf("fremovexattr(-1, \"%s\") = %s\n", name, sprintrc(rc));
128 rc = removexattr(".", name);
129 printf("removexattr(\".\", \"%s\") = %s\n", name, sprintrc(rc));
131 rc = lremovexattr(".", name);
132 printf("lremovexattr(\".\", \"%s\") = %s\n", name, sprintrc(rc));
134 puts("+++ exited with 0 +++");
140 SKIP_MAIN_UNDEFINED("HAVE_SYS_XATTR_H")