Mike Yu | 39cc689 | 2018-12-14 16:18:27 +0800 | [diff] [blame] | 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 | */ |
Ken Chen | 5471dca | 2019-04-15 15:25:35 +0800 | [diff] [blame] | 16 | #define LOG_TAG "resolv" |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 17 | |
Hungming Chen | fbfa1ce | 2019-03-26 17:46:46 +0800 | [diff] [blame] | 18 | #include "ResolverEventReporter.h" |
| 19 | |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 20 | #include <android-base/logging.h> |
| 21 | #include <android/binder_manager.h> |
Mike Yu | 39cc689 | 2018-12-14 16:18:27 +0800 | [diff] [blame] | 22 | |
Mike Yu | 39cc689 | 2018-12-14 16:18:27 +0800 | [diff] [blame] | 23 | using aidl::android::net::metrics::INetdEventListener; |
| 24 | |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 25 | ResolverEventReporter& ResolverEventReporter::getInstance() { |
Mike Yu | 39cc689 | 2018-12-14 16:18:27 +0800 | [diff] [blame] | 26 | // It should be initialized only once. |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 27 | static ResolverEventReporter instance; |
| 28 | |
Hungming Chen | fbfa1ce | 2019-03-26 17:46:46 +0800 | [diff] [blame] | 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. |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 32 | instance.addDefaultListener(); |
| 33 | |
| 34 | return instance; |
Mike Yu | 39cc689 | 2018-12-14 16:18:27 +0800 | [diff] [blame] | 35 | } |
| 36 | |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 37 | ResolverEventReporter::ListenerSet ResolverEventReporter::getListeners() const { |
| 38 | return getListenersImpl(); |
Mike Yu | 39cc689 | 2018-12-14 16:18:27 +0800 | [diff] [blame] | 39 | } |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 40 | |
| 41 | int ResolverEventReporter::addListener(const std::shared_ptr<INetdEventListener>& listener) { |
| 42 | return addListenerImpl(listener); |
| 43 | } |
| 44 | |
Hungming Chen | fbfa1ce | 2019-03-26 17:46:46 +0800 | [diff] [blame] | 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. |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 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 | |
Hungming Chen | fbfa1ce | 2019-03-26 17:46:46 +0800 | [diff] [blame] | 65 | void ResolverEventReporter::handleBinderDied(const void* who) { |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 66 | std::lock_guard lock(mMutex); |
| 67 | |
Hungming Chen | fbfa1ce | 2019-03-26 17:46:46 +0800 | [diff] [blame] | 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); |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | ResolverEventReporter::ListenerSet ResolverEventReporter::getListenersImpl() const { |
| 77 | std::lock_guard lock(mMutex); |
| 78 | return mListeners; |
| 79 | } |
| 80 | |
| 81 | int ResolverEventReporter::addListenerImpl(const std::shared_ptr<INetdEventListener>& listener) { |
| 82 | std::lock_guard lock(mMutex); |
| 83 | return addListenerImplLocked(listener); |
| 84 | } |
| 85 | |
| 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 | |
Hungming Chen | 01dcb41 | 2019-05-31 17:46:49 +0800 | [diff] [blame] | 93 | 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 Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 98 | } |
| 99 | |
Hungming Chen | fbfa1ce | 2019-03-26 17:46:46 +0800 | [diff] [blame] | 100 | static AIBinder_DeathRecipient* deathRecipient = nullptr; |
| 101 | if (deathRecipient == nullptr) { |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 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. |
Hungming Chen | fbfa1ce | 2019-03-26 17:46:46 +0800 | [diff] [blame] | 107 | deathRecipient = AIBinder_DeathRecipient_new([](void* cookie) { |
| 108 | ResolverEventReporter::getInstance().handleBinderDied(cookie); |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 109 | }); |
| 110 | } |
| 111 | |
Hungming Chen | fbfa1ce | 2019-03-26 17:46:46 +0800 | [diff] [blame] | 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 | |
Hungming Chen | a32c8c1 | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 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 | } |