xref: /aosp_15_r20/frameworks/native/cmds/servicemanager/ServiceManager.h (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2019 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #pragma once
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <android/os/BnServiceManager.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <android/os/IClientCallback.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <android/os/IServiceCallback.h>
22*38e8c45fSAndroid Build Coastguard Worker 
23*38e8c45fSAndroid Build Coastguard Worker #if !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__)
24*38e8c45fSAndroid Build Coastguard Worker #include "perfetto/public/te_category_macros.h"
25*38e8c45fSAndroid Build Coastguard Worker #endif // !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__)
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker #include "Access.h"
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker namespace android {
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker using os::ConnectionInfo;
32*38e8c45fSAndroid Build Coastguard Worker using os::IClientCallback;
33*38e8c45fSAndroid Build Coastguard Worker using os::IServiceCallback;
34*38e8c45fSAndroid Build Coastguard Worker using os::ServiceDebugInfo;
35*38e8c45fSAndroid Build Coastguard Worker 
36*38e8c45fSAndroid Build Coastguard Worker #if !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__)
37*38e8c45fSAndroid Build Coastguard Worker #define PERFETTO_SM_CATEGORIES(C) C(servicemanager, "servicemanager", "Service Manager category")
38*38e8c45fSAndroid Build Coastguard Worker PERFETTO_TE_CATEGORIES_DECLARE(PERFETTO_SM_CATEGORIES);
39*38e8c45fSAndroid Build Coastguard Worker #endif // !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__)
40*38e8c45fSAndroid Build Coastguard Worker 
41*38e8c45fSAndroid Build Coastguard Worker class ServiceManager : public os::BnServiceManager, public IBinder::DeathRecipient {
42*38e8c45fSAndroid Build Coastguard Worker public:
43*38e8c45fSAndroid Build Coastguard Worker     ServiceManager(std::unique_ptr<Access>&& access);
44*38e8c45fSAndroid Build Coastguard Worker     ~ServiceManager();
45*38e8c45fSAndroid Build Coastguard Worker 
46*38e8c45fSAndroid Build Coastguard Worker     // getService will try to start any services it cannot find
47*38e8c45fSAndroid Build Coastguard Worker     binder::Status getService(const std::string& name, sp<IBinder>* outBinder) override;
48*38e8c45fSAndroid Build Coastguard Worker     binder::Status getService2(const std::string& name, os::Service* outService) override;
49*38e8c45fSAndroid Build Coastguard Worker     binder::Status checkService(const std::string& name, os::Service* outService) override;
50*38e8c45fSAndroid Build Coastguard Worker     binder::Status addService(const std::string& name, const sp<IBinder>& binder,
51*38e8c45fSAndroid Build Coastguard Worker                               bool allowIsolated, int32_t dumpPriority) override;
52*38e8c45fSAndroid Build Coastguard Worker     binder::Status listServices(int32_t dumpPriority, std::vector<std::string>* outList) override;
53*38e8c45fSAndroid Build Coastguard Worker     binder::Status registerForNotifications(const std::string& name,
54*38e8c45fSAndroid Build Coastguard Worker                                             const sp<IServiceCallback>& callback) override;
55*38e8c45fSAndroid Build Coastguard Worker     binder::Status unregisterForNotifications(const std::string& name,
56*38e8c45fSAndroid Build Coastguard Worker                                               const sp<IServiceCallback>& callback) override;
57*38e8c45fSAndroid Build Coastguard Worker 
58*38e8c45fSAndroid Build Coastguard Worker     binder::Status isDeclared(const std::string& name, bool* outReturn) override;
59*38e8c45fSAndroid Build Coastguard Worker     binder::Status getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) override;
60*38e8c45fSAndroid Build Coastguard Worker     binder::Status updatableViaApex(const std::string& name,
61*38e8c45fSAndroid Build Coastguard Worker                                     std::optional<std::string>* outReturn) override;
62*38e8c45fSAndroid Build Coastguard Worker     binder::Status getUpdatableNames(const std::string& apexName,
63*38e8c45fSAndroid Build Coastguard Worker                                      std::vector<std::string>* outReturn) override;
64*38e8c45fSAndroid Build Coastguard Worker     binder::Status getConnectionInfo(const std::string& name,
65*38e8c45fSAndroid Build Coastguard Worker                                      std::optional<ConnectionInfo>* outReturn) override;
66*38e8c45fSAndroid Build Coastguard Worker     binder::Status registerClientCallback(const std::string& name, const sp<IBinder>& service,
67*38e8c45fSAndroid Build Coastguard Worker                                           const sp<IClientCallback>& cb) override;
68*38e8c45fSAndroid Build Coastguard Worker     binder::Status tryUnregisterService(const std::string& name, const sp<IBinder>& binder) override;
69*38e8c45fSAndroid Build Coastguard Worker     binder::Status getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) override;
70*38e8c45fSAndroid Build Coastguard Worker     void binderDied(const wp<IBinder>& who) override;
71*38e8c45fSAndroid Build Coastguard Worker     void handleClientCallbacks();
72*38e8c45fSAndroid Build Coastguard Worker 
73*38e8c45fSAndroid Build Coastguard Worker     /**
74*38e8c45fSAndroid Build Coastguard Worker      *  This API is added for debug purposes. It clears members which hold service and callback
75*38e8c45fSAndroid Build Coastguard Worker      * information.
76*38e8c45fSAndroid Build Coastguard Worker      */
77*38e8c45fSAndroid Build Coastguard Worker     void clear();
78*38e8c45fSAndroid Build Coastguard Worker 
79*38e8c45fSAndroid Build Coastguard Worker protected:
80*38e8c45fSAndroid Build Coastguard Worker     virtual void tryStartService(const Access::CallingContext& ctx, const std::string& name);
81*38e8c45fSAndroid Build Coastguard Worker 
82*38e8c45fSAndroid Build Coastguard Worker private:
83*38e8c45fSAndroid Build Coastguard Worker     struct Service {
84*38e8c45fSAndroid Build Coastguard Worker         sp<IBinder> binder; // not null
85*38e8c45fSAndroid Build Coastguard Worker         bool allowIsolated;
86*38e8c45fSAndroid Build Coastguard Worker         int32_t dumpPriority;
87*38e8c45fSAndroid Build Coastguard Worker         bool hasClients = false; // notifications sent on true -> false.
88*38e8c45fSAndroid Build Coastguard Worker         bool guaranteeClient = false; // forces the client check to true
89*38e8c45fSAndroid Build Coastguard Worker         Access::CallingContext ctx;   // process that originally registers this
90*38e8c45fSAndroid Build Coastguard Worker 
91*38e8c45fSAndroid Build Coastguard Worker         // the number of clients of the service, including servicemanager itself
92*38e8c45fSAndroid Build Coastguard Worker         ssize_t getNodeStrongRefCount();
93*38e8c45fSAndroid Build Coastguard Worker 
94*38e8c45fSAndroid Build Coastguard Worker         ~Service();
95*38e8c45fSAndroid Build Coastguard Worker     };
96*38e8c45fSAndroid Build Coastguard Worker 
97*38e8c45fSAndroid Build Coastguard Worker     using ServiceCallbackMap = std::map<std::string, std::vector<sp<IServiceCallback>>>;
98*38e8c45fSAndroid Build Coastguard Worker     using ClientCallbackMap = std::map<std::string, std::vector<sp<IClientCallback>>>;
99*38e8c45fSAndroid Build Coastguard Worker     using ServiceMap = std::map<std::string, Service>;
100*38e8c45fSAndroid Build Coastguard Worker 
101*38e8c45fSAndroid Build Coastguard Worker     // removes a callback from mNameToRegistrationCallback, removing it if the vector is empty
102*38e8c45fSAndroid Build Coastguard Worker     // this updates iterator to the next location
103*38e8c45fSAndroid Build Coastguard Worker     void removeRegistrationCallback(const wp<IBinder>& who,
104*38e8c45fSAndroid Build Coastguard Worker                         ServiceCallbackMap::iterator* it,
105*38e8c45fSAndroid Build Coastguard Worker                         bool* found);
106*38e8c45fSAndroid Build Coastguard Worker     // returns whether there are known clients in addition to the count provided
107*38e8c45fSAndroid Build Coastguard Worker     bool handleServiceClientCallback(size_t knownClients, const std::string& serviceName,
108*38e8c45fSAndroid Build Coastguard Worker                                      bool isCalledOnInterval);
109*38e8c45fSAndroid Build Coastguard Worker     // Also updates mHasClients (of what the last callback was)
110*38e8c45fSAndroid Build Coastguard Worker     void sendClientCallbackNotifications(const std::string& serviceName, bool hasClients,
111*38e8c45fSAndroid Build Coastguard Worker                                          const char* context);
112*38e8c45fSAndroid Build Coastguard Worker     // removes a callback from mNameToClientCallback, deleting the entry if the vector is empty
113*38e8c45fSAndroid Build Coastguard Worker     // this updates the iterator to the next location
114*38e8c45fSAndroid Build Coastguard Worker     void removeClientCallback(const wp<IBinder>& who, ClientCallbackMap::iterator* it);
115*38e8c45fSAndroid Build Coastguard Worker 
116*38e8c45fSAndroid Build Coastguard Worker     os::Service tryGetService(const std::string& name, bool startIfNotFound);
117*38e8c45fSAndroid Build Coastguard Worker     os::ServiceWithMetadata tryGetBinder(const std::string& name, bool startIfNotFound);
118*38e8c45fSAndroid Build Coastguard Worker     binder::Status canAddService(const Access::CallingContext& ctx, const std::string& name,
119*38e8c45fSAndroid Build Coastguard Worker                                  std::optional<std::string>* accessor);
120*38e8c45fSAndroid Build Coastguard Worker     binder::Status canFindService(const Access::CallingContext& ctx, const std::string& name,
121*38e8c45fSAndroid Build Coastguard Worker                                   std::optional<std::string>* accessor);
122*38e8c45fSAndroid Build Coastguard Worker 
123*38e8c45fSAndroid Build Coastguard Worker     ServiceMap mNameToService;
124*38e8c45fSAndroid Build Coastguard Worker     ServiceCallbackMap mNameToRegistrationCallback;
125*38e8c45fSAndroid Build Coastguard Worker     ClientCallbackMap mNameToClientCallback;
126*38e8c45fSAndroid Build Coastguard Worker 
127*38e8c45fSAndroid Build Coastguard Worker     std::unique_ptr<Access> mAccess;
128*38e8c45fSAndroid Build Coastguard Worker };
129*38e8c45fSAndroid Build Coastguard Worker 
130*38e8c45fSAndroid Build Coastguard Worker }  // namespace android
131