1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "EventReporter.h" 18 19 using android::interface_cast; 20 using android::net::INetdUnsolicitedEventListener; 21 using android::net::metrics::INetdEventListener; 22 getNetdEventListener()23 android::sp<INetdEventListener> EventReporter::getNetdEventListener() { 24 std::lock_guard lock(mEventMutex); 25 if (mNetdEventListener == nullptr) { 26 // Use checkService instead of getService because getService waits for 5 seconds for the 27 // service to become available. The DNS resolver inside netd is started much earlier in the 28 // boot sequence than the framework DNS listener, and we don't want to delay all DNS lookups 29 // for 5 seconds until the DNS listener starts up. 30 android::sp<android::IBinder> b = android::defaultServiceManager()->checkService( 31 android::String16("netd_listener")); 32 mNetdEventListener = interface_cast<INetdEventListener>(b); 33 } 34 // If the netd listener service is dead, the binder call will just return an error, which should 35 // be fine because the only impact is that we can't log netd events. In any case, this should 36 // only happen if the system server is going down, which means it will shortly be taking us down 37 // with it. 38 return mNetdEventListener; 39 } 40 getNetdUnsolicitedEventListenerMap() const41 EventReporter::UnsolListenerMap EventReporter::getNetdUnsolicitedEventListenerMap() const { 42 std::lock_guard lock(mUnsolicitedMutex); 43 return mUnsolListenerMap; 44 } 45 registerUnsolEventListener(const android::sp<INetdUnsolicitedEventListener> & listener)46 void EventReporter::registerUnsolEventListener( 47 const android::sp<INetdUnsolicitedEventListener>& listener) { 48 std::lock_guard lock(mUnsolicitedMutex); 49 50 // Create the death listener. 51 class DeathRecipient : public android::IBinder::DeathRecipient { 52 public: 53 DeathRecipient(EventReporter* eventReporter, 54 android::sp<INetdUnsolicitedEventListener> listener) 55 : mEventReporter(eventReporter), mListener(std::move(listener)) {} 56 ~DeathRecipient() override = default; 57 void binderDied(const android::wp<android::IBinder>& /* who */) override { 58 mEventReporter->unregisterUnsolEventListener(mListener); 59 } 60 61 private: 62 EventReporter* mEventReporter; 63 android::sp<INetdUnsolicitedEventListener> mListener; 64 }; 65 android::sp<android::IBinder::DeathRecipient> deathRecipient = 66 new DeathRecipient(this, listener); 67 68 android::IInterface::asBinder(listener)->linkToDeath(deathRecipient); 69 70 // TODO: Consider to use remote binder address as registering key 71 mUnsolListenerMap.insert({listener, deathRecipient}); 72 } 73 unregisterUnsolEventListener(const android::sp<INetdUnsolicitedEventListener> & listener)74 void EventReporter::unregisterUnsolEventListener( 75 const android::sp<INetdUnsolicitedEventListener>& listener) { 76 std::lock_guard lock(mUnsolicitedMutex); 77 mUnsolListenerMap.erase(listener); 78 } 79