blob: 3aeff2eda3889714101e24415d1107986675af82 [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>
Mathias Agopianc5b2c0b2009-05-19 19:08:10 -070023#include <binder/Parcel.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <utils/String8.h>
25#include <utils/SystemClock.h>
26
Mathias Agopian208059f2009-05-18 15:08:03 -070027#include <private/binder/Static.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080028
29#include <unistd.h>
30
31namespace android {
32
33sp<IServiceManager> defaultServiceManager()
34{
35 if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
Daniel Eratc2832702015-10-13 15:29:32 -060036
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037 {
38 AutoMutex _l(gDefaultServiceManagerLock);
Todd Poynora7b0f042013-06-18 17:25:37 -070039 while (gDefaultServiceManager == NULL) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080040 gDefaultServiceManager = interface_cast<IServiceManager>(
41 ProcessState::self()->getContextObject(NULL));
Todd Poynora7b0f042013-06-18 17:25:37 -070042 if (gDefaultServiceManager == NULL)
43 sleep(1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044 }
45 }
Daniel Eratc2832702015-10-13 15:29:32 -060046
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080047 return gDefaultServiceManager;
48}
49
50bool checkCallingPermission(const String16& permission)
51{
52 return checkCallingPermission(permission, NULL, NULL);
53}
54
55static String16 _permission("permission");
56
Mathias Agopian375f5632009-06-15 18:24:59 -070057
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080058bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid)
59{
60 IPCThreadState* ipcState = IPCThreadState::self();
Mathias Agopian375f5632009-06-15 18:24:59 -070061 pid_t pid = ipcState->getCallingPid();
62 uid_t uid = ipcState->getCallingUid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080063 if (outPid) *outPid = pid;
Mathias Agopian375f5632009-06-15 18:24:59 -070064 if (outUid) *outUid = uid;
65 return checkPermission(permission, pid, uid);
66}
67
68bool checkPermission(const String16& permission, pid_t pid, uid_t uid)
69{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 sp<IPermissionController> pc;
71 gDefaultServiceManagerLock.lock();
72 pc = gPermissionController;
73 gDefaultServiceManagerLock.unlock();
Daniel Eratc2832702015-10-13 15:29:32 -060074
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075 int64_t startTime = 0;
76
77 while (true) {
78 if (pc != NULL) {
79 bool res = pc->checkPermission(permission, pid, uid);
80 if (res) {
81 if (startTime != 0) {
Steve Blocka19954a2012-01-04 20:05:49 +000082 ALOGI("Check passed after %d seconds for %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083 (int)((uptimeMillis()-startTime)/1000),
84 String8(permission).string(), uid, pid);
85 }
86 return res;
87 }
Daniel Eratc2832702015-10-13 15:29:32 -060088
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080089 // Is this a permission failure, or did the controller go away?
Marco Nelissen097ca272014-11-14 08:01:01 -080090 if (IInterface::asBinder(pc)->isBinderAlive()) {
Steve Block32397c12012-01-05 23:22:43 +000091 ALOGW("Permission failure: %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080092 String8(permission).string(), uid, pid);
93 return false;
94 }
Daniel Eratc2832702015-10-13 15:29:32 -060095
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 // Object is dead!
97 gDefaultServiceManagerLock.lock();
98 if (gPermissionController == pc) {
99 gPermissionController = NULL;
100 }
101 gDefaultServiceManagerLock.unlock();
102 }
Daniel Eratc2832702015-10-13 15:29:32 -0600103
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800104 // Need to retrieve the permission controller.
105 sp<IBinder> binder = defaultServiceManager()->checkService(_permission);
106 if (binder == NULL) {
107 // Wait for the permission controller to come back...
108 if (startTime == 0) {
109 startTime = uptimeMillis();
Steve Blocka19954a2012-01-04 20:05:49 +0000110 ALOGI("Waiting to check permission %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 String8(permission).string(), uid, pid);
112 }
113 sleep(1);
114 } else {
115 pc = interface_cast<IPermissionController>(binder);
Daniel Eratc2832702015-10-13 15:29:32 -0600116 // Install the new permission controller, and try again.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800117 gDefaultServiceManagerLock.lock();
118 gPermissionController = pc;
119 gDefaultServiceManagerLock.unlock();
120 }
121 }
122}
123
124// ----------------------------------------------------------------------
125
126class BpServiceManager : public BpInterface<IServiceManager>
127{
128public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700129 explicit BpServiceManager(const sp<IBinder>& impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800130 : BpInterface<IServiceManager>(impl)
131 {
132 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700133
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800134 virtual sp<IBinder> getService(const String16& name) const
135 {
136 unsigned n;
137 for (n = 0; n < 5; n++){
Andy Hung6456e442016-12-06 09:40:01 -0800138 if (n > 0) {
139 ALOGI("Waiting for service %s...", String8(name).string());
140 sleep(1);
141 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800142 sp<IBinder> svc = checkService(name);
143 if (svc != NULL) return svc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800144 }
145 return NULL;
146 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700147
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148 virtual sp<IBinder> checkService( const String16& name) const
149 {
150 Parcel data, reply;
151 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
152 data.writeString16(name);
153 remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply);
154 return reply.readStrongBinder();
155 }
156
Dianne Hackborna94f1292012-02-09 16:12:18 -0800157 virtual status_t addService(const String16& name, const sp<IBinder>& service,
158 bool allowIsolated)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800159 {
160 Parcel data, reply;
161 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
162 data.writeString16(name);
163 data.writeStrongBinder(service);
Dianne Hackborna94f1292012-02-09 16:12:18 -0800164 data.writeInt32(allowIsolated ? 1 : 0);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800165 status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700166 return err == NO_ERROR ? reply.readExceptionCode() : err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800167 }
168
169 virtual Vector<String16> listServices()
170 {
171 Vector<String16> res;
172 int n = 0;
173
174 for (;;) {
175 Parcel data, reply;
176 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
177 data.writeInt32(n++);
178 status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply);
179 if (err != NO_ERROR)
180 break;
181 res.add(reply.readString16());
182 }
183 return res;
184 }
185};
186
187IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
188
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800189}; // namespace android