Commit 7d131534 authored by Francois Doray's avatar Francois Doray Committed by Commit Bot

Base: Replace CurrentProcessInfo::CreationTime() with Process::CreationTime().

The new API supports getting the creation time of any process (not just
the current process).

This will be used to get a (pid, creation time) pair identifying a
renderer process and sending it to the resource coordinator service
via Mojo. Ideally, we would send a process handle, but this is not
supported by Mojo, and if it was, it would have undesirable security
implications.

Bug: 885293
Change-Id: Ic3a24c93e5352767eb8e6fb025eeacdbc0d23109
Reviewed-on: https://chromium-review.googlesource.com/c/1273607Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarWez <wez@chromium.org>
Reviewed-by: default avatarSigurður Ásgeirsson <siggi@chromium.org>
Commit-Queue: François Doray <fdoray@chromium.org>
Cr-Commit-Position: refs/heads/master@{#601423}
parent c7e36b09
...@@ -598,9 +598,8 @@ jumbo_component("base") { ...@@ -598,9 +598,8 @@ jumbo_component("base") {
#"process/process_handle_openbsd.cc", # Unused in Chromium build. #"process/process_handle_openbsd.cc", # Unused in Chromium build.
"process/process_handle_win.cc", "process/process_handle_win.cc",
"process/process_info.cc",
"process/process_info.h", "process/process_info.h",
"process/process_info_linux.cc",
"process/process_info_mac.cc",
"process/process_info_win.cc", "process/process_info_win.cc",
"process/process_iterator.cc", "process/process_iterator.cc",
"process/process_iterator.h", "process/process_iterator.h",
...@@ -1340,7 +1339,6 @@ jumbo_component("base") { ...@@ -1340,7 +1339,6 @@ jumbo_component("base") {
"process/internal_linux.h", "process/internal_linux.h",
"process/memory_linux.cc", "process/memory_linux.cc",
"process/process_handle_linux.cc", "process/process_handle_linux.cc",
"process/process_info_linux.cc",
"process/process_iterator_linux.cc", "process/process_iterator_linux.cc",
"process/process_metrics_linux.cc", "process/process_metrics_linux.cc",
"sys_info_linux.cc", "sys_info_linux.cc",
...@@ -1436,7 +1434,6 @@ jumbo_component("base") { ...@@ -1436,7 +1434,6 @@ jumbo_component("base") {
"process/memory_fuchsia.cc", "process/memory_fuchsia.cc",
"process/process_fuchsia.cc", "process/process_fuchsia.cc",
"process/process_handle_fuchsia.cc", "process/process_handle_fuchsia.cc",
"process/process_info_fuchsia.cc",
"process/process_iterator_fuchsia.cc", "process/process_iterator_fuchsia.cc",
"process/process_metrics_fuchsia.cc", "process/process_metrics_fuchsia.cc",
"process/process_metrics_posix.cc", "process/process_metrics_posix.cc",
...@@ -1520,6 +1517,7 @@ jumbo_component("base") { ...@@ -1520,6 +1517,7 @@ jumbo_component("base") {
"process/kill.h", "process/kill.h",
"process/memory.cc", "process/memory.cc",
"process/memory.h", "process/memory.h",
"process/process_info.cc",
"process/process_iterator.cc", "process/process_iterator.cc",
"process/process_iterator.h", "process/process_iterator.h",
"process/process_metrics.cc", "process/process_metrics.cc",
......
...@@ -98,6 +98,16 @@ class BASE_EXPORT Process { ...@@ -98,6 +98,16 @@ class BASE_EXPORT Process {
// Get the PID for this process. // Get the PID for this process.
ProcessId Pid() const; ProcessId Pid() const;
#if !defined(OS_ANDROID)
// Get the creation time for this process. Since the Pid can be reused after a
// process dies, it is useful to use both the Pid and the creation time to
// uniquely identify a process.
//
// Not available on Android because /proc/stat/ cannot be accessed on O+.
// https://issuetracker.google.com/issues/37140047
Time CreationTime() const;
#endif // !defined(OS_ANDROID)
// Returns true if this process is the current process. // Returns true if this process is the current process.
bool is_current() const; bool is_current() const;
......
...@@ -125,6 +125,12 @@ ProcessId Process::Pid() const { ...@@ -125,6 +125,12 @@ ProcessId Process::Pid() const {
return GetProcId(Handle()); return GetProcId(Handle());
} }
Time Process::CreationTime() const {
// TODO(https://crbug.com/726484): There is no syscall providing this data.
NOTIMPLEMENTED();
return Time();
}
bool Process::is_current() const { bool Process::is_current() const {
return is_current_process_; return is_current_process_;
} }
......
...@@ -4,14 +4,16 @@ ...@@ -4,14 +4,16 @@
#include "base/process/process_info.h" #include "base/process/process_info.h"
#include "base/process/process.h"
#include "base/time/time.h" #include "base/time/time.h"
namespace base { namespace base {
#if !defined(OS_ANDROID)
// static // static
const Time CurrentProcessInfo::CreationTime() { const Time CurrentProcessInfo::CreationTime() {
// TODO(https://crbug.com/726484): There is no syscall providing this data. return Process::Current().CreationTime();
return Time();
} }
#endif //! defined(OS_ANDROID)
} // namespace base } // namespace base
...@@ -12,6 +12,7 @@ namespace base { ...@@ -12,6 +12,7 @@ namespace base {
class Time; class Time;
#if !defined(OS_ANDROID)
// Vends information about the current process. // Vends information about the current process.
class BASE_EXPORT CurrentProcessInfo { class BASE_EXPORT CurrentProcessInfo {
public: public:
...@@ -19,6 +20,7 @@ class BASE_EXPORT CurrentProcessInfo { ...@@ -19,6 +20,7 @@ class BASE_EXPORT CurrentProcessInfo {
// error occurred retrieving the information. // error occurred retrieving the information.
static const Time CreationTime(); static const Time CreationTime();
}; };
#endif // !defined(OS_ANDROID)
#if defined(OS_WIN) #if defined(OS_WIN)
enum IntegrityLevel { enum IntegrityLevel {
......
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/process/process_info.h"
#include <stdint.h>
#include "base/logging.h"
#include "base/process/internal_linux.h"
#include "base/process/process_handle.h"
#include "base/time/time.h"
namespace base {
// static
const Time CurrentProcessInfo::CreationTime() {
int64_t start_ticks =
internal::ReadProcSelfStatsAndGetFieldAsInt64(internal::VM_STARTTIME);
if (!start_ticks)
return Time();
TimeDelta start_offset = internal::ClockTicksToTimeDelta(start_ticks);
Time boot_time = internal::GetBootTime();
if (boot_time.is_null())
return Time();
return Time(boot_time + start_offset);
}
} // namespace base
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/process/process_info.h"
#include <stddef.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <unistd.h>
#include <memory>
#include "base/macros.h"
#include "base/memory/free_deleter.h"
#include "base/time/time.h"
namespace base {
// static
const Time CurrentProcessInfo::CreationTime() {
int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
size_t len = 0;
if (sysctl(mib, arraysize(mib), NULL, &len, NULL, 0) < 0)
return Time();
std::unique_ptr<struct kinfo_proc, base::FreeDeleter> proc(
static_cast<struct kinfo_proc*>(malloc(len)));
if (sysctl(mib, arraysize(mib), proc.get(), &len, NULL, 0) < 0)
return Time();
return Time::FromTimeVal(proc->kp_proc.p_un.__p_starttime);
}
} // namespace base
...@@ -25,19 +25,6 @@ HANDLE GetCurrentProcessToken() { ...@@ -25,19 +25,6 @@ HANDLE GetCurrentProcessToken() {
} // namespace } // namespace
// static
const Time CurrentProcessInfo::CreationTime() {
FILETIME creation_time = {};
FILETIME ignore1 = {};
FILETIME ignore2 = {};
FILETIME ignore3 = {};
if (!::GetProcessTimes(::GetCurrentProcess(), &creation_time, &ignore1,
&ignore2, &ignore3)) {
return Time();
}
return Time::FromFileTime(creation_time);
}
IntegrityLevel GetCurrentProcessIntegrityLevel() { IntegrityLevel GetCurrentProcessIntegrityLevel() {
HANDLE process_token(GetCurrentProcessToken()); HANDLE process_token(GetCurrentProcessToken());
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/posix/can_lower_nice_to.h" #include "base/posix/can_lower_nice_to.h"
#include "base/process/internal_linux.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/stringprintf.h" #include "base/strings/stringprintf.h"
...@@ -72,6 +73,21 @@ const int kBackgroundPriority = 5; ...@@ -72,6 +73,21 @@ const int kBackgroundPriority = 5;
} // namespace } // namespace
Time Process::CreationTime() const {
int64_t start_ticks = is_current()
? internal::ReadProcSelfStatsAndGetFieldAsInt64(
internal::VM_STARTTIME)
: internal::ReadProcStatsAndGetFieldAsInt64(
Pid(), internal::VM_STARTTIME);
if (!start_ticks)
return Time();
TimeDelta start_offset = internal::ClockTicksToTimeDelta(start_ticks);
Time boot_time = internal::GetBootTime();
if (boot_time.is_null())
return Time();
return Time(boot_time + start_offset);
}
// static // static
bool Process::CanBackgroundProcesses() { bool Process::CanBackgroundProcesses() {
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
......
...@@ -5,9 +5,17 @@ ...@@ -5,9 +5,17 @@
#include "base/process/process.h" #include "base/process/process.h"
#include <mach/mach.h> #include <mach/mach.h>
#include <stddef.h>
#include <sys/sysctl.h>
#include <sys/time.h>
#include <unistd.h>
#include <memory>
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/mac/mach_logging.h" #include "base/mac/mach_logging.h"
#include "base/memory/free_deleter.h"
#include "base/stl_util.h"
namespace base { namespace base {
...@@ -15,6 +23,19 @@ namespace base { ...@@ -15,6 +23,19 @@ namespace base {
const Feature kMacAllowBackgroundingProcesses{"MacAllowBackgroundingProcesses", const Feature kMacAllowBackgroundingProcesses{"MacAllowBackgroundingProcesses",
FEATURE_DISABLED_BY_DEFAULT}; FEATURE_DISABLED_BY_DEFAULT};
Time Process::CreationTime() const {
int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, Pid()};
size_t len = 0;
if (sysctl(mib, size(mib), NULL, &len, NULL, 0) < 0)
return Time();
std::unique_ptr<struct kinfo_proc, base::FreeDeleter> proc(
static_cast<struct kinfo_proc*>(malloc(len)));
if (sysctl(mib, size(mib), proc.get(), &len, NULL, 0) < 0)
return Time();
return Time::FromTimeVal(proc->kp_proc.p_un.__p_starttime);
}
bool Process::CanBackgroundProcesses() { bool Process::CanBackgroundProcesses() {
return FeatureList::IsEnabled(kMacAllowBackgroundingProcesses); return FeatureList::IsEnabled(kMacAllowBackgroundingProcesses);
} }
......
...@@ -20,11 +20,13 @@ ...@@ -20,11 +20,13 @@
namespace { namespace {
#if defined(OS_WIN) #if defined(OS_WIN)
const int kExpectedStillRunningExitCode = 0x102; constexpr int kExpectedStillRunningExitCode = 0x102;
#else #else
const int kExpectedStillRunningExitCode = 0; constexpr int kExpectedStillRunningExitCode = 0;
#endif #endif
constexpr int kDummyExitCode = 42;
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
// Fake port provider that returns the calling process's // Fake port provider that returns the calling process's
// task port, ignoring its argument. // task port, ignoring its argument.
...@@ -129,11 +131,41 @@ MULTIPROCESS_TEST_MAIN(SleepyChildProcess) { ...@@ -129,11 +131,41 @@ MULTIPROCESS_TEST_MAIN(SleepyChildProcess) {
return 0; return 0;
} }
// TODO(https://crbug.com/726484): Enable these tests on Fuchsia when
// CreationTime() is implemented.
//
// Disabled on Android because Process::CreationTime() is not supported.
// https://issuetracker.google.com/issues/37140047
#if !defined(OS_FUCHSIA) && !defined(OS_ANDROID)
TEST_F(ProcessTest, CreationTimeCurrentProcess) {
// The current process creation time should be less than or equal to the
// current time.
EXPECT_LE(Process::Current().CreationTime(), Time::Now());
}
TEST_F(ProcessTest, CreationTimeOtherProcess) {
// The creation time of a process should be between a time recorded before it
// was spawned and a time recorded after it was spawned. However, since the
// base::Time and process creation clocks don't match, tolerate a 1 second
// range. (On Linux, process creation time is relative to boot time which as a
// 1-second resolution. On Windows, process creation time is based on the
// system clock while Time::Now() can be a combination of system clock and
// QueryPerformanceCounter().)
constexpr base::TimeDelta kTolerance = base::TimeDelta::FromSeconds(1);
const Time before_creation = Time::Now();
Process process(SpawnChild("SleepyChildProcess"));
const Time after_creation = Time::Now();
const Time creation = process.CreationTime();
EXPECT_LE(before_creation - kTolerance, creation);
EXPECT_LE(creation, after_creation + kTolerance);
EXPECT_TRUE(process.Terminate(kDummyExitCode, true));
}
#endif // !defined(OS_FUCHSIA)
TEST_F(ProcessTest, Terminate) { TEST_F(ProcessTest, Terminate) {
Process process(SpawnChild("SleepyChildProcess")); Process process(SpawnChild("SleepyChildProcess"));
ASSERT_TRUE(process.IsValid()); ASSERT_TRUE(process.IsValid());
const int kDummyExitCode = 42;
int exit_code = kDummyExitCode; int exit_code = kDummyExitCode;
EXPECT_EQ(TERMINATION_STATUS_STILL_RUNNING, EXPECT_EQ(TERMINATION_STATUS_STILL_RUNNING,
GetTerminationStatus(process.Handle(), &exit_code)); GetTerminationStatus(process.Handle(), &exit_code));
......
...@@ -125,6 +125,18 @@ ProcessId Process::Pid() const { ...@@ -125,6 +125,18 @@ ProcessId Process::Pid() const {
return GetProcId(Handle()); return GetProcId(Handle());
} }
Time Process::CreationTime() const {
FILETIME creation_time = {};
FILETIME ignore1 = {};
FILETIME ignore2 = {};
FILETIME ignore3 = {};
if (!::GetProcessTimes(Handle(), &creation_time, &ignore1, &ignore2,
&ignore3)) {
return Time();
}
return Time::FromFileTime(creation_time);
}
bool Process::is_current() const { bool Process::is_current() const {
return is_current_process_; return is_current_process_;
} }
......
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