blob: 64a13935958c3f1c53d2cb38bb3731b7f512a9fa [file] [log] [blame]
Michal Karpinskibe581e22016-10-06 16:56:04 +01001/*
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 Karpinskibe581e22016-10-06 16:56:04 +010017#include "EventReporter.h"
Michal Karpinskibe581e22016-10-06 16:56:04 +010018
19using android::interface_cast;
Luke Huang528af602018-08-29 19:06:05 +080020using android::net::INetdUnsolicitedEventListener;
Michal Karpinskibe581e22016-10-06 16:56:04 +010021using android::net::metrics::INetdEventListener;
22
Michal Karpinskibe581e22016-10-06 16:56:04 +010023android::sp<INetdEventListener> EventReporter::getNetdEventListener() {
Luke Huang528af602018-08-29 19:06:05 +080024 std::lock_guard lock(mEventMutex);
Michal Karpinskibe581e22016-10-06 16:56:04 +010025 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 Huang528af602018-08-29 19:06:05 +080032 mNetdEventListener = interface_cast<INetdEventListener>(b);
Michal Karpinskibe581e22016-10-06 16:56:04 +010033 }
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 Huang528af602018-08-29 19:06:05 +080040
Luke Huang900f18e2019-01-31 16:50:17 +080041EventReporter::UnsolListenerMap EventReporter::getNetdUnsolicitedEventListenerMap() const {
Luke Huang528af602018-08-29 19:06:05 +080042 std::lock_guard lock(mUnsolicitedMutex);
Luke Huang900f18e2019-01-31 16:50:17 +080043 return mUnsolListenerMap;
Luke Huang528af602018-08-29 19:06:05 +080044}
45
46void EventReporter::registerUnsolEventListener(
Bernie Innocenti9ee088d2019-02-01 17:14:32 +090047 const android::sp<INetdUnsolicitedEventListener>& listener) {
Luke Huang528af602018-08-29 19:06:05 +080048 std::lock_guard lock(mUnsolicitedMutex);
Luke Huang528af602018-08-29 19:06:05 +080049
50 // Create the death listener.
51 class DeathRecipient : public android::IBinder::DeathRecipient {
52 public:
Luke Huang900f18e2019-01-31 16:50:17 +080053 DeathRecipient(EventReporter* eventReporter,
54 android::sp<INetdUnsolicitedEventListener> listener)
55 : mEventReporter(eventReporter), mListener(std::move(listener)) {}
Bernie Innocenti9ee088d2019-02-01 17:14:32 +090056 ~DeathRecipient() override = default;
Luke Huang528af602018-08-29 19:06:05 +080057 void binderDied(const android::wp<android::IBinder>& /* who */) override {
Luke Huang900f18e2019-01-31 16:50:17 +080058 mEventReporter->unregisterUnsolEventListener(mListener);
Luke Huang528af602018-08-29 19:06:05 +080059 }
Bernie Innocenti9ee088d2019-02-01 17:14:32 +090060
Luke Huang900f18e2019-01-31 16:50:17 +080061 private:
62 EventReporter* mEventReporter;
Bernie Innocenti9ee088d2019-02-01 17:14:32 +090063 android::sp<INetdUnsolicitedEventListener> mListener;
Luke Huang528af602018-08-29 19:06:05 +080064 };
65 android::sp<android::IBinder::DeathRecipient> deathRecipient =
Luke Huang900f18e2019-01-31 16:50:17 +080066 new DeathRecipient(this, listener);
67
Luke Huang528af602018-08-29 19:06:05 +080068 android::IInterface::asBinder(listener)->linkToDeath(deathRecipient);
Luke Huang900f18e2019-01-31 16:50:17 +080069
70 // TODO: Consider to use remote binder address as registering key
71 mUnsolListenerMap.insert({listener, deathRecipient});
72}
73
74void EventReporter::unregisterUnsolEventListener(
75 const android::sp<INetdUnsolicitedEventListener>& listener) {
76 std::lock_guard lock(mUnsolicitedMutex);
77 mUnsolListenerMap.erase(listener);
Bernie Innocenti9ee088d2019-02-01 17:14:32 +090078}