thestig@chromium.org | ce2f241 | 2012-06-14 08:20:04 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
sgk@google.com | e1f4a24 | 2009-04-22 02:20:10 +0900 | [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 | |
thestig@chromium.org | a5a2b6e | 2009-07-17 14:55:51 +0900 | [diff] [blame] | 5 | #include "base/linux_util.h" |
sgk@google.com | e1f4a24 | 2009-04-22 02:20:10 +0900 | [diff] [blame] | 6 | |
thestig@chromium.org | 2feb1c8 | 2009-10-29 13:02:55 +0900 | [diff] [blame] | 7 | #include <dirent.h> |
| 8 | #include <errno.h> |
thestig@chromium.org | 44836d4 | 2010-07-17 04:28:17 +0900 | [diff] [blame] | 9 | #include <fcntl.h> |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 10 | #include <limits.h> |
sgk@google.com | e1f4a24 | 2009-04-22 02:20:10 +0900 | [diff] [blame] | 11 | #include <stdlib.h> |
thestig@chromium.org | 2feb1c8 | 2009-10-29 13:02:55 +0900 | [diff] [blame] | 12 | #include <sys/stat.h> |
thestig@chromium.org | 44836d4 | 2010-07-17 04:28:17 +0900 | [diff] [blame] | 13 | #include <sys/types.h> |
thestig@chromium.org | 2feb1c8 | 2009-10-29 13:02:55 +0900 | [diff] [blame] | 14 | #include <unistd.h> |
sgk@google.com | e1f4a24 | 2009-04-22 02:20:10 +0900 | [diff] [blame] | 15 | |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 16 | #include <memory> |
thestig@chromium.org | a5a2b6e | 2009-07-17 14:55:51 +0900 | [diff] [blame] | 17 | #include <vector> |
| 18 | |
| 19 | #include "base/command_line.h" |
brettw@chromium.org | 01f3da4 | 2014-08-14 05:22:14 +0900 | [diff] [blame] | 20 | #include "base/files/file_util.h" |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 21 | #include "base/memory/singleton.h" |
rsesek@chromium.org | 687756f | 2013-07-26 06:38:23 +0900 | [diff] [blame] | 22 | #include "base/process/launch.h" |
reveman | 7294bda | 2016-09-20 11:10:58 +0900 | [diff] [blame] | 23 | #include "base/strings/string_number_conversions.h" |
| 24 | #include "base/strings/string_split.h" |
avi@chromium.org | 67d593d | 2013-06-11 04:06:57 +0900 | [diff] [blame] | 25 | #include "base/strings/string_util.h" |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 26 | #include "base/synchronization/lock.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 27 | #include "build/build_config.h" |
mattm@chromium.org | 625865e | 2009-07-22 09:22:49 +0900 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | |
thestig@chromium.org | 9d0e95c | 2009-10-22 04:04:17 +0900 | [diff] [blame] | 31 | // Not needed for OS_CHROMEOS. |
| 32 | #if defined(OS_LINUX) |
thestig@chromium.org | 741196d | 2009-10-13 11:51:07 +0900 | [diff] [blame] | 33 | enum LinuxDistroState { |
| 34 | STATE_DID_NOT_CHECK = 0, |
| 35 | STATE_CHECK_STARTED = 1, |
| 36 | STATE_CHECK_FINISHED = 2, |
| 37 | }; |
| 38 | |
| 39 | // Helper class for GetLinuxDistro(). |
| 40 | class LinuxDistroHelper { |
| 41 | public: |
| 42 | // Retrieves the Singleton. |
satish@chromium.org | ce3f487 | 2010-12-14 01:52:35 +0900 | [diff] [blame] | 43 | static LinuxDistroHelper* GetInstance() { |
olli.raula | 5ba9489 | 2015-09-10 20:14:22 +0900 | [diff] [blame] | 44 | return base::Singleton<LinuxDistroHelper>::get(); |
thestig@chromium.org | 741196d | 2009-10-13 11:51:07 +0900 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | // The simple state machine goes from: |
| 48 | // STATE_DID_NOT_CHECK -> STATE_CHECK_STARTED -> STATE_CHECK_FINISHED. |
| 49 | LinuxDistroHelper() : state_(STATE_DID_NOT_CHECK) {} |
| 50 | ~LinuxDistroHelper() {} |
| 51 | |
| 52 | // Retrieve the current state, if we're in STATE_DID_NOT_CHECK, |
| 53 | // we automatically move to STATE_CHECK_STARTED so nobody else will |
| 54 | // do the check. |
| 55 | LinuxDistroState State() { |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 56 | base::AutoLock scoped_lock(lock_); |
thestig@chromium.org | 741196d | 2009-10-13 11:51:07 +0900 | [diff] [blame] | 57 | if (STATE_DID_NOT_CHECK == state_) { |
| 58 | state_ = STATE_CHECK_STARTED; |
| 59 | return STATE_DID_NOT_CHECK; |
| 60 | } |
| 61 | return state_; |
| 62 | } |
| 63 | |
| 64 | // Indicate the check finished, move to STATE_CHECK_FINISHED. |
| 65 | void CheckFinished() { |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 66 | base::AutoLock scoped_lock(lock_); |
david.mike.futcher@gmail.com | 9eb2aa5 | 2011-04-19 05:07:08 +0900 | [diff] [blame] | 67 | DCHECK_EQ(STATE_CHECK_STARTED, state_); |
thestig@chromium.org | 741196d | 2009-10-13 11:51:07 +0900 | [diff] [blame] | 68 | state_ = STATE_CHECK_FINISHED; |
| 69 | } |
| 70 | |
| 71 | private: |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 72 | base::Lock lock_; |
thestig@chromium.org | 741196d | 2009-10-13 11:51:07 +0900 | [diff] [blame] | 73 | LinuxDistroState state_; |
| 74 | }; |
thestig@chromium.org | 9d0e95c | 2009-10-22 04:04:17 +0900 | [diff] [blame] | 75 | #endif // if defined(OS_LINUX) |
thestig@chromium.org | 741196d | 2009-10-13 11:51:07 +0900 | [diff] [blame] | 76 | |
reveman | 7294bda | 2016-09-20 11:10:58 +0900 | [diff] [blame] | 77 | bool GetTasksForProcess(pid_t pid, std::vector<pid_t>* tids) { |
| 78 | char buf[256]; |
| 79 | snprintf(buf, sizeof(buf), "/proc/%d/task", pid); |
| 80 | |
| 81 | DIR* task = opendir(buf); |
| 82 | if (!task) { |
| 83 | DLOG(WARNING) << "Cannot open " << buf; |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | struct dirent* dent; |
| 88 | while ((dent = readdir(task))) { |
| 89 | char* endptr; |
| 90 | const unsigned long int tid_ul = strtoul(dent->d_name, &endptr, 10); |
| 91 | if (tid_ul == ULONG_MAX || *endptr) |
| 92 | continue; |
| 93 | tids->push_back(tid_ul); |
| 94 | } |
| 95 | closedir(task); |
| 96 | return true; |
| 97 | } |
| 98 | |
thestig@chromium.org | b6ba943 | 2010-04-03 10:05:39 +0900 | [diff] [blame] | 99 | } // namespace |
thestig@chromium.org | a5a2b6e | 2009-07-17 14:55:51 +0900 | [diff] [blame] | 100 | |
sgk@google.com | e1f4a24 | 2009-04-22 02:20:10 +0900 | [diff] [blame] | 101 | namespace base { |
| 102 | |
mnissler@chromium.org | 8584b6d | 2010-08-26 17:55:22 +0900 | [diff] [blame] | 103 | // Account for the terminating null character. |
| 104 | static const int kDistroSize = 128 + 1; |
| 105 | |
thestig@chromium.org | a5a2b6e | 2009-07-17 14:55:51 +0900 | [diff] [blame] | 106 | // We use this static string to hold the Linux distro info. If we |
| 107 | // crash, the crash handler code will send this in the crash dump. |
mnissler@chromium.org | 8584b6d | 2010-08-26 17:55:22 +0900 | [diff] [blame] | 108 | char g_linux_distro[kDistroSize] = |
rbyers@chromium.org | 088a493 | 2012-08-16 05:54:06 +0900 | [diff] [blame] | 109 | #if defined(OS_CHROMEOS) |
thestig@chromium.org | 9d0e95c | 2009-10-22 04:04:17 +0900 | [diff] [blame] | 110 | "CrOS"; |
nileshagrawal@chromium.org | ea12397 | 2012-07-18 10:49:22 +0900 | [diff] [blame] | 111 | #elif defined(OS_ANDROID) |
| 112 | "Android"; |
thestig@chromium.org | 9d0e95c | 2009-10-22 04:04:17 +0900 | [diff] [blame] | 113 | #else // if defined(OS_LINUX) |
| 114 | "Unknown"; |
| 115 | #endif |
thestig@chromium.org | a5a2b6e | 2009-07-17 14:55:51 +0900 | [diff] [blame] | 116 | |
| 117 | std::string GetLinuxDistro() { |
nileshagrawal@chromium.org | ea12397 | 2012-07-18 10:49:22 +0900 | [diff] [blame] | 118 | #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
mnissler@chromium.org | 8584b6d | 2010-08-26 17:55:22 +0900 | [diff] [blame] | 119 | return g_linux_distro; |
pvalchev@google.com | 1d919db | 2010-03-10 16:46:43 +0900 | [diff] [blame] | 120 | #elif defined(OS_LINUX) |
satish@chromium.org | ce3f487 | 2010-12-14 01:52:35 +0900 | [diff] [blame] | 121 | LinuxDistroHelper* distro_state_singleton = LinuxDistroHelper::GetInstance(); |
thestig@chromium.org | 741196d | 2009-10-13 11:51:07 +0900 | [diff] [blame] | 122 | LinuxDistroState state = distro_state_singleton->State(); |
nileshagrawal@chromium.org | ea12397 | 2012-07-18 10:49:22 +0900 | [diff] [blame] | 123 | if (STATE_CHECK_FINISHED == state) |
| 124 | return g_linux_distro; |
| 125 | if (STATE_CHECK_STARTED == state) |
| 126 | return "Unknown"; // Don't wait for other thread to finish. |
| 127 | DCHECK_EQ(state, STATE_DID_NOT_CHECK); |
| 128 | // We do this check only once per process. If it fails, there's |
| 129 | // little reason to believe it will work if we attempt to run |
| 130 | // lsb_release again. |
| 131 | std::vector<std::string> argv; |
| 132 | argv.push_back("lsb_release"); |
| 133 | argv.push_back("-d"); |
| 134 | std::string output; |
olli.raula | 5ba9489 | 2015-09-10 20:14:22 +0900 | [diff] [blame] | 135 | GetAppOutput(CommandLine(argv), &output); |
nileshagrawal@chromium.org | ea12397 | 2012-07-18 10:49:22 +0900 | [diff] [blame] | 136 | if (output.length() > 0) { |
| 137 | // lsb_release -d should return: Description:<tab>Distro Info |
| 138 | const char field[] = "Description:\t"; |
| 139 | if (output.compare(0, strlen(field), field) == 0) { |
| 140 | SetLinuxDistro(output.substr(strlen(field))); |
thestig@chromium.org | a5a2b6e | 2009-07-17 14:55:51 +0900 | [diff] [blame] | 141 | } |
thestig@chromium.org | a5a2b6e | 2009-07-17 14:55:51 +0900 | [diff] [blame] | 142 | } |
nileshagrawal@chromium.org | ea12397 | 2012-07-18 10:49:22 +0900 | [diff] [blame] | 143 | distro_state_singleton->CheckFinished(); |
| 144 | return g_linux_distro; |
pvalchev@google.com | 1d919db | 2010-03-10 16:46:43 +0900 | [diff] [blame] | 145 | #else |
| 146 | NOTIMPLEMENTED(); |
robert.nagy@gmail.com | ea54e46 | 2011-10-25 07:05:27 +0900 | [diff] [blame] | 147 | return "Unknown"; |
thestig@chromium.org | 9d0e95c | 2009-10-22 04:04:17 +0900 | [diff] [blame] | 148 | #endif |
thestig@chromium.org | a5a2b6e | 2009-07-17 14:55:51 +0900 | [diff] [blame] | 149 | } |
| 150 | |
mnissler@chromium.org | 8584b6d | 2010-08-26 17:55:22 +0900 | [diff] [blame] | 151 | void SetLinuxDistro(const std::string& distro) { |
| 152 | std::string trimmed_distro; |
olli.raula | 5ba9489 | 2015-09-10 20:14:22 +0900 | [diff] [blame] | 153 | TrimWhitespaceASCII(distro, TRIM_ALL, &trimmed_distro); |
| 154 | strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize); |
mnissler@chromium.org | 8584b6d | 2010-08-26 17:55:22 +0900 | [diff] [blame] | 155 | } |
| 156 | |
kmixter@chromium.org | e406ed7 | 2011-06-21 13:21:06 +0900 | [diff] [blame] | 157 | pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data, |
| 158 | bool* syscall_supported) { |
kmixter@chromium.org | e406ed7 | 2011-06-21 13:21:06 +0900 | [diff] [blame] | 159 | if (syscall_supported != NULL) |
| 160 | *syscall_supported = false; |
| 161 | |
thestig@chromium.org | 44836d4 | 2010-07-17 04:28:17 +0900 | [diff] [blame] | 162 | std::vector<pid_t> tids; |
reveman | 7294bda | 2016-09-20 11:10:58 +0900 | [diff] [blame] | 163 | if (!GetTasksForProcess(pid, &tids)) |
| 164 | return -1; |
thestig@chromium.org | 44836d4 | 2010-07-17 04:28:17 +0900 | [diff] [blame] | 165 | |
dcheng | cc8e4d8 | 2016-04-05 06:25:51 +0900 | [diff] [blame] | 166 | std::unique_ptr<char[]> syscall_data(new char[expected_data.length()]); |
reveman | 7294bda | 2016-09-20 11:10:58 +0900 | [diff] [blame] | 167 | for (pid_t tid : tids) { |
| 168 | char buf[256]; |
| 169 | snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, tid); |
thestig@chromium.org | 44836d4 | 2010-07-17 04:28:17 +0900 | [diff] [blame] | 170 | int fd = open(buf, O_RDONLY); |
| 171 | if (fd < 0) |
| 172 | continue; |
kmixter@chromium.org | e406ed7 | 2011-06-21 13:21:06 +0900 | [diff] [blame] | 173 | if (syscall_supported != NULL) |
| 174 | *syscall_supported = true; |
brettw@chromium.org | 2873d9b | 2013-11-28 08:22:08 +0900 | [diff] [blame] | 175 | bool read_ret = ReadFromFD(fd, syscall_data.get(), expected_data.length()); |
thestig@chromium.org | 44836d4 | 2010-07-17 04:28:17 +0900 | [diff] [blame] | 176 | close(fd); |
| 177 | if (!read_ret) |
| 178 | continue; |
| 179 | |
| 180 | if (0 == strncmp(expected_data.c_str(), syscall_data.get(), |
| 181 | expected_data.length())) { |
reveman | 7294bda | 2016-09-20 11:10:58 +0900 | [diff] [blame] | 182 | return tid; |
| 183 | } |
| 184 | } |
| 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | pid_t FindThreadID(pid_t pid, pid_t ns_tid, bool* ns_pid_supported) { |
| 189 | if (ns_pid_supported) |
| 190 | *ns_pid_supported = false; |
| 191 | |
| 192 | std::vector<pid_t> tids; |
| 193 | if (!GetTasksForProcess(pid, &tids)) |
| 194 | return -1; |
| 195 | |
| 196 | for (pid_t tid : tids) { |
| 197 | char buf[256]; |
| 198 | snprintf(buf, sizeof(buf), "/proc/%d/task/%d/status", pid, tid); |
| 199 | std::string status; |
| 200 | if (!ReadFileToString(FilePath(buf), &status)) |
| 201 | return -1; |
| 202 | StringPairs pairs; |
| 203 | SplitStringIntoKeyValuePairs(status, ':', '\n', &pairs); |
| 204 | for (const auto& pair : pairs) { |
| 205 | const std::string& key = pair.first; |
| 206 | const std::string& value_str = pair.second; |
| 207 | if (key == "NSpid") { |
| 208 | if (ns_pid_supported) |
| 209 | *ns_pid_supported = true; |
| 210 | std::vector<StringPiece> split_value_str = SplitStringPiece( |
| 211 | value_str, "\t", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); |
| 212 | DCHECK_NE(split_value_str.size(), 0u); |
| 213 | int value; |
| 214 | // The last value in the list is the PID in the namespace. |
| 215 | if (StringToInt(split_value_str.back(), &value) && value == ns_tid) { |
| 216 | // The first value in the list is the real PID. |
| 217 | if (StringToInt(split_value_str.front(), &value)) |
| 218 | return value; |
| 219 | } |
| 220 | } |
thestig@chromium.org | 44836d4 | 2010-07-17 04:28:17 +0900 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | return -1; |
| 224 | } |
| 225 | |
sgk@google.com | e1f4a24 | 2009-04-22 02:20:10 +0900 | [diff] [blame] | 226 | } // namespace base |