| 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 // This file implements the Windows service controlling Me2Me host processes | 5 // This file implements the Windows service controlling Me2Me host processes |
| 6 // running within user sessions. | 6 // running within user sessions. |
| 7 | 7 |
| 8 #include "remoting/host/win/host_service.h" | 8 #include "remoting/host/win/host_service.h" |
| 9 | 9 |
| 10 #include <sddl.h> | 10 #include <sddl.h> |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 base::Bind(&HostService::StopDaemonProcess, weak_ptr_)); | 217 base::Bind(&HostService::StopDaemonProcess, weak_ptr_)); |
| 218 } | 218 } |
| 219 | 219 |
| 220 int HostService::RunAsService() { | 220 int HostService::RunAsService() { |
| 221 SERVICE_TABLE_ENTRYW dispatch_table[] = { | 221 SERVICE_TABLE_ENTRYW dispatch_table[] = { |
| 222 { const_cast<LPWSTR>(kWindowsServiceName), &HostService::ServiceMain }, | 222 { const_cast<LPWSTR>(kWindowsServiceName), &HostService::ServiceMain }, |
| 223 { NULL, NULL } | 223 { NULL, NULL } |
| 224 }; | 224 }; |
| 225 | 225 |
| 226 if (!StartServiceCtrlDispatcherW(dispatch_table)) { | 226 if (!StartServiceCtrlDispatcherW(dispatch_table)) { |
| 227 LOG_GETLASTERROR(ERROR) | 227 PLOG(ERROR) << "Failed to connect to the service control manager"; |
| 228 << "Failed to connect to the service control manager"; | |
| 229 return kInitializationFailed; | 228 return kInitializationFailed; |
| 230 } | 229 } |
| 231 | 230 |
| 232 // Wait until the service thread completely exited to avoid concurrent | 231 // Wait until the service thread completely exited to avoid concurrent |
| 233 // teardown of objects registered with base::AtExitManager and object | 232 // teardown of objects registered with base::AtExitManager and object |
| 234 // destoyed by the service thread. | 233 // destoyed by the service thread. |
| 235 stopped_event_.Wait(); | 234 stopped_event_.Wait(); |
| 236 | 235 |
| 237 return kSuccessExitCode; | 236 return kSuccessExitCode; |
| 238 } | 237 } |
| 239 | 238 |
| 240 void HostService::RunAsServiceImpl() { | 239 void HostService::RunAsServiceImpl() { |
| 241 base::MessageLoopForUI message_loop; | 240 base::MessageLoopForUI message_loop; |
| 242 base::RunLoop run_loop; | 241 base::RunLoop run_loop; |
| 243 main_task_runner_ = message_loop.message_loop_proxy(); | 242 main_task_runner_ = message_loop.message_loop_proxy(); |
| 244 weak_ptr_ = weak_factory_.GetWeakPtr(); | 243 weak_ptr_ = weak_factory_.GetWeakPtr(); |
| 245 | 244 |
| 246 // Register the service control handler. | 245 // Register the service control handler. |
| 247 service_status_handle_ = RegisterServiceCtrlHandlerExW( | 246 service_status_handle_ = RegisterServiceCtrlHandlerExW( |
| 248 kWindowsServiceName, &HostService::ServiceControlHandler, this); | 247 kWindowsServiceName, &HostService::ServiceControlHandler, this); |
| 249 if (service_status_handle_ == 0) { | 248 if (service_status_handle_ == 0) { |
| 250 LOG_GETLASTERROR(ERROR) | 249 PLOG(ERROR) << "Failed to register the service control handler"; |
| 251 << "Failed to register the service control handler"; | |
| 252 return; | 250 return; |
| 253 } | 251 } |
| 254 | 252 |
| 255 // Report running status of the service. | 253 // Report running status of the service. |
| 256 SERVICE_STATUS service_status; | 254 SERVICE_STATUS service_status; |
| 257 ZeroMemory(&service_status, sizeof(service_status)); | 255 ZeroMemory(&service_status, sizeof(service_status)); |
| 258 service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; | 256 service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; |
| 259 service_status.dwCurrentState = SERVICE_RUNNING; | 257 service_status.dwCurrentState = SERVICE_RUNNING; |
| 260 service_status.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | | 258 service_status.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | |
| 261 SERVICE_ACCEPT_STOP | | 259 SERVICE_ACCEPT_STOP | |
| 262 SERVICE_ACCEPT_SESSIONCHANGE; | 260 SERVICE_ACCEPT_SESSIONCHANGE; |
| 263 service_status.dwWin32ExitCode = kSuccessExitCode; | 261 service_status.dwWin32ExitCode = kSuccessExitCode; |
| 264 if (!SetServiceStatus(service_status_handle_, &service_status)) { | 262 if (!SetServiceStatus(service_status_handle_, &service_status)) { |
| 265 LOG_GETLASTERROR(ERROR) | 263 PLOG(ERROR) |
| 266 << "Failed to report service status to the service control manager"; | 264 << "Failed to report service status to the service control manager"; |
| 267 return; | 265 return; |
| 268 } | 266 } |
| 269 | 267 |
| 270 // Initialize COM. | 268 // Initialize COM. |
| 271 base::win::ScopedCOMInitializer com_initializer; | 269 base::win::ScopedCOMInitializer com_initializer; |
| 272 if (!com_initializer.succeeded()) | 270 if (!com_initializer.succeeded()) |
| 273 return; | 271 return; |
| 274 | 272 |
| 275 if (!InitializeComSecurity(base::WideToUTF8(kComProcessSd), | 273 if (!InitializeComSecurity(base::WideToUTF8(kComProcessSd), |
| 276 base::WideToUTF8(kComProcessMandatoryLabel), | 274 base::WideToUTF8(kComProcessMandatoryLabel), |
| 277 false)) { | 275 false)) { |
| 278 return; | 276 return; |
| 279 } | 277 } |
| 280 | 278 |
| 281 CreateLauncher(scoped_refptr<AutoThreadTaskRunner>( | 279 CreateLauncher(scoped_refptr<AutoThreadTaskRunner>( |
| 282 new AutoThreadTaskRunner(main_task_runner_, | 280 new AutoThreadTaskRunner(main_task_runner_, |
| 283 run_loop.QuitClosure()))); | 281 run_loop.QuitClosure()))); |
| 284 | 282 |
| 285 // Run the service. | 283 // Run the service. |
| 286 run_loop.Run(); | 284 run_loop.Run(); |
| 287 weak_factory_.InvalidateWeakPtrs(); | 285 weak_factory_.InvalidateWeakPtrs(); |
| 288 | 286 |
| 289 // Tell SCM that the service is stopped. | 287 // Tell SCM that the service is stopped. |
| 290 service_status.dwCurrentState = SERVICE_STOPPED; | 288 service_status.dwCurrentState = SERVICE_STOPPED; |
| 291 service_status.dwControlsAccepted = 0; | 289 service_status.dwControlsAccepted = 0; |
| 292 if (!SetServiceStatus(service_status_handle_, &service_status)) { | 290 if (!SetServiceStatus(service_status_handle_, &service_status)) { |
| 293 LOG_GETLASTERROR(ERROR) | 291 PLOG(ERROR) |
| 294 << "Failed to report service status to the service control manager"; | 292 << "Failed to report service status to the service control manager"; |
| 295 return; | 293 return; |
| 296 } | 294 } |
| 297 } | 295 } |
| 298 | 296 |
| 299 int HostService::RunInConsole() { | 297 int HostService::RunInConsole() { |
| 300 base::MessageLoopForUI message_loop; | 298 base::MessageLoopForUI message_loop; |
| 301 base::RunLoop run_loop; | 299 base::RunLoop run_loop; |
| 302 main_task_runner_ = message_loop.message_loop_proxy(); | 300 main_task_runner_ = message_loop.message_loop_proxy(); |
| 303 weak_ptr_ = weak_factory_.GetWeakPtr(); | 301 weak_ptr_ = weak_factory_.GetWeakPtr(); |
| 304 | 302 |
| 305 int result = kInitializationFailed; | 303 int result = kInitializationFailed; |
| 306 | 304 |
| 307 // Initialize COM. | 305 // Initialize COM. |
| 308 base::win::ScopedCOMInitializer com_initializer; | 306 base::win::ScopedCOMInitializer com_initializer; |
| 309 if (!com_initializer.succeeded()) | 307 if (!com_initializer.succeeded()) |
| 310 return result; | 308 return result; |
| 311 | 309 |
| 312 if (!InitializeComSecurity(base::WideToUTF8(kComProcessSd), | 310 if (!InitializeComSecurity(base::WideToUTF8(kComProcessSd), |
| 313 base::WideToUTF8(kComProcessMandatoryLabel), | 311 base::WideToUTF8(kComProcessMandatoryLabel), |
| 314 false)) { | 312 false)) { |
| 315 return result; | 313 return result; |
| 316 } | 314 } |
| 317 | 315 |
| 318 // Subscribe to Ctrl-C and other console events. | 316 // Subscribe to Ctrl-C and other console events. |
| 319 if (!SetConsoleCtrlHandler(&HostService::ConsoleControlHandler, TRUE)) { | 317 if (!SetConsoleCtrlHandler(&HostService::ConsoleControlHandler, TRUE)) { |
| 320 LOG_GETLASTERROR(ERROR) | 318 PLOG(ERROR) << "Failed to set console control handler"; |
| 321 << "Failed to set console control handler"; | |
| 322 return result; | 319 return result; |
| 323 } | 320 } |
| 324 | 321 |
| 325 // Create a window for receiving session change notifications. | 322 // Create a window for receiving session change notifications. |
| 326 base::win::MessageWindow window; | 323 base::win::MessageWindow window; |
| 327 if (!window.Create(base::Bind(&HostService::HandleMessage, | 324 if (!window.Create(base::Bind(&HostService::HandleMessage, |
| 328 base::Unretained(this)))) { | 325 base::Unretained(this)))) { |
| 329 LOG_GETLASTERROR(ERROR) | 326 PLOG(ERROR) << "Failed to create the session notification window"; |
| 330 << "Failed to create the session notification window"; | |
| 331 goto cleanup; | 327 goto cleanup; |
| 332 } | 328 } |
| 333 | 329 |
| 334 // Subscribe to session change notifications. | 330 // Subscribe to session change notifications. |
| 335 if (WTSRegisterSessionNotification(window.hwnd(), | 331 if (WTSRegisterSessionNotification(window.hwnd(), |
| 336 NOTIFY_FOR_ALL_SESSIONS) != FALSE) { | 332 NOTIFY_FOR_ALL_SESSIONS) != FALSE) { |
| 337 CreateLauncher(scoped_refptr<AutoThreadTaskRunner>( | 333 CreateLauncher(scoped_refptr<AutoThreadTaskRunner>( |
| 338 new AutoThreadTaskRunner(main_task_runner_, | 334 new AutoThreadTaskRunner(main_task_runner_, |
| 339 run_loop.QuitClosure()))); | 335 run_loop.QuitClosure()))); |
| 340 | 336 |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 int DaemonProcessMain() { | 434 int DaemonProcessMain() { |
| 439 HostService* service = HostService::GetInstance(); | 435 HostService* service = HostService::GetInstance(); |
| 440 if (!service->InitWithCommandLine(CommandLine::ForCurrentProcess())) { | 436 if (!service->InitWithCommandLine(CommandLine::ForCurrentProcess())) { |
| 441 return kUsageExitCode; | 437 return kUsageExitCode; |
| 442 } | 438 } |
| 443 | 439 |
| 444 return service->Run(); | 440 return service->Run(); |
| 445 } | 441 } |
| 446 | 442 |
| 447 } // namespace remoting | 443 } // namespace remoting |
| OLD | NEW |