blob: a24f3111f3d33dc766ead29125c7dbe9f252e0a2 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 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]9e9b6e8e2009-12-02 08:45:0110#include <glib.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
[email protected]912c6452009-07-17 05:55:5116#include <vector>
17
18#include "base/command_line.h"
[email protected]662183142010-07-16 19:28:1719#include "base/file_util.h"
[email protected]3b63f8f42011-03-28 01:54:1520#include "base/memory/scoped_ptr.h"
21#include "base/memory/singleton.h"
[email protected]9e9b6e8e2009-12-02 08:45:0122#include "base/path_service.h"
[email protected]912c6452009-07-17 05:55:5123#include "base/process_util.h"
[email protected]87fc1682009-07-22 00:22:4924#include "base/string_util.h"
[email protected]20305ec2011-01-21 04:55:5225#include "base/synchronization/lock.h"
[email protected]87fc1682009-07-22 00:22:4926
27namespace {
28
[email protected]78f6f512009-10-21 19:04:1729// Not needed for OS_CHROMEOS.
30#if defined(OS_LINUX)
[email protected]19467c02009-10-13 02:51:0731enum LinuxDistroState {
32 STATE_DID_NOT_CHECK = 0,
33 STATE_CHECK_STARTED = 1,
34 STATE_CHECK_FINISHED = 2,
35};
36
37// Helper class for GetLinuxDistro().
38class LinuxDistroHelper {
39 public:
40 // Retrieves the Singleton.
[email protected]dc8caba2010-12-13 16:52:3541 static LinuxDistroHelper* GetInstance() {
[email protected]19467c02009-10-13 02:51:0742 return Singleton<LinuxDistroHelper>::get();
43 }
44
45 // The simple state machine goes from:
46 // STATE_DID_NOT_CHECK -> STATE_CHECK_STARTED -> STATE_CHECK_FINISHED.
47 LinuxDistroHelper() : state_(STATE_DID_NOT_CHECK) {}
48 ~LinuxDistroHelper() {}
49
50 // Retrieve the current state, if we're in STATE_DID_NOT_CHECK,
51 // we automatically move to STATE_CHECK_STARTED so nobody else will
52 // do the check.
53 LinuxDistroState State() {
[email protected]20305ec2011-01-21 04:55:5254 base::AutoLock scoped_lock(lock_);
[email protected]19467c02009-10-13 02:51:0755 if (STATE_DID_NOT_CHECK == state_) {
56 state_ = STATE_CHECK_STARTED;
57 return STATE_DID_NOT_CHECK;
58 }
59 return state_;
60 }
61
62 // Indicate the check finished, move to STATE_CHECK_FINISHED.
63 void CheckFinished() {
[email protected]20305ec2011-01-21 04:55:5264 base::AutoLock scoped_lock(lock_);
[email protected]60ea6052011-04-18 20:07:0865 DCHECK_EQ(STATE_CHECK_STARTED, state_);
[email protected]19467c02009-10-13 02:51:0766 state_ = STATE_CHECK_FINISHED;
67 }
68
69 private:
[email protected]20305ec2011-01-21 04:55:5270 base::Lock lock_;
[email protected]19467c02009-10-13 02:51:0771 LinuxDistroState state_;
72};
[email protected]78f6f512009-10-21 19:04:1773#endif // if defined(OS_LINUX)
[email protected]19467c02009-10-13 02:51:0774
[email protected]85ebe8f2009-10-29 04:02:5575// expected prefix of the target of the /proc/self/fd/%d link for a socket
76static const char kSocketLinkPrefix[] = "socket:[";
77
78// Parse a symlink in /proc/pid/fd/$x and return the inode number of the
79// socket.
80// inode_out: (output) set to the inode number on success
81// path: e.g. /proc/1234/fd/5 (must be a UNIX domain socket descriptor)
82// log: if true, log messages about failure details
83bool ProcPathGetInode(ino_t* inode_out, const char* path, bool log = false) {
84 DCHECK(inode_out);
85 DCHECK(path);
86
87 char buf[256];
88 const ssize_t n = readlink(path, buf, sizeof(buf) - 1);
89 if (n == -1) {
90 if (log) {
91 LOG(WARNING) << "Failed to read the inode number for a socket from /proc"
92 "(" << errno << ")";
93 }
94 return false;
95 }
96 buf[n] = 0;
97
98 if (memcmp(kSocketLinkPrefix, buf, sizeof(kSocketLinkPrefix) - 1)) {
99 if (log) {
100 LOG(WARNING) << "The descriptor passed from the crashing process wasn't a"
101 " UNIX domain socket.";
102 }
103 return false;
104 }
105
106 char *endptr;
107 const unsigned long long int inode_ul =
108 strtoull(buf + sizeof(kSocketLinkPrefix) - 1, &endptr, 10);
109 if (*endptr != ']')
110 return false;
111
112 if (inode_ul == ULLONG_MAX) {
113 if (log) {
114 LOG(WARNING) << "Failed to parse a socket's inode number: the number was "
115 "too large. Please report this bug: " << buf;
116 }
117 return false;
118 }
119
120 *inode_out = inode_ul;
121 return true;
122}
123
[email protected]9bc8cff2010-04-03 01:05:39124} // namespace
[email protected]912c6452009-07-17 05:55:51125
[email protected]75ae5422009-04-21 17:20:10126namespace base {
127
[email protected]6dde9d72010-08-26 08:55:22128// Account for the terminating null character.
129static const int kDistroSize = 128 + 1;
130
[email protected]912c6452009-07-17 05:55:51131// We use this static string to hold the Linux distro info. If we
132// crash, the crash handler code will send this in the crash dump.
[email protected]6dde9d72010-08-26 08:55:22133char g_linux_distro[kDistroSize] =
[email protected]78f6f512009-10-21 19:04:17134#if defined(OS_CHROMEOS)
135 "CrOS";
136#else // if defined(OS_LINUX)
137 "Unknown";
138#endif
[email protected]912c6452009-07-17 05:55:51139
140std::string GetLinuxDistro() {
[email protected]78f6f512009-10-21 19:04:17141#if defined(OS_CHROMEOS)
[email protected]6dde9d72010-08-26 08:55:22142 return g_linux_distro;
[email protected]66700d42010-03-10 07:46:43143#elif defined(OS_LINUX)
[email protected]dc8caba2010-12-13 16:52:35144 LinuxDistroHelper* distro_state_singleton = LinuxDistroHelper::GetInstance();
[email protected]19467c02009-10-13 02:51:07145 LinuxDistroState state = distro_state_singleton->State();
146 if (STATE_DID_NOT_CHECK == state) {
147 // We do this check only once per process. If it fails, there's
148 // little reason to believe it will work if we attempt to run
149 // lsb_release again.
[email protected]912c6452009-07-17 05:55:51150 std::vector<std::string> argv;
151 argv.push_back("lsb_release");
152 argv.push_back("-d");
153 std::string output;
154 base::GetAppOutput(CommandLine(argv), &output);
155 if (output.length() > 0) {
156 // lsb_release -d should return: Description:<tab>Distro Info
157 static const std::string field = "Description:\t";
[email protected]94aa5bb12009-10-21 22:09:33158 if (output.compare(0, field.length(), field) == 0) {
[email protected]6dde9d72010-08-26 08:55:22159 SetLinuxDistro(output.substr(field.length()));
[email protected]94aa5bb12009-10-21 22:09:33160 }
[email protected]912c6452009-07-17 05:55:51161 }
[email protected]19467c02009-10-13 02:51:07162 distro_state_singleton->CheckFinished();
[email protected]6dde9d72010-08-26 08:55:22163 return g_linux_distro;
[email protected]19467c02009-10-13 02:51:07164 } else if (STATE_CHECK_STARTED == state) {
165 // If the distro check above is in progress in some other thread, we're
166 // not going to wait for the results.
167 return "Unknown";
168 } else {
169 // In STATE_CHECK_FINISHED, no more writing to |linux_distro|.
[email protected]6dde9d72010-08-26 08:55:22170 return g_linux_distro;
[email protected]912c6452009-07-17 05:55:51171 }
[email protected]66700d42010-03-10 07:46:43172#else
173 NOTIMPLEMENTED();
[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) {
203 LOG(WARNING) << "Cannot open /proc";
204 return false;
205 }
206
207 std::vector<pid_t> pids;
208
209 struct dirent* dent;
210 while ((dent = readdir(proc))) {
211 char *endptr;
212 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
234 ino_t fd_inode;
235 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) {
265 LOG(WARNING) << "Cannot open " << buf;
266 return -1;
267 }
268
269 std::vector<pid_t> tids;
270 struct dirent* dent;
271 while ((dent = readdir(task))) {
272 char *endptr;
273 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
280 scoped_array<char> syscall_data(new char[expected_data.length()]);
281 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