| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/mach_broker_mac.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/mac/foundation_util.h" | |
| 12 #include "base/mach_ipc_mac.h" | |
| 13 #include "base/string_util.h" | |
| 14 #include "base/stringprintf.h" | |
| 15 #include "base/strings/sys_string_conversions.h" | |
| 16 #include "base/threading/platform_thread.h" | |
| 17 #include "content/browser/renderer_host/render_process_host_impl.h" | |
| 18 #include "content/public/browser/child_process_data.h" | |
| 19 #include "content/public/browser/browser_thread.h" | |
| 20 #include "content/public/browser/notification_service.h" | |
| 21 #include "content/public/browser/notification_types.h" | |
| 22 #include "content/public/common/content_switches.h" | |
| 23 | |
| 24 namespace content { | |
| 25 | |
| 26 namespace { | |
| 27 // Prints a string representation of a Mach error code. | |
| 28 std::string MachErrorCode(kern_return_t err) { | |
| 29 return base::StringPrintf("0x%x %s", err, mach_error_string(err)); | |
| 30 } | |
| 31 } // namespace | |
| 32 | |
| 33 class MachListenerThreadDelegate : public base::PlatformThread::Delegate { | |
| 34 public: | |
| 35 MachListenerThreadDelegate(MachBroker* broker) : broker_(broker) { | |
| 36 DCHECK(broker_); | |
| 37 std::string port_name = MachBroker::GetMachPortName(); | |
| 38 | |
| 39 // Create the receive port in the constructor, not in ThreadMain(). It is | |
| 40 // important to create and register the receive port before starting the | |
| 41 // thread so that child processes will always have someone who's listening. | |
| 42 receive_port_.reset(new base::ReceivePort(port_name.c_str())); | |
| 43 } | |
| 44 | |
| 45 // Implement |PlatformThread::Delegate|. | |
| 46 virtual void ThreadMain() OVERRIDE { | |
| 47 base::MachReceiveMessage message; | |
| 48 kern_return_t err; | |
| 49 while ((err = receive_port_->WaitForMessage(&message, | |
| 50 MACH_MSG_TIMEOUT_NONE)) == | |
| 51 KERN_SUCCESS) { | |
| 52 // 0 was the secret message id. Reject any messages that don't have it. | |
| 53 if (message.GetMessageID() != 0) { | |
| 54 LOG(ERROR) << "Received message with incorrect id: " | |
| 55 << message.GetMessageID(); | |
| 56 continue; | |
| 57 } | |
| 58 | |
| 59 const task_t child_task = message.GetTranslatedPort(0); | |
| 60 if (child_task == MACH_PORT_NULL) { | |
| 61 LOG(ERROR) << "parent GetTranslatedPort(0) failed."; | |
| 62 continue; | |
| 63 } | |
| 64 | |
| 65 // It is possible for the child process to die after the call to | |
| 66 // |pid_for_task()| but before the call to |FinalizePid()|. To prevent | |
| 67 // leaking MachBroker map entries in this case, lock around both these | |
| 68 // calls. If the child dies, the death notification will be processed | |
| 69 // after the call to FinalizePid(), ensuring proper cleanup. | |
| 70 base::AutoLock lock(broker_->GetLock()); | |
| 71 | |
| 72 int pid; | |
| 73 err = pid_for_task(child_task, &pid); | |
| 74 if (err == KERN_SUCCESS) { | |
| 75 broker_->FinalizePid(pid, | |
| 76 MachBroker::MachInfo().SetTask(child_task)); | |
| 77 } else { | |
| 78 LOG(ERROR) << "Error getting pid for task " << child_task | |
| 79 << ": " << MachErrorCode(err); | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 LOG(ERROR) << "Mach listener thread exiting; " | |
| 84 << "parent WaitForMessage() likely failed: " | |
| 85 << MachErrorCode(err); | |
| 86 } | |
| 87 | |
| 88 private: | |
| 89 // The Mach port to listen on. Created on thread startup. | |
| 90 scoped_ptr<base::ReceivePort> receive_port_; | |
| 91 | |
| 92 // The MachBroker to use when new child task rights are received. Can be | |
| 93 // NULL. | |
| 94 MachBroker* broker_; // weak | |
| 95 | |
| 96 DISALLOW_COPY_AND_ASSIGN(MachListenerThreadDelegate); | |
| 97 }; | |
| 98 | |
| 99 // Returns the global MachBroker. | |
| 100 MachBroker* MachBroker::GetInstance() { | |
| 101 return Singleton<MachBroker, LeakySingletonTraits<MachBroker> >::get(); | |
| 102 } | |
| 103 | |
| 104 void MachBroker::EnsureRunning() { | |
| 105 lock_.AssertAcquired(); | |
| 106 | |
| 107 if (!listener_thread_started_) { | |
| 108 listener_thread_started_ = true; | |
| 109 | |
| 110 BrowserThread::PostTask( | |
| 111 BrowserThread::UI, FROM_HERE, | |
| 112 base::Bind(&MachBroker::RegisterNotifications, base::Unretained(this))); | |
| 113 | |
| 114 // Intentional leak. This thread is never joined or reaped. | |
| 115 base::PlatformThread::CreateNonJoinable( | |
| 116 0, new MachListenerThreadDelegate(this)); | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 // Adds a placeholder to the map for the given pid with MACH_PORT_NULL. | |
| 121 void MachBroker::AddPlaceholderForPid(base::ProcessHandle pid) { | |
| 122 lock_.AssertAcquired(); | |
| 123 | |
| 124 MachInfo mach_info; | |
| 125 DCHECK_EQ(0u, mach_map_.count(pid)); | |
| 126 mach_map_[pid] = mach_info; | |
| 127 } | |
| 128 | |
| 129 // Updates the mapping for |pid| to include the given |mach_info|. | |
| 130 void MachBroker::FinalizePid(base::ProcessHandle pid, | |
| 131 const MachInfo& mach_info) { | |
| 132 lock_.AssertAcquired(); | |
| 133 | |
| 134 const int count = mach_map_.count(pid); | |
| 135 if (count == 0) { | |
| 136 // Do nothing for unknown pids. | |
| 137 LOG(ERROR) << "Unknown process " << pid << " is sending Mach IPC messages!"; | |
| 138 return; | |
| 139 } | |
| 140 | |
| 141 DCHECK_EQ(1, count); | |
| 142 DCHECK(mach_map_[pid].mach_task_ == MACH_PORT_NULL); | |
| 143 if (mach_map_[pid].mach_task_ == MACH_PORT_NULL) | |
| 144 mach_map_[pid] = mach_info; | |
| 145 } | |
| 146 | |
| 147 // Removes all mappings belonging to |pid| from the broker. | |
| 148 void MachBroker::InvalidatePid(base::ProcessHandle pid) { | |
| 149 base::AutoLock lock(lock_); | |
| 150 MachBroker::MachMap::iterator it = mach_map_.find(pid); | |
| 151 if (it == mach_map_.end()) | |
| 152 return; | |
| 153 | |
| 154 kern_return_t kr = mach_port_deallocate(mach_task_self(), | |
| 155 it->second.mach_task_); | |
| 156 LOG_IF(WARNING, kr != KERN_SUCCESS) | |
| 157 << "Failed to mach_port_deallocate mach task " << it->second.mach_task_ | |
| 158 << ", error " << MachErrorCode(kr); | |
| 159 mach_map_.erase(it); | |
| 160 } | |
| 161 | |
| 162 base::Lock& MachBroker::GetLock() { | |
| 163 return lock_; | |
| 164 } | |
| 165 | |
| 166 // Returns the mach task belonging to |pid|. | |
| 167 mach_port_t MachBroker::TaskForPid(base::ProcessHandle pid) const { | |
| 168 base::AutoLock lock(lock_); | |
| 169 MachBroker::MachMap::const_iterator it = mach_map_.find(pid); | |
| 170 if (it == mach_map_.end()) | |
| 171 return MACH_PORT_NULL; | |
| 172 return it->second.mach_task_; | |
| 173 } | |
| 174 | |
| 175 void MachBroker::BrowserChildProcessHostDisconnected( | |
| 176 const ChildProcessData& data) { | |
| 177 InvalidatePid(data.handle); | |
| 178 } | |
| 179 | |
| 180 void MachBroker::BrowserChildProcessCrashed(const ChildProcessData& data) { | |
| 181 InvalidatePid(data.handle); | |
| 182 } | |
| 183 | |
| 184 void MachBroker::Observe(int type, | |
| 185 const NotificationSource& source, | |
| 186 const NotificationDetails& details) { | |
| 187 // TODO(rohitrao): These notifications do not always carry the proper PIDs, | |
| 188 // especially when the renderer is already gone or has crashed. Find a better | |
| 189 // way to listen for child process deaths. http://crbug.com/55734 | |
| 190 base::ProcessHandle handle = 0; | |
| 191 switch (type) { | |
| 192 case NOTIFICATION_RENDERER_PROCESS_CLOSED: | |
| 193 handle = Details<RenderProcessHost::RendererClosedDetails>( | |
| 194 details)->handle; | |
| 195 break; | |
| 196 case NOTIFICATION_RENDERER_PROCESS_TERMINATED: | |
| 197 handle = Source<RenderProcessHost>(source)->GetHandle(); | |
| 198 break; | |
| 199 default: | |
| 200 NOTREACHED() << "Unexpected notification"; | |
| 201 break; | |
| 202 } | |
| 203 InvalidatePid(handle); | |
| 204 } | |
| 205 | |
| 206 // static | |
| 207 std::string MachBroker::GetMachPortName() { | |
| 208 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 209 const bool is_child = command_line->HasSwitch(switches::kProcessType); | |
| 210 | |
| 211 // In non-browser (child) processes, use the parent's pid. | |
| 212 const pid_t pid = is_child ? getppid() : getpid(); | |
| 213 return base::StringPrintf("%s.rohitfork.%d", base::mac::BaseBundleID(), pid); | |
| 214 } | |
| 215 | |
| 216 MachBroker::MachBroker() : listener_thread_started_(false) { | |
| 217 } | |
| 218 | |
| 219 MachBroker::~MachBroker() {} | |
| 220 | |
| 221 void MachBroker::RegisterNotifications() { | |
| 222 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 223 NotificationService::AllBrowserContextsAndSources()); | |
| 224 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED, | |
| 225 NotificationService::AllBrowserContextsAndSources()); | |
| 226 | |
| 227 // No corresponding StopObservingBrowserChildProcesses, | |
| 228 // we leak this singleton. | |
| 229 BrowserChildProcessObserver::Add(this); | |
| 230 } | |
| 231 | |
| 232 } // namespace content | |
| OLD | NEW |