blob: 218970a1b2e92d05c248031366105e845ac704f1 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "ServiceManager"
18
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070019#include <binder/IServiceManager.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
Steven Moreland1c47b582019-08-27 18:05:27 -070021#include <android/os/BnServiceCallback.h>
Steven Moreland80e1e6d2019-06-21 12:35:59 -070022#include <android/os/IServiceManager.h>
Mathias Agopian375f5632009-06-15 18:24:59 -070023#include <binder/IPCThreadState.h>
Steven Moreland28723ae2019-04-01 18:52:30 -070024#include <binder/Parcel.h>
25#include <utils/Log.h>
26#include <utils/String8.h>
27#include <utils/SystemClock.h>
28
Jiyong Park47f876b2018-04-17 13:56:46 +090029#ifndef __ANDROID_VNDK__
30#include <binder/IPermissionController.h>
31#endif
Steven Moreland28723ae2019-04-01 18:52:30 -070032
Steven Morelanded3b5632019-09-20 11:24:28 -070033#ifdef __ANDROID__
Yunfan Chen788e1c42018-03-29 16:52:09 +090034#include <cutils/properties.h>
Steven Moreland28723ae2019-04-01 18:52:30 -070035#endif
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080036
Steven Morelanda4853cd2019-07-12 15:44:37 -070037#include "Static.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080038
39#include <unistd.h>
40
41namespace android {
42
Steven Moreland80e1e6d2019-06-21 12:35:59 -070043using AidlServiceManager = android::os::IServiceManager;
44using android::binder::Status;
45
Steven Moreland635a2912019-10-02 15:50:10 -070046// libbinder's IServiceManager.h can't rely on the values generated by AIDL
47// because many places use its headers via include_dirs (meaning, without
48// declaring the dependency in the build system). So, for now, we can just check
49// the values here.
50static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_CRITICAL == IServiceManager::DUMP_FLAG_PRIORITY_CRITICAL);
51static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_HIGH == IServiceManager::DUMP_FLAG_PRIORITY_HIGH);
52static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_NORMAL == IServiceManager::DUMP_FLAG_PRIORITY_NORMAL);
53static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_DEFAULT == IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT);
54static_assert(AidlServiceManager::DUMP_FLAG_PRIORITY_ALL == IServiceManager::DUMP_FLAG_PRIORITY_ALL);
55static_assert(AidlServiceManager::DUMP_FLAG_PROTO == IServiceManager::DUMP_FLAG_PROTO);
56
Steven Moreland583685e2019-10-02 16:45:11 -070057const String16& IServiceManager::getInterfaceDescriptor() const {
58 return AidlServiceManager::descriptor;
59}
60IServiceManager::IServiceManager() {}
61IServiceManager::~IServiceManager() {}
62
63// From the old libbinder IServiceManager interface to IServiceManager.
64class ServiceManagerShim : public IServiceManager
65{
66public:
67 explicit ServiceManagerShim (const sp<AidlServiceManager>& impl);
68
69 sp<IBinder> getService(const String16& name) const override;
70 sp<IBinder> checkService(const String16& name) const override;
71 status_t addService(const String16& name, const sp<IBinder>& service,
72 bool allowIsolated, int dumpsysPriority) override;
73 Vector<String16> listServices(int dumpsysPriority) override;
74 sp<IBinder> waitForService(const String16& name16) override;
Steven Morelandb82b8f82019-10-28 10:52:34 -070075 bool isDeclared(const String16& name) override;
Steven Moreland583685e2019-10-02 16:45:11 -070076
77 // for legacy ABI
78 const String16& getInterfaceDescriptor() const override {
79 return mTheRealServiceManager->getInterfaceDescriptor();
80 }
81 IBinder* onAsBinder() override {
82 return IInterface::asBinder(mTheRealServiceManager).get();
83 }
84private:
85 sp<AidlServiceManager> mTheRealServiceManager;
86};
87
Steven Moreland26476f32020-05-15 23:43:52 +000088[[clang::no_destroy]] static std::once_flag gSmOnce;
89[[clang::no_destroy]] static sp<IServiceManager> gDefaultServiceManager;
Brett Chabot38dbade2020-01-24 12:59:43 -080090
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091sp<IServiceManager> defaultServiceManager()
92{
Martijn Coenen402c8672020-02-03 10:01:36 +010093 std::call_once(gSmOnce, []() {
94 sp<AidlServiceManager> sm = nullptr;
95 while (sm == nullptr) {
96 sm = interface_cast<AidlServiceManager>(ProcessState::self()->getContextObject(nullptr));
97 if (sm == nullptr) {
Steven Morelandfb10b6b2020-05-19 18:10:07 +000098 ALOGE("Waiting 1s on context object on %s.", ProcessState::self()->getDriverName().c_str());
Todd Poynora7b0f042013-06-18 17:25:37 -070099 sleep(1);
Martijn Coenen402c8672020-02-03 10:01:36 +0100100 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 }
Martijn Coenen402c8672020-02-03 10:01:36 +0100102
103 gDefaultServiceManager = new ServiceManagerShim(sm);
104 });
Daniel Eratc2832702015-10-13 15:29:32 -0600105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 return gDefaultServiceManager;
107}
108
Brett Chabot38dbade2020-01-24 12:59:43 -0800109void setDefaultServiceManager(const sp<IServiceManager>& sm) {
Martijn Coenen402c8672020-02-03 10:01:36 +0100110 bool called = false;
111 std::call_once(gSmOnce, [&]() {
112 gDefaultServiceManager = sm;
113 called = true;
114 });
115
116 if (!called) {
117 LOG_ALWAYS_FATAL("setDefaultServiceManager() called after defaultServiceManager().");
118 }
Brett Chabot38dbade2020-01-24 12:59:43 -0800119}
120
Steven Morelanded3b5632019-09-20 11:24:28 -0700121#if !defined(__ANDROID_VNDK__) && defined(__ANDROID__)
Jiyong Park47f876b2018-04-17 13:56:46 +0900122// IPermissionController is not accessible to vendors
123
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124bool checkCallingPermission(const String16& permission)
125{
Yi Kongfdd8da92018-06-07 17:52:27 -0700126 return checkCallingPermission(permission, nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800127}
128
129static String16 _permission("permission");
130
Mathias Agopian375f5632009-06-15 18:24:59 -0700131
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800132bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid)
133{
134 IPCThreadState* ipcState = IPCThreadState::self();
Mathias Agopian375f5632009-06-15 18:24:59 -0700135 pid_t pid = ipcState->getCallingPid();
136 uid_t uid = ipcState->getCallingUid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 if (outPid) *outPid = pid;
Mathias Agopian375f5632009-06-15 18:24:59 -0700138 if (outUid) *outUid = uid;
139 return checkPermission(permission, pid, uid);
140}
141
142bool checkPermission(const String16& permission, pid_t pid, uid_t uid)
143{
Steven Moreland92b17c62019-04-02 15:46:24 -0700144 static Mutex gPermissionControllerLock;
145 static sp<IPermissionController> gPermissionController;
146
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800147 sp<IPermissionController> pc;
Steven Moreland92b17c62019-04-02 15:46:24 -0700148 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 pc = gPermissionController;
Steven Moreland92b17c62019-04-02 15:46:24 -0700150 gPermissionControllerLock.unlock();
Daniel Eratc2832702015-10-13 15:29:32 -0600151
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 int64_t startTime = 0;
153
154 while (true) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700155 if (pc != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 bool res = pc->checkPermission(permission, pid, uid);
157 if (res) {
158 if (startTime != 0) {
Steve Blocka19954a2012-01-04 20:05:49 +0000159 ALOGI("Check passed after %d seconds for %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800160 (int)((uptimeMillis()-startTime)/1000),
161 String8(permission).string(), uid, pid);
162 }
163 return res;
164 }
Daniel Eratc2832702015-10-13 15:29:32 -0600165
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 // Is this a permission failure, or did the controller go away?
Marco Nelissen097ca272014-11-14 08:01:01 -0800167 if (IInterface::asBinder(pc)->isBinderAlive()) {
Steve Block32397c12012-01-05 23:22:43 +0000168 ALOGW("Permission failure: %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800169 String8(permission).string(), uid, pid);
170 return false;
171 }
Daniel Eratc2832702015-10-13 15:29:32 -0600172
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 // Object is dead!
Steven Moreland92b17c62019-04-02 15:46:24 -0700174 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175 if (gPermissionController == pc) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700176 gPermissionController = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 }
Steven Moreland92b17c62019-04-02 15:46:24 -0700178 gPermissionControllerLock.unlock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 }
Daniel Eratc2832702015-10-13 15:29:32 -0600180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 // Need to retrieve the permission controller.
182 sp<IBinder> binder = defaultServiceManager()->checkService(_permission);
Yi Kongfdd8da92018-06-07 17:52:27 -0700183 if (binder == nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 // Wait for the permission controller to come back...
185 if (startTime == 0) {
186 startTime = uptimeMillis();
Steve Blocka19954a2012-01-04 20:05:49 +0000187 ALOGI("Waiting to check permission %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188 String8(permission).string(), uid, pid);
189 }
190 sleep(1);
191 } else {
192 pc = interface_cast<IPermissionController>(binder);
Daniel Eratc2832702015-10-13 15:29:32 -0600193 // Install the new permission controller, and try again.
Steven Moreland92b17c62019-04-02 15:46:24 -0700194 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 gPermissionController = pc;
Steven Moreland92b17c62019-04-02 15:46:24 -0700196 gPermissionControllerLock.unlock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197 }
198 }
199}
200
Jiyong Park47f876b2018-04-17 13:56:46 +0900201#endif //__ANDROID_VNDK__
202
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203// ----------------------------------------------------------------------
204
Steven Moreland583685e2019-10-02 16:45:11 -0700205ServiceManagerShim::ServiceManagerShim(const sp<AidlServiceManager>& impl)
206 : mTheRealServiceManager(impl)
207{}
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700208
Steven Moreland583685e2019-10-02 16:45:11 -0700209sp<IBinder> ServiceManagerShim::getService(const String16& name) const
210{
211 static bool gSystemBootCompleted = false;
212
213 sp<IBinder> svc = checkService(name);
214 if (svc != nullptr) return svc;
215
216 const bool isVendorService =
217 strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder") == 0;
Steven Moreland62eaabc2021-09-03 22:39:10 +0000218 const long timeout = 5000;
219 int64_t startTime = uptimeMillis();
Steven Moreland583685e2019-10-02 16:45:11 -0700220 // Vendor code can't access system properties
221 if (!gSystemBootCompleted && !isVendorService) {
222#ifdef __ANDROID__
223 char bootCompleted[PROPERTY_VALUE_MAX];
224 property_get("sys.boot_completed", bootCompleted, "0");
225 gSystemBootCompleted = strcmp(bootCompleted, "1") == 0 ? true : false;
226#else
227 gSystemBootCompleted = true;
228#endif
229 }
230 // retry interval in millisecond; note that vendor services stay at 100ms
231 const long sleepTime = gSystemBootCompleted ? 1000 : 100;
232
233 int n = 0;
Steven Moreland62eaabc2021-09-03 22:39:10 +0000234 while (uptimeMillis() - startTime < timeout) {
Steven Moreland583685e2019-10-02 16:45:11 -0700235 n++;
236 ALOGI("Waiting for service '%s' on '%s'...", String8(name).string(),
237 ProcessState::self()->getDriverName().c_str());
238 usleep(1000*sleepTime);
Steven Moreland92b17c62019-04-02 15:46:24 -0700239
Yunfan Chen788e1c42018-03-29 16:52:09 +0900240 sp<IBinder> svc = checkService(name);
Xin Lif11e2bd2018-06-08 15:11:57 -0700241 if (svc != nullptr) return svc;
Steven Moreland583685e2019-10-02 16:45:11 -0700242 }
243 ALOGW("Service %s didn't start. Returning NULL", String8(name).string());
244 return nullptr;
245}
Yunfan Chen788e1c42018-03-29 16:52:09 +0900246
Steven Moreland583685e2019-10-02 16:45:11 -0700247sp<IBinder> ServiceManagerShim::checkService(const String16& name) const
248{
249 sp<IBinder> ret;
250 if (!mTheRealServiceManager->checkService(String8(name).c_str(), &ret).isOk()) {
251 return nullptr;
252 }
253 return ret;
254}
255
256status_t ServiceManagerShim::addService(const String16& name, const sp<IBinder>& service,
257 bool allowIsolated, int dumpsysPriority)
258{
259 Status status = mTheRealServiceManager->addService(
260 String8(name).c_str(), service, allowIsolated, dumpsysPriority);
261 return status.exceptionCode();
262}
263
264Vector<String16> ServiceManagerShim::listServices(int dumpsysPriority)
265{
266 std::vector<std::string> ret;
267 if (!mTheRealServiceManager->listServices(dumpsysPriority, &ret).isOk()) {
268 return {};
269 }
270
271 Vector<String16> res;
272 res.setCapacity(ret.size());
273 for (const std::string& name : ret) {
274 res.push(String16(name.c_str()));
275 }
276 return res;
277}
278
279sp<IBinder> ServiceManagerShim::waitForService(const String16& name16)
280{
281 class Waiter : public android::os::BnServiceCallback {
282 Status onRegistration(const std::string& /*name*/,
283 const sp<IBinder>& binder) override {
284 std::unique_lock<std::mutex> lock(mMutex);
285 mBinder = binder;
286 lock.unlock();
Jon Spivack9f503a42019-10-22 16:49:19 -0700287 // Flushing here helps ensure the service's ref count remains accurate
288 IPCThreadState::self()->flushCommands();
Steven Moreland583685e2019-10-02 16:45:11 -0700289 mCv.notify_one();
290 return Status::ok();
Yunfan Chen788e1c42018-03-29 16:52:09 +0900291 }
Steven Moreland583685e2019-10-02 16:45:11 -0700292 public:
293 sp<IBinder> mBinder;
294 std::mutex mMutex;
295 std::condition_variable mCv;
296 };
Yunfan Chen788e1c42018-03-29 16:52:09 +0900297
Jon Spivackfed6db42019-11-18 16:43:59 -0800298 // Simple RAII object to ensure a function call immediately before going out of scope
299 class Defer {
300 public:
301 Defer(std::function<void()>&& f) : mF(std::move(f)) {}
302 ~Defer() { mF(); }
303 private:
304 std::function<void()> mF;
305 };
306
Steven Moreland583685e2019-10-02 16:45:11 -0700307 const std::string name = String8(name16).c_str();
Yunfan Chen788e1c42018-03-29 16:52:09 +0900308
Steven Moreland583685e2019-10-02 16:45:11 -0700309 sp<IBinder> out;
310 if (!mTheRealServiceManager->getService(name, &out).isOk()) {
311 return nullptr;
312 }
Jon Spivackfed6db42019-11-18 16:43:59 -0800313 if (out != nullptr) return out;
Steven Moreland583685e2019-10-02 16:45:11 -0700314
315 sp<Waiter> waiter = new Waiter;
316 if (!mTheRealServiceManager->registerForNotifications(
317 name, waiter).isOk()) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700318 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800319 }
Jon Spivackfed6db42019-11-18 16:43:59 -0800320 Defer unregister ([&] {
321 mTheRealServiceManager->unregisterForNotifications(name, waiter);
322 });
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700323
Steven Moreland583685e2019-10-02 16:45:11 -0700324 while(true) {
325 {
326 std::unique_lock<std::mutex> lock(waiter->mMutex);
327 using std::literals::chrono_literals::operator""s;
328 waiter->mCv.wait_for(lock, 1s, [&] {
329 return waiter->mBinder != nullptr;
330 });
331 if (waiter->mBinder != nullptr) return waiter->mBinder;
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700332 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800333
Steven Moreland583685e2019-10-02 16:45:11 -0700334 // Handle race condition for lazy services. Here is what can happen:
335 // - the service dies (not processed by init yet).
336 // - sm processes death notification.
337 // - sm gets getService and calls init to start service.
338 // - init gets the start signal, but the service already appears
339 // started, so it does nothing.
340 // - init gets death signal, but doesn't know it needs to restart
341 // the service
342 // - we need to request service again to get it to start
Jon Spivack0d844302019-07-22 18:40:34 -0700343 if (!mTheRealServiceManager->getService(name, &out).isOk()) {
Steven Moreland1c47b582019-08-27 18:05:27 -0700344 return nullptr;
345 }
Jon Spivackfed6db42019-11-18 16:43:59 -0800346 if (out != nullptr) return out;
Steven Moreland1c47b582019-08-27 18:05:27 -0700347
Steven Moreland583685e2019-10-02 16:45:11 -0700348 ALOGW("Waited one second for %s", name.c_str());
Steven Moreland1c47b582019-08-27 18:05:27 -0700349 }
Steven Moreland583685e2019-10-02 16:45:11 -0700350}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800351
Steven Morelandb82b8f82019-10-28 10:52:34 -0700352bool ServiceManagerShim::isDeclared(const String16& name) {
353 bool declared;
354 if (!mTheRealServiceManager->isDeclared(String8(name).c_str(), &declared).isOk()) {
355 return false;
356 }
357 return declared;
358}
359
Steven Moreland61ff8492019-09-26 16:05:45 -0700360} // namespace android