blob: 964abee6af0501b20b05f563419f391c345c0b9b [file] [log] [blame]
Steven Moreland80e1e6d2019-06-21 12:35:59 -07001/*
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 Spivack9f503a42019-10-22 16:49:19 -070020#include <android/os/IClientCallback.h>
Steven Moreland27cfab02019-08-12 14:34:16 -070021#include <android/os/IServiceCallback.h>
Steven Moreland80e1e6d2019-06-21 12:35:59 -070022
Parth Sane5ade9f12024-05-19 13:02:07 +000023#if !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__)
24#include "perfetto/public/te_category_macros.h"
25#endif // !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__)
26
Steven Moreland80e1e6d2019-06-21 12:35:59 -070027#include "Access.h"
28
29namespace android {
30
Devin Moore5e4c2f12021-09-09 22:36:33 +000031using os::ConnectionInfo;
Jon Spivack9f503a42019-10-22 16:49:19 -070032using os::IClientCallback;
Steven Moreland27cfab02019-08-12 14:34:16 -070033using os::IServiceCallback;
Steven Moreland3ea43272021-01-28 22:49:28 +000034using os::ServiceDebugInfo;
Steven Moreland27cfab02019-08-12 14:34:16 -070035
Parth Sane5ade9f12024-05-19 13:02:07 +000036#if !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__)
37#define PERFETTO_SM_CATEGORIES(C) C(servicemanager, "servicemanager", "Service Manager category")
38PERFETTO_TE_CATEGORIES_DECLARE(PERFETTO_SM_CATEGORIES);
39#endif // !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__)
40
Steven Moreland80e1e6d2019-06-21 12:35:59 -070041class ServiceManager : public os::BnServiceManager, public IBinder::DeathRecipient {
42public:
43 ServiceManager(std::unique_ptr<Access>&& access);
Steven Moreland130242d2019-08-26 17:41:32 -070044 ~ServiceManager();
Steven Moreland80e1e6d2019-06-21 12:35:59 -070045
Jon Spivack0d844302019-07-22 18:40:34 -070046 // getService will try to start any services it cannot find
Alice Wang11da1502024-07-25 12:03:22 +000047 binder::Status getService(const std::string& name, sp<IBinder>* outBinder) override;
48 binder::Status getService2(const std::string& name, os::Service* outService) override;
Alice Wang8578f132024-05-03 09:01:56 +000049 binder::Status checkService(const std::string& name, os::Service* outService) override;
Steven Moreland27cfab02019-08-12 14:34:16 -070050 binder::Status addService(const std::string& name, const sp<IBinder>& binder,
51 bool allowIsolated, int32_t dumpPriority) override;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070052 binder::Status listServices(int32_t dumpPriority, std::vector<std::string>* outList) override;
Steven Moreland27cfab02019-08-12 14:34:16 -070053 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 Moreland80e1e6d2019-06-21 12:35:59 -070057
Jon Spivack9f503a42019-10-22 16:49:19 -070058 binder::Status isDeclared(const std::string& name, bool* outReturn) override;
Steven Moreland2e293aa2020-09-23 00:25:16 +000059 binder::Status getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) override;
Steven Morelandedd4e072021-04-21 00:27:29 +000060 binder::Status updatableViaApex(const std::string& name,
61 std::optional<std::string>* outReturn) override;
Jooyung Han76944fe2022-10-25 17:02:45 +090062 binder::Status getUpdatableNames(const std::string& apexName,
63 std::vector<std::string>* outReturn) override;
Devin Moore5e4c2f12021-09-09 22:36:33 +000064 binder::Status getConnectionInfo(const std::string& name,
65 std::optional<ConnectionInfo>* outReturn) override;
Jon Spivack9f503a42019-10-22 16:49:19 -070066 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 Moreland3ea43272021-01-28 22:49:28 +000069 binder::Status getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) override;
Steven Moreland80e1e6d2019-06-21 12:35:59 -070070 void binderDied(const wp<IBinder>& who) override;
Jon Spivack9f503a42019-10-22 16:49:19 -070071 void handleClientCallbacks();
Steven Moreland80e1e6d2019-06-21 12:35:59 -070072
Pawan Wagh243888e2022-09-20 19:37:35 +000073 /**
74 * This API is added for debug purposes. It clears members which hold service and callback
75 * information.
76 */
77 void clear();
78
Jon Spivack0d844302019-07-22 18:40:34 -070079protected:
Steven Morelandaa33e852023-05-10 16:42:15 +000080 virtual void tryStartService(const Access::CallingContext& ctx, const std::string& name);
Jon Spivack0d844302019-07-22 18:40:34 -070081
Steven Moreland80e1e6d2019-06-21 12:35:59 -070082private:
83 struct Service {
Steven Moreland27cfab02019-08-12 14:34:16 -070084 sp<IBinder> binder; // not null
Steven Moreland80e1e6d2019-06-21 12:35:59 -070085 bool allowIsolated;
86 int32_t dumpPriority;
Jon Spivack9f503a42019-10-22 16:49:19 -070087 bool hasClients = false; // notifications sent on true -> false.
88 bool guaranteeClient = false; // forces the client check to true
Steven Moreland7ee423b2022-09-24 03:52:08 +000089 Access::CallingContext ctx; // process that originally registers this
Jon Spivack9f503a42019-10-22 16:49:19 -070090
91 // the number of clients of the service, including servicemanager itself
92 ssize_t getNodeStrongRefCount();
Steven Morelandb8361902023-02-01 23:18:04 +000093
94 ~Service();
Steven Moreland80e1e6d2019-06-21 12:35:59 -070095 };
96
Jon Spivackf288b1d2019-12-19 17:15:51 -080097 using ServiceCallbackMap = std::map<std::string, std::vector<sp<IServiceCallback>>>;
Jon Spivack9f503a42019-10-22 16:49:19 -070098 using ClientCallbackMap = std::map<std::string, std::vector<sp<IClientCallback>>>;
Steven Moreland27cfab02019-08-12 14:34:16 -070099 using ServiceMap = std::map<std::string, Service>;
100
Jon Spivackf288b1d2019-12-19 17:15:51 -0800101 // removes a callback from mNameToRegistrationCallback, removing it if the vector is empty
Steven Moreland27cfab02019-08-12 14:34:16 -0700102 // this updates iterator to the next location
Jon Spivackf288b1d2019-12-19 17:15:51 -0800103 void removeRegistrationCallback(const wp<IBinder>& who,
104 ServiceCallbackMap::iterator* it,
Steven Moreland27cfab02019-08-12 14:34:16 -0700105 bool* found);
Steven Morelandb8361902023-02-01 23:18:04 +0000106 // 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 Moreland3e083b22023-01-26 00:46:30 +0000109 // Also updates mHasClients (of what the last callback was)
110 void sendClientCallbackNotifications(const std::string& serviceName, bool hasClients,
111 const char* context);
Jon Spivack9f503a42019-10-22 16:49:19 -0700112 // 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 Wang8578f132024-05-03 09:01:56 +0000116 os::Service tryGetService(const std::string& name, bool startIfNotFound);
Parth Sane5e1b7e12024-11-29 10:40:41 +0000117 os::ServiceWithMetadata tryGetBinder(const std::string& name, bool startIfNotFound);
Alice Wangd404e0f2024-07-26 16:18:09 +0000118 binder::Status canAddService(const Access::CallingContext& ctx, const std::string& name,
119 std::optional<std::string>* accessor);
Alice Wang8578f132024-05-03 09:01:56 +0000120 binder::Status canFindService(const Access::CallingContext& ctx, const std::string& name,
121 std::optional<std::string>* accessor);
Steven Moreland27cfab02019-08-12 14:34:16 -0700122
Steven Moreland27cfab02019-08-12 14:34:16 -0700123 ServiceMap mNameToService;
Jon Spivackf288b1d2019-12-19 17:15:51 -0800124 ServiceCallbackMap mNameToRegistrationCallback;
Jon Spivack9f503a42019-10-22 16:49:19 -0700125 ClientCallbackMap mNameToClientCallback;
Steven Moreland27cfab02019-08-12 14:34:16 -0700126
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700127 std::unique_ptr<Access> mAccess;
128};
129
130} // namespace android