]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/listxattr.2
ip.7: tfix
[thirdparty/man-pages.git] / man2 / listxattr.2
CommitLineData
bf5a7247
MK
1.\" Copyright (C) Andreas Gruenbacher, February 2001
2.\" Copyright (C) Silicon Graphics Inc, September 2001
636a3877 3.\" Copyright (C) 2015 Heinrich Schuchardt <xypron.glpk@gmx.de>
fea681da 4.\"
1dd72f9c 5.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
fea681da
MK
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.
10.\"
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.
15.\"
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.
20.\"
21.\" You should have received a copy of the GNU General Public
c715f741
MK
22.\" License along with this manual; if not, see
23.\" <http://www.gnu.org/licenses/>.
6a8d8745 24.\" %%%LICENSE_END
fea681da 25.\"
67d2c687 26.TH LISTXATTR 2 2015-05-07 "Linux" "Linux Programmer's Manual"
fea681da
MK
27.SH NAME
28listxattr, llistxattr, flistxattr \- list extended attribute names
29.SH SYNOPSIS
30.fam C
31.nf
32.B #include <sys/types.h>
4f72d935 33.B #include <sys/xattr.h>
fea681da 34.sp
c10859eb
MK
35.BI "ssize_t listxattr(const char\ *" path ", char\ *" list \
36", size_t " size );
37.BI "ssize_t llistxattr(const char\ *" path ", char\ *" list \
38", size_t " size );
d3b03141 39.BI "ssize_t flistxattr(int " fd ", char\ *" list ", size_t " size );
fea681da
MK
40.fi
41.fam T
42.SH DESCRIPTION
2d10343d
MK
43Extended attributes are
44.IR name : value
0967c11f 45pairs associated with inodes (files, directories, symbolic links, etc.).
fea681da 46They are extensions to the normal attributes which are associated
75b94dc3 47with all inodes in the system (i.e., the
fea681da
MK
48.BR stat (2)
49data).
50A complete overview of extended attributes concepts can be found in
89fabe2e 51.BR xattr (7).
fea681da 52.PP
e511ffb6 53.BR listxattr ()
cc66e8b4 54retrieves the list
fea681da
MK
55of extended attribute names associated with the given
56.I path
9ee4a2b6 57in the filesystem.
cc66e8b4
MK
58The retrieved list is placed in
59.IR list ,
60a caller-allocated buffer whose size (in bytes) is specified in the argument
61.IR size .
836f07c1 62The list is the set of (null-terminated) names, one after the other.
fea681da
MK
63Names of extended attributes to which the calling process does not
64have access may be omitted from the list.
65The length of the attribute name
66.I list
67is returned.
68.PP
e511ffb6 69.BR llistxattr ()
c13182ef 70is identical to
e511ffb6 71.BR listxattr (),
fea681da
MK
72except in the case of a symbolic link, where the list of names of
73extended attributes associated with the link itself is retrieved,
74not the file that it refers to.
75.PP
e511ffb6 76.BR flistxattr ()
fea681da 77is identical to
e511ffb6 78.BR listxattr (),
d3b03141
MK
79only the open file referred to by
80.I fd
fea681da
MK
81(as returned by
82.BR open (2))
83is interrogated in place of
84.IR path .
85.PP
86A single extended attribute
87.I name
f1ec5ad1 88is a null-terminated string.
df8a3cac 89The name includes a namespace prefix; there may be several, disjoint
fea681da 90namespaces associated with an individual inode.
a4526c2f
MK
91
92If
fea681da 93.I size
a4526c2f
MK
94is specified as zero, these calls return the current size of the
95list of extended attribute names (and leave
96.I list
97unchanged).
98This can be used to determine the size of the buffer that
99should be supplied in a subsequent call.
100(But, bear in mind that there is a possibility that the
101set of extended attributes may change between the two calls,
102so that it is still necessary to check the return status
103from the second call.)
2b2581ee 104.SS Example
fea681da
MK
105The
106.I list
836f07c1 107of names is returned as an unordered array of null-terminated character
f81fb444 108strings (attribute names are separated by null bytes (\(aq\\0\(aq)), like this:
fea681da
MK
109.fam C
110.RS
111.nf
3fdbf2a2 112
fea681da
MK
113user.name1\\0system.name1\\0user.name2\\0
114.fi
115.RE
116.fam T
117.P
f1ec5ad1
MK
118Filesystems that implement POSIX ACLs using
119extended attributes might return a
fea681da
MK
120.I list
121like this:
122.fam C
123.RS
124.nf
3fdbf2a2 125
fea681da
MK
126system.posix_acl_access\\0system.posix_acl_default\\0
127.fi
128.RE
129.fam T
130.SH RETURN VALUE
2507f0aa 131On success, a nonnegative number is returned indicating the size of the
fea681da
MK
132extended attribute name list.
133On failure, \-1 is returned and
134.I errno
135is set appropriately.
92d67001
MK
136.SH ERRORS
137.TP
92b1a2cc
MK
138.B E2BIG
139The size of the list of attribute names for this file exceeds
140the system-imposed limit.
7353fc54 141See BUGS.
92b1a2cc 142.TP
92d67001 143.B ENOTSUP
9ee4a2b6 144Extended attributes are not supported by the filesystem, or are disabled.
92d67001
MK
145.TP
146.B ERANGE
147The
fea681da
MK
148.I size
149of the
150.I list
92d67001 151buffer is too small to hold the result.
fea681da 152.PP
92d67001 153In addition, the errors documented in
fea681da 154.BR stat (2)
92d67001 155can also occur.
570dd978
MK
156.SH VERSIONS
157These system calls have been available on Linux since kernel 2.4;
158glibc support is provided since version 2.3.
47297adb 159.SH CONFORMING TO
8382f16d 160These system calls are Linux-specific.
dc919d09
MK
161.\" .SH AUTHORS
162.\" Andreas Gruenbacher,
163.\" .RI < a.gruenbacher@computer.org >
164.\" and the SGI XFS development team,
165.\" .RI < linux-xfs@oss.sgi.com >.
166.\" Please send any bug reports or comments to these addresses.
636a3877
HS
167.SH EXAMPLE
168The following program demonstrates the usage of
169.BR listxattr ()
170and
171.BR getxattr (2).
9c321ba2
MK
172For the file whose pathname is provided as a command-line argument,
173it lists all extended file attributes and their values.
636a3877 174
9c321ba2
MK
175To keep the code simple, the program assumes that attribute keys and
176values are constant during the execution of the program.
177A production program should expect and handle changes during
178execution of the program.
a8215524
MK
179For example,
180the number of bytes required for attribute keys
181might increase between the two calls to
182.BR listxattr ().
183An application could handle this possibility using
184a loop that retries the call
185(perhaps up to a predetermined maximum number of attempts)
186with a larger buffer each time it fails with the error
187.BR ERANGE .
188Calls to
189.BR getxattr (2)
190could be handled similarly.
636a3877 191
9c321ba2
MK
192The following output was recorded by first creating a file, setting
193some extended file attributes,
194and then listing the attributes with the example program.
636a3877
HS
195.SS Example output
196.in +4n
197.nf
9c321ba2
MK
198$ \fBtouch /tmp/foo\fP
199$ \fBsetfattr -n user.fred -v chocolate /tmp/foo\fP
200$ \fBsetfattr -n user.frieda -v bar /tmp/foo\fP
201$ \fBsetfattr -n user.empty /tmp/foo\fP
202$ \fB./listxattr /tmp/foo\fP
636a3877
HS
203user.fred: chocolate
204user.frieda: bar
205user.empty: <no value>
206.fi
207.in
9c321ba2 208.SS Program source (listxattr.c)
636a3877
HS
209.nf
210#include <malloc.h>
211#include <stdio.h>
212#include <stdlib.h>
213#include <string.h>
214#include <sys/types.h>
215#include <sys/xattr.h>
216
217int
218main(int argc, char *argv[])
219{
220 ssize_t buflen, keylen, vallen;
221 char *buf, *key, *val;
222
223 if (argc != 2) {
224 fprintf(stderr, "Usage: %s path\\n", argv[0]);
225 exit(EXIT_FAILURE);
226 }
227
228 /*
229 * Determine the length of the buffer needed.
230 */
231 buflen = listxattr(argv[1], NULL, 0);
232 if (buflen == \-1) {
233 perror("listxattr");
234 exit(EXIT_FAILURE);
235 }
236 if (buflen == 0) {
9c321ba2 237 printf("%s has no attributes.\\n", argv[1]);
636a3877
HS
238 exit(EXIT_SUCCESS);
239 }
240
241 /*
242 * Allocate the buffer.
243 */
244 buf = malloc(buflen);
245 if (buf == NULL) {
246 perror("malloc");
247 exit(EXIT_FAILURE);
248 }
249
250 /*
251 * Copy the list of attribute keys to the buffer.
252 */
253 buflen = listxattr(argv[1], buf, buflen);
254 if (buflen == \-1) {
255 perror("listxattr");
256 exit(EXIT_FAILURE);
257 }
258
259 /*
9c321ba2
MK
260 * Loop over the list of zero terminated strings with the
261 * attribute keys. Use the remaining buffer length to determine
262 * the end of the list.
636a3877
HS
263 */
264 key = buf;
265 while (buflen > 0) {
266
267 /*
268 * Output attribute key.
269 */
270 printf("%s: ", key);
271
272 /*
273 * Determine length of the value.
274 */
275 vallen = getxattr(argv[1], key, NULL, 0);
276 if (vallen == \-1)
277 perror("getxattr");
278
279 if (vallen > 0) {
280
281 /*
282 * Allocate value buffer.
283 * One extra byte is needed to append 0x00.
284 */
285 val = malloc(vallen + 1);
286 if (val == NULL) {
287 perror("malloc");
288 exit(EXIT_FAILURE);
289 }
290
291 /*
292 * Copy value to buffer.
293 */
294 vallen = getxattr(argv[1], key, val, vallen);
295 if (vallen == \-1)
296 perror("getxattr");
297 else {
298 /*
299 * Output attribute value.
300 */
301 val[vallen] = 0;
302 printf("%s", val);
303 }
304
305 free(val);
306 } else if (vallen == 0)
307 printf("<no value>");
308
309 printf("\\n");
310
311 /*
312 * Forward to next attribute key.
313 */
314 keylen = strlen(key) + 1;
315 buflen \-= keylen;
316 key += keylen;
317 }
318
319 free(buf);
320 exit(EXIT_SUCCESS);
321}
322.fi
92b1a2cc
MK
323.SH BUGS
324.\" The xattr(7) page refers to this text:
325As noted in
326.BR xattr (7),
327the VFS imposes a limit of 64 kB on the size of the extended
328attribute name list returned by
329.BR listxattr (7).
330If the total size of attribute names attached to a file exceeds this limit,
331it is no longer possible to retrieve the list of attribute names.
fea681da
MK
332.SH SEE ALSO
333.BR getfattr (1),
334.BR setfattr (1),
335.BR getxattr (2),
336.BR open (2),
337.BR removexattr (2),
338.BR setxattr (2),
339.BR stat (2),
89fabe2e
MK
340.BR symlink (7),
341.BR xattr (7)