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