blob: 9d816e8ad6bb6105f5e1e53586fd9d13ba152445 [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>
30
Steven Morelanda4853cd2019-07-12 15:44:37 -070031#include "Static.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032
33#include <unistd.h>
34
35namespace android {
36
37sp<IServiceManager> defaultServiceManager()
38{
Steven Moreland92b17c62019-04-02 15:46:24 -070039 static Mutex gDefaultServiceManagerLock;
40 static sp<IServiceManager> gDefaultServiceManager;
41
Yi Kongfdd8da92018-06-07 17:52:27 -070042 if (gDefaultServiceManager != nullptr) return gDefaultServiceManager;
Daniel Eratc2832702015-10-13 15:29:32 -060043
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044 {
45 AutoMutex _l(gDefaultServiceManagerLock);
Yi Kongfdd8da92018-06-07 17:52:27 -070046 while (gDefaultServiceManager == nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047 gDefaultServiceManager = interface_cast<IServiceManager>(
Yi Kongfdd8da92018-06-07 17:52:27 -070048 ProcessState::self()->getContextObject(nullptr));
49 if (gDefaultServiceManager == nullptr)
Todd Poynora7b0f042013-06-18 17:25:37 -070050 sleep(1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051 }
52 }
Daniel Eratc2832702015-10-13 15:29:32 -060053
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080054 return gDefaultServiceManager;
55}
56
Jiyong Park47f876b2018-04-17 13:56:46 +090057#ifndef __ANDROID_VNDK__
58// IPermissionController is not accessible to vendors
59
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080060bool checkCallingPermission(const String16& permission)
61{
Yi Kongfdd8da92018-06-07 17:52:27 -070062 return checkCallingPermission(permission, nullptr, nullptr);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063}
64
65static String16 _permission("permission");
66
Mathias Agopian375f5632009-06-15 18:24:59 -070067
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080068bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid)
69{
70 IPCThreadState* ipcState = IPCThreadState::self();
Mathias Agopian375f5632009-06-15 18:24:59 -070071 pid_t pid = ipcState->getCallingPid();
72 uid_t uid = ipcState->getCallingUid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080073 if (outPid) *outPid = pid;
Mathias Agopian375f5632009-06-15 18:24:59 -070074 if (outUid) *outUid = uid;
75 return checkPermission(permission, pid, uid);
76}
77
78bool checkPermission(const String16& permission, pid_t pid, uid_t uid)
79{
Steven Moreland92b17c62019-04-02 15:46:24 -070080 static Mutex gPermissionControllerLock;
81 static sp<IPermissionController> gPermissionController;
82
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 sp<IPermissionController> pc;
Steven Moreland92b17c62019-04-02 15:46:24 -070084 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080085 pc = gPermissionController;
Steven Moreland92b17c62019-04-02 15:46:24 -070086 gPermissionControllerLock.unlock();
Daniel Eratc2832702015-10-13 15:29:32 -060087
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088 int64_t startTime = 0;
89
90 while (true) {
Yi Kongfdd8da92018-06-07 17:52:27 -070091 if (pc != nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 bool res = pc->checkPermission(permission, pid, uid);
93 if (res) {
94 if (startTime != 0) {
Steve Blocka19954a2012-01-04 20:05:49 +000095 ALOGI("Check passed after %d seconds for %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 (int)((uptimeMillis()-startTime)/1000),
97 String8(permission).string(), uid, pid);
98 }
99 return res;
100 }
Daniel Eratc2832702015-10-13 15:29:32 -0600101
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800102 // Is this a permission failure, or did the controller go away?
Marco Nelissen097ca272014-11-14 08:01:01 -0800103 if (IInterface::asBinder(pc)->isBinderAlive()) {
Steve Block32397c12012-01-05 23:22:43 +0000104 ALOGW("Permission failure: %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800105 String8(permission).string(), uid, pid);
106 return false;
107 }
Daniel Eratc2832702015-10-13 15:29:32 -0600108
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800109 // Object is dead!
Steven Moreland92b17c62019-04-02 15:46:24 -0700110 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 if (gPermissionController == pc) {
Yi Kongfdd8da92018-06-07 17:52:27 -0700112 gPermissionController = nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800113 }
Steven Moreland92b17c62019-04-02 15:46:24 -0700114 gPermissionControllerLock.unlock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800115 }
Daniel Eratc2832702015-10-13 15:29:32 -0600116
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 // Need to retrieve the permission controller.
118 sp<IBinder> binder = defaultServiceManager()->checkService(_permission);
Yi Kongfdd8da92018-06-07 17:52:27 -0700119 if (binder == nullptr) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800120 // Wait for the permission controller to come back...
121 if (startTime == 0) {
122 startTime = uptimeMillis();
Steve Blocka19954a2012-01-04 20:05:49 +0000123 ALOGI("Waiting to check permission %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 String8(permission).string(), uid, pid);
125 }
126 sleep(1);
127 } else {
128 pc = interface_cast<IPermissionController>(binder);
Daniel Eratc2832702015-10-13 15:29:32 -0600129 // Install the new permission controller, and try again.
Steven Moreland92b17c62019-04-02 15:46:24 -0700130 gPermissionControllerLock.lock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131 gPermissionController = pc;
Steven Moreland92b17c62019-04-02 15:46:24 -0700132 gPermissionControllerLock.unlock();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133 }
134 }
135}
136
Jiyong Park47f876b2018-04-17 13:56:46 +0900137#endif //__ANDROID_VNDK__
138
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139// ----------------------------------------------------------------------
140
141class BpServiceManager : public BpInterface<IServiceManager>
142{
143public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700144 explicit BpServiceManager(const sp<IBinder>& impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800145 : BpInterface<IServiceManager>(impl)
146 {
147 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700148
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800149 virtual sp<IBinder> getService(const String16& name) const
150 {
Steven Moreland92b17c62019-04-02 15:46:24 -0700151 static bool gSystemBootCompleted = false;
152
Yunfan Chen788e1c42018-03-29 16:52:09 +0900153 sp<IBinder> svc = checkService(name);
Xin Lif11e2bd2018-06-08 15:11:57 -0700154 if (svc != nullptr) return svc;
Yunfan Chen788e1c42018-03-29 16:52:09 +0900155
156 const bool isVendorService =
157 strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder") == 0;
158 const long timeout = uptimeMillis() + 5000;
Martijn Coenen2d0d1672018-06-25 09:40:14 +0200159 if (!gSystemBootCompleted && !isVendorService) {
160 // Vendor code can't access system properties
Yunfan Chen788e1c42018-03-29 16:52:09 +0900161 char bootCompleted[PROPERTY_VALUE_MAX];
162 property_get("sys.boot_completed", bootCompleted, "0");
163 gSystemBootCompleted = strcmp(bootCompleted, "1") == 0 ? true : false;
164 }
Martijn Coenen2d0d1672018-06-25 09:40:14 +0200165 // retry interval in millisecond; note that vendor services stay at 100ms
Yunfan Chen788e1c42018-03-29 16:52:09 +0900166 const long sleepTime = gSystemBootCompleted ? 1000 : 100;
167
168 int n = 0;
169 while (uptimeMillis() < timeout) {
170 n++;
Steven Morelandef086dd2018-07-06 12:43:30 -0700171 ALOGI("Waiting for service '%s' on '%s'...", String8(name).string(),
172 ProcessState::self()->getDriverName().c_str());
Yunfan Chen788e1c42018-03-29 16:52:09 +0900173 usleep(1000*sleepTime);
174
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800175 sp<IBinder> svc = checkService(name);
Yi Kongfdd8da92018-06-07 17:52:27 -0700176 if (svc != nullptr) return svc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800177 }
Yunfan Chen788e1c42018-03-29 16:52:09 +0900178 ALOGW("Service %s didn't start. Returning NULL", String8(name).string());
Yi Kongfdd8da92018-06-07 17:52:27 -0700179 return nullptr;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800180 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700181
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800182 virtual sp<IBinder> checkService( const String16& name) const
183 {
184 Parcel data, reply;
185 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
186 data.writeString16(name);
187 remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply);
188 return reply.readStrongBinder();
189 }
190
Dianne Hackborna94f1292012-02-09 16:12:18 -0800191 virtual status_t addService(const String16& name, const sp<IBinder>& service,
Vishnu Nairf56042d2017-09-19 15:25:10 -0700192 bool allowIsolated, int dumpsysPriority) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193 Parcel data, reply;
194 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
195 data.writeString16(name);
196 data.writeStrongBinder(service);
Dianne Hackborna94f1292012-02-09 16:12:18 -0800197 data.writeInt32(allowIsolated ? 1 : 0);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700198 data.writeInt32(dumpsysPriority);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800199 status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700200 return err == NO_ERROR ? reply.readExceptionCode() : err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800201 }
202
Vishnu Nairf56042d2017-09-19 15:25:10 -0700203 virtual Vector<String16> listServices(int dumpsysPriority) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800204 Vector<String16> res;
205 int n = 0;
206
207 for (;;) {
208 Parcel data, reply;
209 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
210 data.writeInt32(n++);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700211 data.writeInt32(dumpsysPriority);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800212 status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply);
213 if (err != NO_ERROR)
214 break;
215 res.add(reply.readString16());
216 }
217 return res;
218 }
219};
220
221IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
222
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800223}; // namespace android