[email protected] | 46e9acad | 2012-06-13 23:20:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 75ae542 | 2009-04-21 17:20:10 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 5 | #include "base/linux_util.h" |
[email protected] | 75ae542 | 2009-04-21 17:20:10 | [diff] [blame] | 6 | |
[email protected] | 85ebe8f | 2009-10-29 04:02:55 | [diff] [blame] | 7 | #include <dirent.h> |
| 8 | #include <errno.h> |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 9 | #include <fcntl.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 10 | #include <limits.h> |
[email protected] | 75ae542 | 2009-04-21 17:20:10 | [diff] [blame] | 11 | #include <stdlib.h> |
[email protected] | 85ebe8f | 2009-10-29 04:02:55 | [diff] [blame] | 12 | #include <sys/stat.h> |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 13 | #include <sys/types.h> |
[email protected] | 85ebe8f | 2009-10-29 04:02:55 | [diff] [blame] | 14 | #include <unistd.h> |
[email protected] | 75ae542 | 2009-04-21 17:20:10 | [diff] [blame] | 15 | |
Tomas Popela | 5b9b01f | 2019-09-10 19:42:31 | [diff] [blame] | 16 | #include <iomanip> |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 17 | #include <memory> |
[email protected] | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 18 | |
Alexandr Ilin | d4f4b34 | 2019-01-08 15:34:09 | [diff] [blame] | 19 | #include "base/files/dir_reader_posix.h" |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 20 | #include "base/files/file_util.h" |
Lei Zhang | 3bf0669 | 2020-02-03 19:41:05 | [diff] [blame] | 21 | #include "base/files/scoped_file.h" |
Alexandr Ilin | d4f4b34 | 2019-01-08 15:34:09 | [diff] [blame] | 22 | #include "base/strings/safe_sprintf.h" |
reveman | 7b97c32 | 2016-09-20 02:10:58 | [diff] [blame] | 23 | #include "base/strings/string_number_conversions.h" |
| 24 | #include "base/strings/string_split.h" |
reveman | e5aece57 | 2017-09-04 07:18:33 | [diff] [blame] | 25 | #include "base/strings/string_tokenizer.h" |
[email protected] | d529cb0 | 2013-06-10 19:06:57 | [diff] [blame] | 26 | #include "base/strings/string_util.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 27 | #include "build/build_config.h" |
Yuta Hijikata | 000df18f | 2020-11-18 06:55:58 | [diff] [blame] | 28 | #include "build/chromeos_buildflags.h" |
[email protected] | 87fc168 | 2009-07-22 00:22:49 | [diff] [blame] | 29 | |
Tomas Popela | 5b9b01f | 2019-09-10 19:42:31 | [diff] [blame] | 30 | namespace base { |
| 31 | |
[email protected] | 87fc168 | 2009-07-22 00:22:49 | [diff] [blame] | 32 | namespace { |
| 33 | |
Yuta Hijikata | 000df18f | 2020-11-18 06:55:58 | [diff] [blame] | 34 | #if !BUILDFLAG(IS_CHROMEOS_ASH) |
Tomas Popela | 5b9b01f | 2019-09-10 19:42:31 | [diff] [blame] | 35 | std::string GetKeyValueFromOSReleaseFile(const std::string& input, |
| 36 | const char* key) { |
| 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; |
| 42 | if (key_str == key) { |
| 43 | // It can contain quoted characters. |
| 44 | std::stringstream ss; |
| 45 | std::string pretty_name; |
| 46 | ss << value_str; |
| 47 | // Quoted with a single tick? |
| 48 | if (value_str[0] == '\'') |
| 49 | ss >> std::quoted(pretty_name, '\''); |
| 50 | else |
| 51 | ss >> std::quoted(pretty_name); |
| 52 | |
| 53 | return pretty_name; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return ""; |
| 58 | } |
| 59 | |
| 60 | bool ReadDistroFromOSReleaseFile(const char* file) { |
| 61 | static const char kPrettyName[] = "PRETTY_NAME"; |
| 62 | |
| 63 | std::string os_release_content; |
| 64 | if (!ReadFileToString(FilePath(file), &os_release_content)) |
| 65 | return false; |
| 66 | |
| 67 | std::string pretty_name = |
| 68 | GetKeyValueFromOSReleaseFile(os_release_content, kPrettyName); |
| 69 | if (pretty_name.empty()) |
| 70 | return false; |
| 71 | |
| 72 | SetLinuxDistro(pretty_name); |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | // https://www.freedesktop.org/software/systemd/man/os-release.html |
Tom Anderson | 02185a0 | 2020-02-01 03:49:25 | [diff] [blame] | 77 | class DistroNameGetter { |
| 78 | public: |
| 79 | 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)) |
| 84 | return; |
| 85 | } |
Tomas Popela | 5b9b01f | 2019-09-10 19:42:31 | [diff] [blame] | 86 | } |
Tom Anderson | 02185a0 | 2020-02-01 03:49:25 | [diff] [blame] | 87 | }; |
Yuta Hijikata | 000df18f | 2020-11-18 06:55:58 | [diff] [blame] | 88 | #endif // !BUILDFLAG(IS_CHROMEOS_ASH) |
[email protected] | 19467c0 | 2009-10-13 02:51:07 | [diff] [blame] | 89 | |
[email protected] | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 90 | // Account for the terminating null character. |
Lei Zhang | 3bf0669 | 2020-02-03 19:41:05 | [diff] [blame] | 91 | constexpr int kDistroSize = 128 + 1; |
| 92 | |
| 93 | } // namespace |
[email protected] | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 94 | |
[email protected] | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 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. |
[email protected] | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 97 | char g_linux_distro[kDistroSize] = |
Yuta Hijikata | 000df18f | 2020-11-18 06:55:58 | [diff] [blame] | 98 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
[email protected] | 78f6f51 | 2009-10-21 19:04:17 | [diff] [blame] | 99 | "CrOS"; |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame^] | 100 | #elif BUILDFLAG(IS_ANDROID) |
[email protected] | 84a1ab5a | 2012-07-18 01:49:22 | [diff] [blame] | 101 | "Android"; |
Tom Anderson | 02185a0 | 2020-02-01 03:49:25 | [diff] [blame] | 102 | #else |
[email protected] | 78f6f51 | 2009-10-21 19:04:17 | [diff] [blame] | 103 | "Unknown"; |
| 104 | #endif |
[email protected] | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 105 | |
Raphael Kubo da Costa | 71c31f5 | 2019-09-11 16:17:00 | [diff] [blame] | 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. |
Tomas Popela | 5b9b01f | 2019-09-10 19:42:31 | [diff] [blame] | 111 | BASE_EXPORT std::string GetKeyValueFromOSReleaseFileForTesting( |
| 112 | const std::string& input, |
| 113 | const char* key) { |
Yuta Hijikata | 000df18f | 2020-11-18 06:55:58 | [diff] [blame] | 114 | #if !BUILDFLAG(IS_CHROMEOS_ASH) |
Tomas Popela | 5b9b01f | 2019-09-10 19:42:31 | [diff] [blame] | 115 | return GetKeyValueFromOSReleaseFile(input, key); |
| 116 | #else |
| 117 | return ""; |
Yuta Hijikata | 000df18f | 2020-11-18 06:55:58 | [diff] [blame] | 118 | #endif // !BUILDFLAG(IS_CHROMEOS_ASH) |
Tomas Popela | 5b9b01f | 2019-09-10 19:42:31 | [diff] [blame] | 119 | } |
| 120 | |
[email protected] | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 121 | std::string GetLinuxDistro() { |
Yuta Hijikata | 000df18f | 2020-11-18 06:55:58 | [diff] [blame] | 122 | #if !BUILDFLAG(IS_CHROMEOS_ASH) |
[email protected] | 84a1ab5a | 2012-07-18 01:49:22 | [diff] [blame] | 123 | // We do this check only once per process. If it fails, there's |
Tomas Popela | 5b9b01f | 2019-09-10 19:42:31 | [diff] [blame] | 124 | // little reason to believe it will work if we attempt to run it again. |
Avi Drissman | ded7717 | 2021-07-02 18:23:00 | [diff] [blame] | 125 | static DistroNameGetter distro_name_getter; |
[email protected] | 78f6f51 | 2009-10-21 19:04:17 | [diff] [blame] | 126 | #endif |
Tom Anderson | 02185a0 | 2020-02-01 03:49:25 | [diff] [blame] | 127 | return g_linux_distro; |
[email protected] | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 130 | void SetLinuxDistro(const std::string& distro) { |
| 131 | std::string trimmed_distro; |
olli.raula | 36aa8be | 2015-09-10 11:14:22 | [diff] [blame] | 132 | TrimWhitespaceASCII(distro, TRIM_ALL, &trimmed_distro); |
| 133 | strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize); |
[email protected] | 6dde9d7 | 2010-08-26 08:55:22 | [diff] [blame] | 134 | } |
| 135 | |
Alexandr Ilin | d4f4b34 | 2019-01-08 15:34:09 | [diff] [blame] | 136 | bool GetThreadsForProcess(pid_t pid, std::vector<pid_t>* tids) { |
| 137 | // 25 > strlen("/proc//task") + strlen(std::to_string(INT_MAX)) + 1 = 22 |
| 138 | char buf[25]; |
Tomas Popela | 5b9b01f | 2019-09-10 19:42:31 | [diff] [blame] | 139 | strings::SafeSPrintf(buf, "/proc/%d/task", pid); |
Alexandr Ilin | d4f4b34 | 2019-01-08 15:34:09 | [diff] [blame] | 140 | DirReaderPosix dir_reader(buf); |
| 141 | |
| 142 | if (!dir_reader.IsValid()) { |
| 143 | DLOG(WARNING) << "Cannot open " << buf; |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | while (dir_reader.Next()) { |
| 148 | char* endptr; |
| 149 | const unsigned long int tid_ul = strtoul(dir_reader.name(), &endptr, 10); |
| 150 | if (tid_ul == ULONG_MAX || *endptr) |
| 151 | continue; |
| 152 | tids->push_back(tid_ul); |
| 153 | } |
| 154 | |
| 155 | return true; |
| 156 | } |
| 157 | |
[email protected] | cb7d53e | 2011-06-21 04:21:06 | [diff] [blame] | 158 | pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data, |
| 159 | bool* syscall_supported) { |
Lei Zhang | 3bf0669 | 2020-02-03 19:41:05 | [diff] [blame] | 160 | if (syscall_supported) |
[email protected] | cb7d53e | 2011-06-21 04:21:06 | [diff] [blame] | 161 | *syscall_supported = false; |
| 162 | |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 163 | std::vector<pid_t> tids; |
Alexandr Ilin | d4f4b34 | 2019-01-08 15:34:09 | [diff] [blame] | 164 | if (!GetThreadsForProcess(pid, &tids)) |
reveman | 7b97c32 | 2016-09-20 02:10:58 | [diff] [blame] | 165 | return -1; |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 166 | |
Lei Zhang | 3bf0669 | 2020-02-03 19:41:05 | [diff] [blame] | 167 | std::vector<char> syscall_data(expected_data.size()); |
reveman | 7b97c32 | 2016-09-20 02:10:58 | [diff] [blame] | 168 | for (pid_t tid : tids) { |
| 169 | char buf[256]; |
| 170 | snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, tid); |
Lei Zhang | 3bf0669 | 2020-02-03 19:41:05 | [diff] [blame] | 171 | ScopedFD fd(open(buf, O_RDONLY)); |
| 172 | if (!fd.is_valid()) |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 173 | continue; |
| 174 | |
Lei Zhang | 3bf0669 | 2020-02-03 19:41:05 | [diff] [blame] | 175 | *syscall_supported = true; |
| 176 | if (!ReadFromFD(fd.get(), syscall_data.data(), syscall_data.size())) |
| 177 | continue; |
| 178 | |
| 179 | if (0 == strncmp(expected_data.c_str(), syscall_data.data(), |
| 180 | expected_data.size())) { |
reveman | 7b97c32 | 2016-09-20 02:10:58 | [diff] [blame] | 181 | return tid; |
| 182 | } |
| 183 | } |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | pid_t FindThreadID(pid_t pid, pid_t ns_tid, bool* ns_pid_supported) { |
Lei Zhang | 3bf0669 | 2020-02-03 19:41:05 | [diff] [blame] | 188 | *ns_pid_supported = false; |
reveman | 7b97c32 | 2016-09-20 02:10:58 | [diff] [blame] | 189 | |
| 190 | std::vector<pid_t> tids; |
Alexandr Ilin | d4f4b34 | 2019-01-08 15:34:09 | [diff] [blame] | 191 | if (!GetThreadsForProcess(pid, &tids)) |
reveman | 7b97c32 | 2016-09-20 02:10:58 | [diff] [blame] | 192 | return -1; |
| 193 | |
| 194 | for (pid_t tid : tids) { |
| 195 | char buf[256]; |
| 196 | snprintf(buf, sizeof(buf), "/proc/%d/task/%d/status", pid, tid); |
| 197 | std::string status; |
| 198 | if (!ReadFileToString(FilePath(buf), &status)) |
| 199 | return -1; |
reveman | e5aece57 | 2017-09-04 07:18:33 | [diff] [blame] | 200 | StringTokenizer tokenizer(status, "\n"); |
| 201 | while (tokenizer.GetNext()) { |
| 202 | StringPiece value_str(tokenizer.token_piece()); |
Jan Wilken Dörrie | f05bb10 | 2020-08-18 19:35:56 | [diff] [blame] | 203 | if (!StartsWith(value_str, "NSpid")) |
reveman | e5aece57 | 2017-09-04 07:18:33 | [diff] [blame] | 204 | continue; |
Lei Zhang | 3bf0669 | 2020-02-03 19:41:05 | [diff] [blame] | 205 | |
| 206 | *ns_pid_supported = true; |
reveman | e5aece57 | 2017-09-04 07:18:33 | [diff] [blame] | 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); |
| 210 | int value; |
| 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)) |
| 215 | return value; |
reveman | 7b97c32 | 2016-09-20 02:10:58 | [diff] [blame] | 216 | } |
reveman | e5aece57 | 2017-09-04 07:18:33 | [diff] [blame] | 217 | break; |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | return -1; |
| 221 | } |
| 222 | |
[email protected] | 75ae542 | 2009-04-21 17:20:10 | [diff] [blame] | 223 | } // namespace base |