| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 #include "mojo/shell/runner/host/child_process_host.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/bind.h" | |
| 12 #include "base/command_line.h" | |
| 13 #include "base/location.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/message_loop/message_loop.h" | |
| 17 #include "base/process/kill.h" | |
| 18 #include "base/process/launch.h" | |
| 19 #include "base/synchronization/lock.h" | |
| 20 #include "base/task_runner.h" | |
| 21 #include "base/thread_task_runner_handle.h" | |
| 22 #include "mojo/edk/embedder/embedder.h" | |
| 23 #include "mojo/public/cpp/bindings/interface_ptr_info.h" | |
| 24 #include "mojo/public/cpp/system/core.h" | |
| 25 #include "mojo/shell/native_runner_delegate.h" | |
| 26 #include "mojo/shell/runner/common/client_util.h" | |
| 27 #include "mojo/shell/runner/common/switches.h" | |
| 28 | |
| 29 #if defined(OS_LINUX) && !defined(OS_ANDROID) | |
| 30 #include "sandbox/linux/services/namespace_sandbox.h" | |
| 31 #endif | |
| 32 | |
| 33 #if defined(OS_WIN) | |
| 34 #include "base/win/windows_version.h" | |
| 35 #endif | |
| 36 | |
| 37 #if defined(OS_MACOSX) | |
| 38 #include "mojo/shell/runner/host/mach_broker.h" | |
| 39 #endif | |
| 40 | |
| 41 namespace mojo { | |
| 42 namespace shell { | |
| 43 | |
| 44 ChildProcessHost::ChildProcessHost(base::TaskRunner* launch_process_runner, | |
| 45 NativeRunnerDelegate* delegate, | |
| 46 bool start_sandboxed, | |
| 47 const Identity& target, | |
| 48 const base::FilePath& app_path) | |
| 49 : launch_process_runner_(launch_process_runner), | |
| 50 delegate_(delegate), | |
| 51 start_sandboxed_(start_sandboxed), | |
| 52 target_(target), | |
| 53 app_path_(app_path), | |
| 54 start_child_process_event_(false, false), | |
| 55 weak_factory_(this) { | |
| 56 } | |
| 57 | |
| 58 ChildProcessHost::~ChildProcessHost() { | |
| 59 if (!app_path_.empty()) { | |
| 60 CHECK(!mojo_ipc_channel_) | |
| 61 << "Destroying ChildProcessHost before calling Join"; | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 mojom::ShellClientPtr ChildProcessHost::Start( | |
| 66 const Identity& target, | |
| 67 const ProcessReadyCallback& callback, | |
| 68 const base::Closure& quit_closure) { | |
| 69 DCHECK(!child_process_.IsValid()); | |
| 70 | |
| 71 const base::CommandLine* parent_command_line = | |
| 72 base::CommandLine::ForCurrentProcess(); | |
| 73 base::FilePath target_path = parent_command_line->GetProgram(); | |
| 74 // |app_path_| can be empty in tests. | |
| 75 if (!app_path_.MatchesExtension(FILE_PATH_LITERAL(".mojo")) && | |
| 76 !app_path_.empty()) { | |
| 77 target_path = app_path_; | |
| 78 } | |
| 79 | |
| 80 scoped_ptr<base::CommandLine> child_command_line( | |
| 81 new base::CommandLine(target_path)); | |
| 82 | |
| 83 child_command_line->AppendArguments(*parent_command_line, false); | |
| 84 | |
| 85 #ifndef NDEBUG | |
| 86 child_command_line->AppendSwitchASCII("n", target.name()); | |
| 87 child_command_line->AppendSwitchASCII("u", target.user_id()); | |
| 88 #endif | |
| 89 | |
| 90 if (target_path != app_path_) | |
| 91 child_command_line->AppendSwitchPath(switches::kChildProcess, app_path_); | |
| 92 | |
| 93 if (start_sandboxed_) | |
| 94 child_command_line->AppendSwitch(switches::kEnableSandbox); | |
| 95 | |
| 96 mojo_ipc_channel_.reset(new edk::PlatformChannelPair); | |
| 97 mojo_ipc_channel_->PrepareToPassClientHandleToChildProcess( | |
| 98 child_command_line.get(), &handle_passing_info_); | |
| 99 | |
| 100 mojom::ShellClientPtr client = | |
| 101 PassShellClientRequestOnCommandLine(child_command_line.get()); | |
| 102 launch_process_runner_->PostTaskAndReply( | |
| 103 FROM_HERE, | |
| 104 base::Bind(&ChildProcessHost::DoLaunch, base::Unretained(this), | |
| 105 base::Passed(&child_command_line)), | |
| 106 base::Bind(&ChildProcessHost::DidStart, | |
| 107 weak_factory_.GetWeakPtr(), callback)); | |
| 108 return client; | |
| 109 } | |
| 110 | |
| 111 void ChildProcessHost::Join() { | |
| 112 if (mojo_ipc_channel_) | |
| 113 start_child_process_event_.Wait(); | |
| 114 mojo_ipc_channel_.reset(); | |
| 115 if (child_process_.IsValid()) { | |
| 116 int rv = -1; | |
| 117 LOG_IF(ERROR, !child_process_.WaitForExit(&rv)) | |
| 118 << "Failed to wait for child process"; | |
| 119 child_process_.Close(); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 void ChildProcessHost::DidStart(const ProcessReadyCallback& callback) { | |
| 124 if (child_process_.IsValid()) { | |
| 125 callback.Run(child_process_.Pid()); | |
| 126 } else { | |
| 127 LOG(ERROR) << "Failed to start child process"; | |
| 128 mojo_ipc_channel_.reset(); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 void ChildProcessHost::DoLaunch( | |
| 133 scoped_ptr<base::CommandLine> child_command_line) { | |
| 134 if (delegate_) { | |
| 135 delegate_->AdjustCommandLineArgumentsForTarget(target_, | |
| 136 child_command_line.get()); | |
| 137 } | |
| 138 | |
| 139 base::LaunchOptions options; | |
| 140 #if defined(OS_WIN) | |
| 141 options.handles_to_inherit = &handle_passing_info_; | |
| 142 #if defined(OFFICIAL_BUILD) | |
| 143 CHECK(false) << "Launching mojo process with inherit_handles is insecure!"; | |
| 144 #endif | |
| 145 options.inherit_handles = true; | |
| 146 options.stdin_handle = INVALID_HANDLE_VALUE; | |
| 147 options.stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE); | |
| 148 options.stderr_handle = GetStdHandle(STD_ERROR_HANDLE); | |
| 149 // Always inherit stdout/stderr as a pair. | |
| 150 if (!options.stdout_handle || !options.stdin_handle) | |
| 151 options.stdin_handle = options.stdout_handle = nullptr; | |
| 152 | |
| 153 // Pseudo handles are used when stdout and stderr redirect to the console. In | |
| 154 // that case, they're automatically inherited by child processes. See | |
| 155 // https://msdn.microsoft.com/en-us/library/windows/desktop/ms682075.aspx | |
| 156 // Trying to add them to the list of handles to inherit causes CreateProcess | |
| 157 // to fail. When this process is launched from Python | |
| 158 // (i.e. by apptest_runner.py) then a real handle is used. In that case, we do | |
| 159 // want to add it to the list of handles that is inherited. | |
| 160 if (options.stdout_handle && | |
| 161 GetFileType(options.stdout_handle) != FILE_TYPE_CHAR) { | |
| 162 handle_passing_info_.push_back(options.stdout_handle); | |
| 163 } | |
| 164 if (options.stderr_handle && | |
| 165 GetFileType(options.stderr_handle) != FILE_TYPE_CHAR && | |
| 166 options.stdout_handle != options.stderr_handle) { | |
| 167 handle_passing_info_.push_back(options.stderr_handle); | |
| 168 } | |
| 169 #elif defined(OS_POSIX) | |
| 170 handle_passing_info_.push_back(std::make_pair(STDIN_FILENO, STDIN_FILENO)); | |
| 171 handle_passing_info_.push_back(std::make_pair(STDOUT_FILENO, STDOUT_FILENO)); | |
| 172 handle_passing_info_.push_back(std::make_pair(STDERR_FILENO, STDERR_FILENO)); | |
| 173 options.fds_to_remap = &handle_passing_info_; | |
| 174 #endif | |
| 175 DVLOG(2) << "Launching child with command line: " | |
| 176 << child_command_line->GetCommandLineString(); | |
| 177 #if defined(OS_LINUX) && !defined(OS_ANDROID) | |
| 178 if (start_sandboxed_) { | |
| 179 child_process_ = | |
| 180 sandbox::NamespaceSandbox::LaunchProcess(*child_command_line, options); | |
| 181 if (!child_process_.IsValid()) { | |
| 182 LOG(ERROR) << "Starting the process with a sandbox failed. Missing kernel" | |
| 183 << " support."; | |
| 184 } | |
| 185 } else | |
| 186 #endif | |
| 187 { | |
| 188 #if defined(OS_MACOSX) | |
| 189 MachBroker* mach_broker = MachBroker::GetInstance(); | |
| 190 base::AutoLock locker(mach_broker->GetLock()); | |
| 191 #endif | |
| 192 LOG(WARNING) << "PATH: " << app_path_.value(); | |
| 193 child_process_ = base::LaunchProcess(*child_command_line, options); | |
| 194 #if defined(OS_MACOSX) | |
| 195 mach_broker->ExpectPid(child_process_.Handle()); | |
| 196 #endif | |
| 197 } | |
| 198 | |
| 199 if (child_process_.IsValid()) { | |
| 200 if (mojo_ipc_channel_.get()) { | |
| 201 mojo_ipc_channel_->ChildProcessLaunched(); | |
| 202 mojo::edk::ChildProcessLaunched( | |
| 203 child_process_.Handle(), | |
| 204 mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle( | |
| 205 mojo_ipc_channel_->PassServerHandle().release().handle))); | |
| 206 } | |
| 207 } | |
| 208 start_child_process_event_.Signal(); | |
| 209 } | |
| 210 | |
| 211 } // namespace shell | |
| 212 } // namespace mojo | |
| OLD | NEW |