Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 <android/os/BnServiceManager.h> |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 20 | #include <android/os/IClientCallback.h> |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 21 | #include <android/os/IServiceCallback.h> |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 22 | |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 23 | #if !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__) |
| 24 | #include "perfetto/public/te_category_macros.h" |
| 25 | #endif // !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__) |
| 26 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 27 | #include "Access.h" |
| 28 | |
| 29 | namespace android { |
| 30 | |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 31 | using os::ConnectionInfo; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 32 | using os::IClientCallback; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 33 | using os::IServiceCallback; |
Steven Moreland | 3ea4327 | 2021-01-28 22:49:28 +0000 | [diff] [blame] | 34 | using os::ServiceDebugInfo; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 35 | |
Parth Sane | 5ade9f1 | 2024-05-19 13:02:07 +0000 | [diff] [blame] | 36 | #if !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__) |
| 37 | #define PERFETTO_SM_CATEGORIES(C) C(servicemanager, "servicemanager", "Service Manager category") |
| 38 | PERFETTO_TE_CATEGORIES_DECLARE(PERFETTO_SM_CATEGORIES); |
| 39 | #endif // !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__) |
| 40 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 41 | class ServiceManager : public os::BnServiceManager, public IBinder::DeathRecipient { |
| 42 | public: |
| 43 | ServiceManager(std::unique_ptr<Access>&& access); |
Steven Moreland | 130242d | 2019-08-26 17:41:32 -0700 | [diff] [blame] | 44 | ~ServiceManager(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 45 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 46 | // getService will try to start any services it cannot find |
Alice Wang | 11da150 | 2024-07-25 12:03:22 +0000 | [diff] [blame] | 47 | binder::Status getService(const std::string& name, sp<IBinder>* outBinder) override; |
| 48 | binder::Status getService2(const std::string& name, os::Service* outService) override; |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 49 | binder::Status checkService(const std::string& name, os::Service* outService) override; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 50 | binder::Status addService(const std::string& name, const sp<IBinder>& binder, |
| 51 | bool allowIsolated, int32_t dumpPriority) override; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 52 | binder::Status listServices(int32_t dumpPriority, std::vector<std::string>* outList) override; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 53 | binder::Status registerForNotifications(const std::string& name, |
| 54 | const sp<IServiceCallback>& callback) override; |
| 55 | binder::Status unregisterForNotifications(const std::string& name, |
| 56 | const sp<IServiceCallback>& callback) override; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 57 | |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 58 | binder::Status isDeclared(const std::string& name, bool* outReturn) override; |
Steven Moreland | 2e293aa | 2020-09-23 00:25:16 +0000 | [diff] [blame] | 59 | binder::Status getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) override; |
Steven Moreland | edd4e07 | 2021-04-21 00:27:29 +0000 | [diff] [blame] | 60 | binder::Status updatableViaApex(const std::string& name, |
| 61 | std::optional<std::string>* outReturn) override; |
Jooyung Han | 76944fe | 2022-10-25 17:02:45 +0900 | [diff] [blame] | 62 | binder::Status getUpdatableNames(const std::string& apexName, |
| 63 | std::vector<std::string>* outReturn) override; |
Devin Moore | 5e4c2f1 | 2021-09-09 22:36:33 +0000 | [diff] [blame] | 64 | binder::Status getConnectionInfo(const std::string& name, |
| 65 | std::optional<ConnectionInfo>* outReturn) override; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 66 | binder::Status registerClientCallback(const std::string& name, const sp<IBinder>& service, |
| 67 | const sp<IClientCallback>& cb) override; |
| 68 | binder::Status tryUnregisterService(const std::string& name, const sp<IBinder>& binder) override; |
Steven Moreland | 3ea4327 | 2021-01-28 22:49:28 +0000 | [diff] [blame] | 69 | binder::Status getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) override; |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 70 | void binderDied(const wp<IBinder>& who) override; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 71 | void handleClientCallbacks(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 72 | |
Pawan Wagh | 243888e | 2022-09-20 19:37:35 +0000 | [diff] [blame] | 73 | /** |
| 74 | * This API is added for debug purposes. It clears members which hold service and callback |
| 75 | * information. |
| 76 | */ |
| 77 | void clear(); |
| 78 | |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 79 | protected: |
Steven Moreland | aa33e85 | 2023-05-10 16:42:15 +0000 | [diff] [blame] | 80 | virtual void tryStartService(const Access::CallingContext& ctx, const std::string& name); |
Jon Spivack | 0d84430 | 2019-07-22 18:40:34 -0700 | [diff] [blame] | 81 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 82 | private: |
| 83 | struct Service { |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 84 | sp<IBinder> binder; // not null |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 85 | bool allowIsolated; |
| 86 | int32_t dumpPriority; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 87 | bool hasClients = false; // notifications sent on true -> false. |
| 88 | bool guaranteeClient = false; // forces the client check to true |
Steven Moreland | 7ee423b | 2022-09-24 03:52:08 +0000 | [diff] [blame] | 89 | Access::CallingContext ctx; // process that originally registers this |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 90 | |
| 91 | // the number of clients of the service, including servicemanager itself |
| 92 | ssize_t getNodeStrongRefCount(); |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 93 | |
| 94 | ~Service(); |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 97 | using ServiceCallbackMap = std::map<std::string, std::vector<sp<IServiceCallback>>>; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 98 | using ClientCallbackMap = std::map<std::string, std::vector<sp<IClientCallback>>>; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 99 | using ServiceMap = std::map<std::string, Service>; |
| 100 | |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 101 | // removes a callback from mNameToRegistrationCallback, removing it if the vector is empty |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 102 | // this updates iterator to the next location |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 103 | void removeRegistrationCallback(const wp<IBinder>& who, |
| 104 | ServiceCallbackMap::iterator* it, |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 105 | bool* found); |
Steven Moreland | b836190 | 2023-02-01 23:18:04 +0000 | [diff] [blame] | 106 | // returns whether there are known clients in addition to the count provided |
| 107 | bool handleServiceClientCallback(size_t knownClients, const std::string& serviceName, |
| 108 | bool isCalledOnInterval); |
Steven Moreland | 3e083b2 | 2023-01-26 00:46:30 +0000 | [diff] [blame] | 109 | // Also updates mHasClients (of what the last callback was) |
| 110 | void sendClientCallbackNotifications(const std::string& serviceName, bool hasClients, |
| 111 | const char* context); |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 112 | // removes a callback from mNameToClientCallback, deleting the entry if the vector is empty |
| 113 | // this updates the iterator to the next location |
| 114 | void removeClientCallback(const wp<IBinder>& who, ClientCallbackMap::iterator* it); |
| 115 | |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 116 | os::Service tryGetService(const std::string& name, bool startIfNotFound); |
Parth Sane | 5e1b7e1 | 2024-11-29 10:40:41 +0000 | [diff] [blame] | 117 | os::ServiceWithMetadata tryGetBinder(const std::string& name, bool startIfNotFound); |
Alice Wang | d404e0f | 2024-07-26 16:18:09 +0000 | [diff] [blame] | 118 | binder::Status canAddService(const Access::CallingContext& ctx, const std::string& name, |
| 119 | std::optional<std::string>* accessor); |
Alice Wang | 8578f13 | 2024-05-03 09:01:56 +0000 | [diff] [blame] | 120 | binder::Status canFindService(const Access::CallingContext& ctx, const std::string& name, |
| 121 | std::optional<std::string>* accessor); |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 122 | |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 123 | ServiceMap mNameToService; |
Jon Spivack | f288b1d | 2019-12-19 17:15:51 -0800 | [diff] [blame] | 124 | ServiceCallbackMap mNameToRegistrationCallback; |
Jon Spivack | 9f503a4 | 2019-10-22 16:49:19 -0700 | [diff] [blame] | 125 | ClientCallbackMap mNameToClientCallback; |
Steven Moreland | 27cfab0 | 2019-08-12 14:34:16 -0700 | [diff] [blame] | 126 | |
Steven Moreland | 80e1e6d | 2019-06-21 12:35:59 -0700 | [diff] [blame] | 127 | std::unique_ptr<Access> mAccess; |
| 128 | }; |
| 129 | |
| 130 | } // namespace android |