Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / base / linux_util.cc
blobd6cd504032080bc0a4b42fe274e8b1f76b91dd40
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"
7 #include <dirent.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <stdlib.h>
11 #include <sys/stat.h>
12 #include <sys/types.h>
13 #include <unistd.h>
15 #include <vector>
17 #include "base/command_line.h"
18 #include "base/files/file_util.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/memory/singleton.h"
21 #include "base/process/launch.h"
22 #include "base/strings/string_util.h"
23 #include "base/synchronization/lock.h"
25 namespace {
27 // Not needed for OS_CHROMEOS.
28 #if defined(OS_LINUX)
29 enum LinuxDistroState {
30 STATE_DID_NOT_CHECK = 0,
31 STATE_CHECK_STARTED = 1,
32 STATE_CHECK_FINISHED = 2,
35 // Helper class for GetLinuxDistro().
36 class LinuxDistroHelper {
37 public:
38 // Retrieves the Singleton.
39 static LinuxDistroHelper* GetInstance() {
40 return Singleton<LinuxDistroHelper>::get();
43 // The simple state machine goes from:
44 // STATE_DID_NOT_CHECK -> STATE_CHECK_STARTED -> STATE_CHECK_FINISHED.
45 LinuxDistroHelper() : state_(STATE_DID_NOT_CHECK) {}
46 ~LinuxDistroHelper() {}
48 // Retrieve the current state, if we're in STATE_DID_NOT_CHECK,
49 // we automatically move to STATE_CHECK_STARTED so nobody else will
50 // do the check.
51 LinuxDistroState State() {
52 base::AutoLock scoped_lock(lock_);
53 if (STATE_DID_NOT_CHECK == state_) {
54 state_ = STATE_CHECK_STARTED;
55 return STATE_DID_NOT_CHECK;
57 return state_;
60 // Indicate the check finished, move to STATE_CHECK_FINISHED.
61 void CheckFinished() {
62 base::AutoLock scoped_lock(lock_);
63 DCHECK_EQ(STATE_CHECK_STARTED, state_);
64 state_ = STATE_CHECK_FINISHED;
67 private:
68 base::Lock lock_;
69 LinuxDistroState state_;
71 #endif // if defined(OS_LINUX)
73 } // namespace
75 namespace base {
77 // Account for the terminating null character.
78 static const int kDistroSize = 128 + 1;
80 // We use this static string to hold the Linux distro info. If we
81 // crash, the crash handler code will send this in the crash dump.
82 char g_linux_distro[kDistroSize] =
83 #if defined(OS_CHROMEOS)
84 "CrOS";
85 #elif defined(OS_ANDROID)
86 "Android";
87 #else // if defined(OS_LINUX)
88 "Unknown";
89 #endif
91 std::string GetLinuxDistro() {
92 #if defined(OS_CHROMEOS) || defined(OS_ANDROID)
93 return g_linux_distro;
94 #elif defined(OS_LINUX)
95 LinuxDistroHelper* distro_state_singleton = LinuxDistroHelper::GetInstance();
96 LinuxDistroState state = distro_state_singleton->State();
97 if (STATE_CHECK_FINISHED == state)
98 return g_linux_distro;
99 if (STATE_CHECK_STARTED == state)
100 return "Unknown"; // Don't wait for other thread to finish.
101 DCHECK_EQ(state, STATE_DID_NOT_CHECK);
102 // We do this check only once per process. If it fails, there's
103 // little reason to believe it will work if we attempt to run
104 // lsb_release again.
105 std::vector<std::string> argv;
106 argv.push_back("lsb_release");
107 argv.push_back("-d");
108 std::string output;
109 base::GetAppOutput(CommandLine(argv), &output);
110 if (output.length() > 0) {
111 // lsb_release -d should return: Description:<tab>Distro Info
112 const char field[] = "Description:\t";
113 if (output.compare(0, strlen(field), field) == 0) {
114 SetLinuxDistro(output.substr(strlen(field)));
117 distro_state_singleton->CheckFinished();
118 return g_linux_distro;
119 #else
120 NOTIMPLEMENTED();
121 return "Unknown";
122 #endif
125 void SetLinuxDistro(const std::string& distro) {
126 std::string trimmed_distro;
127 base::TrimWhitespaceASCII(distro, base::TRIM_ALL, &trimmed_distro);
128 base::strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize);
131 pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data,
132 bool* syscall_supported) {
133 char buf[256];
134 snprintf(buf, sizeof(buf), "/proc/%d/task", pid);
136 if (syscall_supported != NULL)
137 *syscall_supported = false;
139 DIR* task = opendir(buf);
140 if (!task) {
141 DLOG(WARNING) << "Cannot open " << buf;
142 return -1;
145 std::vector<pid_t> tids;
146 struct dirent* dent;
147 while ((dent = readdir(task))) {
148 char* endptr;
149 const unsigned long int tid_ul = strtoul(dent->d_name, &endptr, 10);
150 if (tid_ul == ULONG_MAX || *endptr)
151 continue;
152 tids.push_back(tid_ul);
154 closedir(task);
156 scoped_ptr<char[]> syscall_data(new char[expected_data.length()]);
157 for (std::vector<pid_t>::const_iterator
158 i = tids.begin(); i != tids.end(); ++i) {
159 const pid_t current_tid = *i;
160 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, current_tid);
161 int fd = open(buf, O_RDONLY);
162 if (fd < 0)
163 continue;
164 if (syscall_supported != NULL)
165 *syscall_supported = true;
166 bool read_ret = ReadFromFD(fd, syscall_data.get(), expected_data.length());
167 close(fd);
168 if (!read_ret)
169 continue;
171 if (0 == strncmp(expected_data.c_str(), syscall_data.get(),
172 expected_data.length())) {
173 return current_tid;
176 return -1;
179 } // namespace base