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/command_line.h"
20 #include "base/files/file_util.h"
21 #include "base/memory/singleton.h"
22 #include "base/process/launch.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 "base/synchronization/lock.h"
28 #include "build/build_config.h"
32 // Not needed for OS_CHROMEOS.
34 enum LinuxDistroState {
35 STATE_DID_NOT_CHECK = 0,
36 STATE_CHECK_STARTED = 1,
37 STATE_CHECK_FINISHED = 2,
40 // Helper class for GetLinuxDistro().
41 class LinuxDistroHelper {
43 // Retrieves the Singleton.
44 static LinuxDistroHelper* GetInstance() {
45 return base::Singleton<LinuxDistroHelper>::get();
48 // The simple state machine goes from:
49 // STATE_DID_NOT_CHECK -> STATE_CHECK_STARTED -> STATE_CHECK_FINISHED.
50 LinuxDistroHelper() : state_(STATE_DID_NOT_CHECK) {}
51 ~LinuxDistroHelper() = default;
53 // Retrieve the current state, if we're in STATE_DID_NOT_CHECK,
54 // we automatically move to STATE_CHECK_STARTED so nobody else will
56 LinuxDistroState State() {
57 base::AutoLock scoped_lock(lock_);
58 if (STATE_DID_NOT_CHECK == state_) {
59 state_ = STATE_CHECK_STARTED;
60 return STATE_DID_NOT_CHECK;
65 // Indicate the check finished, move to STATE_CHECK_FINISHED.
66 void CheckFinished() {
67 base::AutoLock scoped_lock(lock_);
68 DCHECK_EQ(STATE_CHECK_STARTED, state_);
69 state_ = STATE_CHECK_FINISHED;
74 LinuxDistroState state_;
76 #endif // if defined(OS_LINUX)
78 bool GetTasksForProcess(pid_t pid, std::vector<pid_t>* tids) {
80 snprintf(buf, sizeof(buf), "/proc/%d/task", pid);
82 DIR* task = opendir(buf);
84 DLOG(WARNING) << "Cannot open " << buf;
89 while ((dent = readdir(task))) {
91 const unsigned long int tid_ul = strtoul(dent->d_name, &endptr, 10);
92 if (tid_ul == ULONG_MAX || *endptr)
94 tids->push_back(tid_ul);
104 // Account for the terminating null character.
105 static const int kDistroSize = 128 + 1;
107 // We use this static string to hold the Linux distro info. If we
108 // crash, the crash handler code will send this in the crash dump.
109 char g_linux_distro[kDistroSize] =
110 #if defined(OS_CHROMEOS)
112 #elif defined(OS_ANDROID)
114 #else // if defined(OS_LINUX)
118 std::string GetLinuxDistro() {
119 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
120 return g_linux_distro;
121 #elif defined(OS_LINUX)
122 LinuxDistroHelper* distro_state_singleton = LinuxDistroHelper::GetInstance();
123 LinuxDistroState state = distro_state_singleton->State();
124 if (STATE_CHECK_FINISHED == state)
125 return g_linux_distro;
126 if (STATE_CHECK_STARTED == state)
127 return "Unknown"; // Don't wait for other thread to finish.
128 DCHECK_EQ(state, STATE_DID_NOT_CHECK);
129 // We do this check only once per process. If it fails, there's
130 // little reason to believe it will work if we attempt to run
131 // lsb_release again.
132 std::vector<std::string> argv;
133 argv.push_back("lsb_release");
134 argv.push_back("-d");
136 GetAppOutput(CommandLine(argv), &output);
137 if (output.length() > 0) {
138 // lsb_release -d should return: Description:<tab>Distro Info
139 const char field[] = "Description:\t";
140 if (output.compare(0, strlen(field), field) == 0) {
141 SetLinuxDistro(output.substr(strlen(field)));
144 distro_state_singleton->CheckFinished();
145 return g_linux_distro;
152 void SetLinuxDistro(const std::string& distro) {
153 std::string trimmed_distro;
154 TrimWhitespaceASCII(distro, TRIM_ALL, &trimmed_distro);
155 strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize);
158 pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data,
159 bool* syscall_supported) {
160 if (syscall_supported != nullptr)
161 *syscall_supported = false;
163 std::vector<pid_t> tids;
164 if (!GetTasksForProcess(pid, &tids))
167 std::unique_ptr<char[]> syscall_data(new char[expected_data.length()]);
168 for (pid_t tid : tids) {
170 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, tid);
171 int fd = open(buf, O_RDONLY);
174 if (syscall_supported != nullptr)
175 *syscall_supported = true;
176 bool read_ret = ReadFromFD(fd, syscall_data.get(), expected_data.length());
181 if (0 == strncmp(expected_data.c_str(), syscall_data.get(),
182 expected_data.length())) {
189 pid_t FindThreadID(pid_t pid, pid_t ns_tid, bool* ns_pid_supported) {
190 if (ns_pid_supported)
191 *ns_pid_supported = false;
193 std::vector<pid_t> tids;
194 if (!GetTasksForProcess(pid, &tids))
197 for (pid_t tid : tids) {
199 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/status", pid, tid);
201 if (!ReadFileToString(FilePath(buf), &status))
203 StringTokenizer tokenizer(status, "\n");
204 while (tokenizer.GetNext()) {
205 StringPiece value_str(tokenizer.token_piece());
206 if (!value_str.starts_with("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_GE(split_value_str.size(), 2u);
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 second value in the list is the real PID.
217 if (StringToInt(split_value_str[1], &value))