Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 5 | // Defines base::PathProviderPosix, default path provider on POSIX OSes that |
| 6 | // don't have their own base_paths_OS.cc implementation (i.e. all but Mac and |
| 7 | // Android). |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 8 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 9 | #include "base/base_paths.h" |
| 10 | |
avi | 51ba3e69 | 2015-12-26 17:30:50 | [diff] [blame] | 11 | #include <limits.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 12 | #include <stddef.h> |
avi | 51ba3e69 | 2015-12-26 17:30:50 | [diff] [blame] | 13 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 14 | #include <memory> |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 15 | #include <ostream> |
| 16 | #include <string> |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 17 | |
[email protected] | 76b90d31 | 2010-08-03 03:00:50 | [diff] [blame] | 18 | #include "base/environment.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 19 | #include "base/files/file_path.h" |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 20 | #include "base/files/file_util.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 21 | #include "base/logging.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 22 | #include "base/nix/xdg_util.h" |
Hans Wennborg | afeb390 | 2020-06-17 14:42:29 | [diff] [blame] | 23 | #include "base/notreached.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 24 | #include "base/path_service.h" |
Avi Drissman | 837d106b | 2023-09-12 14:51:20 | [diff] [blame] | 25 | #include "base/posix/sysctl.h" |
[email protected] | dd4b5126 | 2013-07-25 21:38:23 | [diff] [blame] | 26 | #include "base/process/process_metrics.h" |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 27 | #include "build/build_config.h" |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 28 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 29 | #if BUILDFLAG(IS_FREEBSD) |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 30 | #include <sys/param.h> |
| 31 | #include <sys/sysctl.h> |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 32 | #elif BUILDFLAG(IS_SOLARIS) || BUILDFLAG(IS_AIX) |
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 33 | #include <stdlib.h> |
[email protected] | 2edc286 | 2011-04-04 18:04:37 | [diff] [blame] | 34 | #endif |
| 35 | |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 36 | namespace base { |
| 37 | |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 38 | bool PathProviderPosix(int key, FilePath* result) { |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 39 | switch (key) { |
thestig | bf8ab1b | 2016-11-19 04:15:42 | [diff] [blame] | 40 | case FILE_EXE: |
| 41 | case FILE_MODULE: { // TODO(evanm): is this correct? |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 42 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
[email protected] | 723571a | 2010-12-03 17:37:54 | [diff] [blame] | 43 | FilePath bin_dir; |
[email protected] | b264eab | 2013-11-27 23:22:08 | [diff] [blame] | 44 | if (!ReadSymbolicLink(FilePath(kProcSelfExe), &bin_dir)) { |
Peter Boström | de57333 | 2024-08-26 20:42:45 | [diff] [blame] | 45 | NOTREACHED() << "Unable to resolve " << kProcSelfExe << "."; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 46 | } |
[email protected] | 723571a | 2010-12-03 17:37:54 | [diff] [blame] | 47 | *result = bin_dir; |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 48 | return true; |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 49 | #elif BUILDFLAG(IS_FREEBSD) |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 50 | int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; |
Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 51 | std::optional<std::string> bin_dir = StringSysctl(name, std::size(name)); |
Avi Drissman | 837d106b | 2023-09-12 14:51:20 | [diff] [blame] | 52 | if (!bin_dir.has_value() || bin_dir.value().length() <= 1) { |
Peter Boström | de57333 | 2024-08-26 20:42:45 | [diff] [blame] | 53 | NOTREACHED() << "Unable to resolve path."; |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 54 | } |
Avi Drissman | 837d106b | 2023-09-12 14:51:20 | [diff] [blame] | 55 | *result = FilePath(bin_dir.value()); |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 56 | return true; |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 57 | #elif BUILDFLAG(IS_SOLARIS) |
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 58 | char bin_dir[PATH_MAX + 1]; |
| 59 | if (realpath(getexecname(), bin_dir) == NULL) { |
Peter Boström | de57333 | 2024-08-26 20:42:45 | [diff] [blame] | 60 | NOTREACHED() << "Unable to resolve " << getexecname() << "."; |
[email protected] | 94f8c95 | 2011-06-25 04:54:41 | [diff] [blame] | 61 | } |
| 62 | *result = FilePath(bin_dir); |
| 63 | return true; |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 64 | #elif BUILDFLAG(IS_OPENBSD) || BUILDFLAG(IS_AIX) |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 65 | // There is currently no way to get the executable path on OpenBSD |
[email protected] | 094797b7 | 2012-09-15 10:51:05 | [diff] [blame] | 66 | char* cpath; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 67 | if ((cpath = getenv("CHROME_EXE_PATH")) != NULL) { |
[email protected] | ea725b3 | 2011-10-25 17:43:05 | [diff] [blame] | 68 | *result = FilePath(cpath); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 69 | } else { |
[email protected] | ea725b3 | 2011-10-25 17:43:05 | [diff] [blame] | 70 | *result = FilePath("/usr/local/chrome/chrome"); |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 71 | } |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 72 | return true; |
[email protected] | 99aae10 | 2010-05-10 16:30:27 | [diff] [blame] | 73 | #endif |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 74 | } |
David Dorwin | c694f72 | 2021-10-29 22:46:59 | [diff] [blame] | 75 | case DIR_SRC_TEST_DATA_ROOT: { |
scottmg | 1ab7aa8 | 2017-05-25 05:22:49 | [diff] [blame] | 76 | FilePath path; |
[email protected] | 5d1937bb | 2009-11-21 01:29:00 | [diff] [blame] | 77 | // On POSIX, unit tests execute two levels deep from the source root. |
[email protected] | 016498e | 2010-12-03 00:59:23 | [diff] [blame] | 78 | // For example: out/{Debug|Release}/net_unittest |
thestig | bf8ab1b | 2016-11-19 04:15:42 | [diff] [blame] | 79 | if (PathService::Get(DIR_EXE, &path)) { |
[email protected] | 569edabd | 2012-02-03 23:10:04 | [diff] [blame] | 80 | *result = path.DirName().DirName(); |
[email protected] | 95d050e | 2009-09-10 19:30:46 | [diff] [blame] | 81 | return true; |
| 82 | } |
[email protected] | 569edabd | 2012-02-03 23:10:04 | [diff] [blame] | 83 | |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 84 | DLOG(ERROR) << "Couldn't find your source root. " |
| 85 | << "Try running from your chromium/src directory."; |
[email protected] | 95d050e | 2009-09-10 19:30:46 | [diff] [blame] | 86 | return false; |
[email protected] | 632be2f | 2010-04-21 23:28:43 | [diff] [blame] | 87 | } |
thestig | bf8ab1b | 2016-11-19 04:15:42 | [diff] [blame] | 88 | case DIR_USER_DESKTOP: |
| 89 | *result = nix::GetXDGUserDirectory("DESKTOP", "Desktop"); |
[email protected] | dea1d7d | 2012-09-20 16:24:52 | [diff] [blame] | 90 | return true; |
thestig | bf8ab1b | 2016-11-19 04:15:42 | [diff] [blame] | 91 | case DIR_CACHE: { |
| 92 | std::unique_ptr<Environment> env(Environment::Create()); |
| 93 | FilePath cache_dir( |
| 94 | nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME", ".cache")); |
[email protected] | 9e9b6e8e | 2009-12-02 08:45:01 | [diff] [blame] | 95 | *result = cache_dir; |
| 96 | return true; |
[email protected] | 92e44ae0a | 2012-07-23 08:22:44 | [diff] [blame] | 97 | } |
[email protected] | b2e9729 | 2008-09-02 18:20:34 | [diff] [blame] | 98 | } |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | } // namespace base |