blob: 74f1f47c7afc313dcceb1d3df774158939ab502a [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 Moreland80e1e6d2019-06-21 12:35:59 -070021#include <android/os/IServiceManager.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022#include <utils/Log.h>
Mathias Agopian375f5632009-06-15 18:24:59 -070023#include <binder/IPCThreadState.h>
Jiyong Park47f876b2018-04-17 13:56:46 +090024#ifndef __ANDROID_VNDK__
25#include <binder/IPermissionController.h>
26#endif
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070027#include <binder/Parcel.h>
Yunfan Chen788e1c42018-03-29 16:52:09 +090028#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080029#include <utils/String8.h>
30#include <utils/SystemClock.h>
31
Steven Morelanda4853cd2019-07-12 15:44:37 -070032#include "Static.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
34#include <unistd.h>
35
36namespace android {
37
Steven Moreland80e1e6d2019-06-21 12:35:59 -070038using AidlServiceManager = android::os::IServiceManager;
39using android::binder::Status;
40
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041sp<IServiceManager> defaultServiceManager()
42{
Steven Moreland92b17c62019-04-02 15:46:24 -070043 static Mutex gDefaultServiceManagerLock;
44 static sp<IServiceManager> gDefaultServiceManager;
45
Yi Kongfdd8da92018-06-07 17:52:27 -070046 if (gDefaultServiceManager != nullptr) return gDefaultServiceManager;
Daniel Eratc2832702015-10-13 15:29:32 -060047
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048 {
49 AutoMutex _l(gDefaultServiceManagerLock);
Yi Kongfdd8da92018-06-07 17:52:27 -070050 while (gDefaultServiceManager == nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051 gDefaultServiceManager = interface_cast<IServiceManager>(
Yi Kongfdd8da92018-06-07 17:52:27 -070052 ProcessState::self()->getContextObject(nullptr));
53 if (gDefaultServiceManager == nullptr)
Todd Poynora7b0f042013-06-18 17:25:37 -070054 sleep(1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080055 }
56 }
Daniel Eratc2832702015-10-13 15:29:32 -060057
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058 return gDefaultServiceManager;
59}
60
Jiyong Park47f876b2018-04-17 13:56:46 +090061#ifndef __ANDROID_VNDK__
62// IPermissionController is not accessible to vendors
63
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064bool checkCallingPermission(const String16& permission)
65{
Yi Kongfdd8da92018-06-07 17:52:27 -070066 return checkCallingPermission(permission, nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067}
68
69static String16 _permission("permission");
70
Mathias Agopian375f5632009-06-15 18:24:59 -070071
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid)
73{
74 IPCThreadState* ipcState = IPCThreadState::self();
Mathias Agopian375f5632009-06-15 18:24:59 -070075 pid_t pid = ipcState->getCallingPid();
76 uid_t uid = ipcState->getCallingUid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077 if (outPid) *outPid = pid;
Mathias Agopian375f5632009-06-15 18:24:59 -070078 if (outUid) *outUid = uid;
79 return checkPermission(permission, pid, uid);
80}
81
82bool checkPermission(const String16& permission, pid_t pid, uid_t uid)
83{
Steven Moreland92b17c62019-04-02 15:46:24 -070084 static Mutex gPermissionControllerLock;
85 static sp<IPermissionController> gPermissionController;
86
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 sp<IPermissionController> pc;
Steven Moreland92b17c62019-04-02 15:46:24 -070088 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 pc = gPermissionController;
Steven Moreland92b17c62019-04-02 15:46:24 -070090 gPermissionControllerLock.unlock();
Daniel Eratc2832702015-10-13 15:29:32 -060091
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 int64_t startTime = 0;
93
94 while (true) {
Yi Kongfdd8da92018-06-07 17:52:27 -070095 if (pc != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 bool res = pc->checkPermission(permission, pid, uid);
97 if (res) {
98 if (startTime != 0) {
Steve Blocka19954a2012-01-04 20:05:49 +000099 ALOGI("Check passed after %d seconds for %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 (int)((uptimeMillis()-startTime)/1000),
101 String8(permission).string(), uid, pid);
102 }
103 return res;
104 }
Daniel Eratc2832702015-10-13 15:29:32 -0600105
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800106 // Is this a permission failure, or did the controller go away?
Marco Nelissen097ca272014-11-14 08:01:01 -0800107 if (IInterface::asBinder(pc)->isBinderAlive()) {
Steve Block32397c12012-01-05 23:22:43 +0000108 ALOGW("Permission failure: %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 String8(permission).string(), uid, pid);
110 return false;
111 }
Daniel Eratc2832702015-10-13 15:29:32 -0600112
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 // Object is dead!
Steven Moreland92b17c62019-04-02 15:46:24 -0700114 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 if (gPermissionController == pc) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700116 gPermissionController = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 }
Steven Moreland92b17c62019-04-02 15:46:24 -0700118 gPermissionControllerLock.unlock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 }
Daniel Eratc2832702015-10-13 15:29:32 -0600120
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800121 // Need to retrieve the permission controller.
122 sp<IBinder> binder = defaultServiceManager()->checkService(_permission);
Yi Kongfdd8da92018-06-07 17:52:27 -0700123 if (binder == nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 // Wait for the permission controller to come back...
125 if (startTime == 0) {
126 startTime = uptimeMillis();
Steve Blocka19954a2012-01-04 20:05:49 +0000127 ALOGI("Waiting to check permission %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800128 String8(permission).string(), uid, pid);
129 }
130 sleep(1);
131 } else {
132 pc = interface_cast<IPermissionController>(binder);
Daniel Eratc2832702015-10-13 15:29:32 -0600133 // Install the new permission controller, and try again.
Steven Moreland92b17c62019-04-02 15:46:24 -0700134 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800135 gPermissionController = pc;
Steven Moreland92b17c62019-04-02 15:46:24 -0700136 gPermissionControllerLock.unlock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137 }
138 }
139}
140
Jiyong Park47f876b2018-04-17 13:56:46 +0900141#endif //__ANDROID_VNDK__
142
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143// ----------------------------------------------------------------------
144
145class BpServiceManager : public BpInterface<IServiceManager>
146{
147public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700148 explicit BpServiceManager(const sp<IBinder>& impl)
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700149 : BpInterface<IServiceManager>(impl),
150 mTheRealServiceManager(interface_cast<AidlServiceManager>(impl))
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800151 {
152 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700153
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700154 sp<IBinder> getService(const String16& name) const override
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800155 {
Steven Moreland92b17c62019-04-02 15:46:24 -0700156 static bool gSystemBootCompleted = false;
157
Yunfan Chen788e1c42018-03-29 16:52:09 +0900158 sp<IBinder> svc = checkService(name);
Xin Lif11e2bd2018-06-08 15:11:57 -0700159 if (svc != nullptr) return svc;
Yunfan Chen788e1c42018-03-29 16:52:09 +0900160
161 const bool isVendorService =
162 strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder") == 0;
163 const long timeout = uptimeMillis() + 5000;
Martijn Coenen2d0d1672018-06-25 09:40:14 +0200164 if (!gSystemBootCompleted && !isVendorService) {
165 // Vendor code can't access system properties
Yunfan Chen788e1c42018-03-29 16:52:09 +0900166 char bootCompleted[PROPERTY_VALUE_MAX];
167 property_get("sys.boot_completed", bootCompleted, "0");
168 gSystemBootCompleted = strcmp(bootCompleted, "1") == 0 ? true : false;
169 }
Martijn Coenen2d0d1672018-06-25 09:40:14 +0200170 // retry interval in millisecond; note that vendor services stay at 100ms
Yunfan Chen788e1c42018-03-29 16:52:09 +0900171 const long sleepTime = gSystemBootCompleted ? 1000 : 100;
172
173 int n = 0;
174 while (uptimeMillis() < timeout) {
175 n++;
Steven Morelandef086dd2018-07-06 12:43:30 -0700176 ALOGI("Waiting for service '%s' on '%s'...", String8(name).string(),
177 ProcessState::self()->getDriverName().c_str());
Yunfan Chen788e1c42018-03-29 16:52:09 +0900178 usleep(1000*sleepTime);
179
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 sp<IBinder> svc = checkService(name);
Yi Kongfdd8da92018-06-07 17:52:27 -0700181 if (svc != nullptr) return svc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 }
Yunfan Chen788e1c42018-03-29 16:52:09 +0900183 ALOGW("Service %s didn't start. Returning NULL", String8(name).string());
Yi Kongfdd8da92018-06-07 17:52:27 -0700184 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800185 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700186
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700187 sp<IBinder> checkService(const String16& name) const override {
188 sp<IBinder> ret;
189 if (!mTheRealServiceManager->checkService(String8(name).c_str(), &ret).isOk()) {
190 return nullptr;
191 }
192 return ret;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 }
194
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700195 status_t addService(const String16& name, const sp<IBinder>& service,
196 bool allowIsolated, int dumpsysPriority) override {
197 Status status = mTheRealServiceManager->addService(String8(name).c_str(), service, allowIsolated, dumpsysPriority);
198 return status.exceptionCode();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 }
200
Vishnu Nairf56042d2017-09-19 15:25:10 -0700201 virtual Vector<String16> listServices(int dumpsysPriority) {
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700202 std::vector<std::string> ret;
203 if (!mTheRealServiceManager->listServices(dumpsysPriority, &ret).isOk()) {
204 return {};
205 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800206
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700207 Vector<String16> res;
208 res.setCapacity(ret.size());
209 for (const std::string& name : ret) {
210 res.push(String16(name.c_str()));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800211 }
212 return res;
213 }
Steven Moreland80e1e6d2019-06-21 12:35:59 -0700214
215private:
216 sp<AidlServiceManager> mTheRealServiceManager;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800217};
218
219IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
220
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800221}; // namespace android