blob: 5f9ecd43ab11b1199b4a55a1556e64a433e8431a [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>
[email protected]75ae5422009-04-21 17:20:1010#include <stdlib.h>
[email protected]85ebe8f2009-10-29 04:02:5511#include <sys/stat.h>
[email protected]662183142010-07-16 19:28:1712#include <sys/types.h>
[email protected]85ebe8f2009-10-29 04:02:5513#include <unistd.h>
[email protected]75ae5422009-04-21 17:20:1014
[email protected]912c6452009-07-17 05:55:5115#include <vector>
16
17#include "base/command_line.h"
[email protected]662183142010-07-16 19:28:1718#include "base/file_util.h"
[email protected]3b63f8f42011-03-28 01:54:1519#include "base/memory/scoped_ptr.h"
20#include "base/memory/singleton.h"
[email protected]9e9b6e8e2009-12-02 08:45:0121#include "base/path_service.h"
[email protected]dd4b51262013-07-25 21:38:2322#include "base/process/launch.h"
[email protected]d529cb02013-06-10 19:06:5723#include "base/strings/string_util.h"
[email protected]20305ec2011-01-21 04:55:5224#include "base/synchronization/lock.h"
[email protected]87fc1682009-07-22 00:22:4925
26namespace {
27
[email protected]78f6f512009-10-21 19:04:1728// Not needed for OS_CHROMEOS.
29#if defined(OS_LINUX)
[email protected]19467c02009-10-13 02:51:0730enum LinuxDistroState {
31 STATE_DID_NOT_CHECK = 0,
32 STATE_CHECK_STARTED = 1,
33 STATE_CHECK_FINISHED = 2,
34};
35
36// Helper class for GetLinuxDistro().
37class LinuxDistroHelper {
38 public:
39 // Retrieves the Singleton.
[email protected]dc8caba2010-12-13 16:52:3540 static LinuxDistroHelper* GetInstance() {
[email protected]19467c02009-10-13 02:51:0741 return Singleton<LinuxDistroHelper>::get();
42 }
43
44 // The simple state machine goes from:
45 // STATE_DID_NOT_CHECK -> STATE_CHECK_STARTED -> STATE_CHECK_FINISHED.
46 LinuxDistroHelper() : state_(STATE_DID_NOT_CHECK) {}
47 ~LinuxDistroHelper() {}
48
49 // Retrieve the current state, if we're in STATE_DID_NOT_CHECK,
50 // we automatically move to STATE_CHECK_STARTED so nobody else will
51 // do the check.
52 LinuxDistroState State() {
[email protected]20305ec2011-01-21 04:55:5253 base::AutoLock scoped_lock(lock_);
[email protected]19467c02009-10-13 02:51:0754 if (STATE_DID_NOT_CHECK == state_) {
55 state_ = STATE_CHECK_STARTED;
56 return STATE_DID_NOT_CHECK;
57 }
58 return state_;
59 }
60
61 // Indicate the check finished, move to STATE_CHECK_FINISHED.
62 void CheckFinished() {
[email protected]20305ec2011-01-21 04:55:5263 base::AutoLock scoped_lock(lock_);
[email protected]60ea6052011-04-18 20:07:0864 DCHECK_EQ(STATE_CHECK_STARTED, state_);
[email protected]19467c02009-10-13 02:51:0765 state_ = STATE_CHECK_FINISHED;
66 }
67
68 private:
[email protected]20305ec2011-01-21 04:55:5269 base::Lock lock_;
[email protected]19467c02009-10-13 02:51:0770 LinuxDistroState state_;
71};
[email protected]78f6f512009-10-21 19:04:1772#endif // if defined(OS_LINUX)
[email protected]19467c02009-10-13 02:51:0773
[email protected]85ebe8f2009-10-29 04:02:5574// expected prefix of the target of the /proc/self/fd/%d link for a socket
[email protected]da9ccfb2012-01-28 00:34:4075const char kSocketLinkPrefix[] = "socket:[";
[email protected]85ebe8f2009-10-29 04:02:5576
77// Parse a symlink in /proc/pid/fd/$x and return the inode number of the
78// socket.
79// inode_out: (output) set to the inode number on success
80// path: e.g. /proc/1234/fd/5 (must be a UNIX domain socket descriptor)
81// log: if true, log messages about failure details
82bool ProcPathGetInode(ino_t* inode_out, const char* path, bool log = false) {
83 DCHECK(inode_out);
84 DCHECK(path);
85
86 char buf[256];
87 const ssize_t n = readlink(path, buf, sizeof(buf) - 1);
88 if (n == -1) {
89 if (log) {
[email protected]a42d4632011-10-26 21:48:0090 DLOG(WARNING) << "Failed to read the inode number for a socket from /proc"
[email protected]85ebe8f2009-10-29 04:02:5591 "(" << errno << ")";
92 }
93 return false;
94 }
95 buf[n] = 0;
96
97 if (memcmp(kSocketLinkPrefix, buf, sizeof(kSocketLinkPrefix) - 1)) {
98 if (log) {
[email protected]a42d4632011-10-26 21:48:0099 DLOG(WARNING) << "The descriptor passed from the crashing process wasn't "
100 " a UNIX domain socket.";
[email protected]85ebe8f2009-10-29 04:02:55101 }
102 return false;
103 }
104
[email protected]46e9acad2012-06-13 23:20:04105 char* endptr;
[email protected]85ebe8f2009-10-29 04:02:55106 const unsigned long long int inode_ul =
107 strtoull(buf + sizeof(kSocketLinkPrefix) - 1, &endptr, 10);
108 if (*endptr != ']')
109 return false;
110
111 if (inode_ul == ULLONG_MAX) {
112 if (log) {
[email protected]a42d4632011-10-26 21:48:00113 DLOG(WARNING) << "Failed to parse a socket's inode number: the number "
114 "was too large. Please report this bug: " << buf;
[email protected]85ebe8f2009-10-29 04:02:55115 }
116 return false;
117 }
118
119 *inode_out = inode_ul;
120 return true;
121}
122
[email protected]9bc8cff2010-04-03 01:05:39123} // namespace
[email protected]912c6452009-07-17 05:55:51124
[email protected]75ae5422009-04-21 17:20:10125namespace base {
126
[email protected]46e9acad2012-06-13 23:20:04127const char kFindInodeSwitch[] = "--find-inode";
128
[email protected]6dde9d72010-08-26 08:55:22129// Account for the terminating null character.
130static const int kDistroSize = 128 + 1;
131
[email protected]912c6452009-07-17 05:55:51132// We use this static string to hold the Linux distro info. If we
133// crash, the crash handler code will send this in the crash dump.
[email protected]6dde9d72010-08-26 08:55:22134char g_linux_distro[kDistroSize] =
[email protected]cbf0d1d2012-08-15 20:54:06135#if defined(OS_CHROMEOS)
[email protected]78f6f512009-10-21 19:04:17136 "CrOS";
[email protected]84a1ab5a2012-07-18 01:49:22137#elif defined(OS_ANDROID)
138 "Android";
[email protected]78f6f512009-10-21 19:04:17139#else // if defined(OS_LINUX)
140 "Unknown";
141#endif
[email protected]912c6452009-07-17 05:55:51142
143std::string GetLinuxDistro() {
[email protected]84a1ab5a2012-07-18 01:49:22144#if defined(OS_CHROMEOS) || defined(OS_ANDROID)
[email protected]6dde9d72010-08-26 08:55:22145 return g_linux_distro;
[email protected]66700d42010-03-10 07:46:43146#elif defined(OS_LINUX)
[email protected]dc8caba2010-12-13 16:52:35147 LinuxDistroHelper* distro_state_singleton = LinuxDistroHelper::GetInstance();
[email protected]19467c02009-10-13 02:51:07148 LinuxDistroState state = distro_state_singleton->State();
[email protected]84a1ab5a2012-07-18 01:49:22149 if (STATE_CHECK_FINISHED == state)
150 return g_linux_distro;
151 if (STATE_CHECK_STARTED == state)
152 return "Unknown"; // Don't wait for other thread to finish.
153 DCHECK_EQ(state, STATE_DID_NOT_CHECK);
154 // We do this check only once per process. If it fails, there's
155 // little reason to believe it will work if we attempt to run
156 // lsb_release again.
157 std::vector<std::string> argv;
158 argv.push_back("lsb_release");
159 argv.push_back("-d");
160 std::string output;
161 base::GetAppOutput(CommandLine(argv), &output);
162 if (output.length() > 0) {
163 // lsb_release -d should return: Description:<tab>Distro Info
164 const char field[] = "Description:\t";
165 if (output.compare(0, strlen(field), field) == 0) {
166 SetLinuxDistro(output.substr(strlen(field)));
[email protected]912c6452009-07-17 05:55:51167 }
[email protected]912c6452009-07-17 05:55:51168 }
[email protected]84a1ab5a2012-07-18 01:49:22169 distro_state_singleton->CheckFinished();
170 return g_linux_distro;
[email protected]66700d42010-03-10 07:46:43171#else
172 NOTIMPLEMENTED();
[email protected]167ec822011-10-24 22:05:27173 return "Unknown";
[email protected]78f6f512009-10-21 19:04:17174#endif
[email protected]912c6452009-07-17 05:55:51175}
176
[email protected]6dde9d72010-08-26 08:55:22177void SetLinuxDistro(const std::string& distro) {
178 std::string trimmed_distro;
179 TrimWhitespaceASCII(distro, TRIM_ALL, &trimmed_distro);
180 base::strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize);
181}
182
[email protected]85ebe8f2009-10-29 04:02:55183bool FileDescriptorGetInode(ino_t* inode_out, int fd) {
184 DCHECK(inode_out);
185
186 struct stat buf;
187 if (fstat(fd, &buf) < 0)
188 return false;
189
190 if (!S_ISSOCK(buf.st_mode))
191 return false;
192
193 *inode_out = buf.st_ino;
194 return true;
195}
196
197bool FindProcessHoldingSocket(pid_t* pid_out, ino_t socket_inode) {
198 DCHECK(pid_out);
199 bool already_found = false;
200
201 DIR* proc = opendir("/proc");
202 if (!proc) {
[email protected]a42d4632011-10-26 21:48:00203 DLOG(WARNING) << "Cannot open /proc";
[email protected]85ebe8f2009-10-29 04:02:55204 return false;
205 }
206
207 std::vector<pid_t> pids;
208
209 struct dirent* dent;
210 while ((dent = readdir(proc))) {
[email protected]46e9acad2012-06-13 23:20:04211 char* endptr;
[email protected]85ebe8f2009-10-29 04:02:55212 const unsigned long int pid_ul = strtoul(dent->d_name, &endptr, 10);
213 if (pid_ul == ULONG_MAX || *endptr)
214 continue;
215 pids.push_back(pid_ul);
216 }
217 closedir(proc);
218
219 for (std::vector<pid_t>::const_iterator
220 i = pids.begin(); i != pids.end(); ++i) {
221 const pid_t current_pid = *i;
222 char buf[256];
223 snprintf(buf, sizeof(buf), "/proc/%d/fd", current_pid);
224 DIR* fd = opendir(buf);
225 if (!fd)
226 continue;
227
228 while ((dent = readdir(fd))) {
229 if (snprintf(buf, sizeof(buf), "/proc/%d/fd/%s", current_pid,
230 dent->d_name) >= static_cast<int>(sizeof(buf))) {
231 continue;
232 }
233
[email protected]b8453cc2013-06-06 23:49:26234 ino_t fd_inode = static_cast<ino_t>(-1);
[email protected]85ebe8f2009-10-29 04:02:55235 if (ProcPathGetInode(&fd_inode, buf)) {
236 if (fd_inode == socket_inode) {
237 if (already_found) {
238 closedir(fd);
239 return false;
240 }
241
242 already_found = true;
243 *pid_out = current_pid;
244 break;
245 }
246 }
247 }
248
249 closedir(fd);
250 }
251
252 return already_found;
253}
254
[email protected]cb7d53e2011-06-21 04:21:06255pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data,
256 bool* syscall_supported) {
[email protected]662183142010-07-16 19:28:17257 char buf[256];
258 snprintf(buf, sizeof(buf), "/proc/%d/task", pid);
[email protected]cb7d53e2011-06-21 04:21:06259
260 if (syscall_supported != NULL)
261 *syscall_supported = false;
262
[email protected]662183142010-07-16 19:28:17263 DIR* task = opendir(buf);
264 if (!task) {
[email protected]a42d4632011-10-26 21:48:00265 DLOG(WARNING) << "Cannot open " << buf;
[email protected]662183142010-07-16 19:28:17266 return -1;
267 }
268
269 std::vector<pid_t> tids;
270 struct dirent* dent;
271 while ((dent = readdir(task))) {
[email protected]46e9acad2012-06-13 23:20:04272 char* endptr;
[email protected]662183142010-07-16 19:28:17273 const unsigned long int tid_ul = strtoul(dent->d_name, &endptr, 10);
274 if (tid_ul == ULONG_MAX || *endptr)
275 continue;
276 tids.push_back(tid_ul);
277 }
278 closedir(task);
279
[email protected]604eb052013-01-18 14:21:58280 scoped_ptr<char[]> syscall_data(new char[expected_data.length()]);
[email protected]662183142010-07-16 19:28:17281 for (std::vector<pid_t>::const_iterator
282 i = tids.begin(); i != tids.end(); ++i) {
283 const pid_t current_tid = *i;
284 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, current_tid);
285 int fd = open(buf, O_RDONLY);
286 if (fd < 0)
287 continue;
[email protected]cb7d53e2011-06-21 04:21:06288 if (syscall_supported != NULL)
289 *syscall_supported = true;
[email protected]662183142010-07-16 19:28:17290 bool read_ret =
291 file_util::ReadFromFD(fd, syscall_data.get(), expected_data.length());
292 close(fd);
293 if (!read_ret)
294 continue;
295
296 if (0 == strncmp(expected_data.c_str(), syscall_data.get(),
297 expected_data.length())) {
298 return current_tid;
299 }
300 }
301 return -1;
302}
303
[email protected]75ae5422009-04-21 17:20:10304} // namespace base