Loading cmds/servicemanager/ServiceManager.cpp +4 −3 Original line number Original line Diff line number Diff line Loading @@ -213,17 +213,18 @@ Status ServiceManager::addService(const std::string& name, const sp<IBinder>& bi return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); } } auto entry = mNameToService.emplace(name, Service { // Overwrite the old service if it exists mNameToService[name] = Service { .binder = binder, .binder = binder, .allowIsolated = allowIsolated, .allowIsolated = allowIsolated, .dumpPriority = dumpPriority, .dumpPriority = dumpPriority, .debugPid = ctx.debugPid, .debugPid = ctx.debugPid, }); }; auto it = mNameToRegistrationCallback.find(name); auto it = mNameToRegistrationCallback.find(name); if (it != mNameToRegistrationCallback.end()) { if (it != mNameToRegistrationCallback.end()) { for (const sp<IServiceCallback>& cb : it->second) { for (const sp<IServiceCallback>& cb : it->second) { entry.first->second.guaranteeClient = true; mNameToService[name].guaranteeClient = true; // permission checked in registerForNotifications // permission checked in registerForNotifications cb->onRegistration(name, binder); cb->onRegistration(name, binder); } } Loading cmds/servicemanager/test_sm.cpp +20 −0 Original line number Original line Diff line number Diff line Loading @@ -135,6 +135,26 @@ TEST(AddService, HappyOverExistingService) { IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); } } TEST(AddService, OverwriteExistingService) { auto sm = getPermissiveServiceManager(); sp<IBinder> serviceA = getBinder(); EXPECT_TRUE(sm->addService("foo", serviceA, false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); sp<IBinder> outA; EXPECT_TRUE(sm->getService("foo", &outA).isOk()); EXPECT_EQ(serviceA, outA); // serviceA should be overwritten by serviceB sp<IBinder> serviceB = getBinder(); EXPECT_TRUE(sm->addService("foo", serviceB, false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); sp<IBinder> outB; EXPECT_TRUE(sm->getService("foo", &outB).isOk()); EXPECT_EQ(serviceB, outB); } TEST(AddService, NoPermissions) { TEST(AddService, NoPermissions) { std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); Loading Loading
cmds/servicemanager/ServiceManager.cpp +4 −3 Original line number Original line Diff line number Diff line Loading @@ -213,17 +213,18 @@ Status ServiceManager::addService(const std::string& name, const sp<IBinder>& bi return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); return Status::fromExceptionCode(Status::EX_ILLEGAL_STATE); } } auto entry = mNameToService.emplace(name, Service { // Overwrite the old service if it exists mNameToService[name] = Service { .binder = binder, .binder = binder, .allowIsolated = allowIsolated, .allowIsolated = allowIsolated, .dumpPriority = dumpPriority, .dumpPriority = dumpPriority, .debugPid = ctx.debugPid, .debugPid = ctx.debugPid, }); }; auto it = mNameToRegistrationCallback.find(name); auto it = mNameToRegistrationCallback.find(name); if (it != mNameToRegistrationCallback.end()) { if (it != mNameToRegistrationCallback.end()) { for (const sp<IServiceCallback>& cb : it->second) { for (const sp<IServiceCallback>& cb : it->second) { entry.first->second.guaranteeClient = true; mNameToService[name].guaranteeClient = true; // permission checked in registerForNotifications // permission checked in registerForNotifications cb->onRegistration(name, binder); cb->onRegistration(name, binder); } } Loading
cmds/servicemanager/test_sm.cpp +20 −0 Original line number Original line Diff line number Diff line Loading @@ -135,6 +135,26 @@ TEST(AddService, HappyOverExistingService) { IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); } } TEST(AddService, OverwriteExistingService) { auto sm = getPermissiveServiceManager(); sp<IBinder> serviceA = getBinder(); EXPECT_TRUE(sm->addService("foo", serviceA, false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); sp<IBinder> outA; EXPECT_TRUE(sm->getService("foo", &outA).isOk()); EXPECT_EQ(serviceA, outA); // serviceA should be overwritten by serviceB sp<IBinder> serviceB = getBinder(); EXPECT_TRUE(sm->addService("foo", serviceB, false /*allowIsolated*/, IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT).isOk()); sp<IBinder> outB; EXPECT_TRUE(sm->getService("foo", &outB).isOk()); EXPECT_EQ(serviceB, outB); } TEST(AddService, NoPermissions) { TEST(AddService, NoPermissions) { std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); Loading