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 
17 #define LOG_TAG "Netd"
18 
19 #include "EventReporter.h"
20 #include "log/log.h"
21 
22 using android::interface_cast;
23 using android::net::metrics::INetdEventListener;
24 
setMetricsReportingLevel(const int level)25 int EventReporter::setMetricsReportingLevel(const int level) {
26     if (level < INetdEventListener::REPORTING_LEVEL_NONE
27             || level > INetdEventListener::REPORTING_LEVEL_FULL) {
28         ALOGE("Invalid metrics reporting level %d", level);
29         return -EINVAL;
30     }
31     mReportingLevel = level;
32     return 0;
33 }
34 
getMetricsReportingLevel() const35 int EventReporter::getMetricsReportingLevel() const {
36     return mReportingLevel;
37 }
38 
getNetdEventListener()39 android::sp<INetdEventListener> EventReporter::getNetdEventListener() {
40     std::lock_guard<std::mutex> lock(mutex);
41     if (mNetdEventListener == nullptr) {
42         // Use checkService instead of getService because getService waits for 5 seconds for the
43         // service to become available. The DNS resolver inside netd is started much earlier in the
44         // boot sequence than the framework DNS listener, and we don't want to delay all DNS lookups
45         // for 5 seconds until the DNS listener starts up.
46         android::sp<android::IBinder> b = android::defaultServiceManager()->checkService(
47                 android::String16("netd_listener"));
48         if (b != nullptr) {
49             mNetdEventListener = interface_cast<INetdEventListener>(b);
50         }
51     }
52     // If the netd listener service is dead, the binder call will just return an error, which should
53     // be fine because the only impact is that we can't log netd events. In any case, this should
54     // only happen if the system server is going down, which means it will shortly be taking us down
55     // with it.
56     return mNetdEventListener;
57 }
58