1 /*
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
19 #include <binder/IServiceManager.h>
20
21 #include <utils/Log.h>
22 #include <binder/IPCThreadState.h>
23 #include <binder/Parcel.h>
24 #include <utils/String8.h>
25 #include <utils/SystemClock.h>
26 #include <utils/CallStack.h>
27
28 #include <private/binder/Static.h>
29
30 #include <unistd.h>
31
32 namespace android {
33
defaultServiceManager()34 sp<IServiceManager> defaultServiceManager()
35 {
36 if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
37
38 {
39 AutoMutex _l(gDefaultServiceManagerLock);
40 while (gDefaultServiceManager == NULL) {
41 gDefaultServiceManager = interface_cast<IServiceManager>(
42 ProcessState::self()->getContextObject(NULL));
43 if (gDefaultServiceManager == NULL)
44 sleep(1);
45 }
46 }
47
48 return gDefaultServiceManager;
49 }
50
checkCallingPermission(const String16 & permission)51 bool checkCallingPermission(const String16& permission)
52 {
53 return checkCallingPermission(permission, NULL, NULL);
54 }
55
56 static String16 _permission("permission");
57
58
checkCallingPermission(const String16 & permission,int32_t * outPid,int32_t * outUid)59 bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid)
60 {
61 IPCThreadState* ipcState = IPCThreadState::self();
62 pid_t pid = ipcState->getCallingPid();
63 uid_t uid = ipcState->getCallingUid();
64 if (outPid) *outPid = pid;
65 if (outUid) *outUid = uid;
66 return checkPermission(permission, pid, uid);
67 }
68
checkPermission(const String16 & permission,pid_t pid,uid_t uid)69 bool checkPermission(const String16& permission, pid_t pid, uid_t uid)
70 {
71 sp<IPermissionController> pc;
72 gDefaultServiceManagerLock.lock();
73 pc = gPermissionController;
74 gDefaultServiceManagerLock.unlock();
75
76 int64_t startTime = 0;
77
78 while (true) {
79 if (pc != NULL) {
80 bool res = pc->checkPermission(permission, pid, uid);
81 if (res) {
82 if (startTime != 0) {
83 ALOGI("Check passed after %d seconds for %s from uid=%d pid=%d",
84 (int)((uptimeMillis()-startTime)/1000),
85 String8(permission).string(), uid, pid);
86 }
87 return res;
88 }
89
90 // Is this a permission failure, or did the controller go away?
91 if (IInterface::asBinder(pc)->isBinderAlive()) {
92 ALOGW("Permission failure: %s from uid=%d pid=%d",
93 String8(permission).string(), uid, pid);
94 return false;
95 }
96
97 // Object is dead!
98 gDefaultServiceManagerLock.lock();
99 if (gPermissionController == pc) {
100 gPermissionController = NULL;
101 }
102 gDefaultServiceManagerLock.unlock();
103 }
104
105 // Need to retrieve the permission controller.
106 sp<IBinder> binder = defaultServiceManager()->checkService(_permission);
107 if (binder == NULL) {
108 // Wait for the permission controller to come back...
109 if (startTime == 0) {
110 startTime = uptimeMillis();
111 ALOGI("Waiting to check permission %s from uid=%d pid=%d",
112 String8(permission).string(), uid, pid);
113 }
114 sleep(1);
115 } else {
116 pc = interface_cast<IPermissionController>(binder);
117 // Install the new permission controller, and try again.
118 gDefaultServiceManagerLock.lock();
119 gPermissionController = pc;
120 gDefaultServiceManagerLock.unlock();
121 }
122 }
123 }
124
125 // ----------------------------------------------------------------------
126
127 class BpServiceManager : public BpInterface<IServiceManager>
128 {
129 public:
BpServiceManager(const sp<IBinder> & impl)130 explicit BpServiceManager(const sp<IBinder>& impl)
131 : BpInterface<IServiceManager>(impl)
132 {
133 }
134
getService(const String16 & name) const135 virtual sp<IBinder> getService(const String16& name) const
136 {
137 unsigned n;
138 for (n = 0; n < 5; n++){
139 if (n > 0) {
140 if (!strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder")) {
141 ALOGI("Waiting for vendor service %s...", String8(name).string());
142 CallStack stack(LOG_TAG);
143 } else {
144 ALOGI("Waiting for service %s...", String8(name).string());
145 }
146 sleep(1);
147 }
148 sp<IBinder> svc = checkService(name);
149 if (svc != NULL) return svc;
150 }
151 return NULL;
152 }
153
checkService(const String16 & name) const154 virtual sp<IBinder> checkService( const String16& name) const
155 {
156 Parcel data, reply;
157 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
158 data.writeString16(name);
159 remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply);
160 return reply.readStrongBinder();
161 }
162
addService(const String16 & name,const sp<IBinder> & service,bool allowIsolated)163 virtual status_t addService(const String16& name, const sp<IBinder>& service,
164 bool allowIsolated)
165 {
166 Parcel data, reply;
167 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
168 data.writeString16(name);
169 data.writeStrongBinder(service);
170 data.writeInt32(allowIsolated ? 1 : 0);
171 status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
172 return err == NO_ERROR ? reply.readExceptionCode() : err;
173 }
174
listServices()175 virtual Vector<String16> listServices()
176 {
177 Vector<String16> res;
178 int n = 0;
179
180 for (;;) {
181 Parcel data, reply;
182 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
183 data.writeInt32(n++);
184 status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply);
185 if (err != NO_ERROR)
186 break;
187 res.add(reply.readString16());
188 }
189 return res;
190 }
191 };
192
193 IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
194
195 }; // namespace android
196