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