Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 26485229 authored by Devin Moore's avatar Devin Moore Committed by Steven Moreland
Browse files

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
parent 2147dc73
Loading
Loading
Loading
Loading
+4 −3
Original line number Original line Diff line number Diff line
@@ -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);
        }
        }
+20 −0
Original line number Original line Diff line number Diff line
@@ -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>>();