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 2017-03-13 "Linux" "Linux Programmer's Manual"
28 listxattr, llistxattr, flistxattr \- list extended attribute names
32 .B #include <sys/types.h>
33 .B #include <sys/xattr.h>
35 .BI "ssize_t listxattr(const char\ *" path ", char\ *" list \
37 .BI "ssize_t llistxattr(const char\ *" path ", char\ *" list \
39 .BI "ssize_t flistxattr(int " fd ", char\ *" list ", size_t " size );
43 Extended attributes are
45 pairs associated with inodes (files, directories, symbolic links, etc.).
46 They are extensions to the normal attributes which are associated
47 with all inodes in the system (i.e., the
50 A complete overview of extended attributes concepts can be found in
55 of extended attribute names associated with the given
58 The retrieved list is placed in
60 a caller-allocated buffer whose size (in bytes) is specified in the argument
62 The list is the set of (null-terminated) names, one after the other.
63 Names of extended attributes to which the calling process does not
64 have access may be omitted from the list.
65 The length of the attribute name
72 except in the case of a symbolic link, where the list of names of
73 extended attributes associated with the link itself is retrieved,
74 not the file that it refers to.
79 only the open file referred to by
83 is interrogated in place of
86 A single extended attribute
88 is a null-terminated string.
89 The name includes a namespace prefix; there may be several, disjoint
90 namespaces associated with an individual inode.
94 is specified as zero, these calls return the current size of the
95 list of extended attribute names (and leave
98 This can be used to determine the size of the buffer that
99 should be supplied in a subsequent call.
100 (But, bear in mind that there is a possibility that the
101 set of extended attributes may change between the two calls,
102 so that it is still necessary to check the return status
103 from the second call.)
107 of names is returned as an unordered array of null-terminated character
108 strings (attribute names are separated by null bytes (\(aq\\0\(aq)), like this:
113 user.name1\\0system.name1\\0user.name2\\0
118 Filesystems that implement POSIX ACLs using
119 extended attributes might return a
126 system.posix_acl_access\\0system.posix_acl_default\\0
131 On success, a nonnegative number is returned indicating the size of the
132 extended attribute name list.
133 On failure, \-1 is returned and
135 is set appropriately.
139 The size of the list of extended attribute names is larger than the maximum
140 size allowed; the list cannot be retrieved.
141 This can happen on filesystems that support an unlimited number of
142 extended attributes per file such as XFS, for example.
146 Extended attributes are not supported by the filesystem, or are disabled.
153 buffer is too small to hold the result.
155 In addition, the errors documented in
159 These system calls have been available on Linux since kernel 2.4;
160 glibc support is provided since version 2.3.
162 These system calls are Linux-specific.
164 .\" Andreas Gruenbacher,
165 .\" .RI < a.gruenbacher@computer.org >
166 .\" and the SGI XFS development team,
167 .\" .RI < linux-xfs@oss.sgi.com >.
168 .\" Please send any bug reports or comments to these addresses.
170 .\" The xattr(7) page refers to this text:
173 the VFS imposes a limit of 64 kB on the size of the extended
174 attribute name list returned by
176 If the total size of attribute names attached to a file exceeds this limit,
177 it is no longer possible to retrieve the list of attribute names.
179 The following program demonstrates the usage of
183 For the file whose pathname is provided as a command-line argument,
184 it lists all extended file attributes and their values.
186 To keep the code simple, the program assumes that attribute keys and
187 values are constant during the execution of the program.
188 A production program should expect and handle changes during
189 execution of the program.
191 the number of bytes required for attribute keys
192 might increase between the two calls to
194 An application could handle this possibility using
195 a loop that retries the call
196 (perhaps up to a predetermined maximum number of attempts)
197 with a larger buffer each time it fails with the error
201 could be handled similarly.
203 The following output was recorded by first creating a file, setting
204 some extended file attributes,
205 and then listing the attributes with the example program.
209 $ \fBtouch /tmp/foo\fP
210 $ \fBsetfattr -n user.fred -v chocolate /tmp/foo\fP
211 $ \fBsetfattr -n user.frieda -v bar /tmp/foo\fP
212 $ \fBsetfattr -n user.empty /tmp/foo\fP
213 $ \fB./listxattr /tmp/foo\fP
216 user.empty: <no value>
219 .SS Program source (listxattr.c)
225 #include <sys/types.h>
226 #include <sys/xattr.h>
229 main(int argc, char *argv[])
231 ssize_t buflen, keylen, vallen;
232 char *buf, *key, *val;
235 fprintf(stderr, "Usage: %s path\\n", argv[0]);
240 * Determine the length of the buffer needed.
242 buflen = listxattr(argv[1], NULL, 0);
248 printf("%s has no attributes.\\n", argv[1]);
253 * Allocate the buffer.
255 buf = malloc(buflen);
262 * Copy the list of attribute keys to the buffer.
264 buflen = listxattr(argv[1], buf, buflen);
271 * Loop over the list of zero terminated strings with the
272 * attribute keys. Use the remaining buffer length to determine
273 * the end of the list.
279 * Output attribute key.
284 * Determine length of the value.
286 vallen = getxattr(argv[1], key, NULL, 0);
293 * Allocate value buffer.
294 * One extra byte is needed to append 0x00.
296 val = malloc(vallen + 1);
303 * Copy value to buffer.
305 vallen = getxattr(argv[1], key, val, vallen);
310 * Output attribute value.
317 } else if (vallen == 0)
318 printf("<no value>");
323 * Forward to next attribute key.
325 keylen = strlen(key) + 1;