blob: 2682f69758ad43365b3e4ae01f081b93347fa3bd [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mark Whitley8a633262001-04-30 18:17:00 +00002/*
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00003 * xreadlink.c - safe implementation of readlink.
Denys Vlasenkoc5049382014-03-16 20:53:40 +01004 * Returns a NULL on failure.
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00005 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2, see file LICENSE in this source tree.
Mark Whitley8a633262001-04-30 18:17:00 +00007 */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +00008#include "libbb.h"
Mark Whitley8a633262001-04-30 18:17:00 +00009
Denys Vlasenkoc5049382014-03-16 20:53:40 +010010/* Some systems (eg Hurd) do not have MAXSYMLINKS definition,
Michael Tokarev9f4f6012013-12-09 14:45:01 +040011 * set it to some reasonable value if it isn't defined */
12#ifndef MAXSYMLINKS
13# define MAXSYMLINKS 20
14#endif
15
Mark Whitley8a633262001-04-30 18:17:00 +000016/*
17 * NOTE: This function returns a malloced char* that you will have to free
Bernhard Reutner-Fischer9bd8d0c2007-11-08 21:11:43 +000018 * yourself.
Mark Whitley8a633262001-04-30 18:17:00 +000019 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000020char* FAST_FUNC xmalloc_readlink(const char *path)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000021{
Rob Landleybc68cd12006-03-10 19:22:06 +000022 enum { GROWBY = 80 }; /* how large we will grow strings by */
Mark Whitley8a633262001-04-30 18:17:00 +000023
Eric Andersenc7bda1c2004-03-15 08:29:22 +000024 char *buf = NULL;
Mark Whitley8a633262001-04-30 18:17:00 +000025 int bufsize = 0, readsize = 0;
26
27 do {
Denis Vlasenkob68979a2007-11-02 23:31:10 +000028 bufsize += GROWBY;
29 buf = xrealloc(buf, bufsize);
Denis Vlasenkobeffd432007-09-05 11:30:34 +000030 readsize = readlink(path, buf, bufsize);
Eric Andersen28355a32001-05-07 17:48:28 +000031 if (readsize == -1) {
Glenn L McGrath18bbd9b2004-08-11 03:50:30 +000032 free(buf);
33 return NULL;
Eric Andersen28355a32001-05-07 17:48:28 +000034 }
Denis Vlasenkobeffd432007-09-05 11:30:34 +000035 } while (bufsize < readsize + 1);
Mark Whitley8a633262001-04-30 18:17:00 +000036
37 buf[readsize] = '\0';
38
39 return buf;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000040}
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000041
Paul Fox459a2ba2007-11-08 01:11:41 +000042/*
Bernhard Reutner-Fischer9bd8d0c2007-11-08 21:11:43 +000043 * This routine is not the same as realpath(), which
44 * canonicalizes the given path completely. This routine only
45 * follows trailing symlinks until a real file is reached and
46 * returns its name. If the path ends in a dangling link or if
47 * the target doesn't exist, the path is returned in any case.
48 * Intermediate symlinks in the path are not expanded -- only
Paul Fox599bbfb2007-11-08 20:00:36 +000049 * those at the tail.
Bernhard Reutner-Fischer9bd8d0c2007-11-08 21:11:43 +000050 * A malloced char* is returned, which must be freed by the caller.
Paul Fox459a2ba2007-11-08 01:11:41 +000051 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000052char* FAST_FUNC xmalloc_follow_symlinks(const char *path)
Paul Fox459a2ba2007-11-08 01:11:41 +000053{
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000054 char *buf;
55 char *lpc;
56 char *linkpath;
Paul Fox459a2ba2007-11-08 01:11:41 +000057 int bufsize;
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000058 int looping = MAXSYMLINKS + 1;
Paul Fox459a2ba2007-11-08 01:11:41 +000059
Paul Fox599bbfb2007-11-08 20:00:36 +000060 buf = xstrdup(path);
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000061 goto jump_in;
Paul Fox459a2ba2007-11-08 01:11:41 +000062
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000063 while (1) {
Paul Fox599bbfb2007-11-08 20:00:36 +000064 linkpath = xmalloc_readlink(buf);
65 if (!linkpath) {
66 /* not a symlink, or doesn't exist */
67 if (errno == EINVAL || errno == ENOENT)
68 return buf;
Bernhard Reutner-Fischer9bd8d0c2007-11-08 21:11:43 +000069 goto free_buf_ret_null;
70 }
Paul Fox599bbfb2007-11-08 20:00:36 +000071
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000072 if (!--looping) {
73 free(linkpath);
Denis Vlasenkod031b202007-11-10 01:28:19 +000074 free_buf_ret_null:
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000075 free(buf);
76 return NULL;
77 }
Paul Fox599bbfb2007-11-08 20:00:36 +000078
79 if (*linkpath != '/') {
Paul Fox459a2ba2007-11-08 01:11:41 +000080 bufsize += strlen(linkpath);
Paul Fox459a2ba2007-11-08 01:11:41 +000081 buf = xrealloc(buf, bufsize);
82 lpc = bb_get_last_path_component_strip(buf);
83 strcpy(lpc, linkpath);
84 free(linkpath);
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000085 } else {
86 free(buf);
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000087 buf = linkpath;
Paul Fox599bbfb2007-11-08 20:00:36 +000088 jump_in:
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000089 bufsize = strlen(buf) + 1;
Paul Fox459a2ba2007-11-08 01:11:41 +000090 }
91 }
Paul Fox459a2ba2007-11-08 01:11:41 +000092}
93
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000094char* FAST_FUNC xmalloc_readlink_or_warn(const char *path)
Denis Vlasenkobeffd432007-09-05 11:30:34 +000095{
96 char *buf = xmalloc_readlink(path);
97 if (!buf) {
98 /* EINVAL => "file: Invalid argument" => puzzled user */
Denis Vlasenko3a014b82009-03-21 19:11:23 +000099 const char *errmsg = "not a symlink";
100 int err = errno;
101 if (err != EINVAL)
102 errmsg = strerror(err);
103 bb_error_msg("%s: cannot read link: %s", path, errmsg);
Denis Vlasenkobeffd432007-09-05 11:30:34 +0000104 }
105 return buf;
106}
107
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000108char* FAST_FUNC xmalloc_realpath(const char *path)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000109{
Denys Vlasenkoc5049382014-03-16 20:53:40 +0100110/* NB: uclibc also defines __GLIBC__
111 * Therefore the test "if glibc, or uclibc >= 0.9.31" looks a bit weird:
112 */
113#if defined(__GLIBC__) && \
114 (!defined(__UCLIBC__) || UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 31))
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000115 /* glibc provides a non-standard extension */
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100116 /* new: POSIX.1-2008 specifies this behavior as well */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000117 return realpath(path, NULL);
118#else
119 char buf[PATH_MAX+1];
120
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100121 /* on error returns NULL (xstrdup(NULL) == NULL) */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000122 return xstrdup(realpath(path, buf));
123#endif
124}
Denys Vlasenko74716212018-05-24 17:29:14 +0200125
Ron Yorston94eb1c42021-07-31 13:51:57 +0100126char* FAST_FUNC xmalloc_realpath_coreutils(char *path)
Denys Vlasenko74716212018-05-24 17:29:14 +0200127{
128 char *buf;
129
130 errno = 0;
131 buf = xmalloc_realpath(path);
132 /*
133 * There is one case when "readlink -f" and
134 * "realpath" from coreutils succeed,
135 * even though file does not exist, such as:
136 * /tmp/file_does_not_exist
137 * (the directory must exist).
138 */
139 if (!buf && errno == ENOENT) {
Ron Yorston94eb1c42021-07-31 13:51:57 +0100140 char *target, c, *last_slash;
141 size_t i;
142
143 target = xmalloc_readlink(path);
144 if (target) {
145 /*
146 * $ ln -s /bin/qwe symlink # note: /bin is a link to /usr/bin
147 * $ readlink -f symlink
148 * /usr/bin/qwe
149 * $ realpath symlink
150 * /usr/bin/qwe
151 */
152 if (target[0] != '/') {
Denys Vlasenko070aa612019-06-13 17:08:29 +0200153 /*
154 * $ ln -s target_does_not_exist symlink
155 * $ readlink -f symlink
156 * /CURDIR/target_does_not_exist
157 * $ realpath symlink
158 * /CURDIR/target_does_not_exist
159 */
Ron Yorston94eb1c42021-07-31 13:51:57 +0100160 char *cwd = xrealloc_getcwd_or_warn(NULL);
161 char *tmp = concat_path_file(cwd, target);
Denys Vlasenko070aa612019-06-13 17:08:29 +0200162 free(cwd);
Denys Vlasenko25a871f2019-06-25 18:51:00 +0200163 free(target);
Ron Yorston94eb1c42021-07-31 13:51:57 +0100164 target = tmp;
165 }
166 buf = xmalloc_realpath_coreutils(target);
167 free(target);
168 return buf;
169 }
170
171 /* ignore leading and trailing slashes */
172 while (path[0] == '/' && path[1] == '/')
173 ++path;
174 i = strlen(path) - 1;
175 while (i > 0 && path[i] == '/')
176 i--;
177 c = path[i + 1];
178 path[i + 1] = '\0';
179
180 last_slash = strrchr(path, '/');
181 if (last_slash == path)
182 buf = xstrdup(path);
183 else if (last_slash) {
184 *last_slash = '\0';
185 buf = xmalloc_realpath(path);
186 *last_slash++ = '/';
187 if (buf) {
188 unsigned len = strlen(buf);
189 buf = xrealloc(buf, len + strlen(last_slash) + 2);
190 buf[len++] = '/';
191 strcpy(buf + len, last_slash);
Denys Vlasenko070aa612019-06-13 17:08:29 +0200192 }
Denys Vlasenko74716212018-05-24 17:29:14 +0200193 }
Ron Yorston94eb1c42021-07-31 13:51:57 +0100194 path[i + 1] = c;
Denys Vlasenko74716212018-05-24 17:29:14 +0200195 }
196
197 return buf;
198}