diff options
author | Devin Moore <devinmoore@google.com> | 2020-08-06 13:58:29 -0700 |
---|---|---|
committer | Steven Moreland <smoreland@google.com> | 2020-08-10 16:48:49 +0000 |
commit | 26485229358cb240dd2f2f8bf69e13a0885eb822 (patch) | |
tree | 951c7eb25ce22a3dd37d26dc16dcfb3ac18a0b2a | |
parent | 2147dc738bcbe00d82c3296f577687576d605b15 (diff) | |
download | native-26485229358cb240dd2f2f8bf69e13a0885eb822.tar.gz |
servicemanager: Overwrite old service when a new one is registered
We want to overwrite an old service with the same name as a new service
that is being added. The old one will be cleaned up as servicemanager
loses the reference to it.
Test: atest servicemanager_test && additional tests outlined in Bug
Bug: 162553096
Merged-In: I8835d9f7505367d83a0f29f1d928a07b3d57e56a
Change-Id: I8835d9f7505367d83a0f29f1d928a07b3d57e56a
-rw-r--r-- | cmds/servicemanager/ServiceManager.cpp | 7 | ||||
-rw-r--r-- | cmds/servicemanager/test_sm.cpp | 20 |
2 files changed, 24 insertions, 3 deletions
diff --git a/cmds/servicemanager/ServiceManager.cpp b/cmds/servicemanager/ServiceManager.cpp index 1f9892a262..e80c3210f0 100644 --- a/cmds/servicemanager/ServiceManager.cpp +++ b/cmds/servicemanager/ServiceManager.cpp @@ -213,17 +213,18 @@ Status ServiceManager::addService(const std::string& name, const sp<IBinder>& bi 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, .allowIsolated = allowIsolated, .dumpPriority = dumpPriority, .debugPid = ctx.debugPid, - }); + }; auto it = mNameToRegistrationCallback.find(name); if (it != mNameToRegistrationCallback.end()) { for (const sp<IServiceCallback>& cb : it->second) { - entry.first->second.guaranteeClient = true; + mNameToService[name].guaranteeClient = true; // permission checked in registerForNotifications cb->onRegistration(name, binder); } diff --git a/cmds/servicemanager/test_sm.cpp b/cmds/servicemanager/test_sm.cpp index 25245beaf7..fb9f9df856 100644 --- a/cmds/servicemanager/test_sm.cpp +++ b/cmds/servicemanager/test_sm.cpp @@ -135,6 +135,26 @@ TEST(AddService, HappyOverExistingService) { 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) { std::unique_ptr<MockAccess> access = std::make_unique<NiceMock<MockAccess>>(); |