| 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" | 23 #include "base/strings/string_number_conversions.h" |
| 24 #include "base/strings/string_split.h" | 24 #include "base/strings/string_split.h" |
| 25 #include "base/strings/string_tokenizer.h" |
| 25 #include "base/strings/string_util.h" | 26 #include "base/strings/string_util.h" |
| 26 #include "base/synchronization/lock.h" | 27 #include "base/synchronization/lock.h" |
| 27 #include "build/build_config.h" | 28 #include "build/build_config.h" |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 31 // Not needed for OS_CHROMEOS. | 32 // Not needed for OS_CHROMEOS. |
| 32 #if defined(OS_LINUX) | 33 #if defined(OS_LINUX) |
| 33 enum LinuxDistroState { | 34 enum LinuxDistroState { |
| 34 STATE_DID_NOT_CHECK = 0, | 35 STATE_DID_NOT_CHECK = 0, |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 std::vector<pid_t> tids; | 193 std::vector<pid_t> tids; |
| 193 if (!GetTasksForProcess(pid, &tids)) | 194 if (!GetTasksForProcess(pid, &tids)) |
| 194 return -1; | 195 return -1; |
| 195 | 196 |
| 196 for (pid_t tid : tids) { | 197 for (pid_t tid : tids) { |
| 197 char buf[256]; | 198 char buf[256]; |
| 198 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/status", pid, tid); | 199 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/status", pid, tid); |
| 199 std::string status; | 200 std::string status; |
| 200 if (!ReadFileToString(FilePath(buf), &status)) | 201 if (!ReadFileToString(FilePath(buf), &status)) |
| 201 return -1; | 202 return -1; |
| 202 StringPairs pairs; | 203 StringTokenizer tokenizer(status, "\n"); |
| 203 SplitStringIntoKeyValuePairs(status, ':', '\n', &pairs); | 204 while (tokenizer.GetNext()) { |
| 204 for (const auto& pair : pairs) { | 205 StringPiece value_str(tokenizer.token_piece()); |
| 205 const std::string& key = pair.first; | 206 if (!value_str.starts_with("NSpid")) |
| 206 const std::string& value_str = pair.second; | 207 continue; |
| 207 if (key == "NSpid") { | 208 if (ns_pid_supported) |
| 208 if (ns_pid_supported) | 209 *ns_pid_supported = true; |
| 209 *ns_pid_supported = true; | 210 std::vector<StringPiece> split_value_str = SplitStringPiece( |
| 210 std::vector<StringPiece> split_value_str = SplitStringPiece( | 211 value_str, "\t", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); |
| 211 value_str, "\t", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); | 212 DCHECK_GE(split_value_str.size(), 2u); |
| 212 DCHECK_NE(split_value_str.size(), 0u); | 213 int value; |
| 213 int value; | 214 // The last value in the list is the PID in the namespace. |
| 214 // The last value in the list is the PID in the namespace. | 215 if (StringToInt(split_value_str.back(), &value) && value == ns_tid) { |
| 215 if (StringToInt(split_value_str.back(), &value) && value == ns_tid) { | 216 // The second value in the list is the real PID. |
| 216 // The first value in the list is the real PID. | 217 if (StringToInt(split_value_str[1], &value)) |
| 217 if (StringToInt(split_value_str.front(), &value)) | 218 return value; |
| 218 return value; | |
| 219 } | |
| 220 } | 219 } |
| 220 break; |
| 221 } | 221 } |
| 222 } | 222 } |
| 223 return -1; | 223 return -1; |
| 224 } | 224 } |
| 225 | 225 |
| 226 } // namespace base | 226 } // namespace base |
| OLD | NEW |