blob: 61f24d6805bec64d6c42d5ee8bc2662254ca16aa [file] [log] [blame]
Mathias Agopian7922fa22009-05-18 15:08:03 -07001/*
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 Agopian16475702009-05-19 19:08:10 -070019#include <binder/IServiceManager.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070020
Mathias Agopian7922fa22009-05-18 15:08:03 -070021#include <utils/Log.h>
Mathias Agopian5046a7e2009-06-15 18:24:59 -070022#include <binder/IPCThreadState.h>
Mathias Agopian16475702009-05-19 19:08:10 -070023#include <binder/Parcel.h>
Mathias Agopian7922fa22009-05-18 15:08:03 -070024#include <utils/String8.h>
25#include <utils/SystemClock.h>
26
27#include <private/binder/Static.h>
28
29#include <unistd.h>
30
31namespace android {
32
33sp<IServiceManager> defaultServiceManager()
34{
35 if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
Daniel Erat43b12f42015-10-13 15:29:32 -060036
Mathias Agopian7922fa22009-05-18 15:08:03 -070037 {
38 AutoMutex _l(gDefaultServiceManagerLock);
Todd Poynore6df0c12013-06-18 17:25:37 -070039 while (gDefaultServiceManager == NULL) {
Mathias Agopian7922fa22009-05-18 15:08:03 -070040 gDefaultServiceManager = interface_cast<IServiceManager>(
41 ProcessState::self()->getContextObject(NULL));
Todd Poynore6df0c12013-06-18 17:25:37 -070042 if (gDefaultServiceManager == NULL)
43 sleep(1);
Mathias Agopian7922fa22009-05-18 15:08:03 -070044 }
45 }
Daniel Erat43b12f42015-10-13 15:29:32 -060046
Mathias Agopian7922fa22009-05-18 15:08:03 -070047 return gDefaultServiceManager;
48}
49
50bool checkCallingPermission(const String16& permission)
51{
52 return checkCallingPermission(permission, NULL, NULL);
53}
54
55static String16 _permission("permission");
56
Mathias Agopian5046a7e2009-06-15 18:24:59 -070057
Mathias Agopian7922fa22009-05-18 15:08:03 -070058bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid)
59{
60 IPCThreadState* ipcState = IPCThreadState::self();
Mathias Agopian5046a7e2009-06-15 18:24:59 -070061 pid_t pid = ipcState->getCallingPid();
62 uid_t uid = ipcState->getCallingUid();
Mathias Agopian7922fa22009-05-18 15:08:03 -070063 if (outPid) *outPid = pid;
Mathias Agopian5046a7e2009-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{
Daniel Erat43b12f42015-10-13 15:29:32 -060070#ifdef __BRILLO__
71 // Brillo doesn't currently run ActivityManager or support framework permissions.
72 return true;
73#endif
74
Mathias Agopian7922fa22009-05-18 15:08:03 -070075 sp<IPermissionController> pc;
76 gDefaultServiceManagerLock.lock();
77 pc = gPermissionController;
78 gDefaultServiceManagerLock.unlock();
Daniel Erat43b12f42015-10-13 15:29:32 -060079
Mathias Agopian7922fa22009-05-18 15:08:03 -070080 int64_t startTime = 0;
81
82 while (true) {
83 if (pc != NULL) {
84 bool res = pc->checkPermission(permission, pid, uid);
85 if (res) {
86 if (startTime != 0) {
Steve Block93cf8542012-01-04 20:05:49 +000087 ALOGI("Check passed after %d seconds for %s from uid=%d pid=%d",
Mathias Agopian7922fa22009-05-18 15:08:03 -070088 (int)((uptimeMillis()-startTime)/1000),
89 String8(permission).string(), uid, pid);
90 }
91 return res;
92 }
Daniel Erat43b12f42015-10-13 15:29:32 -060093
Mathias Agopian7922fa22009-05-18 15:08:03 -070094 // Is this a permission failure, or did the controller go away?
Marco Nelissen4112eef2014-11-14 08:01:01 -080095 if (IInterface::asBinder(pc)->isBinderAlive()) {
Steve Blockd8e19162012-01-05 23:22:43 +000096 ALOGW("Permission failure: %s from uid=%d pid=%d",
Mathias Agopian7922fa22009-05-18 15:08:03 -070097 String8(permission).string(), uid, pid);
98 return false;
99 }
Daniel Erat43b12f42015-10-13 15:29:32 -0600100
Mathias Agopian7922fa22009-05-18 15:08:03 -0700101 // Object is dead!
102 gDefaultServiceManagerLock.lock();
103 if (gPermissionController == pc) {
104 gPermissionController = NULL;
105 }
106 gDefaultServiceManagerLock.unlock();
107 }
Daniel Erat43b12f42015-10-13 15:29:32 -0600108
Mathias Agopian7922fa22009-05-18 15:08:03 -0700109 // Need to retrieve the permission controller.
110 sp<IBinder> binder = defaultServiceManager()->checkService(_permission);
111 if (binder == NULL) {
112 // Wait for the permission controller to come back...
113 if (startTime == 0) {
114 startTime = uptimeMillis();
Steve Block93cf8542012-01-04 20:05:49 +0000115 ALOGI("Waiting to check permission %s from uid=%d pid=%d",
Mathias Agopian7922fa22009-05-18 15:08:03 -0700116 String8(permission).string(), uid, pid);
117 }
118 sleep(1);
119 } else {
120 pc = interface_cast<IPermissionController>(binder);
Daniel Erat43b12f42015-10-13 15:29:32 -0600121 // Install the new permission controller, and try again.
Mathias Agopian7922fa22009-05-18 15:08:03 -0700122 gDefaultServiceManagerLock.lock();
123 gPermissionController = pc;
124 gDefaultServiceManagerLock.unlock();
125 }
126 }
127}
128
129// ----------------------------------------------------------------------
130
131class BpServiceManager : public BpInterface<IServiceManager>
132{
133public:
134 BpServiceManager(const sp<IBinder>& impl)
135 : BpInterface<IServiceManager>(impl)
136 {
137 }
Brad Fitzpatrick94c36342010-06-18 13:07:53 -0700138
Mathias Agopian7922fa22009-05-18 15:08:03 -0700139 virtual sp<IBinder> getService(const String16& name) const
140 {
141 unsigned n;
142 for (n = 0; n < 5; n++){
143 sp<IBinder> svc = checkService(name);
144 if (svc != NULL) return svc;
Steve Block93cf8542012-01-04 20:05:49 +0000145 ALOGI("Waiting for service %s...\n", String8(name).string());
Mathias Agopian7922fa22009-05-18 15:08:03 -0700146 sleep(1);
147 }
148 return NULL;
149 }
Brad Fitzpatrick94c36342010-06-18 13:07:53 -0700150
Mathias Agopian7922fa22009-05-18 15:08:03 -0700151 virtual sp<IBinder> checkService( const String16& name) const
152 {
153 Parcel data, reply;
154 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
155 data.writeString16(name);
156 remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply);
157 return reply.readStrongBinder();
158 }
159
Dianne Hackborna3220e52012-02-09 16:12:18 -0800160 virtual status_t addService(const String16& name, const sp<IBinder>& service,
161 bool allowIsolated)
Mathias Agopian7922fa22009-05-18 15:08:03 -0700162 {
163 Parcel data, reply;
164 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
165 data.writeString16(name);
166 data.writeStrongBinder(service);
Dianne Hackborna3220e52012-02-09 16:12:18 -0800167 data.writeInt32(allowIsolated ? 1 : 0);
Mathias Agopian7922fa22009-05-18 15:08:03 -0700168 status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
Brad Fitzpatrick67aa5c92010-07-13 15:33:35 -0700169 return err == NO_ERROR ? reply.readExceptionCode() : err;
Mathias Agopian7922fa22009-05-18 15:08:03 -0700170 }
171
172 virtual Vector<String16> listServices()
173 {
174 Vector<String16> res;
175 int n = 0;
176
177 for (;;) {
178 Parcel data, reply;
179 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
180 data.writeInt32(n++);
181 status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply);
182 if (err != NO_ERROR)
183 break;
184 res.add(reply.readString16());
185 }
186 return res;
187 }
188};
189
190IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
191
Mathias Agopian7922fa22009-05-18 15:08:03 -0700192}; // namespace android