blob: 46d3ef4af685983cb8a598e4322d2dc401c50e72 [file] [log] [blame]
Eric Jeong34edd982019-12-17 17:26:35 -08001/**
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
Eric Jeong3a4b6352020-08-18 18:06:53 -070017#ifndef CPP_WATCHDOG_SERVER_SRC_WATCHDOGPROCESSSERVICE_H_
18#define CPP_WATCHDOG_SERVER_SRC_WATCHDOGPROCESSSERVICE_H_
Eric Jeong34edd982019-12-17 17:26:35 -080019
Eric Jeong6ff82ef2021-10-20 22:23:14 -070020#include <android-base/chrono_utils.h>
Eric Jeong453287e2020-02-07 16:44:11 -080021#include <android-base/result.h>
Lakshman Annadoraie4054622020-11-10 14:09:42 -080022#include <android/automotive/watchdog/ICarWatchdogClient.h>
23#include <android/automotive/watchdog/internal/ICarWatchdogMonitor.h>
24#include <android/automotive/watchdog/internal/ICarWatchdogServiceForSystem.h>
Lakshman Annadorai967b21e2020-10-30 09:35:29 -070025#include <android/automotive/watchdog/internal/PowerCycle.h>
26#include <android/automotive/watchdog/internal/UserState.h>
Eric Jeong3a4b6352020-08-18 18:06:53 -070027#include <android/hardware/automotive/vehicle/2.0/IVehicle.h>
Lakshman Annadorai0dfeeeb2020-03-13 16:57:12 -070028#include <binder/IBinder.h>
29#include <binder/Status.h>
30#include <cutils/multiuser.h>
Eric Jeong34edd982019-12-17 17:26:35 -080031#include <utils/Looper.h>
32#include <utils/Mutex.h>
Lakshman Annadorai0dfeeeb2020-03-13 16:57:12 -070033#include <utils/String16.h>
34#include <utils/StrongPointer.h>
35#include <utils/Vector.h>
Eric Jeong34edd982019-12-17 17:26:35 -080036
Eric Jeong453287e2020-02-07 16:44:11 -080037#include <unordered_map>
38#include <unordered_set>
Eric Jeong34edd982019-12-17 17:26:35 -080039#include <vector>
40
41namespace android {
42namespace automotive {
43namespace watchdog {
44
Lakshman Annadoraiec4d08a2021-04-30 08:29:10 -070045class IWatchdogServiceHelper;
Lakshman Annadoraid2de7f22020-11-17 16:27:22 -080046
Eric Jeong3a4b6352020-08-18 18:06:53 -070047class WatchdogProcessService : public android::RefBase {
Eric Jeong453287e2020-02-07 16:44:11 -080048public:
Eric Jeong34edd982019-12-17 17:26:35 -080049 explicit WatchdogProcessService(const android::sp<Looper>& handlerLooper);
Lakshman Annadoraid2de7f22020-11-17 16:27:22 -080050 ~WatchdogProcessService() { terminate(); }
Eric Jeong34edd982019-12-17 17:26:35 -080051
Lakshman Annadoraid2de7f22020-11-17 16:27:22 -080052 android::base::Result<void> start();
53 void terminate();
Lakshman Annadorai8fae85c2021-04-16 14:42:19 -070054 virtual android::base::Result<void> dump(int fd,
55 const android::Vector<android::String16>& args);
Lakshman Annadoraid2de7f22020-11-17 16:27:22 -080056 void doHealthCheck(int what);
57
58 virtual android::base::Result<void> registerWatchdogServiceHelper(
Lakshman Annadoraiec4d08a2021-04-30 08:29:10 -070059 const android::sp<IWatchdogServiceHelper>& helper);
Eric Jeong34edd982019-12-17 17:26:35 -080060
Lakshman Annadorai967b21e2020-10-30 09:35:29 -070061 virtual android::binder::Status registerClient(const android::sp<ICarWatchdogClient>& client,
62 TimeoutLength timeout);
Lakshman Annadoraie4054622020-11-10 14:09:42 -080063 virtual android::binder::Status unregisterClient(const android::sp<ICarWatchdogClient>& client);
Lakshman Annadoraid2de7f22020-11-17 16:27:22 -080064 virtual android::binder::Status registerCarWatchdogService(const android::sp<IBinder>& binder);
65 virtual void unregisterCarWatchdogService(const android::sp<IBinder>& binder);
Lakshman Annadorai967b21e2020-10-30 09:35:29 -070066 virtual android::binder::Status registerMonitor(
Lakshman Annadoraie4054622020-11-10 14:09:42 -080067 const android::sp<android::automotive::watchdog::internal::ICarWatchdogMonitor>&
68 monitor);
Lakshman Annadorai967b21e2020-10-30 09:35:29 -070069 virtual android::binder::Status unregisterMonitor(
Lakshman Annadoraie4054622020-11-10 14:09:42 -080070 const android::sp<android::automotive::watchdog::internal::ICarWatchdogMonitor>&
71 monitor);
72 virtual android::binder::Status tellClientAlive(const android::sp<ICarWatchdogClient>& client,
Lakshman Annadorai967b21e2020-10-30 09:35:29 -070073 int32_t sessionId);
Lakshman Annadoraie4054622020-11-10 14:09:42 -080074 virtual android::binder::Status tellCarWatchdogServiceAlive(
75 const android::sp<
76 android::automotive::watchdog::internal::ICarWatchdogServiceForSystem>& service,
Lakshman Annadorai967b21e2020-10-30 09:35:29 -070077 const std::vector<int32_t>& clientsNotResponding, int32_t sessionId);
78 virtual android::binder::Status tellDumpFinished(
79 const android::sp<android::automotive::watchdog::internal::ICarWatchdogMonitor>&
80 monitor,
81 int32_t pid);
Lakshman Annadorai0a0a7872021-06-08 14:55:26 -070082 virtual void setEnabled(bool isEnabled);
Lakshman Annadorai967b21e2020-10-30 09:35:29 -070083 virtual android::binder::Status notifyUserStateChange(
84 userid_t userId, android::automotive::watchdog::internal::UserState state);
Eric Jeong34edd982019-12-17 17:26:35 -080085
Eric Jeong453287e2020-02-07 16:44:11 -080086private:
Eric Jeong34edd982019-12-17 17:26:35 -080087 enum ClientType {
88 Regular,
Lakshman Annadoraid2de7f22020-11-17 16:27:22 -080089 Service,
Eric Jeong34edd982019-12-17 17:26:35 -080090 };
91
92 struct ClientInfo {
Lakshman Annadoraid2de7f22020-11-17 16:27:22 -080093 ClientInfo(const android::sp<ICarWatchdogClient>& client, pid_t pid, userid_t userId) :
Eric Jeong01a28aa2020-03-27 16:43:19 -070094 pid(pid),
95 userId(userId),
Lakshman Annadoraid2de7f22020-11-17 16:27:22 -080096 type(ClientType::Regular),
97 client(client) {}
Lakshman Annadoraiec4d08a2021-04-30 08:29:10 -070098 ClientInfo(const android::sp<IWatchdogServiceHelper>& helper,
Lakshman Annadoraid2de7f22020-11-17 16:27:22 -080099 const android::sp<android::IBinder>& binder, pid_t pid, userid_t userId) :
100 pid(pid),
101 userId(userId),
102 type(ClientType::Service),
103 watchdogServiceHelper(helper),
104 watchdogServiceBinder(binder) {}
Eric Jeong34edd982019-12-17 17:26:35 -0800105
Lakshman Annadoraid2de7f22020-11-17 16:27:22 -0800106 std::string toString() const;
107 status_t linkToDeath(const android::sp<android::IBinder::DeathRecipient>& recipient) const;
108 status_t unlinkToDeath(
109 const android::wp<android::IBinder::DeathRecipient>& recipient) const;
110 android::binder::Status checkIfAlive(TimeoutLength timeout) const;
111 android::binder::Status prepareProcessTermination() const;
112 bool operator!=(const ClientInfo& clientInfo) const {
113 return getBinder() != clientInfo.getBinder() || type != clientInfo.type;
114 }
115 bool matchesBinder(const android::sp<android::IBinder>& binder) const {
116 return binder == getBinder();
117 }
118
Eric Jeong34edd982019-12-17 17:26:35 -0800119 pid_t pid;
Eric Jeong01a28aa2020-03-27 16:43:19 -0700120 userid_t userId;
Eric Jeong27a178f2020-03-24 16:39:39 -0700121 int sessionId;
Lakshman Annadoraid2de7f22020-11-17 16:27:22 -0800122
123 private:
124 android::sp<android::IBinder> getBinder() const;
125
Eric Jeong34edd982019-12-17 17:26:35 -0800126 ClientType type;
Lakshman Annadoraid2de7f22020-11-17 16:27:22 -0800127 android::sp<ICarWatchdogClient> client = nullptr;
Lakshman Annadoraiec4d08a2021-04-30 08:29:10 -0700128 android::sp<IWatchdogServiceHelper> watchdogServiceHelper = nullptr;
Lakshman Annadoraid2de7f22020-11-17 16:27:22 -0800129 android::sp<IBinder> watchdogServiceBinder = nullptr;
Eric Jeong34edd982019-12-17 17:26:35 -0800130 };
131
Eric Jeong2ac089c2020-08-27 17:50:38 -0700132 struct HeartBeat {
133 int64_t eventTime;
134 int64_t value;
135 };
136
Eric Jeong27a178f2020-03-24 16:39:39 -0700137 typedef std::unordered_map<int, ClientInfo> PingedClientMap;
Eric Jeong453287e2020-02-07 16:44:11 -0800138
Eric Jeong3a4b6352020-08-18 18:06:53 -0700139 class BinderDeathRecipient : public android::IBinder::DeathRecipient {
140 public:
141 explicit BinderDeathRecipient(const android::sp<WatchdogProcessService>& service);
142
143 void binderDied(const android::wp<android::IBinder>& who) override;
144
145 private:
146 android::sp<WatchdogProcessService> mService;
147 };
148
149 class HidlDeathRecipient : public android::hardware::hidl_death_recipient {
150 public:
151 explicit HidlDeathRecipient(const android::sp<WatchdogProcessService>& service);
152
153 void serviceDied(uint64_t cookie,
154 const android::wp<android::hidl::base::V1_0::IBase>& who) override;
155
156 private:
157 android::sp<WatchdogProcessService> mService;
158 };
159
Eric Jeong2ac089c2020-08-27 17:50:38 -0700160 class PropertyChangeListener :
161 public android::hardware::automotive::vehicle::V2_0::IVehicleCallback {
162 public:
163 explicit PropertyChangeListener(const android::sp<WatchdogProcessService>& service);
164
165 android::hardware::Return<void> onPropertyEvent(
166 const android::hardware::hidl_vec<
167 android::hardware::automotive::vehicle::V2_0::VehiclePropValue>& propValues)
168 override;
169 android::hardware::Return<void> onPropertySet(
170 const android::hardware::automotive::vehicle::V2_0::VehiclePropValue& propValue)
171 override;
172 android::hardware::Return<void> onPropertySetError(
173 android::hardware::automotive::vehicle::V2_0::StatusCode errorCode, int32_t propId,
174 int32_t areaId) override;
175
176 private:
177 android::sp<WatchdogProcessService> mService;
178 };
179
Eric Jeong34edd982019-12-17 17:26:35 -0800180 class MessageHandlerImpl : public MessageHandler {
Eric Jeong453287e2020-02-07 16:44:11 -0800181 public:
Eric Jeong34edd982019-12-17 17:26:35 -0800182 explicit MessageHandlerImpl(const android::sp<WatchdogProcessService>& service);
183
184 void handleMessage(const Message& message) override;
185
Eric Jeong453287e2020-02-07 16:44:11 -0800186 private:
Eric Jeong34edd982019-12-17 17:26:35 -0800187 android::sp<WatchdogProcessService> mService;
188 };
189
Eric Jeong453287e2020-02-07 16:44:11 -0800190private:
Lakshman Annadoraid2de7f22020-11-17 16:27:22 -0800191 android::binder::Status registerClientLocked(const ClientInfo& clientInfo,
192 TimeoutLength timeout);
Lakshman Annadorai967b21e2020-10-30 09:35:29 -0700193 android::binder::Status unregisterClientLocked(const std::vector<TimeoutLength>& timeouts,
194 android::sp<IBinder> binder,
195 ClientType clientType);
Lakshman Annadoraid2de7f22020-11-17 16:27:22 -0800196 android::binder::Status tellClientAliveLocked(const android::sp<android::IBinder>& binder,
Lakshman Annadorai967b21e2020-10-30 09:35:29 -0700197 int32_t sessionId);
Eric Jeong3a4b6352020-08-18 18:06:53 -0700198 android::base::Result<void> startHealthCheckingLocked(TimeoutLength timeout);
199 android::base::Result<void> dumpAndKillClientsIfNotResponding(TimeoutLength timeout);
200 android::base::Result<void> dumpAndKillAllProcesses(
Eric Jeong2ac089c2020-08-27 17:50:38 -0700201 const std::vector<int32_t>& processesNotResponding, bool reportToVhal);
Eric Jeong34edd982019-12-17 17:26:35 -0800202 int32_t getNewSessionId();
Eric Jeong3a4b6352020-08-18 18:06:53 -0700203 android::base::Result<void> updateVhal(
204 const android::hardware::automotive::vehicle::V2_0::VehiclePropValue& value);
205 android::base::Result<void> connectToVhalLocked();
Eric Jeong2ac089c2020-08-27 17:50:38 -0700206 void subscribeToVhalHeartBeatLocked();
Eric Jeong3a4b6352020-08-18 18:06:53 -0700207 void reportWatchdogAliveToVhal();
208 void reportTerminatedProcessToVhal(const std::vector<int32_t>& processesNotResponding);
209 android::base::Result<std::string> readProcCmdLine(int32_t pid);
210 void handleBinderDeath(const android::wp<android::IBinder>& who);
211 void handleHidlDeath(const android::wp<android::hidl::base::V1_0::IBase>& who);
212 void queryVhalPropertiesLocked();
213 bool isVhalPropertySupportedLocked(
214 android::hardware::automotive::vehicle::V2_0::VehicleProperty propId);
Eric Jeong2ac089c2020-08-27 17:50:38 -0700215 void updateVhalHeartBeat(int64_t value);
216 void checkVhalHealth();
217 void terminateVhal();
Eric Jeong34edd982019-12-17 17:26:35 -0800218
219 using Processor =
Eric Jeong453287e2020-02-07 16:44:11 -0800220 std::function<void(std::vector<ClientInfo>&, std::vector<ClientInfo>::const_iterator)>;
Eric Jeong34edd982019-12-17 17:26:35 -0800221 bool findClientAndProcessLocked(const std::vector<TimeoutLength> timeouts,
Lakshman Annadoraid2de7f22020-11-17 16:27:22 -0800222 const ClientInfo& clientInfo, const Processor& processor);
223 bool findClientAndProcessLocked(const std::vector<TimeoutLength> timeouts,
Eric Jeong3a4b6352020-08-18 18:06:53 -0700224 const android::sp<android::IBinder> binder,
225 const Processor& processor);
Eric Jeong34edd982019-12-17 17:26:35 -0800226
Eric Jeong453287e2020-02-07 16:44:11 -0800227private:
Eric Jeong3a4b6352020-08-18 18:06:53 -0700228 android::sp<Looper> mHandlerLooper;
Eric Jeong34edd982019-12-17 17:26:35 -0800229 android::sp<MessageHandlerImpl> mMessageHandler;
Eric Jeong3a4b6352020-08-18 18:06:53 -0700230 android::Mutex mMutex;
Eric Jeong453287e2020-02-07 16:44:11 -0800231 std::unordered_map<TimeoutLength, std::vector<ClientInfo>> mClients GUARDED_BY(mMutex);
Eric Jeong27a178f2020-03-24 16:39:39 -0700232 std::unordered_map<TimeoutLength, PingedClientMap> mPingedClients GUARDED_BY(mMutex);
Eric Jeong3a4b6352020-08-18 18:06:53 -0700233 std::unordered_set<userid_t> mStoppedUserIds GUARDED_BY(mMutex);
Lakshman Annadorai967b21e2020-10-30 09:35:29 -0700234 android::sp<android::automotive::watchdog::internal::ICarWatchdogMonitor> mMonitor
235 GUARDED_BY(mMutex);
Lakshman Annadorai0a0a7872021-06-08 14:55:26 -0700236 bool mIsEnabled GUARDED_BY(mMutex);
Eric Jeong34edd982019-12-17 17:26:35 -0800237 // mLastSessionId is accessed only within main thread. No need for mutual-exclusion.
238 int32_t mLastSessionId;
Eric Jeong3a4b6352020-08-18 18:06:53 -0700239 bool mServiceStarted;
240 android::sp<android::hardware::automotive::vehicle::V2_0::IVehicle> mVhalService
241 GUARDED_BY(mMutex);
242 android::sp<BinderDeathRecipient> mBinderDeathRecipient;
243 android::sp<HidlDeathRecipient> mHidlDeathRecipient;
244 std::unordered_set<android::hardware::automotive::vehicle::V2_0::VehicleProperty>
245 mNotSupportedVhalProperties;
Eric Jeong2ac089c2020-08-27 17:50:38 -0700246 android::sp<PropertyChangeListener> mPropertyChangeListener;
247 HeartBeat mVhalHeartBeat GUARDED_BY(mMutex);
Eric Jeong6ff82ef2021-10-20 22:23:14 -0700248 std::chrono::milliseconds mVhalHealthCheckWindowMs;
Lakshman Annadoraiec4d08a2021-04-30 08:29:10 -0700249 android::sp<IWatchdogServiceHelper> mWatchdogServiceHelper GUARDED_BY(mMutex);
Eric Jeong34edd982019-12-17 17:26:35 -0800250};
251
252} // namespace watchdog
253} // namespace automotive
254} // namespace android
255
Eric Jeong3a4b6352020-08-18 18:06:53 -0700256#endif // CPP_WATCHDOG_SERVER_SRC_WATCHDOGPROCESSSERVICE_H_