"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 2 | /* |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 3 | * xreadlink.c - safe implementation of readlink. |
Denys Vlasenko | c504938 | 2014-03-16 20:53:40 +0100 | [diff] [blame] | 4 | * Returns a NULL on failure. |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame] | 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 10 | |
Denys Vlasenko | c504938 | 2014-03-16 20:53:40 +0100 | [diff] [blame] | 11 | /* Some systems (eg Hurd) do not have MAXSYMLINKS definition, |
Michael Tokarev | 9f4f601 | 2013-12-09 14:45:01 +0400 | [diff] [blame] | 12 | * set it to some reasonable value if it isn't defined */ |
| 13 | #ifndef MAXSYMLINKS |
| 14 | # define MAXSYMLINKS 20 |
| 15 | #endif |
| 16 | |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 17 | /* |
| 18 | * NOTE: This function returns a malloced char* that you will have to free |
Bernhard Reutner-Fischer | 9bd8d0c | 2007-11-08 21:11:43 +0000 | [diff] [blame] | 19 | * yourself. |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 20 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 21 | char* FAST_FUNC xmalloc_readlink(const char *path) |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 22 | { |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 23 | enum { GROWBY = 80 }; /* how large we will grow strings by */ |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 24 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 25 | char *buf = NULL; |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 26 | int bufsize = 0, readsize = 0; |
| 27 | |
| 28 | do { |
Denis Vlasenko | b68979a | 2007-11-02 23:31:10 +0000 | [diff] [blame] | 29 | bufsize += GROWBY; |
| 30 | buf = xrealloc(buf, bufsize); |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 31 | readsize = readlink(path, buf, bufsize); |
Eric Andersen | 28355a3 | 2001-05-07 17:48:28 +0000 | [diff] [blame] | 32 | if (readsize == -1) { |
Glenn L McGrath | 18bbd9b | 2004-08-11 03:50:30 +0000 | [diff] [blame] | 33 | free(buf); |
| 34 | return NULL; |
Eric Andersen | 28355a3 | 2001-05-07 17:48:28 +0000 | [diff] [blame] | 35 | } |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 36 | } while (bufsize < readsize + 1); |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 37 | |
| 38 | buf[readsize] = '\0'; |
| 39 | |
| 40 | return buf; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 41 | } |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 42 | |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 43 | /* |
Bernhard Reutner-Fischer | 9bd8d0c | 2007-11-08 21:11:43 +0000 | [diff] [blame] | 44 | * 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 Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 50 | * those at the tail. |
Bernhard Reutner-Fischer | 9bd8d0c | 2007-11-08 21:11:43 +0000 | [diff] [blame] | 51 | * A malloced char* is returned, which must be freed by the caller. |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 52 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 53 | char* FAST_FUNC xmalloc_follow_symlinks(const char *path) |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 54 | { |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 55 | char *buf; |
| 56 | char *lpc; |
| 57 | char *linkpath; |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 58 | int bufsize; |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 59 | int looping = MAXSYMLINKS + 1; |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 60 | |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 61 | buf = xstrdup(path); |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 62 | goto jump_in; |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 64 | while (1) { |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 65 | 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-Fischer | 9bd8d0c | 2007-11-08 21:11:43 +0000 | [diff] [blame] | 70 | goto free_buf_ret_null; |
| 71 | } |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 72 | |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 73 | if (!--looping) { |
| 74 | free(linkpath); |
Denis Vlasenko | d031b20 | 2007-11-10 01:28:19 +0000 | [diff] [blame] | 75 | free_buf_ret_null: |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 76 | free(buf); |
| 77 | return NULL; |
| 78 | } |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 79 | |
| 80 | if (*linkpath != '/') { |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 81 | bufsize += strlen(linkpath); |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 82 | buf = xrealloc(buf, bufsize); |
| 83 | lpc = bb_get_last_path_component_strip(buf); |
| 84 | strcpy(lpc, linkpath); |
| 85 | free(linkpath); |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 86 | } else { |
| 87 | free(buf); |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 88 | buf = linkpath; |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 89 | jump_in: |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 90 | bufsize = strlen(buf) + 1; |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 95 | char* FAST_FUNC xmalloc_readlink_or_warn(const char *path) |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 96 | { |
| 97 | char *buf = xmalloc_readlink(path); |
| 98 | if (!buf) { |
| 99 | /* EINVAL => "file: Invalid argument" => puzzled user */ |
Denis Vlasenko | 3a014b8 | 2009-03-21 19:11:23 +0000 | [diff] [blame] | 100 | 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 Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 105 | } |
| 106 | return buf; |
| 107 | } |
| 108 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 109 | char* FAST_FUNC xmalloc_realpath(const char *path) |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 110 | { |
Denys Vlasenko | c504938 | 2014-03-16 20:53:40 +0100 | [diff] [blame] | 111 | /* NB: uclibc also defines __GLIBC__ |
| 112 | * Therefore the test "if glibc, or uclibc >= 0.9.31" looks a bit weird: |
| 113 | */ |
| 114 | #if defined(__GLIBC__) && \ |
| 115 | (!defined(__UCLIBC__) || UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 31)) |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 116 | /* glibc provides a non-standard extension */ |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 117 | /* new: POSIX.1-2008 specifies this behavior as well */ |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 118 | return realpath(path, NULL); |
| 119 | #else |
| 120 | char buf[PATH_MAX+1]; |
| 121 | |
Jérémie Koenig | fbedacf | 2010-03-26 19:08:53 +0100 | [diff] [blame] | 122 | /* on error returns NULL (xstrdup(NULL) == NULL) */ |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 123 | return xstrdup(realpath(path, buf)); |
| 124 | #endif |
| 125 | } |