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