1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
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/files/dir_reader_posix.h"
20 #include "base/files/file_util.h"
21 #include "base/files/scoped_file.h"
22 #include "base/strings/safe_sprintf.h"
23 #include "base/strings/string_number_conversions.h"
24 #include "base/strings/string_split.h"
25 #include "base/strings/string_tokenizer.h"
26 #include "base/strings/string_util.h"
27 #include "build/build_config.h"
28 #include "build/chromeos_buildflags.h"
34 #if !BUILDFLAG(IS_CHROMEOS_ASH)
35 std::string GetKeyValueFromOSReleaseFile(const std::string& input,
37 StringPairs key_value_pairs;
38 SplitStringIntoKeyValuePairs(input, '=', '\n', &key_value_pairs);
39 for (const auto& pair : key_value_pairs) {
40 const std::string& key_str = pair.first;
41 const std::string& value_str = pair.second;
43 // It can contain quoted characters.
45 std::string pretty_name;
47 // Quoted with a single tick?
48 if (value_str[0] == '\'')
49 ss >> std::quoted(pretty_name, '\'');
51 ss >> std::quoted(pretty_name);
60 bool ReadDistroFromOSReleaseFile(const char* file) {
61 static const char kPrettyName[] = "PRETTY_NAME";
63 std::string os_release_content;
64 if (!ReadFileToString(FilePath(file), &os_release_content))
67 std::string pretty_name =
68 GetKeyValueFromOSReleaseFile(os_release_content, kPrettyName);
69 if (pretty_name.empty())
72 SetLinuxDistro(pretty_name);
76 // https://www.freedesktop.org/software/systemd/man/os-release.html
77 class DistroNameGetter {
80 static const char* const kFilesToCheck[] = {"/etc/os-release",
81 "/usr/lib/os-release"};
82 for (const char* file : kFilesToCheck) {
83 if (ReadDistroFromOSReleaseFile(file))
88 #endif // !BUILDFLAG(IS_CHROMEOS_ASH)
90 // Account for the terminating null character.
91 constexpr int kDistroSize = 128 + 1;
95 // We use this static string to hold the Linux distro info. If we
96 // crash, the crash handler code will send this in the crash dump.
97 char g_linux_distro[kDistroSize] =
98 #if BUILDFLAG(IS_CHROMEOS_ASH)
100 #elif defined(OS_ANDROID)
106 // This function is only supposed to be used in tests. The declaration in the
107 // header file is guarded by "#if defined(UNIT_TEST)" so that they can be used
108 // by tests but not non-test code. However, this .cc file is compiled as part
109 // of "base" where "UNIT_TEST" is not defined. So we need to specify
110 // "BASE_EXPORT" here again so that they are visible to tests.
111 BASE_EXPORT std::string GetKeyValueFromOSReleaseFileForTesting(
112 const std::string& input,
114 #if !BUILDFLAG(IS_CHROMEOS_ASH)
115 return GetKeyValueFromOSReleaseFile(input, key);
118 #endif // !BUILDFLAG(IS_CHROMEOS_ASH)
121 std::string GetLinuxDistro() {
122 #if !BUILDFLAG(IS_CHROMEOS_ASH)
123 // We do this check only once per process. If it fails, there's
124 // little reason to believe it will work if we attempt to run it again.
125 static DistroNameGetter distro_name_getter;
127 return g_linux_distro;
130 void SetLinuxDistro(const std::string& distro) {
131 std::string trimmed_distro;
132 TrimWhitespaceASCII(distro, TRIM_ALL, &trimmed_distro);
133 strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize);
136 bool GetThreadsForProcess(pid_t pid, std::vector<pid_t>* tids) {
137 // 25 > strlen("/proc//task") + strlen(std::to_string(INT_MAX)) + 1 = 22
139 strings::SafeSPrintf(buf, "/proc/%d/task", pid);
140 DirReaderPosix dir_reader(buf);
142 if (!dir_reader.IsValid()) {
143 DLOG(WARNING) << "Cannot open " << buf;
147 while (dir_reader.Next()) {
149 const unsigned long int tid_ul = strtoul(dir_reader.name(), &endptr, 10);
150 if (tid_ul == ULONG_MAX || *endptr)
152 tids->push_back(tid_ul);
158 pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data,
159 bool* syscall_supported) {
160 if (syscall_supported)
161 *syscall_supported = false;
163 std::vector<pid_t> tids;
164 if (!GetThreadsForProcess(pid, &tids))
167 std::vector<char> syscall_data(expected_data.size());
168 for (pid_t tid : tids) {
170 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, tid);
171 ScopedFD fd(open(buf, O_RDONLY));
175 *syscall_supported = true;
176 if (!ReadFromFD(fd.get(), syscall_data.data(), syscall_data.size()))
179 if (0 == strncmp(expected_data.c_str(), syscall_data.data(),
180 expected_data.size())) {
187 pid_t FindThreadID(pid_t pid, pid_t ns_tid, bool* ns_pid_supported) {
188 *ns_pid_supported = false;
190 std::vector<pid_t> tids;
191 if (!GetThreadsForProcess(pid, &tids))
194 for (pid_t tid : tids) {
196 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/status", pid, tid);
198 if (!ReadFileToString(FilePath(buf), &status))
200 StringTokenizer tokenizer(status, "\n");
201 while (tokenizer.GetNext()) {
202 StringPiece value_str(tokenizer.token_piece());
203 if (!StartsWith(value_str, "NSpid"))
206 *ns_pid_supported = true;
207 std::vector<StringPiece> split_value_str = SplitStringPiece(
208 value_str, "\t", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
209 DCHECK_GE(split_value_str.size(), 2u);
211 // The last value in the list is the PID in the namespace.
212 if (StringToInt(split_value_str.back(), &value) && value == ns_tid) {
213 // The second value in the list is the real PID.
214 if (StringToInt(split_value_str[1], &value))