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/kill.h" | 5 #include "base/process/kill.h" |
6 | 6 |
7 #include <magenta/syscalls.h> | 7 #include <magenta/syscalls.h> |
8 | 8 |
9 #include "base/process/process_iterator.h" | 9 #include "base/process/process_iterator.h" |
10 #include "base/task_scheduler/post_task.h" | 10 #include "base/task_scheduler/post_task.h" |
11 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
12 | 12 |
13 namespace base { | 13 namespace base { |
14 | 14 |
15 bool KillProcessGroup(ProcessHandle process_group_id) { | 15 bool KillProcessGroup(ProcessHandle process_group_id) { |
16 // |process_group_id| is really a job on Fuchsia. | 16 // |process_group_id| is really a job on Fuchsia. |
17 mx_status_t status = mx_task_kill(process_group_id); | 17 mx_status_t status = mx_task_kill(process_group_id); |
18 DLOG_IF(ERROR, status != NO_ERROR) | 18 DLOG_IF(ERROR, status != MX_OK) |
19 << "unable to terminate job " << process_group_id; | 19 << "unable to terminate job " << process_group_id; |
20 return status == NO_ERROR; | 20 return status == MX_OK; |
21 } | 21 } |
22 | 22 |
23 TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) { | 23 TerminationStatus GetTerminationStatus(ProcessHandle handle, int* exit_code) { |
24 mx_info_process_t process_info; | 24 mx_info_process_t process_info; |
25 mx_status_t status = | 25 mx_status_t status = |
26 mx_object_get_info(handle, MX_INFO_PROCESS, &process_info, | 26 mx_object_get_info(handle, MX_INFO_PROCESS, &process_info, |
27 sizeof(process_info), nullptr, nullptr); | 27 sizeof(process_info), nullptr, nullptr); |
28 if (status != NO_ERROR) { | 28 if (status != MX_OK) { |
29 DLOG(ERROR) << "unable to get termination status for " << handle; | 29 DLOG(ERROR) << "unable to get termination status for " << handle; |
30 *exit_code = 0; | 30 *exit_code = 0; |
31 return TERMINATION_STATUS_NORMAL_TERMINATION; | 31 return TERMINATION_STATUS_NORMAL_TERMINATION; |
32 } | 32 } |
33 if (!process_info.started) { | 33 if (!process_info.started) { |
34 return TERMINATION_STATUS_LAUNCH_FAILED; | 34 return TERMINATION_STATUS_LAUNCH_FAILED; |
35 } | 35 } |
36 if (!process_info.exited) { | 36 if (!process_info.exited) { |
37 return TERMINATION_STATUS_STILL_RUNNING; | 37 return TERMINATION_STATUS_STILL_RUNNING; |
38 } | 38 } |
39 | 39 |
40 // TODO(fuchsia): Is there more information about types of crashes, OOM, etc. | 40 // TODO(fuchsia): Is there more information about types of crashes, OOM, etc. |
41 // available? https://crbug.com/706592. | 41 // available? https://crbug.com/706592. |
42 | 42 |
43 *exit_code = process_info.return_code; | 43 *exit_code = process_info.return_code; |
44 return process_info.return_code == 0 | 44 return process_info.return_code == 0 |
45 ? TERMINATION_STATUS_NORMAL_TERMINATION | 45 ? TERMINATION_STATUS_NORMAL_TERMINATION |
46 : TERMINATION_STATUS_ABNORMAL_TERMINATION; | 46 : TERMINATION_STATUS_ABNORMAL_TERMINATION; |
47 } | 47 } |
48 | 48 |
49 void EnsureProcessTerminated(Process process) { | 49 void EnsureProcessTerminated(Process process) { |
50 DCHECK(!process.is_current()); | 50 DCHECK(!process.is_current()); |
51 | 51 |
52 // Wait for up to two seconds for the process to terminate, and then kill it | 52 // Wait for up to two seconds for the process to terminate, and then kill it |
53 // forcefully if it hasn't already exited. | 53 // forcefully if it hasn't already exited. |
54 mx_signals_t signals; | 54 mx_signals_t signals; |
55 if (mx_object_wait_one(process.Handle(), MX_TASK_TERMINATED, | 55 if (mx_object_wait_one(process.Handle(), MX_TASK_TERMINATED, |
56 mx_deadline_after(MX_SEC(2)), &signals) == NO_ERROR) { | 56 mx_deadline_after(MX_SEC(2)), &signals) == MX_OK) { |
57 DCHECK(signals & MX_TASK_TERMINATED); | 57 DCHECK(signals & MX_TASK_TERMINATED); |
58 // If already signaled, then the process is terminated. | 58 // If already signaled, then the process is terminated. |
59 return; | 59 return; |
60 } | 60 } |
61 | 61 |
62 process.Terminate(/*exit_code=*/1, /*wait=*/false); | 62 process.Terminate(/*exit_code=*/1, /*wait=*/false); |
63 } | 63 } |
64 | 64 |
65 } // namespace base | 65 } // namespace base |
OLD | NEW |