blob: bb63da08c86c1c2dad6d90d8da6e974db8fde663 [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.
4 * Returns a NULL on failure...
5 *
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 */
8
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +00009#include "libbb.h"
Mark Whitley8a633262001-04-30 18:17:00 +000010
maxwen27116ba2015-08-14 21:41:28 +020011/* some systems (eg Hurd) does not have MAXSYMLINKS definition,
12 * set it to some reasonable value if it isn't defined */
13#ifndef MAXSYMLINKS
14# define MAXSYMLINKS 20
15#endif
16
Mark Whitley8a633262001-04-30 18:17:00 +000017/*
18 * NOTE: This function returns a malloced char* that you will have to free
Bernhard Reutner-Fischer9bd8d0c2007-11-08 21:11:43 +000019 * yourself.
Mark Whitley8a633262001-04-30 18:17:00 +000020 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000021char* FAST_FUNC xmalloc_readlink(const char *path)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000022{
Rob Landleybc68cd12006-03-10 19:22:06 +000023 enum { GROWBY = 80 }; /* how large we will grow strings by */
Mark Whitley8a633262001-04-30 18:17:00 +000024
Eric Andersenc7bda1c2004-03-15 08:29:22 +000025 char *buf = NULL;
Mark Whitley8a633262001-04-30 18:17:00 +000026 int bufsize = 0, readsize = 0;
27
28 do {
Denis Vlasenkob68979a2007-11-02 23:31:10 +000029 bufsize += GROWBY;
30 buf = xrealloc(buf, bufsize);
Denis Vlasenkobeffd432007-09-05 11:30:34 +000031 readsize = readlink(path, buf, bufsize);
Eric Andersen28355a32001-05-07 17:48:28 +000032 if (readsize == -1) {
Glenn L McGrath18bbd9b2004-08-11 03:50:30 +000033 free(buf);
34 return NULL;
Eric Andersen28355a32001-05-07 17:48:28 +000035 }
Denis Vlasenkobeffd432007-09-05 11:30:34 +000036 } while (bufsize < readsize + 1);
Mark Whitley8a633262001-04-30 18:17:00 +000037
38 buf[readsize] = '\0';
39
40 return buf;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000041}
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000042
Paul Fox459a2ba2007-11-08 01:11:41 +000043/*
Bernhard Reutner-Fischer9bd8d0c2007-11-08 21:11:43 +000044 * This routine is not the same as realpath(), which
45 * canonicalizes the given path completely. This routine only
46 * follows trailing symlinks until a real file is reached and
47 * returns its name. If the path ends in a dangling link or if
48 * the target doesn't exist, the path is returned in any case.
49 * Intermediate symlinks in the path are not expanded -- only
Paul Fox599bbfb2007-11-08 20:00:36 +000050 * those at the tail.
Bernhard Reutner-Fischer9bd8d0c2007-11-08 21:11:43 +000051 * A malloced char* is returned, which must be freed by the caller.
Paul Fox459a2ba2007-11-08 01:11:41 +000052 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000053char* FAST_FUNC xmalloc_follow_symlinks(const char *path)
Paul Fox459a2ba2007-11-08 01:11:41 +000054{
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000055 char *buf;
56 char *lpc;
57 char *linkpath;
Paul Fox459a2ba2007-11-08 01:11:41 +000058 int bufsize;
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000059 int looping = MAXSYMLINKS + 1;
Paul Fox459a2ba2007-11-08 01:11:41 +000060
Paul Fox599bbfb2007-11-08 20:00:36 +000061 buf = xstrdup(path);
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000062 goto jump_in;
Paul Fox459a2ba2007-11-08 01:11:41 +000063
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000064 while (1) {
Paul Fox599bbfb2007-11-08 20:00:36 +000065 linkpath = xmalloc_readlink(buf);
66 if (!linkpath) {
67 /* not a symlink, or doesn't exist */
68 if (errno == EINVAL || errno == ENOENT)
69 return buf;
Bernhard Reutner-Fischer9bd8d0c2007-11-08 21:11:43 +000070 goto free_buf_ret_null;
71 }
Paul Fox599bbfb2007-11-08 20:00:36 +000072
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000073 if (!--looping) {
74 free(linkpath);
Denis Vlasenkod031b202007-11-10 01:28:19 +000075 free_buf_ret_null:
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000076 free(buf);
77 return NULL;
78 }
Paul Fox599bbfb2007-11-08 20:00:36 +000079
80 if (*linkpath != '/') {
Paul Fox459a2ba2007-11-08 01:11:41 +000081 bufsize += strlen(linkpath);
Paul Fox459a2ba2007-11-08 01:11:41 +000082 buf = xrealloc(buf, bufsize);
83 lpc = bb_get_last_path_component_strip(buf);
84 strcpy(lpc, linkpath);
85 free(linkpath);
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000086 } else {
87 free(buf);
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000088 buf = linkpath;
Paul Fox599bbfb2007-11-08 20:00:36 +000089 jump_in:
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000090 bufsize = strlen(buf) + 1;
Paul Fox459a2ba2007-11-08 01:11:41 +000091 }
92 }
Paul Fox459a2ba2007-11-08 01:11:41 +000093}
94
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000095char* FAST_FUNC xmalloc_readlink_or_warn(const char *path)
Denis Vlasenkobeffd432007-09-05 11:30:34 +000096{
97 char *buf = xmalloc_readlink(path);
98 if (!buf) {
99 /* EINVAL => "file: Invalid argument" => puzzled user */
Denis Vlasenko3a014b82009-03-21 19:11:23 +0000100 const char *errmsg = "not a symlink";
101 int err = errno;
102 if (err != EINVAL)
103 errmsg = strerror(err);
104 bb_error_msg("%s: cannot read link: %s", path, errmsg);
Denis Vlasenkobeffd432007-09-05 11:30:34 +0000105 }
106 return buf;
107}
108
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000109char* FAST_FUNC xmalloc_realpath(const char *path)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000110{
maxwen27116ba2015-08-14 21:41:28 +0200111#if defined(__GLIBC__) || \
112 (defined(__UCLIBC__) && UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 31))
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000113 /* glibc provides a non-standard extension */
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100114 /* new: POSIX.1-2008 specifies this behavior as well */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000115 return realpath(path, NULL);
116#else
117 char buf[PATH_MAX+1];
118
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100119 /* on error returns NULL (xstrdup(NULL) == NULL) */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000120 return xstrdup(realpath(path, buf));
121#endif
122}