| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/process/process.h" |
| 6 |
| 7 #include <magenta/syscalls.h> |
| 8 |
| 9 #include "base/debug/activity_tracker.h" |
| 10 |
| 11 namespace base { |
| 12 |
| 13 Process::Process(ProcessHandle handle) : process_(handle) { |
| 14 } |
| 15 |
| 16 Process::~Process() { |
| 17 } |
| 18 |
| 19 Process::Process(Process&& other) : process_(other.process_) { |
| 20 other.Close(); |
| 21 } |
| 22 |
| 23 Process& Process::operator=(Process&& other) { |
| 24 process_ = other.process_; |
| 25 other.Close(); |
| 26 return *this; |
| 27 } |
| 28 |
| 29 // static |
| 30 Process Process::Current() { |
| 31 return Process(GetCurrentProcessHandle()); |
| 32 } |
| 33 |
| 34 // static |
| 35 Process Process::Open(ProcessId pid) { |
| 36 if (pid == GetCurrentProcId()) |
| 37 return Current(); |
| 38 NOTIMPLEMENTED(); |
| 39 // TODO(scottmg): Port. |
| 40 return Process(MX_HANDLE_INVALID); |
| 41 } |
| 42 |
| 43 // static |
| 44 Process Process::OpenWithExtraPrivileges(ProcessId pid) { |
| 45 // No privileges to set. |
| 46 return Open(pid); |
| 47 } |
| 48 |
| 49 // static |
| 50 Process Process::DeprecatedGetProcessFromHandle(ProcessHandle handle) { |
| 51 DCHECK_NE(handle, GetCurrentProcessHandle()); |
| 52 return Process(handle); |
| 53 } |
| 54 |
| 55 // static |
| 56 bool Process::CanBackgroundProcesses() { |
| 57 return false; |
| 58 } |
| 59 |
| 60 // static |
| 61 void Process::TerminateCurrentProcessImmediately(int exit_code) { |
| 62 _exit(exit_code); |
| 63 } |
| 64 |
| 65 bool Process::IsValid() const { |
| 66 return process_ != kNullProcessHandle; |
| 67 } |
| 68 |
| 69 ProcessHandle Process::Handle() const { |
| 70 return process_; |
| 71 } |
| 72 |
| 73 Process Process::Duplicate() const { |
| 74 if (is_current()) |
| 75 return Current(); |
| 76 |
| 77 return Process(process_); |
| 78 } |
| 79 |
| 80 ProcessId Process::Pid() const { |
| 81 DCHECK(IsValid()); |
| 82 return GetProcId(process_); |
| 83 } |
| 84 |
| 85 bool Process::is_current() const { |
| 86 return process_ == GetCurrentProcessHandle(); |
| 87 } |
| 88 |
| 89 void Process::Close() { |
| 90 process_ = kNullProcessHandle; |
| 91 // if the process wasn't terminated (so we waited) or the state |
| 92 // wasn't already collected w/ a wait from process_utils, we're gonna |
| 93 // end up w/ a zombie when it does finally exit. |
| 94 } |
| 95 |
| 96 bool Process::Terminate(int exit_code, bool wait) const { |
| 97 mx_status_t status = mx_task_kill(process_); |
| 98 // TODO(scottmg): Use |wait|. |
| 99 return status >= 0; |
| 100 } |
| 101 |
| 102 bool Process::WaitForExit(int* exit_code) const { |
| 103 return WaitForExitWithTimeout(TimeDelta::Max(), exit_code); |
| 104 } |
| 105 |
| 106 bool Process::WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) const { |
| 107 // Record the event that this thread is blocking upon (for hang diagnosis). |
| 108 base::debug::ScopedProcessWaitActivity process_activity(this); |
| 109 |
| 110 mx_time_t mxtimeout = |
| 111 timeout == TimeDelta::Max() |
| 112 ? MX_TIME_INFINITE |
| 113 : timeout.InMicroseconds() * Time::kNanosecondsPerMicrosecond; |
| 114 mx_signals_t signals_observed; |
| 115 mx_status_t status = mx_object_wait_one(process_, MX_TASK_TERMINATED, |
| 116 mxtimeout, &signals_observed); |
| 117 *exit_code = -1; |
| 118 if (status != NO_ERROR && status != ERR_TIMED_OUT) |
| 119 return false; |
| 120 if (status == ERR_TIMED_OUT && !signals_observed) |
| 121 return false; |
| 122 |
| 123 mx_info_process_t proc_info; |
| 124 status = mx_object_get_info(process_, MX_INFO_PROCESS, &proc_info, |
| 125 sizeof(proc_info), nullptr, nullptr); |
| 126 if (status != NO_ERROR) |
| 127 return status; |
| 128 |
| 129 *exit_code = proc_info.return_code; |
| 130 return true; |
| 131 } |
| 132 |
| 133 bool Process::IsProcessBackgrounded() const { |
| 134 // See SetProcessBackgrounded(). |
| 135 DCHECK(IsValid()); |
| 136 return false; |
| 137 } |
| 138 |
| 139 bool Process::SetProcessBackgrounded(bool value) { |
| 140 // TODO(scottmg): Port. |
| 141 NOTIMPLEMENTED(); |
| 142 return false; |
| 143 } |
| 144 |
| 145 int Process::GetPriority() const { |
| 146 // TODO(scottmg): Port. |
| 147 DCHECK(IsValid()); |
| 148 return 0; |
| 149 } |
| 150 |
| 151 } // namespace base |
| OLD | NEW |