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 #include "fakeservicemanager/FakeServiceManager.h"
18 
19 using android::sp;
20 using android::FakeServiceManager;
21 using android::setDefaultServiceManager;
22 
23 namespace android {
24 
FakeServiceManager()25 FakeServiceManager::FakeServiceManager() {}
26 
getService(const String16 & name) const27 sp<IBinder> FakeServiceManager::getService( const String16& name) const {
28     // Servicemanager is single-threaded and cannot block. This method exists for legacy reasons.
29     return checkService(name);
30 }
31 
checkService(const String16 & name) const32 sp<IBinder> FakeServiceManager::checkService( const String16& name) const {
33     std::lock_guard<std::mutex> l(mMutex);
34 
35     auto it = mNameToService.find(name);
36     if (it == mNameToService.end()) {
37         return nullptr;
38     }
39     return it->second;
40 }
41 
addService(const String16 & name,const sp<IBinder> & service,bool,int)42 status_t FakeServiceManager::addService(const String16& name, const sp<IBinder>& service,
43                                 bool /*allowIsolated*/,
44                                 int /*dumpsysFlags*/) {
45     std::lock_guard<std::mutex> l(mMutex);
46 
47     if (service == nullptr) {
48         return UNEXPECTED_NULL;
49     }
50     mNameToService[name] = service;
51     return NO_ERROR;
52 }
53 
listServices(int)54 Vector<String16> FakeServiceManager::listServices(int /*dumpsysFlags*/) {
55     std::lock_guard<std::mutex> l(mMutex);
56 
57     Vector<String16> services;
58     for (auto const& [name, service] : mNameToService) {
59         (void) service;
60          services.push_back(name);
61     }
62   return services;
63 }
64 
onAsBinder()65 IBinder* FakeServiceManager::onAsBinder() {
66     return nullptr;
67 }
68 
waitForService(const String16 & name)69 sp<IBinder> FakeServiceManager::waitForService(const String16& name) {
70     return checkService(name);
71 }
72 
isDeclared(const String16 & name)73 bool FakeServiceManager::isDeclared(const String16& name) {
74     std::lock_guard<std::mutex> l(mMutex);
75 
76     return mNameToService.find(name) != mNameToService.end();
77 }
78 
getDeclaredInstances(const String16 & name)79 Vector<String16> FakeServiceManager::getDeclaredInstances(const String16& name) {
80     std::lock_guard<std::mutex> l(mMutex);
81 
82     Vector<String16> out;
83     const String16 prefix = name + String16("/");
84     for (const auto& [registeredName, service] : mNameToService) {
85         (void) service;
86         if (registeredName.startsWith(prefix)) {
87             out.add(String16(registeredName.c_str() + prefix.size()));
88         }
89     }
90     return out;
91 }
92 
updatableViaApex(const String16 & name)93 std::optional<String16> FakeServiceManager::updatableViaApex(const String16& name) {
94     (void)name;
95     return std::nullopt;
96 }
97 
getUpdatableNames(const String16 & apexName)98 Vector<String16> FakeServiceManager::getUpdatableNames(const String16& apexName) {
99     (void)apexName;
100     return {};
101 }
102 
getConnectionInfo(const String16 & name)103 std::optional<IServiceManager::ConnectionInfo> FakeServiceManager::getConnectionInfo(
104         const String16& name) {
105     (void)name;
106     return std::nullopt;
107 }
108 
registerForNotifications(const String16 &,const sp<LocalRegistrationCallback> &)109 status_t FakeServiceManager::registerForNotifications(const String16&,
110                                                   const sp<LocalRegistrationCallback>&) {
111     return INVALID_OPERATION;
112 }
113 
unregisterForNotifications(const String16 &,const sp<LocalRegistrationCallback> &)114 status_t FakeServiceManager::unregisterForNotifications(const String16&,
115                                                 const sp<LocalRegistrationCallback>&) {
116     return INVALID_OPERATION;
117 }
118 
getServiceDebugInfo()119 std::vector<IServiceManager::ServiceDebugInfo> FakeServiceManager::getServiceDebugInfo() {
120     std::vector<IServiceManager::ServiceDebugInfo> ret;
121     return ret;
122 }
123 
clear()124 void FakeServiceManager::clear() {
125     std::map<String16, sp<IBinder>> backup;
126 
127     {
128       std::lock_guard<std::mutex> l(mMutex);
129       backup = mNameToService;
130       mNameToService.clear();
131     }
132 
133     // destructors may access FSM, so avoid recursive lock
134     backup.clear(); // explicit
135 
136     // TODO: destructors may have added more services here - may want
137     // to check this or abort
138 }
139 }  // namespace android
140 
141 [[clang::no_destroy]] static sp<FakeServiceManager> gFakeServiceManager;
142 [[clang::no_destroy]] static std::once_flag gSmOnce;
143 
144 extern "C" {
145 
146 // Setup FakeServiceManager to mock dependencies in test using this API for rust backend
setupFakeServiceManager()147 void setupFakeServiceManager() {
148     /* Create a FakeServiceManager instance and add required services */
149     std::call_once(gSmOnce, [&]() {
150         gFakeServiceManager = new FakeServiceManager();
151         android::setDefaultServiceManager(gFakeServiceManager);
152     });
153 }
154 
155 // Clear existing services from Fake SM for rust backend
clearFakeServiceManager()156 void clearFakeServiceManager() {
157     LOG_ALWAYS_FATAL_IF(gFakeServiceManager == nullptr, "Fake Service Manager is not available. Forgot to call setupFakeServiceManager?");
158     gFakeServiceManager->clear();
159 }
160 } //extern "C"
161