Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // This file implements the Windows service controlling Me2Me host processes | |
| 6 // running within user sessions. | |
| 7 | |
| 8 #include "remoting/host/win/unprivileged_process_delegate.h" | |
| 9 | |
| 10 #include "base/base_switches.h" | |
| 11 #include "base/command_line.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/single_thread_task_runner.h" | |
| 14 #include "base/utf_string_conversions.h" | |
| 15 #include "base/win/scoped_handle.h" | |
| 16 #include "remoting/host/win/launch_process_with_token.h" | |
| 17 | |
| 18 using base::win::ScopedHandle; | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 // The command line switch specifying the name of the daemon IPC endpoint. | |
| 23 const char kDaemonIpcSwitchName[] = "daemon-pipe"; | |
| 24 | |
| 25 // The command line parameters that should be copied from the service's command | |
| 26 // line to the host process. | |
| 27 const char* kCopiedSwitchNames[] = { | |
| 28 "host-config", switches::kV, switches::kVModule }; | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 namespace remoting { | |
| 33 | |
| 34 UnprivilegedProcessDelegate::UnprivilegedProcessDelegate( | |
| 35 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | |
| 36 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, | |
| 37 const FilePath& binary_path) | |
| 38 : main_task_runner_(main_task_runner), | |
| 39 io_task_runner_(io_task_runner), | |
| 40 binary_path_(binary_path) { | |
| 41 } | |
| 42 | |
| 43 UnprivilegedProcessDelegate::~UnprivilegedProcessDelegate() { | |
| 44 KillProcess(CONTROL_C_EXIT); | |
| 45 } | |
| 46 | |
| 47 DWORD UnprivilegedProcessDelegate::GetExitCode() { | |
| 48 DCHECK(main_task_runner_->BelongsToCurrentThread()); | |
| 49 | |
| 50 DWORD exit_code = CONTROL_C_EXIT; | |
| 51 if (worker_process_.IsValid()) { | |
| 52 if (!::GetExitCodeProcess(worker_process_, &exit_code)) { | |
| 53 LOG_GETLASTERROR(INFO) | |
| 54 << "Failed to query the exit code of the worker process"; | |
| 55 exit_code = CONTROL_C_EXIT; | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 return exit_code; | |
| 60 } | |
| 61 | |
| 62 void UnprivilegedProcessDelegate::KillProcess(DWORD exit_code) { | |
| 63 DCHECK(main_task_runner_->BelongsToCurrentThread()); | |
| 64 | |
| 65 if (worker_process_.IsValid()) { | |
| 66 TerminateProcess(worker_process_, exit_code); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 bool UnprivilegedProcessDelegate::LaunchProcess( | |
| 71 const std::string& channel_name, | |
| 72 ScopedHandle* process_exit_event_out) { | |
| 73 DCHECK(main_task_runner_->BelongsToCurrentThread()); | |
| 74 | |
| 75 // Create the command line passing the name of the IPC channel to use and | |
| 76 // copying known switches from the caller's command line. | |
| 77 CommandLine command_line(binary_path_); | |
| 78 command_line.AppendSwitchNative(kDaemonIpcSwitchName, | |
| 79 UTF8ToWide(channel_name)); | |
| 80 command_line.CopySwitchesFrom(*CommandLine::ForCurrentProcess(), | |
| 81 kCopiedSwitchNames, | |
| 82 arraysize(kCopiedSwitchNames)); | |
| 83 | |
| 84 // Try to launch the process and attach an object watcher to the returned | |
| 85 // handle so that we get notified when the process terminates. | |
|
Wez
2012/10/09 03:40:39
nit: You're actually duplicating its process handl
alexeypa (please no reviews)
2012/10/09 19:42:04
Done.
| |
| 86 // TODO(alexeypa): Pass a restricted process token. | |
| 87 // See http://crbug.com/134694. | |
| 88 ScopedHandle worker_process; | |
| 89 ScopedHandle worker_thread; | |
| 90 if (!LaunchProcessWithToken(command_line.GetProgram(), | |
| 91 command_line.GetCommandLineString(), | |
| 92 NULL, | |
| 93 0, | |
| 94 &worker_process, | |
| 95 &worker_thread)) { | |
| 96 return false; | |
| 97 } | |
| 98 | |
| 99 ScopedHandle process_exit_event; | |
| 100 if (!DuplicateHandle(GetCurrentProcess(), | |
| 101 worker_process_, | |
| 102 GetCurrentProcess(), | |
| 103 process_exit_event.Receive(), | |
| 104 SYNCHRONIZE, | |
| 105 FALSE, | |
| 106 0)) { | |
| 107 LOG_GETLASTERROR(ERROR) << "Failed to duplicate a handle"; | |
| 108 KillProcess(CONTROL_C_EXIT); | |
| 109 return false; | |
| 110 } | |
| 111 | |
| 112 *process_exit_event_out = process_exit_event.Pass(); | |
| 113 return true; | |
| 114 } | |
| 115 | |
| 116 } // namespace remoting | |
| OLD | NEW |