blob: 284e5382bf2fe38468fb867df39e08e4161005ab [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]b2e97292008-09-02 18:20:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]dea1d7d2012-09-20 16:24:525// 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]b2e97292008-09-02 18:20:348
dcheng093de9b2016-04-04 21:25:519#include "base/base_paths.h"
10
avi51ba3e692015-12-26 17:30:5011#include <limits.h>
avi9b6f42932015-12-26 22:15:1412#include <stddef.h>
avi51ba3e692015-12-26 17:30:5013
dcheng093de9b2016-04-04 21:25:5114#include <memory>
[email protected]2edc2862011-04-04 18:04:3715#include <ostream>
16#include <string>
[email protected]b2e97292008-09-02 18:20:3417
[email protected]76b90d312010-08-03 03:00:5018#include "base/environment.h"
[email protected]57999812013-02-24 05:40:5219#include "base/files/file_path.h"
[email protected]e3177dd52014-08-13 20:22:1420#include "base/files/file_util.h"
[email protected]b2e97292008-09-02 18:20:3421#include "base/logging.h"
[email protected]57999812013-02-24 05:40:5222#include "base/nix/xdg_util.h"
Hans Wennborgafeb3902020-06-17 14:42:2923#include "base/notreached.h"
[email protected]b2e97292008-09-02 18:20:3424#include "base/path_service.h"
Avi Drissman837d106b2023-09-12 14:51:2025#include "base/posix/sysctl.h"
[email protected]dd4b51262013-07-25 21:38:2326#include "base/process/process_metrics.h"
[email protected]dea1d7d2012-09-20 16:24:5227#include "build/build_config.h"
[email protected]b2e97292008-09-02 18:20:3428
Xiaohan Wang38e4ebb2022-01-19 06:57:4329#if BUILDFLAG(IS_FREEBSD)
[email protected]2edc2862011-04-04 18:04:3730#include <sys/param.h>
31#include <sys/sysctl.h>
Xiaohan Wang38e4ebb2022-01-19 06:57:4332#elif BUILDFLAG(IS_SOLARIS) || BUILDFLAG(IS_AIX)
[email protected]94f8c952011-06-25 04:54:4133#include <stdlib.h>
[email protected]2edc2862011-04-04 18:04:3734#endif
35
[email protected]b2e97292008-09-02 18:20:3436namespace base {
37
[email protected]5d1937bb2009-11-21 01:29:0038bool PathProviderPosix(int key, FilePath* result) {
[email protected]b2e97292008-09-02 18:20:3439 switch (key) {
thestigbf8ab1b2016-11-19 04:15:4240 case FILE_EXE:
41 case FILE_MODULE: { // TODO(evanm): is this correct?
Xiaohan Wang38e4ebb2022-01-19 06:57:4342#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
[email protected]723571a2010-12-03 17:37:5443 FilePath bin_dir;
[email protected]b264eab2013-11-27 23:22:0844 if (!ReadSymbolicLink(FilePath(kProcSelfExe), &bin_dir)) {
Peter Boströmde573332024-08-26 20:42:4545 NOTREACHED() << "Unable to resolve " << kProcSelfExe << ".";
[email protected]b2e97292008-09-02 18:20:3446 }
[email protected]723571a2010-12-03 17:37:5447 *result = bin_dir;
[email protected]b2e97292008-09-02 18:20:3448 return true;
Xiaohan Wang38e4ebb2022-01-19 06:57:4349#elif BUILDFLAG(IS_FREEBSD)
Peter Kasting134ef9af2024-12-28 02:30:0950 int name[] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
Arthur Sonzognie5fff99c2024-02-21 15:58:2451 std::optional<std::string> bin_dir = StringSysctl(name, std::size(name));
Avi Drissman837d106b2023-09-12 14:51:2052 if (!bin_dir.has_value() || bin_dir.value().length() <= 1) {
Peter Boströmde573332024-08-26 20:42:4553 NOTREACHED() << "Unable to resolve path.";
[email protected]99aae102010-05-10 16:30:2754 }
Avi Drissman837d106b2023-09-12 14:51:2055 *result = FilePath(bin_dir.value());
[email protected]99aae102010-05-10 16:30:2756 return true;
Xiaohan Wang38e4ebb2022-01-19 06:57:4357#elif BUILDFLAG(IS_SOLARIS)
[email protected]94f8c952011-06-25 04:54:4158 char bin_dir[PATH_MAX + 1];
59 if (realpath(getexecname(), bin_dir) == NULL) {
Peter Boströmde573332024-08-26 20:42:4560 NOTREACHED() << "Unable to resolve " << getexecname() << ".";
[email protected]94f8c952011-06-25 04:54:4161 }
62 *result = FilePath(bin_dir);
63 return true;
Xiaohan Wang38e4ebb2022-01-19 06:57:4364#elif BUILDFLAG(IS_OPENBSD) || BUILDFLAG(IS_AIX)
[email protected]817f0f142011-10-13 04:23:2265 // There is currently no way to get the executable path on OpenBSD
[email protected]094797b72012-09-15 10:51:0566 char* cpath;
Peter Kasting134ef9af2024-12-28 02:30:0967 if ((cpath = getenv("CHROME_EXE_PATH")) != NULL) {
[email protected]ea725b32011-10-25 17:43:0568 *result = FilePath(cpath);
Peter Kasting134ef9af2024-12-28 02:30:0969 } else {
[email protected]ea725b32011-10-25 17:43:0570 *result = FilePath("/usr/local/chrome/chrome");
Peter Kasting134ef9af2024-12-28 02:30:0971 }
[email protected]817f0f142011-10-13 04:23:2272 return true;
[email protected]99aae102010-05-10 16:30:2773#endif
[email protected]b2e97292008-09-02 18:20:3474 }
David Dorwinc694f722021-10-29 22:46:5975 case DIR_SRC_TEST_DATA_ROOT: {
scottmg1ab7aa82017-05-25 05:22:4976 FilePath path;
[email protected]5d1937bb2009-11-21 01:29:0077 // On POSIX, unit tests execute two levels deep from the source root.
[email protected]016498e2010-12-03 00:59:2378 // For example: out/{Debug|Release}/net_unittest
thestigbf8ab1b2016-11-19 04:15:4279 if (PathService::Get(DIR_EXE, &path)) {
[email protected]569edabd2012-02-03 23:10:0480 *result = path.DirName().DirName();
[email protected]95d050e2009-09-10 19:30:4681 return true;
82 }
[email protected]569edabd2012-02-03 23:10:0483
[email protected]a42d4632011-10-26 21:48:0084 DLOG(ERROR) << "Couldn't find your source root. "
85 << "Try running from your chromium/src directory.";
[email protected]95d050e2009-09-10 19:30:4686 return false;
[email protected]632be2f2010-04-21 23:28:4387 }
thestigbf8ab1b2016-11-19 04:15:4288 case DIR_USER_DESKTOP:
89 *result = nix::GetXDGUserDirectory("DESKTOP", "Desktop");
[email protected]dea1d7d2012-09-20 16:24:5290 return true;
thestigbf8ab1b2016-11-19 04:15:4291 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]9e9b6e8e2009-12-02 08:45:0195 *result = cache_dir;
96 return true;
[email protected]92e44ae0a2012-07-23 08:22:4497 }
[email protected]b2e97292008-09-02 18:20:3498 }
99 return false;
100}
101
102} // namespace base