[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 76bea67 | 2013-07-19 16:48:56 | [diff] [blame] | 5 | #include "base/process/process.h" |
[email protected] | dd4b5126 | 2013-07-25 21:38:23 | [diff] [blame] | 6 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 7 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 8 | #include "base/memory/scoped_ptr.h" |
gab | d88d22a | 2015-02-20 04:26:53 | [diff] [blame] | 9 | #include "base/metrics/field_trial.h" |
rvargas | 8572897 | 2015-03-03 20:46:19 | [diff] [blame^] | 10 | #include "base/numerics/safe_conversions.h" |
rvargas | 126fd582 | 2014-12-12 00:25:14 | [diff] [blame] | 11 | #include "base/process/kill.h" |
[email protected] | 568bfb0 | 2011-04-28 23:51:53 | [diff] [blame] | 12 | #include "base/win/windows_version.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 13 | |
rvargas | 747ff24 | 2015-01-17 02:46:47 | [diff] [blame] | 14 | namespace { |
| 15 | |
| 16 | DWORD kBasicProcessAccess = |
| 17 | PROCESS_TERMINATE | PROCESS_QUERY_INFORMATION | SYNCHRONIZE; |
| 18 | |
| 19 | } // namespace |
| 20 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 21 | namespace base { |
| 22 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 23 | Process::Process(ProcessHandle handle) |
| 24 | : is_current_process_(false), |
| 25 | process_(handle) { |
| 26 | CHECK_NE(handle, ::GetCurrentProcess()); |
| 27 | } |
| 28 | |
| 29 | Process::Process(RValue other) |
| 30 | : is_current_process_(other.object->is_current_process_), |
| 31 | process_(other.object->process_.Take()) { |
| 32 | other.object->Close(); |
| 33 | } |
| 34 | |
| 35 | Process& Process::operator=(RValue other) { |
| 36 | if (this != other.object) { |
| 37 | process_.Set(other.object->process_.Take()); |
| 38 | is_current_process_ = other.object->is_current_process_; |
| 39 | other.object->Close(); |
| 40 | } |
| 41 | return *this; |
| 42 | } |
| 43 | |
| 44 | // static |
| 45 | Process Process::Current() { |
| 46 | Process process; |
| 47 | process.is_current_process_ = true; |
| 48 | return process.Pass(); |
| 49 | } |
| 50 | |
| 51 | // static |
rvargas | 6b039c37 | 2015-02-04 21:11:29 | [diff] [blame] | 52 | Process Process::Open(ProcessId pid) { |
| 53 | return Process(::OpenProcess(kBasicProcessAccess, FALSE, pid)); |
| 54 | } |
| 55 | |
| 56 | // static |
rvargas | 747ff24 | 2015-01-17 02:46:47 | [diff] [blame] | 57 | Process Process::OpenWithExtraPriviles(ProcessId pid) { |
| 58 | DWORD access = kBasicProcessAccess | PROCESS_DUP_HANDLE | PROCESS_VM_READ; |
rvargas | 6b039c37 | 2015-02-04 21:11:29 | [diff] [blame] | 59 | return Process(::OpenProcess(access, FALSE, pid)); |
rvargas | 747ff24 | 2015-01-17 02:46:47 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | // static |
rvargas | 17a407d | 2015-01-23 20:36:44 | [diff] [blame] | 63 | Process Process::OpenWithAccess(ProcessId pid, DWORD desired_access) { |
| 64 | return Process(::OpenProcess(desired_access, FALSE, pid)); |
| 65 | } |
| 66 | |
| 67 | // static |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 68 | Process Process::DeprecatedGetProcessFromHandle(ProcessHandle handle) { |
| 69 | DCHECK_NE(handle, ::GetCurrentProcess()); |
| 70 | ProcessHandle out_handle; |
| 71 | if (!::DuplicateHandle(GetCurrentProcess(), handle, |
| 72 | GetCurrentProcess(), &out_handle, |
| 73 | 0, FALSE, DUPLICATE_SAME_ACCESS)) { |
| 74 | return Process(); |
| 75 | } |
| 76 | return Process(out_handle); |
| 77 | } |
| 78 | |
| 79 | // static |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 80 | bool Process::CanBackgroundProcesses() { |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | bool Process::IsValid() const { |
| 85 | return process_.IsValid() || is_current(); |
| 86 | } |
| 87 | |
| 88 | ProcessHandle Process::Handle() const { |
| 89 | return is_current_process_ ? GetCurrentProcess() : process_.Get(); |
| 90 | } |
| 91 | |
| 92 | Process Process::Duplicate() const { |
| 93 | if (is_current()) |
| 94 | return Current(); |
| 95 | |
| 96 | ProcessHandle out_handle; |
| 97 | if (!IsValid() || !::DuplicateHandle(GetCurrentProcess(), |
| 98 | Handle(), |
| 99 | GetCurrentProcess(), |
| 100 | &out_handle, |
| 101 | 0, |
| 102 | FALSE, |
| 103 | DUPLICATE_SAME_ACCESS)) { |
| 104 | return Process(); |
| 105 | } |
| 106 | return Process(out_handle); |
| 107 | } |
| 108 | |
rvargas | 960db88 | 2015-01-24 00:27:25 | [diff] [blame] | 109 | ProcessId Process::Pid() const { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 110 | DCHECK(IsValid()); |
| 111 | return GetProcId(Handle()); |
| 112 | } |
| 113 | |
| 114 | bool Process::is_current() const { |
| 115 | return is_current_process_; |
| 116 | } |
| 117 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 118 | void Process::Close() { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 119 | is_current_process_ = false; |
| 120 | if (!process_.IsValid()) |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 121 | return; |
[email protected] | b987e90e | 2011-08-15 19:22:44 | [diff] [blame] | 122 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 123 | process_.Close(); |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void Process::Terminate(int result_code) { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 127 | DCHECK(IsValid()); |
[email protected] | b987e90e | 2011-08-15 19:22:44 | [diff] [blame] | 128 | |
[email protected] | c4ed802 | 2011-08-19 01:36:21 | [diff] [blame] | 129 | // Call NtTerminateProcess directly, without going through the import table, |
| 130 | // which might have been hooked with a buggy replacement by third party |
| 131 | // software. http://crbug.com/81449. |
[email protected] | b987e90e | 2011-08-15 19:22:44 | [diff] [blame] | 132 | HMODULE module = GetModuleHandle(L"ntdll.dll"); |
| 133 | typedef UINT (WINAPI *TerminateProcessPtr)(HANDLE handle, UINT code); |
| 134 | TerminateProcessPtr terminate_process = reinterpret_cast<TerminateProcessPtr>( |
| 135 | GetProcAddress(module, "NtTerminateProcess")); |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 136 | terminate_process(Handle(), result_code); |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 137 | } |
| 138 | |
rvargas | 126fd582 | 2014-12-12 00:25:14 | [diff] [blame] | 139 | bool Process::WaitForExit(int* exit_code) { |
| 140 | return WaitForExitWithTimeout(TimeDelta::FromMilliseconds(INFINITE), |
| 141 | exit_code); |
| 142 | } |
| 143 | |
| 144 | bool Process::WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) { |
rvargas | 8572897 | 2015-03-03 20:46:19 | [diff] [blame^] | 145 | // Limit timeout to INFINITE. |
| 146 | DWORD timeout_ms = saturated_cast<DWORD>(timeout.InMilliseconds()); |
| 147 | if (::WaitForSingleObject(Handle(), timeout_ms) != WAIT_OBJECT_0) |
| 148 | return false; |
| 149 | |
| 150 | DWORD temp_code; // Don't clobber out-parameters in case of failure. |
| 151 | if (!::GetExitCodeProcess(Handle(), &temp_code)) |
| 152 | return false; |
| 153 | |
| 154 | *exit_code = temp_code; |
| 155 | return true; |
rvargas | 126fd582 | 2014-12-12 00:25:14 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | 2f15de4 | 2008-11-11 22:35:19 | [diff] [blame] | 158 | bool Process::IsProcessBackgrounded() const { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 159 | DCHECK(IsValid()); |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 160 | DWORD priority = GetPriority(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 161 | if (priority == 0) |
| 162 | return false; // Failure case. |
[email protected] | 568bfb0 | 2011-04-28 23:51:53 | [diff] [blame] | 163 | return ((priority == BELOW_NORMAL_PRIORITY_CLASS) || |
| 164 | (priority == IDLE_PRIORITY_CLASS)); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | bool Process::SetProcessBackgrounded(bool value) { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 168 | DCHECK(IsValid()); |
[email protected] | 568bfb0 | 2011-04-28 23:51:53 | [diff] [blame] | 169 | // Vista and above introduce a real background mode, which not only |
| 170 | // sets the priority class on the threads but also on the IO generated |
| 171 | // by it. Unfortunately it can only be set for the calling process. |
| 172 | DWORD priority; |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 173 | if ((base::win::GetVersion() >= base::win::VERSION_VISTA) && (is_current())) { |
[email protected] | 568bfb0 | 2011-04-28 23:51:53 | [diff] [blame] | 174 | priority = value ? PROCESS_MODE_BACKGROUND_BEGIN : |
| 175 | PROCESS_MODE_BACKGROUND_END; |
| 176 | } else { |
gab | d88d22a | 2015-02-20 04:26:53 | [diff] [blame] | 177 | // Experiment (http://crbug.com/458594) with using IDLE_PRIORITY_CLASS as a |
| 178 | // background priority for background renderers (this code path is |
| 179 | // technically for more than just the renderers but they're the only use |
| 180 | // case in practice and experimenting here direclty is thus easier -- plus |
| 181 | // it doesn't really hurt as above we already state our intent of using |
| 182 | // PROCESS_MODE_BACKGROUND_BEGIN if available which is essentially |
| 183 | // IDLE_PRIORITY_CLASS plus lowered IO priority). Enabled by default in the |
| 184 | // asbence of field trials to get coverage on the perf waterfall. |
| 185 | DWORD background_priority = IDLE_PRIORITY_CLASS; |
| 186 | base::FieldTrial* trial = |
| 187 | base::FieldTrialList::Find("BackgroundRendererProcesses"); |
| 188 | if (trial && trial->group_name() == "AllowBelowNormalFromBrowser") |
| 189 | background_priority = BELOW_NORMAL_PRIORITY_CLASS; |
| 190 | |
| 191 | priority = value ? background_priority : NORMAL_PRIORITY_CLASS; |
[email protected] | 568bfb0 | 2011-04-28 23:51:53 | [diff] [blame] | 192 | } |
| 193 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 194 | return (::SetPriorityClass(Handle(), priority) != 0); |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 195 | } |
| 196 | |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 197 | int Process::GetPriority() const { |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 198 | DCHECK(IsValid()); |
| 199 | return ::GetPriorityClass(Handle()); |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 200 | } |
| 201 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 202 | } // namespace base |