OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/linux_util.h" | 5 #include "base/linux_util.h" |
6 | 6 |
7 #include <dirent.h> | 7 #include <dirent.h> |
8 #include <errno.h> | 8 #include <errno.h> |
9 #include <fcntl.h> | 9 #include <fcntl.h> |
10 #include <limits.h> | 10 #include <limits.h> |
11 #include <stdlib.h> | 11 #include <stdlib.h> |
12 #include <sys/stat.h> | 12 #include <sys/stat.h> |
13 #include <sys/types.h> | 13 #include <sys/types.h> |
14 #include <unistd.h> | 14 #include <unistd.h> |
15 | 15 |
16 #include <memory> | 16 #include <memory> |
17 #include <vector> | 17 #include <vector> |
18 | 18 |
19 #include "base/command_line.h" | 19 #include "base/command_line.h" |
20 #include "base/files/file_util.h" | 20 #include "base/files/file_util.h" |
21 #include "base/memory/singleton.h" | 21 #include "base/memory/singleton.h" |
22 #include "base/process/launch.h" | 22 #include "base/process/launch.h" |
| 23 #include "base/strings/string_number_conversions.h" |
| 24 #include "base/strings/string_split.h" |
23 #include "base/strings/string_util.h" | 25 #include "base/strings/string_util.h" |
24 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
25 #include "build/build_config.h" | 27 #include "build/build_config.h" |
26 | 28 |
27 namespace { | 29 namespace { |
28 | 30 |
29 // Not needed for OS_CHROMEOS. | 31 // Not needed for OS_CHROMEOS. |
30 #if defined(OS_LINUX) | 32 #if defined(OS_LINUX) |
31 enum LinuxDistroState { | 33 enum LinuxDistroState { |
32 STATE_DID_NOT_CHECK = 0, | 34 STATE_DID_NOT_CHECK = 0, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 DCHECK_EQ(STATE_CHECK_STARTED, state_); | 67 DCHECK_EQ(STATE_CHECK_STARTED, state_); |
66 state_ = STATE_CHECK_FINISHED; | 68 state_ = STATE_CHECK_FINISHED; |
67 } | 69 } |
68 | 70 |
69 private: | 71 private: |
70 base::Lock lock_; | 72 base::Lock lock_; |
71 LinuxDistroState state_; | 73 LinuxDistroState state_; |
72 }; | 74 }; |
73 #endif // if defined(OS_LINUX) | 75 #endif // if defined(OS_LINUX) |
74 | 76 |
| 77 bool 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 |
75 } // namespace | 99 } // namespace |
76 | 100 |
77 namespace base { | 101 namespace base { |
78 | 102 |
79 // Account for the terminating null character. | 103 // Account for the terminating null character. |
80 static const int kDistroSize = 128 + 1; | 104 static const int kDistroSize = 128 + 1; |
81 | 105 |
82 // We use this static string to hold the Linux distro info. If we | 106 // We use this static string to hold the Linux distro info. If we |
83 // crash, the crash handler code will send this in the crash dump. | 107 // crash, the crash handler code will send this in the crash dump. |
84 char g_linux_distro[kDistroSize] = | 108 char g_linux_distro[kDistroSize] = |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 } | 149 } |
126 | 150 |
127 void SetLinuxDistro(const std::string& distro) { | 151 void SetLinuxDistro(const std::string& distro) { |
128 std::string trimmed_distro; | 152 std::string trimmed_distro; |
129 TrimWhitespaceASCII(distro, TRIM_ALL, &trimmed_distro); | 153 TrimWhitespaceASCII(distro, TRIM_ALL, &trimmed_distro); |
130 strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize); | 154 strlcpy(g_linux_distro, trimmed_distro.c_str(), kDistroSize); |
131 } | 155 } |
132 | 156 |
133 pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data, | 157 pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data, |
134 bool* syscall_supported) { | 158 bool* syscall_supported) { |
135 char buf[256]; | |
136 snprintf(buf, sizeof(buf), "/proc/%d/task", pid); | |
137 | |
138 if (syscall_supported != NULL) | 159 if (syscall_supported != NULL) |
139 *syscall_supported = false; | 160 *syscall_supported = false; |
140 | 161 |
141 DIR* task = opendir(buf); | 162 std::vector<pid_t> tids; |
142 if (!task) { | 163 if (!GetTasksForProcess(pid, &tids)) |
143 DLOG(WARNING) << "Cannot open " << buf; | |
144 return -1; | 164 return -1; |
145 } | |
146 | |
147 std::vector<pid_t> tids; | |
148 struct dirent* dent; | |
149 while ((dent = readdir(task))) { | |
150 char* endptr; | |
151 const unsigned long int tid_ul = strtoul(dent->d_name, &endptr, 10); | |
152 if (tid_ul == ULONG_MAX || *endptr) | |
153 continue; | |
154 tids.push_back(tid_ul); | |
155 } | |
156 closedir(task); | |
157 | 165 |
158 std::unique_ptr<char[]> syscall_data(new char[expected_data.length()]); | 166 std::unique_ptr<char[]> syscall_data(new char[expected_data.length()]); |
159 for (std::vector<pid_t>::const_iterator | 167 for (pid_t tid : tids) { |
160 i = tids.begin(); i != tids.end(); ++i) { | 168 char buf[256]; |
161 const pid_t current_tid = *i; | 169 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, tid); |
162 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, current_tid); | |
163 int fd = open(buf, O_RDONLY); | 170 int fd = open(buf, O_RDONLY); |
164 if (fd < 0) | 171 if (fd < 0) |
165 continue; | 172 continue; |
166 if (syscall_supported != NULL) | 173 if (syscall_supported != NULL) |
167 *syscall_supported = true; | 174 *syscall_supported = true; |
168 bool read_ret = ReadFromFD(fd, syscall_data.get(), expected_data.length()); | 175 bool read_ret = ReadFromFD(fd, syscall_data.get(), expected_data.length()); |
169 close(fd); | 176 close(fd); |
170 if (!read_ret) | 177 if (!read_ret) |
171 continue; | 178 continue; |
172 | 179 |
173 if (0 == strncmp(expected_data.c_str(), syscall_data.get(), | 180 if (0 == strncmp(expected_data.c_str(), syscall_data.get(), |
174 expected_data.length())) { | 181 expected_data.length())) { |
175 return current_tid; | 182 return tid; |
176 } | 183 } |
177 } | 184 } |
178 return -1; | 185 return -1; |
| 186 } |
| 187 |
| 188 pid_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 } |
| 221 } |
| 222 } |
| 223 return -1; |
179 } | 224 } |
180 | 225 |
181 } // namespace base | 226 } // namespace base |
OLD | NEW |