Debian Codegraph @

upstream/debian-unstable/man2/listxattr.2

raw
1 .\" Copyright 2001, Andreas Grünbacher <[email protected]>
2 .\" Copyright 2001, Silicon Graphics, Inc.
3 .\" Copyright, the authors of the Linux man-pages project
4 .\"
5 .\" SPDX-License-Identifier: GPL-2.0-or-later
6 .\"
7 .TH listxattr 2 2026-02-08 "Linux man-pages 6.18"
8 .SH NAME
9 listxattr, llistxattr, flistxattr \- list extended attribute names
10 .SH LIBRARY
11 Standard C library
12 .RI ( libc ,\~ \-lc )
13 .SH SYNOPSIS
14 .nf
15 .B #include <sys/xattr.h>
16 .P
17 .BI "ssize_t listxattr(const char *" path ", char *_Nullable " list \
18 ", size_t " size );
19 .BI "ssize_t llistxattr(const char *" path ", char *_Nullable " list \
20 ", size_t " size );
21 .BI "ssize_t flistxattr(int " fd ", char *_Nullable " list ", size_t " size );
22 .fi
23 .SH DESCRIPTION
24 Extended attributes are
25 .IR name : value
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
29 .BR stat (2)
30 data).
31 A complete overview of extended attributes concepts can be found in
32 .BR xattr (7).
33 .P
34 .BR listxattr ()
35 retrieves the list
36 of extended attribute names associated with the given
37 .I path
38 in the filesystem.
39 The retrieved list is placed in
40 .IR list ,
41 a caller-allocated buffer whose size (in bytes) is specified in the argument
42 .IR size .
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
47 .I list
48 is returned.
49 .P
50 .BR llistxattr ()
51 is identical to
52 .BR listxattr (),
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.
56 .P
57 .BR flistxattr ()
58 is identical to
59 .BR listxattr (),
60 only the open file referred to by
61 .I fd
62 (as returned by
63 .BR open (2))
64 is interrogated in place of
65 .IR path .
66 .P
67 A single extended attribute
68 .I name
69 is a null-terminated string.
70 The name includes a namespace prefix;
71 there may be several,
72 disjoint namespaces associated with an individual inode.
73 .P
74 If
75 .I size
76 is specified as zero, these calls return the current size of the
77 list of extended attribute names (and leave
78 .I list
79 unchanged).
80 This can be used to determine the size of the buffer that
81 should be supplied in a subsequent call.
82 (But, bear in mind that there is a possibility that the
83 set of extended attributes may change between the two calls,
84 so that it is still necessary to check the return status
85 from the second call.)
86 .SS Example
87 The
88 .I list
89 of names is returned as an unordered array of null-terminated character strings
90 (attribute names are separated by null bytes (\[aq]\[rs]0\[aq])),
91 like this:
92 .P
93 .in +4n
94 .EX
95 user.name1\[rs]0system.name1\[rs]0user.name2\[rs]0
96 .EE
97 .in
98 .P
99 Filesystems that implement POSIX ACLs using
100 extended attributes might return a
101 .I list
102 like this:
103 .P
104 .in +4n
105 .EX
106 system.posix_acl_access\[rs]0system.posix_acl_default\[rs]0
107 .EE
108 .in
109 .SH RETURN VALUE
110 On success, a nonnegative number is returned indicating the size of the
111 extended attribute name list.
112 On failure, \-1 is returned and
113 .I errno
114 is set to indicate the error.
115 .SH ERRORS
116 .TP
117 .B E2BIG
118 The size of the list of extended attribute names
119 is larger than the maximum size allowed;
120 the list cannot be retrieved.
121 This can happen on filesystems that support
122 an unlimited number of extended attributes per file,
123 such as XFS,
124 for example.
125 See BUGS.
126 .TP
127 .B ENOTSUP
128 Extended attributes are not supported by the filesystem, or are disabled.
129 .TP
130 .B ERANGE
131 The
132 .I size
133 of the
134 .I list
135 buffer is too small to hold the result.
136 .P
137 In addition, the errors documented in
138 .BR stat (2)
139 can also occur.
140 .SH STANDARDS
141 Linux.
142 .SH HISTORY
143 Linux 2.4,
144 glibc 2.3.
145 .\" .SH AUTHORS
146 .\" Andreas Gruenbacher,
147 .\" .RI < [email protected] >
148 .\" and the SGI XFS development team,
149 .\" .RI < [email protected] >.
150 .\" Please send any bug reports or comments to these addresses.
151 .SH BUGS
152 .\" The xattr(7) page refers to this text:
153 As noted in
154 .BR xattr (7),
155 the VFS imposes a limit of 64\ kB on the size of the extended
156 attribute name list returned by
157 .BR listxattr ().
158 If the total size of attribute names attached to a file exceeds this limit,
159 it is no longer possible to retrieve the list of attribute names.
160 .SH EXAMPLES
161 The following program demonstrates the usage of
162 .BR listxattr ()
163 and
164 .BR getxattr (2).
165 For the file whose pathname is provided as a command-line argument,
166 it lists all extended file attributes and their values.
167 .P
168 To keep the code simple, the program assumes that attribute keys and
169 values are constant during the execution of the program.
170 A production program should expect and handle changes during
171 execution of the program.
172 For example,
173 the number of bytes required for attribute keys
174 might increase between the two calls to
175 .BR listxattr ().
176 An application could handle this possibility using
177 a loop that retries the call
178 (perhaps up to a predetermined maximum number of attempts)
179 with a larger buffer each time it fails with the error
180 .BR ERANGE .
181 Calls to
182 .BR getxattr (2)
183 could be handled similarly.
184 .P
185 The following output was recorded by first creating a file, setting
186 some extended file attributes,
187 and then listing the attributes with the example program.
188 .SS Example output
189 .in +4n
190 .EX
191 .RB $ " touch /tmp/foo" ;
192 .RB $ " setfattr \-n user.fred \-v chocolate /tmp/foo" ;
193 .RB $ " setfattr \-n user.frieda \-v bar /tmp/foo" ;
194 .RB $ " setfattr \-n user.empty /tmp/foo" ;
195 .RB $ " ./listxattr /tmp/foo" ;
196 user.fred: chocolate
197 user.frieda: bar
198 user.empty: <no value>
199 .EE
200 .in
201 .SS Program source (listxattr.c)
202 .\" SRC BEGIN (listxattr.c)
203 .EX
204 #include <stdio.h>
205 #include <stdlib.h>
206 #include <string.h>
207 #include <sys/xattr.h>
208 \&
209 int
210 main(int argc, char *argv[])
211 {
212 char *buf, *key, *val;
213 ssize_t buflen, keylen, vallen;
214 \&
215 if (argc != 2) {
216 fprintf(stderr, "Usage: %s path\[rs]n", argv[0]);
217 exit(EXIT_FAILURE);
218 }
219 \&
220 /*
221 * Determine the length of the buffer needed.
222 */
223 buflen = listxattr(argv[1], NULL, 0);
224 if (buflen == \-1) {
225 perror("listxattr");
226 exit(EXIT_FAILURE);
227 }
228 if (buflen == 0) {
229 printf("%s has no attributes.\[rs]n", argv[1]);
230 exit(EXIT_SUCCESS);
231 }
232 \&
233 /*
234 * Allocate the buffer.
235 */
236 buf = malloc(buflen);
237 if (buf == NULL) {
238 perror("malloc");
239 exit(EXIT_FAILURE);
240 }
241 \&
242 /*
243 * Copy the list of attribute keys to the buffer.
244 */
245 buflen = listxattr(argv[1], buf, buflen);
246 if (buflen == \-1) {
247 perror("listxattr");
248 exit(EXIT_FAILURE);
249 }
250 \&
251 /*
252 * Loop over the list of zero terminated strings with the
253 * attribute keys. Use the remaining buffer length to determine
254 * the end of the list.
255 */
256 key = buf;
257 while (buflen > 0) {
258 \&
259 /*
260 * Output attribute key.
261 */
262 printf("%s: ", key);
263 \&
264 /*
265 * Determine length of the value.
266 */
267 vallen = getxattr(argv[1], key, NULL, 0);
268 if (vallen == \-1)
269 perror("getxattr");
270 \&
271 if (vallen > 0) {
272 \&
273 /*
274 * Allocate value buffer.
275 * One extra byte is needed to append 0x00.
276 */
277 val = malloc(vallen + 1);
278 if (val == NULL) {
279 perror("malloc");
280 exit(EXIT_FAILURE);
281 }
282 \&
283 /*
284 * Copy value to buffer.
285 */
286 vallen = getxattr(argv[1], key, val, vallen);
287 if (vallen == \-1) {
288 perror("getxattr");
289 } else {
290 /*
291 * Output attribute value.
292 */
293 val[vallen] = 0;
294 printf("%s", val);
295 }
296 \&
297 free(val);
298 } else if (vallen == 0) {
299 printf("<no value>");
300 }
301 \&
302 printf("\[rs]n");
303 \&
304 /*
305 * Forward to next attribute key.
306 */
307 keylen = strlen(key) + 1;
308 buflen \-= keylen;
309 key += keylen;
310 }
311 \&
312 free(buf);
313 exit(EXIT_SUCCESS);
314 }
315 .EE
316 .\" SRC END
317 .SH SEE ALSO
318 .BR getfattr (1),
319 .BR setfattr (1),
320 .BR getxattr (2),
321 .BR open (2),
322 .BR removexattr (2),
323 .BR setxattr (2),
324 .BR stat (2),
325 .BR symlink (7),
326 .BR xattr (7)