Android-cuttlefish cvd tool
lock_file.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
18#include <memory>
19#include <optional>
20#include <set>
21#include <string>
22
25
26namespace cuttlefish {
27
28enum class InUseState : char {
29 kInUse = 'I',
30 kNotInUse = 'N',
31};
32
33// Replicates tempfile.gettempdir() in Python
34std::string TempDir();
35
36namespace cvd_impl {
37
38// This class is not thread safe.
39class LockFile {
40 friend class LockFileManager;
41
42 /* b/303724170
43 *
44 * Unfortunately, the LockFile implementation forgot to release
45 * the Flock() it got on destruction. Also, unfortunately, the
46 * LockFile is being copied as we have used std::optional
47 * here and there, which means LockFile objects are copied.
48 *
49 * The goal is to disable LockFile(const LockFile&) and the
50 * copy assignment operator. For now, however, we add to LockFile
51 * a shared_ptr of this class, so that at the very last LockFile
52 * object for a given lock is destroyed, the LockFileReleaser will
53 * releases the lock.
54 *
55 * Must be created only in the LockFile constructors
56 */
58 public:
59 LockFileReleaser(const SharedFD& fd, const std::string& lock_file_path);
61
62 private:
64 const std::string lock_file_path_; // for error message
65 };
66
67 public:
68 const auto& LockFilePath() const { return lock_file_path_; }
71
72 // to put this into a set
73 bool operator<(const LockFile& other) const;
74
75 private:
76 LockFile(SharedFD fd, const std::string& lock_file_path);
77
79 const std::string lock_file_path_;
80 std::shared_ptr<LockFileReleaser> lock_file_lock_releaser_;
81};
82
84 public:
85 LockFileManager() = default;
86
87 Result<LockFile> AcquireLock(const std::string& lock_file_path);
89 const std::set<std::string>& lock_file_paths);
90
92 const std::string& lock_file_path);
94 const std::set<std::string>& lock_file_paths);
95
96 // Best-effort attempt to find a free instance id.
98
99 static Result<SharedFD> OpenLockFile(const std::string& file_path);
100};
101
102} // namespace cvd_impl
103} // namespace cuttlefish
Definition: expected.h:86
Definition: shared_fd.h:129
Definition: lock_file.h:83
Result< std::optional< LockFile > > TryAcquireUnusedLock()
Result< LockFile > AcquireLock(const std::string &lock_file_path)
Definition: lock_file.cpp:119
static Result< SharedFD > OpenLockFile(const std::string &file_path)
Definition: lock_file.cpp:103
Result< std::set< LockFile > > TryAcquireLocks(const std::set< std::string > &lock_file_paths)
Definition: lock_file.cpp:149
Result< std::optional< LockFile > > TryAcquireLock(const std::string &lock_file_path)
Definition: lock_file.cpp:135
Result< std::set< LockFile > > AcquireLocks(const std::set< std::string > &lock_file_paths)
Definition: lock_file.cpp:126
~LockFileReleaser()
Definition: lock_file.cpp:52
const std::string lock_file_path_
Definition: lock_file.h:64
SharedFD flocked_file_fd_
Definition: lock_file.h:63
LockFileReleaser(const SharedFD &fd, const std::string &lock_file_path)
Definition: lock_file.cpp:48
Definition: lock_file.h:39
Result< InUseState > Status() const
Definition: lock_file.cpp:71
SharedFD fd_
Definition: lock_file.h:78
LockFile(SharedFD fd, const std::string &lock_file_path)
Definition: lock_file.cpp:65
const std::string lock_file_path_
Definition: lock_file.h:79
const auto & LockFilePath() const
Definition: lock_file.h:68
bool operator<(const LockFile &other) const
Definition: lock_file.cpp:90
std::shared_ptr< LockFileReleaser > lock_file_lock_releaser_
Definition: lock_file.h:80
Definition: alloc_utils.cpp:23
InUseState
Definition: lock_file.h:28
std::string TempDir()
Definition: known_paths.cpp:25