blob: 17e098c541e4311219759eaf43aff932355f05e1 [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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080021#include <utils/Log.h>
Mathias Agopian375f5632009-06-15 18:24:59 -070022#include <binder/IPCThreadState.h>
Jiyong Park47f876b2018-04-17 13:56:46 +090023#ifndef __ANDROID_VNDK__
24#include <binder/IPermissionController.h>
25#endif
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070026#include <binder/Parcel.h>
Yunfan Chen788e1c42018-03-29 16:52:09 +090027#include <cutils/properties.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028#include <utils/String8.h>
29#include <utils/SystemClock.h>
Iliyan Malchev273e1442017-04-10 14:09:04 -070030#include <utils/CallStack.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080031
Mathias Agopian208059f2009-05-18 15:08:03 -070032#include <private/binder/Static.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080033
34#include <unistd.h>
35
36namespace android {
37
38sp<IServiceManager> defaultServiceManager()
39{
Yi Kongfdd8da92018-06-07 17:52:27 -070040 if (gDefaultServiceManager != nullptr) return gDefaultServiceManager;
Daniel Eratc2832702015-10-13 15:29:32 -060041
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080042 {
43 AutoMutex _l(gDefaultServiceManagerLock);
Yi Kongfdd8da92018-06-07 17:52:27 -070044 while (gDefaultServiceManager == nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080045 gDefaultServiceManager = interface_cast<IServiceManager>(
Yi Kongfdd8da92018-06-07 17:52:27 -070046 ProcessState::self()->getContextObject(nullptr));
47 if (gDefaultServiceManager == nullptr)
Todd Poynora7b0f042013-06-18 17:25:37 -070048 sleep(1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080049 }
50 }
Daniel Eratc2832702015-10-13 15:29:32 -060051
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080052 return gDefaultServiceManager;
53}
54
Jiyong Park47f876b2018-04-17 13:56:46 +090055#ifndef __ANDROID_VNDK__
56// IPermissionController is not accessible to vendors
57
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058bool checkCallingPermission(const String16& permission)
59{
Yi Kongfdd8da92018-06-07 17:52:27 -070060 return checkCallingPermission(permission, nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080061}
62
63static String16 _permission("permission");
64
Mathias Agopian375f5632009-06-15 18:24:59 -070065
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080066bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid)
67{
68 IPCThreadState* ipcState = IPCThreadState::self();
Mathias Agopian375f5632009-06-15 18:24:59 -070069 pid_t pid = ipcState->getCallingPid();
70 uid_t uid = ipcState->getCallingUid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080071 if (outPid) *outPid = pid;
Mathias Agopian375f5632009-06-15 18:24:59 -070072 if (outUid) *outUid = uid;
73 return checkPermission(permission, pid, uid);
74}
75
76bool checkPermission(const String16& permission, pid_t pid, uid_t uid)
77{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080078 sp<IPermissionController> pc;
79 gDefaultServiceManagerLock.lock();
80 pc = gPermissionController;
81 gDefaultServiceManagerLock.unlock();
Daniel Eratc2832702015-10-13 15:29:32 -060082
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 int64_t startTime = 0;
84
85 while (true) {
Yi Kongfdd8da92018-06-07 17:52:27 -070086 if (pc != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080087 bool res = pc->checkPermission(permission, pid, uid);
88 if (res) {
89 if (startTime != 0) {
Steve Blocka19954a2012-01-04 20:05:49 +000090 ALOGI("Check passed after %d seconds for %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080091 (int)((uptimeMillis()-startTime)/1000),
92 String8(permission).string(), uid, pid);
93 }
94 return res;
95 }
Daniel Eratc2832702015-10-13 15:29:32 -060096
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080097 // Is this a permission failure, or did the controller go away?
Marco Nelissen097ca272014-11-14 08:01:01 -080098 if (IInterface::asBinder(pc)->isBinderAlive()) {
Steve Block32397c12012-01-05 23:22:43 +000099 ALOGW("Permission failure: %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800100 String8(permission).string(), uid, pid);
101 return false;
102 }
Daniel Eratc2832702015-10-13 15:29:32 -0600103
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 // Object is dead!
105 gDefaultServiceManagerLock.lock();
106 if (gPermissionController == pc) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700107 gPermissionController = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800108 }
109 gDefaultServiceManagerLock.unlock();
110 }
Daniel Eratc2832702015-10-13 15:29:32 -0600111
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800112 // Need to retrieve the permission controller.
113 sp<IBinder> binder = defaultServiceManager()->checkService(_permission);
Yi Kongfdd8da92018-06-07 17:52:27 -0700114 if (binder == nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 // Wait for the permission controller to come back...
116 if (startTime == 0) {
117 startTime = uptimeMillis();
Steve Blocka19954a2012-01-04 20:05:49 +0000118 ALOGI("Waiting to check permission %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800119 String8(permission).string(), uid, pid);
120 }
121 sleep(1);
122 } else {
123 pc = interface_cast<IPermissionController>(binder);
Daniel Eratc2832702015-10-13 15:29:32 -0600124 // Install the new permission controller, and try again.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800125 gDefaultServiceManagerLock.lock();
126 gPermissionController = pc;
127 gDefaultServiceManagerLock.unlock();
128 }
129 }
130}
131
Jiyong Park47f876b2018-04-17 13:56:46 +0900132#endif //__ANDROID_VNDK__
133
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134// ----------------------------------------------------------------------
135
136class BpServiceManager : public BpInterface<IServiceManager>
137{
138public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700139 explicit BpServiceManager(const sp<IBinder>& impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800140 : BpInterface<IServiceManager>(impl)
141 {
142 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700143
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 virtual sp<IBinder> getService(const String16& name) const
145 {
Yunfan Chen788e1c42018-03-29 16:52:09 +0900146 sp<IBinder> svc = checkService(name);
Xin Lif11e2bd2018-06-08 15:11:57 -0700147 if (svc != nullptr) return svc;
Yunfan Chen788e1c42018-03-29 16:52:09 +0900148
149 const bool isVendorService =
150 strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder") == 0;
151 const long timeout = uptimeMillis() + 5000;
152 if (!gSystemBootCompleted) {
153 char bootCompleted[PROPERTY_VALUE_MAX];
154 property_get("sys.boot_completed", bootCompleted, "0");
155 gSystemBootCompleted = strcmp(bootCompleted, "1") == 0 ? true : false;
156 }
157 // retry interval in millisecond.
158 const long sleepTime = gSystemBootCompleted ? 1000 : 100;
159
160 int n = 0;
161 while (uptimeMillis() < timeout) {
162 n++;
163 if (isVendorService) {
164 ALOGI("Waiting for vendor service %s...", String8(name).string());
165 CallStack stack(LOG_TAG);
166 } else if (n%10 == 0) {
167 ALOGI("Waiting for service %s...", String8(name).string());
Andy Hung6456e442016-12-06 09:40:01 -0800168 }
Yunfan Chen788e1c42018-03-29 16:52:09 +0900169 usleep(1000*sleepTime);
170
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800171 sp<IBinder> svc = checkService(name);
Yi Kongfdd8da92018-06-07 17:52:27 -0700172 if (svc != nullptr) return svc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 }
Yunfan Chen788e1c42018-03-29 16:52:09 +0900174 ALOGW("Service %s didn't start. Returning NULL", String8(name).string());
Yi Kongfdd8da92018-06-07 17:52:27 -0700175 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800176 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700177
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800178 virtual sp<IBinder> checkService( const String16& name) const
179 {
180 Parcel data, reply;
181 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
182 data.writeString16(name);
183 remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply);
184 return reply.readStrongBinder();
185 }
186
Dianne Hackborna94f1292012-02-09 16:12:18 -0800187 virtual status_t addService(const String16& name, const sp<IBinder>& service,
Vishnu Nairf56042d2017-09-19 15:25:10 -0700188 bool allowIsolated, int dumpsysPriority) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189 Parcel data, reply;
190 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
191 data.writeString16(name);
192 data.writeStrongBinder(service);
Dianne Hackborna94f1292012-02-09 16:12:18 -0800193 data.writeInt32(allowIsolated ? 1 : 0);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700194 data.writeInt32(dumpsysPriority);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800195 status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700196 return err == NO_ERROR ? reply.readExceptionCode() : err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800197 }
198
Vishnu Nairf56042d2017-09-19 15:25:10 -0700199 virtual Vector<String16> listServices(int dumpsysPriority) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800200 Vector<String16> res;
201 int n = 0;
202
203 for (;;) {
204 Parcel data, reply;
205 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
206 data.writeInt32(n++);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700207 data.writeInt32(dumpsysPriority);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800208 status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply);
209 if (err != NO_ERROR)
210 break;
211 res.add(reply.readString16());
212 }
213 return res;
214 }
215};
216
217IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
218
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800219}; // namespace android