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