xref: /aosp_15_r20/frameworks/native/cmds/servicemanager/ServiceManager.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
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, os::Service* outService) override;
50     binder::Status addService(const std::string& name, const sp<IBinder>& binder,
51                               bool allowIsolated, int32_t dumpPriority) override;
52     binder::Status listServices(int32_t dumpPriority, std::vector<std::string>* outList) override;
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;
57 
58     binder::Status isDeclared(const std::string& name, bool* outReturn) override;
59     binder::Status getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) override;
60     binder::Status updatableViaApex(const std::string& name,
61                                     std::optional<std::string>* outReturn) override;
62     binder::Status getUpdatableNames(const std::string& apexName,
63                                      std::vector<std::string>* outReturn) override;
64     binder::Status getConnectionInfo(const std::string& name,
65                                      std::optional<ConnectionInfo>* outReturn) override;
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;
69     binder::Status getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) override;
70     void binderDied(const wp<IBinder>& who) override;
71     void handleClientCallbacks();
72 
73     /**
74      *  This API is added for debug purposes. It clears members which hold service and callback
75      * information.
76      */
77     void clear();
78 
79 protected:
80     virtual void tryStartService(const Access::CallingContext& ctx, const std::string& name);
81 
82 private:
83     struct Service {
84         sp<IBinder> binder; // not null
85         bool allowIsolated;
86         int32_t dumpPriority;
87         bool hasClients = false; // notifications sent on true -> false.
88         bool guaranteeClient = false; // forces the client check to true
89         Access::CallingContext ctx;   // process that originally registers this
90 
91         // the number of clients of the service, including servicemanager itself
92         ssize_t getNodeStrongRefCount();
93 
94         ~Service();
95     };
96 
97     using ServiceCallbackMap = std::map<std::string, std::vector<sp<IServiceCallback>>>;
98     using ClientCallbackMap = std::map<std::string, std::vector<sp<IClientCallback>>>;
99     using ServiceMap = std::map<std::string, Service>;
100 
101     // removes a callback from mNameToRegistrationCallback, removing it if the vector is empty
102     // this updates iterator to the next location
103     void removeRegistrationCallback(const wp<IBinder>& who,
104                         ServiceCallbackMap::iterator* it,
105                         bool* found);
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);
109     // Also updates mHasClients (of what the last callback was)
110     void sendClientCallbackNotifications(const std::string& serviceName, bool hasClients,
111                                          const char* context);
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 
116     os::Service tryGetService(const std::string& name, bool startIfNotFound);
117     os::ServiceWithMetadata tryGetBinder(const std::string& name, bool startIfNotFound);
118     binder::Status canAddService(const Access::CallingContext& ctx, const std::string& name,
119                                  std::optional<std::string>* accessor);
120     binder::Status canFindService(const Access::CallingContext& ctx, const std::string& name,
121                                   std::optional<std::string>* accessor);
122 
123     ServiceMap mNameToService;
124     ServiceCallbackMap mNameToRegistrationCallback;
125     ClientCallbackMap mNameToClientCallback;
126 
127     std::unique_ptr<Access> mAccess;
128 };
129 
130 }  // namespace android
131