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 mWatchdogPerfService(watchdogPerfService),
92 mWatchdogServiceHelper(watchdogServiceHelper),
93 mIoOveruseMonitor(ioOveruseMonitor),
94 mAddServiceHandler(addServiceHandler) {
95 if (mAddServiceHandler == nullptr) {
96 mAddServiceHandler = &addToServiceManager;
97 }
98 if (watchdogServiceHelper != nullptr) {
99 mWatchdogInternalHandler =
100 SharedRefBase::make<WatchdogInternalHandler>(watchdogServiceHelper,
101 mWatchdogProcessService,
102 mWatchdogPerfService,
103 mIoOveruseMonitor);
104 }
105 }
106
init()107 Result<void> WatchdogBinderMediator::init() {
108 if (mWatchdogProcessService == nullptr || mWatchdogPerfService == nullptr ||
109 mWatchdogServiceHelper == nullptr || mIoOveruseMonitor == nullptr) {
110 std::string serviceList;
111 if (mWatchdogProcessService == nullptr) {
112 StringAppendF(&serviceList, "%s%s", (!serviceList.empty() ? ", " : ""),
113 "Watchdog process service");
114 }
115 if (mWatchdogPerfService == nullptr) {
116 StringAppendF(&serviceList, "%s%s", (!serviceList.empty() ? ", " : ""),
117 "Watchdog performance service");
118 }
119 if (mWatchdogServiceHelper == nullptr) {
120 StringAppendF(&serviceList, "%s%s", (!serviceList.empty() ? ", " : ""),
121 "Watchdog service helper");
122 }
123 if (mIoOveruseMonitor == nullptr) {
124 StringAppendF(&serviceList, "%s%s", (!serviceList.empty() ? ", " : ""),
125 "I/O overuse monitor service");
126 }
127 return Error(INVALID_OPERATION)
128 << serviceList << " must be initialized with non-null instance";
129 }
130 if (const auto result = mAddServiceHandler(kCarWatchdogServerInterface, this,
131 /* allowIsolated= */ false,
132 IServiceManager::DUMP_FLAG_PRIORITY_HIGH |
133 IServiceManager::DUMP_FLAG_PROTO);
134 !result.ok()) {
135 return result;
136 }
137 if (const auto result = mAddServiceHandler(kCarWatchdogInternalServerInterface,
138 mWatchdogInternalHandler.get(),
139 /* allowIsolated= */ false,
140 IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT);
141 !result.ok()) {
142 return result;
143 }
144 return {};
145 }
146
dump(int fd,const char ** args,uint32_t numArgs)147 binder_status_t WatchdogBinderMediator::dump(int fd, const char** args, uint32_t numArgs) {
148 return mWatchdogInternalHandler->dump(fd, args, numArgs);
149 }
150
registerClient(const std::shared_ptr<ICarWatchdogClient> & client,TimeoutLength timeout)151 ScopedAStatus WatchdogBinderMediator::registerClient(
152 const std::shared_ptr<ICarWatchdogClient>& client, TimeoutLength timeout) {
153 if (client == nullptr) {
154 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, kNullCarWatchdogClientError);
155 }
156 return mWatchdogProcessService->registerClient(client, timeout);
157 }
158
unregisterClient(const std::shared_ptr<ICarWatchdogClient> & client)159 ScopedAStatus WatchdogBinderMediator::unregisterClient(
160 const std::shared_ptr<ICarWatchdogClient>& client) {
161 if (client == nullptr) {
162 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, kNullCarWatchdogClientError);
163 }
164 return mWatchdogProcessService->unregisterClient(client);
165 }
166
tellClientAlive(const std::shared_ptr<ICarWatchdogClient> & client,int32_t sessionId)167 ScopedAStatus WatchdogBinderMediator::tellClientAlive(
168 const std::shared_ptr<ICarWatchdogClient>& client, int32_t sessionId) {
169 if (client == nullptr) {
170 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, kNullCarWatchdogClientError);
171 }
172 return mWatchdogProcessService->tellClientAlive(client, sessionId);
173 }
174
addResourceOveruseListener(const std::vector<ResourceType> & resourceTypes,const std::shared_ptr<IResourceOveruseListener> & listener)175 ScopedAStatus WatchdogBinderMediator::addResourceOveruseListener(
176 const std::vector<ResourceType>& resourceTypes,
177 const std::shared_ptr<IResourceOveruseListener>& listener) {
178 if (listener == nullptr) {
179 return toScopedAStatus(EX_ILLEGAL_ARGUMENT,
180 "Must provide a non-null resource overuse listener");
181 }
182 if (resourceTypes.size() != 1 || resourceTypes[0] != ResourceType::IO) {
183 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, "Must provide exactly one I/O resource type");
184 }
185 /*
186 * When more resource types are added, implement a new module to manage listeners for all
187 * resources.
188 */
189 if (const auto result = mIoOveruseMonitor->addIoOveruseListener(listener); !result.ok()) {
190 return toScopedAStatus(result.error().code(),
191 StringPrintf("Failed to register resource overuse "
192 "listener: %s ",
193 result.error().message().c_str()));
194 }
195 return ScopedAStatus::ok();
196 }
197
removeResourceOveruseListener(const std::shared_ptr<IResourceOveruseListener> & listener)198 ScopedAStatus WatchdogBinderMediator::removeResourceOveruseListener(
199 const std::shared_ptr<IResourceOveruseListener>& listener) {
200 if (listener == nullptr) {
201 return toScopedAStatus(EX_ILLEGAL_ARGUMENT,
202 "Must provide a non-null resource overuse listener");
203 }
204 if (const auto result = mIoOveruseMonitor->removeIoOveruseListener(listener); !result.ok()) {
205 return toScopedAStatus(result.error().code(),
206 StringPrintf("Failed to unregister resource overuse "
207 "listener: %s",
208 result.error().message().c_str()));
209 }
210 return ScopedAStatus::ok();
211 }
212
getResourceOveruseStats(const std::vector<ResourceType> & resourceTypes,std::vector<ResourceOveruseStats> * resourceOveruseStats)213 ScopedAStatus WatchdogBinderMediator::getResourceOveruseStats(
214 const std::vector<ResourceType>& resourceTypes,
215 std::vector<ResourceOveruseStats>* resourceOveruseStats) {
216 if (resourceOveruseStats == nullptr) {
217 return toScopedAStatus(EX_ILLEGAL_ARGUMENT,
218 "Must provide a non-null resource overuse stats "
219 "parcelable");
220 }
221 if (resourceTypes.size() != 1 || resourceTypes[0] != ResourceType::IO) {
222 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, "Must provide exactly one I/O resource type");
223 }
224 IoOveruseStats ioOveruseStats;
225 if (const auto result = mIoOveruseMonitor->getIoOveruseStats(&ioOveruseStats); !result.ok()) {
226 return toScopedAStatus(result.error().code(),
227 StringPrintf("Failed to get resource overuse stats: %s",
228 result.error().message().c_str()));
229 }
230 ResourceOveruseStats stats;
231 stats.set<ResourceOveruseStats::ioOveruseStats>(std::move(ioOveruseStats));
232 resourceOveruseStats->emplace_back(std::move(stats));
233 return ScopedAStatus::ok();
234 }
235
registerMediator(const std::shared_ptr<ICarWatchdogClient> &)236 ScopedAStatus WatchdogBinderMediator::registerMediator(
237 const std::shared_ptr<ICarWatchdogClient>& /*mediator*/) {
238 return toScopedAStatus(EX_UNSUPPORTED_OPERATION, "Deprecated method registerMediator");
239 }
240
unregisterMediator(const std::shared_ptr<ICarWatchdogClient> &)241 ScopedAStatus WatchdogBinderMediator::unregisterMediator(
242 const std::shared_ptr<ICarWatchdogClient>& /*mediator*/) {
243 return toScopedAStatus(EX_UNSUPPORTED_OPERATION, "Deprecated method unregisterMediator");
244 }
245
registerMonitor(const std::shared_ptr<ICarWatchdogMonitor> &)246 ScopedAStatus WatchdogBinderMediator::registerMonitor(
247 const std::shared_ptr<ICarWatchdogMonitor>& /*monitor*/) {
248 return toScopedAStatus(EX_UNSUPPORTED_OPERATION, "Deprecated method registerMonitor");
249 }
250
unregisterMonitor(const std::shared_ptr<ICarWatchdogMonitor> &)251 ScopedAStatus WatchdogBinderMediator::unregisterMonitor(
252 const std::shared_ptr<ICarWatchdogMonitor>& /*monitor*/) {
253 return toScopedAStatus(EX_UNSUPPORTED_OPERATION, "Deprecated method unregisterMonitor");
254 }
255
tellMediatorAlive(const std::shared_ptr<ICarWatchdogClient> &,const std::vector<int32_t> &,int32_t)256 ScopedAStatus WatchdogBinderMediator::tellMediatorAlive(
257 const std::shared_ptr<ICarWatchdogClient>& /*mediator*/,
258 const std::vector<int32_t>& /*clientsNotResponding*/, int32_t /*sessionId*/) {
259 return toScopedAStatus(EX_UNSUPPORTED_OPERATION, "Deprecated method tellMediatorAlive");
260 }
261
tellDumpFinished(const std::shared_ptr<ICarWatchdogMonitor> &,int32_t)262 ScopedAStatus WatchdogBinderMediator::tellDumpFinished(
263 const std::shared_ptr<ICarWatchdogMonitor>& /*monitor*/, int32_t /*pid*/) {
264 return toScopedAStatus(EX_UNSUPPORTED_OPERATION, "Deprecated method tellDumpFinished");
265 }
266
notifySystemStateChange(StateType,int32_t,int32_t)267 ScopedAStatus WatchdogBinderMediator::notifySystemStateChange(StateType /*type*/, int32_t /*arg1*/,
268 int32_t /*arg2*/) {
269 return toScopedAStatus(EX_UNSUPPORTED_OPERATION, "Deprecated method notifySystemStateChange");
270 }
271
272 } // namespace watchdog
273 } // namespace automotive
274 } // namespace android
275