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 <sys/types.h>
19
20#include <memory>
21#include <optional>
22#include <string>
23
27
28namespace cuttlefish {
29
30enum class InUseState : char {
31 kInUse = 'I',
32 kNotInUse = 'N',
33};
34
35// This class is not thread safe.
36class LockFile {
37 friend class LockFileManager;
38
39 /* b/303724170
40 *
41 * Unfortunately, the LockFile implementation forgot to release
42 * the Flock() it got on destruction. Also, unfortunately, the
43 * LockFile is being copied as we have used std::optional
44 * here and there, which means LockFile objects are copied.
45 *
46 * The goal is to disable LockFile(const LockFile&) and the
47 * copy assignment operator. For now, however, we add to LockFile
48 * a shared_ptr of this class, so that at the very last LockFile
49 * object for a given lock is destroyed, the LockFileReleaser will
50 * releases the lock.
51 *
52 * Must be created only in the LockFile constructors
53 */
55 public:
56 LockFileReleaser(const SharedFD& fd, const std::string& lock_file_path);
58
59 private:
61 const std::string lock_file_path_; // for error message
62 };
63
64 public:
65 const auto& LockFilePath() const { return lock_file_path_; }
67 // Returns the UID recorded with kInUse, or nullopt if unused or unattributed.
70
71 private:
72 LockFile(SharedFD fd, const std::string& lock_file_path);
73
75 const std::string lock_file_path_;
76 std::shared_ptr<LockFileReleaser> lock_file_lock_releaser_;
77};
78
80 public:
81 LockFileManager() = default;
82
83 Result<LockFile> AcquireLock(const std::string& lock_file_path);
84
86 const std::string& lock_file_path);
87
88 static Result<Fd> OpenLockFile(const std::string& file_path);
89};
90
91} // namespace cuttlefish
Definition: lock_file.h:79
Result< std::optional< LockFile > > TryAcquireLock(const std::string &lock_file_path)
Definition: lock_file.cpp:124
Result< LockFile > AcquireLock(const std::string &lock_file_path)
Definition: lock_file.cpp:117
static Result< Fd > OpenLockFile(const std::string &file_path)
Definition: lock_file.cpp:109
Definition: lock_file.h:54
const std::string lock_file_path_
Definition: lock_file.h:61
LockFileReleaser(const SharedFD &fd, const std::string &lock_file_path)
Definition: lock_file.cpp:55
~LockFileReleaser()
Definition: lock_file.cpp:59
SharedFD flocked_file_fd_
Definition: lock_file.h:60
Definition: lock_file.h:36
const auto & LockFilePath() const
Definition: lock_file.h:65
const std::string lock_file_path_
Definition: lock_file.h:75
LockFile(SharedFD fd, const std::string &lock_file_path)
Definition: lock_file.cpp:72
Result< InUseState > Status() const
Definition: lock_file.cpp:78
Result< std::optional< uid_t > > HolderUid() const
Definition: lock_file.cpp:92
std::shared_ptr< LockFileReleaser > lock_file_lock_releaser_
Definition: lock_file.h:76
SharedFD fd_
Definition: lock_file.h:74
Definition: shared_fd.h:125
Definition: alloc_driver.h:21
InUseState
Definition: lock_file.h:30
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30