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

Commit 819ca32a authored by David Anderson's avatar David Anderson Committed by Gerrit Code Review
Browse files

Merge changes from topic "snapuserd-gflags"

* changes:
  snapuserd: Add a no-socket mode.
  snapuserd: Use gflags for arguments.
parents 56bcb02b a056eca5
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -423,10 +423,11 @@ cc_defaults {
        "libbase",
        "libbrotli",
        "libcutils_sockets",
        "liblog",
        "libdm",
        "libz",
        "libgflags",
        "liblog",
        "libsnapshot_cow",
        "libz",
    ],
}

+1 −1
Original line number Diff line number Diff line
@@ -169,7 +169,7 @@ void CowSnapuserdTest::StartSnapuserdDaemon() {
    ASSERT_GE(pid, 0);
    if (pid == 0) {
        std::string arg0 = "/system/bin/snapuserd";
        std::string arg1 = kSnapuserdSocketTest;
        std::string arg1 = "-socket="s + kSnapuserdSocketTest;
        char* const argv[] = {arg0.data(), arg1.data(), nullptr};
        ASSERT_GE(execv(arg0.c_str(), argv), 0);
    } else {
+31 −9
Original line number Diff line number Diff line
@@ -17,18 +17,41 @@
#include "snapuserd_daemon.h"

#include <android-base/logging.h>
#include <android-base/strings.h>
#include <gflags/gflags.h>
#include <libsnapshot/snapuserd_client.h>

#include "snapuserd_server.h"

DEFINE_string(socket, android::snapshot::kSnapuserdSocket, "Named socket or socket path.");
DEFINE_bool(no_socket, false,
            "If true, no socket is used. Each additional argument is an INIT message.");

namespace android {
namespace snapshot {

bool Daemon::StartServer(const std::string& socketname) {
    if (!server_.Start(socketname)) {
        LOG(ERROR) << "Snapuserd daemon failed to start...";
        exit(EXIT_FAILURE);
bool Daemon::StartServer(int argc, char** argv) {
    int arg_start = gflags::ParseCommandLineFlags(&argc, &argv, true);

    if (!FLAGS_no_socket) {
        return server_.Start(FLAGS_socket);
    }

    for (int i = arg_start; i < argc; i++) {
        auto parts = android::base::Split(argv[i], ",");
        if (parts.size() != 3) {
            LOG(ERROR) << "Malformed message, expected three sub-arguments.";
            return false;
        }
        auto handler = server_.AddHandler(parts[0], parts[1], parts[2]);
        if (!handler || !server_.StartHandler(handler)) {
            return false;
        }
    }

    // Skip the accept() call to avoid spurious log spam. The server will still
    // run until all handlers have completed.
    server_.SetTerminating();
    return true;
}

@@ -94,16 +117,15 @@ void Daemon::SignalHandler(int signal) {
}  // namespace snapshot
}  // namespace android

int main([[maybe_unused]] int argc, char** argv) {
int main(int argc, char** argv) {
    android::base::InitLogging(argv, &android::base::KernelLogger);

    android::snapshot::Daemon& daemon = android::snapshot::Daemon::Instance();

    std::string socket = android::snapshot::kSnapuserdSocket;
    if (argc >= 2) {
        socket = argv[1];
    if (!daemon.StartServer(argc, argv)) {
        LOG(ERROR) << "Snapuserd daemon failed to start.";
        exit(EXIT_FAILURE);
    }
    daemon.StartServer(socket);
    daemon.Run();

    return 0;
+4 −1
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

#include <poll.h>

#include <string>
#include <vector>

#include "snapuserd_server.h"

namespace android {
@@ -32,7 +35,7 @@ class Daemon {
        return instance;
    }

    bool StartServer(const std::string& socketname);
    bool StartServer(int argc, char** argv);
    void Run();
    void Interrupt();

+38 −19
Original line number Diff line number Diff line
@@ -116,7 +116,7 @@ bool SnapuserdServer::Receivemsg(android::base::borrowed_fd fd, const std::strin
    switch (op) {
        case DaemonOperations::INIT: {
            // Message format:
            // init,<misc_name>,<cow_device_path>,<control_device>
            // init,<misc_name>,<cow_device_path>,<backing_device>
            //
            // Reads the metadata and send the number of sectors
            if (out.size() != 4) {
@@ -124,24 +124,12 @@ bool SnapuserdServer::Receivemsg(android::base::borrowed_fd fd, const std::strin
                return Sendmsg(fd, "fail");
            }

            auto snapuserd = std::make_unique<Snapuserd>(out[1], out[2], out[3]);
            if (!snapuserd->InitCowDevice()) {
                LOG(ERROR) << "Failed to initialize Snapuserd";
            auto handler = AddHandler(out[1], out[2], out[3]);
            if (!handler) {
                return Sendmsg(fd, "fail");
            }

            std::string retval = "success," + std::to_string(snapuserd->GetNumSectors());

            auto handler = std::make_unique<DmUserHandler>(std::move(snapuserd));
            {
                std::lock_guard<std::mutex> lock(lock_);
                if (FindHandler(&lock, out[1]) != dm_users_.end()) {
                    LOG(ERROR) << "Handler already exists: " << out[1];
                    return Sendmsg(fd, "fail");
                }
                dm_users_.push_back(std::move(handler));
            }

            auto retval = "success," + std::to_string(handler->snapuserd()->GetNumSectors());
            return Sendmsg(fd, retval);
        }
        case DaemonOperations::START: {
@@ -164,11 +152,9 @@ bool SnapuserdServer::Receivemsg(android::base::borrowed_fd fd, const std::strin
                LOG(ERROR) << "Tried to re-attach control device: " << out[1];
                return Sendmsg(fd, "fail");
            }
            if (!((*iter)->snapuserd()->InitBackingAndControlDevice())) {
                LOG(ERROR) << "Failed to initialize control device: " << out[1];
            if (!StartHandler(*iter)) {
                return Sendmsg(fd, "fail");
            }
            (*iter)->thread() = std::thread(std::bind(&SnapuserdServer::RunThread, this, *iter));
            return Sendmsg(fd, "success");
        }
        case DaemonOperations::STOP: {
@@ -340,6 +326,39 @@ void SnapuserdServer::Interrupt() {
    SetTerminating();
}

std::shared_ptr<DmUserHandler> SnapuserdServer::AddHandler(const std::string& misc_name,
                                                           const std::string& cow_device_path,
                                                           const std::string& backing_device) {
    auto snapuserd = std::make_unique<Snapuserd>(misc_name, cow_device_path, backing_device);
    if (!snapuserd->InitCowDevice()) {
        LOG(ERROR) << "Failed to initialize Snapuserd";
        return nullptr;
    }

    auto handler = std::make_shared<DmUserHandler>(std::move(snapuserd));
    {
        std::lock_guard<std::mutex> lock(lock_);
        if (FindHandler(&lock, misc_name) != dm_users_.end()) {
            LOG(ERROR) << "Handler already exists: " << misc_name;
            return nullptr;
        }
        dm_users_.push_back(handler);
    }
    return handler;
}

bool SnapuserdServer::StartHandler(const std::shared_ptr<DmUserHandler>& handler) {
    CHECK(!handler->snapuserd()->IsAttached());

    if (!handler->snapuserd()->InitBackingAndControlDevice()) {
        LOG(ERROR) << "Failed to initialize control device: " << handler->GetMiscName();
        return false;
    }

    handler->thread() = std::thread(std::bind(&SnapuserdServer::RunThread, this, handler));
    return true;
}

auto SnapuserdServer::FindHandler(std::lock_guard<std::mutex>* proof_of_lock,
                                  const std::string& misc_name) -> HandlerList::iterator {
    CHECK(proof_of_lock);
Loading