xref: /aosp_15_r20/external/cronet/base/base_paths_posix.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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         return false;
47       }
48       *result = bin_dir;
49       return true;
50 #elif BUILDFLAG(IS_FREEBSD)
51       int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
52       std::optional<std::string> bin_dir = StringSysctl(name, std::size(name));
53       if (!bin_dir.has_value() || bin_dir.value().length() <= 1) {
54         NOTREACHED() << "Unable to resolve path.";
55         return false;
56       }
57       *result = FilePath(bin_dir.value());
58       return true;
59 #elif BUILDFLAG(IS_SOLARIS)
60       char bin_dir[PATH_MAX + 1];
61       if (realpath(getexecname(), bin_dir) == NULL) {
62         NOTREACHED() << "Unable to resolve " << getexecname() << ".";
63         return false;
64       }
65       *result = FilePath(bin_dir);
66       return true;
67 #elif BUILDFLAG(IS_OPENBSD) || BUILDFLAG(IS_AIX)
68       // There is currently no way to get the executable path on OpenBSD
69       char* cpath;
70       if ((cpath = getenv("CHROME_EXE_PATH")) != NULL)
71         *result = FilePath(cpath);
72       else
73         *result = FilePath("/usr/local/chrome/chrome");
74       return true;
75 #endif
76     }
77     case DIR_SRC_TEST_DATA_ROOT: {
78       FilePath path;
79       // On POSIX, unit tests execute two levels deep from the source root.
80       // For example:  out/{Debug|Release}/net_unittest
81       if (PathService::Get(DIR_EXE, &path)) {
82         *result = path.DirName().DirName();
83         return true;
84       }
85 
86       DLOG(ERROR) << "Couldn't find your source root.  "
87                   << "Try running from your chromium/src directory.";
88       return false;
89     }
90     case DIR_USER_DESKTOP:
91       *result = nix::GetXDGUserDirectory("DESKTOP", "Desktop");
92       return true;
93     case DIR_CACHE: {
94       std::unique_ptr<Environment> env(Environment::Create());
95       FilePath cache_dir(
96           nix::GetXDGDirectory(env.get(), "XDG_CACHE_HOME", ".cache"));
97       *result = cache_dir;
98       return true;
99     }
100   }
101   return false;
102 }
103 
104 }  // namespace base
105