• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ServiceManager.h"
20 
21 #include "PackageInfoResolver.h"
22 #include "PerformanceProfiler.h"
23 
24 #include <android/binder_interface_utils.h>
25 
26 namespace android {
27 namespace automotive {
28 namespace watchdog {
29 
30 using ::android::sp;
31 using ::android::base::Error;
32 using ::android::base::Result;
33 using ::ndk::SharedRefBase;
34 
startServices(const sp<Looper> & mainLooper)35 Result<void> ServiceManager::startServices(const sp<Looper>& mainLooper) {
36     if (mWatchdogBinderMediator != nullptr || mWatchdogServiceHelper != nullptr ||
37         mWatchdogProcessService != nullptr || mWatchdogPerfService != nullptr) {
38         return Error(INVALID_OPERATION) << "Cannot start services more than once";
39     }
40     /*
41      * PackageInfoResolver must be initialized first time on the main thread before starting any
42      * other thread as the getInstance method isn't thread safe. Thus initialize PackageInfoResolver
43      * by calling the getInstance method before starting other services as they may access
44      * PackageInfoResolver's instance during initialization.
45      */
46     sp<PackageInfoResolverInterface> packageInfoResolver = PackageInfoResolver::getInstance();
47     if (auto result = startWatchdogProcessService(mainLooper); !result.ok()) {
48         return result;
49     }
50     mWatchdogServiceHelper = sp<WatchdogServiceHelper>::make();
51     if (auto result = mWatchdogServiceHelper->init(mWatchdogProcessService); !result.ok()) {
52         return Error() << "Failed to initialize watchdog service helper: " << result.error();
53     }
54     if (auto result = startWatchdogPerfService(mWatchdogServiceHelper); !result.ok()) {
55         return result;
56     }
57     if (auto result = packageInfoResolver->initWatchdogServiceHelper(mWatchdogServiceHelper);
58         !result.ok()) {
59         return Error() << "Failed to initialize package name resolver: " << result.error();
60     }
61     mIoOveruseMonitor = sp<IoOveruseMonitor>::make(mWatchdogServiceHelper);
62     mWatchdogBinderMediator =
63             SharedRefBase::make<WatchdogBinderMediator>(mWatchdogProcessService,
64                                                         mWatchdogPerfService,
65                                                         mWatchdogServiceHelper, mIoOveruseMonitor);
66     if (auto result = mWatchdogBinderMediator->init(); !result.ok()) {
67         return Error(result.error().code())
68                 << "Failed to initialize watchdog binder mediator: " << result.error();
69     }
70     return {};
71 }
72 
terminateServices()73 void ServiceManager::terminateServices() {
74     if (mWatchdogProcessService != nullptr) {
75         mWatchdogProcessService->terminate();
76         mWatchdogProcessService.clear();
77     }
78     if (mWatchdogPerfService != nullptr) {
79         mWatchdogPerfService->terminate();
80         mWatchdogPerfService.clear();
81     }
82     if (mWatchdogBinderMediator != nullptr) {
83         mWatchdogBinderMediator->terminate();
84         mWatchdogBinderMediator.reset();
85     }
86     if (mWatchdogServiceHelper != nullptr) {
87         mWatchdogServiceHelper->terminate();
88         mWatchdogServiceHelper.clear();
89     }
90     mIoOveruseMonitor.clear();
91     PackageInfoResolver::terminate();
92 }
93 
startWatchdogProcessService(const sp<Looper> & mainLooper)94 Result<void> ServiceManager::startWatchdogProcessService(const sp<Looper>& mainLooper) {
95     mWatchdogProcessService = sp<WatchdogProcessService>::make(mainLooper);
96     if (auto result = mWatchdogProcessService->start(); !result.ok()) {
97         return Error(result.error().code())
98                 << "Failed to start watchdog process monitoring service: " << result.error();
99     }
100     return {};
101 }
102 
startWatchdogPerfService(const sp<WatchdogServiceHelperInterface> & watchdogServiceHelper)103 Result<void> ServiceManager::startWatchdogPerfService(
104         const sp<WatchdogServiceHelperInterface>& watchdogServiceHelper) {
105     mWatchdogPerfService = sp<WatchdogPerfService>::make(watchdogServiceHelper);
106     if (auto result = mWatchdogPerfService->registerDataProcessor(sp<PerformanceProfiler>::make());
107         !result.ok()) {
108         return Error() << "Failed to register performance profiler: " << result.error();
109     }
110     if (auto result = mWatchdogPerfService->start(); !result.ok()) {
111         return Error(result.error().code())
112                 << "Failed to start watchdog performance service: " << result.error();
113     }
114     return {};
115 }
116 
117 }  // namespace watchdog
118 }  // namespace automotive
119 }  // namespace android
120