xref: /aosp_15_r20/frameworks/native/cmds/servicemanager/ServiceManager.cpp (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 #include "ServiceManager.h"
18*38e8c45fSAndroid Build Coastguard Worker 
19*38e8c45fSAndroid Build Coastguard Worker #include <android-base/logging.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <android-base/properties.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <android-base/scopeguard.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <android-base/strings.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <binder/BpBinder.h>
24*38e8c45fSAndroid Build Coastguard Worker #include <binder/IPCThreadState.h>
25*38e8c45fSAndroid Build Coastguard Worker #include <binder/ProcessState.h>
26*38e8c45fSAndroid Build Coastguard Worker #include <binder/Stability.h>
27*38e8c45fSAndroid Build Coastguard Worker #include <cutils/android_filesystem_config.h>
28*38e8c45fSAndroid Build Coastguard Worker #include <cutils/multiuser.h>
29*38e8c45fSAndroid Build Coastguard Worker #include <thread>
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker #if !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__)
32*38e8c45fSAndroid Build Coastguard Worker #include "perfetto/public/protos/trace/android/android_track_event.pzc.h"
33*38e8c45fSAndroid Build Coastguard Worker #include "perfetto/public/te_category_macros.h"
34*38e8c45fSAndroid Build Coastguard Worker #include "perfetto/public/te_macros.h"
35*38e8c45fSAndroid Build Coastguard Worker #endif // !defined(VENDORSERVICEMANAGER) && !defined(__ANDROID_RECOVERY__)
36*38e8c45fSAndroid Build Coastguard Worker 
37*38e8c45fSAndroid Build Coastguard Worker #ifndef VENDORSERVICEMANAGER
38*38e8c45fSAndroid Build Coastguard Worker #include <vintf/VintfObject.h>
39*38e8c45fSAndroid Build Coastguard Worker #ifdef __ANDROID_RECOVERY__
40*38e8c45fSAndroid Build Coastguard Worker #include <vintf/VintfObjectRecovery.h>
41*38e8c45fSAndroid Build Coastguard Worker #endif // __ANDROID_RECOVERY__
42*38e8c45fSAndroid Build Coastguard Worker #include <vintf/constants.h>
43*38e8c45fSAndroid Build Coastguard Worker #endif  // !VENDORSERVICEMANAGER
44*38e8c45fSAndroid Build Coastguard Worker 
45*38e8c45fSAndroid Build Coastguard Worker #include "NameUtil.h"
46*38e8c45fSAndroid Build Coastguard Worker 
47*38e8c45fSAndroid Build Coastguard Worker using ::android::binder::Status;
48*38e8c45fSAndroid Build Coastguard Worker using ::android::internal::Stability;
49*38e8c45fSAndroid Build Coastguard Worker 
50*38e8c45fSAndroid Build Coastguard Worker namespace android {
51*38e8c45fSAndroid Build Coastguard Worker 
52*38e8c45fSAndroid Build Coastguard Worker #if defined(VENDORSERVICEMANAGER) || defined(__ANDROID_RECOVERY__)
53*38e8c45fSAndroid Build Coastguard Worker #define SM_PERFETTO_TRACE_FUNC(...)
54*38e8c45fSAndroid Build Coastguard Worker #else
55*38e8c45fSAndroid Build Coastguard Worker 
56*38e8c45fSAndroid Build Coastguard Worker PERFETTO_TE_CATEGORIES_DEFINE(PERFETTO_SM_CATEGORIES);
57*38e8c45fSAndroid Build Coastguard Worker 
58*38e8c45fSAndroid Build Coastguard Worker #define SM_PERFETTO_TRACE_FUNC(...) \
59*38e8c45fSAndroid Build Coastguard Worker     PERFETTO_TE_SCOPED(servicemanager, PERFETTO_TE_SLICE_BEGIN(__func__) __VA_OPT__(, ) __VA_ARGS__)
60*38e8c45fSAndroid Build Coastguard Worker 
61*38e8c45fSAndroid Build Coastguard Worker constexpr uint32_t kProtoServiceName =
62*38e8c45fSAndroid Build Coastguard Worker         perfetto_protos_AndroidTrackEvent_binder_service_name_field_number;
63*38e8c45fSAndroid Build Coastguard Worker constexpr uint32_t kProtoInterfaceName =
64*38e8c45fSAndroid Build Coastguard Worker         perfetto_protos_AndroidTrackEvent_binder_interface_name_field_number;
65*38e8c45fSAndroid Build Coastguard Worker constexpr uint32_t kProtoApexName = perfetto_protos_AndroidTrackEvent_apex_name_field_number;
66*38e8c45fSAndroid Build Coastguard Worker 
67*38e8c45fSAndroid Build Coastguard Worker #endif // !(defined(VENDORSERVICEMANAGER) || defined(__ANDROID_RECOVERY__))
68*38e8c45fSAndroid Build Coastguard Worker 
is_multiuser_uid_isolated(uid_t uid)69*38e8c45fSAndroid Build Coastguard Worker bool is_multiuser_uid_isolated(uid_t uid) {
70*38e8c45fSAndroid Build Coastguard Worker     uid_t appid = multiuser_get_app_id(uid);
71*38e8c45fSAndroid Build Coastguard Worker     return appid >= AID_ISOLATED_START && appid <= AID_ISOLATED_END;
72*38e8c45fSAndroid Build Coastguard Worker }
73*38e8c45fSAndroid Build Coastguard Worker 
74*38e8c45fSAndroid Build Coastguard Worker #ifndef VENDORSERVICEMANAGER
75*38e8c45fSAndroid Build Coastguard Worker 
76*38e8c45fSAndroid Build Coastguard Worker struct ManifestWithDescription {
77*38e8c45fSAndroid Build Coastguard Worker     std::shared_ptr<const vintf::HalManifest> manifest;
78*38e8c45fSAndroid Build Coastguard Worker     const char* description;
79*38e8c45fSAndroid Build Coastguard Worker };
GetManifestsWithDescription()80*38e8c45fSAndroid Build Coastguard Worker static std::vector<ManifestWithDescription> GetManifestsWithDescription() {
81*38e8c45fSAndroid Build Coastguard Worker #ifdef __ANDROID_RECOVERY__
82*38e8c45fSAndroid Build Coastguard Worker     auto vintfObject = vintf::VintfObjectRecovery::GetInstance();
83*38e8c45fSAndroid Build Coastguard Worker     if (vintfObject == nullptr) {
84*38e8c45fSAndroid Build Coastguard Worker         ALOGE("NULL VintfObjectRecovery!");
85*38e8c45fSAndroid Build Coastguard Worker         return {};
86*38e8c45fSAndroid Build Coastguard Worker     }
87*38e8c45fSAndroid Build Coastguard Worker     return {ManifestWithDescription{vintfObject->getRecoveryHalManifest(), "recovery"}};
88*38e8c45fSAndroid Build Coastguard Worker #else
89*38e8c45fSAndroid Build Coastguard Worker     auto vintfObject = vintf::VintfObject::GetInstance();
90*38e8c45fSAndroid Build Coastguard Worker     if (vintfObject == nullptr) {
91*38e8c45fSAndroid Build Coastguard Worker         ALOGE("NULL VintfObject!");
92*38e8c45fSAndroid Build Coastguard Worker         return {};
93*38e8c45fSAndroid Build Coastguard Worker     }
94*38e8c45fSAndroid Build Coastguard Worker     return {ManifestWithDescription{vintfObject->getDeviceHalManifest(), "device"},
95*38e8c45fSAndroid Build Coastguard Worker             ManifestWithDescription{vintfObject->getFrameworkHalManifest(), "framework"}};
96*38e8c45fSAndroid Build Coastguard Worker #endif
97*38e8c45fSAndroid Build Coastguard Worker }
98*38e8c45fSAndroid Build Coastguard Worker 
99*38e8c45fSAndroid Build Coastguard Worker // func true -> stop search and forEachManifest will return true
forEachManifest(const std::function<bool (const ManifestWithDescription &)> & func)100*38e8c45fSAndroid Build Coastguard Worker static bool forEachManifest(const std::function<bool(const ManifestWithDescription&)>& func) {
101*38e8c45fSAndroid Build Coastguard Worker     for (const ManifestWithDescription& mwd : GetManifestsWithDescription()) {
102*38e8c45fSAndroid Build Coastguard Worker         if (mwd.manifest == nullptr) {
103*38e8c45fSAndroid Build Coastguard Worker             ALOGE("NULL VINTF MANIFEST!: %s", mwd.description);
104*38e8c45fSAndroid Build Coastguard Worker             // note, we explicitly do not retry here, so that we can detect VINTF
105*38e8c45fSAndroid Build Coastguard Worker             // or other bugs (b/151696835)
106*38e8c45fSAndroid Build Coastguard Worker             continue;
107*38e8c45fSAndroid Build Coastguard Worker         }
108*38e8c45fSAndroid Build Coastguard Worker         if (func(mwd)) return true;
109*38e8c45fSAndroid Build Coastguard Worker     }
110*38e8c45fSAndroid Build Coastguard Worker     return false;
111*38e8c45fSAndroid Build Coastguard Worker }
112*38e8c45fSAndroid Build Coastguard Worker 
getNativeInstanceName(const vintf::ManifestInstance & instance)113*38e8c45fSAndroid Build Coastguard Worker static std::string getNativeInstanceName(const vintf::ManifestInstance& instance) {
114*38e8c45fSAndroid Build Coastguard Worker     return instance.package() + "/" + instance.instance();
115*38e8c45fSAndroid Build Coastguard Worker }
116*38e8c45fSAndroid Build Coastguard Worker 
117*38e8c45fSAndroid Build Coastguard Worker struct AidlName {
118*38e8c45fSAndroid Build Coastguard Worker     std::string package;
119*38e8c45fSAndroid Build Coastguard Worker     std::string iface;
120*38e8c45fSAndroid Build Coastguard Worker     std::string instance;
121*38e8c45fSAndroid Build Coastguard Worker 
fillandroid::AidlName122*38e8c45fSAndroid Build Coastguard Worker     static bool fill(const std::string& name, AidlName* aname, bool logError) {
123*38e8c45fSAndroid Build Coastguard Worker         size_t firstSlash = name.find('/');
124*38e8c45fSAndroid Build Coastguard Worker         size_t lastDot = name.rfind('.', firstSlash);
125*38e8c45fSAndroid Build Coastguard Worker         if (firstSlash == std::string::npos || lastDot == std::string::npos) {
126*38e8c45fSAndroid Build Coastguard Worker             if (logError) {
127*38e8c45fSAndroid Build Coastguard Worker                 ALOGE("VINTF HALs require names in the format type/instance (e.g. "
128*38e8c45fSAndroid Build Coastguard Worker                       "some.package.foo.IFoo/default) but got: %s",
129*38e8c45fSAndroid Build Coastguard Worker                       name.c_str());
130*38e8c45fSAndroid Build Coastguard Worker             }
131*38e8c45fSAndroid Build Coastguard Worker             return false;
132*38e8c45fSAndroid Build Coastguard Worker         }
133*38e8c45fSAndroid Build Coastguard Worker         aname->package = name.substr(0, lastDot);
134*38e8c45fSAndroid Build Coastguard Worker         aname->iface = name.substr(lastDot + 1, firstSlash - lastDot - 1);
135*38e8c45fSAndroid Build Coastguard Worker         aname->instance = name.substr(firstSlash + 1);
136*38e8c45fSAndroid Build Coastguard Worker         return true;
137*38e8c45fSAndroid Build Coastguard Worker     }
138*38e8c45fSAndroid Build Coastguard Worker };
139*38e8c45fSAndroid Build Coastguard Worker 
getAidlInstanceName(const vintf::ManifestInstance & instance)140*38e8c45fSAndroid Build Coastguard Worker static std::string getAidlInstanceName(const vintf::ManifestInstance& instance) {
141*38e8c45fSAndroid Build Coastguard Worker     return instance.package() + "." + instance.interface() + "/" + instance.instance();
142*38e8c45fSAndroid Build Coastguard Worker }
143*38e8c45fSAndroid Build Coastguard Worker 
isVintfDeclared(const Access::CallingContext & ctx,const std::string & name)144*38e8c45fSAndroid Build Coastguard Worker static bool isVintfDeclared(const Access::CallingContext& ctx, const std::string& name) {
145*38e8c45fSAndroid Build Coastguard Worker     NativeName nname;
146*38e8c45fSAndroid Build Coastguard Worker     if (NativeName::fill(name, &nname)) {
147*38e8c45fSAndroid Build Coastguard Worker         bool found = forEachManifest([&](const ManifestWithDescription& mwd) {
148*38e8c45fSAndroid Build Coastguard Worker             if (mwd.manifest->hasNativeInstance(nname.package, nname.instance)) {
149*38e8c45fSAndroid Build Coastguard Worker                 ALOGI("%s Found %s in %s VINTF manifest.", ctx.toDebugString().c_str(),
150*38e8c45fSAndroid Build Coastguard Worker                       name.c_str(), mwd.description);
151*38e8c45fSAndroid Build Coastguard Worker                 return true; // break
152*38e8c45fSAndroid Build Coastguard Worker             }
153*38e8c45fSAndroid Build Coastguard Worker             return false; // continue
154*38e8c45fSAndroid Build Coastguard Worker         });
155*38e8c45fSAndroid Build Coastguard Worker         if (!found) {
156*38e8c45fSAndroid Build Coastguard Worker             ALOGI("%s Could not find %s in the VINTF manifest.", ctx.toDebugString().c_str(),
157*38e8c45fSAndroid Build Coastguard Worker                   name.c_str());
158*38e8c45fSAndroid Build Coastguard Worker         }
159*38e8c45fSAndroid Build Coastguard Worker         return found;
160*38e8c45fSAndroid Build Coastguard Worker     }
161*38e8c45fSAndroid Build Coastguard Worker 
162*38e8c45fSAndroid Build Coastguard Worker     AidlName aname;
163*38e8c45fSAndroid Build Coastguard Worker     if (!AidlName::fill(name, &aname, true)) return false;
164*38e8c45fSAndroid Build Coastguard Worker 
165*38e8c45fSAndroid Build Coastguard Worker     bool found = forEachManifest([&](const ManifestWithDescription& mwd) {
166*38e8c45fSAndroid Build Coastguard Worker         if (mwd.manifest->hasAidlInstance(aname.package, aname.iface, aname.instance)) {
167*38e8c45fSAndroid Build Coastguard Worker             ALOGI("%s Found %s in %s VINTF manifest.", ctx.toDebugString().c_str(), name.c_str(),
168*38e8c45fSAndroid Build Coastguard Worker                   mwd.description);
169*38e8c45fSAndroid Build Coastguard Worker             return true; // break
170*38e8c45fSAndroid Build Coastguard Worker         }
171*38e8c45fSAndroid Build Coastguard Worker         return false;  // continue
172*38e8c45fSAndroid Build Coastguard Worker     });
173*38e8c45fSAndroid Build Coastguard Worker 
174*38e8c45fSAndroid Build Coastguard Worker     if (!found) {
175*38e8c45fSAndroid Build Coastguard Worker         std::set<std::string> instances;
176*38e8c45fSAndroid Build Coastguard Worker         forEachManifest([&](const ManifestWithDescription& mwd) {
177*38e8c45fSAndroid Build Coastguard Worker             std::set<std::string> res = mwd.manifest->getAidlInstances(aname.package, aname.iface);
178*38e8c45fSAndroid Build Coastguard Worker             instances.insert(res.begin(), res.end());
179*38e8c45fSAndroid Build Coastguard Worker             return true;
180*38e8c45fSAndroid Build Coastguard Worker         });
181*38e8c45fSAndroid Build Coastguard Worker 
182*38e8c45fSAndroid Build Coastguard Worker         std::string available;
183*38e8c45fSAndroid Build Coastguard Worker         if (instances.empty()) {
184*38e8c45fSAndroid Build Coastguard Worker             available = "No alternative instances declared in VINTF";
185*38e8c45fSAndroid Build Coastguard Worker         } else {
186*38e8c45fSAndroid Build Coastguard Worker             // for logging only. We can't return this information to the client
187*38e8c45fSAndroid Build Coastguard Worker             // because they may not have permissions to find or list those
188*38e8c45fSAndroid Build Coastguard Worker             // instances
189*38e8c45fSAndroid Build Coastguard Worker             available = "VINTF declared instances: " + base::Join(instances, ", ");
190*38e8c45fSAndroid Build Coastguard Worker         }
191*38e8c45fSAndroid Build Coastguard Worker         // Although it is tested, explicitly rebuilding qualified name, in case it
192*38e8c45fSAndroid Build Coastguard Worker         // becomes something unexpected.
193*38e8c45fSAndroid Build Coastguard Worker         ALOGI("%s Could not find %s.%s/%s in the VINTF manifest. %s.", ctx.toDebugString().c_str(),
194*38e8c45fSAndroid Build Coastguard Worker               aname.package.c_str(), aname.iface.c_str(), aname.instance.c_str(),
195*38e8c45fSAndroid Build Coastguard Worker               available.c_str());
196*38e8c45fSAndroid Build Coastguard Worker     }
197*38e8c45fSAndroid Build Coastguard Worker 
198*38e8c45fSAndroid Build Coastguard Worker     return found;
199*38e8c45fSAndroid Build Coastguard Worker }
200*38e8c45fSAndroid Build Coastguard Worker 
getVintfUpdatableApex(const std::string & name)201*38e8c45fSAndroid Build Coastguard Worker static std::optional<std::string> getVintfUpdatableApex(const std::string& name) {
202*38e8c45fSAndroid Build Coastguard Worker     NativeName nname;
203*38e8c45fSAndroid Build Coastguard Worker     if (NativeName::fill(name, &nname)) {
204*38e8c45fSAndroid Build Coastguard Worker         std::optional<std::string> updatableViaApex;
205*38e8c45fSAndroid Build Coastguard Worker 
206*38e8c45fSAndroid Build Coastguard Worker         forEachManifest([&](const ManifestWithDescription& mwd) {
207*38e8c45fSAndroid Build Coastguard Worker             bool cont = mwd.manifest->forEachInstance([&](const auto& manifestInstance) {
208*38e8c45fSAndroid Build Coastguard Worker                 if (manifestInstance.format() != vintf::HalFormat::NATIVE) return true;
209*38e8c45fSAndroid Build Coastguard Worker                 if (manifestInstance.package() != nname.package) return true;
210*38e8c45fSAndroid Build Coastguard Worker                 if (manifestInstance.instance() != nname.instance) return true;
211*38e8c45fSAndroid Build Coastguard Worker                 updatableViaApex = manifestInstance.updatableViaApex();
212*38e8c45fSAndroid Build Coastguard Worker                 return false; // break (libvintf uses opposite convention)
213*38e8c45fSAndroid Build Coastguard Worker             });
214*38e8c45fSAndroid Build Coastguard Worker             return !cont;
215*38e8c45fSAndroid Build Coastguard Worker         });
216*38e8c45fSAndroid Build Coastguard Worker 
217*38e8c45fSAndroid Build Coastguard Worker         return updatableViaApex;
218*38e8c45fSAndroid Build Coastguard Worker     }
219*38e8c45fSAndroid Build Coastguard Worker 
220*38e8c45fSAndroid Build Coastguard Worker     AidlName aname;
221*38e8c45fSAndroid Build Coastguard Worker     if (!AidlName::fill(name, &aname, true)) return std::nullopt;
222*38e8c45fSAndroid Build Coastguard Worker 
223*38e8c45fSAndroid Build Coastguard Worker     std::optional<std::string> updatableViaApex;
224*38e8c45fSAndroid Build Coastguard Worker 
225*38e8c45fSAndroid Build Coastguard Worker     forEachManifest([&](const ManifestWithDescription& mwd) {
226*38e8c45fSAndroid Build Coastguard Worker         bool cont = mwd.manifest->forEachInstance([&](const auto& manifestInstance) {
227*38e8c45fSAndroid Build Coastguard Worker             if (manifestInstance.format() != vintf::HalFormat::AIDL) return true;
228*38e8c45fSAndroid Build Coastguard Worker             if (manifestInstance.package() != aname.package) return true;
229*38e8c45fSAndroid Build Coastguard Worker             if (manifestInstance.interface() != aname.iface) return true;
230*38e8c45fSAndroid Build Coastguard Worker             if (manifestInstance.instance() != aname.instance) return true;
231*38e8c45fSAndroid Build Coastguard Worker             updatableViaApex = manifestInstance.updatableViaApex();
232*38e8c45fSAndroid Build Coastguard Worker             return false; // break (libvintf uses opposite convention)
233*38e8c45fSAndroid Build Coastguard Worker         });
234*38e8c45fSAndroid Build Coastguard Worker         return !cont;
235*38e8c45fSAndroid Build Coastguard Worker     });
236*38e8c45fSAndroid Build Coastguard Worker 
237*38e8c45fSAndroid Build Coastguard Worker     return updatableViaApex;
238*38e8c45fSAndroid Build Coastguard Worker }
239*38e8c45fSAndroid Build Coastguard Worker 
getVintfUpdatableNames(const std::string & apexName)240*38e8c45fSAndroid Build Coastguard Worker static std::vector<std::string> getVintfUpdatableNames(const std::string& apexName) {
241*38e8c45fSAndroid Build Coastguard Worker     std::vector<std::string> names;
242*38e8c45fSAndroid Build Coastguard Worker 
243*38e8c45fSAndroid Build Coastguard Worker     forEachManifest([&](const ManifestWithDescription& mwd) {
244*38e8c45fSAndroid Build Coastguard Worker         mwd.manifest->forEachInstance([&](const auto& manifestInstance) {
245*38e8c45fSAndroid Build Coastguard Worker             if (manifestInstance.updatableViaApex().has_value() &&
246*38e8c45fSAndroid Build Coastguard Worker                 manifestInstance.updatableViaApex().value() == apexName) {
247*38e8c45fSAndroid Build Coastguard Worker                 if (manifestInstance.format() == vintf::HalFormat::NATIVE) {
248*38e8c45fSAndroid Build Coastguard Worker                     names.push_back(getNativeInstanceName(manifestInstance));
249*38e8c45fSAndroid Build Coastguard Worker                 } else if (manifestInstance.format() == vintf::HalFormat::AIDL) {
250*38e8c45fSAndroid Build Coastguard Worker                     names.push_back(getAidlInstanceName(manifestInstance));
251*38e8c45fSAndroid Build Coastguard Worker                 }
252*38e8c45fSAndroid Build Coastguard Worker             }
253*38e8c45fSAndroid Build Coastguard Worker             return true; // continue (libvintf uses opposite convention)
254*38e8c45fSAndroid Build Coastguard Worker         });
255*38e8c45fSAndroid Build Coastguard Worker         return false; // continue
256*38e8c45fSAndroid Build Coastguard Worker     });
257*38e8c45fSAndroid Build Coastguard Worker 
258*38e8c45fSAndroid Build Coastguard Worker     return names;
259*38e8c45fSAndroid Build Coastguard Worker }
260*38e8c45fSAndroid Build Coastguard Worker 
getVintfAccessorName(const std::string & name)261*38e8c45fSAndroid Build Coastguard Worker static std::optional<std::string> getVintfAccessorName(const std::string& name) {
262*38e8c45fSAndroid Build Coastguard Worker     AidlName aname;
263*38e8c45fSAndroid Build Coastguard Worker     if (!AidlName::fill(name, &aname, false)) return std::nullopt;
264*38e8c45fSAndroid Build Coastguard Worker 
265*38e8c45fSAndroid Build Coastguard Worker     std::optional<std::string> accessor;
266*38e8c45fSAndroid Build Coastguard Worker     forEachManifest([&](const ManifestWithDescription& mwd) {
267*38e8c45fSAndroid Build Coastguard Worker         mwd.manifest->forEachInstance([&](const auto& manifestInstance) {
268*38e8c45fSAndroid Build Coastguard Worker             if (manifestInstance.format() != vintf::HalFormat::AIDL) return true;
269*38e8c45fSAndroid Build Coastguard Worker             if (manifestInstance.package() != aname.package) return true;
270*38e8c45fSAndroid Build Coastguard Worker             if (manifestInstance.interface() != aname.iface) return true;
271*38e8c45fSAndroid Build Coastguard Worker             if (manifestInstance.instance() != aname.instance) return true;
272*38e8c45fSAndroid Build Coastguard Worker             accessor = manifestInstance.accessor();
273*38e8c45fSAndroid Build Coastguard Worker             return false; // break (libvintf uses opposite convention)
274*38e8c45fSAndroid Build Coastguard Worker         });
275*38e8c45fSAndroid Build Coastguard Worker         return false; // continue
276*38e8c45fSAndroid Build Coastguard Worker     });
277*38e8c45fSAndroid Build Coastguard Worker     return accessor;
278*38e8c45fSAndroid Build Coastguard Worker }
279*38e8c45fSAndroid Build Coastguard Worker 
getVintfConnectionInfo(const std::string & name)280*38e8c45fSAndroid Build Coastguard Worker static std::optional<ConnectionInfo> getVintfConnectionInfo(const std::string& name) {
281*38e8c45fSAndroid Build Coastguard Worker     AidlName aname;
282*38e8c45fSAndroid Build Coastguard Worker     if (!AidlName::fill(name, &aname, true)) return std::nullopt;
283*38e8c45fSAndroid Build Coastguard Worker 
284*38e8c45fSAndroid Build Coastguard Worker     std::optional<std::string> ip;
285*38e8c45fSAndroid Build Coastguard Worker     std::optional<uint64_t> port;
286*38e8c45fSAndroid Build Coastguard Worker     forEachManifest([&](const ManifestWithDescription& mwd) {
287*38e8c45fSAndroid Build Coastguard Worker         mwd.manifest->forEachInstance([&](const auto& manifestInstance) {
288*38e8c45fSAndroid Build Coastguard Worker             if (manifestInstance.format() != vintf::HalFormat::AIDL) return true;
289*38e8c45fSAndroid Build Coastguard Worker             if (manifestInstance.package() != aname.package) return true;
290*38e8c45fSAndroid Build Coastguard Worker             if (manifestInstance.interface() != aname.iface) return true;
291*38e8c45fSAndroid Build Coastguard Worker             if (manifestInstance.instance() != aname.instance) return true;
292*38e8c45fSAndroid Build Coastguard Worker             ip = manifestInstance.ip();
293*38e8c45fSAndroid Build Coastguard Worker             port = manifestInstance.port();
294*38e8c45fSAndroid Build Coastguard Worker             return false; // break (libvintf uses opposite convention)
295*38e8c45fSAndroid Build Coastguard Worker         });
296*38e8c45fSAndroid Build Coastguard Worker         return false; // continue
297*38e8c45fSAndroid Build Coastguard Worker     });
298*38e8c45fSAndroid Build Coastguard Worker 
299*38e8c45fSAndroid Build Coastguard Worker     if (ip.has_value() && port.has_value()) {
300*38e8c45fSAndroid Build Coastguard Worker         ConnectionInfo info;
301*38e8c45fSAndroid Build Coastguard Worker         info.ipAddress = *ip;
302*38e8c45fSAndroid Build Coastguard Worker         info.port = *port;
303*38e8c45fSAndroid Build Coastguard Worker         return std::make_optional<ConnectionInfo>(info);
304*38e8c45fSAndroid Build Coastguard Worker     } else {
305*38e8c45fSAndroid Build Coastguard Worker         return std::nullopt;
306*38e8c45fSAndroid Build Coastguard Worker     }
307*38e8c45fSAndroid Build Coastguard Worker }
308*38e8c45fSAndroid Build Coastguard Worker 
getVintfInstances(const std::string & interface)309*38e8c45fSAndroid Build Coastguard Worker static std::vector<std::string> getVintfInstances(const std::string& interface) {
310*38e8c45fSAndroid Build Coastguard Worker     size_t lastDot = interface.rfind('.');
311*38e8c45fSAndroid Build Coastguard Worker     if (lastDot == std::string::npos) {
312*38e8c45fSAndroid Build Coastguard Worker         // This might be a package for native instance.
313*38e8c45fSAndroid Build Coastguard Worker         std::vector<std::string> ret;
314*38e8c45fSAndroid Build Coastguard Worker         (void)forEachManifest([&](const ManifestWithDescription& mwd) {
315*38e8c45fSAndroid Build Coastguard Worker             auto instances = mwd.manifest->getNativeInstances(interface);
316*38e8c45fSAndroid Build Coastguard Worker             ret.insert(ret.end(), instances.begin(), instances.end());
317*38e8c45fSAndroid Build Coastguard Worker             return false; // continue
318*38e8c45fSAndroid Build Coastguard Worker         });
319*38e8c45fSAndroid Build Coastguard Worker         // If found, return it without error log.
320*38e8c45fSAndroid Build Coastguard Worker         if (!ret.empty()) {
321*38e8c45fSAndroid Build Coastguard Worker             return ret;
322*38e8c45fSAndroid Build Coastguard Worker         }
323*38e8c45fSAndroid Build Coastguard Worker 
324*38e8c45fSAndroid Build Coastguard Worker         ALOGE("VINTF interfaces require names in Java package format (e.g. some.package.foo.IFoo) "
325*38e8c45fSAndroid Build Coastguard Worker               "but got: %s",
326*38e8c45fSAndroid Build Coastguard Worker               interface.c_str());
327*38e8c45fSAndroid Build Coastguard Worker         return {};
328*38e8c45fSAndroid Build Coastguard Worker     }
329*38e8c45fSAndroid Build Coastguard Worker     const std::string package = interface.substr(0, lastDot);
330*38e8c45fSAndroid Build Coastguard Worker     const std::string iface = interface.substr(lastDot+1);
331*38e8c45fSAndroid Build Coastguard Worker 
332*38e8c45fSAndroid Build Coastguard Worker     std::vector<std::string> ret;
333*38e8c45fSAndroid Build Coastguard Worker     (void)forEachManifest([&](const ManifestWithDescription& mwd) {
334*38e8c45fSAndroid Build Coastguard Worker         auto instances = mwd.manifest->getAidlInstances(package, iface);
335*38e8c45fSAndroid Build Coastguard Worker         ret.insert(ret.end(), instances.begin(), instances.end());
336*38e8c45fSAndroid Build Coastguard Worker         return false;  // continue
337*38e8c45fSAndroid Build Coastguard Worker     });
338*38e8c45fSAndroid Build Coastguard Worker 
339*38e8c45fSAndroid Build Coastguard Worker     return ret;
340*38e8c45fSAndroid Build Coastguard Worker }
341*38e8c45fSAndroid Build Coastguard Worker 
meetsDeclarationRequirements(const Access::CallingContext & ctx,const sp<IBinder> & binder,const std::string & name)342*38e8c45fSAndroid Build Coastguard Worker static bool meetsDeclarationRequirements(const Access::CallingContext& ctx,
343*38e8c45fSAndroid Build Coastguard Worker                                          const sp<IBinder>& binder, const std::string& name) {
344*38e8c45fSAndroid Build Coastguard Worker     if (!Stability::requiresVintfDeclaration(binder)) {
345*38e8c45fSAndroid Build Coastguard Worker         return true;
346*38e8c45fSAndroid Build Coastguard Worker     }
347*38e8c45fSAndroid Build Coastguard Worker 
348*38e8c45fSAndroid Build Coastguard Worker     return isVintfDeclared(ctx, name);
349*38e8c45fSAndroid Build Coastguard Worker }
350*38e8c45fSAndroid Build Coastguard Worker #endif  // !VENDORSERVICEMANAGER
351*38e8c45fSAndroid Build Coastguard Worker 
~Service()352*38e8c45fSAndroid Build Coastguard Worker ServiceManager::Service::~Service() {
353*38e8c45fSAndroid Build Coastguard Worker     if (hasClients) {
354*38e8c45fSAndroid Build Coastguard Worker         // only expected to happen on process death, we don't store the service
355*38e8c45fSAndroid Build Coastguard Worker         // name this late (it's in the map that holds this service), but if it
356*38e8c45fSAndroid Build Coastguard Worker         // is happening, we might want to change 'unlinkToDeath' to explicitly
357*38e8c45fSAndroid Build Coastguard Worker         // clear this bit so that we can abort in other cases, where it would
358*38e8c45fSAndroid Build Coastguard Worker         // mean inconsistent logic in servicemanager (unexpected and tested, but
359*38e8c45fSAndroid Build Coastguard Worker         // the original lazy service impl here had that bug).
360*38e8c45fSAndroid Build Coastguard Worker         ALOGW("A service was removed when there are clients");
361*38e8c45fSAndroid Build Coastguard Worker     }
362*38e8c45fSAndroid Build Coastguard Worker }
363*38e8c45fSAndroid Build Coastguard Worker 
ServiceManager(std::unique_ptr<Access> && access)364*38e8c45fSAndroid Build Coastguard Worker ServiceManager::ServiceManager(std::unique_ptr<Access>&& access) : mAccess(std::move(access)) {
365*38e8c45fSAndroid Build Coastguard Worker // TODO(b/151696835): reenable performance hack when we solve bug, since with
366*38e8c45fSAndroid Build Coastguard Worker //     this hack and other fixes, it is unlikely we will see even an ephemeral
367*38e8c45fSAndroid Build Coastguard Worker //     failure when the manifest parse fails. The goal is that the manifest will
368*38e8c45fSAndroid Build Coastguard Worker //     be read incorrectly and cause the process trying to register a HAL to
369*38e8c45fSAndroid Build Coastguard Worker //     fail. If this is in fact an early boot kernel contention issue, then we
370*38e8c45fSAndroid Build Coastguard Worker //     will get no failure, and by its absence, be signalled to invest more
371*38e8c45fSAndroid Build Coastguard Worker //     effort in re-adding this performance hack.
372*38e8c45fSAndroid Build Coastguard Worker // #ifndef VENDORSERVICEMANAGER
373*38e8c45fSAndroid Build Coastguard Worker //     // can process these at any times, don't want to delay first VINTF client
374*38e8c45fSAndroid Build Coastguard Worker //     std::thread([] {
375*38e8c45fSAndroid Build Coastguard Worker //         vintf::VintfObject::GetDeviceHalManifest();
376*38e8c45fSAndroid Build Coastguard Worker //         vintf::VintfObject::GetFrameworkHalManifest();
377*38e8c45fSAndroid Build Coastguard Worker //     }).detach();
378*38e8c45fSAndroid Build Coastguard Worker // #endif  // !VENDORSERVICEMANAGER
379*38e8c45fSAndroid Build Coastguard Worker }
~ServiceManager()380*38e8c45fSAndroid Build Coastguard Worker ServiceManager::~ServiceManager() {
381*38e8c45fSAndroid Build Coastguard Worker     // this should only happen in tests
382*38e8c45fSAndroid Build Coastguard Worker 
383*38e8c45fSAndroid Build Coastguard Worker     for (const auto& [name, callbacks] : mNameToRegistrationCallback) {
384*38e8c45fSAndroid Build Coastguard Worker         CHECK(!callbacks.empty()) << name;
385*38e8c45fSAndroid Build Coastguard Worker         for (const auto& callback : callbacks) {
386*38e8c45fSAndroid Build Coastguard Worker             CHECK(callback != nullptr) << name;
387*38e8c45fSAndroid Build Coastguard Worker         }
388*38e8c45fSAndroid Build Coastguard Worker     }
389*38e8c45fSAndroid Build Coastguard Worker 
390*38e8c45fSAndroid Build Coastguard Worker     for (const auto& [name, service] : mNameToService) {
391*38e8c45fSAndroid Build Coastguard Worker         CHECK(service.binder != nullptr) << name;
392*38e8c45fSAndroid Build Coastguard Worker     }
393*38e8c45fSAndroid Build Coastguard Worker }
394*38e8c45fSAndroid Build Coastguard Worker 
getService(const std::string & name,sp<IBinder> * outBinder)395*38e8c45fSAndroid Build Coastguard Worker Status ServiceManager::getService(const std::string& name, sp<IBinder>* outBinder) {
396*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
397*38e8c45fSAndroid Build Coastguard Worker             PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
398*38e8c45fSAndroid Build Coastguard Worker 
399*38e8c45fSAndroid Build Coastguard Worker     *outBinder = tryGetBinder(name, true).service;
400*38e8c45fSAndroid Build Coastguard Worker     // returns ok regardless of result for legacy reasons
401*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
402*38e8c45fSAndroid Build Coastguard Worker }
403*38e8c45fSAndroid Build Coastguard Worker 
getService2(const std::string & name,os::Service * outService)404*38e8c45fSAndroid Build Coastguard Worker Status ServiceManager::getService2(const std::string& name, os::Service* outService) {
405*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
406*38e8c45fSAndroid Build Coastguard Worker             PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
407*38e8c45fSAndroid Build Coastguard Worker 
408*38e8c45fSAndroid Build Coastguard Worker     *outService = tryGetService(name, true);
409*38e8c45fSAndroid Build Coastguard Worker     // returns ok regardless of result for legacy reasons
410*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
411*38e8c45fSAndroid Build Coastguard Worker }
412*38e8c45fSAndroid Build Coastguard Worker 
checkService(const std::string & name,os::Service * outService)413*38e8c45fSAndroid Build Coastguard Worker Status ServiceManager::checkService(const std::string& name, os::Service* outService) {
414*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
415*38e8c45fSAndroid Build Coastguard Worker             PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
416*38e8c45fSAndroid Build Coastguard Worker 
417*38e8c45fSAndroid Build Coastguard Worker     *outService = tryGetService(name, false);
418*38e8c45fSAndroid Build Coastguard Worker     // returns ok regardless of result for legacy reasons
419*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
420*38e8c45fSAndroid Build Coastguard Worker }
421*38e8c45fSAndroid Build Coastguard Worker 
tryGetService(const std::string & name,bool startIfNotFound)422*38e8c45fSAndroid Build Coastguard Worker os::Service ServiceManager::tryGetService(const std::string& name, bool startIfNotFound) {
423*38e8c45fSAndroid Build Coastguard Worker     std::optional<std::string> accessorName;
424*38e8c45fSAndroid Build Coastguard Worker #ifndef VENDORSERVICEMANAGER
425*38e8c45fSAndroid Build Coastguard Worker     accessorName = getVintfAccessorName(name);
426*38e8c45fSAndroid Build Coastguard Worker #endif
427*38e8c45fSAndroid Build Coastguard Worker     if (accessorName.has_value()) {
428*38e8c45fSAndroid Build Coastguard Worker         auto ctx = mAccess->getCallingContext();
429*38e8c45fSAndroid Build Coastguard Worker         if (!mAccess->canFind(ctx, name)) {
430*38e8c45fSAndroid Build Coastguard Worker             return os::Service::make<os::Service::Tag::accessor>(nullptr);
431*38e8c45fSAndroid Build Coastguard Worker         }
432*38e8c45fSAndroid Build Coastguard Worker         return os::Service::make<os::Service::Tag::accessor>(
433*38e8c45fSAndroid Build Coastguard Worker                 tryGetBinder(*accessorName, startIfNotFound).service);
434*38e8c45fSAndroid Build Coastguard Worker     } else {
435*38e8c45fSAndroid Build Coastguard Worker         return os::Service::make<os::Service::Tag::serviceWithMetadata>(
436*38e8c45fSAndroid Build Coastguard Worker                 tryGetBinder(name, startIfNotFound));
437*38e8c45fSAndroid Build Coastguard Worker     }
438*38e8c45fSAndroid Build Coastguard Worker }
439*38e8c45fSAndroid Build Coastguard Worker 
tryGetBinder(const std::string & name,bool startIfNotFound)440*38e8c45fSAndroid Build Coastguard Worker os::ServiceWithMetadata ServiceManager::tryGetBinder(const std::string& name,
441*38e8c45fSAndroid Build Coastguard Worker                                                      bool startIfNotFound) {
442*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
443*38e8c45fSAndroid Build Coastguard Worker             PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
444*38e8c45fSAndroid Build Coastguard Worker 
445*38e8c45fSAndroid Build Coastguard Worker     auto ctx = mAccess->getCallingContext();
446*38e8c45fSAndroid Build Coastguard Worker 
447*38e8c45fSAndroid Build Coastguard Worker     sp<IBinder> out;
448*38e8c45fSAndroid Build Coastguard Worker     Service* service = nullptr;
449*38e8c45fSAndroid Build Coastguard Worker     if (auto it = mNameToService.find(name); it != mNameToService.end()) {
450*38e8c45fSAndroid Build Coastguard Worker         service = &(it->second);
451*38e8c45fSAndroid Build Coastguard Worker 
452*38e8c45fSAndroid Build Coastguard Worker         if (!service->allowIsolated && is_multiuser_uid_isolated(ctx.uid)) {
453*38e8c45fSAndroid Build Coastguard Worker             LOG(WARNING) << "Isolated app with UID " << ctx.uid << " requested '" << name
454*38e8c45fSAndroid Build Coastguard Worker                          << "', but the service is not allowed for isolated apps.";
455*38e8c45fSAndroid Build Coastguard Worker             return os::ServiceWithMetadata();
456*38e8c45fSAndroid Build Coastguard Worker         }
457*38e8c45fSAndroid Build Coastguard Worker         out = service->binder;
458*38e8c45fSAndroid Build Coastguard Worker     }
459*38e8c45fSAndroid Build Coastguard Worker 
460*38e8c45fSAndroid Build Coastguard Worker     if (!mAccess->canFind(ctx, name)) {
461*38e8c45fSAndroid Build Coastguard Worker         return os::ServiceWithMetadata();
462*38e8c45fSAndroid Build Coastguard Worker     }
463*38e8c45fSAndroid Build Coastguard Worker 
464*38e8c45fSAndroid Build Coastguard Worker     if (!out && startIfNotFound) {
465*38e8c45fSAndroid Build Coastguard Worker         tryStartService(ctx, name);
466*38e8c45fSAndroid Build Coastguard Worker     }
467*38e8c45fSAndroid Build Coastguard Worker 
468*38e8c45fSAndroid Build Coastguard Worker     if (out) {
469*38e8c45fSAndroid Build Coastguard Worker         // Force onClients to get sent, and then make sure the timerfd won't clear it
470*38e8c45fSAndroid Build Coastguard Worker         // by setting guaranteeClient again. This logic could be simplified by using
471*38e8c45fSAndroid Build Coastguard Worker         // a time-based guarantee. However, forcing onClients(true) to get sent
472*38e8c45fSAndroid Build Coastguard Worker         // right here is always going to be important for processes serving multiple
473*38e8c45fSAndroid Build Coastguard Worker         // lazy interfaces.
474*38e8c45fSAndroid Build Coastguard Worker         service->guaranteeClient = true;
475*38e8c45fSAndroid Build Coastguard Worker         CHECK(handleServiceClientCallback(2 /* sm + transaction */, name, false));
476*38e8c45fSAndroid Build Coastguard Worker         service->guaranteeClient = true;
477*38e8c45fSAndroid Build Coastguard Worker     }
478*38e8c45fSAndroid Build Coastguard Worker     os::ServiceWithMetadata serviceWithMetadata = os::ServiceWithMetadata();
479*38e8c45fSAndroid Build Coastguard Worker     serviceWithMetadata.service = out;
480*38e8c45fSAndroid Build Coastguard Worker     serviceWithMetadata.isLazyService =
481*38e8c45fSAndroid Build Coastguard Worker             service ? service->dumpPriority & FLAG_IS_LAZY_SERVICE : false;
482*38e8c45fSAndroid Build Coastguard Worker     return serviceWithMetadata;
483*38e8c45fSAndroid Build Coastguard Worker }
484*38e8c45fSAndroid Build Coastguard Worker 
isValidServiceName(const std::string & name)485*38e8c45fSAndroid Build Coastguard Worker bool isValidServiceName(const std::string& name) {
486*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
487*38e8c45fSAndroid Build Coastguard Worker             PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
488*38e8c45fSAndroid Build Coastguard Worker 
489*38e8c45fSAndroid Build Coastguard Worker     if (name.size() == 0) return false;
490*38e8c45fSAndroid Build Coastguard Worker     if (name.size() > 127) return false;
491*38e8c45fSAndroid Build Coastguard Worker 
492*38e8c45fSAndroid Build Coastguard Worker     for (char c : name) {
493*38e8c45fSAndroid Build Coastguard Worker         if (c == '_' || c == '-' || c == '.' || c == '/') continue;
494*38e8c45fSAndroid Build Coastguard Worker         if (c >= 'a' && c <= 'z') continue;
495*38e8c45fSAndroid Build Coastguard Worker         if (c >= 'A' && c <= 'Z') continue;
496*38e8c45fSAndroid Build Coastguard Worker         if (c >= '0' && c <= '9') continue;
497*38e8c45fSAndroid Build Coastguard Worker         return false;
498*38e8c45fSAndroid Build Coastguard Worker     }
499*38e8c45fSAndroid Build Coastguard Worker 
500*38e8c45fSAndroid Build Coastguard Worker     return true;
501*38e8c45fSAndroid Build Coastguard Worker }
502*38e8c45fSAndroid Build Coastguard Worker 
addService(const std::string & name,const sp<IBinder> & binder,bool allowIsolated,int32_t dumpPriority)503*38e8c45fSAndroid Build Coastguard Worker Status ServiceManager::addService(const std::string& name, const sp<IBinder>& binder, bool allowIsolated, int32_t dumpPriority) {
504*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
505*38e8c45fSAndroid Build Coastguard Worker             PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
506*38e8c45fSAndroid Build Coastguard Worker 
507*38e8c45fSAndroid Build Coastguard Worker     auto ctx = mAccess->getCallingContext();
508*38e8c45fSAndroid Build Coastguard Worker 
509*38e8c45fSAndroid Build Coastguard Worker     if (multiuser_get_app_id(ctx.uid) >= AID_APP) {
510*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_SECURITY, "App UIDs cannot add services.");
511*38e8c45fSAndroid Build Coastguard Worker     }
512*38e8c45fSAndroid Build Coastguard Worker 
513*38e8c45fSAndroid Build Coastguard Worker     std::optional<std::string> accessorName;
514*38e8c45fSAndroid Build Coastguard Worker     if (auto status = canAddService(ctx, name, &accessorName); !status.isOk()) {
515*38e8c45fSAndroid Build Coastguard Worker         return status;
516*38e8c45fSAndroid Build Coastguard Worker     }
517*38e8c45fSAndroid Build Coastguard Worker 
518*38e8c45fSAndroid Build Coastguard Worker     if (binder == nullptr) {
519*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Null binder.");
520*38e8c45fSAndroid Build Coastguard Worker     }
521*38e8c45fSAndroid Build Coastguard Worker 
522*38e8c45fSAndroid Build Coastguard Worker     if (!isValidServiceName(name)) {
523*38e8c45fSAndroid Build Coastguard Worker         ALOGE("%s Invalid service name: %s", ctx.toDebugString().c_str(), name.c_str());
524*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name.");
525*38e8c45fSAndroid Build Coastguard Worker     }
526*38e8c45fSAndroid Build Coastguard Worker 
527*38e8c45fSAndroid Build Coastguard Worker #ifndef VENDORSERVICEMANAGER
528*38e8c45fSAndroid Build Coastguard Worker     if (!meetsDeclarationRequirements(ctx, binder, name)) {
529*38e8c45fSAndroid Build Coastguard Worker         // already logged
530*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "VINTF declaration error.");
531*38e8c45fSAndroid Build Coastguard Worker     }
532*38e8c45fSAndroid Build Coastguard Worker #endif  // !VENDORSERVICEMANAGER
533*38e8c45fSAndroid Build Coastguard Worker 
534*38e8c45fSAndroid Build Coastguard Worker     if ((dumpPriority & DUMP_FLAG_PRIORITY_ALL) == 0) {
535*38e8c45fSAndroid Build Coastguard Worker         ALOGW("%s Dump flag priority is not set when adding %s", ctx.toDebugString().c_str(),
536*38e8c45fSAndroid Build Coastguard Worker               name.c_str());
537*38e8c45fSAndroid Build Coastguard Worker     }
538*38e8c45fSAndroid Build Coastguard Worker 
539*38e8c45fSAndroid Build Coastguard Worker     // implicitly unlinked when the binder is removed
540*38e8c45fSAndroid Build Coastguard Worker     if (binder->remoteBinder() != nullptr &&
541*38e8c45fSAndroid Build Coastguard Worker         binder->linkToDeath(sp<ServiceManager>::fromExisting(this)) != OK) {
542*38e8c45fSAndroid Build Coastguard Worker         ALOGE("%s Could not linkToDeath when adding %s", ctx.toDebugString().c_str(), name.c_str());
543*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't linkToDeath.");
544*38e8c45fSAndroid Build Coastguard Worker     }
545*38e8c45fSAndroid Build Coastguard Worker 
546*38e8c45fSAndroid Build Coastguard Worker     auto it = mNameToService.find(name);
547*38e8c45fSAndroid Build Coastguard Worker     bool prevClients = false;
548*38e8c45fSAndroid Build Coastguard Worker     if (it != mNameToService.end()) {
549*38e8c45fSAndroid Build Coastguard Worker         const Service& existing = it->second;
550*38e8c45fSAndroid Build Coastguard Worker         prevClients = existing.hasClients;
551*38e8c45fSAndroid Build Coastguard Worker 
552*38e8c45fSAndroid Build Coastguard Worker         // We could do better than this because if the other service dies, it
553*38e8c45fSAndroid Build Coastguard Worker         // may not have an entry here. However, this case is unlikely. We are
554*38e8c45fSAndroid Build Coastguard Worker         // only trying to detect when two different services are accidentally installed.
555*38e8c45fSAndroid Build Coastguard Worker 
556*38e8c45fSAndroid Build Coastguard Worker         if (existing.ctx.uid != ctx.uid) {
557*38e8c45fSAndroid Build Coastguard Worker             ALOGW("Service '%s' originally registered from UID %u but it is now being registered "
558*38e8c45fSAndroid Build Coastguard Worker                   "from UID %u. Multiple instances installed?",
559*38e8c45fSAndroid Build Coastguard Worker                   name.c_str(), existing.ctx.uid, ctx.uid);
560*38e8c45fSAndroid Build Coastguard Worker         }
561*38e8c45fSAndroid Build Coastguard Worker 
562*38e8c45fSAndroid Build Coastguard Worker         if (existing.ctx.sid != ctx.sid) {
563*38e8c45fSAndroid Build Coastguard Worker             ALOGW("Service '%s' originally registered from SID %s but it is now being registered "
564*38e8c45fSAndroid Build Coastguard Worker                   "from SID %s. Multiple instances installed?",
565*38e8c45fSAndroid Build Coastguard Worker                   name.c_str(), existing.ctx.sid.c_str(), ctx.sid.c_str());
566*38e8c45fSAndroid Build Coastguard Worker         }
567*38e8c45fSAndroid Build Coastguard Worker 
568*38e8c45fSAndroid Build Coastguard Worker         ALOGI("Service '%s' originally registered from PID %d but it is being registered again "
569*38e8c45fSAndroid Build Coastguard Worker               "from PID %d. Bad state? Late death notification? Multiple instances installed?",
570*38e8c45fSAndroid Build Coastguard Worker               name.c_str(), existing.ctx.debugPid, ctx.debugPid);
571*38e8c45fSAndroid Build Coastguard Worker     }
572*38e8c45fSAndroid Build Coastguard Worker 
573*38e8c45fSAndroid Build Coastguard Worker     // Overwrite the old service if it exists
574*38e8c45fSAndroid Build Coastguard Worker     mNameToService[name] = Service{
575*38e8c45fSAndroid Build Coastguard Worker             .binder = binder,
576*38e8c45fSAndroid Build Coastguard Worker             .allowIsolated = allowIsolated,
577*38e8c45fSAndroid Build Coastguard Worker             .dumpPriority = dumpPriority,
578*38e8c45fSAndroid Build Coastguard Worker             .hasClients = prevClients, // see b/279898063, matters if existing callbacks
579*38e8c45fSAndroid Build Coastguard Worker             .guaranteeClient = false,
580*38e8c45fSAndroid Build Coastguard Worker             .ctx = ctx,
581*38e8c45fSAndroid Build Coastguard Worker     };
582*38e8c45fSAndroid Build Coastguard Worker 
583*38e8c45fSAndroid Build Coastguard Worker     if (auto it = mNameToRegistrationCallback.find(name); it != mNameToRegistrationCallback.end()) {
584*38e8c45fSAndroid Build Coastguard Worker         // If someone is currently waiting on the service, notify the service that
585*38e8c45fSAndroid Build Coastguard Worker         // we're waiting and flush it to the service.
586*38e8c45fSAndroid Build Coastguard Worker         mNameToService[name].guaranteeClient = true;
587*38e8c45fSAndroid Build Coastguard Worker         CHECK(handleServiceClientCallback(2 /* sm + transaction */, name, false));
588*38e8c45fSAndroid Build Coastguard Worker         mNameToService[name].guaranteeClient = true;
589*38e8c45fSAndroid Build Coastguard Worker 
590*38e8c45fSAndroid Build Coastguard Worker         for (const sp<IServiceCallback>& cb : it->second) {
591*38e8c45fSAndroid Build Coastguard Worker             // permission checked in registerForNotifications
592*38e8c45fSAndroid Build Coastguard Worker             cb->onRegistration(name, binder);
593*38e8c45fSAndroid Build Coastguard Worker         }
594*38e8c45fSAndroid Build Coastguard Worker     }
595*38e8c45fSAndroid Build Coastguard Worker 
596*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
597*38e8c45fSAndroid Build Coastguard Worker }
598*38e8c45fSAndroid Build Coastguard Worker 
listServices(int32_t dumpPriority,std::vector<std::string> * outList)599*38e8c45fSAndroid Build Coastguard Worker Status ServiceManager::listServices(int32_t dumpPriority, std::vector<std::string>* outList) {
600*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC();
601*38e8c45fSAndroid Build Coastguard Worker 
602*38e8c45fSAndroid Build Coastguard Worker     if (!mAccess->canList(mAccess->getCallingContext())) {
603*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
604*38e8c45fSAndroid Build Coastguard Worker     }
605*38e8c45fSAndroid Build Coastguard Worker 
606*38e8c45fSAndroid Build Coastguard Worker     size_t toReserve = 0;
607*38e8c45fSAndroid Build Coastguard Worker     for (auto const& [name, service] : mNameToService) {
608*38e8c45fSAndroid Build Coastguard Worker         (void) name;
609*38e8c45fSAndroid Build Coastguard Worker 
610*38e8c45fSAndroid Build Coastguard Worker         if (service.dumpPriority & dumpPriority) ++toReserve;
611*38e8c45fSAndroid Build Coastguard Worker     }
612*38e8c45fSAndroid Build Coastguard Worker 
613*38e8c45fSAndroid Build Coastguard Worker     CHECK(outList->empty());
614*38e8c45fSAndroid Build Coastguard Worker 
615*38e8c45fSAndroid Build Coastguard Worker     outList->reserve(toReserve);
616*38e8c45fSAndroid Build Coastguard Worker     for (auto const& [name, service] : mNameToService) {
617*38e8c45fSAndroid Build Coastguard Worker         (void) service;
618*38e8c45fSAndroid Build Coastguard Worker 
619*38e8c45fSAndroid Build Coastguard Worker         if (service.dumpPriority & dumpPriority) {
620*38e8c45fSAndroid Build Coastguard Worker             outList->push_back(name);
621*38e8c45fSAndroid Build Coastguard Worker         }
622*38e8c45fSAndroid Build Coastguard Worker     }
623*38e8c45fSAndroid Build Coastguard Worker 
624*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
625*38e8c45fSAndroid Build Coastguard Worker }
626*38e8c45fSAndroid Build Coastguard Worker 
registerForNotifications(const std::string & name,const sp<IServiceCallback> & callback)627*38e8c45fSAndroid Build Coastguard Worker Status ServiceManager::registerForNotifications(
628*38e8c45fSAndroid Build Coastguard Worker         const std::string& name, const sp<IServiceCallback>& callback) {
629*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
630*38e8c45fSAndroid Build Coastguard Worker             PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
631*38e8c45fSAndroid Build Coastguard Worker 
632*38e8c45fSAndroid Build Coastguard Worker     auto ctx = mAccess->getCallingContext();
633*38e8c45fSAndroid Build Coastguard Worker 
634*38e8c45fSAndroid Build Coastguard Worker     // TODO(b/338541373): Implement the notification mechanism for services accessed via
635*38e8c45fSAndroid Build Coastguard Worker     // IAccessor.
636*38e8c45fSAndroid Build Coastguard Worker     std::optional<std::string> accessorName;
637*38e8c45fSAndroid Build Coastguard Worker     if (auto status = canFindService(ctx, name, &accessorName); !status.isOk()) {
638*38e8c45fSAndroid Build Coastguard Worker         return status;
639*38e8c45fSAndroid Build Coastguard Worker     }
640*38e8c45fSAndroid Build Coastguard Worker 
641*38e8c45fSAndroid Build Coastguard Worker     // note - we could allow isolated apps to get notifications if we
642*38e8c45fSAndroid Build Coastguard Worker     // keep track of isolated callbacks and non-isolated callbacks, but
643*38e8c45fSAndroid Build Coastguard Worker     // this is done since isolated apps shouldn't access lazy services
644*38e8c45fSAndroid Build Coastguard Worker     // so we should be able to use different APIs to keep things simple.
645*38e8c45fSAndroid Build Coastguard Worker     // Here, we disallow everything, because the service might not be
646*38e8c45fSAndroid Build Coastguard Worker     // registered yet.
647*38e8c45fSAndroid Build Coastguard Worker     if (is_multiuser_uid_isolated(ctx.uid)) {
648*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_SECURITY, "isolated app");
649*38e8c45fSAndroid Build Coastguard Worker     }
650*38e8c45fSAndroid Build Coastguard Worker 
651*38e8c45fSAndroid Build Coastguard Worker     if (!isValidServiceName(name)) {
652*38e8c45fSAndroid Build Coastguard Worker         ALOGE("%s Invalid service name: %s", ctx.toDebugString().c_str(), name.c_str());
653*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Invalid service name.");
654*38e8c45fSAndroid Build Coastguard Worker     }
655*38e8c45fSAndroid Build Coastguard Worker 
656*38e8c45fSAndroid Build Coastguard Worker     if (callback == nullptr) {
657*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_NULL_POINTER, "Null callback.");
658*38e8c45fSAndroid Build Coastguard Worker     }
659*38e8c45fSAndroid Build Coastguard Worker 
660*38e8c45fSAndroid Build Coastguard Worker     if (OK !=
661*38e8c45fSAndroid Build Coastguard Worker         IInterface::asBinder(callback)->linkToDeath(
662*38e8c45fSAndroid Build Coastguard Worker                 sp<ServiceManager>::fromExisting(this))) {
663*38e8c45fSAndroid Build Coastguard Worker         ALOGE("%s Could not linkToDeath when adding %s", ctx.toDebugString().c_str(), name.c_str());
664*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't link to death.");
665*38e8c45fSAndroid Build Coastguard Worker     }
666*38e8c45fSAndroid Build Coastguard Worker 
667*38e8c45fSAndroid Build Coastguard Worker     mNameToRegistrationCallback[name].push_back(callback);
668*38e8c45fSAndroid Build Coastguard Worker 
669*38e8c45fSAndroid Build Coastguard Worker     if (auto it = mNameToService.find(name); it != mNameToService.end()) {
670*38e8c45fSAndroid Build Coastguard Worker         const sp<IBinder>& binder = it->second.binder;
671*38e8c45fSAndroid Build Coastguard Worker 
672*38e8c45fSAndroid Build Coastguard Worker         // never null if an entry exists
673*38e8c45fSAndroid Build Coastguard Worker         CHECK(binder != nullptr) << name;
674*38e8c45fSAndroid Build Coastguard Worker         callback->onRegistration(name, binder);
675*38e8c45fSAndroid Build Coastguard Worker     }
676*38e8c45fSAndroid Build Coastguard Worker 
677*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
678*38e8c45fSAndroid Build Coastguard Worker }
unregisterForNotifications(const std::string & name,const sp<IServiceCallback> & callback)679*38e8c45fSAndroid Build Coastguard Worker Status ServiceManager::unregisterForNotifications(
680*38e8c45fSAndroid Build Coastguard Worker         const std::string& name, const sp<IServiceCallback>& callback) {
681*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
682*38e8c45fSAndroid Build Coastguard Worker             PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
683*38e8c45fSAndroid Build Coastguard Worker 
684*38e8c45fSAndroid Build Coastguard Worker     auto ctx = mAccess->getCallingContext();
685*38e8c45fSAndroid Build Coastguard Worker 
686*38e8c45fSAndroid Build Coastguard Worker     std::optional<std::string> accessorName;
687*38e8c45fSAndroid Build Coastguard Worker     if (auto status = canFindService(ctx, name, &accessorName); !status.isOk()) {
688*38e8c45fSAndroid Build Coastguard Worker         return status;
689*38e8c45fSAndroid Build Coastguard Worker     }
690*38e8c45fSAndroid Build Coastguard Worker 
691*38e8c45fSAndroid Build Coastguard Worker     bool found = false;
692*38e8c45fSAndroid Build Coastguard Worker 
693*38e8c45fSAndroid Build Coastguard Worker     auto it = mNameToRegistrationCallback.find(name);
694*38e8c45fSAndroid Build Coastguard Worker     if (it != mNameToRegistrationCallback.end()) {
695*38e8c45fSAndroid Build Coastguard Worker         removeRegistrationCallback(IInterface::asBinder(callback), &it, &found);
696*38e8c45fSAndroid Build Coastguard Worker     }
697*38e8c45fSAndroid Build Coastguard Worker 
698*38e8c45fSAndroid Build Coastguard Worker     if (!found) {
699*38e8c45fSAndroid Build Coastguard Worker         ALOGE("%s Trying to unregister callback, but none exists %s", ctx.toDebugString().c_str(),
700*38e8c45fSAndroid Build Coastguard Worker               name.c_str());
701*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Nothing to unregister.");
702*38e8c45fSAndroid Build Coastguard Worker     }
703*38e8c45fSAndroid Build Coastguard Worker 
704*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
705*38e8c45fSAndroid Build Coastguard Worker }
706*38e8c45fSAndroid Build Coastguard Worker 
isDeclared(const std::string & name,bool * outReturn)707*38e8c45fSAndroid Build Coastguard Worker Status ServiceManager::isDeclared(const std::string& name, bool* outReturn) {
708*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
709*38e8c45fSAndroid Build Coastguard Worker             PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
710*38e8c45fSAndroid Build Coastguard Worker 
711*38e8c45fSAndroid Build Coastguard Worker     auto ctx = mAccess->getCallingContext();
712*38e8c45fSAndroid Build Coastguard Worker 
713*38e8c45fSAndroid Build Coastguard Worker     std::optional<std::string> accessorName;
714*38e8c45fSAndroid Build Coastguard Worker     if (auto status = canFindService(ctx, name, &accessorName); !status.isOk()) {
715*38e8c45fSAndroid Build Coastguard Worker         return status;
716*38e8c45fSAndroid Build Coastguard Worker     }
717*38e8c45fSAndroid Build Coastguard Worker 
718*38e8c45fSAndroid Build Coastguard Worker     *outReturn = false;
719*38e8c45fSAndroid Build Coastguard Worker 
720*38e8c45fSAndroid Build Coastguard Worker #ifndef VENDORSERVICEMANAGER
721*38e8c45fSAndroid Build Coastguard Worker     *outReturn = isVintfDeclared(ctx, name);
722*38e8c45fSAndroid Build Coastguard Worker #endif
723*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
724*38e8c45fSAndroid Build Coastguard Worker }
725*38e8c45fSAndroid Build Coastguard Worker 
getDeclaredInstances(const std::string & interface,std::vector<std::string> * outReturn)726*38e8c45fSAndroid Build Coastguard Worker binder::Status ServiceManager::getDeclaredInstances(const std::string& interface, std::vector<std::string>* outReturn) {
727*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
728*38e8c45fSAndroid Build Coastguard Worker             PERFETTO_TE_PROTO_FIELD_CSTR(kProtoInterfaceName, interface.c_str())));
729*38e8c45fSAndroid Build Coastguard Worker 
730*38e8c45fSAndroid Build Coastguard Worker     auto ctx = mAccess->getCallingContext();
731*38e8c45fSAndroid Build Coastguard Worker 
732*38e8c45fSAndroid Build Coastguard Worker     std::vector<std::string> allInstances;
733*38e8c45fSAndroid Build Coastguard Worker #ifndef VENDORSERVICEMANAGER
734*38e8c45fSAndroid Build Coastguard Worker     allInstances = getVintfInstances(interface);
735*38e8c45fSAndroid Build Coastguard Worker #endif
736*38e8c45fSAndroid Build Coastguard Worker 
737*38e8c45fSAndroid Build Coastguard Worker     outReturn->clear();
738*38e8c45fSAndroid Build Coastguard Worker 
739*38e8c45fSAndroid Build Coastguard Worker     std::optional<std::string> _accessorName;
740*38e8c45fSAndroid Build Coastguard Worker     for (const std::string& instance : allInstances) {
741*38e8c45fSAndroid Build Coastguard Worker         if (auto status = canFindService(ctx, interface + "/" + instance, &_accessorName);
742*38e8c45fSAndroid Build Coastguard Worker             status.isOk()) {
743*38e8c45fSAndroid Build Coastguard Worker             outReturn->push_back(instance);
744*38e8c45fSAndroid Build Coastguard Worker         }
745*38e8c45fSAndroid Build Coastguard Worker     }
746*38e8c45fSAndroid Build Coastguard Worker 
747*38e8c45fSAndroid Build Coastguard Worker     if (outReturn->size() == 0 && allInstances.size() != 0) {
748*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
749*38e8c45fSAndroid Build Coastguard Worker     }
750*38e8c45fSAndroid Build Coastguard Worker 
751*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
752*38e8c45fSAndroid Build Coastguard Worker }
753*38e8c45fSAndroid Build Coastguard Worker 
updatableViaApex(const std::string & name,std::optional<std::string> * outReturn)754*38e8c45fSAndroid Build Coastguard Worker Status ServiceManager::updatableViaApex(const std::string& name,
755*38e8c45fSAndroid Build Coastguard Worker                                         std::optional<std::string>* outReturn) {
756*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
757*38e8c45fSAndroid Build Coastguard Worker             PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
758*38e8c45fSAndroid Build Coastguard Worker 
759*38e8c45fSAndroid Build Coastguard Worker     auto ctx = mAccess->getCallingContext();
760*38e8c45fSAndroid Build Coastguard Worker 
761*38e8c45fSAndroid Build Coastguard Worker     std::optional<std::string> _accessorName;
762*38e8c45fSAndroid Build Coastguard Worker     if (auto status = canFindService(ctx, name, &_accessorName); !status.isOk()) {
763*38e8c45fSAndroid Build Coastguard Worker         return status;
764*38e8c45fSAndroid Build Coastguard Worker     }
765*38e8c45fSAndroid Build Coastguard Worker 
766*38e8c45fSAndroid Build Coastguard Worker     *outReturn = std::nullopt;
767*38e8c45fSAndroid Build Coastguard Worker 
768*38e8c45fSAndroid Build Coastguard Worker #ifndef VENDORSERVICEMANAGER
769*38e8c45fSAndroid Build Coastguard Worker     *outReturn = getVintfUpdatableApex(name);
770*38e8c45fSAndroid Build Coastguard Worker #endif
771*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
772*38e8c45fSAndroid Build Coastguard Worker }
773*38e8c45fSAndroid Build Coastguard Worker 
getUpdatableNames(const std::string & apexName,std::vector<std::string> * outReturn)774*38e8c45fSAndroid Build Coastguard Worker Status ServiceManager::getUpdatableNames([[maybe_unused]] const std::string& apexName,
775*38e8c45fSAndroid Build Coastguard Worker                                          std::vector<std::string>* outReturn) {
776*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
777*38e8c45fSAndroid Build Coastguard Worker             PERFETTO_TE_PROTO_FIELD_CSTR(kProtoApexName, apexName.c_str())));
778*38e8c45fSAndroid Build Coastguard Worker 
779*38e8c45fSAndroid Build Coastguard Worker     auto ctx = mAccess->getCallingContext();
780*38e8c45fSAndroid Build Coastguard Worker 
781*38e8c45fSAndroid Build Coastguard Worker     std::vector<std::string> apexUpdatableNames;
782*38e8c45fSAndroid Build Coastguard Worker #ifndef VENDORSERVICEMANAGER
783*38e8c45fSAndroid Build Coastguard Worker     apexUpdatableNames = getVintfUpdatableNames(apexName);
784*38e8c45fSAndroid Build Coastguard Worker #endif
785*38e8c45fSAndroid Build Coastguard Worker 
786*38e8c45fSAndroid Build Coastguard Worker     outReturn->clear();
787*38e8c45fSAndroid Build Coastguard Worker 
788*38e8c45fSAndroid Build Coastguard Worker     std::optional<std::string> _accessorName;
789*38e8c45fSAndroid Build Coastguard Worker     for (const std::string& name : apexUpdatableNames) {
790*38e8c45fSAndroid Build Coastguard Worker         if (auto status = canFindService(ctx, name, &_accessorName); status.isOk()) {
791*38e8c45fSAndroid Build Coastguard Worker             outReturn->push_back(name);
792*38e8c45fSAndroid Build Coastguard Worker         }
793*38e8c45fSAndroid Build Coastguard Worker     }
794*38e8c45fSAndroid Build Coastguard Worker 
795*38e8c45fSAndroid Build Coastguard Worker     if (outReturn->size() == 0 && apexUpdatableNames.size() != 0) {
796*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
797*38e8c45fSAndroid Build Coastguard Worker     }
798*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
799*38e8c45fSAndroid Build Coastguard Worker }
800*38e8c45fSAndroid Build Coastguard Worker 
getConnectionInfo(const std::string & name,std::optional<ConnectionInfo> * outReturn)801*38e8c45fSAndroid Build Coastguard Worker Status ServiceManager::getConnectionInfo(const std::string& name,
802*38e8c45fSAndroid Build Coastguard Worker                                          std::optional<ConnectionInfo>* outReturn) {
803*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
804*38e8c45fSAndroid Build Coastguard Worker             PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
805*38e8c45fSAndroid Build Coastguard Worker 
806*38e8c45fSAndroid Build Coastguard Worker     auto ctx = mAccess->getCallingContext();
807*38e8c45fSAndroid Build Coastguard Worker 
808*38e8c45fSAndroid Build Coastguard Worker     std::optional<std::string> _accessorName;
809*38e8c45fSAndroid Build Coastguard Worker     if (auto status = canFindService(ctx, name, &_accessorName); !status.isOk()) {
810*38e8c45fSAndroid Build Coastguard Worker         return status;
811*38e8c45fSAndroid Build Coastguard Worker     }
812*38e8c45fSAndroid Build Coastguard Worker 
813*38e8c45fSAndroid Build Coastguard Worker     *outReturn = std::nullopt;
814*38e8c45fSAndroid Build Coastguard Worker 
815*38e8c45fSAndroid Build Coastguard Worker #ifndef VENDORSERVICEMANAGER
816*38e8c45fSAndroid Build Coastguard Worker     *outReturn = getVintfConnectionInfo(name);
817*38e8c45fSAndroid Build Coastguard Worker #endif
818*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
819*38e8c45fSAndroid Build Coastguard Worker }
820*38e8c45fSAndroid Build Coastguard Worker 
removeRegistrationCallback(const wp<IBinder> & who,ServiceCallbackMap::iterator * it,bool * found)821*38e8c45fSAndroid Build Coastguard Worker void ServiceManager::removeRegistrationCallback(const wp<IBinder>& who,
822*38e8c45fSAndroid Build Coastguard Worker                                     ServiceCallbackMap::iterator* it,
823*38e8c45fSAndroid Build Coastguard Worker                                     bool* found) {
824*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC();
825*38e8c45fSAndroid Build Coastguard Worker 
826*38e8c45fSAndroid Build Coastguard Worker     std::vector<sp<IServiceCallback>>& listeners = (*it)->second;
827*38e8c45fSAndroid Build Coastguard Worker 
828*38e8c45fSAndroid Build Coastguard Worker     for (auto lit = listeners.begin(); lit != listeners.end();) {
829*38e8c45fSAndroid Build Coastguard Worker         if (IInterface::asBinder(*lit) == who) {
830*38e8c45fSAndroid Build Coastguard Worker             if(found) *found = true;
831*38e8c45fSAndroid Build Coastguard Worker             lit = listeners.erase(lit);
832*38e8c45fSAndroid Build Coastguard Worker         } else {
833*38e8c45fSAndroid Build Coastguard Worker             ++lit;
834*38e8c45fSAndroid Build Coastguard Worker         }
835*38e8c45fSAndroid Build Coastguard Worker     }
836*38e8c45fSAndroid Build Coastguard Worker 
837*38e8c45fSAndroid Build Coastguard Worker     if (listeners.empty()) {
838*38e8c45fSAndroid Build Coastguard Worker         *it = mNameToRegistrationCallback.erase(*it);
839*38e8c45fSAndroid Build Coastguard Worker     } else {
840*38e8c45fSAndroid Build Coastguard Worker         (*it)++;
841*38e8c45fSAndroid Build Coastguard Worker     }
842*38e8c45fSAndroid Build Coastguard Worker }
843*38e8c45fSAndroid Build Coastguard Worker 
binderDied(const wp<IBinder> & who)844*38e8c45fSAndroid Build Coastguard Worker void ServiceManager::binderDied(const wp<IBinder>& who) {
845*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC();
846*38e8c45fSAndroid Build Coastguard Worker 
847*38e8c45fSAndroid Build Coastguard Worker     for (auto it = mNameToService.begin(); it != mNameToService.end();) {
848*38e8c45fSAndroid Build Coastguard Worker         if (who == it->second.binder) {
849*38e8c45fSAndroid Build Coastguard Worker             // TODO: currently, this entry contains the state also
850*38e8c45fSAndroid Build Coastguard Worker             // associated with mNameToClientCallback. If we allowed
851*38e8c45fSAndroid Build Coastguard Worker             // other processes to register client callbacks, we
852*38e8c45fSAndroid Build Coastguard Worker             // would have to preserve hasClients (perhaps moving
853*38e8c45fSAndroid Build Coastguard Worker             // that state into mNameToClientCallback, which is complicated
854*38e8c45fSAndroid Build Coastguard Worker             // because those callbacks are associated w/ particular binder
855*38e8c45fSAndroid Build Coastguard Worker             // objects, though they are indexed by name now, they may
856*38e8c45fSAndroid Build Coastguard Worker             // need to be indexed by binder at that point).
857*38e8c45fSAndroid Build Coastguard Worker             it = mNameToService.erase(it);
858*38e8c45fSAndroid Build Coastguard Worker         } else {
859*38e8c45fSAndroid Build Coastguard Worker             ++it;
860*38e8c45fSAndroid Build Coastguard Worker         }
861*38e8c45fSAndroid Build Coastguard Worker     }
862*38e8c45fSAndroid Build Coastguard Worker 
863*38e8c45fSAndroid Build Coastguard Worker     for (auto it = mNameToRegistrationCallback.begin(); it != mNameToRegistrationCallback.end();) {
864*38e8c45fSAndroid Build Coastguard Worker         removeRegistrationCallback(who, &it, nullptr /*found*/);
865*38e8c45fSAndroid Build Coastguard Worker     }
866*38e8c45fSAndroid Build Coastguard Worker 
867*38e8c45fSAndroid Build Coastguard Worker     for (auto it = mNameToClientCallback.begin(); it != mNameToClientCallback.end();) {
868*38e8c45fSAndroid Build Coastguard Worker         removeClientCallback(who, &it);
869*38e8c45fSAndroid Build Coastguard Worker     }
870*38e8c45fSAndroid Build Coastguard Worker }
871*38e8c45fSAndroid Build Coastguard Worker 
tryStartService(const Access::CallingContext & ctx,const std::string & name)872*38e8c45fSAndroid Build Coastguard Worker void ServiceManager::tryStartService(const Access::CallingContext& ctx, const std::string& name) {
873*38e8c45fSAndroid Build Coastguard Worker     ALOGI("%s Since '%s' could not be found trying to start it as a lazy AIDL service. (if it's "
874*38e8c45fSAndroid Build Coastguard Worker           "not configured to be a lazy service, it may be stuck starting or still starting).",
875*38e8c45fSAndroid Build Coastguard Worker           ctx.toDebugString().c_str(), name.c_str());
876*38e8c45fSAndroid Build Coastguard Worker 
877*38e8c45fSAndroid Build Coastguard Worker     std::thread([=] {
878*38e8c45fSAndroid Build Coastguard Worker         if (!base::SetProperty("ctl.interface_start", "aidl/" + name)) {
879*38e8c45fSAndroid Build Coastguard Worker             ALOGI("%s Tried to start aidl service %s as a lazy service, but was unable to. Usually "
880*38e8c45fSAndroid Build Coastguard Worker                   "this happens when a service is not installed, but if the service is intended to "
881*38e8c45fSAndroid Build Coastguard Worker                   "be used as a lazy service, then it may be configured incorrectly.",
882*38e8c45fSAndroid Build Coastguard Worker                   ctx.toDebugString().c_str(), name.c_str());
883*38e8c45fSAndroid Build Coastguard Worker         }
884*38e8c45fSAndroid Build Coastguard Worker     }).detach();
885*38e8c45fSAndroid Build Coastguard Worker }
886*38e8c45fSAndroid Build Coastguard Worker 
registerClientCallback(const std::string & name,const sp<IBinder> & service,const sp<IClientCallback> & cb)887*38e8c45fSAndroid Build Coastguard Worker Status ServiceManager::registerClientCallback(const std::string& name, const sp<IBinder>& service,
888*38e8c45fSAndroid Build Coastguard Worker                                               const sp<IClientCallback>& cb) {
889*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
890*38e8c45fSAndroid Build Coastguard Worker             PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
891*38e8c45fSAndroid Build Coastguard Worker 
892*38e8c45fSAndroid Build Coastguard Worker     if (cb == nullptr) {
893*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_NULL_POINTER, "Callback null.");
894*38e8c45fSAndroid Build Coastguard Worker     }
895*38e8c45fSAndroid Build Coastguard Worker 
896*38e8c45fSAndroid Build Coastguard Worker     auto ctx = mAccess->getCallingContext();
897*38e8c45fSAndroid Build Coastguard Worker     std::optional<std::string> accessorName;
898*38e8c45fSAndroid Build Coastguard Worker     if (auto status = canAddService(ctx, name, &accessorName); !status.isOk()) {
899*38e8c45fSAndroid Build Coastguard Worker         return status;
900*38e8c45fSAndroid Build Coastguard Worker     }
901*38e8c45fSAndroid Build Coastguard Worker 
902*38e8c45fSAndroid Build Coastguard Worker     auto serviceIt = mNameToService.find(name);
903*38e8c45fSAndroid Build Coastguard Worker     if (serviceIt == mNameToService.end()) {
904*38e8c45fSAndroid Build Coastguard Worker         ALOGE("%s Could not add callback for nonexistent service: %s", ctx.toDebugString().c_str(),
905*38e8c45fSAndroid Build Coastguard Worker               name.c_str());
906*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Service doesn't exist.");
907*38e8c45fSAndroid Build Coastguard Worker     }
908*38e8c45fSAndroid Build Coastguard Worker 
909*38e8c45fSAndroid Build Coastguard Worker     if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) {
910*38e8c45fSAndroid Build Coastguard Worker         ALOGW("%s Only a server can register for client callbacks (for %s)",
911*38e8c45fSAndroid Build Coastguard Worker               ctx.toDebugString().c_str(), name.c_str());
912*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
913*38e8c45fSAndroid Build Coastguard Worker                                          "Only service can register client callback for itself.");
914*38e8c45fSAndroid Build Coastguard Worker     }
915*38e8c45fSAndroid Build Coastguard Worker 
916*38e8c45fSAndroid Build Coastguard Worker     if (serviceIt->second.binder != service) {
917*38e8c45fSAndroid Build Coastguard Worker         ALOGW("%s Tried to register client callback for %s but a different service is registered "
918*38e8c45fSAndroid Build Coastguard Worker               "under this name.",
919*38e8c45fSAndroid Build Coastguard Worker               ctx.toDebugString().c_str(), name.c_str());
920*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT, "Service mismatch.");
921*38e8c45fSAndroid Build Coastguard Worker     }
922*38e8c45fSAndroid Build Coastguard Worker 
923*38e8c45fSAndroid Build Coastguard Worker     if (OK !=
924*38e8c45fSAndroid Build Coastguard Worker         IInterface::asBinder(cb)->linkToDeath(sp<ServiceManager>::fromExisting(this))) {
925*38e8c45fSAndroid Build Coastguard Worker         ALOGE("%s Could not linkToDeath when adding client callback for %s",
926*38e8c45fSAndroid Build Coastguard Worker               ctx.toDebugString().c_str(), name.c_str());
927*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Couldn't linkToDeath.");
928*38e8c45fSAndroid Build Coastguard Worker     }
929*38e8c45fSAndroid Build Coastguard Worker 
930*38e8c45fSAndroid Build Coastguard Worker     // WARNING: binderDied makes an assumption about this. If we open up client
931*38e8c45fSAndroid Build Coastguard Worker     // callbacks to other services, certain race conditions may lead to services
932*38e8c45fSAndroid Build Coastguard Worker     // getting extra client callback notifications.
933*38e8c45fSAndroid Build Coastguard Worker     // Make sure all callbacks have been told about a consistent state - b/278038751
934*38e8c45fSAndroid Build Coastguard Worker     if (serviceIt->second.hasClients) {
935*38e8c45fSAndroid Build Coastguard Worker         cb->onClients(service, true);
936*38e8c45fSAndroid Build Coastguard Worker     }
937*38e8c45fSAndroid Build Coastguard Worker 
938*38e8c45fSAndroid Build Coastguard Worker     mNameToClientCallback[name].push_back(cb);
939*38e8c45fSAndroid Build Coastguard Worker 
940*38e8c45fSAndroid Build Coastguard Worker     // Flush updated info to client callbacks (especially if guaranteeClient
941*38e8c45fSAndroid Build Coastguard Worker     // and !hasClient, see b/285202885). We may or may not have clients at
942*38e8c45fSAndroid Build Coastguard Worker     // this point, so ignore the return value.
943*38e8c45fSAndroid Build Coastguard Worker     (void)handleServiceClientCallback(2 /* sm + transaction */, name, false);
944*38e8c45fSAndroid Build Coastguard Worker 
945*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
946*38e8c45fSAndroid Build Coastguard Worker }
947*38e8c45fSAndroid Build Coastguard Worker 
removeClientCallback(const wp<IBinder> & who,ClientCallbackMap::iterator * it)948*38e8c45fSAndroid Build Coastguard Worker void ServiceManager::removeClientCallback(const wp<IBinder>& who,
949*38e8c45fSAndroid Build Coastguard Worker                                           ClientCallbackMap::iterator* it) {
950*38e8c45fSAndroid Build Coastguard Worker     std::vector<sp<IClientCallback>>& listeners = (*it)->second;
951*38e8c45fSAndroid Build Coastguard Worker 
952*38e8c45fSAndroid Build Coastguard Worker     for (auto lit = listeners.begin(); lit != listeners.end();) {
953*38e8c45fSAndroid Build Coastguard Worker         if (IInterface::asBinder(*lit) == who) {
954*38e8c45fSAndroid Build Coastguard Worker             lit = listeners.erase(lit);
955*38e8c45fSAndroid Build Coastguard Worker         } else {
956*38e8c45fSAndroid Build Coastguard Worker             ++lit;
957*38e8c45fSAndroid Build Coastguard Worker         }
958*38e8c45fSAndroid Build Coastguard Worker     }
959*38e8c45fSAndroid Build Coastguard Worker 
960*38e8c45fSAndroid Build Coastguard Worker     if (listeners.empty()) {
961*38e8c45fSAndroid Build Coastguard Worker         *it = mNameToClientCallback.erase(*it);
962*38e8c45fSAndroid Build Coastguard Worker     } else {
963*38e8c45fSAndroid Build Coastguard Worker         (*it)++;
964*38e8c45fSAndroid Build Coastguard Worker     }
965*38e8c45fSAndroid Build Coastguard Worker }
966*38e8c45fSAndroid Build Coastguard Worker 
getNodeStrongRefCount()967*38e8c45fSAndroid Build Coastguard Worker ssize_t ServiceManager::Service::getNodeStrongRefCount() {
968*38e8c45fSAndroid Build Coastguard Worker     sp<BpBinder> bpBinder = sp<BpBinder>::fromExisting(binder->remoteBinder());
969*38e8c45fSAndroid Build Coastguard Worker     if (bpBinder == nullptr) return -1;
970*38e8c45fSAndroid Build Coastguard Worker 
971*38e8c45fSAndroid Build Coastguard Worker     return ProcessState::self()->getStrongRefCountForNode(bpBinder);
972*38e8c45fSAndroid Build Coastguard Worker }
973*38e8c45fSAndroid Build Coastguard Worker 
handleClientCallbacks()974*38e8c45fSAndroid Build Coastguard Worker void ServiceManager::handleClientCallbacks() {
975*38e8c45fSAndroid Build Coastguard Worker     for (const auto& [name, service] : mNameToService) {
976*38e8c45fSAndroid Build Coastguard Worker         handleServiceClientCallback(1 /* sm has one refcount */, name, true);
977*38e8c45fSAndroid Build Coastguard Worker     }
978*38e8c45fSAndroid Build Coastguard Worker }
979*38e8c45fSAndroid Build Coastguard Worker 
handleServiceClientCallback(size_t knownClients,const std::string & serviceName,bool isCalledOnInterval)980*38e8c45fSAndroid Build Coastguard Worker bool ServiceManager::handleServiceClientCallback(size_t knownClients,
981*38e8c45fSAndroid Build Coastguard Worker                                                  const std::string& serviceName,
982*38e8c45fSAndroid Build Coastguard Worker                                                  bool isCalledOnInterval) {
983*38e8c45fSAndroid Build Coastguard Worker     auto serviceIt = mNameToService.find(serviceName);
984*38e8c45fSAndroid Build Coastguard Worker     if (serviceIt == mNameToService.end() || mNameToClientCallback.count(serviceName) < 1) {
985*38e8c45fSAndroid Build Coastguard Worker         return true; // return we do have clients a.k.a. DON'T DO ANYTHING
986*38e8c45fSAndroid Build Coastguard Worker     }
987*38e8c45fSAndroid Build Coastguard Worker 
988*38e8c45fSAndroid Build Coastguard Worker     Service& service = serviceIt->second;
989*38e8c45fSAndroid Build Coastguard Worker     ssize_t count = service.getNodeStrongRefCount();
990*38e8c45fSAndroid Build Coastguard Worker 
991*38e8c45fSAndroid Build Coastguard Worker     // binder driver doesn't support this feature, consider we have clients
992*38e8c45fSAndroid Build Coastguard Worker     if (count == -1) return true;
993*38e8c45fSAndroid Build Coastguard Worker 
994*38e8c45fSAndroid Build Coastguard Worker     bool hasKernelReportedClients = static_cast<size_t>(count) > knownClients;
995*38e8c45fSAndroid Build Coastguard Worker 
996*38e8c45fSAndroid Build Coastguard Worker     if (service.guaranteeClient) {
997*38e8c45fSAndroid Build Coastguard Worker         if (!service.hasClients && !hasKernelReportedClients) {
998*38e8c45fSAndroid Build Coastguard Worker             sendClientCallbackNotifications(serviceName, true,
999*38e8c45fSAndroid Build Coastguard Worker                                             "service is guaranteed to be in use");
1000*38e8c45fSAndroid Build Coastguard Worker         }
1001*38e8c45fSAndroid Build Coastguard Worker 
1002*38e8c45fSAndroid Build Coastguard Worker         // guarantee is temporary
1003*38e8c45fSAndroid Build Coastguard Worker         service.guaranteeClient = false;
1004*38e8c45fSAndroid Build Coastguard Worker     }
1005*38e8c45fSAndroid Build Coastguard Worker 
1006*38e8c45fSAndroid Build Coastguard Worker     // Regardless of this situation, we want to give this notification as soon as possible.
1007*38e8c45fSAndroid Build Coastguard Worker     // This way, we have a chance of preventing further thrashing.
1008*38e8c45fSAndroid Build Coastguard Worker     if (hasKernelReportedClients && !service.hasClients) {
1009*38e8c45fSAndroid Build Coastguard Worker         sendClientCallbackNotifications(serviceName, true, "we now have a record of a client");
1010*38e8c45fSAndroid Build Coastguard Worker     }
1011*38e8c45fSAndroid Build Coastguard Worker 
1012*38e8c45fSAndroid Build Coastguard Worker     // But limit rate of shutting down service.
1013*38e8c45fSAndroid Build Coastguard Worker     if (isCalledOnInterval) {
1014*38e8c45fSAndroid Build Coastguard Worker         if (!hasKernelReportedClients && service.hasClients) {
1015*38e8c45fSAndroid Build Coastguard Worker             sendClientCallbackNotifications(serviceName, false,
1016*38e8c45fSAndroid Build Coastguard Worker                                             "we now have no record of a client");
1017*38e8c45fSAndroid Build Coastguard Worker         }
1018*38e8c45fSAndroid Build Coastguard Worker     }
1019*38e8c45fSAndroid Build Coastguard Worker 
1020*38e8c45fSAndroid Build Coastguard Worker     // May be different than 'hasKernelReportedClients'. We intentionally delay
1021*38e8c45fSAndroid Build Coastguard Worker     // information about clients going away to reduce thrashing.
1022*38e8c45fSAndroid Build Coastguard Worker     return service.hasClients;
1023*38e8c45fSAndroid Build Coastguard Worker }
1024*38e8c45fSAndroid Build Coastguard Worker 
sendClientCallbackNotifications(const std::string & serviceName,bool hasClients,const char * context)1025*38e8c45fSAndroid Build Coastguard Worker void ServiceManager::sendClientCallbackNotifications(const std::string& serviceName,
1026*38e8c45fSAndroid Build Coastguard Worker                                                      bool hasClients, const char* context) {
1027*38e8c45fSAndroid Build Coastguard Worker     auto serviceIt = mNameToService.find(serviceName);
1028*38e8c45fSAndroid Build Coastguard Worker     if (serviceIt == mNameToService.end()) {
1029*38e8c45fSAndroid Build Coastguard Worker         ALOGW("sendClientCallbackNotifications could not find service %s when %s",
1030*38e8c45fSAndroid Build Coastguard Worker               serviceName.c_str(), context);
1031*38e8c45fSAndroid Build Coastguard Worker         return;
1032*38e8c45fSAndroid Build Coastguard Worker     }
1033*38e8c45fSAndroid Build Coastguard Worker     Service& service = serviceIt->second;
1034*38e8c45fSAndroid Build Coastguard Worker 
1035*38e8c45fSAndroid Build Coastguard Worker     CHECK_NE(hasClients, service.hasClients) << context;
1036*38e8c45fSAndroid Build Coastguard Worker 
1037*38e8c45fSAndroid Build Coastguard Worker     ALOGI("Notifying %s they %s (previously: %s) have clients when %s", serviceName.c_str(),
1038*38e8c45fSAndroid Build Coastguard Worker           hasClients ? "do" : "don't", service.hasClients ? "do" : "don't", context);
1039*38e8c45fSAndroid Build Coastguard Worker 
1040*38e8c45fSAndroid Build Coastguard Worker     auto ccIt = mNameToClientCallback.find(serviceName);
1041*38e8c45fSAndroid Build Coastguard Worker     CHECK(ccIt != mNameToClientCallback.end())
1042*38e8c45fSAndroid Build Coastguard Worker             << "sendClientCallbackNotifications could not find callbacks for service when "
1043*38e8c45fSAndroid Build Coastguard Worker             << context;
1044*38e8c45fSAndroid Build Coastguard Worker 
1045*38e8c45fSAndroid Build Coastguard Worker     for (const auto& callback : ccIt->second) {
1046*38e8c45fSAndroid Build Coastguard Worker         callback->onClients(service.binder, hasClients);
1047*38e8c45fSAndroid Build Coastguard Worker     }
1048*38e8c45fSAndroid Build Coastguard Worker 
1049*38e8c45fSAndroid Build Coastguard Worker     service.hasClients = hasClients;
1050*38e8c45fSAndroid Build Coastguard Worker }
1051*38e8c45fSAndroid Build Coastguard Worker 
tryUnregisterService(const std::string & name,const sp<IBinder> & binder)1052*38e8c45fSAndroid Build Coastguard Worker Status ServiceManager::tryUnregisterService(const std::string& name, const sp<IBinder>& binder) {
1053*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC(PERFETTO_TE_PROTO_FIELDS(
1054*38e8c45fSAndroid Build Coastguard Worker             PERFETTO_TE_PROTO_FIELD_CSTR(kProtoServiceName, name.c_str())));
1055*38e8c45fSAndroid Build Coastguard Worker 
1056*38e8c45fSAndroid Build Coastguard Worker     if (binder == nullptr) {
1057*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_NULL_POINTER, "Null service.");
1058*38e8c45fSAndroid Build Coastguard Worker     }
1059*38e8c45fSAndroid Build Coastguard Worker 
1060*38e8c45fSAndroid Build Coastguard Worker     auto ctx = mAccess->getCallingContext();
1061*38e8c45fSAndroid Build Coastguard Worker     std::optional<std::string> accessorName;
1062*38e8c45fSAndroid Build Coastguard Worker     if (auto status = canAddService(ctx, name, &accessorName); !status.isOk()) {
1063*38e8c45fSAndroid Build Coastguard Worker         return status;
1064*38e8c45fSAndroid Build Coastguard Worker     }
1065*38e8c45fSAndroid Build Coastguard Worker 
1066*38e8c45fSAndroid Build Coastguard Worker     auto serviceIt = mNameToService.find(name);
1067*38e8c45fSAndroid Build Coastguard Worker     if (serviceIt == mNameToService.end()) {
1068*38e8c45fSAndroid Build Coastguard Worker         ALOGW("%s Tried to unregister %s, but that service wasn't registered to begin with.",
1069*38e8c45fSAndroid Build Coastguard Worker               ctx.toDebugString().c_str(), name.c_str());
1070*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE, "Service not registered.");
1071*38e8c45fSAndroid Build Coastguard Worker     }
1072*38e8c45fSAndroid Build Coastguard Worker 
1073*38e8c45fSAndroid Build Coastguard Worker     if (serviceIt->second.ctx.debugPid != IPCThreadState::self()->getCallingPid()) {
1074*38e8c45fSAndroid Build Coastguard Worker         ALOGW("%s Only a server can unregister itself (for %s)", ctx.toDebugString().c_str(),
1075*38e8c45fSAndroid Build Coastguard Worker               name.c_str());
1076*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_UNSUPPORTED_OPERATION,
1077*38e8c45fSAndroid Build Coastguard Worker                                          "Service can only unregister itself.");
1078*38e8c45fSAndroid Build Coastguard Worker     }
1079*38e8c45fSAndroid Build Coastguard Worker 
1080*38e8c45fSAndroid Build Coastguard Worker     sp<IBinder> storedBinder = serviceIt->second.binder;
1081*38e8c45fSAndroid Build Coastguard Worker 
1082*38e8c45fSAndroid Build Coastguard Worker     if (binder != storedBinder) {
1083*38e8c45fSAndroid Build Coastguard Worker         ALOGW("%s Tried to unregister %s, but a different service is registered under this name.",
1084*38e8c45fSAndroid Build Coastguard Worker               ctx.toDebugString().c_str(), name.c_str());
1085*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE,
1086*38e8c45fSAndroid Build Coastguard Worker                                          "Different service registered under this name.");
1087*38e8c45fSAndroid Build Coastguard Worker     }
1088*38e8c45fSAndroid Build Coastguard Worker 
1089*38e8c45fSAndroid Build Coastguard Worker     // important because we don't have timer-based guarantees, we don't want to clear
1090*38e8c45fSAndroid Build Coastguard Worker     // this
1091*38e8c45fSAndroid Build Coastguard Worker     if (serviceIt->second.guaranteeClient) {
1092*38e8c45fSAndroid Build Coastguard Worker         ALOGI("%s Tried to unregister %s, but there is about to be a client.",
1093*38e8c45fSAndroid Build Coastguard Worker               ctx.toDebugString().c_str(), name.c_str());
1094*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE,
1095*38e8c45fSAndroid Build Coastguard Worker                                          "Can't unregister, pending client.");
1096*38e8c45fSAndroid Build Coastguard Worker     }
1097*38e8c45fSAndroid Build Coastguard Worker 
1098*38e8c45fSAndroid Build Coastguard Worker     // - kernel driver will hold onto one refcount (during this transaction)
1099*38e8c45fSAndroid Build Coastguard Worker     // - servicemanager has a refcount (guaranteed by this transaction)
1100*38e8c45fSAndroid Build Coastguard Worker     constexpr size_t kKnownClients = 2;
1101*38e8c45fSAndroid Build Coastguard Worker 
1102*38e8c45fSAndroid Build Coastguard Worker     if (handleServiceClientCallback(kKnownClients, name, false)) {
1103*38e8c45fSAndroid Build Coastguard Worker         ALOGI("%s Tried to unregister %s, but there are clients.", ctx.toDebugString().c_str(),
1104*38e8c45fSAndroid Build Coastguard Worker               name.c_str());
1105*38e8c45fSAndroid Build Coastguard Worker 
1106*38e8c45fSAndroid Build Coastguard Worker         // Since we had a failed registration attempt, and the HIDL implementation of
1107*38e8c45fSAndroid Build Coastguard Worker         // delaying service shutdown for multiple periods wasn't ported here... this may
1108*38e8c45fSAndroid Build Coastguard Worker         // help reduce thrashing, but we should be able to remove it.
1109*38e8c45fSAndroid Build Coastguard Worker         serviceIt->second.guaranteeClient = true;
1110*38e8c45fSAndroid Build Coastguard Worker 
1111*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE,
1112*38e8c45fSAndroid Build Coastguard Worker                                          "Can't unregister, known client.");
1113*38e8c45fSAndroid Build Coastguard Worker     }
1114*38e8c45fSAndroid Build Coastguard Worker 
1115*38e8c45fSAndroid Build Coastguard Worker     ALOGI("%s Unregistering %s", ctx.toDebugString().c_str(), name.c_str());
1116*38e8c45fSAndroid Build Coastguard Worker     mNameToService.erase(name);
1117*38e8c45fSAndroid Build Coastguard Worker 
1118*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
1119*38e8c45fSAndroid Build Coastguard Worker }
1120*38e8c45fSAndroid Build Coastguard Worker 
canAddService(const Access::CallingContext & ctx,const std::string & name,std::optional<std::string> * accessor)1121*38e8c45fSAndroid Build Coastguard Worker Status ServiceManager::canAddService(const Access::CallingContext& ctx, const std::string& name,
1122*38e8c45fSAndroid Build Coastguard Worker                                      std::optional<std::string>* accessor) {
1123*38e8c45fSAndroid Build Coastguard Worker     if (!mAccess->canAdd(ctx, name)) {
1124*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied for service.");
1125*38e8c45fSAndroid Build Coastguard Worker     }
1126*38e8c45fSAndroid Build Coastguard Worker #ifndef VENDORSERVICEMANAGER
1127*38e8c45fSAndroid Build Coastguard Worker     *accessor = getVintfAccessorName(name);
1128*38e8c45fSAndroid Build Coastguard Worker #endif
1129*38e8c45fSAndroid Build Coastguard Worker     if (accessor->has_value()) {
1130*38e8c45fSAndroid Build Coastguard Worker         if (!mAccess->canAdd(ctx, accessor->value())) {
1131*38e8c45fSAndroid Build Coastguard Worker             return Status::fromExceptionCode(Status::EX_SECURITY,
1132*38e8c45fSAndroid Build Coastguard Worker                                              "SELinux denied for the accessor of the service.");
1133*38e8c45fSAndroid Build Coastguard Worker         }
1134*38e8c45fSAndroid Build Coastguard Worker     }
1135*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
1136*38e8c45fSAndroid Build Coastguard Worker }
1137*38e8c45fSAndroid Build Coastguard Worker 
canFindService(const Access::CallingContext & ctx,const std::string & name,std::optional<std::string> * accessor)1138*38e8c45fSAndroid Build Coastguard Worker Status ServiceManager::canFindService(const Access::CallingContext& ctx, const std::string& name,
1139*38e8c45fSAndroid Build Coastguard Worker                                       std::optional<std::string>* accessor) {
1140*38e8c45fSAndroid Build Coastguard Worker     if (!mAccess->canFind(ctx, name)) {
1141*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied for service.");
1142*38e8c45fSAndroid Build Coastguard Worker     }
1143*38e8c45fSAndroid Build Coastguard Worker #ifndef VENDORSERVICEMANAGER
1144*38e8c45fSAndroid Build Coastguard Worker     *accessor = getVintfAccessorName(name);
1145*38e8c45fSAndroid Build Coastguard Worker #endif
1146*38e8c45fSAndroid Build Coastguard Worker     if (accessor->has_value()) {
1147*38e8c45fSAndroid Build Coastguard Worker         if (!mAccess->canFind(ctx, accessor->value())) {
1148*38e8c45fSAndroid Build Coastguard Worker             return Status::fromExceptionCode(Status::EX_SECURITY,
1149*38e8c45fSAndroid Build Coastguard Worker                                              "SELinux denied for the accessor of the service.");
1150*38e8c45fSAndroid Build Coastguard Worker         }
1151*38e8c45fSAndroid Build Coastguard Worker     }
1152*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
1153*38e8c45fSAndroid Build Coastguard Worker }
1154*38e8c45fSAndroid Build Coastguard Worker 
getServiceDebugInfo(std::vector<ServiceDebugInfo> * outReturn)1155*38e8c45fSAndroid Build Coastguard Worker Status ServiceManager::getServiceDebugInfo(std::vector<ServiceDebugInfo>* outReturn) {
1156*38e8c45fSAndroid Build Coastguard Worker     SM_PERFETTO_TRACE_FUNC();
1157*38e8c45fSAndroid Build Coastguard Worker     if (!mAccess->canList(mAccess->getCallingContext())) {
1158*38e8c45fSAndroid Build Coastguard Worker         return Status::fromExceptionCode(Status::EX_SECURITY, "SELinux denied.");
1159*38e8c45fSAndroid Build Coastguard Worker     }
1160*38e8c45fSAndroid Build Coastguard Worker 
1161*38e8c45fSAndroid Build Coastguard Worker     outReturn->reserve(mNameToService.size());
1162*38e8c45fSAndroid Build Coastguard Worker     for (auto const& [name, service] : mNameToService) {
1163*38e8c45fSAndroid Build Coastguard Worker         ServiceDebugInfo info;
1164*38e8c45fSAndroid Build Coastguard Worker         info.name = name;
1165*38e8c45fSAndroid Build Coastguard Worker         info.debugPid = service.ctx.debugPid;
1166*38e8c45fSAndroid Build Coastguard Worker 
1167*38e8c45fSAndroid Build Coastguard Worker         outReturn->push_back(std::move(info));
1168*38e8c45fSAndroid Build Coastguard Worker     }
1169*38e8c45fSAndroid Build Coastguard Worker 
1170*38e8c45fSAndroid Build Coastguard Worker     return Status::ok();
1171*38e8c45fSAndroid Build Coastguard Worker }
1172*38e8c45fSAndroid Build Coastguard Worker 
clear()1173*38e8c45fSAndroid Build Coastguard Worker void ServiceManager::clear() {
1174*38e8c45fSAndroid Build Coastguard Worker     mNameToService.clear();
1175*38e8c45fSAndroid Build Coastguard Worker     mNameToRegistrationCallback.clear();
1176*38e8c45fSAndroid Build Coastguard Worker     mNameToClientCallback.clear();
1177*38e8c45fSAndroid Build Coastguard Worker }
1178*38e8c45fSAndroid Build Coastguard Worker 
1179*38e8c45fSAndroid Build Coastguard Worker }  // namespace android
1180