1 .\" Copyright (C) Andreas Gruenbacher, February 2001
2 .\" Copyright (C) Silicon Graphics Inc, September 2001
3 .\" Copyright (C) 2015 Heinrich Schuchardt <xypron.glpk@gmx.de>
5 .\" SPDX-License-Identifier: GPL-2.0-or-later
7 .TH listxattr 2 (date) "Linux man-pages (unreleased)"
9 listxattr, llistxattr, flistxattr \- list extended attribute names
12 .RI ( libc ", " \-lc )
15 .B #include <sys/xattr.h>
17 .BI "ssize_t listxattr(const char *" path ", char *_Nullable " list \
19 .BI "ssize_t llistxattr(const char *" path ", char *_Nullable " list \
21 .BI "ssize_t flistxattr(int " fd ", char *_Nullable " list ", size_t " size );
24 Extended attributes are
26 pairs associated with inodes (files, directories, symbolic links, etc.).
27 They are extensions to the normal attributes which are associated
28 with all inodes in the system (i.e., the
31 A complete overview of extended attributes concepts can be found in
36 of extended attribute names associated with the given
39 The retrieved list is placed in
41 a caller-allocated buffer whose size (in bytes) is specified in the argument
43 The list is the set of (null-terminated) names, one after the other.
44 Names of extended attributes to which the calling process does not
45 have access may be omitted from the list.
46 The length of the attribute name
53 except in the case of a symbolic link, where the list of names of
54 extended attributes associated with the link itself is retrieved,
55 not the file that it refers to.
60 only the open file referred to by
64 is interrogated in place of
67 A single extended attribute
69 is a null-terminated string.
70 The name includes a namespace prefix; there may be several, disjoint
71 namespaces associated with an individual inode.
75 is specified as zero, these calls return the current size of the
76 list of extended attribute names (and leave
79 This can be used to determine the size of the buffer that
80 should be supplied in a subsequent call.
81 (But, bear in mind that there is a possibility that the
82 set of extended attributes may change between the two calls,
83 so that it is still necessary to check the return status
84 from the second call.)
88 of names is returned as an unordered array of null-terminated character
89 strings (attribute names are separated by null bytes (\[aq]\e0\[aq])), like this:
93 user.name1\e0system.name1\e0user.name2\e0
97 Filesystems that implement POSIX ACLs using
98 extended attributes might return a
104 system.posix_acl_access\e0system.posix_acl_default\e0
108 On success, a nonnegative number is returned indicating the size of the
109 extended attribute name list.
110 On failure, \-1 is returned and
112 is set to indicate the error.
116 The size of the list of extended attribute names is larger than the maximum
117 size allowed; the list cannot be retrieved.
118 This can happen on filesystems that support an unlimited number of
119 extended attributes per file such as XFS, for example.
123 Extended attributes are not supported by the filesystem, or are disabled.
130 buffer is too small to hold the result.
132 In addition, the errors documented in
136 These system calls have been available since Linux 2.4;
137 glibc support is provided since glibc 2.3.
139 These system calls are Linux-specific.
141 .\" Andreas Gruenbacher,
142 .\" .RI < a.gruenbacher@computer.org >
143 .\" and the SGI XFS development team,
144 .\" .RI < linux-xfs@oss.sgi.com >.
145 .\" Please send any bug reports or comments to these addresses.
147 .\" The xattr(7) page refers to this text:
150 the VFS imposes a limit of 64\ kB on the size of the extended
151 attribute name list returned by
153 If the total size of attribute names attached to a file exceeds this limit,
154 it is no longer possible to retrieve the list of attribute names.
156 The following program demonstrates the usage of
160 For the file whose pathname is provided as a command-line argument,
161 it lists all extended file attributes and their values.
163 To keep the code simple, the program assumes that attribute keys and
164 values are constant during the execution of the program.
165 A production program should expect and handle changes during
166 execution of the program.
168 the number of bytes required for attribute keys
169 might increase between the two calls to
171 An application could handle this possibility using
172 a loop that retries the call
173 (perhaps up to a predetermined maximum number of attempts)
174 with a larger buffer each time it fails with the error
178 could be handled similarly.
180 The following output was recorded by first creating a file, setting
181 some extended file attributes,
182 and then listing the attributes with the example program.
186 $ \fBtouch /tmp/foo\fP
187 $ \fBsetfattr \-n user.fred \-v chocolate /tmp/foo\fP
188 $ \fBsetfattr \-n user.frieda \-v bar /tmp/foo\fP
189 $ \fBsetfattr \-n user.empty /tmp/foo\fP
190 $ \fB./listxattr /tmp/foo\fP
193 user.empty: <no value>
196 .SS Program source (listxattr.c)
197 .\" SRC BEGIN (listxattr.c)
202 #include <sys/xattr.h>
205 main(int argc, char *argv[])
207 char *buf, *key, *val;
208 ssize_t buflen, keylen, vallen;
211 fprintf(stderr, "Usage: %s path\en", argv[0]);
216 * Determine the length of the buffer needed.
218 buflen = listxattr(argv[1], NULL, 0);
224 printf("%s has no attributes.\en", argv[1]);
229 * Allocate the buffer.
231 buf = malloc(buflen);
238 * Copy the list of attribute keys to the buffer.
240 buflen = listxattr(argv[1], buf, buflen);
247 * Loop over the list of zero terminated strings with the
248 * attribute keys. Use the remaining buffer length to determine
249 * the end of the list.
255 * Output attribute key.
260 * Determine length of the value.
262 vallen = getxattr(argv[1], key, NULL, 0);
269 * Allocate value buffer.
270 * One extra byte is needed to append 0x00.
272 val = malloc(vallen + 1);
279 * Copy value to buffer.
281 vallen = getxattr(argv[1], key, val, vallen);
286 * Output attribute value.
293 } else if (vallen == 0) {
294 printf("<no value>");
300 * Forward to next attribute key.
302 keylen = strlen(key) + 1;