chmod.2, chown.2, open.2, mkdir.2, mknod.2, readlink.2, stat.2, symlink.2, mkfifo...
[man-pages.git] / man2 / readlink.2
bloba1ffbf7d0182fe0bbfcc3767d2a15d0164f34d35
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.
5 .\"
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
9 .\" are met:
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.
22 .\"
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
33 .\" SUCH DAMAGE.
34 .\" %%%LICENSE_END
35 .\"
36 .\"     @(#)readlink.2  6.8 (Berkeley) 3/10/91
37 .\"
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
43 .\"
44 .TH READLINK 2 2021-03-22 "Linux" "Linux Programmer's Manual"
45 .SH NAME
46 readlink, readlinkat \- read value of a symbolic link
47 .SH SYNOPSIS
48 .nf
49 .B #include <unistd.h>
50 .PP
51 .BI "ssize_t readlink(const char *restrict " pathname ", char *restrict " buf ,
52 .BI "                 size_t " bufsiz );
53 .PP
54 .BR "#include <fcntl.h>            " "/* Definition of " AT_* " constants */"
55 .B #include <unistd.h>
56 .PP
57 .BI "ssize_t readlinkat(int " dirfd ", const char *restrict " pathname ,
58 .BI "                 char *restrict " buf ", size_t " bufsiz );
59 .PP
60 .fi
61 .RS -4
62 Feature Test Macro Requirements for glibc (see
63 .BR feature_test_macros (7)):
64 .RE
65 .PP
66 .BR readlink ():
67 .nf
68     _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L
69 .\"    || _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
70         || /* Glibc <= 2.19: */ _BSD_SOURCE
71 .fi
72 .PP
73 .BR readlinkat ():
74 .nf
75     Since glibc 2.10:
76         _POSIX_C_SOURCE >= 200809L
77     Before glibc 2.10:
78         _ATFILE_SOURCE
79 .fi
80 .SH DESCRIPTION
81 .BR readlink ()
82 places the contents of the symbolic link
83 .I pathname
84 in the buffer
85 .IR buf ,
86 which has size
87 .IR bufsiz .
88 .BR readlink ()
89 does not append a terminating null byte to
90 .IR buf .
91 It will (silently) truncate the contents (to a length of
92 .I bufsiz
93 characters), in case the buffer is too small to hold all of the contents.
94 .SS readlinkat()
95 The
96 .BR readlinkat ()
97 system call operates in exactly the same way as
98 .BR readlink (),
99 except for the differences described here.
101 If the pathname given in
102 .I pathname
103 is relative, then it is interpreted relative to the directory
104 referred to by the file descriptor
105 .I dirfd
106 (rather than relative to the current working directory of
107 the calling process, as is done by
108 .BR readlink ()
109 for a relative pathname).
112 .I pathname
113 is relative and
114 .I dirfd
115 is the special value
116 .BR AT_FDCWD ,
117 then
118 .I pathname
119 is interpreted relative to the current working
120 directory of the calling process (like
121 .BR readlink ()).
124 .I pathname
125 is absolute, then
126 .I dirfd
127 is ignored.
129 Since Linux 2.6.39,
130 .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
131 .I pathname
132 can be an empty string,
133 in which case the call operates on the symbolic link referred to by
134 .IR dirfd
135 (which should have been obtained using
136 .BR open (2)
137 with the
138 .B O_PATH
140 .B O_NOFOLLOW
141 flags).
144 .BR openat (2)
145 for an explanation of the need for
146 .BR readlinkat ().
147 .SH RETURN VALUE
148 On success, these calls return the number of bytes placed in
149 .IR buf .
150 (If the returned value equals
151 .IR bufsiz ,
152 then truncation may have occurred.)
153 On error, \-1 is returned and
154 .I errno
155 is set to indicate the error.
156 .SH ERRORS
158 .B EACCES
159 Search permission is denied for a component of the path prefix.
160 (See also
161 .BR path_resolution (7).)
163 .B EBADF
164 .RB ( readlinkat ())
165 .I pathname
166 is relative but
167 .I dirfd
168 is neither
169 .B AT_FDCWD
170 nor a valid file descriptor.
172 .B EFAULT
173 .I buf
174 extends outside the process's allocated address space.
176 .B EINVAL
177 .I bufsiz
178 is not positive.
179 .\" At the glibc level, bufsiz is unsigned, so this error can only occur
180 .\" if bufsiz==0.  However, the in the kernel syscall, bufsiz is signed,
181 .\" and this error can also occur if bufsiz < 0.
182 .\" See: http://thread.gmane.org/gmane.linux.man/380
183 .\" Subject: [patch 0/3] [RFC] kernel/glibc mismatch of "readlink" syscall?
185 .B EINVAL
186 The named file (i.e., the final filename component of
187 .IR pathname )
188 is not a symbolic link.
190 .B EIO
191 An I/O error occurred while reading from the filesystem.
193 .B ELOOP
194 Too many symbolic links were encountered in translating the pathname.
196 .B ENAMETOOLONG
197 A pathname, or a component of a pathname, was too long.
199 .B ENOENT
200 The named file does not exist.
202 .B ENOMEM
203 Insufficient kernel memory was available.
205 .B ENOTDIR
206 A component of the path prefix is not a directory.
208 .B ENOTDIR
209 .RB ( readlinkat ())
210 .I pathname
211 is relative and
212 .I dirfd
213 is a file descriptor referring to a file other than a directory.
214 .SH VERSIONS
215 .BR readlinkat ()
216 was added to Linux in kernel 2.6.16;
217 library support was added to glibc in version 2.4.
218 .SH CONFORMING TO
219 .BR readlink ():
220 4.4BSD
221 .RB ( readlink ()
222 first appeared in 4.2BSD),
223 POSIX.1-2001, POSIX.1-2008.
225 .BR readlinkat ():
226 POSIX.1-2008.
227 .SH NOTES
228 In versions of glibc up to and including glibc 2.4, the return type of
229 .BR readlink ()
230 was declared as
231 .IR int .
232 Nowadays, the return type is declared as
233 .IR ssize_t ,
234 as (newly) required in POSIX.1-2001.
236 Using a statically sized buffer might not provide enough room for the
237 symbolic link contents.
238 The required size for the buffer can be obtained from the
239 .I stat.st_size
240 value returned by a call to
241 .BR lstat (2)
242 on the link.
243 However, the number of bytes written by
244 .BR readlink ()
246 .BR readlinkat ()
247 should be checked to make sure that the size of the
248 symbolic link did not increase between the calls.
249 Dynamically allocating the buffer for
250 .BR readlink ()
252 .BR readlinkat ()
253 also addresses a common portability problem when using
254 .B PATH_MAX
255 for the buffer size,
256 as this constant is not guaranteed to be defined per POSIX
257 if the system does not have such limit.
258 .SS Glibc notes
259 On older kernels where
260 .BR readlinkat ()
261 is unavailable, the glibc wrapper function falls back to the use of
262 .BR readlink ().
263 When
264 .I pathname
265 is a relative pathname,
266 glibc constructs a pathname based on the symbolic link in
267 .IR /proc/self/fd
268 that corresponds to the
269 .IR dirfd
270 argument.
271 .SH EXAMPLES
272 The following program allocates the buffer needed by
273 .BR readlink ()
274 dynamically from the information provided by
275 .BR lstat (2),
276 falling back to a buffer of size
277 .BR PATH_MAX
278 in cases where
279 .BR lstat (2)
280 reports a size of zero.
283 #include <sys/types.h>
284 #include <sys/stat.h>
285 #include <limits.h>
286 #include <stdio.h>
287 #include <stdlib.h>
288 #include <unistd.h>
291 main(int argc, char *argv[])
293     struct stat sb;
294     char *buf;
295     ssize_t nbytes, bufsiz;
297     if (argc != 2) {
298         fprintf(stderr, "Usage: %s <pathname>\en", argv[0]);
299         exit(EXIT_FAILURE);
300     }
302     if (lstat(argv[1], &sb) == \-1) {
303         perror("lstat");
304         exit(EXIT_FAILURE);
305     }
307     /* Add one to the link size, so that we can determine whether
308        the buffer returned by readlink() was truncated. */
310     bufsiz = sb.st_size + 1;
312     /* Some magic symlinks under (for example) /proc and /sys
313        report \(aqst_size\(aq as zero. In that case, take PATH_MAX as
314        a "good enough" estimate. */
316     if (sb.st_size == 0)
317         bufsiz = PATH_MAX;
319     buf = malloc(bufsiz);
320     if (buf == NULL) {
321         perror("malloc");
322         exit(EXIT_FAILURE);
323     }
325     nbytes = readlink(argv[1], buf, bufsiz);
326     if (nbytes == \-1) {
327         perror("readlink");
328         exit(EXIT_FAILURE);
329     }
331     /* Print only \(aqnbytes\(aq of \(aqbuf\(aq, as it doesn't contain a terminating
332        null byte (\(aq\e0\(aq). */
333     printf("\(aq%s\(aq points to \(aq%.*s\(aq\en", argv[1], (int) nbytes, buf);
335     /* If the return value was equal to the buffer size, then the
336        the link target was larger than expected (perhaps because the
337        target was changed between the call to lstat() and the call to
338        readlink()). Warn the user that the returned target may have
339        been truncated. */
341     if (nbytes == bufsiz)
342         printf("(Returned buffer may have been truncated)\en");
344     free(buf);
345     exit(EXIT_SUCCESS);
348 .SH SEE ALSO
349 .BR readlink (1),
350 .BR lstat (2),
351 .BR stat (2),
352 .BR symlink (2),
353 .BR realpath (3),
354 .BR path_resolution (7),
355 .BR symlink (7)