Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 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 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 17 | #include "fakeservicemanager/FakeServiceManager.h" |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 18 | |
Pawan Wagh | 2811ae2 | 2023-08-22 00:07:03 +0000 | [diff] [blame] | 19 | using android::sp; |
| 20 | using android::FakeServiceManager; |
| 21 | using android::setDefaultServiceManager; |
| 22 | |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 23 | namespace android { |
| 24 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 25 | FakeServiceManager::FakeServiceManager() {} |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 26 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 27 | sp<IBinder> FakeServiceManager::getService( const String16& name) const { |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 28 | // Servicemanager is single-threaded and cannot block. This method exists for legacy reasons. |
| 29 | return checkService(name); |
| 30 | } |
| 31 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 32 | sp<IBinder> FakeServiceManager::checkService( const String16& name) const { |
Steven Moreland | 639ffc4 | 2023-08-01 17:53:56 +0000 | [diff] [blame] | 33 | std::lock_guard<std::mutex> l(mMutex); |
| 34 | |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 35 | auto it = mNameToService.find(name); |
| 36 | if (it == mNameToService.end()) { |
| 37 | return nullptr; |
| 38 | } |
| 39 | return it->second; |
| 40 | } |
| 41 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 42 | status_t FakeServiceManager::addService(const String16& name, const sp<IBinder>& service, |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 43 | bool /*allowIsolated*/, |
| 44 | int /*dumpsysFlags*/) { |
Steven Moreland | 639ffc4 | 2023-08-01 17:53:56 +0000 | [diff] [blame] | 45 | std::lock_guard<std::mutex> l(mMutex); |
| 46 | |
Devin Moore | 5f6ded8 | 2022-11-18 18:36:30 +0000 | [diff] [blame] | 47 | if (service == nullptr) { |
| 48 | return UNEXPECTED_NULL; |
| 49 | } |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 50 | mNameToService[name] = service; |
| 51 | return NO_ERROR; |
| 52 | } |
| 53 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 54 | Vector<String16> FakeServiceManager::listServices(int /*dumpsysFlags*/) { |
Steven Moreland | 639ffc4 | 2023-08-01 17:53:56 +0000 | [diff] [blame] | 55 | std::lock_guard<std::mutex> l(mMutex); |
| 56 | |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 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 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 65 | IBinder* FakeServiceManager::onAsBinder() { |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 66 | return nullptr; |
| 67 | } |
| 68 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 69 | sp<IBinder> FakeServiceManager::waitForService(const String16& name) { |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 70 | return checkService(name); |
| 71 | } |
| 72 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 73 | bool FakeServiceManager::isDeclared(const String16& name) { |
Steven Moreland | 639ffc4 | 2023-08-01 17:53:56 +0000 | [diff] [blame] | 74 | std::lock_guard<std::mutex> l(mMutex); |
| 75 | |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 76 | return mNameToService.find(name) != mNameToService.end(); |
| 77 | } |
| 78 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 79 | Vector<String16> FakeServiceManager::getDeclaredInstances(const String16& name) { |
Steven Moreland | 639ffc4 | 2023-08-01 17:53:56 +0000 | [diff] [blame] | 80 | std::lock_guard<std::mutex> l(mMutex); |
| 81 | |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 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)) { |
Tomasz Wasilczyk | 0bfea2d | 2023-08-11 00:06:51 +0000 | [diff] [blame] | 87 | out.add(String16(registeredName.c_str() + prefix.size())); |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | return out; |
| 91 | } |
| 92 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 93 | std::optional<String16> FakeServiceManager::updatableViaApex(const String16& name) { |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 94 | (void)name; |
| 95 | return std::nullopt; |
| 96 | } |
| 97 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 98 | Vector<String16> FakeServiceManager::getUpdatableNames(const String16& apexName) { |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 99 | (void)apexName; |
| 100 | return {}; |
| 101 | } |
| 102 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 103 | std::optional<IServiceManager::ConnectionInfo> FakeServiceManager::getConnectionInfo( |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 104 | const String16& name) { |
| 105 | (void)name; |
| 106 | return std::nullopt; |
| 107 | } |
| 108 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 109 | status_t FakeServiceManager::registerForNotifications(const String16&, |
Jayant Chowdhary | 3070094 | 2022-01-31 14:12:40 -0800 | [diff] [blame] | 110 | const sp<LocalRegistrationCallback>&) { |
| 111 | return INVALID_OPERATION; |
| 112 | } |
| 113 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 114 | status_t FakeServiceManager::unregisterForNotifications(const String16&, |
Jayant Chowdhary | 3070094 | 2022-01-31 14:12:40 -0800 | [diff] [blame] | 115 | const sp<LocalRegistrationCallback>&) { |
| 116 | return INVALID_OPERATION; |
| 117 | } |
| 118 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 119 | std::vector<IServiceManager::ServiceDebugInfo> FakeServiceManager::getServiceDebugInfo() { |
Jayant Chowdhary | a0a8eb2 | 2022-05-20 03:30:09 +0000 | [diff] [blame] | 120 | std::vector<IServiceManager::ServiceDebugInfo> ret; |
| 121 | return ret; |
| 122 | } |
Devin Moore | 8cc776a | 2022-11-18 18:43:53 +0000 | [diff] [blame] | 123 | |
Devin Moore | 1621b4b | 2023-02-03 22:01:09 +0000 | [diff] [blame] | 124 | void FakeServiceManager::clear() { |
Steven Moreland | 95ac79e | 2023-12-28 22:46:28 +0000 | [diff] [blame] | 125 | std::map<String16, sp<IBinder>> backup; |
Steven Moreland | 639ffc4 | 2023-08-01 17:53:56 +0000 | [diff] [blame] | 126 | |
Steven Moreland | 95ac79e | 2023-12-28 22:46:28 +0000 | [diff] [blame] | 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 |
Devin Moore | 8cc776a | 2022-11-18 18:43:53 +0000 | [diff] [blame] | 138 | } |
Brett Chabot | c9ee330 | 2020-01-31 09:20:28 -0800 | [diff] [blame] | 139 | } // namespace android |
Pawan Wagh | 2811ae2 | 2023-08-22 00:07:03 +0000 | [diff] [blame] | 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 |
| 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 |
| 156 | void clearFakeServiceManager() { |
| 157 | LOG_ALWAYS_FATAL_IF(gFakeServiceManager == nullptr, "Fake Service Manager is not available. Forgot to call setupFakeServiceManager?"); |
| 158 | gFakeServiceManager->clear(); |
| 159 | } |
Steven Moreland | 95ac79e | 2023-12-28 22:46:28 +0000 | [diff] [blame] | 160 | } //extern "C" |