blob: eefc5b1c4fd4a80eb4b061510f449865253f330b [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080023#include <utils/Log.h>
Mathias Agopian375f5632009-06-15 18:24:59 -070024#include <binder/IPCThreadState.h>
Jiyong Park47f876b2018-04-17 13:56:46 +090025#ifndef __ANDROID_VNDK__
26#include <binder/IPermissionController.h>
27#endif
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070028#include <binder/Parcel.h>
Yunfan Chen788e1c42018-03-29 16:52:09 +090029#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030#include <utils/String8.h>
31#include <utils/SystemClock.h>
32
Steven Morelanda4853cd2019-07-12 15:44:37 -070033#include "Static.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080034
35#include <unistd.h>
36
37namespace android {
38
Steven Moreland80e1e6d2019-06-21 12:35:59 -070039using AidlServiceManager = android::os::IServiceManager;
40using android::binder::Status;
41
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042sp<IServiceManager> defaultServiceManager()
43{
Steven Moreland92b17c62019-04-02 15:46:24 -070044 static Mutex gDefaultServiceManagerLock;
45 static sp<IServiceManager> gDefaultServiceManager;
46
Yi Kongfdd8da92018-06-07 17:52:27 -070047 if (gDefaultServiceManager != nullptr) return gDefaultServiceManager;
Daniel Eratc2832702015-10-13 15:29:32 -060048
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049 {
50 AutoMutex _l(gDefaultServiceManagerLock);
Yi Kongfdd8da92018-06-07 17:52:27 -070051 while (gDefaultServiceManager == nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052 gDefaultServiceManager = interface_cast<IServiceManager>(
Yi Kongfdd8da92018-06-07 17:52:27 -070053 ProcessState::self()->getContextObject(nullptr));
54 if (gDefaultServiceManager == nullptr)
Todd Poynora7b0f042013-06-18 17:25:37 -070055 sleep(1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080056 }
57 }
Daniel Eratc2832702015-10-13 15:29:32 -060058
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080059 return gDefaultServiceManager;
60}
61
Jiyong Park47f876b2018-04-17 13:56:46 +090062#ifndef __ANDROID_VNDK__
63// IPermissionController is not accessible to vendors
64
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065bool checkCallingPermission(const String16& permission)
66{
Yi Kongfdd8da92018-06-07 17:52:27 -070067 return checkCallingPermission(permission, nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068}
69
70static String16 _permission("permission");
71
Mathias Agopian375f5632009-06-15 18:24:59 -070072
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid)
74{
75 IPCThreadState* ipcState = IPCThreadState::self();
Mathias Agopian375f5632009-06-15 18:24:59 -070076 pid_t pid = ipcState->getCallingPid();
77 uid_t uid = ipcState->getCallingUid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078 if (outPid) *outPid = pid;
Mathias Agopian375f5632009-06-15 18:24:59 -070079 if (outUid) *outUid = uid;
80 return checkPermission(permission, pid, uid);
81}
82
83bool checkPermission(const String16& permission, pid_t pid, uid_t uid)
84{
Steven Moreland92b17c62019-04-02 15:46:24 -070085 static Mutex gPermissionControllerLock;
86 static sp<IPermissionController> gPermissionController;
87
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 sp<IPermissionController> pc;
Steven Moreland92b17c62019-04-02 15:46:24 -070089 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 pc = gPermissionController;
Steven Moreland92b17c62019-04-02 15:46:24 -070091 gPermissionControllerLock.unlock();
Daniel Eratc2832702015-10-13 15:29:32 -060092
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 int64_t startTime = 0;
94
95 while (true) {
Yi Kongfdd8da92018-06-07 17:52:27 -070096 if (pc != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 bool res = pc->checkPermission(permission, pid, uid);
98 if (res) {
99 if (startTime != 0) {
Steve Blocka19954a2012-01-04 20:05:49 +0000100 ALOGI("Check passed after %d seconds for %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800101 (int)((uptimeMillis()-startTime)/1000),
102 String8(permission).string(), uid, pid);
103 }
104 return res;
105 }
Daniel Eratc2832702015-10-13 15:29:32 -0600106
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800107 // Is this a permission failure, or did the controller go away?
Marco Nelissen097ca272014-11-14 08:01:01 -0800108 if (IInterface::asBinder(pc)->isBinderAlive()) {
Steve Block32397c12012-01-05 23:22:43 +0000109 ALOGW("Permission failure: %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800110 String8(permission).string(), uid, pid);
111 return false;
112 }
Daniel Eratc2832702015-10-13 15:29:32 -0600113
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800114 // Object is dead!
Steven Moreland92b17c62019-04-02 15:46:24 -0700115 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 if (gPermissionController == pc) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700117 gPermissionController = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 }
Steven Moreland92b17c62019-04-02 15:46:24 -0700119 gPermissionControllerLock.unlock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 }
Daniel Eratc2832702015-10-13 15:29:32 -0600121
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 // Need to retrieve the permission controller.
123 sp<IBinder> binder = defaultServiceManager()->checkService(_permission);
Yi Kongfdd8da92018-06-07 17:52:27 -0700124 if (binder == nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125 // Wait for the permission controller to come back...
126 if (startTime == 0) {
127 startTime = uptimeMillis();
Steve Blocka19954a2012-01-04 20:05:49 +0000128 ALOGI("Waiting to check permission %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 String8(permission).string(), uid, pid);
130 }
131 sleep(1);
132 } else {
133 pc = interface_cast<IPermissionController>(binder);
Daniel Eratc2832702015-10-13 15:29:32 -0600134 // Install the new permission controller, and try again.
Steven Moreland92b17c62019-04-02 15:46:24 -0700135 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 gPermissionController = pc;
Steven Moreland92b17c62019-04-02 15:46:24 -0700137 gPermissionControllerLock.unlock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800138 }
139 }
140}
141
Jiyong Park47f876b2018-04-17 13:56:46 +0900142#endif //__ANDROID_VNDK__
143
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144// ----------------------------------------------------------------------
145
146class BpServiceManager : public BpInterface<IServiceManager>
147{
148public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700149 explicit BpServiceManager(const sp<IBinder>& impl)
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700150 : BpInterface<IServiceManager>(impl),
151 mTheRealServiceManager(interface_cast<AidlServiceManager>(impl))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800152 {
153 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700154
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700155 sp<IBinder> getService(const String16& name) const override
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 {
Steven Moreland92b17c62019-04-02 15:46:24 -0700157 static bool gSystemBootCompleted = false;
158
Yunfan Chen788e1c42018-03-29 16:52:09 +0900159 sp<IBinder> svc = checkService(name);
Xin Lif11e2bd2018-06-08 15:11:57 -0700160 if (svc != nullptr) return svc;
Yunfan Chen788e1c42018-03-29 16:52:09 +0900161
162 const bool isVendorService =
163 strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder") == 0;
164 const long timeout = uptimeMillis() + 5000;
Martijn Coenen2d0d1672018-06-25 09:40:14 +0200165 if (!gSystemBootCompleted && !isVendorService) {
166 // Vendor code can't access system properties
Yunfan Chen788e1c42018-03-29 16:52:09 +0900167 char bootCompleted[PROPERTY_VALUE_MAX];
168 property_get("sys.boot_completed", bootCompleted, "0");
169 gSystemBootCompleted = strcmp(bootCompleted, "1") == 0 ? true : false;
170 }
Martijn Coenen2d0d1672018-06-25 09:40:14 +0200171 // retry interval in millisecond; note that vendor services stay at 100ms
Yunfan Chen788e1c42018-03-29 16:52:09 +0900172 const long sleepTime = gSystemBootCompleted ? 1000 : 100;
173
174 int n = 0;
175 while (uptimeMillis() < timeout) {
176 n++;
Steven Morelandef086dd2018-07-06 12:43:30 -0700177 ALOGI("Waiting for service '%s' on '%s'...", String8(name).string(),
178 ProcessState::self()->getDriverName().c_str());
Yunfan Chen788e1c42018-03-29 16:52:09 +0900179 usleep(1000*sleepTime);
180
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 sp<IBinder> svc = checkService(name);
Yi Kongfdd8da92018-06-07 17:52:27 -0700182 if (svc != nullptr) return svc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800183 }
Yunfan Chen788e1c42018-03-29 16:52:09 +0900184 ALOGW("Service %s didn't start. Returning NULL", String8(name).string());
Yi Kongfdd8da92018-06-07 17:52:27 -0700185 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800186 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700187
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700188 sp<IBinder> checkService(const String16& name) const override {
189 sp<IBinder> ret;
190 if (!mTheRealServiceManager->checkService(String8(name).c_str(), &ret).isOk()) {
191 return nullptr;
192 }
193 return ret;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800194 }
195
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700196 status_t addService(const String16& name, const sp<IBinder>& service,
197 bool allowIsolated, int dumpsysPriority) override {
198 Status status = mTheRealServiceManager->addService(String8(name).c_str(), service, allowIsolated, dumpsysPriority);
199 return status.exceptionCode();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200 }
201
Vishnu Nairf56042d2017-09-19 15:25:10 -0700202 virtual Vector<String16> listServices(int dumpsysPriority) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700203 std::vector<std::string> ret;
204 if (!mTheRealServiceManager->listServices(dumpsysPriority, &ret).isOk()) {
205 return {};
206 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800207
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700208 Vector<String16> res;
209 res.setCapacity(ret.size());
210 for (const std::string& name : ret) {
211 res.push(String16(name.c_str()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 }
213 return res;
214 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700215
Steven Moreland1c47b582019-08-27 18:05:27 -0700216 sp<IBinder> waitForService(const String16& name16) override {
217 class Waiter : public android::os::BnServiceCallback {
218 Status onRegistration(const std::string& /*name*/,
219 const sp<IBinder>& binder) override {
220 std::unique_lock<std::mutex> lock(mMutex);
221 mBinder = binder;
222 lock.unlock();
223 mCv.notify_one();
224 return Status::ok();
225 }
226 public:
227 sp<IBinder> mBinder;
228 std::mutex mMutex;
229 std::condition_variable mCv;
230 };
231
232 const std::string name = String8(name16).c_str();
233
234 sp<IBinder> out;
Jon Spivack0d844302019-07-22 18:40:34 -0700235 if (!mTheRealServiceManager->getService(name, &out).isOk()) {
Steven Moreland1c47b582019-08-27 18:05:27 -0700236 return nullptr;
237 }
238 if(out != nullptr) return out;
239
240 sp<Waiter> waiter = new Waiter;
241 if (!mTheRealServiceManager->registerForNotifications(
242 name, waiter).isOk()) {
243 return nullptr;
244 }
245
246 while(true) {
247 {
248 std::unique_lock<std::mutex> lock(waiter->mMutex);
249 using std::literals::chrono_literals::operator""s;
250 waiter->mCv.wait_for(lock, 1s, [&] {
251 return waiter->mBinder != nullptr;
252 });
253 if (waiter->mBinder != nullptr) return waiter->mBinder;
254 }
255
256 // Handle race condition for lazy services. Here is what can happen:
257 // - the service dies (not processed by init yet).
258 // - sm processes death notification.
Jon Spivack0d844302019-07-22 18:40:34 -0700259 // - sm gets getService and calls init to start service.
Steven Moreland1c47b582019-08-27 18:05:27 -0700260 // - init gets the start signal, but the service already appears
261 // started, so it does nothing.
262 // - init gets death signal, but doesn't know it needs to restart
263 // the service
264 // - we need to request service again to get it to start
Jon Spivack0d844302019-07-22 18:40:34 -0700265 if (!mTheRealServiceManager->getService(name, &out).isOk()) {
Steven Moreland1c47b582019-08-27 18:05:27 -0700266 return nullptr;
267 }
268 if(out != nullptr) return out;
269
270 ALOGW("Waited one second for %s", name.c_str());
271 }
272 }
273
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700274private:
275 sp<AidlServiceManager> mTheRealServiceManager;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276};
277
278IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
279
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800280}; // namespace android