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 .\" SPDX-License-Identifier: BSD-4-Clause-UC
8 .\" @(#)readlink.2 6.8 (Berkeley) 3/10/91
10 .\" Modified Sat Jul 24 00:10:21 1993 by Rik Faith (faith@cs.unc.edu)
11 .\" Modified Tue Jul 9 23:55:17 1996 by aeb
12 .\" Modified Fri Jan 24 00:26:00 1997 by aeb
13 .\" 2011-09-20, Guillem Jover <guillem@hadrons.org>:
14 .\" Added text on dynamically allocating buffer + example program
16 .TH READLINK 2 2021-08-27 "Linux" "Linux Programmer's Manual"
18 readlink, readlinkat \- read value of a symbolic link
21 .RI ( libc ", " \-lc )
24 .B #include <unistd.h>
26 .BI "ssize_t readlink(const char *restrict " pathname ", char *restrict " buf ,
27 .BI " size_t " bufsiz );
29 .BR "#include <fcntl.h> " "/* Definition of " AT_* " constants */"
30 .B #include <unistd.h>
32 .BI "ssize_t readlinkat(int " dirfd ", const char *restrict " pathname ,
33 .BI " char *restrict " buf ", size_t " bufsiz );
37 Feature Test Macro Requirements for glibc (see
38 .BR feature_test_macros (7)):
43 _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L
44 .\" || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
45 || /* Glibc <= 2.19: */ _BSD_SOURCE
51 _POSIX_C_SOURCE >= 200809L
57 places the contents of the symbolic link
64 does not append a terminating null byte to
66 It will (silently) truncate the contents (to a length of
68 characters), in case the buffer is too small to hold all of the contents.
72 system call operates in exactly the same way as
74 except for the differences described here.
76 If the pathname given in
78 is relative, then it is interpreted relative to the directory
79 referred to by the file descriptor
81 (rather than relative to the current working directory of
82 the calling process, as is done by
84 for a relative pathname).
94 is interpreted relative to the current working
95 directory of the calling process (like
105 .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
107 can be an empty string,
108 in which case the call operates on the symbolic link referred to by
110 (which should have been obtained using
120 for an explanation of the need for
123 On success, these calls return the number of bytes placed in
125 (If the returned value equals
127 then truncation may have occurred.)
128 On error, \-1 is returned and
130 is set to indicate the error.
134 Search permission is denied for a component of the path prefix.
136 .BR path_resolution (7).)
145 nor a valid file descriptor.
149 extends outside the process's allocated address space.
154 .\" At the glibc level, bufsiz is unsigned, so this error can only occur
155 .\" if bufsiz==0. However, the in the kernel syscall, bufsiz is signed,
156 .\" and this error can also occur if bufsiz < 0.
157 .\" See: http://thread.gmane.org/gmane.linux.man/380
158 .\" Subject: [patch 0/3] [RFC] kernel/glibc mismatch of "readlink" syscall?
161 The named file (i.e., the final filename component of
163 is not a symbolic link.
166 An I/O error occurred while reading from the filesystem.
169 Too many symbolic links were encountered in translating the pathname.
172 A pathname, or a component of a pathname, was too long.
175 The named file does not exist.
178 Insufficient kernel memory was available.
181 A component of the path prefix is not a directory.
188 is a file descriptor referring to a file other than a directory.
191 was added to Linux in kernel 2.6.16;
192 library support was added to glibc in version 2.4.
197 first appeared in 4.2BSD),
198 POSIX.1-2001, POSIX.1-2008.
203 In versions of glibc up to and including glibc 2.4, the return type of
207 Nowadays, the return type is declared as
209 as (newly) required in POSIX.1-2001.
211 Using a statically sized buffer might not provide enough room for the
212 symbolic link contents.
213 The required size for the buffer can be obtained from the
215 value returned by a call to
218 However, the number of bytes written by
222 should be checked to make sure that the size of the
223 symbolic link did not increase between the calls.
224 Dynamically allocating the buffer for
228 also addresses a common portability problem when using
231 as this constant is not guaranteed to be defined per POSIX
232 if the system does not have such limit.
234 On older kernels where
236 is unavailable, the glibc wrapper function falls back to the use of
240 is a relative pathname,
241 glibc constructs a pathname based on the symbolic link in
243 that corresponds to the
247 The following program allocates the buffer needed by
249 dynamically from the information provided by
251 falling back to a buffer of size
255 reports a size of zero.
257 .\" SRC BEGIN (readlink.c)
262 #include <sys/stat.h>
266 main(int argc, char *argv[])
270 ssize_t nbytes, bufsiz;
273 fprintf(stderr, "Usage: %s <pathname>\en", argv[0]);
277 if (lstat(argv[1], &sb) == \-1) {
282 /* Add one to the link size, so that we can determine whether
283 the buffer returned by readlink() was truncated. */
285 bufsiz = sb.st_size + 1;
287 /* Some magic symlinks under (for example) /proc and /sys
288 report \(aqst_size\(aq as zero. In that case, take PATH_MAX as
289 a "good enough" estimate. */
294 buf = malloc(bufsiz);
300 nbytes = readlink(argv[1], buf, bufsiz);
306 /* Print only \(aqnbytes\(aq of \(aqbuf\(aq, as it doesn't contain a terminating
307 null byte (\(aq\e0\(aq). */
308 printf("\(aq%s\(aq points to \(aq%.*s\(aq\en", argv[1], (int) nbytes, buf);
310 /* If the return value was equal to the buffer size, then the
311 the link target was larger than expected (perhaps because the
312 target was changed between the call to lstat() and the call to
313 readlink()). Warn the user that the returned target may have
316 if (nbytes == bufsiz)
317 printf("(Returned buffer may have been truncated)\en");
330 .BR path_resolution (7),