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

Commit 1cf0e904 authored by David Anderson's avatar David Anderson Committed by Gerrit Code Review
Browse files

Merge changes Ifddb5cb8,I8735ef6c,Ifd33c28e into main

* changes:
  snapuserd: Factor setpriority/settid calls into a helper.
  snapuserd: Add a harness to run tests without dm-user specific code.
  snapuserd: Add an IBlockServerFactory abstraction.
parents 0c0745e6 dba77ad7
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -72,6 +72,7 @@ cc_library_static {
        "user-space-merge/snapuserd_transitions.cpp",
        "user-space-merge/snapuserd_verify.cpp",
        "user-space-merge/worker.cpp",
        "utility.cpp",
    ],
    static_libs: [
        "libbase",
@@ -219,6 +220,7 @@ cc_test {
        "libsnapshot_cow_defaults",
    ],
    srcs: [
        "testing/dm_user_harness.cpp",
        "user-space-merge/snapuserd_test.cpp",
    ],
    shared_libs: [
+6 −0
Original line number Diff line number Diff line
@@ -142,5 +142,11 @@ std::unique_ptr<IBlockServer> DmUserBlockServerOpener::Open(IBlockServer::Delega
    return std::make_unique<DmUserBlockServer>(misc_name_, std::move(fd), delegate, buffer_size);
}

std::shared_ptr<IBlockServerOpener> DmUserBlockServerFactory::CreateOpener(
        const std::string& misc_name) {
    auto dm_path = "/dev/dm-user/" + misc_name;
    return std::make_shared<DmUserBlockServerOpener>(misc_name, dm_path);
}

}  // namespace snapshot
}  // namespace android
+8 −0
Original line number Diff line number Diff line
@@ -83,5 +83,13 @@ class IBlockServerOpener {
                                               size_t buffer_size) = 0;
};

class IBlockServerFactory {
  public:
    virtual ~IBlockServerFactory() {}

    // Return a new IBlockServerOpener given a unique device name.
    virtual std::shared_ptr<IBlockServerOpener> CreateOpener(const std::string& misc_name) = 0;
};

}  // namespace snapshot
}  // namespace android
+5 −0
Original line number Diff line number Diff line
@@ -59,5 +59,10 @@ class DmUserBlockServerOpener : public IBlockServerOpener {
    std::string dm_user_path_;
};

class DmUserBlockServerFactory : public IBlockServerFactory {
  public:
    std::shared_ptr<IBlockServerOpener> CreateOpener(const std::string& misc_name) override;
};

}  // namespace snapshot
}  // namespace android
+67 −0
Original line number Diff line number Diff line
// Copyright (C) 2023 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "dm_user_harness.h"

#include <fcntl.h>

#include <android-base/file.h>
#include <fs_mgr/file_wait.h>
#include <libdm/dm.h>
#include <snapuserd/dm_user_block_server.h>

namespace android {
namespace snapshot {

using namespace std::chrono_literals;
using android::base::unique_fd;

DmUserDevice::DmUserDevice(std::unique_ptr<Tempdevice>&& dev) : dev_(std::move(dev)) {}

const std::string& DmUserDevice::GetPath() {
    return dev_->path();
}

bool DmUserDevice::Destroy() {
    return dev_->Destroy();
}

DmUserTestHarness::DmUserTestHarness() {
    block_server_factory_ = std::make_unique<DmUserBlockServerFactory>();
}

std::unique_ptr<IUserDevice> DmUserTestHarness::CreateUserDevice(const std::string& dev_name,
                                                                 const std::string& misc_name,
                                                                 uint64_t num_sectors) {
    android::dm::DmTable dmuser_table;
    dmuser_table.Emplace<android::dm::DmTargetUser>(0, num_sectors, misc_name);
    auto dev = std::make_unique<Tempdevice>(dev_name, dmuser_table);
    if (!dev->valid()) {
        return nullptr;
    }

    auto misc_device = "/dev/dm-user/" + misc_name;
    if (!android::fs_mgr::WaitForFile(misc_device, 10s)) {
        return nullptr;
    }

    return std::make_unique<DmUserDevice>(std::move(dev));
}

IBlockServerFactory* DmUserTestHarness::GetBlockServerFactory() {
    return block_server_factory_.get();
}

}  // namespace snapshot
}  // namespace android
Loading