Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 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 | |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 17 | #include "EventReporter.h" |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 18 | |
| 19 | using android::interface_cast; |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 20 | using android::net::INetdUnsolicitedEventListener; |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 21 | using android::net::metrics::INetdEventListener; |
| 22 | |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 23 | android::sp<INetdEventListener> EventReporter::getNetdEventListener() { |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 24 | std::lock_guard lock(mEventMutex); |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 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")); |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 32 | mNetdEventListener = interface_cast<INetdEventListener>(b); |
Michal Karpinski | be581e2 | 2016-10-06 16:56:04 +0100 | [diff] [blame] | 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 | } |
Luke Huang | 528af60 | 2018-08-29 19:06:05 +0800 | [diff] [blame] | 40 | |
| 41 | std::map<pid_t, const android::sp<INetdUnsolicitedEventListener>> |
| 42 | EventReporter::getNetdUnsolicitedEventListenerVec() { |
| 43 | std::lock_guard lock(mUnsolicitedMutex); |
| 44 | return mNetdUnsolicitedEventListenerMap; |
| 45 | } |
| 46 | |
| 47 | void EventReporter::registerUnsolEventListener( |
| 48 | pid_t pid, const android::sp<INetdUnsolicitedEventListener>& listener) { |
| 49 | std::lock_guard lock(mUnsolicitedMutex); |
| 50 | if (mNetdUnsolicitedEventListenerMap.find(pid) != mNetdUnsolicitedEventListenerMap.end()) { |
| 51 | return; |
| 52 | } |
| 53 | mNetdUnsolicitedEventListenerMap.insert({pid, listener}); |
| 54 | |
| 55 | // Create the death listener. |
| 56 | class DeathRecipient : public android::IBinder::DeathRecipient { |
| 57 | public: |
| 58 | DeathRecipient(UnsolListenerMap* map, pid_t pid, std::mutex& unsolMutex) |
| 59 | : mNetdUnsolicitedEventListenerMap(map), mPid(pid), mMutex(unsolMutex) {} |
| 60 | |
| 61 | private: |
| 62 | void binderDied(const android::wp<android::IBinder>& /* who */) override { |
| 63 | std::lock_guard lock(mMutex); |
| 64 | mNetdUnsolicitedEventListenerMap->erase(mPid); |
| 65 | } |
| 66 | UnsolListenerMap* mNetdUnsolicitedEventListenerMap; |
| 67 | pid_t mPid; |
| 68 | std::mutex& mMutex; |
| 69 | }; |
| 70 | android::sp<android::IBinder::DeathRecipient> deathRecipient = |
| 71 | new DeathRecipient(&mNetdUnsolicitedEventListenerMap, pid, mUnsolicitedMutex); |
| 72 | android::IInterface::asBinder(listener)->linkToDeath(deathRecipient); |
| 73 | } |