• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #define LOG_TAG "ResolverEventReporter"
17 
18 #include "ResolverEventReporter.h"
19 
20 #include <android-base/logging.h>
21 #include <android/binder_manager.h>
22 
23 using aidl::android::net::metrics::INetdEventListener;
24 
getInstance()25 ResolverEventReporter& ResolverEventReporter::getInstance() {
26     // It should be initialized only once.
27     static ResolverEventReporter instance;
28 
29     // Add framework metrics listener. Because the binder service "netd_listener" may be launched
30     // later than Netd, try to get binder handler in every instance query if any. The framework
31     // metrics listener should be added only once if it has been already added successfully.
32     instance.addDefaultListener();
33 
34     return instance;
35 }
36 
getListeners() const37 ResolverEventReporter::ListenerSet ResolverEventReporter::getListeners() const {
38     return getListenersImpl();
39 }
40 
addListener(const std::shared_ptr<INetdEventListener> & listener)41 int ResolverEventReporter::addListener(const std::shared_ptr<INetdEventListener>& listener) {
42     return addListenerImpl(listener);
43 }
44 
45 // TODO: Consider registering metrics listener from framework and remove this function.
46 // Currently, the framework listener "netd_listener" is shared by netd and libnetd_resolv.
47 // Consider breaking it into two listeners. Once it has done, may let framework register
48 // the listener proactively.
addDefaultListener()49 void ResolverEventReporter::addDefaultListener() {
50     std::lock_guard lock(mMutex);
51 
52     static bool added = false;
53     if (added) return;
54 
55     // Use the non-blocking call AServiceManager_checkService in order not to delay DNS
56     // lookup threads when the netd_listener service is not ready.
57     ndk::SpAIBinder binder = ndk::SpAIBinder(AServiceManager_checkService("netd_listener"));
58     std::shared_ptr<INetdEventListener> listener = INetdEventListener::fromBinder(binder);
59 
60     if (listener == nullptr) return;
61 
62     if (!addListenerImplLocked(listener)) added = true;
63 }
64 
handleBinderDied(const void * who)65 void ResolverEventReporter::handleBinderDied(const void* who) {
66     std::lock_guard lock(mMutex);
67 
68     // Use the raw binder pointer address to be the identification of dead binder. Treat "who"
69     // which passes the raw address of dead binder as an identification only.
70     auto found = std::find_if(mListeners.begin(), mListeners.end(),
71                               [=](const auto& it) { return static_cast<void*>(it.get()) == who; });
72 
73     if (found != mListeners.end()) mListeners.erase(found);
74 }
75 
getListenersImpl() const76 ResolverEventReporter::ListenerSet ResolverEventReporter::getListenersImpl() const {
77     std::lock_guard lock(mMutex);
78     return mListeners;
79 }
80 
addListenerImpl(const std::shared_ptr<INetdEventListener> & listener)81 int ResolverEventReporter::addListenerImpl(const std::shared_ptr<INetdEventListener>& listener) {
82     std::lock_guard lock(mMutex);
83     return addListenerImplLocked(listener);
84 }
85 
addListenerImplLocked(const std::shared_ptr<INetdEventListener> & listener)86 int ResolverEventReporter::addListenerImplLocked(
87         const std::shared_ptr<INetdEventListener>& listener) {
88     if (listener == nullptr) {
89         LOG(ERROR) << "The listener should not be null";
90         return -EINVAL;
91     }
92 
93     // TODO: Perhaps ignore the listener which comes from the same binder.
94     const auto& it = mListeners.find(listener);
95     if (it != mListeners.end()) {
96         LOG(WARNING) << "The listener was already subscribed";
97         return -EEXIST;
98     }
99 
100     static AIBinder_DeathRecipient* deathRecipient = nullptr;
101     if (deathRecipient == nullptr) {
102         // The AIBinder_DeathRecipient object is used to manage all death recipients for multiple
103         // binder objects. It doesn't released because there should have at least one binder object
104         // from framework.
105         // TODO: Considering to remove death recipient for the binder object from framework because
106         // it doesn't need death recipient actually.
107         deathRecipient = AIBinder_DeathRecipient_new([](void* cookie) {
108             ResolverEventReporter::getInstance().handleBinderDied(cookie);
109         });
110     }
111 
112     // Pass the raw binder pointer address to be the cookie of the death recipient. While the death
113     // notification is fired, the cookie is used for identifying which binder was died. Because
114     // the NDK binder doesn't pass dead binder pointer to binder death handler, the binder death
115     // handler can't know who was died via wp<IBinder>. The reason for wp<IBinder> is not passed
116     // is that NDK binder can't transform a wp<IBinder> to a wp<AIBinder> in some cases.
117     // See more information in b/128712772.
118     auto binder = listener->asBinder().get();
119     auto cookie = static_cast<void*>(listener.get());  // Used for dead binder identification.
120     binder_status_t status = AIBinder_linkToDeath(binder, deathRecipient, cookie);
121 
122     if (STATUS_OK != status) {
123         LOG(ERROR) << "Failed to register death notification for INetdEventListener";
124         return -EAGAIN;
125     }
126 
127     mListeners.insert(listener);
128     return 0;
129 }