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.
5 #include "base/linux_util.h"
13 #include <sys/types.h>
19 #include "base/base_export.h"
20 #include "base/files/dir_reader_posix.h"
21 #include "base/files/file_util.h"
22 #include "base/files/scoped_file.h"
23 #include "base/strings/safe_sprintf.h"
24 #include "base/strings/string_number_conversions.h"
25 #include "base/strings/string_split.h"
26 #include "base/strings/string_tokenizer.h"
27 #include "base/strings/string_util.h"
28 #include "build/build_config.h"
29 #include "build/chromeos_buildflags.h"
35 #if !BUILDFLAG(IS_CHROMEOS_ASH)
36 std::string GetKeyValueFromOSReleaseFile(const std::string& input,
38 StringPairs key_value_pairs;
39 SplitStringIntoKeyValuePairs(input, '=', '\n', &key_value_pairs);
40 for (const auto& pair : key_value_pairs) {
41 const std::string& key_str = pair.first;
42 const std::string& value_str = pair.second;
44 // It can contain quoted characters.
46 std::string pretty_name;
48 // Quoted with a single tick?
49 if (value_str[0] == '\'')
50 ss >> std::quoted(pretty_name, '\'');
52 ss >> std::quoted(pretty_name);
61 bool ReadDistroFromOSReleaseFile(const char* file) {
62 static const char kPrettyName[] = "PRETTY_NAME";
64 std::string os_release_content;
65 if (!ReadFileToString(FilePath(file), &os_release_content))
68 std::string pretty_name =
69 GetKeyValueFromOSReleaseFile(os_release_content, kPrettyName);
70 if (pretty_name.empty())
73 SetLinuxDistro(pretty_name);
77 // https://www.freedesktop.org/software/systemd/man/os-release.html
78 class DistroNameGetter {
81 static const char* const kFilesToCheck[] = {"/etc/os-release",
82 "/usr/lib/os-release"};
83 for (const char* file : kFilesToCheck) {
84 if (ReadDistroFromOSReleaseFile(file))
89 #endif // !BUILDFLAG(IS_CHROMEOS_ASH)
91 bool GetThreadsFromProcessDir(const char* dir_path, std::vector<pid_t>* tids) {
92 DirReaderPosix dir_reader(dir_path);
94 if (!dir_reader.IsValid()) {
95 DLOG(WARNING) << "Cannot open " << dir_path;
99 while (dir_reader.Next()) {
101 if (StringToInt(dir_reader.name(), &tid)) {
102 tids->push_back(tid);
109 // Account for the terminating null character.
110 constexpr int kDistroSize = 128 + 1;
114 // We use this static string to hold the Linux distro info. If we
115 // crash, the crash handler code will send this in the crash dump.
116 char g_linux_distro[kDistroSize] =
117 #if BUILDFLAG(IS_CHROMEOS_ASH)
119 #elif BUILDFLAG(IS_ANDROID)
125 // This function is only supposed to be used in tests. The declaration in the
126 // header file is guarded by "#if defined(UNIT_TEST)" so that they can be used
127 // by tests but not non-test code. However, this .cc file is compiled as part
128 // of "base" where "UNIT_TEST" is not defined. So we need to specify
129 // "BASE_EXPORT" here again so that they are visible to tests.
130 BASE_EXPORT std::string GetKeyValueFromOSReleaseFileForTesting(
131 const std::string& input,
133 #if !BUILDFLAG(IS_CHROMEOS_ASH)
134 return GetKeyValueFromOSReleaseFile(input, key);
137 #endif // !BUILDFLAG(IS_CHROMEOS_ASH)
140 std::string GetLinuxDistro() {
141 #if !BUILDFLAG(IS_CHROMEOS_ASH)
142 // We do this check only once per process. If it fails, there's
143 // little reason to believe it will work if we attempt to run it again.
144 static DistroNameGetter distro_name_getter;
146 return g_linux_distro;
149 void SetLinuxDistro(const std::string& distro) {
150 std::string trimmed_distro;
151 TrimWhitespaceASCII(distro, TRIM_ALL, &trimmed_distro);
152 strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize);
155 bool GetThreadsForProcess(pid_t pid, std::vector<pid_t>* tids) {
156 // 25 > strlen("/proc//task") + strlen(std::to_string(INT_MAX)) + 1 = 22
158 strings::SafeSPrintf(buf, "/proc/%d/task", pid);
159 return GetThreadsFromProcessDir(buf, tids);
162 bool GetThreadsForCurrentProcess(std::vector<pid_t>* tids) {
163 return GetThreadsFromProcessDir("/proc/self/task", tids);
166 pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data,
167 bool* syscall_supported) {
168 if (syscall_supported)
169 *syscall_supported = false;
171 std::vector<pid_t> tids;
172 if (!GetThreadsForProcess(pid, &tids))
175 std::vector<char> syscall_data(expected_data.size());
176 for (pid_t tid : tids) {
178 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, tid);
179 ScopedFD fd(open(buf, O_RDONLY));
183 *syscall_supported = true;
184 if (!ReadFromFD(fd.get(), syscall_data.data(), syscall_data.size()))
187 if (0 == strncmp(expected_data.c_str(), syscall_data.data(),
188 expected_data.size())) {
195 pid_t FindThreadID(pid_t pid, pid_t ns_tid, bool* ns_pid_supported) {
196 *ns_pid_supported = false;
198 std::vector<pid_t> tids;
199 if (!GetThreadsForProcess(pid, &tids))
202 for (pid_t tid : tids) {
204 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/status", pid, tid);
206 if (!ReadFileToString(FilePath(buf), &status))
208 StringTokenizer tokenizer(status, "\n");
209 while (tokenizer.GetNext()) {
210 StringPiece value_str(tokenizer.token_piece());
211 if (!StartsWith(value_str, "NSpid"))
214 *ns_pid_supported = true;
215 std::vector<StringPiece> split_value_str = SplitStringPiece(
216 value_str, "\t", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
217 DCHECK_GE(split_value_str.size(), 2u);
219 // The last value in the list is the PID in the namespace.
220 if (StringToInt(split_value_str.back(), &value) && value == ns_tid) {
221 // The second value in the list is the real PID.
222 if (StringToInt(split_value_str[1], &value))