| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/browser/handle_enumerator_win.h" | 5 #include "content/common/handle_enumerator_win.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/process.h" | 12 #include "base/process.h" |
| 12 #include "base/process_util.h" | 13 #include "base/process_util.h" |
| 13 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 14 #include "base/win/windows_version.h" | 15 #include "base/win/windows_version.h" |
| 15 #include "content/browser/browser_child_process_host.h" | 16 #include "content/common/content_switches.h" |
| 16 #include "content/browser/browser_thread.h" | |
| 17 #include "content/browser/renderer_host/render_process_host.h" | |
| 18 #include "content/common/result_codes.h" | 17 #include "content/common/result_codes.h" |
| 19 #include "sandbox/src/handle_table.h" | 18 #include "sandbox/src/handle_table.h" |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 | 21 |
| 23 typedef std::map<const string16, content::HandleType> HandleTypeMap; | 22 typedef std::map<const string16, content::HandleType> HandleTypeMap; |
| 24 | 23 |
| 25 HandleTypeMap& MakeHandleTypeMap() { | 24 HandleTypeMap& MakeHandleTypeMap() { |
| 26 HandleTypeMap& handle_types = *(new HandleTypeMap()); | 25 HandleTypeMap& handle_types = *(new HandleTypeMap()); |
| 27 handle_types[sandbox::HandleTable::kTypeProcess] = content::ProcessHandle; | 26 handle_types[sandbox::HandleTable::kTypeProcess] = content::ProcessHandle; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 46 handle_types[sandbox::HandleTable::kTypeAlpcPort] = | 45 handle_types[sandbox::HandleTable::kTypeAlpcPort] = |
| 47 content::AlpcPortHandle; | 46 content::AlpcPortHandle; |
| 48 | 47 |
| 49 return handle_types; | 48 return handle_types; |
| 50 } | 49 } |
| 51 | 50 |
| 52 } // namespace | 51 } // namespace |
| 53 | 52 |
| 54 namespace content { | 53 namespace content { |
| 55 | 54 |
| 56 const wchar_t kNtdllDllName[] = L"ntdll.dll"; | |
| 57 const size_t kMaxHandleNameLength = 1024; | 55 const size_t kMaxHandleNameLength = 1024; |
| 58 | 56 |
| 59 void HandleEnumerator::EnumerateHandles() { | 57 void HandleEnumerator::EnumerateHandles() { |
| 60 sandbox::HandleTable handles; | 58 sandbox::HandleTable handles; |
| 61 | 59 std::string process_type = |
| 62 string16 output = ProcessTypeString(type_); | 60 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 63 output.append(ASCIIToUTF16(" Process - Handles at shutdown:\n")); | 61 switches::kProcessType); |
| 62 string16 output = ASCIIToUTF16(process_type); |
| 63 output.append(ASCIIToUTF16(" process - Handles at shutdown:\n")); |
| 64 for (sandbox::HandleTable::Iterator sys_handle | 64 for (sandbox::HandleTable::Iterator sys_handle |
| 65 = handles.HandlesForProcess(::GetProcessId(handle_)); | 65 = handles.HandlesForProcess(::GetCurrentProcessId()); |
| 66 sys_handle != handles.end(); ++sys_handle) { | 66 sys_handle != handles.end(); ++sys_handle) { |
| 67 HandleType current_type = StringToHandleType(sys_handle->Type()); | 67 HandleType current_type = StringToHandleType(sys_handle->Type()); |
| 68 if (!all_handles_ && (current_type != ProcessHandle && | 68 if (!all_handles_ && (current_type != ProcessHandle && |
| 69 current_type != FileHandle && | 69 current_type != FileHandle && |
| 70 current_type != DirectoryHandle && | 70 current_type != DirectoryHandle && |
| 71 current_type != KeyHandle && | 71 current_type != KeyHandle && |
| 72 current_type != WindowStationHandle && | 72 current_type != WindowStationHandle && |
| 73 current_type != DesktopHandle && | 73 current_type != DesktopHandle && |
| 74 current_type != ServiceHandle)) | 74 current_type != ServiceHandle)) |
| 75 continue; | 75 continue; |
| 76 | 76 |
| 77 output += ASCIIToUTF16("["); | 77 output += ASCIIToUTF16("["); |
| 78 output += sys_handle->Type(); | 78 output += sys_handle->Type(); |
| 79 output += ASCIIToUTF16("] ("); | 79 output += ASCIIToUTF16("] ("); |
| 80 output += sys_handle->Name(); | 80 output += sys_handle->Name(); |
| 81 output += ASCIIToUTF16(")\n"); | 81 output += ASCIIToUTF16(")\n"); |
| 82 output += GetAccessString(current_type, | 82 output += GetAccessString(current_type, |
| 83 sys_handle->handle_entry()->GrantedAccess); | 83 sys_handle->handle_entry()->GrantedAccess); |
| 84 } | 84 } |
| 85 LOG(INFO) << output; | 85 LOG(INFO) << output; |
| 86 } | 86 } |
| 87 | 87 |
| 88 void HandleEnumerator::RunHandleEnumeration() { | |
| 89 DCHECK(status_ == Starting); | |
| 90 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) | |
| 91 FindProcessOnIOThread(); | |
| 92 else if (BrowserThread::CurrentlyOn(BrowserThread::UI)) | |
| 93 FindProcessOnUIThread(); | |
| 94 else | |
| 95 BrowserThread::PostTask( | |
| 96 BrowserThread::IO, FROM_HERE, | |
| 97 NewRunnableMethod(this, | |
| 98 &HandleEnumerator::FindProcessOnIOThread)); | |
| 99 } | |
| 100 | |
| 101 void HandleEnumerator::FindProcessOnIOThread() { | |
| 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 103 | |
| 104 for (BrowserChildProcessHost::Iterator iter; !iter.Done(); ++iter) { | |
| 105 if (iter->handle() == handle_) { | |
| 106 type_ = iter->type(); | |
| 107 status_ = Finished; | |
| 108 break; | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 if (status_ == Starting) { | |
| 113 status_ = InProgress; | |
| 114 BrowserThread::PostTask( | |
| 115 BrowserThread::UI, FROM_HERE, | |
| 116 NewRunnableMethod(this, | |
| 117 &HandleEnumerator::FindProcessOnUIThread)); | |
| 118 } else { | |
| 119 status_ = Finished; | |
| 120 BrowserThread::PostTask( | |
| 121 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, | |
| 122 NewRunnableMethod(this, | |
| 123 &HandleEnumerator::EnumerateHandlesAndTerminateProcess)); | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 void HandleEnumerator::FindProcessOnUIThread() { | |
| 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 129 | |
| 130 for (RenderProcessHost::iterator renderer_iter( | |
| 131 RenderProcessHost::AllHostsIterator()); !renderer_iter.IsAtEnd(); | |
| 132 renderer_iter.Advance()) { | |
| 133 RenderProcessHost* render_process_host = renderer_iter.GetCurrentValue(); | |
| 134 if (render_process_host->GetHandle() == handle_) { | |
| 135 type_ = ChildProcessInfo::RENDER_PROCESS; | |
| 136 status_ = Finished; | |
| 137 break; | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 if (status_ == Starting) { | |
| 142 status_ = InProgress; | |
| 143 BrowserThread::PostTask( | |
| 144 BrowserThread::IO, FROM_HERE, | |
| 145 NewRunnableMethod(this, | |
| 146 &HandleEnumerator::FindProcessOnIOThread)); | |
| 147 } else { | |
| 148 status_ = Finished; | |
| 149 BrowserThread::PostTask( | |
| 150 BrowserThread::PROCESS_LAUNCHER, FROM_HERE, | |
| 151 NewRunnableMethod(this, | |
| 152 &HandleEnumerator::EnumerateHandlesAndTerminateProcess)); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 void HandleEnumerator::EnumerateHandlesAndTerminateProcess() { | |
| 157 HandleEnumerator::EnumerateHandles(); | |
| 158 base::Process process(handle_); | |
| 159 process.Terminate(content::RESULT_CODE_NORMAL_EXIT); | |
| 160 process.Close(); | |
| 161 } | |
| 162 | |
| 163 string16 ProcessTypeString(ChildProcessInfo::ProcessType process_type) { | |
| 164 switch (process_type) { | |
| 165 case ChildProcessInfo::UNKNOWN_PROCESS: | |
| 166 return ASCIIToUTF16("Unknown"); | |
| 167 case ChildProcessInfo::BROWSER_PROCESS: | |
| 168 return ASCIIToUTF16("Browser"); | |
| 169 case ChildProcessInfo::RENDER_PROCESS: | |
| 170 return ASCIIToUTF16("Renderer"); | |
| 171 case ChildProcessInfo::PLUGIN_PROCESS: | |
| 172 return ASCIIToUTF16("Plugin"); | |
| 173 case ChildProcessInfo::WORKER_PROCESS: | |
| 174 return ASCIIToUTF16("Worker"); | |
| 175 case ChildProcessInfo::NACL_LOADER_PROCESS: | |
| 176 return ASCIIToUTF16("NaCL Loader"); | |
| 177 case ChildProcessInfo::UTILITY_PROCESS: | |
| 178 return ASCIIToUTF16("Utility"); | |
| 179 case ChildProcessInfo::PROFILE_IMPORT_PROCESS: | |
| 180 return ASCIIToUTF16("Profile Import"); | |
| 181 case ChildProcessInfo::ZYGOTE_PROCESS: | |
| 182 return ASCIIToUTF16("Zygote"); | |
| 183 case ChildProcessInfo::SANDBOX_HELPER_PROCESS: | |
| 184 return ASCIIToUTF16("Sandbox Helper"); | |
| 185 case ChildProcessInfo::NACL_BROKER_PROCESS: | |
| 186 return ASCIIToUTF16("NaCL Broker"); | |
| 187 case ChildProcessInfo::GPU_PROCESS: | |
| 188 return ASCIIToUTF16("GPU"); | |
| 189 case ChildProcessInfo::PPAPI_PLUGIN_PROCESS: | |
| 190 return ASCIIToUTF16("Pepper Plugin"); | |
| 191 case ChildProcessInfo::PPAPI_BROKER_PROCESS: | |
| 192 return ASCIIToUTF16("Pepper Broker"); | |
| 193 default: | |
| 194 return string16(); | |
| 195 } | |
| 196 } | |
| 197 | |
| 198 HandleType StringToHandleType(const string16& type) { | 88 HandleType StringToHandleType(const string16& type) { |
| 199 static HandleTypeMap handle_types = MakeHandleTypeMap(); | 89 static HandleTypeMap handle_types = MakeHandleTypeMap(); |
| 200 HandleTypeMap::iterator result = handle_types.find(type); | 90 HandleTypeMap::iterator result = handle_types.find(type); |
| 201 return result != handle_types.end() ? result->second : OtherHandle; | 91 return result != handle_types.end() ? result->second : OtherHandle; |
| 202 } | 92 } |
| 203 | 93 |
| 204 string16 GetAccessString(HandleType handle_type, | 94 string16 GetAccessString(HandleType handle_type, |
| 205 ACCESS_MASK access) { | 95 ACCESS_MASK access) { |
| 206 string16 output; | 96 string16 output; |
| 207 if (access & GENERIC_READ) | 97 if (access & GENERIC_READ) |
| (...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 if (access & FILE_MAP_READ) | 320 if (access & FILE_MAP_READ) |
| 431 output.append(ASCIIToUTF16("\tFILE_MAP_READ\n")); | 321 output.append(ASCIIToUTF16("\tFILE_MAP_READ\n")); |
| 432 if (access & FILE_MAP_WRITE) | 322 if (access & FILE_MAP_WRITE) |
| 433 output.append(ASCIIToUTF16("\tFILE_MAP_WRITE\n")); | 323 output.append(ASCIIToUTF16("\tFILE_MAP_WRITE\n")); |
| 434 break; | 324 break; |
| 435 } | 325 } |
| 436 return output; | 326 return output; |
| 437 } | 327 } |
| 438 | 328 |
| 439 } // namespace content | 329 } // namespace content |
| OLD | NEW |