Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/callback.h" | 5 #include "base/callback.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "sandbox/win/src/sharedmem_ipc_server.h" | 8 #include "sandbox/win/src/sharedmem_ipc_server.h" |
| 9 #include "sandbox/win/src/sharedmem_ipc_client.h" | 9 #include "sandbox/win/src/sharedmem_ipc_client.h" |
| 10 #include "sandbox/win/src/sandbox.h" | 10 #include "sandbox/win/src/sandbox.h" |
| 11 #include "sandbox/win/src/sandbox_types.h" | 11 #include "sandbox/win/src/sandbox_types.h" |
| 12 #include "sandbox/win/src/crosscall_params.h" | 12 #include "sandbox/win/src/crosscall_params.h" |
| 13 #include "sandbox/win/src/crosscall_server.h" | 13 #include "sandbox/win/src/crosscall_server.h" |
| 14 | 14 |
| 15 namespace { | |
| 16 // This handle must not be closed. | |
|
rvargas (doing something else)
2014/02/20 15:45:53
nit: add a line here or remove line 18
| |
| 17 volatile HANDLE g_alive_mutex = NULL; | |
| 18 | |
| 19 } | |
| 20 | |
| 15 namespace sandbox { | 21 namespace sandbox { |
| 16 | 22 |
| 17 SharedMemIPCServer::SharedMemIPCServer(HANDLE target_process, | 23 SharedMemIPCServer::SharedMemIPCServer(HANDLE target_process, |
| 18 DWORD target_process_id, | 24 DWORD target_process_id, |
| 19 HANDLE target_job, | 25 HANDLE target_job, |
| 20 ThreadProvider* thread_provider, | 26 ThreadProvider* thread_provider, |
| 21 Dispatcher* dispatcher) | 27 Dispatcher* dispatcher) |
| 22 : client_control_(NULL), | 28 : client_control_(NULL), |
| 23 thread_provider_(thread_provider), | 29 thread_provider_(thread_provider), |
| 24 target_process_(target_process), | 30 target_process_(target_process), |
| 25 target_process_id_(target_process_id), | 31 target_process_id_(target_process_id), |
| 26 target_job_object_(target_job), | 32 target_job_object_(target_job), |
| 27 call_dispatcher_(dispatcher) { | 33 call_dispatcher_(dispatcher) { |
| 34 // We create a initially owned mutex. If the server dies unexpectedly, | |
| 35 // the thread that owns it will fail to release the lock and windows will | |
| 36 // report to the target (when it tries to acquire it) that the wait was | |
| 37 // abandoned. Note: We purposely leak the local handle because we want it to | |
| 38 // be closed by Windows itself so it is properly marked as abandoned if the | |
| 39 // server dies. | |
| 40 if (!g_alive_mutex) { | |
| 41 HANDLE mutex = ::CreateMutexW(NULL, TRUE, NULL); | |
| 42 if (::InterlockedCompareExchangePointer(&g_alive_mutex, mutex, NULL)) { | |
| 43 // We lost the race to create the mutex. | |
| 44 ::CloseHandle(mutex); | |
| 45 } | |
| 46 } | |
| 28 } | 47 } |
| 29 | 48 |
| 30 SharedMemIPCServer::~SharedMemIPCServer() { | 49 SharedMemIPCServer::~SharedMemIPCServer() { |
| 31 // Free the wait handles associated with the thread pool. | 50 // Free the wait handles associated with the thread pool. |
| 32 if (!thread_provider_->UnRegisterWaits(this)) { | 51 if (!thread_provider_->UnRegisterWaits(this)) { |
| 33 // Better to leak than to crash. | 52 // Better to leak than to crash. |
| 34 return; | 53 return; |
| 35 } | 54 } |
| 36 // Free the IPC signal events. | 55 // Free the IPC signal events. |
| 37 ServerContexts::iterator it; | 56 ServerContexts::iterator it; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 service_context->dispatcher = call_dispatcher_; | 120 service_context->dispatcher = call_dispatcher_; |
| 102 service_context->target_info.process = target_process_; | 121 service_context->target_info.process = target_process_; |
| 103 service_context->target_info.process_id = target_process_id_; | 122 service_context->target_info.process_id = target_process_id_; |
| 104 service_context->target_info.job_object = target_job_object_; | 123 service_context->target_info.job_object = target_job_object_; |
| 105 // Advance to the next channel. | 124 // Advance to the next channel. |
| 106 base_start += channel_size; | 125 base_start += channel_size; |
| 107 // Register the ping event with the threadpool. | 126 // Register the ping event with the threadpool. |
| 108 thread_provider_->RegisterWait(this, service_context->ping_event, | 127 thread_provider_->RegisterWait(this, service_context->ping_event, |
| 109 ThreadPingEventReady, service_context); | 128 ThreadPingEventReady, service_context); |
| 110 } | 129 } |
| 111 | |
| 112 // We create a mutex that the server locks. If the server dies unexpectedly, | |
| 113 // the thread that owns it will fail to release the lock and windows will | |
| 114 // report to the target (when it tries to acquire it) that the wait was | |
| 115 // abandoned. Note: We purposely leak the local handle because we want it to | |
| 116 // be closed by Windows itself so it is properly marked as abandoned if the | |
| 117 // server dies. | |
| 118 if (!::DuplicateHandle(::GetCurrentProcess(), | 130 if (!::DuplicateHandle(::GetCurrentProcess(), |
| 119 ::CreateMutexW(NULL, TRUE, NULL), | 131 g_alive_mutex, |
|
rvargas (doing something else)
2014/02/20 15:45:53
nit: move to the previous line.
| |
| 120 target_process_, &client_control_->server_alive, | 132 target_process_, &client_control_->server_alive, |
| 121 SYNCHRONIZE | EVENT_MODIFY_STATE, FALSE, 0)) { | 133 SYNCHRONIZE | EVENT_MODIFY_STATE, FALSE, 0)) { |
| 122 return false; | 134 return false; |
| 123 } | 135 } |
| 124 // This last setting indicates to the client all is setup. | 136 // This last setting indicates to the client all is setup. |
| 125 client_control_->channels_count = channel_count; | 137 client_control_->channels_count = channel_count; |
| 126 return true; | 138 return true; |
| 127 } | 139 } |
| 128 | 140 |
| 129 // Releases memory allocated for IPC arguments, if needed. | 141 // Releases memory allocated for IPC arguments, if needed. |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 401 } | 413 } |
| 402 *server_pong = ::CreateEventW(NULL, FALSE, FALSE, NULL); | 414 *server_pong = ::CreateEventW(NULL, FALSE, FALSE, NULL); |
| 403 if (!::DuplicateHandle(::GetCurrentProcess(), *server_pong, target_process_, | 415 if (!::DuplicateHandle(::GetCurrentProcess(), *server_pong, target_process_, |
| 404 client_pong, kDesiredAccess, FALSE, 0)) { | 416 client_pong, kDesiredAccess, FALSE, 0)) { |
| 405 return false; | 417 return false; |
| 406 } | 418 } |
| 407 return true; | 419 return true; |
| 408 } | 420 } |
| 409 | 421 |
| 410 } // namespace sandbox | 422 } // namespace sandbox |
| OLD | NEW |