Merge "snapuserd: Refactor client to allow persistent connections." am: 45662c8941 am: cc747f8500

Original change: https://android-review.googlesource.com/c/platform/system/core/+/1468411

Change-Id: I86a847218af3a307ee5babdf6961ceaa55901223
This commit is contained in:
David Anderson 2020-10-23 23:50:15 +00:00 committed by Automerger Merge Worker
commit 140feb62f6
3 changed files with 112 additions and 175 deletions

View file

@ -65,7 +65,7 @@ class SnapuserdTest : public ::testing::Test {
product_a_ = std::make_unique<TemporaryFile>(path); product_a_ = std::make_unique<TemporaryFile>(path);
ASSERT_GE(product_a_->fd, 0) << strerror(errno); ASSERT_GE(product_a_->fd, 0) << strerror(errno);
size_ = 100_MiB; size_ = 1_MiB;
} }
void TearDown() override { void TearDown() override {
@ -123,7 +123,7 @@ class SnapuserdTest : public ::testing::Test {
} }
void TestIO(unique_fd& snapshot_fd, std::unique_ptr<uint8_t[]>& buffer); void TestIO(unique_fd& snapshot_fd, std::unique_ptr<uint8_t[]>& buffer);
SnapuserdClient client_; std::unique_ptr<SnapuserdClient> client_;
}; };
void SnapuserdTest::Init() { void SnapuserdTest::Init() {
@ -151,12 +151,12 @@ void SnapuserdTest::Init() {
offset += 1_MiB; offset += 1_MiB;
} }
for (size_t j = 0; j < (800_MiB / 1_MiB); j++) { for (size_t j = 0; j < (8_MiB / 1_MiB); j++) {
ASSERT_EQ(ReadFullyAtOffset(rnd_fd, (char*)random_buffer.get(), 1_MiB, 0), true); ASSERT_EQ(ReadFullyAtOffset(rnd_fd, (char*)random_buffer.get(), 1_MiB, 0), true);
ASSERT_EQ(android::base::WriteFully(system_a_->fd, random_buffer.get(), 1_MiB), true); ASSERT_EQ(android::base::WriteFully(system_a_->fd, random_buffer.get(), 1_MiB), true);
} }
for (size_t j = 0; j < (800_MiB / 1_MiB); j++) { for (size_t j = 0; j < (8_MiB / 1_MiB); j++) {
ASSERT_EQ(ReadFullyAtOffset(rnd_fd, (char*)random_buffer.get(), 1_MiB, 0), true); ASSERT_EQ(ReadFullyAtOffset(rnd_fd, (char*)random_buffer.get(), 1_MiB, 0), true);
ASSERT_EQ(android::base::WriteFully(product_a_->fd, random_buffer.get(), 1_MiB), true); ASSERT_EQ(android::base::WriteFully(product_a_->fd, random_buffer.get(), 1_MiB), true);
} }
@ -297,18 +297,18 @@ void SnapuserdTest::CreateProductDmUser(std::unique_ptr<TemporaryFile>& cow) {
} }
void SnapuserdTest::StartSnapuserdDaemon() { void SnapuserdTest::StartSnapuserdDaemon() {
int ret; ASSERT_TRUE(EnsureSnapuserdStarted());
ret = client_.StartSnapuserd(); client_ = SnapuserdClient::Connect(kSnapuserdSocket, 5s);
ASSERT_EQ(ret, 0); ASSERT_NE(client_, nullptr);
ret = client_.InitializeSnapuserd(cow_system_->path, system_a_loop_->device(), bool ok = client_->InitializeSnapuserd(cow_system_->path, system_a_loop_->device(),
GetSystemControlPath()); GetSystemControlPath());
ASSERT_EQ(ret, 0); ASSERT_TRUE(ok);
ret = client_.InitializeSnapuserd(cow_product_->path, product_a_loop_->device(), ok = client_->InitializeSnapuserd(cow_product_->path, product_a_loop_->device(),
GetProductControlPath()); GetProductControlPath());
ASSERT_EQ(ret, 0); ASSERT_TRUE(ok);
} }
void SnapuserdTest::CreateSnapshotDevices() { void SnapuserdTest::CreateSnapshotDevices() {
@ -464,10 +464,6 @@ TEST_F(SnapuserdTest, ReadWrite) {
{cow_system_1_->path, system_a_loop_->device(), GetSystemControlPath()}, {cow_system_1_->path, system_a_loop_->device(), GetSystemControlPath()},
{cow_product_1_->path, product_a_loop_->device(), GetProductControlPath()}}; {cow_product_1_->path, product_a_loop_->device(), GetProductControlPath()}};
// Start the second stage deamon and send the devices information through
// vector.
ASSERT_EQ(client_.RestartSnapuserd(vec), 0);
// TODO: This is not switching snapshot device but creates a new table; // TODO: This is not switching snapshot device but creates a new table;
// Second stage daemon will be ready to serve the IO request. From now // Second stage daemon will be ready to serve the IO request. From now
// onwards, we can go ahead and shutdown the first stage daemon // onwards, we can go ahead and shutdown the first stage daemon
@ -476,9 +472,6 @@ TEST_F(SnapuserdTest, ReadWrite) {
DeleteDmUser(cow_system_, "system-snapshot"); DeleteDmUser(cow_system_, "system-snapshot");
DeleteDmUser(cow_product_, "product-snapshot"); DeleteDmUser(cow_product_, "product-snapshot");
// Stop the first stage daemon
ASSERT_EQ(client_.StopSnapuserd(true), 0);
// Test the IO again with the second stage daemon // Test the IO again with the second stage daemon
snapshot_fd.reset(open("/dev/block/mapper/system-snapshot-1", O_RDONLY)); snapshot_fd.reset(open("/dev/block/mapper/system-snapshot-1", O_RDONLY));
ASSERT_TRUE(snapshot_fd > 0); ASSERT_TRUE(snapshot_fd > 0);
@ -494,7 +487,7 @@ TEST_F(SnapuserdTest, ReadWrite) {
DeleteDmUser(cow_product_1_, "product-snapshot-1"); DeleteDmUser(cow_product_1_, "product-snapshot-1");
// Stop the second stage daemon // Stop the second stage daemon
ASSERT_EQ(client_.StopSnapuserd(false), 0); ASSERT_TRUE(client_->StopSnapuserd());
} }
} // namespace snapshot } // namespace snapshot

View file

@ -14,49 +14,45 @@
#pragma once #pragma once
#include <chrono>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <thread> #include <thread>
#include <vector> #include <vector>
#include <android-base/unique_fd.h>
namespace android { namespace android {
namespace snapshot { namespace snapshot {
static constexpr uint32_t PACKET_SIZE = 512; static constexpr uint32_t PACKET_SIZE = 512;
static constexpr uint32_t MAX_CONNECT_RETRY_COUNT = 10;
static constexpr char kSnapuserdSocketFirstStage[] = "snapuserd_first_stage"; static constexpr char kSnapuserdSocketFirstStage[] = "snapuserd_first_stage";
static constexpr char kSnapuserdSocket[] = "snapuserd"; static constexpr char kSnapuserdSocket[] = "snapuserd";
// Ensure that the second-stage daemon for snapuserd is running.
bool EnsureSnapuserdStarted();
class SnapuserdClient { class SnapuserdClient {
private: private:
int sockfd_ = 0; android::base::unique_fd sockfd_;
int Sendmsg(const char* msg, size_t size); bool Sendmsg(const std::string& msg);
std::string Receivemsg(); std::string Receivemsg();
int StartSnapuserdaemon(std::string socketname);
bool ConnectToServerSocket(std::string socketname);
bool ConnectToServer();
void DisconnectFromServer() { close(sockfd_); } bool ValidateConnection();
std::string GetSocketNameFirstStage() {
static std::string snapd_one("snapdone");
return snapd_one;
}
std::string GetSocketNameSecondStage() {
static std::string snapd_two("snapdtwo");
return snapd_two;
}
public: public:
int StartSnapuserd(); explicit SnapuserdClient(android::base::unique_fd&& sockfd);
int StopSnapuserd(bool firstStageDaemon);
static std::unique_ptr<SnapuserdClient> Connect(const std::string& socket_name,
std::chrono::milliseconds timeout_ms);
bool StopSnapuserd();
int RestartSnapuserd(std::vector<std::vector<std::string>>& vec); int RestartSnapuserd(std::vector<std::vector<std::string>>& vec);
int InitializeSnapuserd(std::string cow_device, std::string backing_device, bool InitializeSnapuserd(const std::string& cow_device, const std::string& backing_device,
std::string control_device); const std::string& control_device);
}; };
} // namespace snapshot } // namespace snapshot

View file

@ -29,72 +29,98 @@
#include <chrono> #include <chrono>
#include <android-base/logging.h> #include <android-base/logging.h>
#include <android-base/properties.h>
#include <libsnapshot/snapuserd_client.h> #include <libsnapshot/snapuserd_client.h>
namespace android { namespace android {
namespace snapshot { namespace snapshot {
bool SnapuserdClient::ConnectToServerSocket(std::string socketname) { using namespace std::chrono_literals;
sockfd_ = 0; using android::base::unique_fd;
sockfd_ = bool EnsureSnapuserdStarted() {
socket_local_client(socketname.c_str(), ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); if (android::base::GetProperty("init.svc.snapuserd", "") == "running") {
if (sockfd_ < 0) { return true;
LOG(ERROR) << "Failed to connect to " << socketname;
return false;
} }
std::string msg = "query"; android::base::SetProperty("ctl.start", "snapuserd");
if (!android::base::WaitForProperty("init.svc.snapuserd", "running", 10s)) {
LOG(ERROR) << "Timed out waiting for snapuserd to start.";
return false;
}
return true;
}
int sendRet = Sendmsg(msg.c_str(), msg.size()); SnapuserdClient::SnapuserdClient(android::base::unique_fd&& sockfd) : sockfd_(std::move(sockfd)) {}
if (sendRet < 0) {
LOG(ERROR) << "Failed to send query message to snapuserd daemon with socket " << socketname; static inline bool IsRetryErrno() {
DisconnectFromServer(); return errno == ECONNREFUSED || errno == EINTR;
}
std::unique_ptr<SnapuserdClient> SnapuserdClient::Connect(const std::string& socket_name,
std::chrono::milliseconds timeout_ms) {
unique_fd fd;
auto start = std::chrono::steady_clock::now();
while (true) {
fd.reset(socket_local_client(socket_name.c_str(), ANDROID_SOCKET_NAMESPACE_RESERVED,
SOCK_STREAM));
if (fd >= 0) break;
if (fd < 0 && !IsRetryErrno()) {
PLOG(ERROR) << "connect failed: " << socket_name;
return nullptr;
}
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start);
if (elapsed >= timeout_ms) {
LOG(ERROR) << "Timed out connecting to snapuserd socket: " << socket_name;
return nullptr;
}
std::this_thread::sleep_for(100ms);
}
auto client = std::make_unique<SnapuserdClient>(std::move(fd));
if (!client->ValidateConnection()) {
return nullptr;
}
return client;
}
bool SnapuserdClient::ValidateConnection() {
if (!Sendmsg("query")) {
return false; return false;
} }
std::string str = Receivemsg(); std::string str = Receivemsg();
if (str.find("fail") != std::string::npos) {
LOG(ERROR) << "Failed to receive message from snapuserd daemon with socket " << socketname;
DisconnectFromServer();
return false;
}
// If the daemon is passive then fallback to secondary active daemon. Daemon // If the daemon is passive then fallback to secondary active daemon. Daemon
// is passive during transition phase. Please see RestartSnapuserd() // is passive during transition phase. Please see RestartSnapuserd()
if (str.find("passive") != std::string::npos) { if (str.find("passive") != std::string::npos) {
LOG(DEBUG) << "Snapuserd is passive with socket " << socketname; LOG(ERROR) << "Snapuserd is terminating";
DisconnectFromServer();
return false; return false;
} }
CHECK(str.find("active") != std::string::npos); if (str != "active") {
LOG(ERROR) << "Received failure querying daemon";
return false;
}
return true; return true;
} }
bool SnapuserdClient::ConnectToServer() { bool SnapuserdClient::Sendmsg(const std::string& msg) {
if (ConnectToServerSocket(GetSocketNameFirstStage())) return true; ssize_t numBytesSent = TEMP_FAILURE_RETRY(send(sockfd_, msg.data(), msg.size(), 0));
if (ConnectToServerSocket(GetSocketNameSecondStage())) return true;
return false;
}
int SnapuserdClient::Sendmsg(const char* msg, size_t size) {
int numBytesSent = TEMP_FAILURE_RETRY(send(sockfd_, msg, size, 0));
if (numBytesSent < 0) { if (numBytesSent < 0) {
LOG(ERROR) << "Send failed " << strerror(errno); PLOG(ERROR) << "Send failed";
return -1; return false;
} }
if ((uint)numBytesSent < size) { if ((size_t)numBytesSent < msg.size()) {
LOG(ERROR) << "Partial data sent " << strerror(errno); LOG(ERROR) << "Partial data sent, expected " << msg.size() << " bytes, sent "
return -1; << numBytesSent;
return false;
} }
return true;
return 0;
} }
std::string SnapuserdClient::Receivemsg() { std::string SnapuserdClient::Receivemsg() {
@ -127,98 +153,33 @@ std::string SnapuserdClient::Receivemsg() {
return msgStr; return msgStr;
} }
int SnapuserdClient::StopSnapuserd(bool firstStageDaemon) { bool SnapuserdClient::StopSnapuserd() {
if (firstStageDaemon) { if (!Sendmsg("stop")) {
sockfd_ = socket_local_client(GetSocketNameFirstStage().c_str(),
ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
if (sockfd_ < 0) {
LOG(ERROR) << "Failed to connect to " << GetSocketNameFirstStage();
return -1;
}
} else {
if (!ConnectToServer()) {
LOG(ERROR) << "Failed to connect to socket " << GetSocketNameSecondStage();
return -1;
}
}
std::string msg = "stop";
int sendRet = Sendmsg(msg.c_str(), msg.size());
if (sendRet < 0) {
LOG(ERROR) << "Failed to send stop message to snapuserd daemon"; LOG(ERROR) << "Failed to send stop message to snapuserd daemon";
return -1; return false;
} }
DisconnectFromServer(); sockfd_ = {};
return true;
return 0;
} }
int SnapuserdClient::StartSnapuserdaemon(std::string socketname) { bool SnapuserdClient::InitializeSnapuserd(const std::string& cow_device,
int retry_count = 0; const std::string& backing_device,
const std::string& control_device) {
if (fork() == 0) {
const char* argv[] = {"/system/bin/snapuserd", socketname.c_str(), nullptr};
if (execv(argv[0], const_cast<char**>(argv))) {
LOG(ERROR) << "Failed to exec snapuserd daemon";
return -1;
}
}
// snapuserd is a daemon and will never exit; parent can't wait here
// to get the return code. Since Snapuserd starts the socket server,
// give it some time to fully launch.
//
// Try to connect to server to verify snapuserd server is started
while (retry_count < MAX_CONNECT_RETRY_COUNT) {
if (!ConnectToServer()) {
retry_count++;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
} else {
close(sockfd_);
return 0;
}
}
LOG(ERROR) << "Failed to start snapuserd daemon";
return -1;
}
int SnapuserdClient::StartSnapuserd() {
if (StartSnapuserdaemon(GetSocketNameFirstStage()) < 0) return -1;
return 0;
}
int SnapuserdClient::InitializeSnapuserd(std::string cow_device, std::string backing_device,
std::string control_device) {
int ret = 0;
if (!ConnectToServer()) {
LOG(ERROR) << "Failed to connect to server ";
return -1;
}
std::string msg = "start," + cow_device + "," + backing_device + "," + control_device; std::string msg = "start," + cow_device + "," + backing_device + "," + control_device;
if (!Sendmsg(msg)) {
ret = Sendmsg(msg.c_str(), msg.size());
if (ret < 0) {
LOG(ERROR) << "Failed to send message " << msg << " to snapuserd daemon"; LOG(ERROR) << "Failed to send message " << msg << " to snapuserd daemon";
return -1; return false;
} }
std::string str = Receivemsg(); std::string str = Receivemsg();
if (str != "success") {
if (str.find("fail") != std::string::npos) {
LOG(ERROR) << "Failed to receive ack for " << msg << " from snapuserd daemon"; LOG(ERROR) << "Failed to receive ack for " << msg << " from snapuserd daemon";
return -1; return false;
} }
DisconnectFromServer();
LOG(DEBUG) << "Snapuserd daemon initialized with " << msg; LOG(DEBUG) << "Snapuserd daemon initialized with " << msg;
return 0; return true;
} }
/* /*
@ -254,18 +215,8 @@ int SnapuserdClient::InitializeSnapuserd(std::string cow_device, std::string bac
* *
*/ */
int SnapuserdClient::RestartSnapuserd(std::vector<std::vector<std::string>>& vec) { int SnapuserdClient::RestartSnapuserd(std::vector<std::vector<std::string>>& vec) {
// Connect to first-stage daemon and send a terminate-request control
// message. This will not terminate the daemon but will mark the daemon as
// passive.
if (!ConnectToServer()) {
LOG(ERROR) << "Failed to connect to server ";
return -1;
}
std::string msg = "terminate-request"; std::string msg = "terminate-request";
if (!Sendmsg(msg)) {
int sendRet = Sendmsg(msg.c_str(), msg.size());
if (sendRet < 0) {
LOG(ERROR) << "Failed to send message " << msg << " to snapuserd daemon"; LOG(ERROR) << "Failed to send message " << msg << " to snapuserd daemon";
return -1; return -1;
} }
@ -279,16 +230,13 @@ int SnapuserdClient::RestartSnapuserd(std::vector<std::vector<std::string>>& vec
CHECK(str.find("success") != std::string::npos); CHECK(str.find("success") != std::string::npos);
DisconnectFromServer();
// Start the new daemon // Start the new daemon
if (StartSnapuserdaemon(GetSocketNameSecondStage()) < 0) { if (!EnsureSnapuserdStarted()) {
LOG(ERROR) << "Failed to start new daemon at socket " << GetSocketNameSecondStage(); LOG(ERROR) << "Failed to start new daemon";
return -1; return -1;
} }
LOG(DEBUG) << "Second stage Snapuserd daemon created successfully at socket " LOG(DEBUG) << "Second stage Snapuserd daemon created successfully";
<< GetSocketNameSecondStage();
// Vector contains all the device information to be passed to the new // Vector contains all the device information to be passed to the new
// daemon. Note that the caller can choose to initialize separately // daemon. Note that the caller can choose to initialize separately