blob: 52dd5c90921e84d107225ee34cd02e05ab995d21 [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
Bernie Innocenti9ee088d2019-02-01 17:14:32 +090041EventReporter::UnsolListeners EventReporter::getNetdUnsolicitedEventListeners() {
Luke Huang528af602018-08-29 19:06:05 +080042 std::lock_guard lock(mUnsolicitedMutex);
Bernie Innocenti9ee088d2019-02-01 17:14:32 +090043 return mUnsolListeners;
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);
Bernie Innocenti9ee088d2019-02-01 17:14:32 +090049 mUnsolListeners.insert(listener);
Luke Huang528af602018-08-29 19:06:05 +080050
51 // Create the death listener.
52 class DeathRecipient : public android::IBinder::DeathRecipient {
53 public:
Bernie Innocenti9ee088d2019-02-01 17:14:32 +090054 DeathRecipient(UnsolListeners* listeners,
55 android::sp<INetdUnsolicitedEventListener> listener, std::mutex& unsolMutex)
56 : mMutex(unsolMutex), mUnsolListeners(listeners), mListener(std::move(listener)) {}
57 ~DeathRecipient() override = default;
Luke Huang528af602018-08-29 19:06:05 +080058
59 private:
60 void binderDied(const android::wp<android::IBinder>& /* who */) override {
61 std::lock_guard lock(mMutex);
Bernie Innocenti9ee088d2019-02-01 17:14:32 +090062 mUnsolListeners->erase(mListener);
Luke Huang528af602018-08-29 19:06:05 +080063 }
Bernie Innocenti9ee088d2019-02-01 17:14:32 +090064
Luke Huang528af602018-08-29 19:06:05 +080065 std::mutex& mMutex;
Bernie Innocenti9ee088d2019-02-01 17:14:32 +090066 UnsolListeners* mUnsolListeners GUARDED_BY(mMutex);
67 android::sp<INetdUnsolicitedEventListener> mListener;
Luke Huang528af602018-08-29 19:06:05 +080068 };
69 android::sp<android::IBinder::DeathRecipient> deathRecipient =
Bernie Innocenti9ee088d2019-02-01 17:14:32 +090070 new DeathRecipient(&mUnsolListeners, listener, mUnsolicitedMutex);
Luke Huang528af602018-08-29 19:06:05 +080071 android::IInterface::asBinder(listener)->linkToDeath(deathRecipient);
Bernie Innocenti9ee088d2019-02-01 17:14:32 +090072}