Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion base/cvd/cuttlefish/host/commands/cvd/instances/lock/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("//:build_variables.bzl", "COPTS")
load("//cuttlefish/bazel:rules.bzl", "cf_cc_library")
load("//cuttlefish/bazel:rules.bzl", "cf_cc_library", "cf_cc_test")

package(
default_visibility = ["//:android_cuttlefish"],
Expand Down Expand Up @@ -33,3 +33,16 @@ cf_cc_library(
"@android_system_libbase//:file_logging",
],
)

cf_cc_test(
name = "instance_lock_test",
srcs = ["instance_lock_test.cpp"],
deps = [
":lock",
"//cuttlefish/files:file_exists",
"//cuttlefish/files:recursively_remove_directory",
"//cuttlefish/result",
"//cuttlefish/result:result_matchers",
"@abseil-cpp//absl/strings",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "cuttlefish/host/commands/cvd/instances/lock/instance_lock.h"

#include <sys/file.h>
#include <unistd.h>

#include <optional>
#include <sstream>
Expand All @@ -36,6 +37,10 @@ InstanceLockFile::InstanceLockFile(LockFile&& lock_file,

unsigned InstanceLockFile::Instance() const { return instance_num_; }

const std::string& InstanceLockFile::LockFilePath() const {
return lock_file_.LockFilePath();
}

Result<InUseState> InstanceLockFile::Status() const {
auto in_use_state = CF_EXPECT(lock_file_.Status());
return in_use_state;
Expand All @@ -46,6 +51,10 @@ Result<void> InstanceLockFile::Status(InUseState state) {
return {};
}

Result<uid_t> InstanceLockFile::OwnerUid() const {
return CF_EXPECT(lock_file_.OwnerUid());
}

InstanceLockFileManager::InstanceLockFileManager(
std::string instance_locks_path)
: instance_locks_path_(std::move(instance_locks_path)) {};
Expand All @@ -59,18 +68,97 @@ Result<std::string> InstanceLockFileManager::LockFilePath(
return path.str();
}

Result<std::optional<InstanceLockFile>>
InstanceLockFileManager::AcquireOwnedLockIfPresent(unsigned instance_num) {
const auto lock_file_path = CF_EXPECT(LockFilePath(instance_num));
Result<uid_t> path_owner = FileOwner(lock_file_path);
if (!path_owner.has_value() || *path_owner != geteuid()) {
return std::nullopt;
}
InstanceLockFile lock = CF_EXPECT(AcquireLock(instance_num));
uid_t fd_owner = CF_EXPECT(lock.OwnerUid());
if (fd_owner != geteuid()) {
return std::nullopt;
}
return lock;
}

Result<void> InstanceLockFileManager::ReleaseHeldLock(InstanceLockFile& lock) {
uid_t fd_owner = CF_EXPECT(lock.OwnerUid());
if (fd_owner != geteuid()) {
return {};
}
CF_EXPECT(lock.Status(InUseState::kNotInUse));
CF_EXPECT(RemoveFile(lock.LockFilePath()), StrError());
return {};
}

Result<void> InstanceLockFileManager::ReleaseInstanceLock(
unsigned instance_num) {
std::optional<InstanceLockFile> lock =
CF_EXPECT(AcquireOwnedLockIfPresent(instance_num));
if (!lock.has_value()) {
return {};
}
CF_EXPECT(ReleaseHeldLock(*lock));
return {};
}

Result<void> InstanceLockFileManager::RemoveLockFile(unsigned instance_num) {
const auto lock_file_path = CF_EXPECT(LockFilePath(instance_num));
CF_EXPECT(RemoveFile(lock_file_path), StrError());
return {};
}

Result<InstanceLockFile> InstanceLockFileManager::AcquireCurrentUserLock(
unsigned instance_num) {
constexpr int kMaxAttempts = 16;
for (int attempt = 0; attempt < kMaxAttempts; ++attempt) {
InstanceLockFile lock = CF_EXPECT(AcquireLock(instance_num));
InUseState status = CF_EXPECT(lock.Status());
uid_t owner = CF_EXPECT(lock.OwnerUid());
if (owner == geteuid()) {
return lock;
}
if (status == InUseState::kInUse) {
return CF_ERRF(
"Instance ID {} is currently in use by another user (uid {})",
instance_num, owner);
}
// Stale `kNotInUse` file owned by another UID: unlink while holding
// `LOCK_EX` so the next iteration creates a fresh inode owned by geteuid().
CF_EXPECTF(RemoveFile(lock.LockFilePath()),
"Failed to replace stale lock file '{}': {}",
lock.LockFilePath(), StrError());
}
return CF_ERRF("Failed to acquire instance lock {} for current user",
instance_num);
}

Result<InstanceLockFile> InstanceLockFileManager::AcquireUnusedLock() {
for (unsigned i = 1;; i++) {
auto lock = CF_EXPECT(TryAcquireLock(i));
if (lock && CF_EXPECT(lock->Status()) == InUseState::kNotInUse) {
if (!lock) {
continue;
}
if (CF_EXPECT(lock->Status()) != InUseState::kNotInUse) {
continue;
}
if (CF_EXPECT(lock->OwnerUid()) == geteuid()) {
return *lock;
}
// Replace stale `kNotInUse` inode owned by another UID so `st_uid` matches
// `geteuid()`. If unlinking fails (e.g. sticky custom dir), skip to i + 1.
if (!RemoveFile(lock->LockFilePath()).has_value()) {
continue;
}
lock.reset();
auto fresh_lock = CF_EXPECT(TryAcquireLock(i));
if (fresh_lock &&
CF_EXPECT(fresh_lock->Status()) == InUseState::kNotInUse &&
CF_EXPECT(fresh_lock->OwnerUid()) == geteuid()) {
return *fresh_lock;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
#pragma once

#include <sys/types.h>

#include <optional>
#include <string>

Expand All @@ -28,8 +30,10 @@ class InstanceLockFile {

public:
unsigned Instance() const;
const std::string& LockFilePath() const;
Result<InUseState> Status() const;
Result<void> Status(InUseState);
Result<uid_t> OwnerUid() const;

private:
InstanceLockFile(LockFile&& lock_file, unsigned instance_num);
Expand All @@ -42,7 +46,24 @@ class InstanceLockFileManager {
InstanceLockFileManager(std::string instance_locks_path);

Result<InstanceLockFile> AcquireLock(unsigned instance_num);
// Acquires `instance_num` for the current user (`geteuid()`): fails if
// marked `kInUse` by another UID, and replaces any stale `kNotInUse` inode
// owned by another UID with a fresh inode owned by `geteuid()`.
Result<InstanceLockFile> AcquireCurrentUserLock(unsigned instance_num);
Result<InstanceLockFile> AcquireUnusedLock();
Result<std::optional<InstanceLockFile>> TryAcquireLock(unsigned instance_num);

// Acquires `instance_num` if its lock file exists and is owned by `geteuid()`
// so the caller can release it while holding the lock.
Result<std::optional<InstanceLockFile>> AcquireOwnedLockIfPresent(
unsigned instance_num);

// Resets and removes an acquired lock file while holding its exclusive flock.
Result<void> ReleaseHeldLock(InstanceLockFile& lock);

// Releases `instance_num` if its lock file exists and is owned by
// `geteuid()`.
Result<void> ReleaseInstanceLock(unsigned instance_num);

// TODO: This routine should be removed and replaced with allocd
// The caller must check if the instance_num belongs to the user, before
Expand All @@ -51,7 +72,6 @@ class InstanceLockFileManager {

private:
Result<std::string> LockFilePath(unsigned instance_num);
Result<std::optional<InstanceLockFile>> TryAcquireLock(unsigned instance_num);

std::string instance_locks_path_;
LockFileManager lock_file_manager_;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright (C) 2026 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 "cuttlefish/host/commands/cvd/instances/lock/instance_lock.h"

#include <stdlib.h>
#include <unistd.h>

#include <atomic>
#include <optional>
#include <string>
#include <thread>
#include <vector>

#include "absl/strings/str_cat.h"
#include "gmock/gmock-matchers.h"
#include "gtest/gtest.h"

#include "cuttlefish/files/file_exists.h"
#include "cuttlefish/files/recursively_remove_directory.h"
#include "cuttlefish/host/commands/cvd/instances/lock/lock_file.h"
#include "cuttlefish/result/result.h"
#include "cuttlefish/result/result_matchers.h"

namespace cuttlefish {
namespace {

class TempLocksDir {
public:
TempLocksDir() {
const char* tmpdir = getenv("TEST_TMPDIR");
std::string base =
(tmpdir != nullptr && tmpdir[0] != '\0') ? tmpdir : "/tmp";
std::string tmpl = absl::StrCat(base, "/cf_lock_test.XXXXXX");
char* created = mkdtemp(tmpl.data());
if (created != nullptr) {
dir_ = absl::StrCat(created, "/");
}
}

~TempLocksDir() {
if (!dir_.empty()) {
(void)RecursivelyRemoveDirectory(dir_);
}
}

const std::string& Dir() const { return dir_; }

private:
std::string dir_;
};

TEST(InstanceLockTest, AcquireCurrentUserAndReleaseHeldLock) {
TempLocksDir tmp;
ASSERT_FALSE(tmp.Dir().empty());
InstanceLockFileManager mgr(tmp.Dir());

Result<InstanceLockFile> lock = mgr.AcquireCurrentUserLock(42);
ASSERT_THAT(lock, IsOk());
EXPECT_EQ(lock->Instance(), 42u);
ASSERT_THAT(lock->OwnerUid(), IsOkAndValue(geteuid()));
ASSERT_THAT(lock->Status(), IsOkAndValue(InUseState::kNotInUse));
ASSERT_THAT(lock->Status(InUseState::kInUse), IsOk());

// Non-blocking `TryAcquireLock` from another manager fails while held.
InstanceLockFileManager other_mgr(tmp.Dir());
Result<std::optional<InstanceLockFile>> contended =
other_mgr.TryAcquireLock(42);
ASSERT_THAT(contended, IsOk());
EXPECT_FALSE(contended->has_value());

std::string lock_path = lock->LockFilePath();
EXPECT_TRUE(FileExists(lock_path));
ASSERT_THAT(mgr.ReleaseHeldLock(*lock), IsOk());
EXPECT_FALSE(FileExists(lock_path));
}

TEST(InstanceLockTest, ConcurrentAcquireAndReleaseNeverSplitBrain) {
TempLocksDir tmp;
ASSERT_FALSE(tmp.Dir().empty());

constexpr int kNumThreads = 8;
constexpr int kIterations = 40;
std::atomic<int> active_holders{0};
std::atomic<bool> overlap_detected{false};

std::vector<std::thread> threads;
threads.reserve(kNumThreads);
for (int t = 0; t < kNumThreads; ++t) {
threads.emplace_back([&]() {
InstanceLockFileManager mgr(tmp.Dir());
for (int i = 0; i < kIterations; ++i) {
Result<InstanceLockFile> lock = mgr.AcquireCurrentUserLock(7);
ASSERT_THAT(lock, IsOk());
int prev = active_holders.fetch_add(1, std::memory_order_seq_cst);
if (prev != 0) {
overlap_detected.store(true, std::memory_order_seq_cst);
}
std::this_thread::yield();
active_holders.fetch_sub(1, std::memory_order_seq_cst);
if ((i % 2) == 0) {
EXPECT_THAT(mgr.ReleaseHeldLock(*lock), IsOk());
}
}
});
}
for (std::thread& th : threads) {
th.join();
}

EXPECT_FALSE(overlap_detected.load());
}

TEST(InstanceLockTest, AcquireUnusedLockSkipsInUseIds) {
TempLocksDir tmp;
ASSERT_FALSE(tmp.Dir().empty());
InstanceLockFileManager mgr(tmp.Dir());

{
// Drop flock at scope exit while leaving `1` marked `kInUse`.
Result<InstanceLockFile> lock1 = mgr.AcquireCurrentUserLock(1);
ASSERT_THAT(lock1, IsOk());
ASSERT_THAT(lock1->Status(InUseState::kInUse), IsOk());
}

Result<InstanceLockFile> unused = mgr.AcquireUnusedLock();
ASSERT_THAT(unused, IsOk());
EXPECT_EQ(unused->Instance(), 2u);
ASSERT_THAT(mgr.ReleaseInstanceLock(1), IsOk());
EXPECT_FALSE(FileExists(absl::StrCat(tmp.Dir(), "local-instance-1.lock")));
}

} // namespace
} // namespace cuttlefish
Loading
Loading