static
| 26 | |
| 27 | // static |
| 28 | bool Paths::Executable(base::FilePath* path) { |
| 29 | // Linux does not provide a straightforward way to size the buffer before |
| 30 | // calling readlink(). Normally, the st_size field returned by lstat() could |
| 31 | // be used, but this is usually zero for things in /proc. |
| 32 | // |
| 33 | // The /proc filesystem does not provide any way to read “exe” links for |
| 34 | // pathnames longer than a page. See linux-4.9.20/fs/proc/base.c |
| 35 | // do_proc_readlink(), which allocates a single page to receive the path |
| 36 | // string. Coincidentally, the page size and PATH_MAX are normally the same |
| 37 | // value, although neither is strictly a limit on the length of a pathname. |
| 38 | // |
| 39 | // On Android, the smaller of the page size and PATH_MAX actually does serve |
| 40 | // as an effective limit on the length of an executable’s pathname. See |
| 41 | // Android 7.1.1 bionic/linker/linker.cpp get_executable_path(), which aborts |
| 42 | // via __libc_fatal() if the “exe” link can’t be read into a PATH_MAX-sized |
| 43 | // buffer. |
| 44 | std::string exe_path(std::max(getpagesize(), PATH_MAX), |
| 45 | std::string::value_type()); |
| 46 | ssize_t exe_path_len = |
| 47 | readlink("/proc/self/exe", &exe_path[0], exe_path.size()); |
| 48 | if (exe_path_len < 0) { |
| 49 | PLOG(ERROR) << "readlink"; |
| 50 | return false; |
| 51 | } else if (static_cast<size_t>(exe_path_len) >= exe_path.size()) { |
| 52 | LOG(ERROR) << "readlink"; |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | exe_path.resize(exe_path_len); |
| 57 | *path = base::FilePath(exe_path); |
| 58 | return true; |
| 59 | } |
| 60 | |
| 61 | } // namespace crashpad |