blob: 4bea21793011fbd8b3519537314b54c03c6d071d [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057sp<IServiceManager> defaultServiceManager()
58{
Steven Moreland92b17c62019-04-02 15:46:24 -070059 static Mutex gDefaultServiceManagerLock;
60 static sp<IServiceManager> gDefaultServiceManager;
61
Yi Kongfdd8da92018-06-07 17:52:27 -070062 if (gDefaultServiceManager != nullptr) return gDefaultServiceManager;
Daniel Eratc2832702015-10-13 15:29:32 -060063
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064 {
65 AutoMutex _l(gDefaultServiceManagerLock);
Yi Kongfdd8da92018-06-07 17:52:27 -070066 while (gDefaultServiceManager == nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067 gDefaultServiceManager = interface_cast<IServiceManager>(
Yi Kongfdd8da92018-06-07 17:52:27 -070068 ProcessState::self()->getContextObject(nullptr));
69 if (gDefaultServiceManager == nullptr)
Todd Poynora7b0f042013-06-18 17:25:37 -070070 sleep(1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071 }
72 }
Daniel Eratc2832702015-10-13 15:29:32 -060073
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080074 return gDefaultServiceManager;
75}
76
Steven Morelanded3b5632019-09-20 11:24:28 -070077#if !defined(__ANDROID_VNDK__) && defined(__ANDROID__)
Jiyong Park47f876b2018-04-17 13:56:46 +090078// IPermissionController is not accessible to vendors
79
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080080bool checkCallingPermission(const String16& permission)
81{
Yi Kongfdd8da92018-06-07 17:52:27 -070082 return checkCallingPermission(permission, nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083}
84
85static String16 _permission("permission");
86
Mathias Agopian375f5632009-06-15 18:24:59 -070087
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid)
89{
90 IPCThreadState* ipcState = IPCThreadState::self();
Mathias Agopian375f5632009-06-15 18:24:59 -070091 pid_t pid = ipcState->getCallingPid();
92 uid_t uid = ipcState->getCallingUid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080093 if (outPid) *outPid = pid;
Mathias Agopian375f5632009-06-15 18:24:59 -070094 if (outUid) *outUid = uid;
95 return checkPermission(permission, pid, uid);
96}
97
98bool checkPermission(const String16& permission, pid_t pid, uid_t uid)
99{
Steven Moreland92b17c62019-04-02 15:46:24 -0700100 static Mutex gPermissionControllerLock;
101 static sp<IPermissionController> gPermissionController;
102
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 sp<IPermissionController> pc;
Steven Moreland92b17c62019-04-02 15:46:24 -0700104 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 pc = gPermissionController;
Steven Moreland92b17c62019-04-02 15:46:24 -0700106 gPermissionControllerLock.unlock();
Daniel Eratc2832702015-10-13 15:29:32 -0600107
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 int64_t startTime = 0;
109
110 while (true) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700111 if (pc != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 bool res = pc->checkPermission(permission, pid, uid);
113 if (res) {
114 if (startTime != 0) {
Steve Blocka19954a2012-01-04 20:05:49 +0000115 ALOGI("Check passed after %d seconds for %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116 (int)((uptimeMillis()-startTime)/1000),
117 String8(permission).string(), uid, pid);
118 }
119 return res;
120 }
Daniel Eratc2832702015-10-13 15:29:32 -0600121
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800122 // Is this a permission failure, or did the controller go away?
Marco Nelissen097ca272014-11-14 08:01:01 -0800123 if (IInterface::asBinder(pc)->isBinderAlive()) {
Steve Block32397c12012-01-05 23:22:43 +0000124 ALOGW("Permission failure: %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125 String8(permission).string(), uid, pid);
126 return false;
127 }
Daniel Eratc2832702015-10-13 15:29:32 -0600128
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 // Object is dead!
Steven Moreland92b17c62019-04-02 15:46:24 -0700130 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131 if (gPermissionController == pc) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700132 gPermissionController = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 }
Steven Moreland92b17c62019-04-02 15:46:24 -0700134 gPermissionControllerLock.unlock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 }
Daniel Eratc2832702015-10-13 15:29:32 -0600136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 // Need to retrieve the permission controller.
138 sp<IBinder> binder = defaultServiceManager()->checkService(_permission);
Yi Kongfdd8da92018-06-07 17:52:27 -0700139 if (binder == nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 // Wait for the permission controller to come back...
141 if (startTime == 0) {
142 startTime = uptimeMillis();
Steve Blocka19954a2012-01-04 20:05:49 +0000143 ALOGI("Waiting to check permission %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 String8(permission).string(), uid, pid);
145 }
146 sleep(1);
147 } else {
148 pc = interface_cast<IPermissionController>(binder);
Daniel Eratc2832702015-10-13 15:29:32 -0600149 // Install the new permission controller, and try again.
Steven Moreland92b17c62019-04-02 15:46:24 -0700150 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151 gPermissionController = pc;
Steven Moreland92b17c62019-04-02 15:46:24 -0700152 gPermissionControllerLock.unlock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800153 }
154 }
155}
156
Jiyong Park47f876b2018-04-17 13:56:46 +0900157#endif //__ANDROID_VNDK__
158
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159// ----------------------------------------------------------------------
160
161class BpServiceManager : public BpInterface<IServiceManager>
162{
163public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700164 explicit BpServiceManager(const sp<IBinder>& impl)
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700165 : BpInterface<IServiceManager>(impl),
166 mTheRealServiceManager(interface_cast<AidlServiceManager>(impl))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 {
168 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700169
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700170 sp<IBinder> getService(const String16& name) const override
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 {
Steven Moreland92b17c62019-04-02 15:46:24 -0700172 static bool gSystemBootCompleted = false;
173
Yunfan Chen788e1c42018-03-29 16:52:09 +0900174 sp<IBinder> svc = checkService(name);
Xin Lif11e2bd2018-06-08 15:11:57 -0700175 if (svc != nullptr) return svc;
Yunfan Chen788e1c42018-03-29 16:52:09 +0900176
177 const bool isVendorService =
178 strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder") == 0;
179 const long timeout = uptimeMillis() + 5000;
Steven Morelanded3b5632019-09-20 11:24:28 -0700180 // Vendor code can't access system properties
Martijn Coenen2d0d1672018-06-25 09:40:14 +0200181 if (!gSystemBootCompleted && !isVendorService) {
Steven Morelanded3b5632019-09-20 11:24:28 -0700182#ifdef __ANDROID__
Yunfan Chen788e1c42018-03-29 16:52:09 +0900183 char bootCompleted[PROPERTY_VALUE_MAX];
184 property_get("sys.boot_completed", bootCompleted, "0");
185 gSystemBootCompleted = strcmp(bootCompleted, "1") == 0 ? true : false;
Steven Morelanded3b5632019-09-20 11:24:28 -0700186#else
187 gSystemBootCompleted = true;
Steven Moreland28723ae2019-04-01 18:52:30 -0700188#endif
Yunfan Chen788e1c42018-03-29 16:52:09 +0900189 }
Martijn Coenen2d0d1672018-06-25 09:40:14 +0200190 // retry interval in millisecond; note that vendor services stay at 100ms
Yunfan Chen788e1c42018-03-29 16:52:09 +0900191 const long sleepTime = gSystemBootCompleted ? 1000 : 100;
192
193 int n = 0;
194 while (uptimeMillis() < timeout) {
195 n++;
Steven Morelandef086dd2018-07-06 12:43:30 -0700196 ALOGI("Waiting for service '%s' on '%s'...", String8(name).string(),
197 ProcessState::self()->getDriverName().c_str());
Yunfan Chen788e1c42018-03-29 16:52:09 +0900198 usleep(1000*sleepTime);
199
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200 sp<IBinder> svc = checkService(name);
Yi Kongfdd8da92018-06-07 17:52:27 -0700201 if (svc != nullptr) return svc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800202 }
Yunfan Chen788e1c42018-03-29 16:52:09 +0900203 ALOGW("Service %s didn't start. Returning NULL", String8(name).string());
Yi Kongfdd8da92018-06-07 17:52:27 -0700204 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800205 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700206
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700207 sp<IBinder> checkService(const String16& name) const override {
208 sp<IBinder> ret;
209 if (!mTheRealServiceManager->checkService(String8(name).c_str(), &ret).isOk()) {
210 return nullptr;
211 }
212 return ret;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213 }
214
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700215 status_t addService(const String16& name, const sp<IBinder>& service,
216 bool allowIsolated, int dumpsysPriority) override {
217 Status status = mTheRealServiceManager->addService(String8(name).c_str(), service, allowIsolated, dumpsysPriority);
218 return status.exceptionCode();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219 }
220
Vishnu Nairf56042d2017-09-19 15:25:10 -0700221 virtual Vector<String16> listServices(int dumpsysPriority) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700222 std::vector<std::string> ret;
223 if (!mTheRealServiceManager->listServices(dumpsysPriority, &ret).isOk()) {
224 return {};
225 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800226
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700227 Vector<String16> res;
228 res.setCapacity(ret.size());
229 for (const std::string& name : ret) {
230 res.push(String16(name.c_str()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800231 }
232 return res;
233 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700234
Steven Moreland1c47b582019-08-27 18:05:27 -0700235 sp<IBinder> waitForService(const String16& name16) override {
236 class Waiter : public android::os::BnServiceCallback {
237 Status onRegistration(const std::string& /*name*/,
238 const sp<IBinder>& binder) override {
239 std::unique_lock<std::mutex> lock(mMutex);
240 mBinder = binder;
241 lock.unlock();
242 mCv.notify_one();
243 return Status::ok();
244 }
245 public:
246 sp<IBinder> mBinder;
247 std::mutex mMutex;
248 std::condition_variable mCv;
249 };
250
251 const std::string name = String8(name16).c_str();
252
253 sp<IBinder> out;
Jon Spivack0d844302019-07-22 18:40:34 -0700254 if (!mTheRealServiceManager->getService(name, &out).isOk()) {
Steven Moreland1c47b582019-08-27 18:05:27 -0700255 return nullptr;
256 }
257 if(out != nullptr) return out;
258
259 sp<Waiter> waiter = new Waiter;
260 if (!mTheRealServiceManager->registerForNotifications(
261 name, waiter).isOk()) {
262 return nullptr;
263 }
264
265 while(true) {
266 {
267 std::unique_lock<std::mutex> lock(waiter->mMutex);
268 using std::literals::chrono_literals::operator""s;
269 waiter->mCv.wait_for(lock, 1s, [&] {
270 return waiter->mBinder != nullptr;
271 });
272 if (waiter->mBinder != nullptr) return waiter->mBinder;
273 }
274
275 // Handle race condition for lazy services. Here is what can happen:
276 // - the service dies (not processed by init yet).
277 // - sm processes death notification.
Jon Spivack0d844302019-07-22 18:40:34 -0700278 // - sm gets getService and calls init to start service.
Steven Moreland1c47b582019-08-27 18:05:27 -0700279 // - init gets the start signal, but the service already appears
280 // started, so it does nothing.
281 // - init gets death signal, but doesn't know it needs to restart
282 // the service
283 // - we need to request service again to get it to start
Jon Spivack0d844302019-07-22 18:40:34 -0700284 if (!mTheRealServiceManager->getService(name, &out).isOk()) {
Steven Moreland1c47b582019-08-27 18:05:27 -0700285 return nullptr;
286 }
287 if(out != nullptr) return out;
288
289 ALOGW("Waited one second for %s", name.c_str());
290 }
291 }
292
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700293private:
294 sp<AidlServiceManager> mTheRealServiceManager;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800295};
296
297IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
298
Steven Moreland61ff8492019-09-26 16:05:45 -0700299} // namespace android