1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
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).
8
9 #include "base/base_paths.h"
10
11 #include <limits.h>
12 #include <stddef.h>
13
14 #include <memory>
15 #include <ostream>
16 #include <string>
17
18 #include "base/environment.h"
19 #include "base/files/file_path.h"
20 #include "base/files/file_util.h"
21 #include "base/logging.h"
22 #include "base/nix/xdg_util.h"
23 #include "base/notreached.h"
24 #include "base/path_service.h"
25 #include "base/posix/sysctl.h"
26 #include "base/process/process_metrics.h"
27 #include "build/build_config.h"
28
29 #if BUILDFLAG(IS_FREEBSD)
30 #include <sys/param.h>
31 #include <sys/sysctl.h>
32 #elif BUILDFLAG(IS_SOLARIS) || BUILDFLAG(IS_AIX)
33 #include <stdlib.h>
34 #endif
35
36 namespace base {
37
PathProviderPosix(int key,FilePath * result)38 bool PathProviderPosix(int key, FilePath* result) {
39 switch (key) {
40 case FILE_EXE:
41 case FILE_MODULE: { // TODO(evanm): is this correct?
42 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
43 FilePath bin_dir;
44 if (!ReadSymbolicLink(FilePath(kProcSelfExe), &bin_dir)) {
45 NOTREACHED() << "Unable to resolve " << kProcSelfExe << ".";
46 }
47 *result = bin_dir;
48 return true;
49 #elif BUILDFLAG(IS_FREEBSD)
50 int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
51 std::optional<std::string> bin_dir = StringSysctl(name, std::size(name));
52 if (!bin_dir.has_value() || bin_dir.value().length() <= 1) {
53 NOTREACHED() << "Unable to resolve path.";
54 }
55 *result = FilePath(bin_dir.value());
56 return true;
57 #elif BUILDFLAG(IS_SOLARIS)
58 char bin_dir[PATH_MAX + 1];
59 if (realpath(getexecname(), bin_dir) == NULL) {
60 NOTREACHED() << "Unable to resolve " << getexecname() << ".";
61 }
62 *result = FilePath(bin_dir);
63 return true;
64 #elif BUILDFLAG(IS_OPENBSD) || BUILDFLAG(IS_AIX)
65 // There is currently no way to get the executable path on OpenBSD
66 char* cpath;
67 if ((cpath = getenv("CHROME_EXE_PATH")) != NULL)
68 *result = FilePath(cpath);
69 else
70 *result = FilePath("/usr/local/chrome/chrome");
71 return true;
72 #endif
73 }
74 case DIR_SRC_TEST_DATA_ROOT: {
75 FilePath path;
76 // On POSIX, unit tests execute two levels deep from the source root.
77 // For example: out/{Debug|Release}/net_unittest
78 if (PathService::Get(DIR_EXE, &path)) {
79 *result = path.DirName().DirName();
80 return true;
81 }
82
83 DLOG(ERROR) << "Couldn't find your source root. "
84 << "Try running from your chromium/src directory.";
85 return false;
86 }
87 case DIR_USER_DESKTOP:
88 *result = nix::GetXDGUserDirectory("DESKTOP", "Desktop");
89 return true;
90 case DIR_CACHE: {
91 std::unique_ptr<Environment> env(Environment::Create());
92 FilePath cache_dir(
93 nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME", ".cache"));
94 *result = cache_dir;
95 return true;
96 }
97 }
98 return false;
99 }
100
101 } // namespace base
102