blob: c9d966ab5accaa60bf1c77498acfa419d4036a4a [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
41std::map<pid_t, const android::sp<INetdUnsolicitedEventListener>>
42EventReporter::getNetdUnsolicitedEventListenerVec() {
43 std::lock_guard lock(mUnsolicitedMutex);
44 return mNetdUnsolicitedEventListenerMap;
45}
46
47void 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}