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> 20 #include <android/os/IClientCallback.h> 21 #include <android/os/IServiceCallback.h> 22 23 #if !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__) 24 #include "perfetto/public/te_category_macros.h" 25 #endif // !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__) 26 27 #include "Access.h" 28 29 namespace android { 30 31 using os::ConnectionInfo; 32 using os::IClientCallback; 33 using os::IServiceCallback; 34 using os::ServiceDebugInfo; 35 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 41 class ServiceManager : public os::BnServiceManager, public IBinder::DeathRecipient { 42 public: 43 ServiceManager(std::unique_ptr<Access>&& access); 44 ~ServiceManager(); 45 46 // getService will try to start any services it cannot find 47 binder::Status getService(const std::string& name, sp<IBinder>* outBinder) override; 48 binder::Status getService2(const std::string& name, os::Service* outService) override; 49 binder::Status checkService(const std::string& name, sp<IBinder>* outBinder) override; 50 binder::Status checkService2(const std::string& name, os::Service* outService) override; 51 binder::Status addService(const std::string& name, const sp<IBinder>& binder, 52 bool allowIsolated, int32_t dumpPriority) override; 53 binder::Status listServices(int32_t dumpPriority, std::vector<std::string>* outList) override; 54 binder::Status registerForNotifications(const std::string& name, 55 const sp<IServiceCallback>& callback) override; 56 binder::Status unregisterForNotifications(const std::string& name, 57 const sp<IServiceCallback>& callback) override; 58 59 binder::Status isDeclared(const std::string& name, bool* outReturn) override; 60 binder::Status getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) override; 61 binder::Status updatableViaApex(const std::string& name, 62 std::optional<std::string>* outReturn) override; 63 binder::Status getUpdatableNames(const std::string& apexName, 64 std::vector<std::string>* outReturn) override; 65 binder::Status getConnectionInfo(const std::string& name, 66 std::optional<ConnectionInfo>* outReturn) override; 67 binder::Status registerClientCallback(const std::string& name, const sp<IBinder>& service, 68 const sp<IClientCallback>& cb) override; 69 binder::Status tryUnregisterService(const std::string& name, const sp<IBinder>& binder) override; 70 binder::Status getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) override; 71 void binderDied(const wp<IBinder>& who) override; 72 void handleClientCallbacks(); 73 74 /** 75 * This API is added for debug purposes. It clears members which hold service and callback 76 * information. 77 */ 78 void clear(); 79 80 protected: 81 virtual void tryStartService(const Access::CallingContext& ctx, const std::string& name); 82 83 private: 84 struct Service { 85 sp<IBinder> binder; // not null 86 bool allowIsolated; 87 int32_t dumpPriority; 88 bool hasClients = false; // notifications sent on true -> false. 89 bool guaranteeClient = false; // forces the client check to true 90 Access::CallingContext ctx; // process that originally registers this 91 92 // the number of clients of the service, including servicemanager itself 93 ssize_t getNodeStrongRefCount(); 94 95 ~Service(); 96 }; 97 98 using ServiceCallbackMap = std::map<std::string, std::vector<sp<IServiceCallback>>>; 99 using ClientCallbackMap = std::map<std::string, std::vector<sp<IClientCallback>>>; 100 using ServiceMap = std::map<std::string, Service>; 101 102 // removes a callback from mNameToRegistrationCallback, removing it if the vector is empty 103 // this updates iterator to the next location 104 void removeRegistrationCallback(const wp<IBinder>& who, 105 ServiceCallbackMap::iterator* it, 106 bool* found); 107 // returns whether there are known clients in addition to the count provided 108 bool handleServiceClientCallback(size_t knownClients, const std::string& serviceName, 109 bool isCalledOnInterval); 110 // Also updates mHasClients (of what the last callback was) 111 void sendClientCallbackNotifications(const std::string& serviceName, bool hasClients, 112 const char* context); 113 // removes a callback from mNameToClientCallback, deleting the entry if the vector is empty 114 // this updates the iterator to the next location 115 void removeClientCallback(const wp<IBinder>& who, ClientCallbackMap::iterator* it); 116 117 os::Service tryGetService(const std::string& name, bool startIfNotFound); 118 os::ServiceWithMetadata tryGetBinder(const std::string& name, bool startIfNotFound); 119 binder::Status canAddService(const Access::CallingContext& ctx, const std::string& name, 120 std::optional<std::string>* accessor); 121 binder::Status canFindService(const Access::CallingContext& ctx, const std::string& name, 122 std::optional<std::string>* accessor); 123 124 ServiceMap mNameToService; 125 ServiceCallbackMap mNameToRegistrationCallback; 126 ClientCallbackMap mNameToClientCallback; 127 128 std::unique_ptr<Access> mAccess; 129 }; 130 131 } // namespace android 132