blob: b4e9b5cb7ee6c7e43bea7c00ecda1260d2b032b8 [file] [log] [blame]
Mike Yu39cc6892018-12-14 16:18:27 +08001/*
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 */
Ken Chen5471dca2019-04-15 15:25:35 +080016#define LOG_TAG "resolv"
Hungming Chena32c8c12019-01-25 10:47:40 +080017
Hungming Chenfbfa1ce2019-03-26 17:46:46 +080018#include "ResolverEventReporter.h"
19
Hungming Chena32c8c12019-01-25 10:47:40 +080020#include <android-base/logging.h>
21#include <android/binder_manager.h>
Mike Yu39cc6892018-12-14 16:18:27 +080022
Mike Yu39cc6892018-12-14 16:18:27 +080023using aidl::android::net::metrics::INetdEventListener;
24
Hungming Chena32c8c12019-01-25 10:47:40 +080025ResolverEventReporter& ResolverEventReporter::getInstance() {
Mike Yu39cc6892018-12-14 16:18:27 +080026 // It should be initialized only once.
Hungming Chena32c8c12019-01-25 10:47:40 +080027 static ResolverEventReporter instance;
28
Hungming Chenfbfa1ce2019-03-26 17:46:46 +080029 // 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.
Hungming Chena32c8c12019-01-25 10:47:40 +080032 instance.addDefaultListener();
33
34 return instance;
Mike Yu39cc6892018-12-14 16:18:27 +080035}
36
Hungming Chena32c8c12019-01-25 10:47:40 +080037ResolverEventReporter::ListenerSet ResolverEventReporter::getListeners() const {
38 return getListenersImpl();
Mike Yu39cc6892018-12-14 16:18:27 +080039}
Hungming Chena32c8c12019-01-25 10:47:40 +080040
41int ResolverEventReporter::addListener(const std::shared_ptr<INetdEventListener>& listener) {
42 return addListenerImpl(listener);
43}
44
Hungming Chenfbfa1ce2019-03-26 17:46:46 +080045// 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.
Hungming Chena32c8c12019-01-25 10:47:40 +080049void 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
Hungming Chenfbfa1ce2019-03-26 17:46:46 +080065void ResolverEventReporter::handleBinderDied(const void* who) {
Hungming Chena32c8c12019-01-25 10:47:40 +080066 std::lock_guard lock(mMutex);
67
Hungming Chenfbfa1ce2019-03-26 17:46:46 +080068 // 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);
Hungming Chena32c8c12019-01-25 10:47:40 +080074}
75
76ResolverEventReporter::ListenerSet ResolverEventReporter::getListenersImpl() const {
77 std::lock_guard lock(mMutex);
78 return mListeners;
79}
80
81int ResolverEventReporter::addListenerImpl(const std::shared_ptr<INetdEventListener>& listener) {
82 std::lock_guard lock(mMutex);
83 return addListenerImplLocked(listener);
84}
85
86int 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
Hungming Chen01dcb412019-05-31 17:46:49 +080093 for (const auto& it : mListeners) {
94 if (it->asBinder().get() == listener->asBinder().get()) {
95 LOG(WARNING) << "The listener was already subscribed";
96 return -EEXIST;
97 }
Hungming Chena32c8c12019-01-25 10:47:40 +080098 }
99
Hungming Chenfbfa1ce2019-03-26 17:46:46 +0800100 static AIBinder_DeathRecipient* deathRecipient = nullptr;
101 if (deathRecipient == nullptr) {
Hungming Chena32c8c12019-01-25 10:47:40 +0800102 // 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.
Hungming Chenfbfa1ce2019-03-26 17:46:46 +0800107 deathRecipient = AIBinder_DeathRecipient_new([](void* cookie) {
108 ResolverEventReporter::getInstance().handleBinderDied(cookie);
Hungming Chena32c8c12019-01-25 10:47:40 +0800109 });
110 }
111
Hungming Chenfbfa1ce2019-03-26 17:46:46 +0800112 // 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
Hungming Chena32c8c12019-01-25 10:47:40 +0800122 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}