Linux/Cros: Add base::Process::IsSeccompSandboxed()
This involves reading /proc/<pid>/status, so some helpers that were
previously in base/process/process_metrics_linux.cc are moved to
base/process/internal_linux.h where there are other /proc/<pid>
reading utilities.
Change-Id: Ibc1756ba3bc0ade123fb3300010f4805485a6e82
Bug: 1079808
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4754907
Commit-Queue: Matthew Denton <mpdenton@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1180640}
diff --git a/base/process/process_metrics_linux.cc b/base/process/process_metrics_linux.cc
index f284840..97eb24d 100644
--- a/base/process/process_metrics_linux.cc
+++ b/base/process/process_metrics_linux.cc
@@ -43,13 +43,6 @@
namespace {
-void TrimKeyValuePairs(StringPairs* pairs) {
- for (auto& pair : *pairs) {
- TrimWhitespaceASCII(pair.first, TRIM_ALL, &pair.first);
- TrimWhitespaceASCII(pair.second, TRIM_ALL, &pair.second);
- }
-}
-
#if BUILDFLAG(IS_CHROMEOS)
// Read a file with a single number string and return the number as a uint64_t.
uint64_t ReadFileToUint64(const FilePath& file) {
@@ -64,79 +57,6 @@
}
#endif
-// Read |filename| in /proc/<pid>/, split the entries into key/value pairs, and
-// trim the key and value. On success, return true and write the trimmed
-// key/value pairs into |key_value_pairs|.
-bool ReadProcFileToTrimmedStringPairs(pid_t pid,
- StringPiece filename,
- StringPairs* key_value_pairs) {
- std::string status_data;
- FilePath status_file = internal::GetProcPidDir(pid).Append(filename);
- if (!internal::ReadProcFile(status_file, &status_data))
- return false;
- SplitStringIntoKeyValuePairs(status_data, ':', '\n', key_value_pairs);
- TrimKeyValuePairs(key_value_pairs);
- return true;
-}
-
-// Read /proc/<pid>/status and return the value for |field|, or 0 on failure.
-// Only works for fields in the form of "Field: value kB".
-size_t ReadProcStatusAndGetFieldAsSizeT(pid_t pid, StringPiece field) {
- StringPairs pairs;
- if (!ReadProcFileToTrimmedStringPairs(pid, "status", &pairs))
- return 0;
-
- for (const auto& pair : pairs) {
- const std::string& key = pair.first;
- const std::string& value_str = pair.second;
- if (key != field)
- continue;
-
- std::vector<StringPiece> split_value_str =
- SplitStringPiece(value_str, " ", TRIM_WHITESPACE, SPLIT_WANT_ALL);
- if (split_value_str.size() != 2 || split_value_str[1] != "kB") {
- NOTREACHED();
- return 0;
- }
- size_t value;
- if (!StringToSizeT(split_value_str[0], &value)) {
- NOTREACHED();
- return 0;
- }
- return value;
- }
- // This can be reached if the process dies when proc is read -- in that case,
- // the kernel can return missing fields.
- return 0;
-}
-
-#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_AIX)
-// Read /proc/<pid>/status and look for |field|. On success, return true and
-// write the value for |field| into |result|.
-// Only works for fields in the form of "field : uint_value"
-bool ReadProcStatusAndGetFieldAsUint64(pid_t pid,
- StringPiece field,
- uint64_t* result) {
- StringPairs pairs;
- if (!ReadProcFileToTrimmedStringPairs(pid, "status", &pairs))
- return false;
-
- for (const auto& pair : pairs) {
- const std::string& key = pair.first;
- const std::string& value_str = pair.second;
- if (key != field)
- continue;
-
- uint64_t value;
- if (!StringToUint64(value_str, &value))
- return false;
- *result = value;
- return true;
- }
- return false;
-}
-#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_AIX)
-
// Get the total CPU from a proc stat buffer. Return value is number of jiffies
// on success or 0 if parsing failed.
int64_t ParseTotalCPUTimeFromStats(const std::vector<std::string>& proc_stats) {
@@ -202,8 +122,9 @@
// CONFIG_TASK_IO_ACCOUNTING enabled.
bool ProcessMetrics::GetIOCounters(IoCounters* io_counters) const {
StringPairs pairs;
- if (!ReadProcFileToTrimmedStringPairs(process_, "io", &pairs))
+ if (!internal::ReadProcFileToTrimmedStringPairs(process_, "io", &pairs)) {
return false;
+ }
io_counters->OtherOperationCount = 0;
io_counters->OtherTransferCount = 0;
@@ -230,7 +151,8 @@
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID)
uint64_t ProcessMetrics::GetVmSwapBytes() const {
- return ReadProcStatusAndGetFieldAsSizeT(process_, "VmSwap") * 1024;
+ return internal::ReadProcStatusAndGetKbFieldAsSizeT(process_, "VmSwap") *
+ 1024;
}
bool ProcessMetrics::GetPageFaultCounts(PageFaultCounts* counts) const {
@@ -968,7 +890,8 @@
int ProcessMetrics::GetIdleWakeupsPerSecond() {
uint64_t num_switches;
static const char kSwitchStat[] = "voluntary_ctxt_switches";
- return ReadProcStatusAndGetFieldAsUint64(process_, kSwitchStat, &num_switches)
+ return internal::ReadProcStatusAndGetFieldAsUint64(process_, kSwitchStat,
+ &num_switches)
? CalculateIdleWakeupsPerSecond(num_switches)
: 0;
}