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 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
6 .\" This is free documentation; you can redistribute it and/or
7 .\" modify it under the terms of the GNU General Public License as
8 .\" published by the Free Software Foundation; either version 2 of
9 .\" the License, or (at your option) any later version.
11 .\" The GNU General Public License's references to "object code"
12 .\" and "executables" are to be interpreted as the output of any
13 .\" document formatting or typesetting system, including
14 .\" intermediate and printed output.
16 .\" This manual is distributed in the hope that it will be useful,
17 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
18 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 .\" GNU General Public License for more details.
21 .\" You should have received a copy of the GNU General Public
22 .\" License along with this manual; if not, see
23 .\" <http://www.gnu.org/licenses/>.
26 .TH LISTXATTR 2 2021-03-22 "Linux" "Linux Programmer's Manual"
28 listxattr, llistxattr, flistxattr \- list extended attribute names
32 .B #include <sys/xattr.h>
34 .BI "ssize_t listxattr(const char *" path ", char *" list \
36 .BI "ssize_t llistxattr(const char *" path ", char *" list \
38 .BI "ssize_t flistxattr(int " fd ", char *" list ", size_t " size );
42 Extended attributes are
44 pairs associated with inodes (files, directories, symbolic links, etc.).
45 They are extensions to the normal attributes which are associated
46 with all inodes in the system (i.e., the
49 A complete overview of extended attributes concepts can be found in
54 of extended attribute names associated with the given
57 The retrieved list is placed in
59 a caller-allocated buffer whose size (in bytes) is specified in the argument
61 The list is the set of (null-terminated) names, one after the other.
62 Names of extended attributes to which the calling process does not
63 have access may be omitted from the list.
64 The length of the attribute name
71 except in the case of a symbolic link, where the list of names of
72 extended attributes associated with the link itself is retrieved,
73 not the file that it refers to.
78 only the open file referred to by
82 is interrogated in place of
85 A single extended attribute
87 is a null-terminated string.
88 The name includes a namespace prefix; there may be several, disjoint
89 namespaces associated with an individual inode.
93 is specified as zero, these calls return the current size of the
94 list of extended attribute names (and leave
97 This can be used to determine the size of the buffer that
98 should be supplied in a subsequent call.
99 (But, bear in mind that there is a possibility that the
100 set of extended attributes may change between the two calls,
101 so that it is still necessary to check the return status
102 from the second call.)
106 of names is returned as an unordered array of null-terminated character
107 strings (attribute names are separated by null bytes (\(aq\e0\(aq)), like this:
111 user.name1\e0system.name1\e0user.name2\e0
115 Filesystems that implement POSIX ACLs using
116 extended attributes might return a
122 system.posix_acl_access\e0system.posix_acl_default\e0
126 On success, a nonnegative number is returned indicating the size of the
127 extended attribute name list.
128 On failure, \-1 is returned and
130 is set to indicate the error.
134 The size of the list of extended attribute names is larger than the maximum
135 size allowed; the list cannot be retrieved.
136 This can happen on filesystems that support an unlimited number of
137 extended attributes per file such as XFS, for example.
141 Extended attributes are not supported by the filesystem, or are disabled.
148 buffer is too small to hold the result.
150 In addition, the errors documented in
154 These system calls have been available on Linux since kernel 2.4;
155 glibc support is provided since version 2.3.
157 These system calls are Linux-specific.
159 .\" Andreas Gruenbacher,
160 .\" .RI < a.gruenbacher@computer.org >
161 .\" and the SGI XFS development team,
162 .\" .RI < linux-xfs@oss.sgi.com >.
163 .\" Please send any bug reports or comments to these addresses.
165 .\" The xattr(7) page refers to this text:
168 the VFS imposes a limit of 64\ kB on the size of the extended
169 attribute name list returned by
171 If the total size of attribute names attached to a file exceeds this limit,
172 it is no longer possible to retrieve the list of attribute names.
174 The following program demonstrates the usage of
178 For the file whose pathname is provided as a command-line argument,
179 it lists all extended file attributes and their values.
181 To keep the code simple, the program assumes that attribute keys and
182 values are constant during the execution of the program.
183 A production program should expect and handle changes during
184 execution of the program.
186 the number of bytes required for attribute keys
187 might increase between the two calls to
189 An application could handle this possibility using
190 a loop that retries the call
191 (perhaps up to a predetermined maximum number of attempts)
192 with a larger buffer each time it fails with the error
196 could be handled similarly.
198 The following output was recorded by first creating a file, setting
199 some extended file attributes,
200 and then listing the attributes with the example program.
204 $ \fBtouch /tmp/foo\fP
205 $ \fBsetfattr \-n user.fred \-v chocolate /tmp/foo\fP
206 $ \fBsetfattr \-n user.frieda \-v bar /tmp/foo\fP
207 $ \fBsetfattr \-n user.empty /tmp/foo\fP
208 $ \fB./listxattr /tmp/foo\fP
211 user.empty: <no value>
214 .SS Program source (listxattr.c)
220 #include <sys/types.h>
221 #include <sys/xattr.h>
224 main(int argc, char *argv[])
226 ssize_t buflen, keylen, vallen;
227 char *buf, *key, *val;
230 fprintf(stderr, "Usage: %s path\en", argv[0]);
235 * Determine the length of the buffer needed.
237 buflen = listxattr(argv[1], NULL, 0);
243 printf("%s has no attributes.\en", argv[1]);
248 * Allocate the buffer.
250 buf = malloc(buflen);
257 * Copy the list of attribute keys to the buffer.
259 buflen = listxattr(argv[1], buf, buflen);
266 * Loop over the list of zero terminated strings with the
267 * attribute keys. Use the remaining buffer length to determine
268 * the end of the list.
274 * Output attribute key.
279 * Determine length of the value.
281 vallen = getxattr(argv[1], key, NULL, 0);
288 * Allocate value buffer.
289 * One extra byte is needed to append 0x00.
291 val = malloc(vallen + 1);
298 * Copy value to buffer.
300 vallen = getxattr(argv[1], key, val, vallen);
305 * Output attribute value.
312 } else if (vallen == 0)
313 printf("<no value>");
318 * Forward to next attribute key.
320 keylen = strlen(key) + 1;