Elliott Hughes | 22fb267 | 2019-11-01 08:07:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <stdlib.h> |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <string.h> |
| 34 | #include <sys/stat.h> |
| 35 | |
| 36 | #include "private/FdPath.h" |
| 37 | #include "private/ScopedFd.h" |
| 38 | |
| 39 | // This function needs a 4KiB (PATH_MAX) buffer. |
| 40 | // The alternative is to heap allocate and then trim, but that's 2x the code. |
| 41 | // (Remember that readlink(2) won't tell you the needed size, so the multi-pass |
| 42 | // algorithm isn't even an option unless you want to just guess, in which case |
| 43 | // you're back needing to trim again.) |
| 44 | #pragma GCC diagnostic ignored "-Wframe-larger-than=" |
| 45 | |
| 46 | char* realpath(const char* path, char* result) { |
| 47 | // Weird special case. |
| 48 | if (!path) { |
| 49 | errno = EINVAL; |
| 50 | return nullptr; |
| 51 | } |
| 52 | |
| 53 | // Get an O_PATH fd, and... |
| 54 | ScopedFd fd(open(path, O_PATH | O_CLOEXEC)); |
| 55 | if (fd.get() == -1) return nullptr; |
| 56 | |
| 57 | // (...remember the device/inode that we're talking about and...) |
| 58 | struct stat sb; |
| 59 | if (fstat(fd.get(), &sb) == -1) return nullptr; |
| 60 | dev_t st_dev = sb.st_dev; |
| 61 | ino_t st_ino = sb.st_ino; |
| 62 | |
| 63 | // ...ask the kernel to do the hard work for us. |
| 64 | FdPath fd_path(fd.get()); |
| 65 | char dst[PATH_MAX]; |
| 66 | ssize_t l = readlink(fd_path.c_str(), dst, sizeof(dst) - 1); |
| 67 | if (l == -1) return nullptr; |
| 68 | dst[l] = '\0'; |
| 69 | |
| 70 | // What if the file was removed in the meantime? readlink(2) will have |
| 71 | // returned "/a/b/c (deleted)", and we want to return ENOENT instead. |
| 72 | if (stat(dst, &sb) == -1 || st_dev != sb.st_dev || st_ino != sb.st_ino) { |
| 73 | errno = ENOENT; |
| 74 | return nullptr; |
| 75 | } |
| 76 | |
| 77 | return result ? strcpy(result, dst) : strdup(dst); |
| 78 | } |