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 #include "Access.h"
24 
25 namespace android {
26 
27 using os::IClientCallback;
28 using os::IServiceCallback;
29 using os::ServiceDebugInfo;
30 
31 class ServiceManager : public os::BnServiceManager, public IBinder::DeathRecipient {
32 public:
33     ServiceManager(std::unique_ptr<Access>&& access);
34     ~ServiceManager();
35 
36     // getService will try to start any services it cannot find
37     binder::Status getService(const std::string& name, sp<IBinder>* outBinder) override;
38     binder::Status checkService(const std::string& name, sp<IBinder>* outBinder) override;
39     binder::Status addService(const std::string& name, const sp<IBinder>& binder,
40                               bool allowIsolated, int32_t dumpPriority) override;
41     binder::Status listServices(int32_t dumpPriority, std::vector<std::string>* outList) override;
42     binder::Status registerForNotifications(const std::string& name,
43                                             const sp<IServiceCallback>& callback) override;
44     binder::Status unregisterForNotifications(const std::string& name,
45                                               const sp<IServiceCallback>& callback) override;
46 
47     binder::Status isDeclared(const std::string& name, bool* outReturn) override;
48     binder::Status getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) override;
49     binder::Status updatableViaApex(const std::string& name,
50                                     std::optional<std::string>* outReturn) override;
51     binder::Status registerClientCallback(const std::string& name, const sp<IBinder>& service,
52                                           const sp<IClientCallback>& cb) override;
53     binder::Status tryUnregisterService(const std::string& name, const sp<IBinder>& binder) override;
54     binder::Status getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) override;
55     void binderDied(const wp<IBinder>& who) override;
56     void handleClientCallbacks();
57 
58 protected:
59     virtual void tryStartService(const std::string& name);
60 
61 private:
62     struct Service {
63         sp<IBinder> binder; // not null
64         bool allowIsolated;
65         int32_t dumpPriority;
66         bool hasClients = false; // notifications sent on true -> false.
67         bool guaranteeClient = false; // forces the client check to true
68         pid_t debugPid = 0; // the process in which this service runs
69 
70         // the number of clients of the service, including servicemanager itself
71         ssize_t getNodeStrongRefCount();
72     };
73 
74     using ServiceCallbackMap = std::map<std::string, std::vector<sp<IServiceCallback>>>;
75     using ClientCallbackMap = std::map<std::string, std::vector<sp<IClientCallback>>>;
76     using ServiceMap = std::map<std::string, Service>;
77 
78     // removes a callback from mNameToRegistrationCallback, removing it if the vector is empty
79     // this updates iterator to the next location
80     void removeRegistrationCallback(const wp<IBinder>& who,
81                         ServiceCallbackMap::iterator* it,
82                         bool* found);
83     ssize_t handleServiceClientCallback(const std::string& serviceName, bool isCalledOnInterval);
84      // Also updates mHasClients (of what the last callback was)
85     void sendClientCallbackNotifications(const std::string& serviceName, bool hasClients);
86     // removes a callback from mNameToClientCallback, deleting the entry if the vector is empty
87     // this updates the iterator to the next location
88     void removeClientCallback(const wp<IBinder>& who, ClientCallbackMap::iterator* it);
89 
90     sp<IBinder> tryGetService(const std::string& name, bool startIfNotFound);
91 
92     ServiceMap mNameToService;
93     ServiceCallbackMap mNameToRegistrationCallback;
94     ClientCallbackMap mNameToClientCallback;
95 
96     std::unique_ptr<Access> mAccess;
97 };
98 
99 }  // namespace android
100