Commit 3bf06698 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Tidy base/linux_util.cc.

- Remove unused includes.
- Use ScopedFD.
- Remove memory allocation with new.
- Remove out-parameter checks that are always true.

Change-Id: Ia4627f692057a3306f795f27b261489ca11981a2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2034110Reviewed-by: default avatarThomas Anderson <thomasanderson@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#737883}
parent cc457825
...@@ -16,18 +16,15 @@ ...@@ -16,18 +16,15 @@
#include <iomanip> #include <iomanip>
#include <memory> #include <memory>
#include "base/command_line.h"
#include "base/files/dir_reader_posix.h" #include "base/files/dir_reader_posix.h"
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/memory/singleton.h" #include "base/files/scoped_file.h"
#include "base/no_destructor.h" #include "base/no_destructor.h"
#include "base/process/launch.h"
#include "base/strings/safe_sprintf.h" #include "base/strings/safe_sprintf.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h" #include "base/strings/string_split.h"
#include "base/strings/string_tokenizer.h" #include "base/strings/string_tokenizer.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/synchronization/lock.h"
#include "build/build_config.h" #include "build/build_config.h"
namespace base { namespace base {
...@@ -90,10 +87,10 @@ class DistroNameGetter { ...@@ -90,10 +87,10 @@ class DistroNameGetter {
}; };
#endif // !defined(OS_CHROMEOS) #endif // !defined(OS_CHROMEOS)
} // namespace
// Account for the terminating null character. // Account for the terminating null character.
static const int kDistroSize = 128 + 1; constexpr int kDistroSize = 128 + 1;
} // namespace
// We use this static string to hold the Linux distro info. If we // We use this static string to hold the Linux distro info. If we
// crash, the crash handler code will send this in the crash dump. // crash, the crash handler code will send this in the crash dump.
...@@ -125,7 +122,7 @@ std::string GetLinuxDistro() { ...@@ -125,7 +122,7 @@ std::string GetLinuxDistro() {
#if !defined(OS_CHROMEOS) #if !defined(OS_CHROMEOS)
// We do this check only once per process. If it fails, there's // We do this check only once per process. If it fails, there's
// little reason to believe it will work if we attempt to run it again. // little reason to believe it will work if we attempt to run it again.
static base::NoDestructor<DistroNameGetter> distro_name_getter; static NoDestructor<DistroNameGetter> distro_name_getter;
#endif #endif
return g_linux_distro; return g_linux_distro;
} }
...@@ -160,29 +157,27 @@ bool GetThreadsForProcess(pid_t pid, std::vector<pid_t>* tids) { ...@@ -160,29 +157,27 @@ bool GetThreadsForProcess(pid_t pid, std::vector<pid_t>* tids) {
pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data, pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data,
bool* syscall_supported) { bool* syscall_supported) {
if (syscall_supported != nullptr) if (syscall_supported)
*syscall_supported = false; *syscall_supported = false;
std::vector<pid_t> tids; std::vector<pid_t> tids;
if (!GetThreadsForProcess(pid, &tids)) if (!GetThreadsForProcess(pid, &tids))
return -1; return -1;
std::unique_ptr<char[]> syscall_data(new char[expected_data.length()]); std::vector<char> syscall_data(expected_data.size());
for (pid_t tid : tids) { for (pid_t tid : tids) {
char buf[256]; char buf[256];
snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, tid); snprintf(buf, sizeof(buf), "/proc/%d/task/%d/syscall", pid, tid);
int fd = open(buf, O_RDONLY); ScopedFD fd(open(buf, O_RDONLY));
if (fd < 0) if (!fd.is_valid())
continue; continue;
if (syscall_supported != nullptr)
*syscall_supported = true; *syscall_supported = true;
bool read_ret = ReadFromFD(fd, syscall_data.get(), expected_data.length()); if (!ReadFromFD(fd.get(), syscall_data.data(), syscall_data.size()))
close(fd);
if (!read_ret)
continue; continue;
if (0 == strncmp(expected_data.c_str(), syscall_data.get(), if (0 == strncmp(expected_data.c_str(), syscall_data.data(),
expected_data.length())) { expected_data.size())) {
return tid; return tid;
} }
} }
...@@ -190,8 +185,7 @@ pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data, ...@@ -190,8 +185,7 @@ pid_t FindThreadIDWithSyscall(pid_t pid, const std::string& expected_data,
} }
pid_t FindThreadID(pid_t pid, pid_t ns_tid, bool* ns_pid_supported) { pid_t FindThreadID(pid_t pid, pid_t ns_tid, bool* ns_pid_supported) {
if (ns_pid_supported) *ns_pid_supported = false;
*ns_pid_supported = false;
std::vector<pid_t> tids; std::vector<pid_t> tids;
if (!GetThreadsForProcess(pid, &tids)) if (!GetThreadsForProcess(pid, &tids))
...@@ -208,8 +202,8 @@ pid_t FindThreadID(pid_t pid, pid_t ns_tid, bool* ns_pid_supported) { ...@@ -208,8 +202,8 @@ pid_t FindThreadID(pid_t pid, pid_t ns_tid, bool* ns_pid_supported) {
StringPiece value_str(tokenizer.token_piece()); StringPiece value_str(tokenizer.token_piece());
if (!value_str.starts_with("NSpid")) if (!value_str.starts_with("NSpid"))
continue; continue;
if (ns_pid_supported)
*ns_pid_supported = true; *ns_pid_supported = true;
std::vector<StringPiece> split_value_str = SplitStringPiece( std::vector<StringPiece> split_value_str = SplitStringPiece(
value_str, "\t", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); value_str, "\t", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY);
DCHECK_GE(split_value_str.size(), 2u); DCHECK_GE(split_value_str.size(), 2u);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment