1 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
2 .\" And Copyright (C) 2011 Guillem Jover <guillem@hadrons.org>
3 .\" And Copyright (C) 2006, 2014 Michael Kerrisk
4 .\" All rights reserved.
6 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
15 .\" 3. All advertising materials mentioning features or use of this software
16 .\" must display the following acknowledgement:
17 .\" This product includes software developed by the University of
18 .\" California, Berkeley and its contributors.
19 .\" 4. Neither the name of the University nor the names of its contributors
20 .\" may be used to endorse or promote products derived from this software
21 .\" without specific prior written permission.
23 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .\" @(#)readlink.2 6.8 (Berkeley) 3/10/91
38 .\" Modified Sat Jul 24 00:10:21 1993 by Rik Faith (faith@cs.unc.edu)
39 .\" Modified Tue Jul 9 23:55:17 1996 by aeb
40 .\" Modified Fri Jan 24 00:26:00 1997 by aeb
41 .\" 2011-09-20, Guillem Jover <guillem@hadrons.org>:
42 .\" Added text on dynamically allocating buffer + example program
44 .TH READLINK 2 2014-10-15 "Linux" "Linux Programmer's Manual"
46 readlink, readlinkat \- read value of a symbolic link
49 .B #include <unistd.h>
51 .BI "ssize_t readlink(const char *" pathname ", char *" buf \
54 .BR "#include <fcntl.h> " "/* Definition of AT_* constants */"
55 .B #include <unistd.h>
57 .BI "ssize_t readlinkat(int " dirfd ", const char *" pathname ,
58 .BI " char *" buf ", size_t " bufsiz );
62 Feature Test Macro Requirements for glibc (see
63 .BR feature_test_macros (7)):
69 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
70 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED || _POSIX_C_SOURCE\ >=\ 200112L
79 _XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
88 places the contents of the symbolic link
95 does not append a null byte to
97 It will truncate the contents (to a length of
99 characters), in case the buffer is too small to hold all of the contents.
103 system call operates in exactly the same way as
105 except for the differences described here.
107 If the pathname given in
109 is relative, then it is interpreted relative to the directory
110 referred to by the file descriptor
112 (rather than relative to the current working directory of
113 the calling process, as is done by
115 for a relative pathname).
125 is interpreted relative to the current working
126 directory of the calling process (like
136 .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
138 can be an empty string,
139 in which case the call operates on the symbolic link referred to by
141 (which should have been obtained using
151 for an explanation of the need for
154 On success, these calls return the number of bytes placed in
156 On error, \-1 is returned and
158 is set to indicate the error.
162 Search permission is denied for a component of the path prefix.
164 .BR path_resolution (7).)
168 extends outside the process's allocated address space.
173 .\" At the glibc level, bufsiz is unsigned, so this error can only occur
174 .\" if bufsiz==0. However, the in the kernel syscall, bufsiz is signed,
175 .\" and this error can also occur if bufsiz < 0.
176 .\" See: http://thread.gmane.org/gmane.linux.man/380
177 .\" Subject: [patch 0/3] [RFC] kernel/glibc mismatch of "readlink" syscall?
180 The named file is not a symbolic link.
183 An I/O error occurred while reading from the filesystem.
186 Too many symbolic links were encountered in translating the pathname.
189 A pathname, or a component of a pathname, was too long.
192 The named file does not exist.
195 Insufficient kernel memory was available.
198 A component of the path prefix is not a directory.
200 The following additional errors can occur for
205 is not a valid file descriptor.
211 is a file descriptor referring to a file other than a directory.
214 was added to Linux in kernel 2.6.16;
215 library support was added to glibc in version 2.4.
220 first appeared in 4.2BSD),
221 POSIX.1-2001, POSIX.1-2008.
226 In versions of glibc up to and including glibc 2.4, the return type of
230 Nowadays, the return type is declared as
232 as (newly) required in POSIX.1-2001.
234 Using a statically sized buffer might not provide enough room for the
235 symbolic link contents.
236 The required size for the buffer can be obtained from the
238 value returned by a call to
241 However, the number of bytes written by
245 should be checked to make sure that the size of the
246 symbolic link did not increase between the calls.
247 Dynamically allocating the buffer for
251 also addresses a common portability problem when using
254 as this constant is not guaranteed to be defined per POSIX
255 if the system does not have such limit.
257 On older kernels where
259 is unavailable, the glibc wrapper function falls back to the use of
263 is a relative pathname,
264 glibc constructs a pathname based on the symbolic link in
266 that corresponds to the
270 The following program allocates the buffer needed by
272 dynamically from the information provided by
274 making sure there's no race condition between the calls.
277 #include <sys/types.h>
278 #include <sys/stat.h>
284 main(int argc, char *argv[])
291 fprintf(stderr, "Usage: %s <pathname>\\n", argv[0]);
295 if (lstat(argv[1], &sb) == \-1) {
300 linkname = malloc(sb.st_size + 1);
301 if (linkname == NULL) {
302 fprintf(stderr, "insufficient memory\\n");
306 r = readlink(argv[1], linkname, sb.st_size + 1);
313 if (r > sb.st_size) {
314 fprintf(stderr, "symlink increased in size "
315 "between lstat() and readlink()\\n");
319 linkname[r] = \(aq\\0\(aq;
321 printf("\(aq%s\(aq points to \(aq%s\(aq\\n", argv[1], linkname);
334 .BR path_resolution (7),
337 This page is part of release 3.78 of the Linux
340 A description of the project,
341 information about reporting bugs,
342 and the latest version of this page,
344 \%http://www.kernel.org/doc/man\-pages/.