Blame man2/listxattr.2

Packit 7cfc04
.\" Copyright (C) Andreas Gruenbacher, February 2001
Packit 7cfc04
.\" Copyright (C) Silicon Graphics Inc, September 2001
Packit 7cfc04
.\" Copyright (C) 2015 Heinrich Schuchardt <xypron.glpk@gmx.de>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
Packit 7cfc04
.\" This is free documentation; you can redistribute it and/or
Packit 7cfc04
.\" modify it under the terms of the GNU General Public License as
Packit 7cfc04
.\" published by the Free Software Foundation; either version 2 of
Packit 7cfc04
.\" the License, or (at your option) any later version.
Packit 7cfc04
.\"
Packit 7cfc04
.\" The GNU General Public License's references to "object code"
Packit 7cfc04
.\" and "executables" are to be interpreted as the output of any
Packit 7cfc04
.\" document formatting or typesetting system, including
Packit 7cfc04
.\" intermediate and printed output.
Packit 7cfc04
.\"
Packit 7cfc04
.\" This manual is distributed in the hope that it will be useful,
Packit 7cfc04
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 7cfc04
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 7cfc04
.\" GNU General Public License for more details.
Packit 7cfc04
.\"
Packit 7cfc04
.\" You should have received a copy of the GNU General Public
Packit 7cfc04
.\" License along with this manual; if not, see
Packit 7cfc04
.\" <http://www.gnu.org/licenses/>.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.TH LISTXATTR 2 2017-09-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
listxattr, llistxattr, flistxattr \- list extended attribute names
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.fam C
Packit 7cfc04
.nf
Packit 7cfc04
.B #include <sys/types.h>
Packit 7cfc04
.B #include <sys/xattr.h>
Packit 7cfc04
.PP
Packit 7cfc04
.BI "ssize_t listxattr(const char\ *" path ", char\ *" list \
Packit 7cfc04
", size_t " size );
Packit 7cfc04
.BI "ssize_t llistxattr(const char\ *" path ", char\ *" list \
Packit 7cfc04
", size_t " size );
Packit 7cfc04
.BI "ssize_t flistxattr(int " fd ", char\ *" list ", size_t " size );
Packit 7cfc04
.fi
Packit 7cfc04
.fam T
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
Extended attributes are
Packit 7cfc04
.IR name : value
Packit 7cfc04
pairs associated with inodes (files, directories, symbolic links, etc.).
Packit 7cfc04
They are extensions to the normal attributes which are associated
Packit 7cfc04
with all inodes in the system (i.e., the
Packit 7cfc04
.BR stat (2)
Packit 7cfc04
data).
Packit 7cfc04
A complete overview of extended attributes concepts can be found in
Packit 7cfc04
.BR xattr (7).
Packit 7cfc04
.PP
Packit 7cfc04
.BR listxattr ()
Packit 7cfc04
retrieves the list
Packit 7cfc04
of extended attribute names associated with the given
Packit 7cfc04
.I path
Packit 7cfc04
in the filesystem.
Packit 7cfc04
The retrieved list is placed in
Packit 7cfc04
.IR list ,
Packit 7cfc04
a caller-allocated buffer whose size (in bytes) is specified in the argument
Packit 7cfc04
.IR size .
Packit 7cfc04
The list is the set of (null-terminated) names, one after the other.
Packit 7cfc04
Names of extended attributes to which the calling process does not
Packit 7cfc04
have access may be omitted from the list.
Packit 7cfc04
The length of the attribute name
Packit 7cfc04
.I list
Packit 7cfc04
is returned.
Packit 7cfc04
.PP
Packit 7cfc04
.BR llistxattr ()
Packit 7cfc04
is identical to
Packit 7cfc04
.BR listxattr (),
Packit 7cfc04
except in the case of a symbolic link, where the list of names of
Packit 7cfc04
extended attributes associated with the link itself is retrieved,
Packit 7cfc04
not the file that it refers to.
Packit 7cfc04
.PP
Packit 7cfc04
.BR flistxattr ()
Packit 7cfc04
is identical to
Packit 7cfc04
.BR listxattr (),
Packit 7cfc04
only the open file referred to by
Packit 7cfc04
.I fd
Packit 7cfc04
(as returned by
Packit 7cfc04
.BR open (2))
Packit 7cfc04
is interrogated in place of
Packit 7cfc04
.IR path .
Packit 7cfc04
.PP
Packit 7cfc04
A single extended attribute
Packit 7cfc04
.I name
Packit 7cfc04
is a null-terminated string.
Packit 7cfc04
The name includes a namespace prefix; there may be several, disjoint
Packit 7cfc04
namespaces associated with an individual inode.
Packit 7cfc04
.PP
Packit 7cfc04
If
Packit 7cfc04
.I size
Packit 7cfc04
is specified as zero, these calls return the current size of the
Packit 7cfc04
list of extended attribute names (and leave
Packit 7cfc04
.I list
Packit 7cfc04
unchanged).
Packit 7cfc04
This can be used to determine the size of the buffer that
Packit 7cfc04
should be supplied in a subsequent call.
Packit 7cfc04
(But, bear in mind that there is a possibility that the
Packit 7cfc04
set of extended attributes may change between the two calls,
Packit 7cfc04
so that it is still necessary to check the return status
Packit 7cfc04
from the second call.)
Packit 7cfc04
.SS Example
Packit 7cfc04
The
Packit 7cfc04
.I list
Packit 7cfc04
of names is returned as an unordered array of null-terminated character
Packit 7cfc04
strings (attribute names are separated by null bytes (\(aq\\0\(aq)), like this:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
user.name1\\0system.name1\\0user.name2\\0
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.PP
Packit 7cfc04
Filesystems that implement POSIX ACLs using
Packit 7cfc04
extended attributes might return a
Packit 7cfc04
.I list
Packit 7cfc04
like this:
Packit 7cfc04
.PP
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
system.posix_acl_access\\0system.posix_acl_default\\0
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.SH RETURN VALUE
Packit 7cfc04
On success, a nonnegative number is returned indicating the size of the
Packit 7cfc04
extended attribute name list.
Packit 7cfc04
On failure, \-1 is returned and
Packit 7cfc04
.I errno
Packit 7cfc04
is set appropriately.
Packit 7cfc04
.SH ERRORS
Packit 7cfc04
.TP
Packit 7cfc04
.B E2BIG
Packit 7cfc04
The size of the list of extended attribute names is larger than the maximum
Packit 7cfc04
size allowed; the list cannot be retrieved.
Packit 7cfc04
This can happen on filesystems that support an unlimited number of
Packit 7cfc04
extended attributes per file such as XFS, for example.
Packit 7cfc04
See BUGS.
Packit 7cfc04
.TP
Packit 7cfc04
.B ENOTSUP
Packit 7cfc04
Extended attributes are not supported by the filesystem, or are disabled.
Packit 7cfc04
.TP
Packit 7cfc04
.B ERANGE
Packit 7cfc04
The
Packit 7cfc04
.I size
Packit 7cfc04
of the
Packit 7cfc04
.I list
Packit 7cfc04
buffer is too small to hold the result.
Packit 7cfc04
.PP
Packit 7cfc04
In addition, the errors documented in
Packit 7cfc04
.BR stat (2)
Packit 7cfc04
can also occur.
Packit 7cfc04
.SH VERSIONS
Packit 7cfc04
These system calls have been available on Linux since kernel 2.4;
Packit 7cfc04
glibc support is provided since version 2.3.
Packit 7cfc04
.SH CONFORMING TO
Packit 7cfc04
These system calls are Linux-specific.
Packit 7cfc04
.\" .SH AUTHORS
Packit 7cfc04
.\" Andreas Gruenbacher,
Packit 7cfc04
.\" .RI < a.gruenbacher@computer.org >
Packit 7cfc04
.\" and the SGI XFS development team,
Packit 7cfc04
.\" .RI < linux-xfs@oss.sgi.com >.
Packit 7cfc04
.\" Please send any bug reports or comments to these addresses.
Packit 7cfc04
.SH BUGS
Packit 7cfc04
.\" The xattr(7) page refers to this text:
Packit 7cfc04
As noted in
Packit 7cfc04
.BR xattr (7),
Packit 7cfc04
the VFS imposes a limit of 64\ kB on the size of the extended
Packit 7cfc04
attribute name list returned by
Packit 7cfc04
.BR listxattr (7).
Packit 7cfc04
If the total size of attribute names attached to a file exceeds this limit,
Packit 7cfc04
it is no longer possible to retrieve the list of attribute names.
Packit 7cfc04
.SH EXAMPLE
Packit 7cfc04
The following program demonstrates the usage of
Packit 7cfc04
.BR listxattr ()
Packit 7cfc04
and
Packit 7cfc04
.BR getxattr (2).
Packit 7cfc04
For the file whose pathname is provided as a command-line argument,
Packit 7cfc04
it lists all extended file attributes and their values.
Packit 7cfc04
.PP
Packit 7cfc04
To keep the code simple, the program assumes that attribute keys and
Packit 7cfc04
values are constant during the execution of the program.
Packit 7cfc04
A production program should expect and handle changes during
Packit 7cfc04
execution of the program.
Packit 7cfc04
For example,
Packit 7cfc04
the number of bytes required for attribute keys
Packit 7cfc04
might increase between the two calls to
Packit 7cfc04
.BR listxattr ().
Packit 7cfc04
An application could handle this possibility using
Packit 7cfc04
a loop that retries the call
Packit 7cfc04
(perhaps up to a predetermined maximum number of attempts)
Packit 7cfc04
with a larger buffer each time it fails with the error
Packit 7cfc04
.BR ERANGE .
Packit 7cfc04
Calls to
Packit 7cfc04
.BR getxattr (2)
Packit 7cfc04
could be handled similarly.
Packit 7cfc04
.PP
Packit 7cfc04
The following output was recorded by first creating a file, setting
Packit 7cfc04
some extended file attributes,
Packit 7cfc04
and then listing the attributes with the example program.
Packit 7cfc04
.SS Example output
Packit 7cfc04
.in +4n
Packit 7cfc04
.EX
Packit 7cfc04
$ \fBtouch /tmp/foo\fP
Packit 7cfc04
$ \fBsetfattr -n user.fred -v chocolate /tmp/foo\fP
Packit 7cfc04
$ \fBsetfattr -n user.frieda -v bar /tmp/foo\fP
Packit 7cfc04
$ \fBsetfattr -n user.empty /tmp/foo\fP
Packit 7cfc04
$ \fB./listxattr /tmp/foo\fP
Packit 7cfc04
user.fred: chocolate
Packit 7cfc04
user.frieda: bar
Packit 7cfc04
user.empty: <no value>
Packit 7cfc04
.EE
Packit 7cfc04
.in
Packit 7cfc04
.SS Program source (listxattr.c)
Packit 7cfc04
.EX
Packit 7cfc04
#include <malloc.h>
Packit 7cfc04
#include <stdio.h>
Packit 7cfc04
#include <stdlib.h>
Packit 7cfc04
#include <string.h>
Packit 7cfc04
#include <sys/types.h>
Packit 7cfc04
#include <sys/xattr.h>
Packit 7cfc04
Packit 7cfc04
int
Packit 7cfc04
main(int argc, char *argv[])
Packit 7cfc04
{
Packit 7cfc04
    ssize_t buflen, keylen, vallen;
Packit 7cfc04
    char *buf, *key, *val;
Packit 7cfc04
Packit 7cfc04
    if (argc != 2) {
Packit 7cfc04
        fprintf(stderr, "Usage: %s path\\n", argv[0]);
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    /*
Packit 7cfc04
     * Determine the length of the buffer needed.
Packit 7cfc04
     */
Packit 7cfc04
    buflen = listxattr(argv[1], NULL, 0);
Packit 7cfc04
    if (buflen == \-1) {
Packit 7cfc04
        perror("listxattr");
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
    if (buflen == 0) {
Packit 7cfc04
        printf("%s has no attributes.\\n", argv[1]);
Packit 7cfc04
        exit(EXIT_SUCCESS);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    /*
Packit 7cfc04
     * Allocate the buffer.
Packit 7cfc04
     */
Packit 7cfc04
    buf = malloc(buflen);
Packit 7cfc04
    if (buf == NULL) {
Packit 7cfc04
        perror("malloc");
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    /*
Packit 7cfc04
     * Copy the list of attribute keys to the buffer.
Packit 7cfc04
     */
Packit 7cfc04
    buflen = listxattr(argv[1], buf, buflen);
Packit 7cfc04
    if (buflen == \-1) {
Packit 7cfc04
        perror("listxattr");
Packit 7cfc04
        exit(EXIT_FAILURE);
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    /*
Packit 7cfc04
     * Loop over the list of zero terminated strings with the
Packit 7cfc04
     * attribute keys. Use the remaining buffer length to determine
Packit 7cfc04
     * the end of the list.
Packit 7cfc04
     */
Packit 7cfc04
    key = buf;
Packit 7cfc04
    while (buflen > 0) {
Packit 7cfc04
Packit 7cfc04
        /*
Packit 7cfc04
         * Output attribute key.
Packit 7cfc04
         */
Packit 7cfc04
        printf("%s: ", key);
Packit 7cfc04
Packit 7cfc04
        /*
Packit 7cfc04
         * Determine length of the value.
Packit 7cfc04
         */
Packit 7cfc04
        vallen = getxattr(argv[1], key, NULL, 0);
Packit 7cfc04
        if (vallen == \-1)
Packit 7cfc04
            perror("getxattr");
Packit 7cfc04
Packit 7cfc04
        if (vallen > 0) {
Packit 7cfc04
Packit 7cfc04
            /*
Packit 7cfc04
             * Allocate value buffer.
Packit 7cfc04
             * One extra byte is needed to append 0x00.
Packit 7cfc04
             */
Packit 7cfc04
            val = malloc(vallen + 1);
Packit 7cfc04
            if (val == NULL) {
Packit 7cfc04
                perror("malloc");
Packit 7cfc04
                exit(EXIT_FAILURE);
Packit 7cfc04
            }
Packit 7cfc04
Packit 7cfc04
            /*
Packit 7cfc04
             * Copy value to buffer.
Packit 7cfc04
             */
Packit 7cfc04
            vallen = getxattr(argv[1], key, val, vallen);
Packit 7cfc04
            if (vallen == \-1)
Packit 7cfc04
                perror("getxattr");
Packit 7cfc04
            else {
Packit 7cfc04
                /*
Packit 7cfc04
                 * Output attribute value.
Packit 7cfc04
                 */
Packit 7cfc04
                val[vallen] = 0;
Packit 7cfc04
                printf("%s", val);
Packit 7cfc04
            }
Packit 7cfc04
Packit 7cfc04
            free(val);
Packit 7cfc04
        } else if (vallen == 0)
Packit 7cfc04
            printf("<no value>");
Packit 7cfc04
Packit 7cfc04
        printf("\\n");
Packit 7cfc04
Packit 7cfc04
        /*
Packit 7cfc04
         * Forward to next attribute key.
Packit 7cfc04
         */
Packit 7cfc04
        keylen = strlen(key) + 1;
Packit 7cfc04
        buflen \-= keylen;
Packit 7cfc04
        key += keylen;
Packit 7cfc04
    }
Packit 7cfc04
Packit 7cfc04
    free(buf);
Packit 7cfc04
    exit(EXIT_SUCCESS);
Packit 7cfc04
}
Packit 7cfc04
.EE
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.BR getfattr (1),
Packit 7cfc04
.BR setfattr (1),
Packit 7cfc04
.BR getxattr (2),
Packit 7cfc04
.BR open (2),
Packit 7cfc04
.BR removexattr (2),
Packit 7cfc04
.BR setxattr (2),
Packit 7cfc04
.BR stat (2),
Packit 7cfc04
.BR symlink (7),
Packit 7cfc04
.BR xattr (7)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.