Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(350)

Side by Side Diff: base/linux_util.cc

Issue 2900323002: Revert of base: Avoid unnecessary allocations in base::FindThreadID. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
26 #include "base/strings/string_util.h" 25 #include "base/strings/string_util.h"
27 #include "base/synchronization/lock.h" 26 #include "base/synchronization/lock.h"
28 #include "build/build_config.h" 27 #include "build/build_config.h"
29 28
30 namespace { 29 namespace {
31 30
32 // Not needed for OS_CHROMEOS. 31 // Not needed for OS_CHROMEOS.
33 #if defined(OS_LINUX) 32 #if defined(OS_LINUX)
34 enum LinuxDistroState { 33 enum LinuxDistroState {
35 STATE_DID_NOT_CHECK = 0, 34 STATE_DID_NOT_CHECK = 0,
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 std::vector<pid_t> tids; 192 std::vector<pid_t> tids;
194 if (!GetTasksForProcess(pid, &tids)) 193 if (!GetTasksForProcess(pid, &tids))
195 return -1; 194 return -1;
196 195
197 for (pid_t tid : tids) { 196 for (pid_t tid : tids) {
198 char buf[256]; 197 char buf[256];
199 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/status", pid, tid); 198 snprintf(buf, sizeof(buf), "/proc/%d/task/%d/status", pid, tid);
200 std::string status; 199 std::string status;
201 if (!ReadFileToString(FilePath(buf), &status)) 200 if (!ReadFileToString(FilePath(buf), &status))
202 return -1; 201 return -1;
203 StringTokenizer tokenizer(status, "\n"); 202 StringPairs pairs;
204 while (tokenizer.GetNext()) { 203 SplitStringIntoKeyValuePairs(status, ':', '\n', &pairs);
205 StringPiece value_str(tokenizer.token_piece()); 204 for (const auto& pair : pairs) {
206 if (!value_str.starts_with("NSpid")) 205 const std::string& key = pair.first;
207 continue; 206 const std::string& value_str = pair.second;
208 if (ns_pid_supported) 207 if (key == "NSpid") {
209 *ns_pid_supported = true; 208 if (ns_pid_supported)
210 std::vector<StringPiece> split_value_str = SplitStringPiece( 209 *ns_pid_supported = true;
211 value_str, "\t", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); 210 std::vector<StringPiece> split_value_str = SplitStringPiece(
212 DCHECK_GE(split_value_str.size(), 3u); 211 value_str, "\t", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
213 int value; 212 DCHECK_NE(split_value_str.size(), 0u);
214 // The last value in the list is the PID in the namespace. 213 int value;
215 if (StringToInt(split_value_str.back(), &value) && value == ns_tid) { 214 // The last value in the list is the PID in the namespace.
216 // The second value in the list is the real PID. 215 if (StringToInt(split_value_str.back(), &value) && value == ns_tid) {
217 if (StringToInt(split_value_str[1], &value)) 216 // The first value in the list is the real PID.
218 return value; 217 if (StringToInt(split_value_str.front(), &value))
218 return value;
219 }
219 } 220 }
220 break;
221 } 221 }
222 } 222 }
223 return -1; 223 return -1;
224 } 224 }
225 225
226 } // namespace base 226 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698