blob: bf504718f06f2accdfb9656c8714a5ba6c536e9f [file] [log] [blame]
thestig@chromium.orgce2f2412012-06-14 08:20:04 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
sgk@google.come1f4a242009-04-22 02:20:10 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +09005#include "base/linux_util.h"
sgk@google.come1f4a242009-04-22 02:20:10 +09006
thestig@chromium.org2feb1c82009-10-29 13:02:55 +09007#include <dirent.h>
8#include <errno.h>
thestig@chromium.org44836d42010-07-17 04:28:17 +09009#include <fcntl.h>
avia6a6a682015-12-27 07:15:14 +090010#include <limits.h>
sgk@google.come1f4a242009-04-22 02:20:10 +090011#include <stdlib.h>
thestig@chromium.org2feb1c82009-10-29 13:02:55 +090012#include <sys/stat.h>
thestig@chromium.org44836d42010-07-17 04:28:17 +090013#include <sys/types.h>
thestig@chromium.org2feb1c82009-10-29 13:02:55 +090014#include <unistd.h>
sgk@google.come1f4a242009-04-22 02:20:10 +090015
dchengcc8e4d82016-04-05 06:25:51 +090016#include <memory>
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +090017#include <vector>
18
19#include "base/command_line.h"
brettw@chromium.org01f3da42014-08-14 05:22:14 +090020#include "base/files/file_util.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090021#include "base/memory/singleton.h"
rsesek@chromium.org687756f2013-07-26 06:38:23 +090022#include "base/process/launch.h"
reveman7294bda2016-09-20 11:10:58 +090023#include "base/strings/string_number_conversions.h"
24#include "base/strings/string_split.h"
avi@chromium.org67d593d2013-06-11 04:06:57 +090025#include "base/strings/string_util.h"
brettw@chromium.orgabe477a2011-01-21 13:55:52 +090026#include "base/synchronization/lock.h"
avia6a6a682015-12-27 07:15:14 +090027#include "build/build_config.h"
mattm@chromium.org625865e2009-07-22 09:22:49 +090028
29namespace {
30
thestig@chromium.org9d0e95c2009-10-22 04:04:17 +090031// Not needed for OS_CHROMEOS.
32#if defined(OS_LINUX)
thestig@chromium.org741196d2009-10-13 11:51:07 +090033enum 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.
satish@chromium.orgce3f4872010-12-14 01:52:35 +090043 static LinuxDistroHelper* GetInstance() {
olli.raula5ba94892015-09-10 20:14:22 +090044 return base::Singleton<LinuxDistroHelper>::get();
thestig@chromium.org741196d2009-10-13 11:51:07 +090045 }
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.orgabe477a2011-01-21 13:55:52 +090056 base::AutoLock scoped_lock(lock_);
thestig@chromium.org741196d2009-10-13 11:51:07 +090057 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.orgabe477a2011-01-21 13:55:52 +090066 base::AutoLock scoped_lock(lock_);
david.mike.futcher@gmail.com9eb2aa52011-04-19 05:07:08 +090067 DCHECK_EQ(STATE_CHECK_STARTED, state_);
thestig@chromium.org741196d2009-10-13 11:51:07 +090068 state_ = STATE_CHECK_FINISHED;
69 }
70
71 private:
brettw@chromium.orgabe477a2011-01-21 13:55:52 +090072 base::Lock lock_;
thestig@chromium.org741196d2009-10-13 11:51:07 +090073 LinuxDistroState state_;
74};
thestig@chromium.org9d0e95c2009-10-22 04:04:17 +090075#endif // if defined(OS_LINUX)
thestig@chromium.org741196d2009-10-13 11:51:07 +090076
reveman7294bda2016-09-20 11:10:58 +090077bool 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.orgb6ba9432010-04-03 10:05:39 +090099} // namespace
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +0900100
sgk@google.come1f4a242009-04-22 02:20:10 +0900101namespace base {
102
mnissler@chromium.org8584b6d2010-08-26 17:55:22 +0900103// Account for the terminating null character.
104static const int kDistroSize = 128 + 1;
105
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +0900106// 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.org8584b6d2010-08-26 17:55:22 +0900108char g_linux_distro[kDistroSize] =
rbyers@chromium.org088a4932012-08-16 05:54:06 +0900109#if defined(OS_CHROMEOS)
thestig@chromium.org9d0e95c2009-10-22 04:04:17 +0900110 "CrOS";
nileshagrawal@chromium.orgea123972012-07-18 10:49:22 +0900111#elif defined(OS_ANDROID)
112 "Android";
thestig@chromium.org9d0e95c2009-10-22 04:04:17 +0900113#else // if defined(OS_LINUX)
114 "Unknown";
115#endif
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +0900116
117std::string GetLinuxDistro() {
nileshagrawal@chromium.orgea123972012-07-18 10:49:22 +0900118#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
mnissler@chromium.org8584b6d2010-08-26 17:55:22 +0900119 return g_linux_distro;
pvalchev@google.com1d919db2010-03-10 16:46:43 +0900120#elif defined(OS_LINUX)
satish@chromium.orgce3f4872010-12-14 01:52:35 +0900121 LinuxDistroHelper* distro_state_singleton = LinuxDistroHelper::GetInstance();
thestig@chromium.org741196d2009-10-13 11:51:07 +0900122 LinuxDistroState state = distro_state_singleton->State();
nileshagrawal@chromium.orgea123972012-07-18 10:49:22 +0900123 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.raula5ba94892015-09-10 20:14:22 +0900135 GetAppOutput(CommandLine(argv), &output);
nileshagrawal@chromium.orgea123972012-07-18 10:49:22 +0900136 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.orga5a2b6e2009-07-17 14:55:51 +0900141 }
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +0900142 }
nileshagrawal@chromium.orgea123972012-07-18 10:49:22 +0900143 distro_state_singleton->CheckFinished();
144 return g_linux_distro;
pvalchev@google.com1d919db2010-03-10 16:46:43 +0900145#else
146 NOTIMPLEMENTED();
robert.nagy@gmail.comea54e462011-10-25 07:05:27 +0900147 return "Unknown";
thestig@chromium.org9d0e95c2009-10-22 04:04:17 +0900148#endif
thestig@chromium.orga5a2b6e2009-07-17 14:55:51 +0900149}
150
mnissler@chromium.org8584b6d2010-08-26 17:55:22 +0900151void SetLinuxDistro(const std::string& distro) {
152 std::string trimmed_distro;
olli.raula5ba94892015-09-10 20:14:22 +0900153 TrimWhitespaceASCII(distro, TRIM_ALL, &trimmed_distro);
154 strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize);
mnissler@chromium.org8584b6d2010-08-26 17:55:22 +0900155}
156
kmixter@chromium.orge406ed72011-06-21 13:21:06 +0900157pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data,
158 bool* syscall_supported) {
kmixter@chromium.orge406ed72011-06-21 13:21:06 +0900159 if (syscall_supported != NULL)
160 *syscall_supported = false;
161
thestig@chromium.org44836d42010-07-17 04:28:17 +0900162 std::vector<pid_t> tids;
reveman7294bda2016-09-20 11:10:58 +0900163 if (!GetTasksForProcess(pid, &tids))
164 return -1;
thestig@chromium.org44836d42010-07-17 04:28:17 +0900165
dchengcc8e4d82016-04-05 06:25:51 +0900166 std::unique_ptr<char[]> syscall_data(new char[expected_data.length()]);
reveman7294bda2016-09-20 11:10:58 +0900167 for (pid_t tid : tids) {
168 char buf[256];
169 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, tid);
thestig@chromium.org44836d42010-07-17 04:28:17 +0900170 int fd = open(buf, O_RDONLY);
171 if (fd < 0)
172 continue;
kmixter@chromium.orge406ed72011-06-21 13:21:06 +0900173 if (syscall_supported != NULL)
174 *syscall_supported = true;
brettw@chromium.org2873d9b2013-11-28 08:22:08 +0900175 bool read_ret = ReadFromFD(fd, syscall_data.get(), expected_data.length());
thestig@chromium.org44836d42010-07-17 04:28:17 +0900176 close(fd);
177 if (!read_ret)
178 continue;
179
180 if (0 == strncmp(expected_data.c_str(), syscall_data.get(),
181 expected_data.length())) {
reveman7294bda2016-09-20 11:10:58 +0900182 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 }
thestig@chromium.org44836d42010-07-17 04:28:17 +0900221 }
222 }
223 return -1;
224}
225
sgk@google.come1f4a242009-04-22 02:20:10 +0900226} // namespace base