IServiceManager.cpp revision 208059f67ed2dd9fa025e07fcb6954d3cb61c79e
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 <utils/IServiceManager.h>
20
21#include <utils/Debug.h>
22#include <utils/IPCThreadState.h>
23#include <utils/Log.h>
24#include <utils/Parcel.h>
25#include <utils/String8.h>
26#include <utils/SystemClock.h>
27
28#include <private/binder/Static.h>
29
30#include <unistd.h>
31
32namespace android {
33
34sp<IServiceManager> defaultServiceManager()
35{
36    if (gDefaultServiceManager != NULL) return gDefaultServiceManager;
37
38    {
39        AutoMutex _l(gDefaultServiceManagerLock);
40        if (gDefaultServiceManager == NULL) {
41            gDefaultServiceManager = interface_cast<IServiceManager>(
42                ProcessState::self()->getContextObject(NULL));
43        }
44    }
45
46    return gDefaultServiceManager;
47}
48
49bool checkCallingPermission(const String16& permission)
50{
51    return checkCallingPermission(permission, NULL, NULL);
52}
53
54static String16 _permission("permission");
55
56bool checkCallingPermission(const String16& permission, int32_t* outPid, int32_t* outUid)
57{
58    IPCThreadState* ipcState = IPCThreadState::self();
59    int32_t pid = ipcState->getCallingPid();
60    int32_t uid = ipcState->getCallingUid();
61    if (outPid) *outPid = pid;
62    if (outUid) *outUid= uid;
63
64    sp<IPermissionController> pc;
65    gDefaultServiceManagerLock.lock();
66    pc = gPermissionController;
67    gDefaultServiceManagerLock.unlock();
68
69    int64_t startTime = 0;
70
71    while (true) {
72        if (pc != NULL) {
73            bool res = pc->checkPermission(permission, pid, uid);
74            if (res) {
75                if (startTime != 0) {
76                    LOGI("Check passed after %d seconds for %s from uid=%d pid=%d",
77                            (int)((uptimeMillis()-startTime)/1000),
78                            String8(permission).string(), uid, pid);
79                }
80                return res;
81            }
82
83            // Is this a permission failure, or did the controller go away?
84            if (pc->asBinder()->isBinderAlive()) {
85                LOGW("Permission failure: %s from uid=%d pid=%d",
86                        String8(permission).string(), uid, pid);
87                return false;
88            }
89
90            // Object is dead!
91            gDefaultServiceManagerLock.lock();
92            if (gPermissionController == pc) {
93                gPermissionController = NULL;
94            }
95            gDefaultServiceManagerLock.unlock();
96        }
97
98        // Need to retrieve the permission controller.
99        sp<IBinder> binder = defaultServiceManager()->checkService(_permission);
100        if (binder == NULL) {
101            // Wait for the permission controller to come back...
102            if (startTime == 0) {
103                startTime = uptimeMillis();
104                LOGI("Waiting to check permission %s from uid=%d pid=%d",
105                        String8(permission).string(), uid, pid);
106            }
107            sleep(1);
108        } else {
109            pc = interface_cast<IPermissionController>(binder);
110            // Install the new permission controller, and try again.
111            gDefaultServiceManagerLock.lock();
112            gPermissionController = pc;
113            gDefaultServiceManagerLock.unlock();
114        }
115    }
116}
117
118// ----------------------------------------------------------------------
119
120class BpServiceManager : public BpInterface<IServiceManager>
121{
122public:
123    BpServiceManager(const sp<IBinder>& impl)
124        : BpInterface<IServiceManager>(impl)
125    {
126    }
127
128    virtual sp<IBinder> getService(const String16& name) const
129    {
130        unsigned n;
131        for (n = 0; n < 5; n++){
132            sp<IBinder> svc = checkService(name);
133            if (svc != NULL) return svc;
134            LOGI("Waiting for sevice %s...\n", String8(name).string());
135            sleep(1);
136        }
137        return NULL;
138    }
139
140    virtual sp<IBinder> checkService( const String16& name) const
141    {
142        Parcel data, reply;
143        data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
144        data.writeString16(name);
145        remote()->transact(CHECK_SERVICE_TRANSACTION, data, &reply);
146        return reply.readStrongBinder();
147    }
148
149    virtual status_t addService(const String16& name, const sp<IBinder>& service)
150    {
151        Parcel data, reply;
152        data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
153        data.writeString16(name);
154        data.writeStrongBinder(service);
155        status_t err = remote()->transact(ADD_SERVICE_TRANSACTION, data, &reply);
156        return err == NO_ERROR ? reply.readInt32() : err;
157    }
158
159    virtual Vector<String16> listServices()
160    {
161        Vector<String16> res;
162        int n = 0;
163
164        for (;;) {
165            Parcel data, reply;
166            data.writeInterfaceToken(IServiceManager::getInterfaceDescriptor());
167            data.writeInt32(n++);
168            status_t err = remote()->transact(LIST_SERVICES_TRANSACTION, data, &reply);
169            if (err != NO_ERROR)
170                break;
171            res.add(reply.readString16());
172        }
173        return res;
174    }
175};
176
177IMPLEMENT_META_INTERFACE(ServiceManager, "android.os.IServiceManager");
178
179// ----------------------------------------------------------------------
180
181#define CHECK_INTERFACE(interface, data, reply) \
182        do { if (!data.enforceInterface(interface::getInterfaceDescriptor())) { \
183            LOGW("Call incorrectly routed to " #interface); \
184            return PERMISSION_DENIED; \
185        } } while (0)
186
187status_t BnServiceManager::onTransact(
188    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
189{
190    //printf("ServiceManager received: "); data.print();
191    switch(code) {
192        case GET_SERVICE_TRANSACTION: {
193            CHECK_INTERFACE(IServiceManager, data, reply);
194            String16 which = data.readString16();
195            sp<IBinder> b = const_cast<BnServiceManager*>(this)->getService(which);
196            reply->writeStrongBinder(b);
197            return NO_ERROR;
198        } break;
199        case CHECK_SERVICE_TRANSACTION: {
200            CHECK_INTERFACE(IServiceManager, data, reply);
201            String16 which = data.readString16();
202            sp<IBinder> b = const_cast<BnServiceManager*>(this)->checkService(which);
203            reply->writeStrongBinder(b);
204            return NO_ERROR;
205        } break;
206        case ADD_SERVICE_TRANSACTION: {
207            CHECK_INTERFACE(IServiceManager, data, reply);
208            String16 which = data.readString16();
209            sp<IBinder> b = data.readStrongBinder();
210            status_t err = addService(which, b);
211            reply->writeInt32(err);
212            return NO_ERROR;
213        } break;
214        case LIST_SERVICES_TRANSACTION: {
215            CHECK_INTERFACE(IServiceManager, data, reply);
216            Vector<String16> list = listServices();
217            const size_t N = list.size();
218            reply->writeInt32(N);
219            for (size_t i=0; i<N; i++) {
220                reply->writeString16(list[i]);
221            }
222            return NO_ERROR;
223        } break;
224        default:
225            return BBinder::onTransact(code, data, reply, flags);
226    }
227}
228
229}; // namespace android
230
231