OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Crashpad Authors. All rights reserved. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include "test/paths.h" |
| 16 |
| 17 #include <limits.h> |
| 18 #include <unistd.h> |
| 19 |
| 20 #include <algorithm> |
| 21 #include <string> |
| 22 |
| 23 #include "base/logging.h" |
| 24 #include "util/misc/implicit_cast.h" |
| 25 |
| 26 namespace crashpad { |
| 27 namespace test { |
| 28 |
| 29 // static |
| 30 base::FilePath Paths::Executable() { |
| 31 // Linux does not provide a straightforward way to size the buffer before |
| 32 // calling readlink(). Normally, the st_size field returned by lstat() could |
| 33 // be used, but this is usually zero for things in /proc. |
| 34 // |
| 35 // The /proc filesystem does not provide any way to read “exe” links for |
| 36 // pathnames longer than a page. See linux-4.4.27/fs/proc/base.c |
| 37 // do_proc_readlink(), which allocates a single page to receive the path |
| 38 // string. Coincidentally, the page size and PATH_MAX are normally the same |
| 39 // value, although neither is strictly a limit on the length of a pathname. |
| 40 // |
| 41 // On Android, the smaller of the page size and PATH_MAX actually does serve |
| 42 // as an effective limit on the length of an executable’s pathname. See |
| 43 // Android 7.0.0 bionic/linker/linker.cpp get_executable_path(), which aborts |
| 44 // via __libc_fatal() if the “exe” link can’t be read into a PATH_MAX-sized |
| 45 // buffer. |
| 46 std::string exe_path(std::max(implicit_cast<size_t>(sysconf(_SC_PAGESIZE)), |
| 47 implicit_cast<size_t>(PATH_MAX)), |
| 48 std::string::value_type()); |
| 49 ssize_t exe_path_len = |
| 50 readlink("/proc/self/exe", &exe_path[0], exe_path.size()); |
| 51 if (exe_path_len < 0) { |
| 52 PLOG(FATAL) << "readlink"; |
| 53 } else if (static_cast<size_t>(exe_path_len) >= exe_path.size()) { |
| 54 LOG(FATAL) << "readlink"; |
| 55 } |
| 56 |
| 57 exe_path.resize(exe_path_len); |
| 58 return base::FilePath(exe_path); |
| 59 } |
| 60 |
| 61 } // namespace test |
| 62 } // namespace crashpad |
OLD | NEW |