1READLINK(2)                Linux Programmer's Manual               READLINK(2)
2
3
4

NAME

6       readlink, readlinkat - read value of a symbolic link
7

SYNOPSIS

9       #include <unistd.h>
10
11       ssize_t readlink(const char *restrict pathname, char *restrict buf,
12                        size_t bufsiz);
13
14       #include <fcntl.h>            /* Definition of AT_* constants */
15       #include <unistd.h>
16
17       ssize_t readlinkat(int dirfd, const char *restrict pathname,
18                        char *restrict buf, size_t bufsiz);
19
20   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
21
22       readlink():
23           _XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 200112L
24               || /* Glibc <= 2.19: */ _BSD_SOURCE
25
26       readlinkat():
27           Since glibc 2.10:
28               _POSIX_C_SOURCE >= 200809L
29           Before glibc 2.10:
30               _ATFILE_SOURCE
31

DESCRIPTION

33       readlink()  places  the  contents  of the symbolic link pathname in the
34       buffer buf, which has size bufsiz.  readlink() does not append a termi‐
35       nating  null byte to buf.  It will (silently) truncate the contents (to
36       a length of bufsiz characters), in case the buffer is too small to hold
37       all of the contents.
38
39   readlinkat()
40       The  readlinkat() system call operates in exactly the same way as read‐
41       link(), except for the differences described here.
42
43       If the pathname given in pathname is relative, then it  is  interpreted
44       relative  to  the  directory  referred  to by the file descriptor dirfd
45       (rather than relative to the current working directory of  the  calling
46       process, as is done by readlink() for a relative pathname).
47
48       If  pathname  is relative and dirfd is the special value AT_FDCWD, then
49       pathname is interpreted relative to the current  working  directory  of
50       the calling process (like readlink()).
51
52       If pathname is absolute, then dirfd is ignored.
53
54       Since  Linux 2.6.39, pathname can be an empty string, in which case the
55       call operates on the symbolic link referred to by dirfd  (which  should
56       have been obtained using open(2) with the O_PATH and O_NOFOLLOW flags).
57
58       See openat(2) for an explanation of the need for readlinkat().
59

RETURN VALUE

61       On  success, these calls return the number of bytes placed in buf.  (If
62       the returned value equals bufsiz, then truncation may  have  occurred.)
63       On error, -1 is returned and errno is set to indicate the error.
64

ERRORS

66       EACCES Search  permission is denied for a component of the path prefix.
67              (See also path_resolution(7).)
68
69       EFAULT buf extends outside the process's allocated address space.
70
71       EINVAL bufsiz is not positive.
72
73       EINVAL The named file (i.e., the final filename component of  pathname)
74              is not a symbolic link.
75
76       EIO    An I/O error occurred while reading from the filesystem.
77
78       ELOOP  Too  many  symbolic  links  were  encountered in translating the
79              pathname.
80
81       ENAMETOOLONG
82              A pathname, or a component of a pathname, was too long.
83
84       ENOENT The named file does not exist.
85
86       ENOMEM Insufficient kernel memory was available.
87
88       ENOTDIR
89              A component of the path prefix is not a directory.
90
91       The following additional errors can occur for readlinkat():
92
93       EBADF  dirfd is not a valid file descriptor.
94
95       ENOTDIR
96              pathname is relative and dirfd is a file descriptor referring to
97              a file other than a directory.
98

VERSIONS

100       readlinkat()  was  added to Linux in kernel 2.6.16; library support was
101       added to glibc in version 2.4.
102

CONFORMING TO

104       readlink(): 4.4BSD (readlink() first appeared in 4.2BSD), POSIX.1-2001,
105       POSIX.1-2008.
106
107       readlinkat(): POSIX.1-2008.
108

NOTES

110       In  versions of glibc up to and including glibc 2.4, the return type of
111       readlink() was declared as int.  Nowadays, the return type is  declared
112       as ssize_t, as (newly) required in POSIX.1-2001.
113
114       Using  a  statically sized buffer might not provide enough room for the
115       symbolic link contents.  The required size for the buffer  can  be  ob‐
116       tained  from  the  stat.st_size value returned by a call to lstat(2) on
117       the link.  However, the number of bytes written by readlink() and read‐
118       linkat()  should  be checked to make sure that the size of the symbolic
119       link did not increase between the calls.   Dynamically  allocating  the
120       buffer  for  readlink() and readlinkat() also addresses a common porta‐
121       bility problem when using PATH_MAX for the buffer size,  as  this  con‐
122       stant  is not guaranteed to be defined per POSIX if the system does not
123       have such limit.
124
125   Glibc notes
126       On older kernels where readlinkat() is unavailable, the  glibc  wrapper
127       function falls back to the use of readlink().  When pathname is a rela‐
128       tive pathname, glibc constructs a pathname based on the  symbolic  link
129       in /proc/self/fd that corresponds to the dirfd argument.
130

EXAMPLES

132       The following program allocates the buffer needed by readlink() dynami‐
133       cally from the information provided by lstat(2), falling back to a buf‐
134       fer of size PATH_MAX in cases where lstat(2) reports a size of zero.
135
136       #include <sys/types.h>
137       #include <sys/stat.h>
138       #include <limits.h>
139       #include <stdio.h>
140       #include <stdlib.h>
141       #include <unistd.h>
142
143       int
144       main(int argc, char *argv[])
145       {
146           struct stat sb;
147           char *buf;
148           ssize_t nbytes, bufsiz;
149
150           if (argc != 2) {
151               fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
152               exit(EXIT_FAILURE);
153           }
154
155           if (lstat(argv[1], &sb) == -1) {
156               perror("lstat");
157               exit(EXIT_FAILURE);
158           }
159
160           /* Add one to the link size, so that we can determine whether
161              the buffer returned by readlink() was truncated. */
162
163           bufsiz = sb.st_size + 1;
164
165           /* Some magic symlinks under (for example) /proc and /sys
166              report 'st_size' as zero. In that case, take PATH_MAX as
167              a "good enough" estimate. */
168
169           if (sb.st_size == 0)
170               bufsiz = PATH_MAX;
171
172           buf = malloc(bufsiz);
173           if (buf == NULL) {
174               perror("malloc");
175               exit(EXIT_FAILURE);
176           }
177
178           nbytes = readlink(argv[1], buf, bufsiz);
179           if (nbytes == -1) {
180               perror("readlink");
181               exit(EXIT_FAILURE);
182           }
183
184           /* Print only 'nbytes' of 'buf', as it doesn't contain a terminating
185              null byte ('\0'). */
186           printf("'%s' points to '%.*s'\n", argv[1], (int) nbytes, buf);
187
188           /* If the return value was equal to the buffer size, then the
189              the link target was larger than expected (perhaps because the
190              target was changed between the call to lstat() and the call to
191              readlink()). Warn the user that the returned target may have
192              been truncated. */
193
194           if (nbytes == bufsiz)
195               printf("(Returned buffer may have been truncated)\n");
196
197           free(buf);
198           exit(EXIT_SUCCESS);
199       }
200

SEE ALSO

202       readlink(1),  lstat(2),  stat(2), symlink(2), realpath(3), path_resolu‐
203       tion(7), symlink(7)
204

COLOPHON

206       This page is part of release 5.12 of the Linux  man-pages  project.   A
207       description  of  the project, information about reporting bugs, and the
208       latest    version    of    this    page,    can     be     found     at
209       https://www.kernel.org/doc/man-pages/.
210
211
212
213Linux                             2021-03-22                       READLINK(2)
Impressum