OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 // The main point of this class is to cache ARC proc nspid<->pid mapping | 5 // The main point of this class is to cache ARC proc nspid<->pid mapping |
6 // globally. Since the calculation is costly, a dedicated worker thread is | 6 // globally. Since the calculation is costly, a dedicated worker thread is |
7 // used. All read/write of its internal data structure (i.e., the mapping) | 7 // used. All read/write of its internal data structure (i.e., the mapping) |
8 // should be on this thread. | 8 // should be on this thread. |
9 | 9 |
10 #include "chrome/browser/chromeos/arc/arc_process_service.h" | 10 #include "chrome/browser/chromeos/arc/arc_process_service.h" |
11 | 11 |
12 #include <algorithm> | |
12 #include <queue> | 13 #include <queue> |
13 #include <set> | 14 #include <set> |
14 #include <string> | 15 #include <string> |
15 | 16 |
17 #include "base/callback.h" | |
16 #include "base/process/process.h" | 18 #include "base/process/process.h" |
17 #include "base/process/process_iterator.h" | 19 #include "base/process/process_iterator.h" |
18 #include "base/task_runner_util.h" | 20 #include "base/task_runner_util.h" |
19 #include "base/trace_event/trace_event.h" | 21 #include "base/trace_event/trace_event.h" |
20 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
21 | 23 |
22 namespace arc { | 24 namespace arc { |
23 | 25 |
24 namespace { | |
25 | |
26 const char kSequenceToken[] = "arc_process_service"; | |
27 | |
28 // Weak pointer. This class is owned by ArcServiceManager. | |
29 ArcProcessService* g_arc_process_service = nullptr; | |
30 | |
31 } // namespace | |
32 | |
33 using base::kNullProcessId; | 26 using base::kNullProcessId; |
34 using base::Process; | 27 using base::Process; |
35 using base::ProcessId; | 28 using base::ProcessId; |
36 using base::SequencedWorkerPool; | 29 using base::SequencedWorkerPool; |
37 using std::map; | 30 using std::map; |
38 using std::set; | 31 using std::set; |
39 using std::vector; | 32 using std::vector; |
40 | 33 |
41 ArcProcessService::ArcProcessService(ArcBridgeService* bridge_service) | 34 namespace { |
42 : ArcService(bridge_service), | 35 |
43 worker_pool_(new SequencedWorkerPool(1, "arc_process_manager")), | 36 // Weak pointer. This class is owned by ArcServiceManager. |
44 weak_ptr_factory_(this) { | 37 ArcProcessService* g_arc_process_service = nullptr; |
45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 38 static constexpr char kInitName[] = "/init"; |
46 arc_bridge_service()->process()->AddObserver(this); | 39 |
47 DCHECK(!g_arc_process_service); | 40 // Matches the process name "/init" in the process tree and get the |
48 g_arc_process_service = this; | 41 // corresponding process ID. |
49 // Not intended to be used from the creating thread. | 42 base::ProcessId GetArcInitProcessId( |
50 thread_checker_.DetachFromThread(); | 43 const base::ProcessIterator::ProcessEntries& entry_list) { |
44 for (const base::ProcessEntry& entry : entry_list) { | |
45 if (entry.cmd_line_args().empty()) { | |
46 continue; | |
47 } | |
48 // TODO(nya): Add more constraints to avoid mismatches. | |
49 std::string process_name = entry.cmd_line_args()[0]; | |
Luis Héctor Chávez
2016/07/25 18:34:11
nit: const std::string&
Hsu-Cheng
2016/07/27 08:08:00
Done.
| |
50 if (process_name == kInitName) { | |
51 return entry.pid(); | |
52 } | |
53 } | |
54 return base::kNullProcessId; | |
51 } | 55 } |
52 | 56 |
53 ArcProcessService::~ArcProcessService() { | 57 std::vector<arc::ArcProcess> GetArcSystemProcessList() { |
54 DCHECK(g_arc_process_service == this); | 58 std::vector<arc::ArcProcess> ret_processes; |
55 g_arc_process_service = nullptr; | 59 const base::ProcessIterator::ProcessEntries& entry_list = |
56 arc_bridge_service()->process()->RemoveObserver(this); | 60 base::ProcessIterator(nullptr).Snapshot(); |
57 worker_pool_->Shutdown(); | 61 const base::ProcessId arc_init_pid = GetArcInitProcessId(entry_list); |
62 | |
63 if (arc_init_pid == base::kNullProcessId) { | |
64 return ret_processes; | |
65 } | |
66 | |
67 // Enumerate the child processes of ARC init for gathering ARC System | |
68 // Processes. | |
69 for (const base::ProcessEntry& entry : entry_list) { | |
70 if (entry.cmd_line_args().empty()) { | |
71 continue; | |
72 } | |
73 // TODO(hctsai): For now, we only gather direct child process of init, need | |
74 // to get the processes below. For example, installd might | |
75 // fork dex2oat and it can be executed for minutes. | |
76 if (entry.parent_pid() == arc_init_pid) { | |
77 const base::ProcessId child_pid = entry.pid(); | |
78 const base::ProcessId child_nspid = | |
79 base::Process(child_pid).GetPidInNamespace(); | |
80 if (child_nspid != base::kNullProcessId) { | |
81 const std::string process_name = entry.cmd_line_args()[0]; | |
Luis Héctor Chávez
2016/07/25 18:34:11
nit: const std::string&
Hsu-Cheng
2016/07/27 08:08:00
Done.
| |
82 ret_processes.emplace_back(child_nspid, child_pid, process_name, | |
83 mojom::ProcessState::PERSISTENT); | |
84 } | |
85 } | |
86 } | |
87 | |
88 return ret_processes; | |
58 } | 89 } |
59 | 90 |
60 // static | 91 void UpdateNspidToPidMap( |
61 ArcProcessService* ArcProcessService::Get() { | 92 scoped_refptr<ArcProcessService::NSPidToPidMap> pid_map) { |
62 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
63 return g_arc_process_service; | |
64 } | |
65 | |
66 void ArcProcessService::OnInstanceReady() { | |
67 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
68 worker_pool_->PostNamedSequencedWorkerTask( | |
69 kSequenceToken, | |
70 FROM_HERE, | |
71 base::Bind(&ArcProcessService::Reset, | |
72 weak_ptr_factory_.GetWeakPtr())); | |
73 } | |
74 | |
75 void ArcProcessService::Reset() { | |
76 DCHECK(thread_checker_.CalledOnValidThread()); | |
77 nspid_to_pid_.clear(); | |
78 } | |
79 | |
80 bool ArcProcessService::RequestProcessList( | |
81 RequestProcessListCallback callback) { | |
82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
83 | |
84 arc::mojom::ProcessInstance* process_instance = | |
85 arc_bridge_service()->process()->instance(); | |
86 if (!process_instance) { | |
87 return false; | |
88 } | |
89 process_instance->RequestProcessList( | |
90 base::Bind(&ArcProcessService::OnReceiveProcessList, | |
91 weak_ptr_factory_.GetWeakPtr(), | |
92 callback)); | |
93 return true; | |
94 } | |
95 | |
96 void ArcProcessService::OnReceiveProcessList( | |
97 const RequestProcessListCallback& callback, | |
98 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> mojo_processes) { | |
99 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
100 | |
101 auto raw_processes = new vector<mojom::RunningAppProcessInfoPtr>(); | |
102 mojo_processes.Swap(raw_processes); | |
103 | |
104 auto ret_processes = new vector<ArcProcess>(); | |
105 // Post to its dedicated worker thread to avoid race condition. | |
106 // Since no two tasks with the same token should be run at the same. | |
107 // Note: GetSequencedTaskRunner's shutdown behavior defaults to | |
108 // SKIP_ON_SHUTDOWN (ongoing task blocks shutdown). | |
109 // So in theory using Unretained(this) should be fine since the life cycle | |
110 // of |this| is the same as the main browser. | |
111 // To be safe I still use weak pointers, but weak_ptrs can only bind to | |
112 // methods without return values. That's why I can't use | |
113 // PostTaskAndReplyWithResult but handle the return object by myself. | |
114 auto runner = worker_pool_->GetSequencedTaskRunner( | |
115 worker_pool_->GetNamedSequenceToken(kSequenceToken)); | |
116 runner->PostTaskAndReply( | |
117 FROM_HERE, | |
118 base::Bind(&ArcProcessService::UpdateAndReturnProcessList, | |
119 weak_ptr_factory_.GetWeakPtr(), | |
120 base::Owned(raw_processes), | |
121 base::Unretained(ret_processes)), | |
122 base::Bind(&ArcProcessService::CallbackRelay, | |
123 weak_ptr_factory_.GetWeakPtr(), | |
124 callback, | |
125 base::Owned(ret_processes))); | |
126 } | |
127 | |
128 void ArcProcessService::CallbackRelay( | |
129 const RequestProcessListCallback& callback, | |
130 const vector<ArcProcess>* ret_processes) { | |
131 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
132 callback.Run(*ret_processes); | |
133 } | |
134 | |
135 void ArcProcessService::UpdateAndReturnProcessList( | |
136 const vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, | |
137 vector<ArcProcess>* ret_processes) { | |
138 DCHECK(thread_checker_.CalledOnValidThread()); | |
139 | |
140 // Cleanup dead pids in the cache |nspid_to_pid_|. | |
141 set<ProcessId> nspid_to_remove; | |
142 for (const auto& entry : nspid_to_pid_) { | |
143 nspid_to_remove.insert(entry.first); | |
144 } | |
145 bool unmapped_nspid = false; | |
146 for (const auto& entry : *raw_processes) { | |
147 // erase() returns 0 if coudln't find the key. It means a new process. | |
148 if (nspid_to_remove.erase(entry->pid) == 0) { | |
149 nspid_to_pid_[entry->pid] = kNullProcessId; | |
150 unmapped_nspid = true; | |
151 } | |
152 } | |
153 for (const auto& entry : nspid_to_remove) { | |
154 nspid_to_pid_.erase(entry); | |
155 } | |
156 | |
157 // The operation is costly so avoid calling it when possible. | |
158 if (unmapped_nspid) { | |
159 UpdateNspidToPidMap(); | |
160 } | |
161 | |
162 PopulateProcessList(raw_processes, ret_processes); | |
163 } | |
164 | |
165 void ArcProcessService::PopulateProcessList( | |
166 const vector<arc::mojom::RunningAppProcessInfoPtr>* raw_processes, | |
167 vector<ArcProcess>* ret_processes) { | |
168 DCHECK(thread_checker_.CalledOnValidThread()); | |
169 | |
170 for (const auto& entry : *raw_processes) { | |
171 const auto it = nspid_to_pid_.find(entry->pid); | |
172 // In case the process already dies so couldn't find corresponding pid. | |
173 if (it != nspid_to_pid_.end() && it->second != kNullProcessId) { | |
174 ArcProcess arc_process(entry->pid, it->second, entry->process_name, | |
175 entry->process_state); | |
176 // |entry->packages| is provided only when process.mojom's verion is >=4. | |
177 if (entry->packages) { | |
178 for (const auto& package : entry->packages) { | |
179 arc_process.packages().push_back(package.get()); | |
180 } | |
181 } | |
182 ret_processes->push_back(std::move(arc_process)); | |
183 } | |
184 } | |
185 } | |
186 | |
187 // Computes a map from PID in ARC namespace to PID in system namespace. | |
188 // The returned map contains ARC processes only. | |
189 void ArcProcessService::UpdateNspidToPidMap() { | |
190 DCHECK(thread_checker_.CalledOnValidThread()); | |
191 | |
192 TRACE_EVENT0("browser", "ArcProcessService::UpdateNspidToPidMap"); | 93 TRACE_EVENT0("browser", "ArcProcessService::UpdateNspidToPidMap"); |
193 | 94 |
194 // NB: Despite of its name, ProcessIterator::Snapshot() may return | 95 // NB: Despite of its name, ProcessIterator::Snapshot() may return |
195 // inconsistent information because it simply walks procfs. Especially | 96 // inconsistent information because it simply walks procfs. Especially |
196 // we must not assume the parent-child relationships are consistent. | 97 // we must not assume the parent-child relationships are consistent. |
197 const base::ProcessIterator::ProcessEntries& entry_list = | 98 const base::ProcessIterator::ProcessEntries& entry_list = |
198 base::ProcessIterator(nullptr).Snapshot(); | 99 base::ProcessIterator(nullptr).Snapshot(); |
199 | 100 |
200 // System may contain many different namespaces so several different | |
201 // processes may have the same nspid. We need to get the proper subset of | |
202 // processes to create correct nspid -> pid map. | |
203 | |
204 // Construct the process tree. | 101 // Construct the process tree. |
205 // NB: This can contain a loop in case of race conditions. | 102 // NB: This can contain a loop in case of race conditions. |
206 map<ProcessId, vector<ProcessId> > process_tree; | 103 map<ProcessId, std::vector<ProcessId>> process_tree; |
207 for (const base::ProcessEntry& entry : entry_list) | 104 for (const base::ProcessEntry& entry : entry_list) |
208 process_tree[entry.parent_pid()].push_back(entry.pid()); | 105 process_tree[entry.parent_pid()].push_back(entry.pid()); |
209 | 106 |
210 // Find the ARC init process. | 107 ProcessId arc_init_pid = GetArcInitProcessId(entry_list); |
211 ProcessId arc_init_pid = kNullProcessId; | |
212 for (const base::ProcessEntry& entry : entry_list) { | |
213 // TODO(nya): Add more constraints to avoid mismatches. | |
214 std::string process_name = | |
215 !entry.cmd_line_args().empty() ? entry.cmd_line_args()[0] : ""; | |
216 if (process_name == "/init") { | |
217 arc_init_pid = entry.pid(); | |
218 break; | |
219 } | |
220 } | |
221 | 108 |
222 // Enumerate all processes under ARC init and create nspid -> pid map. | 109 // Enumerate all processes under ARC init and create nspid -> pid map. |
223 if (arc_init_pid != kNullProcessId) { | 110 if (arc_init_pid != kNullProcessId) { |
224 std::queue<ProcessId> queue; | 111 std::queue<ProcessId> queue; |
225 std::set<ProcessId> visited; | 112 std::set<ProcessId> visited; |
Luis Héctor Chávez
2016/07/25 18:34:11
can all the std::sets be std::unordered_sets and s
Hsu-Cheng
2016/07/27 08:08:00
Done.
| |
226 queue.push(arc_init_pid); | 113 queue.push(arc_init_pid); |
227 while (!queue.empty()) { | 114 while (!queue.empty()) { |
228 ProcessId pid = queue.front(); | 115 ProcessId pid = queue.front(); |
229 queue.pop(); | 116 queue.pop(); |
230 // Do not visit the same process twice. Otherwise we may enter an infinite | 117 // Do not visit the same process twice. Otherwise we may enter an infinite |
231 // loop if |process_tree| contains a loop. | 118 // loop if |process_tree| contains a loop. |
232 if (!visited.insert(pid).second) | 119 if (!visited.insert(pid).second) |
233 continue; | 120 continue; |
234 | 121 |
235 ProcessId nspid = base::Process(pid).GetPidInNamespace(); | 122 const ProcessId nspid = base::Process(pid).GetPidInNamespace(); |
236 | 123 |
237 // All ARC processes should be in namespace so nspid is usually non-null, | 124 // All ARC processes should be in namespace so nspid is usually non-null, |
238 // but this can happen if the process has already gone. | 125 // but this can happen if the process has already gone. |
239 // Only add processes we're interested in (those appear as keys in | 126 // Only add processes we're interested in (those appear as keys in |
240 // |nspid_to_pid_|). | 127 // |pid_map|). |
241 if (nspid != kNullProcessId && | 128 if (nspid != kNullProcessId && pid_map->find(nspid) != pid_map->end()) |
242 nspid_to_pid_.find(nspid) != nspid_to_pid_.end()) | 129 (*pid_map)[nspid] = pid; |
243 nspid_to_pid_[nspid] = pid; | |
244 | 130 |
245 for (ProcessId child_pid : process_tree[pid]) | 131 for (ProcessId child_pid : process_tree[pid]) |
246 queue.push(child_pid); | 132 queue.push(child_pid); |
247 } | 133 } |
248 } | 134 } |
249 } | 135 } |
250 | 136 |
137 std::vector<ArcProcess> FilterProcessList( | |
138 const ArcProcessService::NSPidToPidMap& pid_map, | |
139 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> processes) { | |
140 std::vector<ArcProcess> ret_processes; | |
141 for (const auto& entry : processes) { | |
142 const auto it = pid_map.find(entry->pid); | |
143 // The nspid could be missing due to race condition. For example, the | |
144 // process is still running when we get the process snapshot and ends when | |
145 // we update the nspid to pid mapping. | |
146 if (it == pid_map.end() || it->second == base::kNullProcessId) { | |
147 continue; | |
148 } | |
149 // Constructs the ArcProcess instance if the mapping is found. | |
150 ArcProcess arc_process(entry->pid, pid_map.at(entry->pid), | |
151 entry->process_name, entry->process_state); | |
152 // |entry->packages| is provided only when process.mojom's verion is >=4. | |
153 if (entry->packages) { | |
154 for (const auto& package : entry->packages) { | |
155 arc_process.packages().push_back(package.get()); | |
156 } | |
157 } | |
158 ret_processes.push_back(std::move(arc_process)); | |
159 } | |
160 return ret_processes; | |
161 } | |
162 | |
163 std::vector<ArcProcess> UpdateAndReturnProcessList( | |
164 scoped_refptr<ArcProcessService::NSPidToPidMap> nspid_map, | |
165 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> processes) { | |
166 ArcProcessService::NSPidToPidMap& pid_map = *nspid_map; | |
167 // Cleanup dead pids in the cache |pid_map|. | |
168 std::unordered_set<ProcessId> nspid_to_remove; | |
169 for (const auto& entry : pid_map) { | |
170 nspid_to_remove.insert(entry.first); | |
171 } | |
172 bool unmapped_nspid = false; | |
173 for (const auto& entry : processes) { | |
174 // erase() returns 0 if coudln't find the key. It means a new process. | |
175 if (nspid_to_remove.erase(entry->pid) == 0) { | |
176 pid_map[entry->pid] = base::kNullProcessId; | |
177 unmapped_nspid = true; | |
178 } | |
179 } | |
180 for (const auto& entry : nspid_to_remove) { | |
181 pid_map.erase(entry); | |
182 } | |
183 | |
184 // The operation is costly so avoid calling it when possible. | |
185 if (unmapped_nspid) { | |
186 UpdateNspidToPidMap(nspid_map); | |
187 } | |
188 | |
189 return FilterProcessList(pid_map, std::move(processes)); | |
190 } | |
191 | |
192 void Reset(scoped_refptr<ArcProcessService::NSPidToPidMap> pid_map) { | |
193 pid_map->clear(); | |
194 } | |
195 | |
196 } // namespace | |
197 | |
198 ArcProcessService::ArcProcessService(ArcBridgeService* bridge_service) | |
199 : ArcService(bridge_service), | |
200 heavy_task_thread_("ArcProcessServiceThread"), | |
201 weak_ptr_factory_(this) { | |
202 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
203 arc_bridge_service()->process()->AddObserver(this); | |
204 DCHECK(!g_arc_process_service); | |
205 g_arc_process_service = this; | |
206 heavy_task_thread_.Start(); | |
207 } | |
208 | |
209 ArcProcessService::~ArcProcessService() { | |
210 DCHECK(g_arc_process_service == this); | |
211 g_arc_process_service = nullptr; | |
212 arc_bridge_service()->process()->RemoveObserver(this); | |
213 heavy_task_thread_.Stop(); | |
214 } | |
215 | |
216 // static | |
217 ArcProcessService* ArcProcessService::Get() { | |
218 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
219 return g_arc_process_service; | |
220 } | |
221 | |
222 void ArcProcessService::OnInstanceReady() { | |
223 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
224 GetTaskRunner()->PostTask(FROM_HERE, base::Bind(&Reset, nspid_to_pid_)); | |
225 } | |
226 | |
227 void ArcProcessService::RequestSystemProcessList( | |
228 RequestProcessListCallback callback) { | |
229 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
230 | |
231 base::PostTaskAndReplyWithResult(GetTaskRunner().get(), FROM_HERE, | |
232 base::Bind(&GetArcSystemProcessList), | |
233 callback); | |
234 } | |
235 | |
236 bool ArcProcessService::RequestAppProcessList( | |
237 RequestProcessListCallback callback) { | |
238 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
239 | |
240 arc::mojom::ProcessInstance* process_instance = | |
241 arc_bridge_service()->process()->instance(); | |
242 if (!process_instance) { | |
243 return false; | |
244 } | |
245 process_instance->RequestProcessList( | |
246 base::Bind(&ArcProcessService::OnReceiveProcessList, | |
247 weak_ptr_factory_.GetWeakPtr(), callback)); | |
248 return true; | |
249 } | |
250 | |
251 void ArcProcessService::OnReceiveProcessList( | |
252 const RequestProcessListCallback& callback, | |
253 mojo::Array<arc::mojom::RunningAppProcessInfoPtr> instance_processes) { | |
254 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
255 | |
256 base::PostTaskAndReplyWithResult( | |
257 GetTaskRunner().get(), FROM_HERE, | |
258 base::Bind(&UpdateAndReturnProcessList, nspid_to_pid_, | |
259 base::Passed(&instance_processes)), | |
260 callback); | |
261 } | |
262 | |
263 scoped_refptr<base::SingleThreadTaskRunner> ArcProcessService::GetTaskRunner() { | |
264 return heavy_task_thread_.task_runner(); | |
265 } | |
266 | |
251 } // namespace arc | 267 } // namespace arc |
OLD | NEW |