man-pages/man2/readlink.2.html
2021-03-31 01:06:50 +01:00

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">&nbsp;</A>
<H2>NAME</H2>
readlink, readlinkat - read value of a symbolic link
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</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 &lt;<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>&gt; </B>/* Definition of AT_* constants */
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</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&nbsp;&gt;=&nbsp;500 || _POSIX_C_SOURCE&nbsp;&gt;=&nbsp;200112L
<BR>&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;/*&nbsp;Glibc&nbsp;versions&nbsp;&lt;=&nbsp;2.19:&nbsp;*/&nbsp;_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&nbsp;&gt;=&nbsp;200809L
<DT id="4">Before glibc 2.10:<DD>
_ATFILE_SOURCE
</DL>
</DL>
<A NAME="lbAD">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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 &lt;<A HREF="file:///usr/include/sys/types.h">sys/types.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/sys/stat.h">sys/stat.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/limits.h">limits.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;stat&nbsp;sb;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;*buf;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;ssize_t&nbsp;nbytes,&nbsp;bufsiz;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;!=&nbsp;2)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Usage:&nbsp;%s&nbsp;&lt;pathname&gt;\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(lstat(argv[1],&nbsp;&amp;sb)&nbsp;==&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;lstat&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Add&nbsp;one&nbsp;to&nbsp;the&nbsp;link&nbsp;size,&nbsp;so&nbsp;that&nbsp;we&nbsp;can&nbsp;determine&nbsp;whether
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the&nbsp;buffer&nbsp;returned&nbsp;by&nbsp;readlink()&nbsp;was&nbsp;truncated.&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;bufsiz&nbsp;=&nbsp;sb.st_size&nbsp;+&nbsp;1;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Some&nbsp;magic&nbsp;symlinks&nbsp;under&nbsp;(for&nbsp;example)&nbsp;/proc&nbsp;and&nbsp;/sys
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;report&nbsp;'st_size'&nbsp;as&nbsp;zero.&nbsp;In&nbsp;that&nbsp;case,&nbsp;take&nbsp;PATH_MAX&nbsp;as
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a&nbsp;&quot;good&nbsp;enough&quot;&nbsp;estimate.&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(sb.st_size&nbsp;==&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bufsiz&nbsp;=&nbsp;PATH_MAX;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;buf&nbsp;=&nbsp;malloc(bufsiz);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(buf&nbsp;==&nbsp;NULL)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;malloc&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;nbytes&nbsp;=&nbsp;readlink(argv[1],&nbsp;buf,&nbsp;bufsiz);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(nbytes&nbsp;==&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;readlink&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;'%s'&nbsp;points&nbsp;to&nbsp;'%.*s'\n&quot;,&nbsp;argv[1],&nbsp;(int)&nbsp;nbytes,&nbsp;buf);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;If&nbsp;the&nbsp;return&nbsp;value&nbsp;was&nbsp;equal&nbsp;to&nbsp;the&nbsp;buffer&nbsp;size,&nbsp;then&nbsp;the
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the&nbsp;link&nbsp;target&nbsp;was&nbsp;larger&nbsp;than&nbsp;expected&nbsp;(perhaps&nbsp;because&nbsp;the
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;target&nbsp;was&nbsp;changed&nbsp;between&nbsp;the&nbsp;call&nbsp;to&nbsp;lstat()&nbsp;and&nbsp;the&nbsp;call&nbsp;to
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;readlink()).&nbsp;Warn&nbsp;the&nbsp;user&nbsp;that&nbsp;the&nbsp;returned&nbsp;target&nbsp;may&nbsp;have
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;been&nbsp;truncated.&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(nbytes&nbsp;==&nbsp;bufsiz)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;(Returned&nbsp;buffer&nbsp;may&nbsp;have&nbsp;been&nbsp;truncated)\n&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;free(buf);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAM">&nbsp;</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">&nbsp;</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">&nbsp;</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>