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 #pragma once 18 19 #include "AIBinderDeathRegistrationWrapper.h" 20 #include "UidProcStatsCollector.h" 21 22 #include <aidl/android/automotive/watchdog/ICarWatchdogClient.h> 23 #include <aidl/android/automotive/watchdog/TimeoutLength.h> 24 #include <aidl/android/automotive/watchdog/internal/GarageMode.h> 25 #include <aidl/android/automotive/watchdog/internal/ICarWatchdogMonitor.h> 26 #include <aidl/android/automotive/watchdog/internal/ICarWatchdogServiceForSystem.h> 27 #include <aidl/android/automotive/watchdog/internal/ProcessIdentifier.h> 28 #include <android-base/chrono_utils.h> 29 #include <android-base/result.h> 30 #include <android/binder_auto_utils.h> 31 #include <android/hidl/manager/1.0/IServiceManager.h> 32 #include <android/util/ProtoOutputStream.h> 33 #include <cutils/multiuser.h> 34 #include <utils/Looper.h> 35 #include <utils/Mutex.h> 36 #include <utils/RefBase.h> 37 #include <utils/String16.h> 38 #include <utils/StrongPointer.h> 39 #include <utils/Vector.h> 40 41 #include <IVhalClient.h> 42 #include <VehicleHalTypes.h> 43 44 #include <optional> 45 #include <unordered_map> 46 #include <unordered_set> 47 #include <vector> 48 49 namespace android { 50 namespace automotive { 51 namespace watchdog { 52 53 // Forward declaration for testing use only. 54 namespace internal { 55 56 class WatchdogProcessServicePeer; 57 58 } // namespace internal 59 60 class WatchdogServiceHelperInterface; 61 class PackageInfoResolverInterface; 62 63 class WatchdogProcessServiceInterface : virtual public android::RefBase { 64 public: 65 virtual android::base::Result<void> start() = 0; 66 virtual void terminate() = 0; 67 virtual void onDump(int fd) = 0; 68 virtual void onDumpProto(android::util::ProtoOutputStream& outProto) = 0; 69 virtual void doHealthCheck(int what) = 0; 70 virtual void handleBinderDeath(void* cookie) = 0; 71 virtual ndk::ScopedAStatus registerClient( 72 const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient>& client, 73 aidl::android::automotive::watchdog::TimeoutLength timeout) = 0; 74 virtual ndk::ScopedAStatus unregisterClient( 75 const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient>& 76 client) = 0; 77 virtual ndk::ScopedAStatus registerCarWatchdogService( 78 const ndk::SpAIBinder& binder, 79 const android::sp<WatchdogServiceHelperInterface>& helper) = 0; 80 virtual void unregisterCarWatchdogService(const ndk::SpAIBinder& binder) = 0; 81 virtual ndk::ScopedAStatus registerMonitor( 82 const std::shared_ptr< 83 aidl::android::automotive::watchdog::internal::ICarWatchdogMonitor>& 84 monitor) = 0; 85 virtual void setGarageMode( 86 aidl::android::automotive::watchdog::internal::GarageMode garageMode) = 0; 87 virtual ndk::ScopedAStatus unregisterMonitor( 88 const std::shared_ptr< 89 aidl::android::automotive::watchdog::internal::ICarWatchdogMonitor>& 90 monitor) = 0; 91 virtual ndk::ScopedAStatus tellClientAlive( 92 const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient>& client, 93 int32_t sessionId) = 0; 94 virtual ndk::ScopedAStatus tellCarWatchdogServiceAlive( 95 const std::shared_ptr< 96 aidl::android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& 97 service, 98 const std::vector<aidl::android::automotive::watchdog::internal::ProcessIdentifier>& 99 clientsNotResponding, 100 int32_t sessionId) = 0; 101 virtual ndk::ScopedAStatus tellDumpFinished( 102 const std::shared_ptr< 103 aidl::android::automotive::watchdog::internal::ICarWatchdogMonitor>& monitor, 104 const std::vector<aidl::android::automotive::watchdog::internal::ProcessIdentifier>& 105 processIdentifiers) = 0; 106 virtual void setEnabled(bool isEnabled) = 0; 107 virtual void onUserStateChange(userid_t userId, bool isStarted) = 0; 108 virtual void onAidlVhalPidFetched(int32_t) = 0; 109 }; 110 111 class WatchdogProcessService final : public WatchdogProcessServiceInterface { 112 public: 113 explicit WatchdogProcessService(const android::sp<Looper>& handlerLooper); 114 WatchdogProcessService( 115 const std::function<std::shared_ptr< 116 android::frameworks::automotive::vhal::IVhalClient>()>& tryCreateVhalClientFunc, 117 const std::function<android::sp<android::hidl::manager::V1_0::IServiceManager>()>& 118 tryGetHidlServiceManagerFunc, 119 const std::function<PidStat(pid_t)>& getPidStatForPidFunc, 120 const std::function<uid_t(pid_t)>& getUidForPidFunc, 121 const std::chrono::nanoseconds& vhalPidCachingRetryDelayNs, 122 const sp<Looper>& handlerLooper, 123 const sp<AIBinderDeathRegistrationWrapperInterface>& deathRegistrationWrapper, 124 const std::chrono::milliseconds& vhalHealthCheckIntervalMillis, 125 const std::chrono::milliseconds& vhalHealthCheckDelayMillis); 126 ~WatchdogProcessService(); 127 128 android::base::Result<void> start() override; 129 void terminate() override; 130 void onDump(int fd) override; 131 void onDumpProto(util::ProtoOutputStream& outProto) override; 132 void doHealthCheck(int what) override; 133 void handleBinderDeath(void* cookie) override; 134 void setGarageMode( 135 aidl::android::automotive::watchdog::internal::GarageMode garageMode) override; 136 ndk::ScopedAStatus registerClient( 137 const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient>& client, 138 aidl::android::automotive::watchdog::TimeoutLength timeout) override; 139 ndk::ScopedAStatus unregisterClient( 140 const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient>& client) 141 override; 142 ndk::ScopedAStatus registerCarWatchdogService( 143 const ndk::SpAIBinder& binder, 144 const android::sp<WatchdogServiceHelperInterface>& helper) override; 145 void unregisterCarWatchdogService(const ndk::SpAIBinder& binder) override; 146 ndk::ScopedAStatus registerMonitor( 147 const std::shared_ptr< 148 aidl::android::automotive::watchdog::internal::ICarWatchdogMonitor>& monitor) 149 override; 150 ndk::ScopedAStatus unregisterMonitor( 151 const std::shared_ptr< 152 aidl::android::automotive::watchdog::internal::ICarWatchdogMonitor>& monitor) 153 override; 154 ndk::ScopedAStatus tellClientAlive( 155 const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient>& client, 156 int32_t sessionId) override; 157 ndk::ScopedAStatus tellCarWatchdogServiceAlive( 158 const std::shared_ptr< 159 aidl::android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& 160 service, 161 const std::vector<aidl::android::automotive::watchdog::internal::ProcessIdentifier>& 162 clientsNotResponding, 163 int32_t sessionId) override; 164 ndk::ScopedAStatus tellDumpFinished( 165 const std::shared_ptr< 166 aidl::android::automotive::watchdog::internal::ICarWatchdogMonitor>& monitor, 167 const std::vector<aidl::android::automotive::watchdog::internal::ProcessIdentifier>& 168 processIdentifiers) override; 169 void setEnabled(bool isEnabled) override; 170 void onUserStateChange(userid_t userId, bool isStarted) override; 171 void onAidlVhalPidFetched(int32_t) override; 172 173 private: 174 enum ClientType { 175 Regular, 176 Service, 177 }; 178 179 class ClientInfo { 180 public: ClientInfo(const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient> & client,pid_t pid,uid_t uid,const std::string & processName,uint64_t startTimeMillis,const WatchdogProcessService & service)181 ClientInfo(const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient>& 182 client, 183 pid_t pid, uid_t uid, const std::string& processName, uint64_t startTimeMillis, 184 const WatchdogProcessService& service) : 185 kPid(pid), 186 kUid(uid), 187 kProcessName(processName), 188 kStartTimeMillis(startTimeMillis), 189 kType(ClientType::Regular), 190 kService(service), 191 kClient(client) {} ClientInfo(const android::sp<WatchdogServiceHelperInterface> & helper,const ndk::SpAIBinder & binder,pid_t pid,uid_t uid,const std::string & processName,uint64_t startTimeMillis,const WatchdogProcessService & service)192 ClientInfo(const android::sp<WatchdogServiceHelperInterface>& helper, 193 const ndk::SpAIBinder& binder, pid_t pid, uid_t uid, 194 const std::string& processName, uint64_t startTimeMillis, 195 const WatchdogProcessService& service) : 196 kPid(pid), 197 kUid(uid), 198 kProcessName(processName), 199 kStartTimeMillis(startTimeMillis), 200 kType(ClientType::Service), 201 kService(service), 202 kWatchdogServiceHelper(helper), 203 kWatchdogServiceBinder(binder) {} 204 205 std::string toString() const; 206 userid_t getUserId() const; 207 AIBinder* getAIBinder() const; 208 ndk::ScopedAStatus linkToDeath(AIBinder_DeathRecipient* recipient) const; 209 ndk::ScopedAStatus unlinkToDeath(AIBinder_DeathRecipient* recipient) const; 210 ndk::ScopedAStatus checkIfAlive( 211 aidl::android::automotive::watchdog::TimeoutLength timeout) const; 212 ndk::ScopedAStatus prepareProcessTermination() const; 213 214 const pid_t kPid; 215 const uid_t kUid; 216 const std::string kProcessName; 217 const int64_t kStartTimeMillis; 218 const ClientType kType; 219 const WatchdogProcessService& kService; 220 const std::shared_ptr<aidl::android::automotive::watchdog::ICarWatchdogClient> kClient; 221 const android::sp<WatchdogServiceHelperInterface> kWatchdogServiceHelper; 222 const ndk::SpAIBinder kWatchdogServiceBinder; 223 224 int sessionId; 225 std::string packageName; 226 }; 227 228 struct HeartBeat { 229 int64_t eventTime; 230 int64_t value; 231 }; 232 233 typedef std::unordered_map<int, ClientInfo> PingedClientMap; 234 235 class PropertyChangeListener final : 236 public android::frameworks::automotive::vhal::ISubscriptionCallback { 237 public: PropertyChangeListener(const android::sp<WatchdogProcessService> & service)238 explicit PropertyChangeListener(const android::sp<WatchdogProcessService>& service) : 239 kService(service) {} 240 241 void onPropertyEvent(const std::vector< 242 std::unique_ptr<android::frameworks::automotive::vhal::IHalPropValue>>& 243 values) override; 244 245 void onPropertySetError( 246 const std::vector<android::frameworks::automotive::vhal::HalPropError>& errors) 247 override; 248 249 private: 250 const android::sp<WatchdogProcessService> kService; 251 }; 252 253 class MessageHandlerImpl final : public MessageHandler { 254 public: MessageHandlerImpl(const android::sp<WatchdogProcessService> & service)255 explicit MessageHandlerImpl(const android::sp<WatchdogProcessService>& service) : 256 kService(service) {} 257 258 void handleMessage(const Message& message) override; 259 260 private: 261 const android::sp<WatchdogProcessService> kService; 262 }; 263 264 private: 265 android::base::Result<void> registerClient( 266 const ClientInfo& clientInfo, 267 aidl::android::automotive::watchdog::TimeoutLength timeout); 268 ndk::ScopedAStatus unregisterClientLocked( 269 const std::vector<aidl::android::automotive::watchdog::TimeoutLength>& timeouts, 270 const ndk::SpAIBinder& binder, ClientType clientType); 271 ndk::ScopedAStatus tellClientAliveLocked(const ndk::SpAIBinder& binder, int32_t sessionId); 272 android::base::Result<void> startHealthCheckingLocked( 273 aidl::android::automotive::watchdog::TimeoutLength timeout); 274 android::base::Result<void> dumpAndKillClientsIfNotResponding( 275 aidl::android::automotive::watchdog::TimeoutLength timeout); 276 android::base::Result<void> dumpAndKillAllProcesses( 277 const std::vector<aidl::android::automotive::watchdog::internal::ProcessIdentifier>& 278 processesNotResponding, 279 bool reportToVhal); 280 int32_t getNewSessionId(); 281 android::base::Result<void> updateVhal( 282 const aidl::android::hardware::automotive::vehicle::VehiclePropValue& value); 283 android::base::Result<void> connectToVhal(); 284 void subscribeToVhalHeartBeat(); 285 const sp<WatchdogServiceHelperInterface> getWatchdogServiceHelperLocked(); 286 void cacheVhalProcessIdentifier(); 287 void cacheVhalProcessIdentifierForPid(int32_t pid); 288 android::base::Result<void> requestAidlVhalPid(); 289 void reportWatchdogAliveToVhal(); 290 void reportTerminatedProcessToVhal( 291 const std::vector<aidl::android::automotive::watchdog::internal::ProcessIdentifier>& 292 processesNotResponding); 293 android::base::Result<std::string> readProcCmdLine(int32_t pid); 294 void handleVhalDeath(); 295 void queryVhalProperties(); 296 void updateVhalHeartBeat(int64_t value); 297 void handleVhalHealthCheckTimeout(); 298 void resetVhalInfoLocked(); 299 void terminateVhal(); 300 301 using ClientInfoMap = std::unordered_map<uintptr_t, ClientInfo>; 302 using Processor = std::function<void(ClientInfoMap&, ClientInfoMap::const_iterator)>; 303 bool findClientAndProcessLocked( 304 const std::vector<aidl::android::automotive::watchdog::TimeoutLength>& timeouts, 305 AIBinder* binder, const Processor& processor); 306 bool findClientAndProcessLocked( 307 const std::vector<aidl::android::automotive::watchdog::TimeoutLength>& timeouts, 308 uintptr_t binderPtrId, const Processor& processor); 309 std::chrono::nanoseconds getTimeoutDurationNs( 310 const aidl::android::automotive::watchdog::TimeoutLength& timeout); 311 static int toProtoClientType(ClientType clientType); 312 313 private: 314 const std::function<std::shared_ptr<android::frameworks::automotive::vhal::IVhalClient>()> 315 kTryCreateVhalClientFunc; 316 const std::function<android::sp<android::hidl::manager::V1_0::IServiceManager>()> 317 kTryGetHidlServiceManagerFunc; 318 const std::function<PidStat(pid_t)> kGetPidStatForPidFunc; 319 // Function to fetch UID from the /proc/<pid>/status file of the given PID. 320 // This field is used in tests to stub the function. 321 const std::function<uid_t(pid_t)> kGetUidForPidFunc; 322 const std::chrono::nanoseconds kVhalPidCachingRetryDelayNs; 323 324 android::sp<Looper> mHandlerLooper; 325 android::sp<MessageHandlerImpl> mMessageHandler; 326 ndk::ScopedAIBinder_DeathRecipient mClientBinderDeathRecipient; 327 std::unordered_set<aidl::android::hardware::automotive::vehicle::VehicleProperty> 328 mNotSupportedVhalProperties; 329 std::shared_ptr<PropertyChangeListener> mPropertyChangeListener; 330 // mLastSessionId is accessed only within main thread. No need for mutual-exclusion. 331 int32_t mLastSessionId; 332 bool mServiceStarted; 333 std::chrono::milliseconds mVhalHealthCheckIntervalMillis; 334 std::optional<std::chrono::nanoseconds> mOverriddenClientHealthCheckWindowNs; 335 std::shared_ptr<android::frameworks::automotive::vhal::IVhalClient::OnBinderDiedCallbackFunc> 336 mVhalBinderDiedCallback; 337 android::sp<AIBinderDeathRegistrationWrapperInterface> mDeathRegistrationWrapper; 338 std::shared_ptr<PackageInfoResolverInterface> mPackageInfoResolver; 339 340 android::Mutex mMutex; 341 342 std::unordered_map<aidl::android::automotive::watchdog::TimeoutLength, ClientInfoMap> 343 mClientsByTimeout GUARDED_BY(mMutex); 344 std::unordered_map<aidl::android::automotive::watchdog::TimeoutLength, PingedClientMap> 345 mPingedClients GUARDED_BY(mMutex); 346 std::unordered_set<userid_t> mStoppedUserIds GUARDED_BY(mMutex); 347 std::shared_ptr<aidl::android::automotive::watchdog::internal::ICarWatchdogMonitor> mMonitor 348 GUARDED_BY(mMutex); 349 bool mIsEnabled GUARDED_BY(mMutex); 350 std::shared_ptr<android::frameworks::automotive::vhal::IVhalClient> mVhalService 351 GUARDED_BY(mMutex); 352 std::unique_ptr<android::frameworks::automotive::vhal::ISubscriptionClient> 353 mVhalSubscriptionClient GUARDED_BY(mMutex); 354 std::optional<aidl::android::automotive::watchdog::internal::ProcessIdentifier> 355 mVhalProcessIdentifier GUARDED_BY(mMutex); 356 int32_t mTotalVhalPidCachingAttempts GUARDED_BY(mMutex); 357 HeartBeat mVhalHeartBeat GUARDED_BY(mMutex); 358 aidl::android::automotive::watchdog::internal::GarageMode mCurrentGarageModeState 359 GUARDED_BY(mMutex); 360 361 // For unit tests. 362 friend class internal::WatchdogProcessServicePeer; 363 }; 364 365 } // namespace watchdog 366 } // namespace automotive 367 } // namespace android 368