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