IServiceManager.cpp 9.0 KB

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