blob: 711143c34a7038fff2122087d543bb222c256b31 [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>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080027#include <utils/String8.h>
28#include <utils/SystemClock.h>
Iliyan Malchev273e1442017-04-10 14:09:04 -070029#include <utils/CallStack.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030
Mathias Agopian208059f2009-05-18 15:08:03 -070031#include <private/binder/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{
39 if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
Daniel Eratc2832702015-10-13 15:29:32 -060040
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080041 {
42 AutoMutex _l(gDefaultServiceManagerLock);
Todd Poynora7b0f042013-06-18 17:25:37 -070043 while (gDefaultServiceManager == NULL) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080044 gDefaultServiceManager = interface_cast<IServiceManager>(
45 ProcessState::self()->getContextObject(NULL));
Todd Poynora7b0f042013-06-18 17:25:37 -070046 if (gDefaultServiceManager == NULL)
47 sleep(1);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080048 }
49 }
Daniel Eratc2832702015-10-13 15:29:32 -060050
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080051 return gDefaultServiceManager;
52}
53
Jiyong Park47f876b2018-04-17 13:56:46 +090054#ifndef __ANDROID_VNDK__
55// IPermissionController is not accessible to vendors
56
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080057bool checkCallingPermission(const String16& permission)
58{
59 return checkCallingPermission(permission, NULL, NULL);
60}
61
62static String16 _permission("permission");
63
Mathias Agopian375f5632009-06-15 18:24:59 -070064
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080065bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid)
66{
67 IPCThreadState* ipcState = IPCThreadState::self();
Mathias Agopian375f5632009-06-15 18:24:59 -070068 pid_t pid = ipcState->getCallingPid();
69 uid_t uid = ipcState->getCallingUid();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080070 if (outPid) *outPid = pid;
Mathias Agopian375f5632009-06-15 18:24:59 -070071 if (outUid) *outUid = uid;
72 return checkPermission(permission, pid, uid);
73}
74
75bool checkPermission(const String16& permission, pid_t pid, uid_t uid)
76{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077 sp<IPermissionController> pc;
78 gDefaultServiceManagerLock.lock();
79 pc = gPermissionController;
80 gDefaultServiceManagerLock.unlock();
Daniel Eratc2832702015-10-13 15:29:32 -060081
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080082 int64_t startTime = 0;
83
84 while (true) {
85 if (pc != NULL) {
86 bool res = pc->checkPermission(permission, pid, uid);
87 if (res) {
88 if (startTime != 0) {
Steve Blocka19954a2012-01-04 20:05:49 +000089 ALOGI("Check passed after %d seconds for %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090 (int)((uptimeMillis()-startTime)/1000),
91 String8(permission).string(), uid, pid);
92 }
93 return res;
94 }
Daniel Eratc2832702015-10-13 15:29:32 -060095
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080096 // Is this a permission failure, or did the controller go away?
Marco Nelissen097ca272014-11-14 08:01:01 -080097 if (IInterface::asBinder(pc)->isBinderAlive()) {
Steve Block32397c12012-01-05 23:22:43 +000098 ALOGW("Permission failure: %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080099 String8(permission).string(), uid, pid);
100 return false;
101 }
Daniel Eratc2832702015-10-13 15:29:32 -0600102
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800103 // Object is dead!
104 gDefaultServiceManagerLock.lock();
105 if (gPermissionController == pc) {
106 gPermissionController = NULL;
107 }
108 gDefaultServiceManagerLock.unlock();
109 }
Daniel Eratc2832702015-10-13 15:29:32 -0600110
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800111 // Need to retrieve the permission controller.
112 sp<IBinder> binder = defaultServiceManager()->checkService(_permission);
113 if (binder == NULL) {
114 // Wait for the permission controller to come back...
115 if (startTime == 0) {
116 startTime = uptimeMillis();
Steve Blocka19954a2012-01-04 20:05:49 +0000117 ALOGI("Waiting to check permission %s from uid=%d pid=%d",
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800118 String8(permission).string(), uid, pid);
119 }
120 sleep(1);
121 } else {
122 pc = interface_cast<IPermissionController>(binder);
Daniel Eratc2832702015-10-13 15:29:32 -0600123 // Install the new permission controller, and try again.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800124 gDefaultServiceManagerLock.lock();
125 gPermissionController = pc;
126 gDefaultServiceManagerLock.unlock();
127 }
128 }
129}
130
Jiyong Park47f876b2018-04-17 13:56:46 +0900131#endif //__ANDROID_VNDK__
132
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800133// ----------------------------------------------------------------------
134
135class BpServiceManager : public BpInterface<IServiceManager>
136{
137public:
Chih-Hung Hsiehe2347b72016-04-25 15:41:05 -0700138 explicit BpServiceManager(const sp<IBinder>& impl)
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 : BpInterface<IServiceManager>(impl)
140 {
141 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700142
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800143 virtual sp<IBinder> getService(const String16& name) const
144 {
145 unsigned n;
146 for (n = 0; n < 5; n++){
Andy Hung6456e442016-12-06 09:40:01 -0800147 if (n > 0) {
Iliyan Malchev273e1442017-04-10 14:09:04 -0700148 if (!strcmp(ProcessState::self()->getDriverName().c_str(), "/dev/vndbinder")) {
149 ALOGI("Waiting for vendor service %s...", String8(name).string());
150 CallStack stack(LOG_TAG);
151 } else {
152 ALOGI("Waiting for service %s...", String8(name).string());
153 }
Andy Hung6456e442016-12-06 09:40:01 -0800154 sleep(1);
155 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800156 sp<IBinder> svc = checkService(name);
157 if (svc != NULL) return svc;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800158 }
159 return NULL;
160 }
Brad Fitzpatrick702ea9d2010-06-18 13:07:53 -0700161
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 virtual sp<IBinder> checkService( const String16& name) const
163 {
164 Parcel data, reply;
165 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
166 data.writeString16(name);
167 remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply);
168 return reply.readStrongBinder();
169 }
170
Dianne Hackborna94f1292012-02-09 16:12:18 -0800171 virtual status_t addService(const String16& name, const sp<IBinder>& service,
Vishnu Nairf56042d2017-09-19 15:25:10 -0700172 bool allowIsolated, int dumpsysPriority) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800173 Parcel data, reply;
174 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
175 data.writeString16(name);
176 data.writeStrongBinder(service);
Dianne Hackborna94f1292012-02-09 16:12:18 -0800177 data.writeInt32(allowIsolated ? 1 : 0);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700178 data.writeInt32(dumpsysPriority);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800179 status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
Brad Fitzpatrick837a0d02010-07-13 15:33:35 -0700180 return err == NO_ERROR ? reply.readExceptionCode() : err;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800181 }
182
Vishnu Nairf56042d2017-09-19 15:25:10 -0700183 virtual Vector<String16> listServices(int dumpsysPriority) {
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800184 Vector<String16> res;
185 int n = 0;
186
187 for (;;) {
188 Parcel data, reply;
189 data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
190 data.writeInt32(n++);
Vishnu Nairf56042d2017-09-19 15:25:10 -0700191 data.writeInt32(dumpsysPriority);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800192 status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply);
193 if (err != NO_ERROR)
194 break;
195 res.add(reply.readString16());
196 }
197 return res;
198 }
199};
200
201IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
202
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800203}; // namespace android