OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "services/native_support/process_controller_impl.h" | 5 #include "services/native_support/process_controller_impl.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <signal.h> | 8 #include <signal.h> |
9 #include <sys/types.h> | 9 #include <sys/types.h> |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
23 namespace native_support { | 23 namespace native_support { |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 void WaitForProcess( | 27 void WaitForProcess( |
28 base::Process process, | 28 base::Process process, |
29 scoped_refptr<base::TaskRunner> done_runner, | 29 scoped_refptr<base::TaskRunner> done_runner, |
30 const base::Callback<void(mojo::files::Error, int32_t)>& done_callback) { | 30 const base::Callback<void(mojo::files::Error, int32_t)>& done_callback) { |
31 int exit_status = 0; | 31 int exit_status = 0; |
32 mojo::files::Error result = process.WaitForExit(&exit_status) | 32 mojo::files::Error result = process.WaitForExit(&exit_status) |
33 ? mojo::files::ERROR_OK | 33 ? mojo::files::Error::OK |
34 : mojo::files::ERROR_UNKNOWN; | 34 : mojo::files::Error::UNKNOWN; |
35 done_runner->PostTask( | 35 done_runner->PostTask( |
36 FROM_HERE, | 36 FROM_HERE, |
37 base::Bind(done_callback, result, static_cast<int32_t>(exit_status))); | 37 base::Bind(done_callback, result, static_cast<int32_t>(exit_status))); |
38 } | 38 } |
39 | 39 |
40 void TerminateProcess(base::Process process) { | 40 void TerminateProcess(base::Process process) { |
41 if (!process.Terminate(-1, true)) | 41 if (!process.Terminate(-1, true)) |
42 LOG(ERROR) << "Failed to kill PID " << process.Pid(); | 42 LOG(ERROR) << "Failed to kill PID " << process.Pid(); |
43 } | 43 } |
44 | 44 |
(...skipping 15 matching lines...) Expand all Loading... |
60 ProcessControllerImpl::~ProcessControllerImpl() { | 60 ProcessControllerImpl::~ProcessControllerImpl() { |
61 if (process_.IsValid()) { | 61 if (process_.IsValid()) { |
62 worker_runner_->PostTask( | 62 worker_runner_->PostTask( |
63 FROM_HERE, base::Bind(&TerminateProcess, base::Passed(&process_))); | 63 FROM_HERE, base::Bind(&TerminateProcess, base::Passed(&process_))); |
64 } | 64 } |
65 } | 65 } |
66 | 66 |
67 void ProcessControllerImpl::Wait(const WaitCallback& callback) { | 67 void ProcessControllerImpl::Wait(const WaitCallback& callback) { |
68 if (!process_.IsValid()) { | 68 if (!process_.IsValid()) { |
69 // TODO(vtl): This isn't quite right. | 69 // TODO(vtl): This isn't quite right. |
70 callback.Run(mojo::files::ERROR_UNAVAILABLE, 0); | 70 callback.Run(mojo::files::Error::UNAVAILABLE, 0); |
71 return; | 71 return; |
72 } | 72 } |
73 | 73 |
74 worker_runner_->PostTask( | 74 worker_runner_->PostTask( |
75 FROM_HERE, base::Bind(&WaitForProcess, base::Passed(&process_), | 75 FROM_HERE, base::Bind(&WaitForProcess, base::Passed(&process_), |
76 base::MessageLoop::current()->task_runner(), | 76 base::MessageLoop::current()->task_runner(), |
77 base::Bind(&ProcessControllerImpl::OnWaitComplete, | 77 base::Bind(&ProcessControllerImpl::OnWaitComplete, |
78 weak_factory_.GetWeakPtr(), callback))); | 78 weak_factory_.GetWeakPtr(), callback))); |
79 } | 79 } |
80 | 80 |
81 void ProcessControllerImpl::Kill(int32_t signal, const KillCallback& callback) { | 81 void ProcessControllerImpl::Kill(int32_t signal, const KillCallback& callback) { |
82 callback.Run(KillHelper(signal)); | 82 callback.Run(KillHelper(signal)); |
83 } | 83 } |
84 | 84 |
85 void ProcessControllerImpl::OnWaitComplete(const WaitCallback& callback, | 85 void ProcessControllerImpl::OnWaitComplete(const WaitCallback& callback, |
86 mojo::files::Error result, | 86 mojo::files::Error result, |
87 int32_t exit_status) { | 87 int32_t exit_status) { |
88 callback.Run(result, exit_status); | 88 callback.Run(result, exit_status); |
89 } | 89 } |
90 | 90 |
91 mojo::files::Error ProcessControllerImpl::KillHelper(int32_t signal) { | 91 mojo::files::Error ProcessControllerImpl::KillHelper(int32_t signal) { |
92 if (signal < 0) | 92 if (signal < 0) |
93 return mojo::files::ERROR_INVALID_ARGUMENT; | 93 return mojo::files::Error::INVALID_ARGUMENT; |
94 | 94 |
95 if (!process_.IsValid()) { | 95 if (!process_.IsValid()) { |
96 LOG(ERROR) << "Kill() called after Wait()"; | 96 LOG(ERROR) << "Kill() called after Wait()"; |
97 // TODO(vtl): This error code isn't quite right, but "unavailable" (which | 97 // TODO(vtl): This error code isn't quite right, but "unavailable" (which |
98 // would also be wrong) is used for a more appropriate purpose below. | 98 // would also be wrong) is used for a more appropriate purpose below. |
99 return mojo::files::ERROR_INVALID_ARGUMENT; | 99 return mojo::files::Error::INVALID_ARGUMENT; |
100 } | 100 } |
101 | 101 |
102 // |base::HandleType| is just a typedef for |pid_t|. | 102 // |base::HandleType| is just a typedef for |pid_t|. |
103 pid_t pid = process_.Handle(); | 103 pid_t pid = process_.Handle(); |
104 | 104 |
105 // Note: |kill()| is not interruptible. | 105 // Note: |kill()| is not interruptible. |
106 if (kill(pid, static_cast<int>(signal)) == 0) | 106 if (kill(pid, static_cast<int>(signal)) == 0) |
107 return mojo::files::ERROR_OK; | 107 return mojo::files::Error::OK; |
108 | 108 |
109 switch (errno) { | 109 switch (errno) { |
110 case EINVAL: | 110 case EINVAL: |
111 return mojo::files::ERROR_INVALID_ARGUMENT; | 111 return mojo::files::Error::INVALID_ARGUMENT; |
112 case EPERM: | 112 case EPERM: |
113 return mojo::files::ERROR_PERMISSION_DENIED; | 113 return mojo::files::Error::PERMISSION_DENIED; |
114 case ESRCH: | 114 case ESRCH: |
115 return mojo::files::ERROR_UNAVAILABLE; | 115 return mojo::files::Error::UNAVAILABLE; |
116 default: | 116 default: |
117 break; | 117 break; |
118 } | 118 } |
119 return mojo::files::ERROR_UNKNOWN; | 119 return mojo::files::Error::UNKNOWN; |
120 } | 120 } |
121 | 121 |
122 } // namespace native_support | 122 } // namespace native_support |
OLD | NEW |