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 "WatchdogInternalHandler.h"
20
21 #include "UidProcStatsCollector.h"
22
23 #include <aidl/android/automotive/watchdog/internal/BootPhase.h>
24 #include <aidl/android/automotive/watchdog/internal/GarageMode.h>
25 #include <android-base/file.h>
26 #include <binder/IPCThreadState.h>
27 #include <private/android_filesystem_config.h>
28
29 namespace android {
30 namespace automotive {
31 namespace watchdog {
32
33 using ::aidl::android::automotive::watchdog::internal::BootPhase;
34 using ::aidl::android::automotive::watchdog::internal::ComponentType;
35 using ::aidl::android::automotive::watchdog::internal::GarageMode;
36 using ::aidl::android::automotive::watchdog::internal::ICarWatchdogMonitor;
37 using ::aidl::android::automotive::watchdog::internal::ICarWatchdogServiceForSystem;
38 using ::aidl::android::automotive::watchdog::internal::PowerCycle;
39 using ::aidl::android::automotive::watchdog::internal::ProcessIdentifier;
40 using ::aidl::android::automotive::watchdog::internal::ResourceOveruseConfiguration;
41 using ::aidl::android::automotive::watchdog::internal::StateType;
42 using ::aidl::android::automotive::watchdog::internal::ThreadPolicyWithPriority;
43 using ::aidl::android::automotive::watchdog::internal::UserPackageIoUsageStats;
44 using ::aidl::android::automotive::watchdog::internal::UserState;
45 using ::android::sp;
46 using ::android::String16;
47 using ::android::base::EqualsIgnoreCase;
48 using ::android::base::Join;
49 using ::android::base::Result;
50 using ::android::base::Split;
51 using ::android::base::StringPrintf;
52 using ::android::base::WriteStringToFd;
53 using ::ndk::ScopedAStatus;
54
55 namespace {
56
57 constexpr const char* kDumpAllFlag = "-a";
58 constexpr const char* kHelpFlag = "--help";
59 constexpr const char* kHelpShortFlag = "-h";
60 constexpr const char* kHelpText =
61 "Car watchdog daemon dumpsys help page:\n"
62 "Format: dumpsys android.automotive.watchdog.ICarWatchdog/default [options]\n\n"
63 "%s or %s: Displays this help text.\n"
64 "When no options are specified, car watchdog report is generated.\n";
65 constexpr const char* kNullCarWatchdogServiceError =
66 "Must provide a non-null car watchdog service instance";
67 constexpr const char* kNullCarWatchdogMonitorError =
68 "Must provide a non-null car watchdog monitor instance";
69
toScopedAStatus(int32_t exceptionCode,const std::string & message)70 ScopedAStatus toScopedAStatus(int32_t exceptionCode, const std::string& message) {
71 ALOGW("%s", message.c_str());
72 return ScopedAStatus::fromExceptionCodeWithMessage(exceptionCode, message.c_str());
73 }
74
toScopedAStatus(const Result<void> & result)75 ScopedAStatus toScopedAStatus(const Result<void>& result) {
76 return toScopedAStatus(result.error().code(), result.error().message());
77 }
78
checkSystemUser(const std::string & methodName)79 ScopedAStatus checkSystemUser(const std::string& methodName) {
80 if (IPCThreadState::self()->getCallingUid() != AID_SYSTEM) {
81 return toScopedAStatus(EX_SECURITY,
82 StringPrintf("Calling process does not have proper "
83 "privilege to call %s",
84 methodName.c_str()));
85 }
86 return ScopedAStatus::ok();
87 }
88
89 } // namespace
90
dump(int fd,const char ** args,uint32_t numArgs)91 binder_status_t WatchdogInternalHandler::dump(int fd, const char** args, uint32_t numArgs) {
92 if (numArgs == 0 || strcmp(args[0], kDumpAllFlag) == 0) {
93 return dumpServices(fd);
94 }
95 if (numArgs == 1 &&
96 (EqualsIgnoreCase(args[0], kHelpFlag) || EqualsIgnoreCase(args[0], kHelpShortFlag))) {
97 return dumpHelpText(fd, "");
98 }
99 if (EqualsIgnoreCase(args[0], kStartCustomCollectionFlag) ||
100 EqualsIgnoreCase(args[0], kEndCustomCollectionFlag)) {
101 if (auto result = mWatchdogPerfService->onCustomCollection(fd, args, numArgs);
102 !result.ok()) {
103 std::string mode =
104 EqualsIgnoreCase(args[0], kStartCustomCollectionFlag) ? "start" : "stop";
105 std::string errorMsg = StringPrintf("Failed to %s custom perf collection: %s",
106 mode.c_str(), result.error().message().c_str());
107 if (result.error().code() == BAD_VALUE) {
108 dumpHelpText(fd, errorMsg);
109 } else {
110 ALOGW("%s", errorMsg.c_str());
111 WriteStringToFd(StringPrintf("Error: %s\n", errorMsg.c_str()), fd);
112 }
113 return result.error().code();
114 }
115 std::string mode =
116 EqualsIgnoreCase(args[0], kStartCustomCollectionFlag) ? "started" : "stopped";
117 // The message returned on success is used in the integration tests. If this message is
118 // updated, the CarWatchdog's integration tests must be updated too.
119 WriteStringToFd(StringPrintf("Successfully %s custom perf collection\n", mode.c_str()), fd);
120 return OK;
121 }
122 if (numArgs == 2 && EqualsIgnoreCase(args[0], kResetResourceOveruseStatsFlag)) {
123 std::string value = std::string(args[1]);
124 std::vector<std::string> packageNames = Split(value, ",");
125 if (value.empty() || packageNames.empty()) {
126 dumpHelpText(fd,
127 StringPrintf("Must provide valid package names: [%s]\n", value.c_str()));
128 return BAD_VALUE;
129 }
130 if (auto result = mIoOveruseMonitor->resetIoOveruseStats(packageNames); !result.ok()) {
131 ALOGW("Failed to reset stats for packages: [%s]", value.c_str());
132 return FAILED_TRANSACTION;
133 }
134 return OK;
135 }
136 std::vector<const char*> argsVector;
137 for (uint32_t i = 0; i < numArgs; ++i) {
138 argsVector.push_back(args[i]);
139 }
140 dumpHelpText(fd,
141 StringPrintf("Invalid car watchdog dumpsys options: [%s]\n",
142 Join(argsVector, " ").c_str()));
143 return dumpServices(fd);
144 }
145
dumpServices(int fd)146 status_t WatchdogInternalHandler::dumpServices(int fd) {
147 mWatchdogProcessService->onDump(fd);
148 if (auto result = mWatchdogPerfService->onDump(fd); !result.ok()) {
149 ALOGW("Failed to dump car watchdog perf service: %s", result.error().message().c_str());
150 return result.error().code();
151 }
152 if (auto result = mIoOveruseMonitor->onDump(fd); !result.ok()) {
153 ALOGW("Failed to dump I/O overuse monitor: %s", result.error().message().c_str());
154 return result.error().code();
155 }
156 return OK;
157 }
158
dumpHelpText(const int fd,const std::string & errorMsg)159 status_t WatchdogInternalHandler::dumpHelpText(const int fd, const std::string& errorMsg) {
160 if (!errorMsg.empty()) {
161 ALOGW("Error: %s", errorMsg.c_str());
162 if (!WriteStringToFd(StringPrintf("Error: %s\n\n", errorMsg.c_str()), fd)) {
163 ALOGW("Failed to write error message to fd");
164 return FAILED_TRANSACTION;
165 }
166 }
167 if (!WriteStringToFd(StringPrintf(kHelpText, kHelpFlag, kHelpShortFlag), fd) ||
168 !mWatchdogPerfService->dumpHelpText(fd) || !mIoOveruseMonitor->dumpHelpText(fd)) {
169 ALOGW("Failed to write help text to fd");
170 return FAILED_TRANSACTION;
171 }
172 return OK;
173 }
174
checkAndRegisterIoOveruseMonitor()175 void WatchdogInternalHandler::checkAndRegisterIoOveruseMonitor() {
176 if (mIoOveruseMonitor->isInitialized()) {
177 return;
178 }
179 if (const auto result = mWatchdogPerfService->registerDataProcessor(mIoOveruseMonitor);
180 !result.ok()) {
181 ALOGE("Failed to register I/O overuse monitor to watchdog performance service: %s",
182 result.error().message().c_str());
183 }
184 return;
185 }
186
registerCarWatchdogService(const std::shared_ptr<ICarWatchdogServiceForSystem> & service)187 ScopedAStatus WatchdogInternalHandler::registerCarWatchdogService(
188 const std::shared_ptr<ICarWatchdogServiceForSystem>& service) {
189 if (auto status = checkSystemUser(/*methodName=*/"registerCarWatchdogService");
190 !status.isOk()) {
191 return status;
192 }
193 if (service == nullptr) {
194 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, kNullCarWatchdogServiceError);
195 }
196 /*
197 * I/O overuse monitor reads from system, vendor, and data partitions during initialization.
198 * When CarService is running these partitions are available to read, thus register the I/O
199 * overuse monitor on processing the request to register CarService.
200 */
201 checkAndRegisterIoOveruseMonitor();
202 auto status = mWatchdogServiceHelper->registerService(service);
203 if (status.isOk()) {
204 mWatchdogPerfService->onCarWatchdogServiceRegistered();
205 }
206 return status;
207 }
208
unregisterCarWatchdogService(const std::shared_ptr<ICarWatchdogServiceForSystem> & service)209 ScopedAStatus WatchdogInternalHandler::unregisterCarWatchdogService(
210 const std::shared_ptr<ICarWatchdogServiceForSystem>& service) {
211 if (auto status = checkSystemUser(/*methodName=*/"unregisterCarWatchdogService");
212 !status.isOk()) {
213 return status;
214 }
215 if (service == nullptr) {
216 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, kNullCarWatchdogServiceError);
217 }
218 return mWatchdogServiceHelper->unregisterService(service);
219 }
220
registerMonitor(const std::shared_ptr<ICarWatchdogMonitor> & monitor)221 ScopedAStatus WatchdogInternalHandler::registerMonitor(
222 const std::shared_ptr<ICarWatchdogMonitor>& monitor) {
223 if (auto status = checkSystemUser(/*methodName=*/"registerMonitor"); !status.isOk()) {
224 return status;
225 }
226 if (monitor == nullptr) {
227 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, kNullCarWatchdogMonitorError);
228 }
229 return mWatchdogProcessService->registerMonitor(monitor);
230 }
231
unregisterMonitor(const std::shared_ptr<ICarWatchdogMonitor> & monitor)232 ScopedAStatus WatchdogInternalHandler::unregisterMonitor(
233 const std::shared_ptr<ICarWatchdogMonitor>& monitor) {
234 if (auto status = checkSystemUser(/*methodName=*/"unregisterMonitor"); !status.isOk()) {
235 return status;
236 }
237 if (monitor == nullptr) {
238 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, kNullCarWatchdogMonitorError);
239 }
240 return mWatchdogProcessService->unregisterMonitor(monitor);
241 }
242
tellCarWatchdogServiceAlive(const std::shared_ptr<ICarWatchdogServiceForSystem> & service,const std::vector<ProcessIdentifier> & clientsNotResponding,int32_t sessionId)243 ScopedAStatus WatchdogInternalHandler::tellCarWatchdogServiceAlive(
244 const std::shared_ptr<ICarWatchdogServiceForSystem>& service,
245 const std::vector<ProcessIdentifier>& clientsNotResponding, int32_t sessionId) {
246 if (auto status = checkSystemUser(/*methodName=*/"tellCarWatchdogServiceAlive");
247 !status.isOk()) {
248 return status;
249 }
250 if (service == nullptr) {
251 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, kNullCarWatchdogServiceError);
252 }
253 return mWatchdogProcessService->tellCarWatchdogServiceAlive(service, clientsNotResponding,
254 sessionId);
255 }
256
tellDumpFinished(const std::shared_ptr<ICarWatchdogMonitor> & monitor,const ProcessIdentifier & processIdentifier)257 ScopedAStatus WatchdogInternalHandler::tellDumpFinished(
258 const std::shared_ptr<ICarWatchdogMonitor>& monitor,
259 const ProcessIdentifier& processIdentifier) {
260 if (auto status = checkSystemUser(/*methodName=*/"tellDumpFinished"); !status.isOk()) {
261 return status;
262 }
263 if (monitor == nullptr) {
264 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, kNullCarWatchdogMonitorError);
265 }
266 return mWatchdogProcessService->tellDumpFinished(monitor, processIdentifier);
267 }
268
notifySystemStateChange(StateType type,int32_t arg1,int32_t arg2)269 ScopedAStatus WatchdogInternalHandler::notifySystemStateChange(StateType type, int32_t arg1,
270 int32_t arg2) {
271 if (auto status = checkSystemUser(/*methodName=*/"notifySystemStateChange"); !status.isOk()) {
272 return status;
273 }
274 switch (type) {
275 case StateType::POWER_CYCLE: {
276 PowerCycle powerCycle = static_cast<PowerCycle>(static_cast<uint32_t>(arg1));
277 return handlePowerCycleChange(powerCycle);
278 }
279 case StateType::GARAGE_MODE: {
280 GarageMode garageMode = static_cast<GarageMode>(static_cast<uint32_t>(arg1));
281 mWatchdogPerfService->setSystemState(garageMode == GarageMode::GARAGE_MODE_OFF
282 ? SystemState::NORMAL_MODE
283 : SystemState::GARAGE_MODE);
284 return ScopedAStatus::ok();
285 }
286 case StateType::USER_STATE: {
287 userid_t userId = static_cast<userid_t>(arg1);
288 UserState userState = static_cast<UserState>(static_cast<uint32_t>(arg2));
289 return handleUserStateChange(userId, userState);
290 }
291 case StateType::BOOT_PHASE: {
292 BootPhase phase = static_cast<BootPhase>(static_cast<uint32_t>(arg1));
293 if (phase >= BootPhase::BOOT_COMPLETED) {
294 if (const auto result = mWatchdogPerfService->onBootFinished(); !result.ok()) {
295 return toScopedAStatus(result);
296 }
297 }
298 return ScopedAStatus::ok();
299 }
300 }
301 return toScopedAStatus(EX_ILLEGAL_ARGUMENT, StringPrintf("Invalid state change type %d", type));
302 }
303
handlePowerCycleChange(PowerCycle powerCycle)304 ScopedAStatus WatchdogInternalHandler::handlePowerCycleChange(PowerCycle powerCycle) {
305 switch (powerCycle) {
306 case PowerCycle::POWER_CYCLE_SHUTDOWN_PREPARE:
307 ALOGI("Received SHUTDOWN_PREPARE power cycle");
308 mWatchdogProcessService->setEnabled(/*isEnabled=*/false);
309 break;
310 case PowerCycle::POWER_CYCLE_SHUTDOWN_ENTER:
311 ALOGI("Received SHUTDOWN_ENTER power cycle");
312 mWatchdogProcessService->setEnabled(/*isEnabled=*/false);
313 mWatchdogPerfService->onShutdownEnter();
314 break;
315 case PowerCycle::POWER_CYCLE_SUSPEND_EXIT:
316 ALOGI("Received SUSPEND_EXIT power cycle");
317 mWatchdogPerfService->onSuspendExit();
318 break;
319 case PowerCycle::POWER_CYCLE_RESUME:
320 ALOGI("Received RESUME power cycle");
321 mWatchdogProcessService->setEnabled(/*isEnabled=*/true);
322 break;
323 default:
324 return toScopedAStatus(EX_ILLEGAL_ARGUMENT,
325 StringPrintf("Unsupported power cycle: %d", powerCycle));
326 }
327 return ScopedAStatus::ok();
328 }
329
handleUserStateChange(userid_t userId,const UserState & userState)330 ScopedAStatus WatchdogInternalHandler::handleUserStateChange(userid_t userId,
331 const UserState& userState) {
332 std::string stateDesc;
333 switch (userState) {
334 case UserState::USER_STATE_STARTED:
335 stateDesc = "started";
336 mWatchdogProcessService->onUserStateChange(userId, /*isStarted=*/true);
337 break;
338 case UserState::USER_STATE_SWITCHING:
339 stateDesc = "switching";
340 mWatchdogPerfService->onUserStateChange(userId, userState);
341 break;
342 case UserState::USER_STATE_UNLOCKING:
343 stateDesc = "unlocking";
344 mWatchdogPerfService->onUserStateChange(userId, userState);
345 break;
346 case UserState::USER_STATE_POST_UNLOCKED:
347 stateDesc = "post_unlocked";
348 mWatchdogPerfService->onUserStateChange(userId, userState);
349 break;
350 case UserState::USER_STATE_STOPPED:
351 stateDesc = "stopped";
352 mWatchdogProcessService->onUserStateChange(userId, /*isStarted=*/false);
353 break;
354 case UserState::USER_STATE_REMOVED:
355 stateDesc = "removed";
356 mIoOveruseMonitor->removeStatsForUser(userId);
357 break;
358 default:
359 // UserState::USER_STATE_UNLOCKED is not sent by CarService to the daemon. If signal is
360 // received, an exception will be thrown.
361 return toScopedAStatus(EX_ILLEGAL_ARGUMENT,
362 StringPrintf("Unsupported user state: %d", userState));
363 }
364 ALOGI("Received user state change: user(%" PRId32 ") is %s", userId, stateDesc.c_str());
365 return ScopedAStatus::ok();
366 }
367
updateResourceOveruseConfigurations(const std::vector<ResourceOveruseConfiguration> & configs)368 ScopedAStatus WatchdogInternalHandler::updateResourceOveruseConfigurations(
369 const std::vector<ResourceOveruseConfiguration>& configs) {
370 if (auto status = checkSystemUser(/*methodName=*/"updateResourceOveruseConfigurations");
371 !status.isOk()) {
372 return status;
373 }
374 // Maybe retry registring I/O overuse monitor if failed to initialize previously.
375 checkAndRegisterIoOveruseMonitor();
376 if (auto result = mIoOveruseMonitor->updateResourceOveruseConfigurations(configs);
377 !result.ok()) {
378 return toScopedAStatus(result);
379 }
380 return ScopedAStatus::ok();
381 }
382
getResourceOveruseConfigurations(std::vector<ResourceOveruseConfiguration> * configs)383 ScopedAStatus WatchdogInternalHandler::getResourceOveruseConfigurations(
384 std::vector<ResourceOveruseConfiguration>* configs) {
385 if (auto status = checkSystemUser(/*methodName=*/"getResourceOveruseConfigurations");
386 !status.isOk()) {
387 return status;
388 }
389 // Maybe retry registring I/O overuse monitor if failed to initialize previously.
390 checkAndRegisterIoOveruseMonitor();
391 if (auto result = mIoOveruseMonitor->getResourceOveruseConfigurations(configs); !result.ok()) {
392 return toScopedAStatus(result);
393 }
394 return ScopedAStatus::ok();
395 }
396
controlProcessHealthCheck(bool enable)397 ScopedAStatus WatchdogInternalHandler::controlProcessHealthCheck(bool enable) {
398 if (auto status = checkSystemUser(/*methodName=*/"controlProcessHealthCheck"); !status.isOk()) {
399 return status;
400 }
401 mWatchdogProcessService->setEnabled(enable);
402 return ScopedAStatus::ok();
403 }
404
setThreadPriority(int pid,int tid,int uid,int policy,int priority)405 ScopedAStatus WatchdogInternalHandler::setThreadPriority(int pid, int tid, int uid, int policy,
406 int priority) {
407 if (auto status = checkSystemUser(/*methodName=*/"setThreadPriority"); !status.isOk()) {
408 return status;
409 }
410 if (auto result = mThreadPriorityController->setThreadPriority(pid, tid, uid, policy, priority);
411 !result.ok()) {
412 return toScopedAStatus(result);
413 }
414 return ScopedAStatus::ok();
415 }
416
getThreadPriority(int pid,int tid,int uid,ThreadPolicyWithPriority * threadPolicyWithPriority)417 ScopedAStatus WatchdogInternalHandler::getThreadPriority(
418 int pid, int tid, int uid, ThreadPolicyWithPriority* threadPolicyWithPriority) {
419 if (auto status = checkSystemUser(/*methodName=*/"getThreadPriority"); !status.isOk()) {
420 return status;
421 }
422 if (auto result = mThreadPriorityController->getThreadPriority(pid, tid, uid,
423 threadPolicyWithPriority);
424 !result.ok()) {
425 return toScopedAStatus(result);
426 }
427 return ScopedAStatus::ok();
428 }
429
onAidlVhalPidFetched(int pid)430 ScopedAStatus WatchdogInternalHandler::onAidlVhalPidFetched(int pid) {
431 if (auto status = checkSystemUser(/*methodName=*/"onAidlVhalPidFetched"); !status.isOk()) {
432 return status;
433 }
434 mWatchdogProcessService->onAidlVhalPidFetched(pid);
435 return ScopedAStatus::ok();
436 }
437
setThreadPriorityController(std::unique_ptr<ThreadPriorityControllerInterface> threadPriorityController)438 void WatchdogInternalHandler::setThreadPriorityController(
439 std::unique_ptr<ThreadPriorityControllerInterface> threadPriorityController) {
440 mThreadPriorityController = std::move(threadPriorityController);
441 }
442
onTodayIoUsageStatsFetched(const std::vector<UserPackageIoUsageStats> & userPackageIoUsageStats)443 ScopedAStatus WatchdogInternalHandler::onTodayIoUsageStatsFetched(
444 const std::vector<UserPackageIoUsageStats>& userPackageIoUsageStats) {
445 if (auto status = checkSystemUser(/*methodName=*/"onTodayIoUsageStatsFetched");
446 !status.isOk()) {
447 return status;
448 }
449 if (auto result = mIoOveruseMonitor->onTodayIoUsageStatsFetched(userPackageIoUsageStats);
450 !result.ok()) {
451 return toScopedAStatus(result);
452 }
453 return ScopedAStatus::ok();
454 }
455
456 } // namespace watchdog
457 } // namespace automotive
458 } // namespace android
459