OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/process/process.h" | 5 #include "base/process/process.h" |
6 | 6 |
7 #include <magenta/process.h> | 7 #include <magenta/process.h> |
8 #include <magenta/syscalls.h> | 8 #include <magenta/syscalls.h> |
9 | 9 |
10 #include "base/debug/activity_tracker.h" | 10 #include "base/debug/activity_tracker.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 // static | 42 // static |
43 Process Process::Open(ProcessId pid) { | 43 Process Process::Open(ProcessId pid) { |
44 if (pid == GetCurrentProcId()) | 44 if (pid == GetCurrentProcId()) |
45 return Current(); | 45 return Current(); |
46 | 46 |
47 // While a process with object id |pid| might exist, the job returned by | 47 // While a process with object id |pid| might exist, the job returned by |
48 // mx_job_default() might not contain it, so this call can fail. | 48 // mx_job_default() might not contain it, so this call can fail. |
49 mx_handle_t handle; | 49 mx_handle_t handle; |
50 mx_status_t status = | 50 mx_status_t status = |
51 mx_object_get_child(mx_job_default(), pid, MX_RIGHT_SAME_RIGHTS, &handle); | 51 mx_object_get_child(mx_job_default(), pid, MX_RIGHT_SAME_RIGHTS, &handle); |
52 if (status != NO_ERROR) { | 52 if (status != MX_OK) { |
53 DLOG(ERROR) << "mx_object_get_child failed: " << status; | 53 DLOG(ERROR) << "mx_object_get_child failed: " << status; |
54 return Process(); | 54 return Process(); |
55 } | 55 } |
56 return Process(handle); | 56 return Process(handle); |
57 } | 57 } |
58 | 58 |
59 // static | 59 // static |
60 Process Process::OpenWithExtraPrivileges(ProcessId pid) { | 60 Process Process::OpenWithExtraPrivileges(ProcessId pid) { |
61 // No privileges to set. | 61 // No privileges to set. |
62 return Open(pid); | 62 return Open(pid); |
63 } | 63 } |
64 | 64 |
65 // static | 65 // static |
66 Process Process::DeprecatedGetProcessFromHandle(ProcessHandle handle) { | 66 Process Process::DeprecatedGetProcessFromHandle(ProcessHandle handle) { |
67 DCHECK_NE(handle, GetCurrentProcessHandle()); | 67 DCHECK_NE(handle, GetCurrentProcessHandle()); |
68 mx_handle_t out; | 68 mx_handle_t out; |
69 if (mx_handle_duplicate(handle, MX_RIGHT_SAME_RIGHTS, &out) != NO_ERROR) { | 69 if (mx_handle_duplicate(handle, MX_RIGHT_SAME_RIGHTS, &out) != MX_OK) { |
70 DLOG(ERROR) << "mx_handle_duplicate failed: " << handle; | 70 DLOG(ERROR) << "mx_handle_duplicate failed: " << handle; |
71 return Process(); | 71 return Process(); |
72 } | 72 } |
73 | 73 |
74 return Process(out); | 74 return Process(out); |
75 } | 75 } |
76 | 76 |
77 // static | 77 // static |
78 bool Process::CanBackgroundProcesses() { | 78 bool Process::CanBackgroundProcesses() { |
79 return false; | 79 return false; |
(...skipping 13 matching lines...) Expand all Loading... |
93 } | 93 } |
94 | 94 |
95 Process Process::Duplicate() const { | 95 Process Process::Duplicate() const { |
96 if (is_current()) | 96 if (is_current()) |
97 return Current(); | 97 return Current(); |
98 | 98 |
99 if (!IsValid()) | 99 if (!IsValid()) |
100 return Process(); | 100 return Process(); |
101 | 101 |
102 mx_handle_t out; | 102 mx_handle_t out; |
103 if (mx_handle_duplicate(process_, MX_RIGHT_SAME_RIGHTS, &out) != NO_ERROR) { | 103 if (mx_handle_duplicate(process_, MX_RIGHT_SAME_RIGHTS, &out) != MX_OK) { |
104 DLOG(ERROR) << "mx_handle_duplicate failed: " << process_; | 104 DLOG(ERROR) << "mx_handle_duplicate failed: " << process_; |
105 return Process(); | 105 return Process(); |
106 } | 106 } |
107 | 107 |
108 return Process(out); | 108 return Process(out); |
109 } | 109 } |
110 | 110 |
111 ProcessId Process::Pid() const { | 111 ProcessId Process::Pid() const { |
112 DCHECK(IsValid()); | 112 DCHECK(IsValid()); |
113 return GetProcId(process_); | 113 return GetProcId(process_); |
114 } | 114 } |
115 | 115 |
116 bool Process::is_current() const { | 116 bool Process::is_current() const { |
117 return is_current_process_; | 117 return is_current_process_; |
118 } | 118 } |
119 | 119 |
120 void Process::Close() { | 120 void Process::Close() { |
121 is_current_process_ = false; | 121 is_current_process_ = false; |
122 if (IsValid()) { | 122 if (IsValid()) { |
123 mx_status_t status = mx_handle_close(process_); | 123 mx_status_t status = mx_handle_close(process_); |
124 DCHECK_EQ(status, NO_ERROR); | 124 DCHECK_EQ(status, MX_OK); |
125 process_ = kNullProcessHandle; | 125 process_ = kNullProcessHandle; |
126 } | 126 } |
127 } | 127 } |
128 | 128 |
129 bool Process::Terminate(int exit_code, bool wait) const { | 129 bool Process::Terminate(int exit_code, bool wait) const { |
130 // exit_code isn't supportable. | 130 // exit_code isn't supportable. |
131 mx_status_t status = mx_task_kill(process_); | 131 mx_status_t status = mx_task_kill(process_); |
132 if (status == NO_ERROR && wait) { | 132 if (status == MX_OK && wait) { |
133 mx_signals_t signals; | 133 mx_signals_t signals; |
134 status = mx_object_wait_one(process_, MX_TASK_TERMINATED, | 134 status = mx_object_wait_one(process_, MX_TASK_TERMINATED, |
135 mx_deadline_after(MX_SEC(60)), &signals); | 135 mx_deadline_after(MX_SEC(60)), &signals); |
136 if (status != NO_ERROR) { | 136 if (status != MX_OK) { |
137 DLOG(ERROR) << "Error waiting for process exit: " << status; | 137 DLOG(ERROR) << "Error waiting for process exit: " << status; |
138 } else { | 138 } else { |
139 DCHECK(signals & MX_TASK_TERMINATED); | 139 DCHECK(signals & MX_TASK_TERMINATED); |
140 } | 140 } |
141 } else if (status != NO_ERROR) { | 141 } else if (status != MX_OK) { |
142 DLOG(ERROR) << "Unable to terminate process: " << status; | 142 DLOG(ERROR) << "Unable to terminate process: " << status; |
143 } | 143 } |
144 | 144 |
145 return status >= 0; | 145 return status >= 0; |
146 } | 146 } |
147 | 147 |
148 bool Process::WaitForExit(int* exit_code) const { | 148 bool Process::WaitForExit(int* exit_code) const { |
149 return WaitForExitWithTimeout(TimeDelta::Max(), exit_code); | 149 return WaitForExitWithTimeout(TimeDelta::Max(), exit_code); |
150 } | 150 } |
151 | 151 |
152 bool Process::WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) const { | 152 bool Process::WaitForExitWithTimeout(TimeDelta timeout, int* exit_code) const { |
153 // Record the event that this thread is blocking upon (for hang diagnosis). | 153 // Record the event that this thread is blocking upon (for hang diagnosis). |
154 base::debug::ScopedProcessWaitActivity process_activity(this); | 154 base::debug::ScopedProcessWaitActivity process_activity(this); |
155 | 155 |
156 mx_time_t mxtimeout_nanos = | 156 mx_time_t mxtimeout_nanos = |
157 timeout == TimeDelta::Max() ? MX_TIME_INFINITE : timeout.InNanoseconds(); | 157 timeout == TimeDelta::Max() ? MX_TIME_INFINITE : timeout.InNanoseconds(); |
158 mx_signals_t signals_observed; | 158 mx_signals_t signals_observed; |
159 mx_status_t status = mx_object_wait_one(process_, MX_TASK_TERMINATED, | 159 mx_status_t status = mx_object_wait_one(process_, MX_TASK_TERMINATED, |
160 mxtimeout_nanos, &signals_observed); | 160 mxtimeout_nanos, &signals_observed); |
161 *exit_code = -1; | 161 *exit_code = -1; |
162 if (status != NO_ERROR && status != ERR_TIMED_OUT) | 162 if (status != MX_OK && status != MX_ERR_TIMED_OUT) |
163 return false; | 163 return false; |
164 if (status == ERR_TIMED_OUT && !signals_observed) | 164 if (status == MX_ERR_TIMED_OUT && !signals_observed) |
165 return false; | 165 return false; |
166 | 166 |
167 mx_info_process_t proc_info; | 167 mx_info_process_t proc_info; |
168 status = mx_object_get_info(process_, MX_INFO_PROCESS, &proc_info, | 168 status = mx_object_get_info(process_, MX_INFO_PROCESS, &proc_info, |
169 sizeof(proc_info), nullptr, nullptr); | 169 sizeof(proc_info), nullptr, nullptr); |
170 if (status != NO_ERROR) | 170 if (status != MX_OK) |
171 return status; | 171 return status; |
172 | 172 |
173 *exit_code = proc_info.return_code; | 173 *exit_code = proc_info.return_code; |
174 return true; | 174 return true; |
175 } | 175 } |
176 | 176 |
177 bool Process::IsProcessBackgrounded() const { | 177 bool Process::IsProcessBackgrounded() const { |
178 // See SetProcessBackgrounded(). | 178 // See SetProcessBackgrounded(). |
179 DCHECK(IsValid()); | 179 DCHECK(IsValid()); |
180 return false; | 180 return false; |
181 } | 181 } |
182 | 182 |
183 bool Process::SetProcessBackgrounded(bool value) { | 183 bool Process::SetProcessBackgrounded(bool value) { |
184 // No process priorities on Fuchsia. TODO(fuchsia): See MG-783, and update | 184 // No process priorities on Fuchsia. TODO(fuchsia): See MG-783, and update |
185 // this later if priorities are implemented. | 185 // this later if priorities are implemented. |
186 return false; | 186 return false; |
187 } | 187 } |
188 | 188 |
189 int Process::GetPriority() const { | 189 int Process::GetPriority() const { |
190 DCHECK(IsValid()); | 190 DCHECK(IsValid()); |
191 // No process priorities on Fuchsia. | 191 // No process priorities on Fuchsia. |
192 return 0; | 192 return 0; |
193 } | 193 } |
194 | 194 |
195 } // namespace base | 195 } // namespace base |
OLD | NEW |