487 lines
12 KiB
HTML
487 lines
12 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of READLINK</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>READLINK</H1>
|
|
Section: Linux Programmer's Manual (2)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
readlink, readlinkat - read value of a symbolic link
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>></B>
|
|
|
|
<B>ssize_t readlink(const char *</B><I>pathname</I><B>, char *</B><I>buf</I><B>, size_t </B><I>bufsiz</I><B>);</B>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>> </B>/* Definition of AT_* constants */
|
|
<B>#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>></B>
|
|
|
|
<B>ssize_t readlinkat(int </B><I>dirfd</I><B>, const char *</B><I>pathname</I><B>,</B>
|
|
<B> char *</B><I>buf</I><B>, size_t </B><I>bufsiz</I><B>);</B>
|
|
|
|
</PRE>
|
|
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
|
|
|
|
|
|
<P>
|
|
|
|
|
|
<B>readlink</B>():
|
|
|
|
<DL COMPACT><DT id="1"><DD>
|
|
_XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L
|
|
|
|
<BR> || /* Glibc versions <= 2.19: */ _BSD_SOURCE
|
|
</DL>
|
|
|
|
<P>
|
|
|
|
<B>readlinkat</B>():
|
|
|
|
|
|
|
|
<DL COMPACT><DT id="2"><DD>
|
|
<DL COMPACT>
|
|
<DT id="3">Since glibc 2.10:<DD>
|
|
_POSIX_C_SOURCE >= 200809L
|
|
<DT id="4">Before glibc 2.10:<DD>
|
|
_ATFILE_SOURCE
|
|
</DL>
|
|
</DL>
|
|
|
|
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
<B>readlink</B>()
|
|
|
|
places the contents of the symbolic link
|
|
<I>pathname</I>
|
|
|
|
in the buffer
|
|
<I>buf</I>,
|
|
|
|
which has size
|
|
<I>bufsiz</I>.
|
|
|
|
<B>readlink</B>()
|
|
|
|
does not append a null byte to
|
|
<I>buf</I>.
|
|
|
|
It will (silently) truncate the contents (to a length of
|
|
<I>bufsiz</I>
|
|
|
|
characters), in case the buffer is too small to hold all of the contents.
|
|
<A NAME="lbAE"> </A>
|
|
<H3>readlinkat()</H3>
|
|
|
|
The
|
|
<B>readlinkat</B>()
|
|
|
|
system call operates in exactly the same way as
|
|
<B>readlink</B>(),
|
|
|
|
except for the differences described here.
|
|
<P>
|
|
|
|
If the pathname given in
|
|
<I>pathname</I>
|
|
|
|
is relative, then it is interpreted relative to the directory
|
|
referred to by the file descriptor
|
|
<I>dirfd</I>
|
|
|
|
(rather than relative to the current working directory of
|
|
the calling process, as is done by
|
|
<B>readlink</B>()
|
|
|
|
for a relative pathname).
|
|
<P>
|
|
|
|
If
|
|
<I>pathname</I>
|
|
|
|
is relative and
|
|
<I>dirfd</I>
|
|
|
|
is the special value
|
|
<B>AT_FDCWD</B>,
|
|
|
|
then
|
|
<I>pathname</I>
|
|
|
|
is interpreted relative to the current working
|
|
directory of the calling process (like
|
|
<B>readlink</B>()).
|
|
|
|
<P>
|
|
|
|
If
|
|
<I>pathname</I>
|
|
|
|
is absolute, then
|
|
<I>dirfd</I>
|
|
|
|
is ignored.
|
|
<P>
|
|
|
|
Since Linux 2.6.39,
|
|
|
|
<I>pathname</I>
|
|
|
|
can be an empty string,
|
|
in which case the call operates on the symbolic link referred to by
|
|
<I>dirfd</I>
|
|
|
|
(which should have been obtained using
|
|
<B><A HREF="/cgi-bin/man/man2html?2+open">open</A></B>(2)
|
|
|
|
with the
|
|
<B>O_PATH</B>
|
|
|
|
and
|
|
<B>O_NOFOLLOW</B>
|
|
|
|
flags).
|
|
<P>
|
|
|
|
See
|
|
<B><A HREF="/cgi-bin/man/man2html?2+openat">openat</A></B>(2)
|
|
|
|
for an explanation of the need for
|
|
<B>readlinkat</B>().
|
|
|
|
<A NAME="lbAF"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success, these calls return the number of bytes placed in
|
|
<I>buf</I>.
|
|
|
|
(If the returned value equals
|
|
<I>bufsiz</I>,
|
|
|
|
then truncation may have occurred.)
|
|
On error, -1 is returned and
|
|
<I>errno</I>
|
|
|
|
is set to indicate the error.
|
|
<A NAME="lbAG"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="5"><B>EACCES</B>
|
|
|
|
<DD>
|
|
Search permission is denied for a component of the path prefix.
|
|
(See also
|
|
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7).)
|
|
|
|
<DT id="6"><B>EFAULT</B>
|
|
|
|
<DD>
|
|
<I>buf</I>
|
|
|
|
extends outside the process's allocated address space.
|
|
<DT id="7"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
<I>bufsiz</I>
|
|
|
|
is not positive.
|
|
|
|
|
|
|
|
|
|
|
|
<DT id="8"><B>EINVAL</B>
|
|
|
|
<DD>
|
|
The named file (i.e., the final filename component of
|
|
<I>pathname</I>)
|
|
|
|
is not a symbolic link.
|
|
<DT id="9"><B>EIO</B>
|
|
|
|
<DD>
|
|
An I/O error occurred while reading from the filesystem.
|
|
<DT id="10"><B>ELOOP</B>
|
|
|
|
<DD>
|
|
Too many symbolic links were encountered in translating the pathname.
|
|
<DT id="11"><B>ENAMETOOLONG</B>
|
|
|
|
<DD>
|
|
A pathname, or a component of a pathname, was too long.
|
|
<DT id="12"><B>ENOENT</B>
|
|
|
|
<DD>
|
|
The named file does not exist.
|
|
<DT id="13"><B>ENOMEM</B>
|
|
|
|
<DD>
|
|
Insufficient kernel memory was available.
|
|
<DT id="14"><B>ENOTDIR</B>
|
|
|
|
<DD>
|
|
A component of the path prefix is not a directory.
|
|
</DL>
|
|
<P>
|
|
|
|
The following additional errors can occur for
|
|
<B>readlinkat</B>():
|
|
|
|
<DL COMPACT>
|
|
<DT id="15"><B>EBADF</B>
|
|
|
|
<DD>
|
|
<I>dirfd</I>
|
|
|
|
is not a valid file descriptor.
|
|
<DT id="16"><B>ENOTDIR</B>
|
|
|
|
<DD>
|
|
<I>pathname</I>
|
|
|
|
is relative and
|
|
<I>dirfd</I>
|
|
|
|
is a file descriptor referring to a file other than a directory.
|
|
</DL>
|
|
<A NAME="lbAH"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
<B>readlinkat</B>()
|
|
|
|
was added to Linux in kernel 2.6.16;
|
|
library support was added to glibc in version 2.4.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
<B>readlink</B>():
|
|
|
|
4.4BSD
|
|
(<B>readlink</B>()
|
|
|
|
first appeared in 4.2BSD),
|
|
POSIX.1-2001, POSIX.1-2008.
|
|
<P>
|
|
|
|
<B>readlinkat</B>():
|
|
|
|
POSIX.1-2008.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
In versions of glibc up to and including glibc 2.4, the return type of
|
|
<B>readlink</B>()
|
|
|
|
was declared as
|
|
<I>int</I>.
|
|
|
|
Nowadays, the return type is declared as
|
|
<I>ssize_t</I>,
|
|
|
|
as (newly) required in POSIX.1-2001.
|
|
<P>
|
|
|
|
Using a statically sized buffer might not provide enough room for the
|
|
symbolic link contents.
|
|
The required size for the buffer can be obtained from the
|
|
<I>stat.st_size</I>
|
|
|
|
value returned by a call to
|
|
<B><A HREF="/cgi-bin/man/man2html?2+lstat">lstat</A></B>(2)
|
|
|
|
on the link.
|
|
However, the number of bytes written by
|
|
<B>readlink</B>()
|
|
|
|
and
|
|
<B>readlinkat</B>()
|
|
|
|
should be checked to make sure that the size of the
|
|
symbolic link did not increase between the calls.
|
|
Dynamically allocating the buffer for
|
|
<B>readlink</B>()
|
|
|
|
and
|
|
<B>readlinkat</B>()
|
|
|
|
also addresses a common portability problem when using
|
|
<I>PATH_MAX</I>
|
|
|
|
for the buffer size,
|
|
as this constant is not guaranteed to be defined per POSIX
|
|
if the system does not have such limit.
|
|
<A NAME="lbAK"> </A>
|
|
<H3>Glibc notes</H3>
|
|
|
|
On older kernels where
|
|
<B>readlinkat</B>()
|
|
|
|
is unavailable, the glibc wrapper function falls back to the use of
|
|
<B>readlink</B>().
|
|
|
|
When
|
|
<I>pathname</I>
|
|
|
|
is a relative pathname,
|
|
glibc constructs a pathname based on the symbolic link in
|
|
<I>/proc/self/fd</I>
|
|
|
|
that corresponds to the
|
|
<I>dirfd</I>
|
|
|
|
argument.
|
|
<A NAME="lbAL"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The following program allocates the buffer needed by
|
|
<B>readlink</B>()
|
|
|
|
dynamically from the information provided by
|
|
<B><A HREF="/cgi-bin/man/man2html?2+lstat">lstat</A></B>(2),
|
|
|
|
falling back to a buffer of size
|
|
<B>PATH_MAX</B>
|
|
|
|
in cases where
|
|
<B><A HREF="/cgi-bin/man/man2html?2+lstat">lstat</A></B>(2)
|
|
|
|
reports a size of zero.
|
|
<P>
|
|
|
|
|
|
#include <<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>>
|
|
#include <<A HREF="file:///usr/include/sys/stat.h">sys/stat.h</A>>
|
|
#include <<A HREF="file:///usr/include/limits.h">limits.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> struct stat sb;
|
|
<BR> char *buf;
|
|
<BR> ssize_t nbytes, bufsiz;
|
|
<P>
|
|
<BR> if (argc != 2) {
|
|
<BR> fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> if (lstat(argv[1], &sb) == -1) {
|
|
<BR> perror("lstat");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> /* Add one to the link size, so that we can determine whether
|
|
<BR> the buffer returned by readlink() was truncated. */
|
|
<P>
|
|
<BR> bufsiz = sb.st_size + 1;
|
|
<P>
|
|
<BR> /* Some magic symlinks under (for example) /proc and /sys
|
|
<BR> report 'st_size' as zero. In that case, take PATH_MAX as
|
|
<BR> a "good enough" estimate. */
|
|
<P>
|
|
<BR> if (sb.st_size == 0)
|
|
<BR> bufsiz = PATH_MAX;
|
|
<P>
|
|
<BR> buf = malloc(bufsiz);
|
|
<BR> if (buf == NULL) {
|
|
<BR> perror("malloc");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> nbytes = readlink(argv[1], buf, bufsiz);
|
|
<BR> if (nbytes == -1) {
|
|
<BR> perror("readlink");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> printf("'%s' points to '%.*s'\n", argv[1], (int) nbytes, buf);
|
|
<P>
|
|
<BR> /* If the return value was equal to the buffer size, then the
|
|
<BR> the link target was larger than expected (perhaps because the
|
|
<BR> target was changed between the call to lstat() and the call to
|
|
<BR> readlink()). Warn the user that the returned target may have
|
|
<BR> been truncated. */
|
|
<P>
|
|
<BR> if (nbytes == bufsiz)
|
|
<BR> printf("(Returned buffer may have been truncated)\n");
|
|
<P>
|
|
<BR> free(buf);
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?1+readlink">readlink</A></B>(1),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+lstat">lstat</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+stat">stat</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+symlink">symlink</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+realpath">realpath</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+path_resolution">path_resolution</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+symlink">symlink</A></B>(7)
|
|
|
|
<A NAME="lbAN"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="17"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="18"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="19"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="20"><A HREF="#lbAE">readlinkat()</A><DD>
|
|
</DL>
|
|
<DT id="21"><A HREF="#lbAF">RETURN VALUE</A><DD>
|
|
<DT id="22"><A HREF="#lbAG">ERRORS</A><DD>
|
|
<DT id="23"><A HREF="#lbAH">VERSIONS</A><DD>
|
|
<DT id="24"><A HREF="#lbAI">CONFORMING TO</A><DD>
|
|
<DT id="25"><A HREF="#lbAJ">NOTES</A><DD>
|
|
<DL>
|
|
<DT id="26"><A HREF="#lbAK">Glibc notes</A><DD>
|
|
</DL>
|
|
<DT id="27"><A HREF="#lbAL">EXAMPLE</A><DD>
|
|
<DT id="28"><A HREF="#lbAM">SEE ALSO</A><DD>
|
|
<DT id="29"><A HREF="#lbAN">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:34 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|