1 /**
2 * Copyright (c) 2020, 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 "carwatchdogd"
18
19 #include "WatchdogBinderMediator.h"
20
21 #include <aidl/android/automotive/watchdog/IoOveruseStats.h>
22 #include <android-base/parseint.h>
23 #include <android-base/stringprintf.h>
24 #include <android-base/strings.h>
25 #include <android/binder_interface_utils.h>
26 #include <binder/IServiceManager.h>
27 #include <log/log.h>
28
29 namespace android {
30 namespace automotive {
31 namespace watchdog {
32
33 using ::aidl::android::automotive::watchdog::ICarWatchdogClient;
34 using ::aidl::android::automotive::watchdog::ICarWatchdogMonitor;
35 using ::aidl::android::automotive::watchdog::IoOveruseStats;
36 using ::aidl::android::automotive::watchdog::IResourceOveruseListener;
37 using ::aidl::android::automotive::watchdog::ResourceOveruseStats;
38 using ::aidl::android::automotive::watchdog::ResourceType;
39 using ::aidl::android::automotive::watchdog::StateType;
40 using ::aidl::android::automotive::watchdog::TimeoutLength;
41 using ::android::defaultServiceManager;
42 using ::android::sp;
43 using ::android::String16;
44 using ::android::base::Error;
45 using ::android::base::Result;
46 using ::android::base::StringAppendF;
47 using ::android::base::StringPrintf;
48 using ::ndk::ICInterface;
49 using ::ndk::ScopedAStatus;
50 using ::ndk::SharedRefBase;
51
52 using AddServiceFunction =
53 std::function<android::base::Result<void>(const char*, ICInterface*, bool, int)>;
54
55 namespace {
56
57 constexpr const char* kCarWatchdogServerInterface =
58 "android.automotive.watchdog.ICarWatchdog/default";
59 constexpr const char* kCarWatchdogInternalServerInterface =
60 "android.automotive.watchdog.internal.ICarWatchdog/default";
61 constexpr const char* kNullCarWatchdogClientError =
62 "Must provide a non-null car watchdog client instance";
63
toScopedAStatus(const int32_t exceptionCode,const std::string & message)64 ScopedAStatus toScopedAStatus(const int32_t exceptionCode, const std::string& message) {
65 ALOGW("%s", message.c_str());
66 return ScopedAStatus::fromExceptionCodeWithMessage(exceptionCode, message.c_str());
67 }
68
addToServiceManager(const char * name,ICInterface * service,bool allowIsolated,int dumpsysFlags)69 Result<void> addToServiceManager(const char* name, ICInterface* service, bool allowIsolated,
70 int dumpsysFlags) {
71 auto serviceStatus = defaultServiceManager()->addService(String16(name),
72 AIBinder_toPlatformBinder(
73 service->asBinder().get()),
74 allowIsolated, dumpsysFlags);
75
76 if (serviceStatus != android::OK) {
77 return Error(FAILED_TRANSACTION) << "Failed to add '" << name << "' to ServiceManager";
78 }
79 return {};
80 }
81
82 } // namespace
83
WatchdogBinderMediator(const android::sp<WatchdogProcessServiceInterface> & watchdogProcessService,const android::sp<WatchdogPerfServiceInterface> & watchdogPerfService,const android::sp<WatchdogServiceHelperInterface> & watchdogServiceHelper,const android::sp<IoOveruseMonitorInterface> & ioOveruseMonitor,const AddServiceFunction & addServiceHandler)84 WatchdogBinderMediator::WatchdogBinderMediator(
85 const android::sp<WatchdogProcessServiceInterface>& watchdogProcessService,
86 const android::sp<WatchdogPerfServiceInterface>& watchdogPerfService,
87 const android::sp<WatchdogServiceHelperInterface>& watchdogServiceHelper,
88 const android::sp<IoOveruseMonitorInterface>& ioOveruseMonitor,
89 const AddServiceFunction& addServiceHandler) :
90 mWatchdogProcessService(watchdogProcessService),
91 mIoOveruseMonitor(ioOveruseMonitor),
92 mAddServiceHandler(addServiceHandler) {
93 if (mAddServiceHandler == nullptr) {
94 mAddServiceHandler = &addToServiceManager;
95 }
96 if (watchdogServiceHelper != nullptr) {
97 mWatchdogInternalHandler =
98 SharedRefBase::make<WatchdogInternalHandler>(watchdogServiceHelper,
99 mWatchdogProcessService,
100 watchdogPerfService,
101 mIoOveruseMonitor);
102 }
103 }
104
init()105 Result<void> WatchdogBinderMediator::init() {
106 if (mWatchdogProcessService == nullptr || mIoOveruseMonitor == nullptr ||
107 mWatchdogInternalHandler == nullptr) {
108 std::string serviceList;
109 if (mWatchdogProcessService == nullptr) {
110 StringAppendF(&serviceList, "%s%s", (!serviceList.empty() ? ", " : ""),
111 "Watchdog process service");
112 }
113 if (mIoOveruseMonitor == nullptr) {
114 StringAppendF(&serviceList, "%s%s", (!serviceList.empty() ? ", " : ""),
115 "I/O overuse monitor service");
116 }
117 if (mWatchdogInternalHandler == nullptr) {
118 StringAppendF(&serviceList, "%s%s", (!serviceList.empty() ? ", " : ""),
119 "Watchdog internal handler");
120 }
121 return Error(INVALID_OPERATION)
122 << serviceList << " must be initialized with non-null instance";
123 }
124 if (const auto result = mWatchdogInternalHandler->init(); !result.ok()) {
125 return result;
126 }
127 if (const auto result = mAddServiceHandler(kCarWatchdogServerInterface, this,
128 /* allowIsolated= */ false,
129 IServiceManager::DUMP_FLAG_PRIORITY_HIGH |
130 IServiceManager::DUMP_FLAG_PROTO);
131 !result.ok()) {
132 return result;
133 }
134 if (const auto result = mAddServiceHandler(kCarWatchdogInternalServerInterface,
135 mWatchdogInternalHandler.get(),
136 /* allowIsolated= */ false,
137 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT);
138 !result.ok()) {
139 return result;
140 }
141 return {};
142 }
143
dump(int fd,const char ** args,uint32_t numArgs)144 binder_status_t WatchdogBinderMediator::dump(int fd, const char** args, uint32_t numArgs) {
145 return mWatchdogInternalHandler->dump(fd, args, numArgs);
146 }
147
registerClient(const std::shared_ptr<ICarWatchdogClient> & client,TimeoutLength timeout)148 ScopedAStatus WatchdogBinderMediator::registerClient(
149 const std::shared_ptr<ICarWatchdogClient>& client, TimeoutLength timeout) {
150 if (client == nullptr) {
151 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, kNullCarWatchdogClientError);
152 }
153 return mWatchdogProcessService->registerClient(client, timeout);
154 }
155
unregisterClient(const std::shared_ptr<ICarWatchdogClient> & client)156 ScopedAStatus WatchdogBinderMediator::unregisterClient(
157 const std::shared_ptr<ICarWatchdogClient>& client) {
158 if (client == nullptr) {
159 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, kNullCarWatchdogClientError);
160 }
161 return mWatchdogProcessService->unregisterClient(client);
162 }
163
tellClientAlive(const std::shared_ptr<ICarWatchdogClient> & client,int32_t sessionId)164 ScopedAStatus WatchdogBinderMediator::tellClientAlive(
165 const std::shared_ptr<ICarWatchdogClient>& client, int32_t sessionId) {
166 if (client == nullptr) {
167 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, kNullCarWatchdogClientError);
168 }
169 return mWatchdogProcessService->tellClientAlive(client, sessionId);
170 }
171
addResourceOveruseListener(const std::vector<ResourceType> & resourceTypes,const std::shared_ptr<IResourceOveruseListener> & listener)172 ScopedAStatus WatchdogBinderMediator::addResourceOveruseListener(
173 const std::vector<ResourceType>& resourceTypes,
174 const std::shared_ptr<IResourceOveruseListener>& listener) {
175 if (listener == nullptr) {
176 return toScopedAStatus(EX_ILLEGAL_ARGUMENT,
177 "Must provide a non-null resource overuse listener");
178 }
179 if (resourceTypes.size() != 1 || resourceTypes[0] != ResourceType::IO) {
180 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, "Must provide exactly one I/O resource type");
181 }
182 /*
183 * When more resource types are added, implement a new module to manage listeners for all
184 * resources.
185 */
186 if (const auto result = mIoOveruseMonitor->addIoOveruseListener(listener); !result.ok()) {
187 return toScopedAStatus(result.error().code(),
188 StringPrintf("Failed to register resource overuse "
189 "listener: %s ",
190 result.error().message().c_str()));
191 }
192 return ScopedAStatus::ok();
193 }
194
removeResourceOveruseListener(const std::shared_ptr<IResourceOveruseListener> & listener)195 ScopedAStatus WatchdogBinderMediator::removeResourceOveruseListener(
196 const std::shared_ptr<IResourceOveruseListener>& listener) {
197 if (listener == nullptr) {
198 return toScopedAStatus(EX_ILLEGAL_ARGUMENT,
199 "Must provide a non-null resource overuse listener");
200 }
201 if (const auto result = mIoOveruseMonitor->removeIoOveruseListener(listener); !result.ok()) {
202 return toScopedAStatus(result.error().code(),
203 StringPrintf("Failed to unregister resource overuse "
204 "listener: %s",
205 result.error().message().c_str()));
206 }
207 return ScopedAStatus::ok();
208 }
209
getResourceOveruseStats(const std::vector<ResourceType> & resourceTypes,std::vector<ResourceOveruseStats> * resourceOveruseStats)210 ScopedAStatus WatchdogBinderMediator::getResourceOveruseStats(
211 const std::vector<ResourceType>& resourceTypes,
212 std::vector<ResourceOveruseStats>* resourceOveruseStats) {
213 if (resourceOveruseStats == nullptr) {
214 return toScopedAStatus(EX_ILLEGAL_ARGUMENT,
215 "Must provide a non-null resource overuse stats "
216 "parcelable");
217 }
218 if (resourceTypes.size() != 1 || resourceTypes[0] != ResourceType::IO) {
219 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, "Must provide exactly one I/O resource type");
220 }
221 IoOveruseStats ioOveruseStats;
222 if (const auto result = mIoOveruseMonitor->getIoOveruseStats(&ioOveruseStats); !result.ok()) {
223 return toScopedAStatus(result.error().code(),
224 StringPrintf("Failed to get resource overuse stats: %s",
225 result.error().message().c_str()));
226 }
227 ResourceOveruseStats stats;
228 stats.set<ResourceOveruseStats::ioOveruseStats>(std::move(ioOveruseStats));
229 resourceOveruseStats->emplace_back(std::move(stats));
230 return ScopedAStatus::ok();
231 }
232
registerMediator(const std::shared_ptr<ICarWatchdogClient> &)233 ScopedAStatus WatchdogBinderMediator::registerMediator(
234 const std::shared_ptr<ICarWatchdogClient>& /*mediator*/) {
235 return toScopedAStatus(EX_UNSUPPORTED_OPERATION, "Deprecated method registerMediator");
236 }
237
unregisterMediator(const std::shared_ptr<ICarWatchdogClient> &)238 ScopedAStatus WatchdogBinderMediator::unregisterMediator(
239 const std::shared_ptr<ICarWatchdogClient>& /*mediator*/) {
240 return toScopedAStatus(EX_UNSUPPORTED_OPERATION, "Deprecated method unregisterMediator");
241 }
242
registerMonitor(const std::shared_ptr<ICarWatchdogMonitor> &)243 ScopedAStatus WatchdogBinderMediator::registerMonitor(
244 const std::shared_ptr<ICarWatchdogMonitor>& /*monitor*/) {
245 return toScopedAStatus(EX_UNSUPPORTED_OPERATION, "Deprecated method registerMonitor");
246 }
247
unregisterMonitor(const std::shared_ptr<ICarWatchdogMonitor> &)248 ScopedAStatus WatchdogBinderMediator::unregisterMonitor(
249 const std::shared_ptr<ICarWatchdogMonitor>& /*monitor*/) {
250 return toScopedAStatus(EX_UNSUPPORTED_OPERATION, "Deprecated method unregisterMonitor");
251 }
252
tellMediatorAlive(const std::shared_ptr<ICarWatchdogClient> &,const std::vector<int32_t> &,int32_t)253 ScopedAStatus WatchdogBinderMediator::tellMediatorAlive(
254 const std::shared_ptr<ICarWatchdogClient>& /*mediator*/,
255 const std::vector<int32_t>& /*clientsNotResponding*/, int32_t /*sessionId*/) {
256 return toScopedAStatus(EX_UNSUPPORTED_OPERATION, "Deprecated method tellMediatorAlive");
257 }
258
tellDumpFinished(const std::shared_ptr<ICarWatchdogMonitor> &,int32_t)259 ScopedAStatus WatchdogBinderMediator::tellDumpFinished(
260 const std::shared_ptr<ICarWatchdogMonitor>& /*monitor*/, int32_t /*pid*/) {
261 return toScopedAStatus(EX_UNSUPPORTED_OPERATION, "Deprecated method tellDumpFinished");
262 }
263
notifySystemStateChange(StateType,int32_t,int32_t)264 ScopedAStatus WatchdogBinderMediator::notifySystemStateChange(StateType /*type*/, int32_t /*arg1*/,
265 int32_t /*arg2*/) {
266 return toScopedAStatus(EX_UNSUPPORTED_OPERATION, "Deprecated method notifySystemStateChange");
267 }
268
269 } // namespace watchdog
270 } // namespace automotive
271 } // namespace android
272