blob: bf504718f06f2accdfb9656c8714a5ba6c536e9f [file] [log] [blame]
[email protected]46e9acad2012-06-13 23:20:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]75ae5422009-04-21 17:20:102// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]912c6452009-07-17 05:55:515#include "base/linux_util.h"
[email protected]75ae5422009-04-21 17:20:106
[email protected]85ebe8f2009-10-29 04:02:557#include <dirent.h>
8#include <errno.h>
[email protected]662183142010-07-16 19:28:179#include <fcntl.h>
avi9b6f42932015-12-26 22:15:1410#include <limits.h>
[email protected]75ae5422009-04-21 17:20:1011#include <stdlib.h>
[email protected]85ebe8f2009-10-29 04:02:5512#include <sys/stat.h>
[email protected]662183142010-07-16 19:28:1713#include <sys/types.h>
[email protected]85ebe8f2009-10-29 04:02:5514#include <unistd.h>
[email protected]75ae5422009-04-21 17:20:1015
dcheng093de9b2016-04-04 21:25:5116#include <memory>
[email protected]912c6452009-07-17 05:55:5117#include <vector>
18
19#include "base/command_line.h"
[email protected]e3177dd52014-08-13 20:22:1420#include "base/files/file_util.h"
[email protected]3b63f8f42011-03-28 01:54:1521#include "base/memory/singleton.h"
[email protected]dd4b51262013-07-25 21:38:2322#include "base/process/launch.h"
reveman7b97c322016-09-20 02:10:5823#include "base/strings/string_number_conversions.h"
24#include "base/strings/string_split.h"
[email protected]d529cb02013-06-10 19:06:5725#include "base/strings/string_util.h"
[email protected]20305ec2011-01-21 04:55:5226#include "base/synchronization/lock.h"
avi9b6f42932015-12-26 22:15:1427#include "build/build_config.h"
[email protected]87fc1682009-07-22 00:22:4928
29namespace {
30
[email protected]78f6f512009-10-21 19:04:1731// Not needed for OS_CHROMEOS.
32#if defined(OS_LINUX)
[email protected]19467c02009-10-13 02:51:0733enum LinuxDistroState {
34 STATE_DID_NOT_CHECK = 0,
35 STATE_CHECK_STARTED = 1,
36 STATE_CHECK_FINISHED = 2,
37};
38
39// Helper class for GetLinuxDistro().
40class LinuxDistroHelper {
41 public:
42 // Retrieves the Singleton.
[email protected]dc8caba2010-12-13 16:52:3543 static LinuxDistroHelper* GetInstance() {
olli.raula36aa8be2015-09-10 11:14:2244 return base::Singleton<LinuxDistroHelper>::get();
[email protected]19467c02009-10-13 02:51:0745 }
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() {
[email protected]20305ec2011-01-21 04:55:5256 base::AutoLock scoped_lock(lock_);
[email protected]19467c02009-10-13 02:51:0757 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() {
[email protected]20305ec2011-01-21 04:55:5266 base::AutoLock scoped_lock(lock_);
[email protected]60ea6052011-04-18 20:07:0867 DCHECK_EQ(STATE_CHECK_STARTED, state_);
[email protected]19467c02009-10-13 02:51:0768 state_ = STATE_CHECK_FINISHED;
69 }
70
71 private:
[email protected]20305ec2011-01-21 04:55:5272 base::Lock lock_;
[email protected]19467c02009-10-13 02:51:0773 LinuxDistroState state_;
74};
[email protected]78f6f512009-10-21 19:04:1775#endif // if defined(OS_LINUX)
[email protected]19467c02009-10-13 02:51:0776
reveman7b97c322016-09-20 02:10:5877bool 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
[email protected]9bc8cff2010-04-03 01:05:3999} // namespace
[email protected]912c6452009-07-17 05:55:51100
[email protected]75ae5422009-04-21 17:20:10101namespace base {
102
[email protected]6dde9d72010-08-26 08:55:22103// Account for the terminating null character.
104static const int kDistroSize = 128 + 1;
105
[email protected]912c6452009-07-17 05:55:51106// 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.
[email protected]6dde9d72010-08-26 08:55:22108char g_linux_distro[kDistroSize] =
[email protected]cbf0d1d2012-08-15 20:54:06109#if defined(OS_CHROMEOS)
[email protected]78f6f512009-10-21 19:04:17110 "CrOS";
[email protected]84a1ab5a2012-07-18 01:49:22111#elif defined(OS_ANDROID)
112 "Android";
[email protected]78f6f512009-10-21 19:04:17113#else // if defined(OS_LINUX)
114 "Unknown";
115#endif
[email protected]912c6452009-07-17 05:55:51116
117std::string GetLinuxDistro() {
[email protected]84a1ab5a2012-07-18 01:49:22118#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]6dde9d72010-08-26 08:55:22119 return g_linux_distro;
[email protected]66700d42010-03-10 07:46:43120#elif defined(OS_LINUX)
[email protected]dc8caba2010-12-13 16:52:35121 LinuxDistroHelper* distro_state_singleton = LinuxDistroHelper::GetInstance();
[email protected]19467c02009-10-13 02:51:07122 LinuxDistroState state = distro_state_singleton->State();
[email protected]84a1ab5a2012-07-18 01:49:22123 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.raula36aa8be2015-09-10 11:14:22135 GetAppOutput(CommandLine(argv), &output);
[email protected]84a1ab5a2012-07-18 01:49:22136 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)));
[email protected]912c6452009-07-17 05:55:51141 }
[email protected]912c6452009-07-17 05:55:51142 }
[email protected]84a1ab5a2012-07-18 01:49:22143 distro_state_singleton->CheckFinished();
144 return g_linux_distro;
[email protected]66700d42010-03-10 07:46:43145#else
146 NOTIMPLEMENTED();
[email protected]167ec822011-10-24 22:05:27147 return "Unknown";
[email protected]78f6f512009-10-21 19:04:17148#endif
[email protected]912c6452009-07-17 05:55:51149}
150
[email protected]6dde9d72010-08-26 08:55:22151void SetLinuxDistro(const std::string& distro) {
152 std::string trimmed_distro;
olli.raula36aa8be2015-09-10 11:14:22153 TrimWhitespaceASCII(distro, TRIM_ALL, &trimmed_distro);
154 strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize);
[email protected]6dde9d72010-08-26 08:55:22155}
156
[email protected]cb7d53e2011-06-21 04:21:06157pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data,
158 bool* syscall_supported) {
[email protected]cb7d53e2011-06-21 04:21:06159 if (syscall_supported != NULL)
160 *syscall_supported = false;
161
[email protected]662183142010-07-16 19:28:17162 std::vector<pid_t> tids;
reveman7b97c322016-09-20 02:10:58163 if (!GetTasksForProcess(pid, &tids))
164 return -1;
[email protected]662183142010-07-16 19:28:17165
dcheng093de9b2016-04-04 21:25:51166 std::unique_ptr<char[]> syscall_data(new char[expected_data.length()]);
reveman7b97c322016-09-20 02:10:58167 for (pid_t tid : tids) {
168 char buf[256];
169 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, tid);
[email protected]662183142010-07-16 19:28:17170 int fd = open(buf, O_RDONLY);
171 if (fd < 0)
172 continue;
[email protected]cb7d53e2011-06-21 04:21:06173 if (syscall_supported != NULL)
174 *syscall_supported = true;
[email protected]b264eab2013-11-27 23:22:08175 bool read_ret = ReadFromFD(fd, syscall_data.get(), expected_data.length());
[email protected]662183142010-07-16 19:28:17176 close(fd);
177 if (!read_ret)
178 continue;
179
180 if (0 == strncmp(expected_data.c_str(), syscall_data.get(),
181 expected_data.length())) {
reveman7b97c322016-09-20 02:10:58182 return tid;
183 }
184 }
185 return -1;
186}
187
188pid_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 }
[email protected]662183142010-07-16 19:28:17221 }
222 }
223 return -1;
224}
225
[email protected]75ae5422009-04-21 17:20:10226} // namespace base